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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| var express = require('express'); var app = express(); var fs = require('fs'); var path = require('path'); var http = require('http'); var pug = require('pug'); var morgan = require('morgan'); const multer = require('multer');
app.use(multer({ dest: './dist' }).array('file')); app.use(morgan('short')); app.use("/uploads", express.static(path.join(__dirname, '/uploads'))); app.use("/template", express.static(path.join(__dirname, '/template')));
app.get('/', function(req, res) { var action = req.query.action ? req.query.action : "index"; if (action.includes("/") || action.includes("\\")) { res.send("Errrrr, You have been Blocked"); return; } var file = path.join(__dirname, '/template/' + action + '.pug'); var html = pug.renderFile(file); res.send(html); });
app.post('/file_upload', function(req, res) { var ip = req.connection.remoteAddress; var obj = { msg: '' };
if (!ip.includes('127.0.0.1')) { obj.msg = "only admin's ip can use it"; res.send(JSON.stringify(obj)); return; }
fs.readFile(req.files[0].path, function(err, data) { if (err) { obj.msg = 'upload failed'; res.send(JSON.stringify(obj)); } else { var file_path = '/uploads/' + req.files[0].mimetype + "/"; var file_name = req.files[0].originalname; var dir_file = __dirname + file_path + file_name;
if (!fs.existsSync(__dirname + file_path)) { try { fs.mkdirSync(__dirname + file_path); } catch (error) { obj.msg = "file type error"; res.send(JSON.stringify(obj)); return; } }
try { fs.writeFileSync(dir_file, data); obj = { msg: 'upload success', filename: file_path + file_name }; } catch (error) { obj.msg = 'upload failed'; }
res.send(JSON.stringify(obj)); } }); });
app.get('/source', function(req, res) { res.sendFile(path.join(__dirname, '/template/source.txt')); });
app.get('/core', function(req, res) { var q = req.query.q;
if (q) { var url = 'http://localhost:8081/source?' + q; console.log(url);
var trigger = blacklist(url); if (trigger === true) { res.send("error occurs!"); } else { try { http.get(url, function(resp) { resp.setEncoding('utf8');
resp.on('error', function(err) { if (err.code === "ECONNRESET") { console.log("Timeout occurs"); return; } });
resp.on('data', function(chunk) { try { var resps = chunk.toString(); res.send(resps); } catch (e) { res.send(e.message); } }).on('error', (e) => { res.send(e.message); }); }); } catch (error) { console.log(error); } } } else { res.send("search param 'q' missing!"); } });
function blacklist(url) { var evilwords = ["global", "process", "mainModule", "require", "root", "child_process", "exec", "\"", "'", "!"]; var arrayLen = evilwords.length;
for (var i = 0; i < arrayLen; i++) { const trigger = url.includes(evilwords[i]); if (trigger === true) { return true; } } return false; }
var server = app.listen(8081, function() { var host = server.address().address; var port = server.address().port; console.log("Example app listening at http://%s:%s", host, port); });
|