From 0f7de056eced8a8fd2035404b267aa7b1a9824b9 Mon Sep 17 00:00:00 2001 From: junfengP <840282629@qq.com> Date: Tue, 25 Feb 2020 00:03:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20anti-crawl=20of=20=E8=89=B2=E8=8A=B1?= =?UTF-8?q?=E5=A0=82=20(#4051)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/dsndsht23/index.js | 27 +++++++++++++++++++++++++-- lib/routes/dsndsht23/pictures.js | 27 +++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lib/routes/dsndsht23/index.js b/lib/routes/dsndsht23/index.js index d7c656f44c..e71ec70b19 100644 --- a/lib/routes/dsndsht23/index.js +++ b/lib/routes/dsndsht23/index.js @@ -23,7 +23,30 @@ module.exports = async (ctx) => { const subformId = subformName in forumIdMaps ? forumIdMaps[subformName] : subformName; const typefilter = ctx.params.type ? `&filter=typeid&typeid=${ctx.params.type}` : ''; const link = `${host}forum.php?mod=forumdisplay&fid=${subformId}${typefilter}`; - const response = await got.get(link); + const headers = { + 'Accept-Encoding': 'gzip, deflate, br', + 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8', + }; + let c; + const response = await got({ + method: 'get', + url: link, + headers: headers, + hooks: { + beforeRedirect: [ + (options, response) => { + const cookie = response.headers['set-cookie']; + if (cookie) { + const cook = cookie.map((c) => c.split(';')[0]).join('; '); + options.headers.Cookie = cook; + c = cook; + options.headers.Referer = response.url; + } + }, + ], + }, + }); + headers.Cookie = c; const $ = cheerio.load(response.data); const list = $('#threadlisttableid tbody[id^=normalthread]') @@ -62,7 +85,7 @@ module.exports = async (ctx) => { return Promise.resolve(JSON.parse(cache)); } - const response = await got.get(itemUrl); + const response = await got.get(itemUrl, { headers: headers }); const $ = cheerio.load(response.data); const postMessage = $("td[id^='postmessage']").slice(0, 1); diff --git a/lib/routes/dsndsht23/pictures.js b/lib/routes/dsndsht23/pictures.js index b942d3cda9..454cf7c607 100644 --- a/lib/routes/dsndsht23/pictures.js +++ b/lib/routes/dsndsht23/pictures.js @@ -14,7 +14,30 @@ module.exports = async (ctx) => { const subforumid = ctx.params.subforumid || 'hrxazp'; const link = `${host}${map[subforumid]}`; - const response = await got.get(link); + const headers = { + 'Accept-Encoding': 'gzip, deflate, br', + 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8', + }; + let c; + const response = await got({ + method: 'get', + url: link, + headers: headers, + hooks: { + beforeRedirect: [ + (options, response) => { + const cookie = response.headers['set-cookie']; + if (cookie) { + const cook = cookie.map((c) => c.split(';')[0]).join('; '); + options.headers.Cookie = cook; + c = cook; + options.headers.Referer = response.url; + } + }, + ], + }, + }); + headers.Cookie = c; const $ = cheerio.load(response.body); const list = $('#threadlisttableid tbody') @@ -52,7 +75,7 @@ module.exports = async (ctx) => { return Promise.resolve(JSON.parse(cache)); } - const response = await got.get(itemUrl); + const response = await got.get(itemUrl, { headers: headers }); const $ = cheerio.load(response.data); const postMessage = $("td[id^='postmessage']").slice(0, 1);