gpt4 book ai didi

node.js - RequestJS - SSL 连接已授权但 getPeerCertificate() 返回 null

转载 作者:搜寻专家 更新时间:2023-10-31 23:41:52 24 4
gpt4 key购买 nike

我正在使用 Node 包 RequestJS Node v4.1.2 上的 v2.65.0

我正在尝试 read the SSL certificate来自某些站点(例如 GitHub.com)。 这以前适用于 Node 0.12。然而,在 Node 4.2.1 上,getPeerCertificate() 返回 null

例如:

request({
url: 'https://github.com'
}, function (err, res, body) {
console.log('err:', err)
console.log('peerCertificate:',res.req.connection.getPeerCertificate());
console.log('authorized:',res.req.connection.authorized);
console.log('authorizationError:',res.req.connection.authorizationError);
});

会打印出来

err: null
peerCertificate: null
authorized: true
authorizationError: null

即安全连接已建立,但证书为空。

根据我的(基本)理解,如果连接被授权,则应该有对等证书。

我试过很多 SSL 站点,结果都是一样的。请求中是否有一个选项, Node 4 的错误,或者我对 SSL/TLS works in node 的误解?

最佳答案

我认为你的问题是因为 getPeerCertificate() 只会在连接处于 connected 状态时输出任何内容,但是当你收到你的回复时,它可能已经太晚的。

如果您希望 getPeerCertificate 输出,您应该在 TLS 级别独立执行:

const socket = require('tls').connect(443, "github.com", () => {
console.log('client connected',
socket.authorized ? 'authorized' : 'unauthorized');
process.stdin.pipe(socket);
process.stdin.resume();
});

重要! : 不要将协议(protocol)放在 URL 中。相反,使用 require('url').parse(yourURL).hostname 作为目标。

更多信息和例子在这里:https://nodejs.org/api/tls.html#tls_tls_connect_port_host_options_callback

关于node.js - RequestJS - SSL 连接已授权但 getPeerCertificate() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33344016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com