在 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: [
|
commanderOptions: [
|
||||||
// 命令行里可以接受的参数。将传给 config.js 里的 commander。每个参数的定义格式是 [参数名,参数键,描述],后两者用于传给commander,取值后覆盖掉Config里的同名变量。
|
// 命令行里可以接受的参数。将传给 config.js 里的 commander。每个参数的定义格式是 [参数名,参数键,描述],后两者用于传给commander,取值后覆盖掉Config里的同名变量。
|
||||||
['protocol', '-P, --protocol <string>', 'Web Server protocol: http|https|httpall.'],
|
['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.'],
|
['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.'],
|
['ssl', '--ssl <string>', 'SSL options in JSON string.'],
|
||||||
],
|
],
|
||||||
// 最基础的必须的默认配置,如果用户什么也没有提供
|
// 最基础的必须的默认配置,如果用户什么也没有提供
|
||||||
@ -85,19 +85,21 @@ if (typeof wo.envi.ssl === 'string') {
|
|||||||
/*** 启动 Web 服务 ***/
|
/*** 启动 Web 服务 ***/
|
||||||
let webServer
|
let webServer
|
||||||
let ipv4 = nettool.getMyIp()
|
let ipv4 = nettool.getMyIp()
|
||||||
let portHttp = wo.envi.port || 80
|
|
||||||
let portHttps = wo.envi.port || 443
|
|
||||||
if ('http' === wo.envi.protocol) {
|
if ('http' === wo.envi.protocol) {
|
||||||
|
let portHttp = wo.envi.port || 80
|
||||||
webServer = require('http')
|
webServer = require('http')
|
||||||
.createServer(server)
|
.createServer(server)
|
||||||
.listen(portHttp, function (err) {
|
.listen(portHttp, function (err) {
|
||||||
if (err) console.log(err)
|
if (err) {
|
||||||
else
|
console.log(err)
|
||||||
|
} else {
|
||||||
console.log(
|
console.log(
|
||||||
`[${new Date().toJSON()}] Server listening on ${wo.envi.protocol}://${wo.envi.host}:${portHttp} with IPv4=${ipv4} for ${wo.envi.prodev} environment`
|
`[${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) {
|
} else if ('https' === wo.envi.protocol) {
|
||||||
|
let portHttps = wo.envi.port || 443
|
||||||
webServer = require('https')
|
webServer = require('https')
|
||||||
.createServer(
|
.createServer(
|
||||||
// wo.wo.envi.ssl.type==='greenlock' ? greenlock.httpsOptions :
|
// wo.wo.envi.ssl.type==='greenlock' ? greenlock.httpsOptions :
|
||||||
@ -109,14 +111,17 @@ if (typeof wo.envi.ssl === 'string') {
|
|||||||
server
|
server
|
||||||
)
|
)
|
||||||
.listen(portHttps, function (err) {
|
.listen(portHttps, function (err) {
|
||||||
if (err) console.log(err)
|
if (err) {
|
||||||
else
|
console.log(err)
|
||||||
|
} else {
|
||||||
console.log(
|
console.log(
|
||||||
`[${new Date().toJSON()}] Server listening on ${wo.envi.protocol}://${wo.envi.host}:${portHttps} with IPv4=${ipv4} for ${wo.envi.prodev} environment`
|
`[${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) {
|
} 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') {
|
// if (wo.wo.envi.ssl.type==='greenlock') {
|
||||||
// greenlock.listen(portHttp, portHttps, function (err) {
|
// greenlock.listen(portHttp, portHttps, function (err) {
|
||||||
// if (err) console.log(err)
|
// if (err) console.log(err)
|
||||||
@ -130,13 +135,15 @@ if (typeof wo.envi.ssl === 'string') {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
.listen(portHttp, function (err) {
|
.listen(portHttp, function (err) {
|
||||||
if (err) console.log(err)
|
if (err) {
|
||||||
else
|
console.log(err)
|
||||||
|
} else {
|
||||||
console.log(
|
console.log(
|
||||||
`[${new Date().toJSON()}] Server redirecting from http://${wo.envi.host}:${portHttp} with IPv4=${ipv4} for ${
|
`[${new Date().toJSON()}] Server redirecting from http://${wo.envi.host}:${portHttp} with IPv4=${ipv4} for ${
|
||||||
wo.envi.prodev
|
wo.envi.prodev
|
||||||
} environment`
|
} environment`
|
||||||
)
|
)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
webServer = require('https')
|
webServer = require('https')
|
||||||
.createServer(
|
.createServer(
|
||||||
@ -148,35 +155,42 @@ if (typeof wo.envi.ssl === 'string') {
|
|||||||
server
|
server
|
||||||
)
|
)
|
||||||
.listen(portHttps, function (err) {
|
.listen(portHttps, function (err) {
|
||||||
if (err) console.log(err)
|
if (err) {
|
||||||
else
|
console.log(err)
|
||||||
|
} else {
|
||||||
console.log(
|
console.log(
|
||||||
`[${new Date().toJSON()}] Server listening on https://${wo.envi.host}:${portHttps} with IPv4=${ipv4} for ${
|
`[${new Date().toJSON()}] Server listening on https://${wo.envi.host}:${portHttps} with IPv4=${ipv4} for ${
|
||||||
wo.envi.prodev
|
wo.envi.prodev
|
||||||
} environment`
|
} environment`
|
||||||
)
|
)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
// }
|
// }
|
||||||
} else if ('redirectHttp2Https' === wo.envi.protocol) {
|
} 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) {
|
webServer = server.all('*', function (ask, reply) {
|
||||||
reply.redirect(301, `https://${ask.headers.host.replace(`:${portHttp}`, `:${portHttps}`)}${ask.url}`)
|
reply.redirect(301, `https://${ask.headers.host.replace(`:${portHttp}`, `:${portHttps}`)}${ask.url}`)
|
||||||
})
|
})
|
||||||
.listen(portHttp, function (err) {
|
.listen(portHttp, function (err) {
|
||||||
if (err) console.log(err)
|
if (err) {
|
||||||
else
|
console.log(err)
|
||||||
|
} else {
|
||||||
console.log(
|
console.log(
|
||||||
`[${new Date().toJSON()}] Server listening on ${wo.envi.protocol}://${wo.envi.host}:${portHttp} with IPv4=${ipv4} for ${wo.envi.prodev} environment`
|
`[${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) {
|
}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({
|
var proxy=require('http-proxy').createProxyServer({
|
||||||
ssl: {
|
ssl: {
|
||||||
key: fs.readFileSync(wo.envi.ssl.file.key),
|
key: fs.readFileSync(wo.envi.ssl.file.key),
|
||||||
cert: fs.readFileSync(wo.envi.ssl.file.cert),
|
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
|
// 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
|
// secure: true, // proxying https to https
|
||||||
ws: true // proxying websockets
|
ws: true // proxying websockets
|
||||||
})
|
})
|
||||||
@ -185,11 +199,13 @@ if (typeof wo.envi.ssl === 'string') {
|
|||||||
res.end('Proxy Error.')
|
res.end('Proxy Error.')
|
||||||
})
|
})
|
||||||
proxy.listen(portHttps, function (err) {
|
proxy.listen(portHttps, function (err) {
|
||||||
if (err) console.log(err)
|
if (err) {
|
||||||
else
|
console.log(err)
|
||||||
|
} else {
|
||||||
console.log(
|
console.log(
|
||||||
`[${new Date().toJSON()}] Server listening on ${wo.envi.protocol}://${wo.envi.host}:${portHttps} with IPv4=${ipv4} for ${wo.envi.prodev} environment`
|
`[${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