From 08878e1d5233b6ae36f87fe2d0b16611ac36b87a Mon Sep 17 00:00:00 2001 From: DIYgod Date: Sat, 3 Aug 2019 20:38:37 +0800 Subject: [PATCH] fix: set proxy for http module, close #2647 #2094 --- lib/index.js | 1 + lib/utils/agent-wrapper.js | 83 ++++++++++++++++++++++++++++++++++++++ lib/utils/got.js | 53 ------------------------ 3 files changed, 84 insertions(+), 53 deletions(-) create mode 100644 lib/utils/agent-wrapper.js diff --git a/lib/index.js b/lib/index.js index 4f56c17b0..fe20c4097 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,6 +4,7 @@ const config = require('./config'); const Koa = require('koa'); const fs = require('fs'); const logger = require('./utils/logger'); +require('./utils/agent-wrapper'); const onerror = require('./middleware/onerror'); const header = require('./middleware/header'); diff --git a/lib/utils/agent-wrapper.js b/lib/utils/agent-wrapper.js new file mode 100644 index 000000000..687126b8d --- /dev/null +++ b/lib/utils/agent-wrapper.js @@ -0,0 +1,83 @@ +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 agentWrapper = (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) { + options.headers['Proxy-Authorization'] = `Basic ${config.proxy.auth}`; + } + logger.info(`Proxy for ${url}`); + } +}; + +const httpWrap = (func) => { + const origin = func; + return function(url, request) { + if (typeof url === 'object') { + const req = url; + agentWrapper(req.url || req.href || `${req.protocol}//${req.hostname}${req.path}`, req); + } else { + agentWrapper(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); diff --git a/lib/utils/got.js b/lib/utils/got.js index 525b75557..39ef12c30 100644 --- a/lib/utils/got.js +++ b/lib/utils/got.js @@ -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, @@ -82,15 +38,6 @@ const custom = got.extend({ options.query = options.query || queryString.stringify(options.params); 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}`); - } }, ], },