mirror of
https://github.com/avwo/whistle.git
synced 2026-10-29 07:06:36 +08:00
refactor: refine proxy://&&socks://
This commit is contained in:
+43
-12
@@ -1,6 +1,8 @@
|
||||
var net = require('net');
|
||||
var tls = require('tls');
|
||||
var url = require('url');
|
||||
var tls = require('tls');
|
||||
var socks = require('socksv5');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var extend = require('util')._extend;
|
||||
var util = require('../util');
|
||||
@@ -76,22 +78,51 @@ function handleWebsocket(socket, clientIp, callback) {
|
||||
proxyUrl = (wss ? 'wss:' : 'ws:') + util.removeProtocol(proxyUrl);
|
||||
resolveHost(proxyUrl, function(ip) {
|
||||
options = url.parse(proxyUrl);
|
||||
if (isSocks) {
|
||||
options.port = parseInt(options.port, 10) || 1080;
|
||||
}
|
||||
if ((!options.port || options.port == config.port) && util.isLocalAddress(ip)) {
|
||||
return execCallback(new Error('Unable to agent to itself (' + ip + ':' + config.port + ')'));
|
||||
}
|
||||
|
||||
if (isSocks) {
|
||||
var dstOptions = url.parse(fullUrl);
|
||||
dstOptions.proxyHost = ip;
|
||||
dstOptions.proxyPort = options.port;
|
||||
dstOptions.localDNS = false;
|
||||
dstOptions.auths = config.getAuths(options)
|
||||
if (!dstOptions.port) {
|
||||
dstOptions.port = wss ? 443 : 80;
|
||||
}
|
||||
socks.createConnection(dstOptions, function(proxySocket) {
|
||||
|
||||
if (wss) {
|
||||
proxySocket = tls.connect({
|
||||
rejectUnauthorized: false,
|
||||
socket: proxySocket,
|
||||
servername: dstOptions.hostname
|
||||
}).on('error', execCallback);
|
||||
}
|
||||
|
||||
reqSocket = proxySocket;
|
||||
abortIfUnavailable(reqSocket);
|
||||
pipeData();
|
||||
}).on('error', execCallback);
|
||||
return;
|
||||
}
|
||||
|
||||
util.connect({
|
||||
isSocks: isSocks,
|
||||
host: ip,
|
||||
port: options.port,
|
||||
isHttps: wss,
|
||||
url: fullUrl,
|
||||
auth: options.auth,
|
||||
headers: {
|
||||
host: headers.host,
|
||||
'proxy-connection': 'keep-alive',
|
||||
'user-agent': headers['user-agent']
|
||||
}
|
||||
}, function(err, proxySocket) {
|
||||
host: ip,
|
||||
port: options.port,
|
||||
isHttps: wss,
|
||||
url: fullUrl,
|
||||
auth: options.auth,
|
||||
headers: {
|
||||
host: headers.host,
|
||||
'proxy-connection': 'keep-alive',
|
||||
'user-agent': headers['user-agent']
|
||||
}
|
||||
}, function(err, proxySocket) {
|
||||
if (err) {
|
||||
return execCallback(err);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user