本文整理汇总了TypeScript中socks.createConnection函数的典型用法代码示例。如果您正苦于以下问题:TypeScript createConnection函数的具体用法?TypeScript createConnection怎么用?TypeScript createConnection使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了createConnection函数的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
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();
});
});
示例2: Promise
return new Promise((fulfill, reject) => {
const testDomain =
CREDENTIALS_TEST_DOMAINS[Math.floor(Math.random() * CREDENTIALS_TEST_DOMAINS.length)];
socks.createConnection(
{
proxy: {ipaddress: PROXY_IP, port: SS_LOCAL_PORT, type: 5},
target: {host: testDomain, port: 80}
},
(e, socket) => {
if (e) {
reject(new Error(`could not connect to remote test website: ${e.message}`));
return;
}
socket.write(`HEAD / HTTP/1.1\r\nHost: ${testDomain}\r\n\r\n`);
socket.on('data', (data) => {
if (data.toString().startsWith('HTTP/1.1')) {
socket.end();
fulfill();
} else {
socket.end();
reject(new Error(`unexpected response from remote test website`));
}
});
socket.on('close', () => {
reject(new Error(`could not connect to remote test website`));
});
// Sockets must be resumed before any data will come in, as they are paused right before
// this callback is fired.
socket.resume();
});
});
示例3: Promise
return new Promise((resolve, reject) => {
socks.createConnection(
{
proxy: {ipaddress: proxyAddress, port: proxyIp, type: 5, command: 'associate'},
target: {host: '0.0.0.0', port: 0}, // Specify the actual target once we get a response.
},
(err, socket, info) => {
if (err) {
reject(new errors.RemoteUdpForwardingDisabled(`could not connect to local proxy`));
return;
}
const packet = socks.createUDPFrame({host: '1.1.1.1', port: 53}, DNS_REQUEST);
const udpSocket = dgram.createSocket('udp4');
udpSocket.on('error', (e) => {
reject(new errors.RemoteUdpForwardingDisabled('UDP socket failure'));
});
udpSocket.on('message', (msg, info) => {
stopUdp();
resolve(true);
});
// Retry sending the query every second.
// TODO: logging here is a bit verbose
const intervalId = setInterval(() => {
try {
udpSocket.send(packet, info.port, info.host, (err) => {
if (err) {
console.error(`Failed to send data through UDP: ${err}`);
}
});
} catch (e) {
console.error(`Failed to send data through UDP ${e}`);
}
}, UDP_FORWARDING_TEST_RETRY_INTERVAL_MS);
const stopUdp = () => {
try {
clearInterval(intervalId);
udpSocket.close();
} catch (e) {
// Ignore; there may be multiple calls to this function.
}
};
// Give up after the timeout elapses.
setTimeout(() => {
stopUdp();
resolve(false);
}, UDP_FORWARDING_TEST_TIMEOUT_MS);
});
});