nodejs创建TLS服务
nodejs创建TLS服务
var tls = require('tls'); var fs = require('fs'); var options = { key: fs.readFileSync('./keys/server.key'), cert: fs.readFileSync('./keys/server.crt'), requestCert: true, ca: [ fs.readFileSync('./keys/ca.crt')] }; var server = tls.createServer(options,function(stream) { console.log('server connected',stream.authorized?'authorized':'unauthorized'); stream.write("welcome!\n"); stream.setEncoding('utf8'); stream.pipe(stream); }); server.listen(8000,function() { console.log('server bound'); });
client.js
var tls = require('tls'); var fs = require('fs'); var options = { key: fs.readFileSync('./keys/client.key'), cert: fs.readFileSync('./keys/client.crt'), ca: [ fs.readFileSync('./keys/ca.crt')] }; var stream = tls.connect(8000,options,function() { console.log('client connected',stream.authorized?'authorized':'unauthorized'); process.stdin.pipe(stream); }); stream.setEncoding('utf8'); stream.on('data',function(data) { console.log(data); }); stream.on('end',function() { server.close(); });
证书生成:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。