Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'socks' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
server: function (onConnection, cb) {
if(!opts.server) return
var serverOpts = {
proxy: proxyOpts,
command: "bind",
destination: {
host: opts.host,
port: opts.port
}
}
var controlSocket = null
socks.createConnection(serverOpts, function (err, socket) {
if(err) {
console.error('unable to find local tor server.')
console.error('will be able receive tor connections') // << ???
return
}
controlSocket = socket
socket.on('data', function(stream) {
stream = toPull.duplex(stream)
stream.address = 'onion:'
onConnection(stream)
})
cb(null, true);
if (!host) {
throw new Error('No `host` defined!');
}
if (lookup) {
// Client-side DNS resolution for "4" and "5" socks proxy versions.
host = await dnsLookup(host);
}
const socksOpts: SocksClientOptions = {
proxy,
destination: { host, port },
command: 'connect'
};
debug('Creating socks proxy connection: %o', socksOpts);
const { socket } = await SocksClient.createConnection(socksOpts);
debug('Successfully created socks proxy connection');
if (opts.secureEndpoint) {
const servername = opts.servername || opts.host;
if (!servername) {
throw new Error('Could not determine "servername"');
}
// The proxy is connecting to a TLS server, so upgrade
// this socket connection to a TLS connection.
debug('Upgrading socket connection to TLS');
return tls.connect({
...omit(opts, 'host', 'hostname', 'path', 'port'),
socket,
servername
});
}
timeout: readyTimeout
}
request(opts)
.on('error', (e) => {
console.error(`fail to connect proxy: ${e.message}`)
reject(e)
})
.on('connect', (res, socket) => {
resolve({ socket: socket })
})
.end()
})
}
// use socks proxy
return SocksClient.createConnection(options)
}
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
}
}
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
client: function (opts, cb) {
var started = false, _socket, destroy
var connectOpts = {
proxy: proxyOpts,
command: "connect",
destination: {
host: opts.host,
port: opts.port
}
}
socks.createConnection(connectOpts, function(err, result) {
if (err) return cb(err)
var socket = result.socket
if(destroy) return socket.destroy()
_socket = socket
var duplexStream = toPull.duplex(socket)
duplexStream.address = 'onion:'+connectOpts.destination.host+':'+connectOpts.destination.port
cb(null, duplexStream)
// Remember to resume the socket stream.
socket.resume()
})
return function () {
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)