Skip to content

Commit 9143b82

Browse files
support for skipping requests to some hosts
allows skipping local requests with this prooxy
1 parent 1e13a70 commit 9143b82

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

src/cli.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const optionNames = [
1010
'level',
1111
'config',
1212
'host',
13+
'skip',
1314
];
1415

1516
function getFileConfig(filePath) {
@@ -51,6 +52,7 @@ function main() {
5152
.option('-p, --port [port]', 'specify the listening port of http proxy server, default: 8080')
5253
.option('-l, --host [host]', 'specify the listening host of http proxy server, default: 127.0.0.1')
5354
.option('-c, --config [config]', 'read configs from file in json format')
55+
.option('-s, --skip [hostname1|hostname2]', 'skip proxy for hostname1 and hostname2 and so on')
5456
.option('--level [level]', 'log level, vals: info, error')
5557
.parse(process.argv);
5658

src/proxy_server.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,28 @@ function parseProxyLine(line) {
2929
return getProxyObject.apply(this, proxyInfo);
3030
}
3131

32-
function requestListener(getProxyInfo, request, response) {
32+
function requestListener(getProxyInfo, request, response, hostnamesToSkip) {
3333
logger.info(`request: ${request.url}`);
34-
3534
const proxy = getProxyInfo();
3635
const ph = url.parse(request.url);
37-
38-
const socksAgent = new Socks.Agent({
39-
proxy,
40-
target: { host: ph.hostname, port: ph.port },
41-
});
36+
const shouldProxy = !hostnamesToSkip
37+
|| (hostnamesToSkip.indexOf(ph.hostname.toLowerCase()) === -1);
4238

4339
const options = {
4440
port: ph.port,
4541
hostname: ph.hostname,
4642
method: request.method,
4743
path: ph.path,
4844
headers: request.headers,
49-
agent: socksAgent,
5045
};
46+
if (shouldProxy) {
47+
options.agent = new Socks.Agent({
48+
proxy,
49+
target: { host: ph.hostname, port: ph.port },
50+
});
51+
} else {
52+
logger.info('skipping this request because it is local');
53+
}
5154

5255
const proxyRequest = http.request(options);
5356

@@ -120,8 +123,8 @@ function connectListener(getProxyInfo, request, socketRequest, head) {
120123

121124
function ProxyServer(options) {
122125
// TODO: start point
123-
http.Server.call(this, () => {});
124-
126+
http.Server.call(this, () => { });
127+
this.hostnamesToSkip = (options.skip || '').split('|');
125128
this.proxyList = [];
126129

127130
if (options.socks) {
@@ -142,7 +145,7 @@ function ProxyServer(options) {
142145

143146
this.addListener(
144147
'request',
145-
requestListener.bind(null, () => randomElement(this.proxyList))
148+
requestListener.bind(null, () => randomElement(this.proxyList), this.hostnamesToSkip)
146149
);
147150
this.addListener(
148151
'connect',

src/server.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const DEFAULT_OPTIONS = {
66
socks: '127.0.0.1:1080',
77
proxyListReloadTimeout: 60,
88
port: 8080,
9+
skip: 'localhost|127.0.0.1'
910
};
1011

1112
function createServer(opts) {
@@ -15,10 +16,10 @@ function createServer(opts) {
1516
changeLevel(logger, options.level);
1617
}
1718

18-
const { port, socks, host } = options;
19+
const { port, socks, host, skip } = options;
1920

2021
// eslint-disable-next-line
21-
console.log(`SOCKS: ${socks}\nhttp-proxy listening: ${host}:${port}`);
22+
console.log(`SOCKS: ${socks}\nhttp-proxy listening: ${host}:${port}, skiplist: ${skip}`);
2223

2324
return createProxyServer(options).listen(port, host);
2425
}

0 commit comments

Comments
 (0)