Add socks proxy support (#1379)

This commit is contained in:
NStal 2019-01-14 14:52:30 +08:00 committed by DIYgod
parent 6ae0543f29
commit c956c8a0dd
5 changed files with 43 additions and 2 deletions

View File

@ -280,6 +280,12 @@ Use environment variables is recommended to avoid conflicts during upgrade.
`LOGGER_LEVEL`: specifies the maximum [level](https://github.com/winstonjs/winston#logging-levels) of messages to the console and log file, default to `info`
`PROXY_PROTOCOL`: Using proxy of such protocol, Supports socks, socks4,socks4a,socks5,socks5h
`PROXY_HOST`: host of the proxy
`PROXY_PORT`: port of the proxy
### User Authentication
Routes in `protected_route.js` will be protected using HTTP Basic Authentication.

View File

@ -286,6 +286,12 @@ gcloud app deploy
`LOGGER_LEVEL`: 指明输出到 console 和日志文件的日志的最大[等级](https://github.com/winstonjs/winston#logging-levels),默认 `info`
`PROXY_PROTOCOL`: 使用 proxy 来访问的协议, 目前只支持 socks, socks4,socks4a,socks5,socks5h
`PROXY_HOST`: proxy 的域名
`PROXY_PORT`: proxy 的端口
### 用户认证
`protected_route.js` 内的路由将启用 HTTP Basic Authentication 认证.

View File

@ -53,4 +53,12 @@ module.exports = {
},
puppeteerWSEndpoint: process.env.PUPPETEER_WS_ENDPOINT,
loggerLevel: process.env.LOGGER_LEVEL || 'info',
// Something like socks://{{host}}:{{port}}
proxy: {
// Supported socks protocols see:
// https://github.com/TooTallNate/node-socks-proxy-agent/blob/5867dd04e92b51af0a35d5b3cb6a82f8f5590b6f/index.js#L53
protocol: process.env.PROXY_PROTOCOL || null,
host: process.env.PROXY_HOST || null,
port: process.env.PROXY_PORT || null,
},
};

View File

@ -1,9 +1,29 @@
const logger = require('./logger');
const config = require('../config');
const SocksProxyAgent = require('socks-proxy-agent');
const axiosRetry = require('axios-retry');
const axios = require('axios');
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
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),
});
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,

View File

@ -73,6 +73,7 @@
"redis": "2.8.0",
"rss-parser": "3.6.2",
"sharp": "^0.21.0",
"socks-proxy-agent": "^4.0.1",
"twit": "2.2.11",
"winston": "3.1.0"
},