Nodejs Creating a TLS service

Source: Internet
Author: User
Tags openssl x509

Nodejs creating the TLS service by Wusheying
Server.js
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 ();});

Certificate Generation:Server.key,client.keyOpenSSL genrsa-out server.key 1024x768OpenSSL genrsa-out client.key 1024x768
CA.CRTOpenSSL genrsa-out ca.key 1024x768OpenSSL req-new-key ca.key-out CA.CSROpenSSL x509-req-in ca.csr-signkey ca.key-out ca.crt
SERVER.CRTOpenSSL req-new-key server.key-out SERVER.CSROpenSSL x509-req-ca ca.crt-cakey ca.key-cacreateserial-in server.csr-out server.crt
CLIENT.CRTOpenSSL req-new-key client.key-out CLIENT.CSROpenSSL x509-req-ca ca.crt-cakey ca.key-cacreateserial-in client.csr-out client.crt

Nodejs Creating a TLS service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.