準備
秘密鍵(server-key.pem)と自己署名証明書(server-cert.pem)を作ります
$ openssl genrsa -out server-key.pem 1024
$ openssl req -new -key server-key.pem -out server-csr.pem
$ openssl x509 -req -in server-csr.pem -signkey server-key.pem -out server-cert.pem
サーバー側
server.js
var tls = require('tls');
var fs = require('fs');
var options = {
key: fs.readFileSync('server-key.pem'),
cert: fs.readFileSync('server-cert.pem')
};
tls.createServer(options, function (socket) {
socket.write("welcome!\n");
socket.on('data', function(data){
console.log(data);
console.log(data.toString());
});
}).listen(8000);
クライアント側
client.rb
require "openssl"
require "socket"
tcp_socket = TCPSocket.new("127.0.0.1", 8000)
ssl_socket = OpenSSL::SSL::SSLSocket.new(tcp_socket)
ssl_socket.connect
puts ssl_socket.gets
ssl_socket.write "hello, world from client"
ssl_socket.close
tcp_socket.close
実行
クライアント側
$ ruby client.rb
welcome!
$
サーバー側
$ node server.js
<Buffer 68 65 6c 6c 6f 2c 20 77 6f 72 6c 64 20 66 72 6f 6d 20 63 6c 69 65 6e 74>
hello, world from client