-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebmodule.js
87 lines (76 loc) · 2.86 KB
/
webmodule.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const fs = require('fs')
const http = require('http')
const path = require('path')
const express = require('express')
const compression = require('compression')
const { CONFIG, CONFIG_Port } = require('./config')
const { crtHost } = require('./func')
const { isAuthReq, logger, websocketSer } = require('./utils')
const clog = new logger({ head: 'webServer' })
const { wbefss, wbconfig, wbfeed, wbcrt, wbjs, wbtask, wblogs, wbstore, wbdata, wblist, wbhook, wbrpc, wbrun, wbeapp } = require('./webser')
async function newServer(app) {
if (CONFIG?.webUI?.tls?.enable) {
const host = CONFIG.webUI.tls.host || '127.0.0.1'
try {
if (!(fs.existsSync(`rootCA/${host}.key`) && fs.existsSync(`rootCA/${host}.crt`))) {
await crtHost(host)
}
clog.notify('enable TLS for webUI, HOST:', host)
return require('https').createServer({
key: fs.readFileSync(`rootCA/${host}.key`),
cert: fs.readFileSync(`rootCA/${host}.crt`)
}, app)
} catch(error) {
clog.error('fail to enable TLS for webUI, reason:', error)
}
}
return http.createServer(app)
}
module.exports = () => {
const app = express()
app.use(express.json({ limit: '10mb' }))
app.set('json spaces', 2)
app.use((req, res, next)=>{
if (isAuthReq(req, res)) {
if (CONFIG.cors?.enable && CONFIG.cors?.origin) {
res.set({ 'Access-Control-Allow-Origin': CONFIG.cors.origin})
}
next()
} else {
res.status(403).send(`<p>You have no permission to access.</p><p>IP: ${req.headers['x-forwarded-for'] || req.connection.remoteAddress} is recorded.</p><br><p>Powered BY elecV2P: <a href='https://github.com/elecV2/elecV2P'>https://github.com/elecV2/elecV2P</a></p>`)
}
})
wbrpc(app)
app.use(compression())
app.use(express.static(path.resolve(__dirname, 'web/dist')))
app.use(express.text({ type: 'text/*' }))
app.use(express.raw())
wbconfig(app)
wbfeed(app)
wbcrt(app)
wbjs(app)
wbtask(app)
wblogs(app)
wbstore(app)
wbdata(app)
wblist(app)
wbhook(app)
wbefss(app)
wbrun(app)
wbeapp(app)
app.use((req, res, next) => {
res.status(404).send(`<p>404</p><br><a href="/">BACK TO HOME</a><br><p><span>Powered BY </span><a target="_blank" href="https://github.com/elecV2/elecV2P">elecV2P</a></p><p><span>TG Channel </span><a target="_blank" href="https://t.me/elecV2">@elecV2</a></p>`)
})
newServer(app).then(server=>{
server.on('clientError', (err, socket) => {
clog.error('elecV2P clientError', err)
socket.end('HTTP/1.1 400 Bad Request\r\n')
})
server.listen(CONFIG_Port.webst, ()=>{
clog.notify('elecV2P', 'v' + CONFIG.version, 'started on port', CONFIG_Port.webst);
})
websocketSer({ server, path: '/elecV2P' })
}).catch(err=>{
clog.error('elecV2P new server error:', err)
})
}