简体   繁体   English

"通过nodejs上的代理的TCP套接字客户端"

[英]TCP socket client through proxy on nodejs

I need to make tcp socket connection to smtp server.我需要与 smtp 服务器建立 tcp 套接字连接。 Is it possible to connect through proxy server on nodejs?是否可以通过nodejs上的代理服务器连接? Is there any npm modules available to use?有没有可用的 npm 模块? I couldn't find any at all.我根本找不到。

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 and dgram have no proxy support. net.sockettls.connectdgram没有代理支持。

The simplest way to use it with proxy is to replace some libc functions with proxychains or something similar. 与代理一起使用它的最简单方法是用代理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 proxychains node fit.js

connected
received HTTP/1.1 400 Bad Request
...
closed

Yes, it is possible with one of these NPM modules: 是的,可以使用其中一个NPM模块:

http-proxy-agent : An HTTP(s) proxy http.Agent implementation for HTTP endpoints http-proxy-agent :HTTP端点的HTTP(s)代理http.Agent实现

https-proxy-agent : An HTTP(s) proxy http.Agent implementation for HTTPS endpoints https-proxy-agent :HTTPS端点的HTTP(s)代理http.Agent实现

pac-proxy-agent : A PAC file proxy http.Agent implementation for HTTP and HTTPS pac-proxy-agent :用于HTTP和HTTPS的PAC文件代理http.Agent实现

socks-proxy-agent : A SOCKS (v4a) proxy http.Agent implementation for HTTP and HTTPS socks-proxy-agent :用于HTTP和HTTPS的SOCKS(v4a)代理http.Agent实现

HTTPS Proxy Example: 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();
});

I hope this helps. 我希望这有帮助。

Other than 'net' you don't need another module to make a socket connect to a host through a proxy server as long at the proxy server supports HTTPS traffic.除了“net”之外,只要代理服务器支持 HTTPS 流量,您就不需要其他模块来通过代理服务器使套接字连接到主机。

  1. Create a socket connection to the proxy server创建到代理服务器的套接字连接<\/li>
  2. Send an HTTP CONNECT message to inform the proxy server of the host and port you want to connect to发送 HTTP CONNECT 消息通知代理服务器您要连接的主机和端口<\/li>
  3. If the proxy server responds with an HTTP 200 response then the proxy server established a socket connection to your desired target and is now relaying the traffic between the sockets如果代理服务器以 HTTP 200 响应进行响应,则代理服务器建立到您所需目标的套接字连接,现在正在中继套接字之间的流量<\/li>
  4. If the proxy server responded with any HTTP response other than 200 then the connection was not established.如果代理服务器以 200 以外的任何 HTTP 响应进行响应,则连接未建立。<\/li><\/ol>

    Even though this process starts with HTTP it doesn't mean it wouldn't work for SMTP traffic.即使此过程从 HTTP 开始,但这并不意味着它不适用于 SMTP 流量。 The initial HTTP message is just used to negotiate the connection, after that its just raw traffic.最初的 HTTP 消息仅用于协商连接,之后它只是原始流量。 A proxy server might look at the port or host you want to connect to and disallow it.代理服务器可能会查看您要连接的端口或主机并禁止它。 For example, block connections to ports below 80 or below 443 such as port 25 so it really depends on the proxy server if the connection will be allowed.例如,阻止连接到低于 80 或低于 443 的端口(例如端口 25),因此它实际上取决于代理服务器是否允许连接。

    "

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM