增加 http2https 选项,把所有请求重定向到 https://${wo.Config.host}

This commit is contained in:
luk.lu
2019-01-24 21:03:40 +08:00
parent 08e14dc1f9
commit 5073c86cfe

View File

@@ -28,11 +28,11 @@ try {
// 载入命令行参数
commander
.version(Config.VERSION, '-v, --version') // 默认是 -V。如果要 -v就要加 '-v --version'
.option('-H, --host <host>', 'host ip or domain name')
.option('-P, --protocol <protocol>', 'Web server protocol http|https|httpall, default to httpall')
.option('-p, --port <port>', 'Server port, default to 80|443')
.option('--sslCert <cert>', 'SSL cert file')
.option('--sslKey <key>', 'SSL privkey file')
.option('-H, --host <host>', 'Host ip or domain name. Default to ' + Config.host)
.option('-P, --protocol <protocol>', 'Web server protocol http|https|httpall|http2https. Default to ' + Config.protocol)
.option('-p, --port <port>', `Server port. Default to ${Config.port?Config.port:'80|443'}`)
.option('--sslCert <cert>', 'SSL cert file. Default to ' + Config.sslCert)
.option('--sslKey <key>', 'SSL privkey file. Default to ' + Config.sslKey)
.option('--sslCA <ca>', 'SSL ca bundle file')
.parse(process.argv)
@@ -101,14 +101,21 @@ async function init(){ /*** 设置全局对象 ***/
}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://%s:%d for %s environment', 'httpall:http', wo.Config.host, portHttp, server.settings.env)
console.log('Server listening on [%s] http://%s:%d for %s environment', wo.Config.protocol, wo.Config.host, portHttp, server.settings.env)
})
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://%s:%d for %s environment', 'httpall:https', wo.Config.host, portHttps, server.settings.env)
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
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)
})
}