11let prompt = require ( "prompt-sync" ) ( )
2- let chalk = import ( "chalk" )
3- let path = require ( "path" )
42let fs = require ( "fs" )
53let log = console . log ;
64let http = require ( "http" )
75let crypto = require ( "crypto" )
8- let { parse} = require ( "url" )
6+
7+
8+
9+
10+
911if ( fs . existsSync ( "data" ) ) {
1012 console . log ( "\x1b[31m WARNING: Folder \"data\" will be erased and rebuilt if you proceed \x1b[37m" )
1113 const answer = prompt ( "Would you like to proceed? (S/N)" )
@@ -28,8 +30,10 @@ fs.mkdirSync("./data");
2830fs . mkdirSync ( "./data/keys" )
2931
3032const requestListener = function ( req , res ) {
33+ let intcode = "" + crypto . randomInt ( 999999 ) ;
34+
3135 if ( req . url !== "/favicon.ico" ) {
32- log ( String ( new Date ) + " => " + String ( req . url ) )
36+ log ( String ( new Date ) + ": " + req . method + " => " + String ( req . url ) )
3337
3438 }
3539 if ( req . url === "/test" ) {
@@ -54,23 +58,42 @@ const requestListener = function (req, res) {
5458
5559 data = data . toString ( ) . replace ( "\\\\code2" , dc ) // error lol
5660 data = data . toString ( ) . replace ( "\\\\code" , upc )
57- log ( dc )
58- log ( upc )
61+ // log(dc)
62+ // log(upc)
5963 fs . writeFileSync
6064 res . writeHead ( 200 ) ;
6165 res . end ( data ) ;
6266 } ) ;
6367 } else if ( String ( req . url ) . split ( "?" ) [ 0 ] === "/upload" ) {
64-
68+ console . log ( "upload" )
6569 let body = "" ;
6670 req . on ( "data" , ( chunk ) => {
71+ console . log ( String ( chunk ) )
6772 body += chunk
6873
6974 } )
7075 req . on ( "end" , ( ) => {
7176 res . writeHead ( 200 , { "Content-Type" : "text/plain" } ) ;
72- res . end ( "Success" ) ;
73- fs . writeFileSync ( "./data/run.txt" , body )
77+ res . end ( "Success: your code is " + intcode ) ;
78+
79+
80+ intcode = "./data/keys/" + intcode
81+ fs . writeFileSync ( intcode , body )
82+ fs . readFile ( intcode , 'utf8' , function ( err , data )
83+ {
84+ if ( err ) { throw err ; }
85+ var linesExceptFirst = data . split ( '\n' ) . slice ( 3 ) . join ( '\n' ) ;
86+ var lines = linesExceptFirst . split ( "\n" )
87+ let i = 0
88+ while ( i < 6 ) {
89+
90+ lines . splice ( - 1 )
91+ i ++
92+ }
93+ lines = lines . join ( '\n' )
94+ //console.log(lines)
95+ fs . writeFileSync ( intcode , lines ) ;
96+ } ) ;
7497 } )
7598
7699 } else if ( req . url . includes ( "/../" ) ) {
@@ -84,7 +107,36 @@ const requestListener = function (req, res) {
84107 res . end ( data ) ;
85108 } ) ;
86109
87- } else {
110+ } else if ( String ( req . url ) . startsWith ( "/download/" ) ) {
111+ log ( String ( req . url ) . trim ( "/download/" ) )
112+ fs . readFile ( "./data/keys/" + String ( req . url ) . trimStart ( "/download/" ) , function ( err , data ) {
113+ if ( err ) {
114+
115+ if ( err . code == "ENOENT" ) {
116+
117+ fs . readFile ( "templates/404.html" , function ( err , data ) {
118+ if ( err ) {
119+ res . writeHead ( 203 ) ;
120+ res . end ( "<html><body><b>internal error</b></body></html>" + JSON . stringify ( err ) ) ;
121+ return ;
122+ }
123+
124+ res . writeHead ( 404 ) ;
125+ res . end ( data ) ;
126+ return ;
127+ } )
128+ return ;
129+ }
130+
131+ res . writeHead ( 203 ) ;
132+ res . end ( JSON . stringify ( err ) ) ;
133+ return ;
134+ }
135+
136+ res . writeHead ( 200 ) ;
137+ res . end ( data ) ;
138+ } ) ;
139+ } else {
88140 fs . readFile ( "static" + req . url , function ( err , data ) {
89141 if ( err ) {
90142
0 commit comments