当前位置 : 主页 > 网页制作 > Nodejs >

node.js – 通过nodejs上的代理的TCP套接字客户端

来源:互联网 收集:自由互联 发布时间:2021-06-16
我需要将tcp套接字连接到smtp服务器.是否可以通过nodejs上的代理服务器进行连接?有没有可用的npm模块?我根本找不到任何东西. var net = require('net');var HOST = '127.0.0.1';var PORT = 6969;var cli
我需要将tcp套接字连接到smtp服务器.是否可以通过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没有代理支持.

与代理一起使用它的最简单方法是用代理链或类似的东西替换一些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
网友评论