[英]TCP socket client through proxy on nodejs
我需要与 smtp 服务器建立 tcp 套接字连接。 是否可以通过nodejs上的代理服务器连接? 有没有可用的 npm 模块? 我根本找不到。
var net = require('net');
var HOST = '127.0.0.1';
var PORT = 6969;
var client = new net.Socket();
client.connect(PORT, HOST, function() {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
client.write('I am here!');
});
// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
client.on('data', function(data) {
console.log('DATA: ' + data);
});
// Add a 'close' event handler for the client socket
client.on('close', function() {
console.log('Connection closed');
});
net.socket
, tls.connect
和dgram
没有代理支持。
与代理一起使用它的最简单方法是用代理proxychains
或类似的东西替换一些libc函数。
var client = require('tls')
.connect(443, 'www.facebook.com', function() {
console.log('connected');
client.write('hello');
})
.on('data', function(data) {
console.log('received', data.toString());
})
.on('close', function() {
console.log('closed');
});
proxychains node fit.js
connected
received HTTP/1.1 400 Bad Request
...
closed
是的,可以使用其中一个NPM模块:
http-proxy-agent :HTTP端点的HTTP(s)代理http.Agent实现
https-proxy-agent :HTTPS端点的HTTP(s)代理http.Agent实现
pac-proxy-agent :用于HTTP和HTTPS的PAC文件代理http.Agent实现
socks-proxy-agent :用于HTTP和HTTPS的SOCKS(v4a)代理http.Agent实现
HTTPS代理示例:
var url = require('url');
var WebSocket = require('ws');
var HttpsProxyAgent = require('https-proxy-agent');
// HTTP/HTTPS proxy to connect to
var proxy = process.env.http_proxy || 'http://168.63.76.32:3128';
console.log('using proxy server %j', proxy);
// WebSocket endpoint for the proxy to connect to
var endpoint = process.argv[2] || 'ws://echo.websocket.org';
var parsed = url.parse(endpoint);
console.log('attempting to connect to WebSocket %j', endpoint);
// create an instance of the `HttpsProxyAgent` class with the proxy server information
var opts = url.parse(proxy);
var agent = new HttpsProxyAgent(opts);
// finally, initiate the WebSocket connection
var socket = new WebSocket(endpoint, { agent: agent });
socket.on('open', function () {
console.log('"open" event!');
socket.send('hello world');
});
socket.on('message', function (data, flags) {
console.log('"message" event! %j %j', data, flags);
socket.close();
});
我希望这有帮助。
除了“net”之外,只要代理服务器支持 HTTPS 流量,您就不需要其他模块来通过代理服务器使套接字连接到主机。
即使此过程从 HTTP 开始,但这并不意味着它不适用于 SMTP 流量。 最初的 HTTP 消息仅用于协商连接,之后它只是原始流量。 代理服务器可能会查看您要连接的端口或主机并禁止它。 例如,阻止连接到低于 80 或低于 443 的端口(例如端口 25),因此它实际上取决于代理服务器是否允许连接。
"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.