use greenlock-express to automatically generate ssl

This commit is contained in:
luk.lu
2019-05-30 22:30:30 +08:00
parent b423dfdbf4
commit 238d907459
4 changed files with 60 additions and 59 deletions

View File

@@ -2,10 +2,6 @@ module.exports={
protocol:'http', protocol:'http',
host:'localhost', host:'localhost',
port:undefined, port:undefined,
// 如果使用 https 协议,必须填写以下内容,或在命令行参数中设置:
// sslKey: '/etc/letsencrypt/live/bittic.org/privkey.pem', // ssl key file,
// sslCert: '/etc/letsencrypt/live/bittic.org/cert.pem', // ssl cert file,
// sslCA: '../SSL/ca_bundle.crt', // ssl ca file,
deploy:{ deploy:{
host:'', // 待部署到的主机 host:'', // 待部署到的主机

View File

@@ -1,3 +1,6 @@
module.exports={ module.exports={
// 如果使用 https 协议,必须填写以下内容,或在命令行参数中设置:
// sslKey: '/etc/letsencrypt/live/test.bittic.net/privkey.pem', // ssl key file,
// sslCert: '/etc/letsencrypt/live/test.bittic.net/fullchain.pem', // ssl cert file,
// sslCA: '/etc/letsencrypt/live/test.bittic.net/bundle.crt', // ssl ca file,
} }

View File

@@ -22,6 +22,7 @@
"deepmerge": "^2.2.1", "deepmerge": "^2.2.1",
"errorhandler": "^1.5.0", "errorhandler": "^1.5.0",
"express": "^4.16.2", "express": "^4.16.2",
"greenlock-express": "^2.7.8",
"lodash": "^4.17.11", "lodash": "^4.17.11",
"material-design-icons-iconfont": "^3.0.3", "material-design-icons-iconfont": "^3.0.3",
"method-override": "^2.3.10", "method-override": "^2.3.10",

107
server.js
View File

@@ -1,11 +1,11 @@
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
function config(){ function config() {
const commander = require('commander') const commander = require('commander')
const deepmerge = require('deepmerge') const deepmerge = require('deepmerge')
var Config={} var Config = {}
// 读取配置文件 // 读取配置文件
try { try {
@@ -37,38 +37,47 @@ try {
.parse(process.argv) .parse(process.argv)
// 把命令行参数 合并入配置。 // 把命令行参数 合并入配置。
Config.host=commander.host || Config.host Config.host = commander.host || Config.host
Config.protocol=commander.protocol || Config.protocol // 默认同时启用 http 和 https Config.protocol = commander.protocol || Config.protocol // 默认同时启用 http 和 https
Config.port=parseInt(commander.port) || parseInt(Config.port) || (Config.protocol==='http'?80:Config.protocol==='https'?443:undefined) // 端口默认为 http:80, https:443, httpall: 80|443 Config.port = parseInt(commander.port) || parseInt(Config.port) || (Config.protocol==='http'?80:Config.protocol==='https'?443:undefined) // 端口默认为 http:80, https:443, httpall: 80|443
Config.sslCert=commander.sslCert || Config.sslCert Config.sslCert = commander.sslCert || Config.sslCert
Config.sslKey=commander.sslKey || Config.sslKey Config.sslKey = commander.sslKey || Config.sslKey
Config.sslCA=commander.sslCA || Config.sslCA Config.sslCA = commander.sslCA || Config.sslCA
console.info('Configuration is ready.') console.info('Configuration is ready.')
return Config return Config
} }
async function init(){ /*** 设置全局对象 ***/ async function init() { /*** 设置全局对象 ***/
global.wo={} // wo 代表 world或是当前的命名空间把各种类都放在这里防止和其他库的冲突。 global.wo = {} // wo 代表 world或是当前的命名空间把各种类都放在这里防止和其他库的冲突。
wo.Config=config() // 依次载入系统默认配置、用户配置文件、命令行参数 wo.Config = config() // 依次载入系统默认配置、用户配置文件、命令行参数
} }
(async function start(){ (async function start() {
await init() await init()
var express = require('express') const express = require('express')
var favicon = require('serve-favicon') const logger = require('morgan')
var logger = require('morgan') const cookieParser = require('cookie-parser')
var cookieParser = require('cookie-parser') const bodyParser = require('body-parser')
var bodyParser = require('body-parser')
const compression = require('compression') const compression = require('compression')
var server = express() const server = express()
const greenlock = require('greenlock-express').create({
version: 'draft-11',
server: 'https://acme-v02.api.letsencrypt.org/directory', // for test: acme-staging-v02
agreeTos: true,
communityMember: false,
store: require('greenlock-store-fs'),
email: 'ssl@faronear.org',
approvedDomains: [wo.Config.host],
configDir: path.resolve(__dirname, 'ssl'),
app: server,
})
/*** 通用中间件 ***/ /*** 通用中间件 ***/
// uncomment after placing your favicon in /public
//server.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
server.use(logger('development'===server.get('env')?'dev':'combined')) server.use(logger('development'===server.get('env')?'dev':'combined'))
server.use(bodyParser.json()) server.use(bodyParser.json())
server.use(bodyParser.urlencoded({ extended: false })) server.use(bodyParser.urlencoded({ extended: false }))
@@ -76,46 +85,38 @@ async function init(){ /*** 设置全局对象 ***/
server.use(compression()) server.use(compression())
/*** 路由 ***/ /*** 路由 ***/
server.use(express.static(path.join(__dirname,'dist'), {index:'index.html'})) server.use(express.static(path.join(__dirname, 'dist'), {index:'index.html'}))
//server.use(require('serve-favicon')(path.join(__dirname, 'public', 'favicon.ico'))) // uncomment after placing your favicon in /public
// var vhost = require('vhost')
// var webroot=express.static(path.join(__dirname,'dist'), {index:'index.html'})
// server.use(vhost('bittic.org', webroot))
// server.use(vhost('www.bittic.org', webroot))
// server.use(vhost('*', webroot))
// server.use(vhost('*.*', webroot))
// server.use(vhost('*.*.*', webroot))
// server.use(vhost('*.*.*.*', webroot))
/*** 启动 Web 服务 ***/ /*** 启动 Web 服务 ***/
if ('http'===wo.Config.protocol) { if ('http' === wo.Config.protocol) {
require('http').createServer(server).listen(wo.Config.port, function(err) { require('http').createServer(server).listen(wo.Config.port, function (err) {
console.log('Server listening on %s://%s:%d for %s environment', wo.Config.protocol, wo.Config.host, wo.Config.port, server.settings.env) if (err) console.log(err)
else console.log(`Server listening on ${wo.Config.protocol}://${wo.Config.host}:${wo.Config.port} for ${server.settings.env} environment`)
}) })
}else if ('https'===wo.Config.protocol) { } else if ('https' === wo.Config.protocol) {
require('https').createServer({ // require('https').createServer({
key: fs.readFileSync(wo.Config.sslKey), cert: fs.readFileSync(wo.Config.sslCert) // , ca: [ fs.readFileSync(wo.Config.sslCA) ] // only for self-signed certificate: https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener // key: fs.readFileSync(wo.Config.sslKey),
}, server).listen(wo.Config.port, function(err){ // cert: fs.readFileSync(wo.Config.sslCert) // , ca: [ fs.readFileSync(wo.Config.sslCA) ] // only for self-signed certificate: https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener
console.log('Server listening on %s://%s:%d for %s environment', wo.Config.protocol, wo.Config.host, wo.Config.port, server.settings.env) // }, server).listen(......)
require('https').createServer(greenlock.httpsOptions, server).listen(wo.Config.port, function (err) {
if (err) console.log(err)
else console.log(`Server listening on ${wo.Config.protocol}://${wo.Config.host}:${wo.Config.port} for ${server.settings.env} environment`)
}) })
}else if ('httpall'===wo.Config.protocol) { } else if ('httpall' === wo.Config.protocol) {
let portHttp=wo.Config.port?wo.Config.port:80 // 如果port参数已设置使用它否则默认为80 let portHttp = wo.Config.port ? wo.Config.port : 80 // 如果port参数已设置使用它否则默认为80
require('http').createServer(server).listen(portHttp, function(err) { let portHttps = (wo.Config.port && wo.Config.port !== 80) ? wo.Config.port + 443 : 443 // 如果port参数已设置使用它+443否则默认为443
console.log('Server listening on [%s] http://%s:%d for %s environment', wo.Config.protocol, wo.Config.host, portHttp, server.settings.env) greenlock.listen(portHttp, portHttps, function (err) {
if (err) console.log(err)
else console.log(`Server listening on [${wo.Config.protocol}] http=>https://${wo.Config.host}:${portHttp}=>${portHttps} for ${server.settings.env} environment`)
}) })
} else if ('http2https' === wo.Config.protocol) {
let portHttps=(wo.Config.port && wo.Config.port!==80)?wo.Config.port+443:443 // 如果port参数已设置使用它+443否则默认为443
require('https').createServer({
key: fs.readFileSync(wo.Config.sslKey), cert: fs.readFileSync(wo.Config.sslCert) // , ca: [ fs.readFileSync(wo.Config.sslCA) ] // https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener
}, server).listen(portHttps, function(err){
console.log('Server listening on [%s] https://%s:%d for %s environment', wo.Config.protocol, wo.Config.host, portHttps, server.settings.env)
})
}else if ('http2https'===wo.Config.protocol) {
wo.Config.port = wo.Config.port || 80 wo.Config.port = wo.Config.port || 80
require('http').createServer(express().all('*', function(ask, reply){ /* 错误的API调用进入这里。*/ require('http').createServer(express().all('*', function (ask, reply) { /* 错误的API调用进入这里。*/
reply.redirect(`https://${wo.Config.host}`) reply.redirect(`https://${wo.Config.host}`)
})).listen(wo.Config.port, function(err){ })).listen(wo.Config.port, function (err) {
console.log('Server listening on [%s] http://%s:%d for %s environment', wo.Config.protocol, wo.Config.host, wo.Config.port, server.settings.env) if (err) console.log(err)
else console.log(`Server listening on ${wo.Config.protocol}://${wo.Config.host}:${wo.Config.port} for ${server.settings.env} environment`)
}) })
} }