Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/__tests__/proxy_server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe('proxy_server', () => {

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

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

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

const onErrorArgs = getLastMockOn('error');

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

requestListener(getProxyInfo, request, response);
requestListener([], getProxyInfo, request, response);

const onResponseArgs = getLastMockOn('response');

Expand Down
2 changes: 2 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const optionNames = [
'level',
'config',
'host',
'skip',
];

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

Expand Down
25 changes: 14 additions & 11 deletions src/proxy_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,28 @@ function parseProxyLine(line) {
return getProxyObject.apply(this, proxyInfo);
}

function requestListener(getProxyInfo, request, response) {
function requestListener(hostnamesToSkip, getProxyInfo, request, response) {
logger.info(`request: ${request.url}`);

const proxy = getProxyInfo();
const ph = url.parse(request.url);

const socksAgent = new Socks.Agent({
proxy,
target: { host: ph.hostname, port: ph.port },
});
const shouldProxy = !hostnamesToSkip
|| (hostnamesToSkip.indexOf(ph.hostname.toLowerCase()) === -1);

const options = {
port: ph.port,
hostname: ph.hostname,
method: request.method,
path: ph.path,
headers: request.headers,
agent: socksAgent,
};
if (shouldProxy) {
options.agent = new Socks.Agent({
proxy,
target: { host: ph.hostname, port: ph.port },
});
} else {
logger.info('skipping this request because it is local');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The skip has beed listed at the start so that I prefer we don't log again here.

}

const proxyRequest = http.request(options);

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

function ProxyServer(options) {
// TODO: start point
http.Server.call(this, () => {});

http.Server.call(this, () => { });
this.hostnamesToSkip = (options.skip || '').split('|');
this.proxyList = [];

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

this.addListener(
'request',
requestListener.bind(null, () => randomElement(this.proxyList))
requestListener.bind(null, this.hostnamesToSkip, () => randomElement(this.proxyList))
);
this.addListener(
'connect',
Expand Down
5 changes: 3 additions & 2 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const DEFAULT_OPTIONS = {
socks: '127.0.0.1:1080',
proxyListReloadTimeout: 60,
port: 8080,
skip: 'localhost|127.0.0.1'
};

function createServer(opts) {
Expand All @@ -15,10 +16,10 @@ function createServer(opts) {
changeLevel(logger, options.level);
}

const { port, socks, host } = options;
const { port, socks, host, skip } = options;

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

return createProxyServer(options).listen(port, host);
}
Expand Down