Skip to content

Commit 96222ee

Browse files
support for skipping requests to some hosts
allows skipping local requests or any other requests by hostname
1 parent 1e13a70 commit 96222ee

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

src/__tests__/proxy_server.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ describe('proxy_server', () => {
148148

149149
describe('requestListener', () => {
150150
it('should create an socks agent and take it as request agent', () => {
151-
requestListener(getProxyInfo, request, response);
151+
requestListener([], getProxyInfo, request, response);
152152

153153
const lastCall = last(Socks.Agent.mock.calls);
154154
const httpLastCall = last(http.request.mock.calls);
@@ -158,7 +158,7 @@ describe('proxy_server', () => {
158158
});
159159

160160
it('should return 500 when error thrown', () => {
161-
requestListener(getProxyInfo, request, response);
161+
requestListener([], getProxyInfo, request, response);
162162

163163
const onErrorArgs = getLastMockOn('error');
164164

@@ -179,7 +179,7 @@ describe('proxy_server', () => {
179179
pipe: jest.fn(),
180180
};
181181

182-
requestListener(getProxyInfo, request, response);
182+
requestListener([], getProxyInfo, request, response);
183183

184184
const onResponseArgs = getLastMockOn('response');
185185

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('--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(hostnamesToSkip, getProxyInfo, request, response) {
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, this.hostnamesToSkip, () => randomElement(this.proxyList))
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)