diff --git a/ConfigBasic.js b/ConfigBasic.js index 5b2606a..814512c 100644 --- a/ConfigBasic.js +++ b/ConfigBasic.js @@ -2,10 +2,6 @@ module.exports={ protocol:'http', host:'localhost', 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:{ host:'', // 待部署到的主机 diff --git a/ConfigCustom.js b/ConfigCustom.js index 054eb83..5f374e1 100644 --- a/ConfigCustom.js +++ b/ConfigCustom.js @@ -1,3 +1,6 @@ module.exports={ - -} \ No newline at end of file + // 如果使用 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, +} diff --git a/package.json b/package.json index c0e508b..e8e2c88 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "deepmerge": "^2.2.1", "errorhandler": "^1.5.0", "express": "^4.16.2", + "greenlock-express": "^2.7.8", "lodash": "^4.17.11", "material-design-icons-iconfont": "^3.0.3", "method-override": "^2.3.10", diff --git a/server.js b/server.js index 8020a9b..a2ed1fc 100644 --- a/server.js +++ b/server.js @@ -1,11 +1,11 @@ const fs = require('fs') const path = require('path') -function config(){ +function config() { const commander = require('commander') const deepmerge = require('deepmerge') - var Config={} + var Config = {} // 读取配置文件 try { @@ -37,38 +37,47 @@ try { .parse(process.argv) // 把命令行参数 合并入配置。 - Config.host=commander.host || Config.host - 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.sslCert=commander.sslCert || Config.sslCert - Config.sslKey=commander.sslKey || Config.sslKey - Config.sslCA=commander.sslCA || Config.sslCA + Config.host = commander.host || Config.host + 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.sslCert = commander.sslCert || Config.sslCert + Config.sslKey = commander.sslKey || Config.sslKey + Config.sslCA = commander.sslCA || Config.sslCA console.info('Configuration is ready.') return Config } -async function init(){ /*** 设置全局对象 ***/ - global.wo={} // wo 代表 world或‘我’,是当前的命名空间,把各种类都放在这里,防止和其他库的冲突。 - wo.Config=config() // 依次载入系统默认配置、用户配置文件、命令行参数 +async function init() { /*** 设置全局对象 ***/ + global.wo = {} // wo 代表 world或‘我’,是当前的命名空间,把各种类都放在这里,防止和其他库的冲突。 + wo.Config = config() // 依次载入系统默认配置、用户配置文件、命令行参数 } -(async function start(){ +(async function start() { await init() - var express = require('express') - var favicon = require('serve-favicon') - var logger = require('morgan') - var cookieParser = require('cookie-parser') - var bodyParser = require('body-parser') + const express = require('express') + const logger = require('morgan') + const cookieParser = require('cookie-parser') + const bodyParser = require('body-parser') 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(bodyParser.json()) server.use(bodyParser.urlencoded({ extended: false })) @@ -76,46 +85,38 @@ async function init(){ /*** 设置全局对象 ***/ server.use(compression()) /*** 路由 ***/ - server.use(express.static(path.join(__dirname,'dist'), {index:'index.html'})) - -// 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)) + 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 /*** 启动 Web 服务 ***/ - if ('http'===wo.Config.protocol) { - 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 ('http' === wo.Config.protocol) { + require('http').createServer(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 ('https'===wo.Config.protocol) { - 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 - }, 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) + } else if ('https' === wo.Config.protocol) { + // 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 + // }, 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) { - let portHttp=wo.Config.port?wo.Config.port:80 // 如果port参数已设置,使用它;否则默认为80 - require('http').createServer(server).listen(portHttp, function(err) { - console.log('Server listening on [%s] http://%s:%d for %s environment', wo.Config.protocol, wo.Config.host, portHttp, server.settings.env) + } else if ('httpall' === wo.Config.protocol) { + let portHttp = wo.Config.port ? wo.Config.port : 80 // 如果port参数已设置,使用它;否则默认为80 + let portHttps = (wo.Config.port && wo.Config.port !== 80) ? wo.Config.port + 443 : 443 // 如果port参数已设置,使用它+443;否则默认为443 + 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`) }) - - 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) { + } else if ('http2https' === wo.Config.protocol) { 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}`) - })).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) + })).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`) }) }