forked from elecV2/elecV2P
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webmodule.js
82 lines (67 loc) · 2.59 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
const http = require('http')
const path = require('path')
const express = require('express')
const compression = require('compression')
const { CONFIG, CONFIG_Port } = require('./config')
const { logger, websocketSer } = require('./utils')
const clog = new logger({ head: 'webServer', level: 'debug' })
const { wbefss, wbconfig, wbfeed, wbcrt, wbjs, wbtask, wblogs, wbstore, wbdata, wblist, wbhook } = require('./webser')
module.exports = () => {
const app = express()
app.use(compression())
app.use(express.json({ limit: '20mb' }))
app.use((req, res, next)=>{
if (!CONFIG.SECURITY || CONFIG.SECURITY.enable === false) {
next()
return
}
let ipAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress
if (CONFIG.wbrtoken) {
let token = req.query.token || req.body.token
if (!token) {
let ref = req.get('Referer')
if (ref) {
ref = new URL(ref)
token = new URLSearchParams(ref.search).get('token')
}
}
if (token === CONFIG.wbrtoken) {
clog.debug(ipAddress, 'is access elecV2P server by webhook token')
next()
return
}
}
const blacklist = CONFIG.SECURITY.blacklist || []
const whitelist = CONFIG.SECURITY.whitelist || []
if (ipAddress.substr(0, 7) == "::ffff:") ipAddress = ipAddress.substr(7)
if (whitelist.indexOf(ipAddress) !== -1 || (blacklist.indexOf('*') === -1 && blacklist.indexOf(ipAddress) === -1)) {
next()
} else {
clog.error(ipAddress, 'is try to access elecV2P sever.')
res.send('you don\'t have permission to access. <br><br>Powered BY elecV2P: https://github.com/elecV2/elecV2P')
}
})
const ONEMONTH = 60 * 1000 * 60 * 24 * 30 // 页面缓存时间
app.use(express.static(path.resolve(__dirname, 'web/dist'), { maxAge: ONEMONTH }))
wbefss(app)
wbconfig(app)
wbfeed(app)
wbcrt(app)
wbjs(app)
wbtask(app)
wblogs(app)
wbstore(app)
wbdata(app)
wblist(app)
wbhook(app)
app.use((req, res, next) => {
res.writeHead(200, { 'Content-Type': 'text/html;charset=utf-8' })
res.end(`404<br>当前访问地址不存在<br><br><a href="/">返回首页</a><br><br><br><a target="_blank" href="https://github.com/elecV2/elecV2P">elecV2P 项目主页</a>`)
})
const server = http.createServer(app)
const webstPort = process.env.PORT || CONFIG_Port.webst || 80
server.listen(webstPort, ()=>{
clog.notify("elecV2P", CONFIG.version, "on port", webstPort)
})
websocketSer({ server, path: '/elecV2P' })
}