From cf7e1653cba2db458dd4b8cffa8b59572785cc70 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Sun, 22 Jan 2023 16:34:07 -0500 Subject: [PATCH] switch to auto reload console --- index.js | 13 ++++++---- public/js/api.js | 2 +- src/console.js | 67 ++++++++++++++++++++++++++---------------------- 3 files changed, 46 insertions(+), 36 deletions(-) diff --git a/index.js b/index.js index 313dd5d..ab05c60 100644 --- a/index.js +++ b/index.js @@ -13,12 +13,15 @@ const con = require('./src/console.js') const api = require('./src/api.js') app.use((req, res, next) => { - const public = { ... req.body } - if (public.password !== undefined) { - public.password = '********' + 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}) } - console.log(req.ip, req.method, req.path, public) - con.update(req.ip, req.method, req.path, public) next() }) diff --git a/public/js/api.js b/public/js/api.js index 371ecf3..c0ae980 100644 --- a/public/js/api.js +++ b/public/js/api.js @@ -1,4 +1,4 @@ -const endpoint = 'https://xssbook.com/api' +const endpoint = 'http://localhost:8080/api' const request = async (url, body, method) => { if (method === undefined) method = 'POST' diff --git a/src/console.js b/src/console.js index c7f4089..cbc8f51 100644 --- a/src/console.js +++ b/src/console.js @@ -1,30 +1,16 @@ const express = require('express') const router = express.Router() -const sleep = ms => new Promise(r => setTimeout(r, ms)); -const connections = [] +var requests = [] router.get('/', async (req, res) => { - res.write(` - - - - - - XSSBook - Console - - - `) - res.write(new Array(2048).join(" ")) - await sleep(500) - connections.push(res) - while (true) { - res.write(" ") - await sleep(100) + res.send(render()) + if(requests.length > 100) { + requests.splice(0, 50) } }) -function color(method) { +function parseMethod(method) { switch(method) { case 'GET': return '4ae04a' @@ -43,7 +29,7 @@ function color(method) { } } -function highlight(json) { +function parseJson(json) { if (typeof json != 'string') { json = JSON.stringify(json, undefined, 2); } @@ -65,17 +51,38 @@ function highlight(json) { }); } -async function update(ip, method, path, json) { - connections.forEach(con => { - con.write(` +function parseRequest(req) { + const html = `
- ${ip} - ${method} - ${path} - ${highlight(json)} + ${req.ip} + ${req.method} + ${req.path} + ${parseJson(req.body)}
- `) - }) + ` + return html } -module.exports = { router, update }; \ No newline at end of file +function render() { + const html = ` + + + + + + XSSBook - Console + + + + ${requests.map(r => parseRequest(r)).join('')} + + + ` + return html +} + +module.exports = { router, requests }; \ No newline at end of file