Skip to content

Commit fe27cea

Browse files
author
Taois
committed
feat: 尝试增加底层拦截请求头的ua手段
1 parent be0d97d commit fe27cea

File tree

4 files changed

+184
-8
lines changed

4 files changed

+184
-8
lines changed

libs_drpy/fetchAxios.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,50 @@
44
*/
55
import FormData from 'form-data';
66
import https from "https";
7+
import diagnosticsChannel from 'diagnostics_channel';
8+
9+
let undiciStripUASubscribed = false;
10+
11+
function ensureUndiciStripUASubscription() {
12+
if (undiciStripUASubscribed) {
13+
return;
14+
}
15+
undiciStripUASubscribed = true;
16+
17+
diagnosticsChannel.channel('undici:request:create').subscribe(({request}) => {
18+
if (!request || !Array.isArray(request.headers)) {
19+
return;
20+
}
21+
const headers = request.headers;
22+
23+
let shouldStrip = false;
24+
for (let i = 0; i < headers.length; i += 2) {
25+
const k = headers[i];
26+
if (typeof k === 'string' && k.toLowerCase() === 'x-remove-user-agent') {
27+
shouldStrip = true;
28+
break;
29+
}
30+
}
31+
if (!shouldStrip) {
32+
return;
33+
}
34+
35+
for (let i = 0; i < headers.length; i += 2) {
36+
const k = headers[i];
37+
if (typeof k === 'string' && k.toLowerCase() === 'x-remove-user-agent') {
38+
headers.splice(i, 2);
39+
i -= 2;
40+
}
41+
}
42+
for (let i = 0; i < headers.length; i += 2) {
43+
const k = headers[i];
44+
if (typeof k === 'string' && k.toLowerCase() === 'user-agent') {
45+
headers.splice(i, 2);
46+
i -= 2;
47+
}
48+
}
49+
});
50+
}
751

852
/**
953
* FetchAxios类 - HTTP客户端实现
@@ -71,6 +115,18 @@ class FetchAxios {
71115
finalConfig = await interceptor(finalConfig) || finalConfig;
72116
}
73117

118+
if (finalConfig.headers) {
119+
const headerKeys = Object.keys(finalConfig.headers);
120+
for (const key of headerKeys) {
121+
if (key.toLowerCase() === 'user-agent' && finalConfig.headers[key] === 'RemoveUserAgent') {
122+
delete finalConfig.headers[key];
123+
finalConfig.headers['x-remove-user-agent'] = '1';
124+
ensureUndiciStripUASubscription();
125+
break;
126+
}
127+
}
128+
}
129+
74130
// 拼接查询参数
75131
if (finalConfig.params) {
76132
const query = new URLSearchParams(finalConfig.params).toString();
@@ -300,4 +356,4 @@ export function createHttpsInstance() {
300356
responseType: 'arraybuffer',
301357
httpsAgent: httpsAgent
302358
});
303-
}
359+
}

spider/js/_debug.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// _debug.js
2+
// 测试方法: http://localhost:5757/api/_debug?pwd=dzyyds
3+
var rule = {
4+
title: '_debug',
5+
description: '这是描述',
6+
类型: '测试',
7+
searchUrl: '',
8+
class_parse: async () => {
9+
log(`[${rule.title}] --class_parse--`);
10+
return [
11+
{type_id: '1', type_name: '电影'},
12+
{type_id: '2', type_name: '电视剧'},
13+
{type_id: '3', type_name: '综艺'},
14+
{type_id: '4', type_name: '动漫'},
15+
]
16+
},
17+
预处理: async () => {
18+
log(`[${rule.title}] --预处理--`);
19+
rule.title = '_debug';
20+
},
21+
推荐: async () => {
22+
// return '这是推荐:' + rule.title;
23+
let d = [];
24+
let html = '{}';
25+
html = await request('https://httpbin.org/headers', {
26+
headers: {
27+
'Accept': '*/*',
28+
'User-Agent': ''
29+
}
30+
});
31+
// log(html);
32+
d.push({
33+
title: 'request结果1-传空UA',
34+
content: html.parseX.headers,
35+
});
36+
37+
html = await request('https://httpbin.org/headers', {
38+
headers: {
39+
'Accept': '*/*',
40+
'User-Agent': 'RemoveUserAgent',
41+
}
42+
});
43+
// log(html);
44+
d.push({
45+
title: 'request结果2-不传UA',
46+
content: html.parseX.headers,
47+
});
48+
49+
html = (await req('https://httpbin.org/headers', {
50+
headers: {
51+
'Accept': '*/*',
52+
'User-Agent': 'RemoveUserAgent',
53+
}
54+
})).content;
55+
d.push({
56+
title: 'req结果-不传UA',
57+
content: html.parseX.headers,
58+
});
59+
60+
html = (await req('https://conn.origjoy.com/auth/init?appid=d4eeacc6cec3434fbc8c41608a3056a0&mac=0afa691314fd_a12d4a7c9n12&sn=a12d4a7c9n12&time=1768728113&ver=2.0&vn=4.1.3.03281430&sign=6a1ee16242b93a3ae6492bc55992b691',
61+
{
62+
headers: {
63+
'Accept': '*/*',
64+
'User-Agent': 'RemoveUserAgent',
65+
}
66+
})).content;
67+
d.push({
68+
title: 'req结果-60wmv',
69+
content: html,
70+
});
71+
return d;
72+
},
73+
一级: async () => {
74+
return '这是一级:' + rule.title
75+
},
76+
二级: async () => {
77+
return '这是二级:' + rule.title
78+
},
79+
搜索: async () => {
80+
return ['这是搜索:' + rule.title]
81+
},
82+
lazy: async () => {
83+
return '这是播放:' + rule.title
84+
},
85+
};

