Node.js RequestJS-SSL连接已授权,但getPeerCertificate()返回null
我正在节点v4.1.2上使用节点包v2.65.0 我正试图从某些网站(如GitHub.com)下载这以前在节点0.12上起作用。但是,在节点4.2.1上,Node.js RequestJS-SSL连接已授权,但getPeerCertificate()返回null,node.js,ssl,ssl-certificate,tls1.2,requestjs,Node.js,Ssl,Ssl Certificate,Tls1.2,Requestjs,我正在节点v4.1.2上使用节点包v2.65.0 我正试图从某些网站(如GitHub.com)下载这以前在节点0.12上起作用。但是,在节点4.2.1上,getPeerCertificate()返回null 例如: request({ url: 'https://github.com' }, function (err, res, body) { console.log('err:', err) console.log('peerCertificate:',res.req.conne
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
i、 e.已建立安全连接,但证书为空
根据我的(基本)理解,如果连接被授权,应该有一个对等证书
我尝试了许多SSL站点,结果都是一样的。请求中是否有选项,节点4是否有错误,或者我是否有误解?我认为您的问题是因为
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();
});
重要!:不要将协议放在URL中。而是使用require('url').parse(yourURL).hostname作为目标。
更多信息和示例如下:@nembleton关于发生这种情况的原因是正确的。在这个问题上有一个问题 您可以坚持使用请求并使用其流API,而不是直接使用原始TLS套接字。如果您正在利用其他会使低级连接更加复杂的请求功能(例如,通过HTTPS代理),则此方法特别有用 原始问题中的代码片段变为:
request({
url: 'https://github.com'
})
.on('error', function(err) {
console.log('err:', err);
})
.on('response', function (res) {
console.log('peerCertificate:',res.socket.getPeerCertificate());
console.log('authorized:',res.socket.authorized);
console.log('authorizationError:',res.socket.authorizationError);
});
(为了简洁/直接,我使用了
res.socket
而不是res.req.connection
,但两者都可以。)我不明白为什么它应该工作。您遇到了一个错误:是什么让您认为存在TCP连接?更不用说经过身份验证的SSL连接了?