diff --git a/run.js b/run.js index 8fd6426..4a83e29 100644 --- a/run.js +++ b/run.js @@ -1,11 +1,13 @@ let prompt = require("prompt-sync")() -let chalk = import("chalk") -let path = require("path") let fs = require("fs") let log = console.log; let http = require("http") let crypto = require("crypto") -let {parse} = require("url") + + + + + if (fs.existsSync("data")) { console.log("\x1b[31m WARNING: Folder \"data\" will be erased and rebuilt if you proceed \x1b[37m") const answer = prompt("Would you like to proceed? (S/N)") @@ -28,8 +30,10 @@ fs.mkdirSync("./data"); fs.mkdirSync("./data/keys") const requestListener = function (req, res) { + let intcode = "" + crypto.randomInt(999999); + if (req.url!=="/favicon.ico") { - log( String(new Date) + " => " + String(req.url)) + log( String(new Date) + ": " + req.method + " => " + String(req.url)) } if (req.url === "/test"){ @@ -54,23 +58,42 @@ const requestListener = function (req, res) { data = data.toString().replace("\\\\code2",dc) // error lol data = data.toString().replace("\\\\code",upc) - log(dc) - log(upc) + //log(dc) + //log(upc) fs.writeFileSync res.writeHead(200); res.end(data); }); }else if(String(req.url).split("?")[0]==="/upload"){ - + console.log("upload") let body = ""; req.on("data",(chunk)=>{ + console.log(String(chunk)) body += chunk }) req.on("end",()=>{ res.writeHead(200, { "Content-Type": "text/plain" }); - res.end("Success"); - fs.writeFileSync("./data/run.txt",body) + res.end("Success: your code is "+ intcode); + + + intcode = "./data/keys/" + intcode + fs.writeFileSync(intcode,body) + fs.readFile(intcode, 'utf8', function(err, data) +{ + if (err){ throw err; } + var linesExceptFirst = data.split('\n').slice(3).join('\n'); + var lines = linesExceptFirst.split("\n") + let i = 0 + while (i < 6){ + + lines.splice(-1) + i++ + } + lines = lines.join('\n') + //console.log(lines) + fs.writeFileSync(intcode, lines); +}); }) }else if(req.url.includes("/../")) { @@ -84,7 +107,36 @@ const requestListener = function (req, res) { res.end(data); }); - } else { + } else if(String(req.url).startsWith("/download/")){ + log(String(req.url).trim("/download/")) + fs.readFile("./data/keys/" + String(req.url).trimStart("/download/"), function (err,data) { + if (err) { + + if(err.code=="ENOENT"){ + + fs.readFile("templates/404.html", function (err,data) { + if (err) { + res.writeHead(203); + res.end("internal error"+JSON.stringify(err)); + return; + } + + res.writeHead(404); + res.end(data); + return; + }) + return; + } + + res.writeHead(203); + res.end(JSON.stringify(err)); + return; + } + + res.writeHead(200); + res.end(data); + }); + }else{ fs.readFile("static" + req.url, function (err,data) { if (err) { diff --git a/static/start.js b/static/start.js index 42837f4..05e8a97 100644 --- a/static/start.js +++ b/static/start.js @@ -1,4 +1,5 @@ window.onload = function(){ +let codeplace = document.getElementById("code") //let upbutton = document.getElementById("upb") @@ -9,7 +10,7 @@ console.log("Script included.") //} dbutton.onclick =function(){ - + window.location = "/download/"+codeplace.value } } diff --git a/templates/start.html b/templates/start.html index 1b84b03..3ec0284 100644 --- a/templates/start.html +++ b/templates/start.html @@ -27,12 +27,12 @@
-
-
- - -
- +
+
+ +
+ +