如何使用带有节点的https代理。js https /请求的客户端吗?

时间:2021-09-26 15:15:41

I need to send my client HTTPS requests through an intranet proxy to a server. I use both https and request+global-tunnel and neither solutions seem to work.
The similar code with 'http' works. Is there other settings I missed?

我需要通过内部网代理将客户机HTTPS请求发送到服务器。我使用https和request+global-tunnel这两种解决方案似乎都不起作用。类似的“http”代码也可以工作。还有其他的设置吗?

The code failed with an error: REQUEST: problem with request: tunneling socket could not be established, cause=socket hang up

错误的代码失败:请求:请求的问题:无法建立tunneling套接字,原因=套接字挂起。

HTTPS: events.js:72 throw er; // Unhandled 'error' event ^ Error: socket hang up at SecurePair.error (tls.js:1011:23) at EncryptedStream.CryptoStream._done (tls.js:703:22) at CleartextStream.read [as _read] (tls.js:499:24)

HTTPS:事件。js:72 er扔;/ /未处理的‘错误’事件^错误:套接字在SecurePair挂断。在EncryptedStream.CryptoStream错误(tls.js:1011:23)。在CleartextStream _done(tls.js:703:22)。读(因为_read)(tls.js 499:24):

  • The code is the simple https test.

    代码是简单的https测试。

    var http = require("https");

    var http =要求(“https”);

    var options = { host: "proxy.myplace.com", port: 912, path: "https://www.google.com", headers: { Host: "www.google.com" } };

    var选项= {host: "proxy.myplace.com", port: 912, path: "https://www.google.com", header: {host: "www.google.com"};

    http.get(options, function(res) { console.log(res); res.pipe(process.stdout); });

    http。得到(选项,函数(res){ console.log(res);res.pipe(process.stdout);});

1 个解决方案

#1


4  

You probably want to establish a TLS encrypted connection between your node app and target destination through a proxy.

您可能希望通过代理在节点应用程序和目标目的地之间建立一个TLS加密连接。

In order to do this you need to send a CONNECT request with the target destination host name and port. The proxy will create a TCP connection to the target host and then simply forwards packs between you and the target destination.

为了做到这一点,您需要向目标目的地主机名和端口发送连接请求。代理将创建到目标主机的TCP连接,然后简单地在您和目标目的地之间转发包。

I highly recommend using the request client. This package simplifies the process and handling of making HTTP/S requests.

我强烈建议使用请求客户端。这个包简化了发出HTTP/S请求的过程和处理。

Example code using request client:

示例代码使用请求客户端:

var request = require('request');

request({
    url: 'https://www.google.com',
    proxy: 'http://97.77.104.22:3128'
}, function (error, response, body) {
    if (error) {
        console.log(error);
    } else {
        console.log(response);
    }
});

Example code using no external dependencies:

示例代码不使用外部依赖项:

var http = require('http'),
    tls = require('tls');

var req = http.request({
    host: '97.77.104.22',
    port: 3128,
    method: 'CONNECT',
    path: 'twitter.com:443'
});

req.on('connect', function (res, socket, head) {
    var tlsConnection = tls.connect({
        host: 'twitter.com',
        socket: socket
    }, function () {
        tlsConnection.write('GET / HTTP/1.1\r\nHost: twitter.com\r\n\r\n');
    });

    tlsConnection.on('data', function (data) {
        console.log(data.toString());
    });
});

req.end();

#1


4  

You probably want to establish a TLS encrypted connection between your node app and target destination through a proxy.

您可能希望通过代理在节点应用程序和目标目的地之间建立一个TLS加密连接。

In order to do this you need to send a CONNECT request with the target destination host name and port. The proxy will create a TCP connection to the target host and then simply forwards packs between you and the target destination.

为了做到这一点,您需要向目标目的地主机名和端口发送连接请求。代理将创建到目标主机的TCP连接,然后简单地在您和目标目的地之间转发包。

I highly recommend using the request client. This package simplifies the process and handling of making HTTP/S requests.

我强烈建议使用请求客户端。这个包简化了发出HTTP/S请求的过程和处理。

Example code using request client:

示例代码使用请求客户端:

var request = require('request');

request({
    url: 'https://www.google.com',
    proxy: 'http://97.77.104.22:3128'
}, function (error, response, body) {
    if (error) {
        console.log(error);
    } else {
        console.log(response);
    }
});

Example code using no external dependencies:

示例代码不使用外部依赖项:

var http = require('http'),
    tls = require('tls');

var req = http.request({
    host: '97.77.104.22',
    port: 3128,
    method: 'CONNECT',
    path: 'twitter.com:443'
});

req.on('connect', function (res, socket, head) {
    var tlsConnection = tls.connect({
        host: 'twitter.com',
        socket: socket
    }, function () {
        tlsConnection.write('GET / HTTP/1.1\r\nHost: twitter.com\r\n\r\n');
    });

    tlsConnection.on('data', function (data) {
        console.log(data.toString());
    });
});

req.end();