2023-01-20 04:50:15 +00:00
|
|
|
const express = require('express')
|
2023-01-22 21:00:31 +00:00
|
|
|
const cookie = require('cookie-parser')
|
2023-01-20 04:50:15 +00:00
|
|
|
const app = express()
|
|
|
|
const port = 8080
|
|
|
|
|
2023-01-22 19:41:39 +00:00
|
|
|
app.set('trust proxy', true)
|
2023-01-22 21:00:31 +00:00
|
|
|
app.use(cookie())
|
2023-01-20 04:50:15 +00:00
|
|
|
app.use(express.json());
|
|
|
|
app.use(express.static('public'))
|
|
|
|
|
2023-01-22 21:00:31 +00:00
|
|
|
const database = require('./src/database.js')
|
|
|
|
const con = require('./src/console.js')
|
|
|
|
const api = require('./src/api.js')
|
|
|
|
|
2023-01-22 19:41:39 +00:00
|
|
|
app.use((req, res, next) => {
|
2023-01-22 21:34:07 +00:00
|
|
|
var ip = req.headers['x-real-ip'] || req.connection.remoteAddress;
|
|
|
|
if (req.path !== '/console') {
|
|
|
|
const public = { ... req.body }
|
|
|
|
if (public.password !== undefined) {
|
|
|
|
public.password = '********'
|
|
|
|
}
|
|
|
|
console.log(ip, req.method, req.path, public)
|
|
|
|
con.requests.push({ip: ip, method: req.method, path: req.path, body: public})
|
2023-01-22 19:41:39 +00:00
|
|
|
}
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
|
|
|
|
app.get('/', (req, res) => {
|
|
|
|
const cookies = req.cookies;
|
|
|
|
if (cookies === undefined || cookies.auth === undefined) {
|
|
|
|
res.redirect('/login')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const user = database.auth(req.cookies.auth)
|
|
|
|
if (user === undefined) {
|
|
|
|
res.redirect('/login')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res.redirect('/home')
|
|
|
|
})
|
|
|
|
|
2023-01-21 14:08:22 +00:00
|
|
|
app.get('/login', (req, res) => {
|
2023-01-22 19:41:39 +00:00
|
|
|
const cookies = req.cookies;
|
|
|
|
if (cookies === undefined || cookies.auth === undefined) {
|
|
|
|
res.sendFile('login.html', { root: './public' })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const user = database.auth(req.cookies.auth)
|
|
|
|
if (user === undefined) {
|
|
|
|
res.sendFile('login.html', { root: './public' })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res.redirect('/home')
|
2023-01-20 04:50:15 +00:00
|
|
|
})
|
|
|
|
|
2023-01-20 17:26:51 +00:00
|
|
|
app.get('/home', (req, res) => {
|
2023-01-22 19:41:39 +00:00
|
|
|
const cookies = req.cookies;
|
|
|
|
if (cookies === undefined || cookies.auth === undefined) {
|
|
|
|
res.redirect('/login')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const user = database.auth(req.cookies.auth)
|
|
|
|
if (user === undefined) {
|
|
|
|
res.redirect('/login')
|
|
|
|
return
|
|
|
|
}
|
2023-01-20 17:26:51 +00:00
|
|
|
res.sendFile('home.html', { root: './public' })
|
|
|
|
})
|
|
|
|
|
|
|
|
app.get('/people', (req, res) => {
|
|
|
|
res.sendFile('people.html', { root: './public' })
|
|
|
|
})
|
|
|
|
|
2023-01-21 14:08:22 +00:00
|
|
|
app.get('/profile', (req, res) => {
|
|
|
|
res.sendFile('profile.html', { root: './public' })
|
|
|
|
})
|
|
|
|
|
|
|
|
app.use('/api', api);
|
2023-01-22 21:00:31 +00:00
|
|
|
app.use('/console', con.router);
|
2023-01-22 19:41:39 +00:00
|
|
|
|
2023-01-20 17:26:51 +00:00
|
|
|
app.use((req, res, next) => {
|
|
|
|
res.status(404).sendFile('404.html', { root: './public' })
|
|
|
|
})
|
|
|
|
|
2023-01-22 19:41:39 +00:00
|
|
|
app.use((err, req, res, next) => {
|
|
|
|
if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
|
|
|
|
res.status(400).send({msg: 'Invalid json body'})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
console.error(err)
|
|
|
|
res.status(500).send({msg: 'Internal server error'})
|
|
|
|
})
|
|
|
|
|
2023-01-20 04:50:15 +00:00
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`App listening on port http://127.0.0.1:${port}`)
|
|
|
|
})
|