support proxy-uri

This commit is contained in:
zengxs
2020-05-06 22:43:09 +08:00
parent 9a1b6ca0cc
commit 58e930b4cc
4 changed files with 26 additions and 9 deletions

View File

@@ -1,4 +1,5 @@
const config = require('@/config').value;
const HttpsProxyAgent = require('https-proxy-agent');
const SocksProxyAgent = require('socks-proxy-agent');
const tunnel = require('tunnel');
const logger = require('./logger');
@@ -6,7 +7,21 @@ const http = require('http');
const https = require('https');
let agent = null;
if (config.proxy && config.proxy.protocol && config.proxy.host && config.proxy.port) {
if (config.proxyUri && typeof config.proxyUri === 'string') {
let proxy = null;
if (config.proxyUri.startsWith('http')) {
proxy = new HttpsProxyAgent(config.proxyUri);
} else if (config.proxyUri.startsWith('socks')) {
proxy = new SocksProxyAgent(config.proxyUri);
} else {
throw 'Unknown proxy-uri format';
}
agent = {
http: proxy,
https: proxy,
};
} else if (config.proxy && config.proxy.protocol && config.proxy.host && config.proxy.port) {
agent = {};
const proxyUrl = `${config.proxy.protocol}://${config.proxy.host}:${config.proxy.port}`;