mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-08 05:59:00 +08:00
support http and https proxy
This commit is contained in:
@@ -2,28 +2,59 @@ const logger = require('./logger');
|
||||
const config = require('../config');
|
||||
const SocksProxyAgent = require('socks-proxy-agent');
|
||||
const axiosRetry = require('axios-retry');
|
||||
const axios = require('axios');
|
||||
const tunnel = require('tunnel');
|
||||
|
||||
let axios = require('axios');
|
||||
if (config.proxy && config.proxy.protocol && typeof config.proxy.protocol === 'string' && config.proxy.protocol.slice(0, 5) === 'socks' && config.proxy.host && config.proxy.port) {
|
||||
// axios closure lead to recursive invokation on create
|
||||
if (config.proxy && config.proxy.protocol && config.proxy.host && config.proxy.port) {
|
||||
const proxyUrl = `${config.proxy.protocol}://${config.proxy.host}:${config.proxy.port}`;
|
||||
const axiosCpy = axios;
|
||||
// When used directly
|
||||
const dump = axios.create({
|
||||
httpAgent: new SocksProxyAgent(proxyUrl),
|
||||
httpsAgent: new SocksProxyAgent(proxyUrl),
|
||||
axios.interceptors.request.use((options) => {
|
||||
if (new RegExp(config.proxy.url_regex).test(options.url)) {
|
||||
switch (config.proxy.protocol) {
|
||||
case 'socks':
|
||||
options.httpAgent = new SocksProxyAgent(proxyUrl);
|
||||
options.httpsAgent = new SocksProxyAgent(proxyUrl);
|
||||
break;
|
||||
case 'http':
|
||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
|
||||
options.httpAgent = tunnel.httpOverHttp({
|
||||
proxy: {
|
||||
host: config.proxy.host,
|
||||
port: parseInt(config.proxy.port),
|
||||
},
|
||||
});
|
||||
options.httpsAgent = tunnel.httpsOverHttp({
|
||||
proxy: {
|
||||
host: config.proxy.host,
|
||||
port: parseInt(config.proxy.port),
|
||||
},
|
||||
});
|
||||
break;
|
||||
case 'https':
|
||||
options.httpAgent = tunnel.httpOverHttps({
|
||||
proxy: {
|
||||
host: config.proxy.host,
|
||||
port: parseInt(config.proxy.port),
|
||||
proxyAuth: `${config.proxy.auth.username}:${config.proxy.auth.password}`,
|
||||
},
|
||||
});
|
||||
options.httpsAgent = tunnel.httpsOverHttps({
|
||||
proxy: {
|
||||
host: config.proxy.host,
|
||||
port: parseInt(config.proxy.port),
|
||||
proxyAuth: `${config.proxy.auth.username}:${config.proxy.auth.password}`,
|
||||
},
|
||||
});
|
||||
break;
|
||||
}
|
||||
if (config.proxy.auth) {
|
||||
options.headers['Proxy-Authorization'] = `Basic ${config.proxy.auth}`;
|
||||
}
|
||||
logger.info(`Proxy for ${options.url}`);
|
||||
}
|
||||
return options;
|
||||
});
|
||||
dump.create = function(option, ...args) {
|
||||
option = option || {};
|
||||
option = {
|
||||
httpAgent: new SocksProxyAgent(proxyUrl),
|
||||
httpsAgent: new SocksProxyAgent(proxyUrl),
|
||||
...option,
|
||||
};
|
||||
return axiosCpy.create(option, ...args);
|
||||
};
|
||||
axios = dump;
|
||||
}
|
||||
|
||||
axiosRetry(axios, {
|
||||
retries: config.requestRetry,
|
||||
retryCondition: () => true,
|
||||
|
||||
Reference in New Issue
Block a user