Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
transporter.getSocket = function (options, callback) {
console.log('Socket requested to %s:%s', options.host, options.port);
var proxyOptions = {
proxy: proxy,
target: {
host: options.host,
port: options.port
},
command: 'connect'
};
console.log(proxyOptions);
Socks.createConnection(proxyOptions, function(err, socket){
callback(err, {
// everything we pass here will be appended to the options of smtp-connection
// see possible options here:
// https://github.com/nodemailer/smtp-connection#create-smtpconnection-instance
connection: socket,
tls: {
rejectUnauthorized: true
}
});
});
};
resolve(ph.hostname, function (err, hostname) {
if (err) {
console.error('Resolve error for domain ' + ph.hostname + ': ' + err.message);
socketRequest.write('HTTP/' + request.httpVersion + ' 105 Name Not Resolved\r\n\r\n');
socketRequest.end();
return;
}
var options = {
proxy: proxy,
//target: {host: ph.hostname, port: ph.port},
target: {host: hostname, port: ph.port},
command: 'connect'
};
Socks.createConnection(options, function (err, socket, info) {
if (err) {
// error in SocksSocket creation
console.error(err.message + ' connection creating on ' + proxy.ipaddress + ':' + proxy.port);
socketRequest.write('HTTP/' + request.httpVersion + ' 500 Connection error\r\n\r\n');
return;
}
// tunneling to the host
socket.on('data', function (chunk) { socketRequest.write(chunk) });
socket.on('end', function () { socketRequest.end() });
socket.on('error', function (err) {
// error in transfer
console.error(err.message + ' on proxy ' + proxy.ipaddress + ':' + proxy.port);
socketRequest.write('HTTP/' + request.httpVersion + ' 500 Connection error\r\n\r\n');
socketRequest.end();
});
// tunneling to the client
proxySocket.write(head)
proxySocket.pipe(socket)
socket.pipe(proxySocket)
})
proxySocket.on('error', () => {
socket.write(headerError(request.httpVersion))
})
} else {
const options = {
proxy,
target: { host, port }
}
Socks.createConnection(options, (err, proxySocket) => {
if (err) {
return socket.write(headerError(request.httpVersion))
}
// tell the client that the connection is established
socket.write(headerEstablished(request.httpVersion))
proxySocket.write(head)
// creating pipes in both ends
proxySocket.pipe(socket)
socket.pipe(proxySocket)
})
}
}
const options = {
proxy,
target: { host, port },
command: 'connect',
};
let socket;
socketRequest.on('error', (err) => {
logger.error(`${err.message}`);
if (socket) {
socket.destroy(err);
}
});
Socks.createConnection(options, (error, _socket) => {
socket = _socket;
if (error) {
// error in SocksSocket creation
logger.error(`${error.message} connection creating on ${proxy.ipaddress}:${proxy.port}`);
socketRequest.write(`HTTP/${request.httpVersion} 500 Connection error\r\n\r\n`);
return;
}
socket.on('error', (err) => {
logger.error(`${err.message}`);
socketRequest.destroy(err);
});
// tunneling to the host
socket.pipe(socketRequest);
const _socket = net.connect(port, hostname, () => {
// tell the client that the connection is established
socket.write(jetHeader(req.httpVersion))
_socket.write(head)
// creating pipes in both ends
_socket.pipe(socket)
socket.pipe(_socket)
})
_socket.on('error', (err) => {
return next(err)
})
} else {
logger.request(req, 'tunnel')
Socks.createConnection({
proxy,
target: {
host: hostname,
port: port
},
command: 'connect'
}, (err, _socket, info) => {
if (err) {
return next(err)
} else {
// tell the client that the connection is established
socket.write(jetHeader(req.httpVersion))
_socket.write(head)
// creating pipes in both ends
_socket.pipe(socket)
socket.pipe(_socket)
it('status test', async (done) => {
socks.createConnection(clientOpts, async (err, socket, info) => {
if (err) return assert.fail(err, null, err.message);
assert(net.isIP(socket.remoteAddress));
done();
});
});
});
};
if(config.agentType == '1') {
let socksOptions = {
proxy: {
ipaddress: config.agentHost,
port: config.agentPort,
type: 5
},
target: {
host: requestOptions.host,
port: requestOptions.port
}
};
Socks.createConnection(socksOptions, function(err, tunnel, info) {
if (err)
console.log(err);
else {
_synReply(socket, 200, 'Connection established', {
'Connection': 'keep-alive'
},
function(error) {
if (error) {
console.log(`syn error: ${error.message}`);
tunnel.end();
socket.end();
return;
}
tunnel.resume();
tunnel.pipe(socket);