我正在尝试设置一个使用 HTTPS 的 node.js 服务器。然后我将用 Perl 编写一个脚本来向服务器发出 HTTPS 请求并测量往返的延迟。
这是我的 node.js:
var express = require('express');
var https = require('https');
var fs = require('fs');
var key = fs.readFileSync('encrypt/rootCA.key');
var cert = fs.readFileSync('encrypt/rootCA.pem');
// This line is from the Node.js HTTPS documentation.
var options = {
key: key,
cert: cert
};
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world - https\n");
}).listen(8088);
key /证书生成如下:
openssl genrsa -out rootCA.key 2048
openssl req -x509 -new -nodes -key rootCA.key -sha256 -days 1024 -out rootCA.pem
这是我的 Perl 脚本:
#!/usr/bin/perl
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(GET => 'https://127.0.0.1:8080');
my $res = $ua->request($req);
if ($res->is_success) {
print $res->as_string;
} else {
print "Failed: ", $res->status_line, "\n";
}
返回错误:
Failed: 500 Can't verify SSL peers without knowing which Certificate Authorities to trust
node.js 文档描述了如何设置 HTTPS 服务器,但对生成主证书和中间证书含糊不清。
https://medium.com/netscape/everything-about-creating-an-https-server-using-node-js-2fc5c48a8d4e
最佳答案
要使 LWP::UserAgent 忽略服务器证书,请使用以下配置:
my $ua = LWP::UserAgent->new;
$ua->ssl_opts(
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
verify_hostname => 0
);
关于node.js - 如何使用 LWP::UserAgent 接受自签名证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47662461/