diff --git a/docs/en/install/README.md b/docs/en/install/README.md index 825193e52..076a3bbb3 100644 --- a/docs/en/install/README.md +++ b/docs/en/install/README.md @@ -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. diff --git a/docs/install/README.md b/docs/install/README.md index 4e50c73e6..19a95c49b 100644 --- a/docs/install/README.md +++ b/docs/install/README.md @@ -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 认证. diff --git a/lib/config.js b/lib/config.js index e5a235a5c..1e4c13869 100644 --- a/lib/config.js +++ b/lib/config.js @@ -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, + }, }; diff --git a/lib/utils/axios.js b/lib/utils/axios.js index 49864d0a2..de4edfaa0 100644 --- a/lib/utils/axios.js +++ b/lib/utils/axios.js @@ -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, diff --git a/package.json b/package.json index 6e655f4b0..0a1c50f17 100644 --- a/package.json +++ b/package.json @@ -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" },