Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
628 lines (539 loc) · 19.1 KB
/
Copy pathserver.js
File metadata and controls
628 lines (539 loc) · 19.1 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
const express = require("express");
const bodyParser = require("body-parser");
const bcrypt = require("bcrypt");
const cors = require("cors");
const session = require("express-session");
const app = express();
const fs = require("fs");
const port = 3002;
const { exec } = require("child_process");
const path = require("path");
// Middleware to parse JSON bodies
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Serve static files (not necessary if using a frontend build system like webpack)
app.use(express.static("public"));
// CORS Middleware (if needed)
app.use(cors());
// Endpoint to handle user registration (signup)
// app.post("/signup", (req, res) => {
// const { email, username, password } = req.body;
// if (!email || !username || !password) {
// return res
// .status(400)
// .json({ error: "Email, username, and password are required" });
// }
// // Check if the user already exists
// const checkUserQuery = "SELECT * FROM users WHERE email = ?";
// db.query(checkUserQuery, [email], (err, results) => {
// if (err) {
// console.error("Error checking user:", err);
// return res.status(500).json({ error: "Database error" });
// }
// if (results.length > 0) {
// return res.status(409).json({ error: "User already exists" });
// }
// // Hash the password before storing it in the database
// bcrypt.hash(password, 10, (err, hashedPassword) => {
// if (err) {
// console.error("Error hashing password:", err);
// return res.status(500).json({ error: "Failed to hash password" });
// }
// // Insert new user into database with hashed password
// const insertUserQuery =
// "INSERT INTO users (email, username, password) VALUES (?, ?, ?)";
// db.query(
// insertUserQuery,
// [email, username, hashedPassword],
// (err, results) => {
// if (err) {
// console.error("Error creating user:", err);
// return res.status(500).json({ error: "Failed to create user" });
// }
// res.json({ message: "User registered successfully" });
// }
// );
// });
// });
// });
// // Endpoint to handle user authentication (signin)
// app.post("/signin", (req, res) => {
// const { email, password } = req.body;
// if (!email || !password) {
// return res.status(400).json({ error: "Email and password are required" });
// }
// const findUserQuery = "SELECT * FROM users WHERE email = ?";
// db.query(findUserQuery, [email], (err, results) => {
// if (err) {
// console.error("Error finding user:", err);
// return res.status(500).json({ error: "Database error" });
// }
// if (results.length === 0) {
// console.log("No user found with the provided email");
// return res.status(401).json({ error: "Invalid email or password" });
// }
// bcrypt.compare(password, results[0].password, (err, passwordMatch) => {
// if (err) {
// console.error("Error comparing passwords:", err);
// return res.status(500).json({ error: "Authentication error" });
// }
// if (!passwordMatch) {
// console.log("Password does not match for the provided email");
// return res.status(401).json({ error: "Invalid email or password" });
// }
// // Set session to indicate the user is logged in
// req.session.loggedIn = true;
// req.session.userId = results[0].id;
// console.log("User authenticated successfully");
// res.json({ message: "Login successful" });
// });
// });
// });
// // Endpoint to check if the user is logged in
// app.get("/api/check-login", (req, res) => {
// if (req.session.loggedIn) {
// res.json({ loggedIn: true });
// } else {
// res.json({ loggedIn: false });
// }
// });
// // Endpoint to handle user logout
// app.post("/logout", (req, res) => {
// req.session.destroy((err) => {
// if (err) {
// return res.status(500).json({ error: "Failed to log out" });
// }
// res.json({ message: "Logout successful" });
// });
// });
// Endpoint to handle POST request for executing Python code
app.post("/execute_python", (req, res) => {
const { code, inputs } = req.body;
const tempFile = "temp.py";
fs.writeFileSync(tempFile, code);
let command = `python3 ${tempFile}`;
const child = exec(command, (error, stdout, stderr) => {
fs.unlinkSync(tempFile); // Clean up the temp file
if (error) {
res.json({ output: stderr });
return;
}
res.json({ output: stdout });
});
if (inputs && inputs.length > 0) {
inputs.forEach((input) => {
child.stdin.write(input + "\n");
});
child.stdin.end();
}
});
// Endpoint to handle POST request for executing C++ code
app.post("/execute_cpp", (req, res) => {
const { code, inputs } = req.body;
const fileName = "temp.cpp";
const codeFilePath = path.join(__dirname, "temp", fileName);
const executablePath = path.join(__dirname, "temp", "temp.exe");
const compileCommand = `g++ -o ${executablePath} ${codeFilePath}`;
fs.writeFile(codeFilePath, code, (err) => {
if (err) {
console.error("Error writing code file:", err);
return res.status(500).json({ error: "Failed to write code file" });
}
exec(compileCommand, (compileError, compileStdout, compileStderr) => {
if (compileError) {
console.error("Compilation Error:", compileError);
console.error("Compilation stderr:", compileStderr);
return res
.status(500)
.json({ error: "Compilation failed", stderr: compileStderr });
}
const child = exec(
executablePath,
{ timeout: 10000 },
(execError, stdout, stderr) => {
if (execError) {
console.error("Execution Error:", execError);
console.error("Execution stderr:", stderr);
return res
.status(500)
.json({ error: "Execution failed", stderr: stderr });
}
console.log("stdout:", stdout);
console.error("stderr:", stderr);
res.json({ output: stdout, error: stderr });
}
);
if (inputs && inputs.length > 0) {
inputs.forEach((input) => {
child.stdin.write(input + "\n");
});
child.stdin.end();
}
});
});
});
// Endpoint to handle POST request for executing Java code
app.post("/execute_java", (req, res) => {
const { code, inputs } = req.body;
// Extract class name from Java code
const className = extractClassName(code);
if (!className) {
return res.status(400).json({ error: "Class name not found in Java code" });
}
// Create temporary Java file
const javaFilePath = path.join(__dirname, "temp", `${className}.java`);
const javaCompileCommand = `javac ${javaFilePath}`;
fs.writeFile(javaFilePath, code, (err) => {
if (err) {
console.error("Error writing Java file:", err);
return res.status(500).json({ error: "Failed to write Java file" });
}
exec(javaCompileCommand, (compileError, compileStdout, compileStderr) => {
if (compileError) {
console.error("Compilation Error:", compileError);
console.error("Compilation stderr:", compileStderr);
return res
.status(500)
.json({ error: "Compilation failed", stderr: compileStderr });
}
const javaExecCommand = `java -cp ${path.join(
__dirname,
"temp"
)} ${className}`;
const child = exec(
javaExecCommand,
{ timeout: 10000 },
(execError, stdout, stderr) => {
if (execError) {
console.error("Execution Error:", execError);
console.error("Execution stderr:", stderr);
return res
.status(500)
.json({ error: "Execution failed", stderr: stderr });
}
console.log("stdout:", stdout);
console.error("stderr:", stderr);
res.json({ output: stdout, error: stderr });
}
);
if (inputs && inputs.length > 0) {
inputs.forEach((input) => {
child.stdin.write(input + "\n");
});
child.stdin.end();
}
});
});
});
// Endpoint to handle POST request for executing C code
app.post("/execute_c", (req, res) => {
const { code, inputs } = req.body;
// Create temporary C file
const cFilePath = path.join(__dirname, "temp", "temp.c");
const cCompileCommand = `gcc -o ${path.join(
__dirname,
"temp",
"temp"
)} ${cFilePath}`;
fs.writeFile(cFilePath, code, (err) => {
if (err) {
console.error("Error writing C file:", err);
return res.status(500).json({ error: "Failed to write C file" });
}
exec(cCompileCommand, (compileError, compileStdout, compileStderr) => {
if (compileError) {
console.error("Compilation Error:", compileError);
console.error("Compilation stderr:", compileStderr);
return res
.status(500)
.json({ error: "Compilation failed", stderr: compileStderr });
}
const child = exec(
path.join(__dirname, "temp", "temp"),
{ timeout: 10000 },
(execError, stdout, stderr) => {
if (execError) {
console.error("Execution Error:", execError);
console.error("Execution stderr:", stderr);
return res
.status(500)
.json({ error: "Execution failed", stderr: stderr });
}
console.log("stdout:", stdout);
console.error("stderr:", stderr);
res.json({ output: stdout, error: stderr });
}
);
if (inputs && inputs.length > 0) {
inputs.forEach((input) => {
child.stdin.write(input + "\n");
});
child.stdin.end();
}
});
});
});
// Helper function to extract class name from Java code
function extractClassName(javaCode) {
// Regex to find class declaration
const classRegex = /class\s+([a-zA-Z_$][a-zA-Z\d_$]*)\s*\{/;
const match = javaCode.match(classRegex);
if (match && match[1]) {
return match[1];
}
return null;
}
app.post("/execute_csharp", (req, res) => {
const { code, inputs } = req.body;
if (!code) {
return res.status(400).json({ error: "No C# code provided" });
}
const fileName = "temp.cs";
const filePath = path.join(__dirname, "temp", fileName);
const executablePath = path.join(__dirname, "temp", "temp");
// Write C# code to file
fs.writeFile(filePath, code, (err) => {
if (err) {
console.error("Error writing C# file:", err);
return res.status(500).json({ error: "Failed to write C# file" });
}
// Compile C# code
const compileCommand = `dotnet build ${filePath}`;
exec(compileCommand, (compileError, compileStdout, compileStderr) => {
if (compileError) {
console.error("Compilation Error:", compileError);
console.error("Compilation stderr:", compileStderr);
return res
.status(500)
.json({ error: "Compilation failed", stderr: compileStderr });
}
// Execute compiled C# program
exec(
`${executablePath}`,
{ timeout: 10000, cwd: path.dirname(executablePath) },
(execError, stdout, stderr) => {
if (execError) {
console.error("Execution Error:", execError);
console.error("Execution stderr:", stderr);
return res
.status(500)
.json({ error: "Execution failed", stderr: stderr });
}
console.log("stdout:", stdout);
console.error("stderr:", stderr);
res.json({ output: stdout, error: stderr });
}
);
// Provide inputs to the program if any
if (inputs && inputs.length > 0) {
const inputString = inputs.join("\n");
const inputFilePath = path.join(__dirname, "temp", "inputs.txt");
fs.writeFile(inputFilePath, inputString, (inputErr) => {
if (inputErr) {
console.error("Error writing inputs file:", inputErr);
} else {
const child = exec(
`${executablePath} < ${inputFilePath}`,
{ timeout: 10000, cwd: path.dirname(executablePath) },
(execError, stdout, stderr) => {
if (execError) {
console.error("Execution Error:", execError);
console.error("Execution stderr:", stderr);
return res
.status(500)
.json({ error: "Execution failed", stderr: stderr });
}
console.log("stdout:", stdout);
console.error("stderr:", stderr);
res.json({ output: stdout, error: stderr });
// Clean up temporary inputs file
fs.unlink(inputFilePath, (unlinkErr) => {
if (unlinkErr) {
console.error("Error deleting inputs file:", unlinkErr);
}
});
}
);
}
});
}
});
});
});
// Route for handling PHP execution
app.post("/execute-php", (req, res) => {
const phpCode = req.body.phpCode;
// Validate phpCode
if (!phpCode) {
return res.status(400).send("PHP code is required");
}
// Write PHP code to a temporary file
const tempFilePath = path.join(__dirname, "temp.php");
fs.writeFileSync(tempFilePath, phpCode);
// Use PHP CLI to execute the PHP file
exec(`php ${tempFilePath}`, (error, stdout, stderr) => {
// Clean up the temporary file
fs.unlinkSync(tempFilePath);
if (error) {
console.error(`exec error: ${error}`);
return res.status(400).send(error.toString());
}
if (stderr) {
console.error(`stderr: ${stderr}`);
return res.status(400).send(stderr.toString());
}
console.log(`stdout: ${stdout}`);
res.send(stdout);
});
});
app.post("/execute_ruby", (req, res) => {
const { code, inputs } = req.body;
if (!code) {
return res.status(400).json({ error: "No code provided" });
}
const filePath = "temp.rb";
const inputFilePath = "inputs.txt";
// Write Ruby code to a temporary file
fs.writeFile(filePath, code, (err) => {
if (err) {
console.error("Error writing code file:", err);
return res.status(500).json({ error: "Error writing code file" });
}
// Write inputs to a temporary file
fs.writeFile(inputFilePath, inputs.join("\n"), (err) => {
if (err) {
console.error("Error writing inputs file:", err);
return res.status(500).json({ error: "Error writing inputs file" });
}
// Execute the Ruby code using the inputs
exec(`ruby ${filePath} < ${inputFilePath}`, (err, stdout, stderr) => {
if (err) {
console.error("Error executing Ruby code:", err);
console.error("Stderr:", stderr);
return res.status(500).json({ error: "Error executing Ruby code" });
}
// Send the output back to the client
res.json({ output: stdout || stderr });
// Clean up temporary files
fs.unlink(filePath, (err) => {
if (err) console.error("Error deleting code file:", err);
});
fs.unlink(inputFilePath, (err) => {
if (err) console.error("Error deleting inputs file:", err);
});
});
});
});
});
// Endpoint to handle POST request for executing Go code
app.post("/execute_go", (req, res) => {
const { code, inputs } = req.body;
const fileName = "temp.go";
const filePath = path.join(__dirname, "temp", fileName);
// Write Go code to file
fs.writeFile(filePath, code, (err) => {
if (err) {
console.error("Error writing Go file:", err);
return res.status(500).json({ error: "Failed to write Go file" });
}
// Execute Go program
const command = `go run ${filePath}`;
const child = exec(command, (error, stdout, stderr) => {
if (error) {
console.error("Execution error:", error);
return res.status(500).json({ error: "Execution error", stderr });
}
// Output result
res.status(200).json({ output: stdout, error: stderr });
});
// Provide inputs to the program if any
if (inputs && inputs.length > 0) {
child.stdin.setEncoding("utf-8");
child.stdin.write(inputs.join("\n"));
child.stdin.end();
}
});
});
// POST endpoint to execute Rust code
app.post("/execute_rust", (req, res) => {
const { code, inputs } = req.body;
// Create a unique temporary directory for each execution
const tempDir = path.join(__dirname, "temp_" + Date.now());
fs.mkdirSync(tempDir);
// Write the code to a temporary Rust file within the directory
const tmpFileName = path.join(tempDir, "temp.rs");
fs.writeFile(tmpFileName, code, (err) => {
if (err) {
console.error("Error writing Rust code to file:", err);
return res.status(500).json({ error: "Error writing Rust code to file" });
}
// Construct the command to compile and execute Rust code
const executablePath = path.join(tempDir, "temp");
const command = `rustc ${tmpFileName} -o ${executablePath} && echo "${inputs.join(
"\n"
)}" | ${executablePath}`;
// Execute the command
exec(command, (error, stdout, stderr) => {
// Clean up temporary directory and files
fs.rmSync(tempDir, { recursive: true, force: true });
if (error) {
console.error("Error executing Rust code:", error);
return res.status(500).json({ error: "Error executing Rust code" });
}
console.log("Rust code output:", stdout);
// Send the output back to the client
res.json({ output: stdout });
});
});
});
app.post("/execute_swift", (req, res) => {
const { code, inputs } = req.body;
// Write Swift code to a temporary file
const fs = require("fs");
fs.writeFileSync("temp.swift", code);
// Execute Swift code and capture output
exec(`swift temp.swift`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
res.status(500).json({ output: `Execution error: ${error.message}` });
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
res.status(500).json({ output: `Execution stderr: ${stderr}` });
return;
}
// Split stdout by lines and remove empty lines
const outputLines = stdout.split("\n").filter((line) => line.trim() !== "");
// Send output back to client
res.json({ output: outputLines.join("\n") });
});
});
app.post("/execute_kotlin", (req, res) => {
const { code, inputs } = req.body;
// Write the Kotlin code to a temporary file
fs.writeFileSync("Main.kt", code);
// Compile the Kotlin code
exec(
"kotlinc Main.kt -include-runtime -d Main.jar",
(compileErr, stdout, stderr) => {
if (compileErr) {
return res.json({ output: stderr });
}
// Run the compiled Kotlin program
exec(
`echo "${inputs.join("\n")}" | java -jar Main.jar`,
(runErr, runStdout, runStderr) => {
if (runErr) {
return res.json({ output: runStderr });
}
res.json({ output: runStdout });
}
);
}
);
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});