Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function optimal() {
let httpRunning = false // to prevent mutiple http create
if (socksPorts.length <= 0) {
init()
return false
}
for (let i = 0; i < socksPorts.length; i++) {
let tmp = socksPorts[i]
let req = http.get({
hostname: 'google.com',
port: 80,
agent: new socks.Agent({
proxy: {
ipaddress: config.host,
port: socksPorts[i],
type: 5
}
}, false, false)
}, (res) => {
if (res.statusCode == 200 || res.statusCode == 302) {
if (!httpRunning) {
start(tmp)
if (updateIPs) {
pac.updateIPs(tmp)
}
httpRunning = true
}
}
resolve(ph.host, function(err, hostname) {
if (err) {
console.error('Resolve error for domain ' + ph.hostname + ': ' + err.message);
response.writeHead(105);
response.end('Name Not Resolved');
} else {
var socksAgent = new Socks.Agent({
proxy: proxy,
target: {host: hostname, port: ph.port}
});
var options = {
port: ph.port,
hostname: ph.hostname,
method: request.method,
path: ph.path,
headers: request.headers,
agent: socksAgent
};
var proxyRequest = http.request(options);
proxyRequest.setTimeout(10000, function() {
response.writeHead(504);
response.end('Gateway Timeout\n');
if (proxyRequest.socket)
function requestListener (request, response) {
const { direct, hostname, port, path } = request.urlObj
const method = request.method
const headers = request.headers
const agent = new Socks.Agent({ proxy }, false, false)
const options = {
hostname,
port,
path,
method,
headers
}
if (!direct) {
options.agent = agent
}
const proxyRequest = http.request(options)
proxyRequest.on('response', proxyResponse => {
const method = req.method
const headers = req.headers
const options = {
hostname,
port,
path,
method,
headers
}
if (direct) {
logger.request(req, 'direct')
} else {
logger.request(req, 'tunnel')
options.agent = new Socks.Agent({proxy}, false, false)
}
const jetRequest = http.request(options, (_res) => {
_res.pipe(res)
res.writeHead(_res.statusCode, _res.headers)
})
jetRequest.on('error', (err) => {
logger.error(err)
})
req.pipe(jetRequest)
}
}
const method = req.method
const headers = req.headers
const options = {
hostname,
port,
path,
method,
headers
}
if (direct) {
logger.request(req, 'direct')
} else {
logger.request(req, 'tunnel')
options.agent = new Socks.Agent({ proxy }, false, false)
}
const _req = http.request(options, (_res) => {
res.writeHead(_res.statusCode, _res.headers)
_res.pipe(res)
})
_req.on('error', (err) => {
return next(err)
})
_req.end()
}
exports.updateIPs = function (port) {
let req = http.get({
hostname: 'www.ipdeny.com',
port: 80,
path: '/ipblocks/data/aggregated/cn-aggregated.zone',
agent: new socks.Agent({
proxy: {
ipaddress: config.host,
port: port,
type: 5
}
}, false, false)
}, function (res) {
if (res.statusCode == 200 || res.statusCode == 302) {
res.setEncoding('utf-8')
let allIps = ''
res.on('data', function (chunk) {
allIps += chunk
}).on('end', function () {
fs.writeFile(path.join(__dirname, '../config/GeoIP-CN'), allIps)
})
}
return function (req, res) {
const _url = url.parse(req.url)
let options = {
hostname: _url.hostname,
port: _url.port,
path: _url.path,
method: _url.method,
headers: req.headers
}
options.agent = new Socks.Agent({
proxy: {
ipaddress: host,
port: sockPort,
type: 5
}
}, false, false)
const _req = http.request(options, (_res) => {
res.writeHead(_res.statusCode, _res.headers)
_res.pipe(res)
}).on('error', (err) => {
logger.error(`Agent http ${err}`)
if (callback) callback()
res.end()
})
req.pipe(_req)
function requestListener(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 options = {
port: ph.port,
hostname: ph.hostname,
method: request.method,
path: ph.path,
headers: request.headers,
agent: socksAgent,
};
const proxyRequest = http.request(options);
request.on('error', (err) => {