TCP socket client through a proxy server on the nodes

I need to connect tcp socket to an SMTP server. Is it possible to connect through a proxy server on nodejs? Are any npm modules available? I could not find 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');
});
+4
source share
2 answers

net.socket, tls.connectand dgramdo not have proxy support.

The easiest way to use it with a proxy server is to replace some of the libc functions with proxychainsor something similar.

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
+1
source

Yes, it is possible with one of these NPM modules:

http-proxy-agent : HTTP proxy implementation of HTTP.Agent for HTTP endpoints

https-proxy-agent: HTTP-- HTTP.Agent HTTPS

pac-proxy-agent: - PAC HTTP.Agent HTTP HTTPS

socks-proxy-agent: - SOCKS (v4a) HTTP.Agent HTTP HTTPS

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();
});

, .

0

Source: https://habr.com/ru/post/1615893/


All Articles