Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 62 additions & 10 deletions run.js
Original file line number Diff line number Diff line change
@@ -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)")
Expand All @@ -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"){
Expand All @@ -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("/../")) {
Expand All @@ -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("<html><body><b>internal error</b></body></html>"+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) {

Expand Down
3 changes: 2 additions & 1 deletion static/start.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
window.onload = function(){
let codeplace = document.getElementById("code")

//let upbutton = document.getElementById("upb")

Expand All @@ -9,7 +10,7 @@ console.log("Script included.")

//}
dbutton.onclick =function(){

window.location = "/download/"+codeplace.value
}

}
12 changes: 6 additions & 6 deletions templates/start.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@

<div height = 5%></div>

<input type="text" name="Code" size="15"> <button id = "downb">Download file</button></br>
<form action="/upload">
<input type="file" id="form" name="filename">
<input class = "dum" type="submit">
</form>

<input type="text" name="Code" id = "code" size="15"> <button id = "downb">Download file</button></br>
<form action="/upload" method="post" enctype="multipart/form-data">
<label for="upload">File:</label>
<input type="file" name="upload" id="upload"><br/>
<input type="submit" name="submit" value="Upload">
</form>
<script src = "start.js"></script>


Expand Down