spider/js/_lib.request.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
const iconv = require('iconv-lite');
22

3+
function sanitizeUserAgent(headers) {
4+
if (!headers) {
5+
return headers;
6+
}
7+
const keys = Object.keys(headers);
8+
for (const key of keys) {
9+
if (key.toLowerCase() === 'user-agent' && headers[key] === 'RemoveUserAgent') {
10+
delete headers[key];
11+
break;
12+
}
13+
}
14+
return headers;
15+
}
16+
317
async function requestHtml(url, options) {
418
try {
519
let html = (await req(url, options)).content;
@@ -30,15 +44,20 @@ async function getPublicIp() {
3044

3145
async function getHtml(config) {
3246
try {
33-
return await axios.request(typeof config === "string" ? config : {
47+
if (typeof config === "string") {
48+
return await axios.request(config)
49+
}
50+
const cfg = {
3451
url: config.url,
3552
method: config.method || 'GET',
3653
headers: config.headers || {
3754
'User-Agent': PC_UA
3855
},
3956
data: config.data || '',
40-
responseType: config.responseType || '',//'arraybuffer'
41-
})
57+
responseType: config.responseType || ''
58+
};
59+
cfg.headers = sanitizeUserAgent(cfg.headers);
60+
return await axios.request(cfg)
4261
} catch (e) {
4362
return e.response
4463
}
@@ -54,6 +73,7 @@ async function req_(reqUrl, mt, headers, data) {
5473
},
5574
data: data || '',
5675
};
76+
config.headers = sanitizeUserAgent(config.headers);
5777
let res = await axios.request(config);
5878
return res.data;
5979
}
@@ -68,6 +88,7 @@ async function req_encoding(reqUrl, mt, headers, encoding, data) {
6888
data: data || '',
6989
responseType: 'arraybuffer'
7090
};
91+
config.headers = sanitizeUserAgent(config.headers);
7192
let res = await axios.request(config);
7293
if (encoding) {
7394
res.data = iconv.decode(res.data, encoding);
@@ -88,6 +109,7 @@ async function req_proxy(reqUrl, mt, headers, data) {
88109
port: "7890"
89110
}
90111
};
112+
config.headers = sanitizeUserAgent(config.headers);
91113
if (data) {
92114
config.data = data;
93115
}

utils/createAxiosAgent.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,27 @@ export function createAxiosInstance(options = {}) {
3939

4040
const httpsAgent = new https.Agent(httpsAgentOptions);
4141

42-
// 配置 axios 使用代理
4342
const _axios = axios.create({
44-
httpAgent, // 用于 HTTP 请求的代理
45-
httpsAgent, // 用于 HTTPS 请求的代理
43+
httpAgent,
44+
httpsAgent,
45+
});
46+
47+
_axios.interceptors.request.use(config => {
48+
if (config && config.headers) {
49+
const headers = config.headers;
50+
const keys = Object.keys(headers);
51+
for (const key of keys) {
52+
if (key.toLowerCase() === 'user-agent' && headers[key] === 'RemoveUserAgent') {
53+
delete headers[key];
54+
break;
55+
}
56+
}
57+
}
58+
return config;
4659
});
4760

4861
return _axios;
4962
}
5063

5164
// 默认导出
52-
export default createAxiosInstance;
65+
export default createAxiosInstance;

0 commit comments

Comments
 (0)