node.js - 如何使用 LWP::UserAgent 接受自签名证书

标签 node.js perl networking https lwp-useragent

我正在尝试设置一个使用 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/

相关文章:

c - 一个二进制文件中的两个过滤器驱动程序导致第二个驱动程序出现系统错误 2

android - Android模拟器中的网络访问

Java - 向整个网络发送UDP广播,但不向自身发送

node.js - 我应该如何存储由 RESTful API 生成的 token ?

javascript - 编译 ejs 时出现语法错误 : Unexpected token. ejs

mysql - 如何打印 Perl 的 DBI 填充占位符后执行的 SQL 查询?

perl - 范围运算符如何处理常量,例如 `print if 2..4` ?

node.js - npm install 在 Mac 上的 Node-gyp 构建错误

node.js - 无法访问中间件内 app.use() 的 req.params

string - Perl Text::CSV_XS 从字符串中读取