用Node.js创建自签名的HTTPS服务器

  1. 创建自己的CA机构
  2. 创建服务器端证书
  3. 创建客户端证书
  4. 将证书打包

创建自己的CA机构

  • 为CA生成私钥

openssl genrsa -out ca-key.pem -des 1024

  • 通过CA私钥生成CSR

openssl req -new -key ca-key.pem -out ca-csr.pem

  • 通过CSR文件和私钥生成CA证书

openssl x509 -req -in ca-csr.pem -signkey ca-key.pem -out ca-cert.pem

可能会遇到的问题

你需要root或者admin的权限 Unable to load config info from /user/local/ssl/openssl.cnf 对于这个问题,你可以从网上下载一份正确的openssl.cnf文件, 然后set OPENSSL_CONF=openssl.cnf文件的本地路径

创建服务器端证书

  • 为服务器生成私钥

openssl genrsa -out server-key.pem 1024

  • 利用服务器私钥文件服务器生成CSR

openssl req -new -key server-key.pem -config openssl.cnf -out server-csr.pem

这一步非常关键,你需要指定一份openssl.cnf文件。可以用这个

[req]
distinguished_name = req_distinguished_name
req_extensions = v3_req [req_distinguished_name]
countryName = Country Name (2 letter code)
countryName_default = CN
stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default = BeiJing
localityName = Locality Name (eg, city)
localityName_default = YaYunCun
organizationalUnitName = Organizational Unit Name (eg, section)
organizationalUnitName_default = Domain Control Validated
commonName = Internet Widgits Ltd
commonName_max = 64 [ v3_req ]
# Extensions to add to a certificate request
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names [alt_names]
#注意这个IP.1的设置,IP地址需要和你的服务器的监听地址一样
IP.1 = 127.0.0.1
  • 通过服务器私钥文件和CSR文件生成服务器证书

openssl x509 -req -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -in server-csr.pem -out server-cert.pem -extensions v3_req -extfile openssl.cnf

创建客户端证书

  • 生成客户端私钥

openssl genrsa -out client-key.pem

  • 利用私钥生成CSR

openssl req -new -key client-key.pem -out client-csr.pem

  • 生成客户端证书

openssl x509 -req -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -in client-csr.pem -out client-cert.pem

HTTPS 服务器代码

var https = require('https');
var fs = require('fs'); var options = {
key: fs.readFileSync('./keys/server-key.pem'),
ca: [fs.readFileSync('./keys/ca-cert.pem')],
cert: fs.readFileSync('./keys/server-cert.pem')
}; https.createServer(options,function(req,res){
res.writeHead(200);
res.end('hello world\n');
}).listen(3000,'127.0.0.1');

HTTPS 客户端代码

var https = require('https');
var fs = require('fs'); var options = {
hostname:'127.0.0.1',
port:3000,
path:'/',
method:'GET',
key:fs.readFileSync('./keys/client-key.pem'),
cert:fs.readFileSync('./keys/client-cert.pem'),
ca: [fs.readFileSync('./keys/ca-cert.pem')],
agent:false
}; options.agent = new https.Agent(options);
var req = https.request(options,function(res){
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.setEncoding('utf-8');
res.on('data',function(d){
console.log(d);
})
}); req.end(); req.on('error',function(e){
console.log(e);
})

将证书打包

  • 打包服务器端证书

openssl pkcs12 -export -in server-cert.pem -inkey server-key.pem -certfile ca-cert.pem -out server.pfx

  • 打包客户端证书

openssl pkcs12 -export -in client-cert.pem -inkey client-key.pem -certfile ca-cert.pem -out client.pfx

服务器端代码

var https = require('https');
var fs = require('fs'); var options = {
pfx:fs.readFileSync('./keys/server.pfx'),
passphrase:'your password'
}; https.createServer(options,function(req,res){
res.writeHead(200);
res.end('hello world\n');
}).listen(3000,'127.0.0.1');

客户端代码

var https = require('https');
var fs = require('fs'); var options = {
hostname:'127.0.0.1',
port:3000,
path:'/',
method:'GET',
pfx:fs.readFileSync('./keys/server.pfx'),
passphrase:'your password',
agent:false
}; options.agent = new https.Agent(options);
var req = https.request(options,function(res){
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.setEncoding('utf-8');
res.on('data',function(d){
console.log(d);
})
}); req.end(); req.on('error',function(e){
console.log(e);
}) 原文地址:https://cnodejs.org/topic/54745ac22804a0997d38b32d

