Merge branch 'request-wrapper'
This commit is contained in:
@@ -1,52 +1,8 @@
|
||||
const logger = require('./logger');
|
||||
const config = require('@/config');
|
||||
const SocksProxyAgent = require('socks-proxy-agent');
|
||||
const tunnel = require('tunnel');
|
||||
const got = require('got');
|
||||
const queryString = require('query-string');
|
||||
|
||||
let agent = null;
|
||||
if (config.proxy && config.proxy.protocol && config.proxy.host && config.proxy.port) {
|
||||
agent = {};
|
||||
const proxyUrl = `${config.proxy.protocol}://${config.proxy.host}:${config.proxy.port}`;
|
||||
|
||||
switch (config.proxy.protocol) {
|
||||
case 'socks':
|
||||
agent.http = new SocksProxyAgent(proxyUrl);
|
||||
agent.https = new SocksProxyAgent(proxyUrl);
|
||||
break;
|
||||
case 'http':
|
||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
|
||||
agent.http = tunnel.httpOverHttp({
|
||||
proxy: {
|
||||
host: config.proxy.host,
|
||||
port: parseInt(config.proxy.port),
|
||||
},
|
||||
});
|
||||
agent.https = tunnel.httpsOverHttp({
|
||||
proxy: {
|
||||
host: config.proxy.host,
|
||||
port: parseInt(config.proxy.port),
|
||||
},
|
||||
});
|
||||
break;
|
||||
case 'https':
|
||||
agent.http = tunnel.httpOverHttps({
|
||||
proxy: {
|
||||
host: config.proxy.host,
|
||||
port: parseInt(config.proxy.port),
|
||||
},
|
||||
});
|
||||
agent.https = tunnel.httpsOverHttps({
|
||||
proxy: {
|
||||
host: config.proxy.host,
|
||||
port: parseInt(config.proxy.port),
|
||||
},
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const custom = got.extend({
|
||||
retry: {
|
||||
retries: config.requestRetry,
|
||||
@@ -85,20 +41,11 @@ const custom = got.extend({
|
||||
options.query = options.query.replace(/([\u4e00-\u9fa5])/g, (str) => encodeURIComponent(str));
|
||||
options.searchParams = options.query; // for Got v11 after
|
||||
}
|
||||
if (agent && new RegExp(config.proxy.url_regex).test(options.href)) {
|
||||
options.agent = agent[options.protocol.slice(0, -1)];
|
||||
|
||||
if (config.proxy.auth) {
|
||||
options.headers['Proxy-Authorization'] = `Basic ${config.proxy.auth}`;
|
||||
}
|
||||
logger.info(`Proxy for ${options.href}`);
|
||||
}
|
||||
},
|
||||
],
|
||||
},
|
||||
headers: {
|
||||
'user-agent': config.ua,
|
||||
'x-app': 'RSSHub',
|
||||
},
|
||||
});
|
||||
custom.all = (list) => Promise.all(list);
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
const config = require('@/config');
|
||||
const SocksProxyAgent = require('socks-proxy-agent');
|
||||
const tunnel = require('tunnel');
|
||||
const logger = require('./logger');
|
||||
const http = require('http');
|
||||
const https = require('https');
|
||||
|
||||
let agent = null;
|
||||
if (config.proxy && config.proxy.protocol && config.proxy.host && config.proxy.port) {
|
||||
agent = {};
|
||||
const proxyUrl = `${config.proxy.protocol}://${config.proxy.host}:${config.proxy.port}`;
|
||||
|
||||
switch (config.proxy.protocol) {
|
||||
case 'socks':
|
||||
agent.http = new SocksProxyAgent(proxyUrl);
|
||||
agent.https = new SocksProxyAgent(proxyUrl);
|
||||
break;
|
||||
case 'http':
|
||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
|
||||
agent.http = tunnel.httpOverHttp({
|
||||
proxy: {
|
||||
host: config.proxy.host,
|
||||
port: parseInt(config.proxy.port),
|
||||
},
|
||||
});
|
||||
agent.https = tunnel.httpsOverHttp({
|
||||
proxy: {
|
||||
host: config.proxy.host,
|
||||
port: parseInt(config.proxy.port),
|
||||
},
|
||||
});
|
||||
break;
|
||||
case 'https':
|
||||
agent.http = tunnel.httpOverHttps({
|
||||
proxy: {
|
||||
host: config.proxy.host,
|
||||
port: parseInt(config.proxy.port),
|
||||
},
|
||||
});
|
||||
agent.https = tunnel.httpsOverHttps({
|
||||
proxy: {
|
||||
host: config.proxy.host,
|
||||
port: parseInt(config.proxy.port),
|
||||
},
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const requestWrapper = (url, options) => {
|
||||
if (agent && new RegExp(config.proxy.url_regex).test(url)) {
|
||||
let agentResult;
|
||||
try {
|
||||
agentResult = agent[(options.protocol || url.match(/(https?:)/)[1]).slice(0, -1)];
|
||||
} catch (error) {
|
||||
agentResult = null;
|
||||
}
|
||||
options.agent = agentResult;
|
||||
|
||||
if (config.proxy.auth) {
|
||||
if (!options.headers) {
|
||||
options.headers = {};
|
||||
}
|
||||
options.headers['Proxy-Authorization'] = `Basic ${config.proxy.auth}`;
|
||||
}
|
||||
logger.info(`Proxy for ${url}`);
|
||||
}
|
||||
let hasUA = false;
|
||||
for (const header in options.headers) {
|
||||
if (header.toLowerCase() === 'user-agent') {
|
||||
hasUA = true;
|
||||
}
|
||||
}
|
||||
if (!hasUA) {
|
||||
if (!options.headers) {
|
||||
options.headers = {};
|
||||
}
|
||||
options.headers['user-agent'] = config.ua;
|
||||
}
|
||||
options.headers['x-app'] = 'RSSHub';
|
||||
};
|
||||
|
||||
const httpWrap = (func) => {
|
||||
const origin = func;
|
||||
return function(url, request) {
|
||||
if (typeof url === 'object') {
|
||||
const req = url;
|
||||
requestWrapper(req.url || req.href || `${req.protocol}//${req.hostname}${req.path}`, req);
|
||||
} else {
|
||||
requestWrapper(url, request);
|
||||
}
|
||||
return origin.apply(this, arguments);
|
||||
};
|
||||
};
|
||||
|
||||
http.get = httpWrap(http.get);
|
||||
https.get = httpWrap(https.get);
|
||||
http.request = httpWrap(http.request);
|
||||
https.request = httpWrap(https.request);
|
||||
@@ -7,7 +7,6 @@ const parser = new Parser({
|
||||
},
|
||||
headers: {
|
||||
'User-Agent': config.ua,
|
||||
'X-APP': 'RSSHub',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user