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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| const express = require('express'); const bodyParser = require('body-parser'); const _ = require('lodash'); const app = express(); var fs = require('fs');
app.set('view engine', 'pug'); app.set('views', 'views');
app.use(bodyParser.urlencoded({extended: true})); app.use(express.static('static'));
const users = [ {name: 'test', password: 'test'}, {name: 'admin', password: Math.random().toString(32), admin: true}, ];
let messages = []; let lastId = 1;
function findUser(auth) { return users.find((u) => u.name === auth.name && u.password === auth.password); }
app.use(bodyParser.json());
app.get('/users', (req, res, next) => { const lists = users; res.render('users', {lists: lists, pageTitle: 'List of Users', path: '/users'}); });
app.get('/', (req, res, next) => { res.render('home', {pageTitle: 'Home', path: '/'}); });
app.post('/', (req, res, next) => { users.push({name: req.body.name, password: req.body.password}); res.redirect('/users'); });
app.get('/message', (req, res) => { res.send(messages); });
app.put('/message', (req, res) => { const user = findUser(req.body.auth || {}); console.log(req.body.auth); console.log(user); if (!user) { res.status(403).send({ok: false, error: 'Access denied'}); return; }
const message = { avator: '= =', };
_.merge(message, req.body.message, { id: lastId++, userName: user.name, });
messages.push(message); res.send({ok: true, message: message}); });
app.delete('/', (req, res) => { res.send({ok: true}); });
app.post('/upload', (req, res) => { res.send({ok: true}); });
app.post('/api', (req, res) => { const user = findUser(req.body.auth || {}); if (!user) { res.status(403).send({ok: false, error: 'Access denied'}); return; }
filename = req.body.filename; testFolder = "/app/"; fs.readdirSync(testFolder).forEach(file => { if (file.indexOf(filename) > -1) { var buffer = fs.readFileSync(filename).toString(); res.send({ok: true, content: buffer}); } }); });
app.post('/debug', (req, res) => { const user = findUser(req.body.auth || {}); if (!user || !user.admin) { res.status(403).send({ok: false, error: 'Access denied'}); return; } var buffer = fs.readFileSync('/flag').toString(); res.send({ok: true, content: buffer}); });
app.listen(80, () => { console.log('Listening port 80'); });
|