[转]用Node.js创建自签名的HTTPS服务器的更多相关文章

  1. Node.js创建自签名的HTTPS服务器

    https://cnodejs.org/topic/54745ac22804a0997d38b32d  用Node.js创建自签名的HTTPS服务器  发布于 4 年前  作者 eeandrew  6 ...

  2. Node.js 创建HTTP服务器

    Node.js 创建HTTP服务器 如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi. 从这个角度看,整个& ...

  3. Node.js 创建HTTP服务器(经过测试,这篇文章是靠谱的T_T)

    Node.js 创建HTTP服务器 如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi. 从这个角度看,整个& ...

  4. node.js在windows下的学习笔记(5)---用NODE.JS创建服务器和客户端

    //引入http模块 var http = require('http'); //调用http的createServer的方法,这个方法有一个回调函数,这个回调数 //的作用是当有请求发送给服务器的时 ...

  5. Node.js创建服务器和模拟客户端请求

    1. 何为服务器 服务器是某种长期运行,等待请求资源的应用程序 2. 常见Web应用架构 3. 如何创建web服务器 Web服务器是使用HTTP协议,等待客户端连接后请求资源的驻守应用程序:HTTP协 ...

  6. 极简 Node.js 入门 - 5.3 静态资源服务器

    极简 Node.js 入门系列教程:https://www.yuque.com/sunluyong/node 本文更佳阅读体验:https://www.yuque.com/sunluyong/node ...

  7. 前端使用node.js的http-server开启一个本地服务器

    前端使用node.js的http-server开启一个本地服务器 在写前端页面中,经常会在浏览器运行HTML页面,从本地文件夹中直接打开的一般都是file协议,当代码中存在http或https的链接时 ...

  8. Node.js 创建第一个应用

    如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi. 从这个角度看,整个"接收 HTTP 请求并提供 ...

  9. 使用node.js创建第一个应用

    node.js应用组成部分 1.引入require模块:我们可以使用require指令来载入Node.js模块. 2.创建服务器:服务器可以监听客户端的请求,类似于Apache,Nninx等HTTP服 ...

随机推荐

  1. FIR滤波器设计

    FIR滤波器的优越性: 相位对应为严格的线性,不存在延迟失真,仅仅有固定的时间延迟: 因为不存在稳定性问题,设计相对简单: 仅仅包括实数算法,不涉及复数算法,不须要递推运算,长度为M,阶数为M-1,计 ...

  2. Ftp实现文件同步

    通常在做服务器与服务器文件.服务器与本地文件同步时通过Ftp服务实现,下面就以服务器文件和本地同步为例,介绍一下Ftp同步文件:首先建立一个Ftp站点服务,基本身份验证登陆,端口号为默认的21:Ftp ...

  3. ASP.NET获取用户端的真实IP

    ASP.NET获取用户端的真实IP在各种场景都能用到,但是用户网端变幻莫测情况众多,获取真实IP还真是不容易啊.下面分享个比较好一点的方法: 获取IP初始版本 /// <summary> ...

  4. mysql求时间差

    SELECT TIMESTAMPDIFF(SECOND, now(), "2012-11-11 00:00:00") 语法为:TIMESTAMPDIFF(unit,datetime ...

  5. C#去除byte数组头尾杂质(即不需要的数据)

    代码如下: /// <summary> /// 去除byte数组头尾杂质(即不需要的数据) /// </summary> /// <param name="ar ...

  6. css属性之vertical-align详解

    inline-block 该值会让元素生成一个内联级块容器(inline-level block container).一个inline-block的内部会被格式化成一个块盒,而该元素本身会被格式化成 ...

  7. ORA-02447: cannot defer a constraint that is not deferrable

    一个constraint如果被定义成deferrable那么这个constraints可以在deferred和imediate两种状态相互转换. deferred只在transaction中有效,也就 ...

  8. C#--virtual,abstract,override,new,sealed

    virtual:使用此关键字,可以使其在派生类中被重写. abstract:抽象方法,由子类重写,或继续为抽象方法存在,并由其子子类实现. override: 重写父类方法,属性,或事件的抽象实现或虚 ...

  9. C++中的struct与class继承方式

    代码: #include <iostream> #include <cstdio> using namespace std; //class A{ struct A{ publ ...

  10. github 推送时can't be established.

    http://www.xuebuyuan.com/2095099.html 飞凡@FANZ /e/learngit (master)$ git push origin masterThe authen ...