将https代理configuration为只允许TLS1.2进行传出请求

我正在使用客户端证书从节点应用程序进行HTTPS连接:

var options = { hostname: 'https://my-server.com', port: 443, path: '/', method: 'GET', key: fs.readFileSync('client1-key.pem'), cert: fs.readFileSync('client1-crt.pem'), ca: fs.readFileSync('ca-crt.pem') }; var req = https.request(options, res => { [...] }); 

一切工作正常,但是我想添加代码,以确保只允许TLS 1.2连接。 我找不到任何方法来configurationhttps.agent选项或其他地方。 有没有可能configuration这个,或者我必须build立一个连接,然后查询协议版本,如下所示:

res.socket.getProtocol() === 'TLSv1.2'

如果协议不满意就中止连接?

首先,我find了有关制作HTTPS请求的文档。 它提到您可以将其他选项传递给tls.connect() ,其中包含一些名为secureProtocol 。 挖掘到tls.connect() ,我发现了提到tls.createSecureContext()secureContext选项。 在那里它最后提到了可以用OpenSSL页面中的string指定的secureProtocol 。 我select了一个看起来合理的string( TLSv1_2_method ),并将secureProtocol选项直接传递给https.request

这将打印SSL Version: TLS 1.2secureProtocolSSL Version: TLS 1.1secureProtocol: "TLSv1_1_method" 。 如果使用给定的TLS版本无法build立连接,则会调用最后的error handling程序。

 var https = require('https') var options = { hostname: 'www.howsmyssl.com', port: 443, path: '/a/check', method: 'GET', secureProtocol: "TLSv1_2_method" } https.request(options, res => { let body = '' res.on('data', d => body += d) res.on('end', () => { data = JSON.parse(body) console.log('SSL Version: ' + data.tls_version) }) }).on('error', err => { // This gets called if a connection cannot be established. console.warn(err) }).end()