小编典典

使用https.request忽略node.js中无效的自签名ssl证书吗?

node.js

我正在开发一个登录到本地无线路由器(Linksys)的小应用程序,但是我遇到了路由器的自签名ssl证书问题。

我运行wget 192.168.1.1并得到:

ERROR: cannot verify 192.168.1.1's certificate, issued by `/C=US/ST=California/L=Irvine/O=Cisco-Linksys, LLC/OU=Division/CN=Linksys/emailAddress=support@linksys.com':
Self-signed certificate encountered.
ERROR: certificate common name `Linksys' doesn't match requested host name `192.168.1.1'.
To connect to 192.168.1.1 insecurely, use `--no-check-certificate'.

在节点中,捕获的错误是:

{ [Error: socket hang up] code: 'ECONNRESET' }

我当前的示例代码是:

var req = https.request({ 
    host: '192.168.1.1', 
    port: 443,
    path: '/',
    method: 'GET'

}, function(res){

    var body = [];
    res.on('data', function(data){
        body.push(data);
    });

    res.on('end', function(){
        console.log( body.join('') );
    });

});
req.end();

req.on('error', function(err){
    console.log(err);
});

我如何才能使node.js等效于“ –no-check-certificate”?


阅读 559

收藏
2020-07-07

共1个答案

小编典典

便宜又不安全的答案:

process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;

在代码中,调用之前 https.request()

在此问题中回答了一种更安全的方法(上述解决方案使整个节点过程不安全)

2020-07-07