在 httpall 时,定义 port: {portHttp, portHttps}
This commit is contained in:
parent
3129a23c07
commit
ea60281bf3
54
server.js
54
server.js
@ -8,9 +8,9 @@ const wo = (global.wo = {
|
||||
commanderOptions: [
|
||||
// 命令行里可以接受的参数。将传给 config.js 里的 commander。每个参数的定义格式是 [参数名,参数键,描述],后两者用于传给commander,取值后覆盖掉Config里的同名变量。
|
||||
['protocol', '-P, --protocol <string>', 'Web Server protocol: http|https|httpall.'],
|
||||
['host', '-H, --host <string>', 'Host IP or domain name, default to localhost.'],
|
||||
['host', '-H, --host <string>', 'Host IP or domain name.'],
|
||||
['port', '-p, --port <number>', 'HTTP port number.'],
|
||||
['webroot', '-w, --webroot <string>', 'Path to serve as website'],
|
||||
['webroot', '-w, --webroot <string>', 'Path to serve as website.'],
|
||||
['ssl', '--ssl <string>', 'SSL options in JSON string.'],
|
||||
],
|
||||
// 最基础的必须的默认配置,如果用户什么也没有提供
|
||||
@ -85,19 +85,21 @@ if (typeof wo.envi.ssl === 'string') {
|
||||
/*** 启动 Web 服务 ***/
|
||||
let webServer
|
||||
let ipv4 = nettool.getMyIp()
|
||||
let portHttp = wo.envi.port || 80
|
||||
let portHttps = wo.envi.port || 443
|
||||
if ('http' === wo.envi.protocol) {
|
||||
let portHttp = wo.envi.port || 80
|
||||
webServer = require('http')
|
||||
.createServer(server)
|
||||
.listen(portHttp, function (err) {
|
||||
if (err) console.log(err)
|
||||
else
|
||||
if (err) {
|
||||
console.log(err)
|
||||
} else {
|
||||
console.log(
|
||||
`[${new Date().toJSON()}] Server listening on ${wo.envi.protocol}://${wo.envi.host}:${portHttp} with IPv4=${ipv4} for ${wo.envi.prodev} environment`
|
||||
)
|
||||
}
|
||||
})
|
||||
} else if ('https' === wo.envi.protocol) {
|
||||
let portHttps = wo.envi.port || 443
|
||||
webServer = require('https')
|
||||
.createServer(
|
||||
// wo.wo.envi.ssl.type==='greenlock' ? greenlock.httpsOptions :
|
||||
@ -109,14 +111,17 @@ if (typeof wo.envi.ssl === 'string') {
|
||||
server
|
||||
)
|
||||
.listen(portHttps, function (err) {
|
||||
if (err) console.log(err)
|
||||
else
|
||||
if (err) {
|
||||
console.log(err)
|
||||
} else {
|
||||
console.log(
|
||||
`[${new Date().toJSON()}] Server listening on ${wo.envi.protocol}://${wo.envi.host}:${portHttps} with IPv4=${ipv4} for ${wo.envi.prodev} environment`
|
||||
)
|
||||
}
|
||||
})
|
||||
} else if ('httpall' === wo.envi.protocol) {
|
||||
portHttp = 80
|
||||
let portHttp = wo.envi.port?.portHttp || 80
|
||||
let portHttps = wo.envi.port?.portHttps || 443
|
||||
// if (wo.wo.envi.ssl.type==='greenlock') {
|
||||
// greenlock.listen(portHttp, portHttps, function (err) {
|
||||
// if (err) console.log(err)
|
||||
@ -130,13 +135,15 @@ if (typeof wo.envi.ssl === 'string') {
|
||||
}
|
||||
)
|
||||
.listen(portHttp, function (err) {
|
||||
if (err) console.log(err)
|
||||
else
|
||||
if (err) {
|
||||
console.log(err)
|
||||
} else {
|
||||
console.log(
|
||||
`[${new Date().toJSON()}] Server redirecting from http://${wo.envi.host}:${portHttp} with IPv4=${ipv4} for ${
|
||||
wo.envi.prodev
|
||||
} environment`
|
||||
)
|
||||
}
|
||||
})
|
||||
webServer = require('https')
|
||||
.createServer(
|
||||
@ -148,35 +155,42 @@ if (typeof wo.envi.ssl === 'string') {
|
||||
server
|
||||
)
|
||||
.listen(portHttps, function (err) {
|
||||
if (err) console.log(err)
|
||||
else
|
||||
if (err) {
|
||||
console.log(err)
|
||||
} else {
|
||||
console.log(
|
||||
`[${new Date().toJSON()}] Server listening on https://${wo.envi.host}:${portHttps} with IPv4=${ipv4} for ${
|
||||
wo.envi.prodev
|
||||
} environment`
|
||||
)
|
||||
}
|
||||
})
|
||||
// }
|
||||
} else if ('redirectHttp2Https' === wo.envi.protocol) {
|
||||
portHttp = 80
|
||||
let portHttp = wo.envi.port?.portHttp || 80
|
||||
let portHttps = wo.envi.port?.portHttps || 443
|
||||
webServer = server.all('*', function (ask, reply) {
|
||||
reply.redirect(301, `https://${ask.headers.host.replace(`:${portHttp}`, `:${portHttps}`)}${ask.url}`)
|
||||
})
|
||||
.listen(portHttp, function (err) {
|
||||
if (err) console.log(err)
|
||||
else
|
||||
if (err) {
|
||||
console.log(err)
|
||||
} else {
|
||||
console.log(
|
||||
`[${new Date().toJSON()}] Server listening on ${wo.envi.protocol}://${wo.envi.host}:${portHttp} with IPv4=${ipv4} for ${wo.envi.prodev} environment`
|
||||
)
|
||||
}
|
||||
})
|
||||
}else if ('proxyHttps2Http' === wo.envi.protocol) {
|
||||
let portHttp = wo.envi.port?.portHttp || 80
|
||||
let portHttps = wo.envi.port?.portHttps || 443
|
||||
var proxy=require('http-proxy').createProxyServer({
|
||||
ssl: {
|
||||
key: fs.readFileSync(wo.envi.ssl.file.key),
|
||||
cert: fs.readFileSync(wo.envi.ssl.file.cert),
|
||||
// ca: [ fs.readFileSync(wo.envi.sslCA) ] // https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener
|
||||
},
|
||||
target: `http://127.0.0.1:${wo.envi.proxyPort}`, // iOS 的 AppStore 要求支持IPv6,只能用国外的vultr.com服务器,因此再代理回国内的solet主机。
|
||||
target: `http://127.0.0.1:${portHttp}`, // iOS 的 AppStore 要求支持IPv6,只能用国外的vultr.com服务器,因此再代理回国内的solet主机。
|
||||
// secure: true, // proxying https to https
|
||||
ws: true // proxying websockets
|
||||
})
|
||||
@ -185,11 +199,13 @@ if (typeof wo.envi.ssl === 'string') {
|
||||
res.end('Proxy Error.')
|
||||
})
|
||||
proxy.listen(portHttps, function (err) {
|
||||
if (err) console.log(err)
|
||||
else
|
||||
if (err) {
|
||||
console.log(err)
|
||||
} else {
|
||||
console.log(
|
||||
`[${new Date().toJSON()}] Server listening on ${wo.envi.protocol}://${wo.envi.host}:${portHttps} with IPv4=${ipv4} for ${wo.envi.prodev} environment`
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user