Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…utor into main
  • Loading branch information
tomarviii88 committed Jan 9, 2021
2 parents bbb399a + fec3f73 commit 0fe1a61
Showing 1 changed file with 53 additions and 53 deletions.
106 changes: 53 additions & 53 deletions rce-server/server/api/services/code.service.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import fs from 'fs';
import path from 'path';
import util from 'util';
import { execFile, spawn, exec } from 'child_process';
import ValidationService from './validation.service';
import fs from "fs";
import path from "path";
import util from "util";
import { execFile, spawn, exec } from "child_process";
import ValidationService from "./validation.service";
const ROOT_DIR = `${process.cwd()}`;
const SOURCE_DIR = path.join(ROOT_DIR, 'executor');
const SOURCE_DIR = path.join(ROOT_DIR, "executor");
const TARGET_DIR = `/app/codes`;
const IMAGE_NAME = 'executor:1.0';
const IMAGE_NAME = "executor:1.0";
//const VOL_NAME = `my_vol`;
const VOL_NAME = SOURCE_DIR;

class CodeService {
async execute(code, input, lang, id) {
//console.log('code', code);
try {
!input ? (input = '') : null;
!input ? (input = "") : null;

// validating code
// await this.validateCode(code, input, lang, id);
Expand All @@ -26,7 +26,7 @@ class CodeService {
);
if (!isValid) {
throw {
message
message,
};
}

Expand Down Expand Up @@ -54,7 +54,7 @@ class CodeService {
);

if (OUTPUT) {
console.log('output', OUTPUT.toString());
console.log("output", OUTPUT.toString());
return OUTPUT.toString();
}
} catch (error) {
Expand All @@ -65,28 +65,28 @@ class CodeService {
async writeFile(code, lang, input, id) {
let fileName = `${id}code`;
switch (lang) {
case 'javascript': {
fileName += '.js';
case "javascript": {
fileName += ".js";
break;
}
case 'cpp': {
fileName += '.cpp';
case "cpp": {
fileName += ".cpp";
break;
}
case 'python': {
fileName += '.py';
case "python": {
fileName += ".py";
break;
}
case 'java': {
fileName += '.java';
case "java": {
fileName += ".java";
break;
}
case 'c': {
fileName += '.c';
case "c": {
fileName += ".c";
break;
}
default: {
throw { message: 'Invalid language' };
throw { message: "Invalid language" };
}
}
const write = util.promisify(fs.writeFile);
Expand All @@ -96,60 +96,60 @@ class CodeService {
await write(path.join(SOURCE_DIR, `${id}input.txt`), input);
return {
file: fileName,
inputFile: `${id}input.txt`
inputFile: `${id}input.txt`,
};
} catch (error) {
throw { message: error };
}
}

async writeCommand(lang, file, input, id, code) {
let command = '';
let command = "";
switch (lang) {
case 'javascript': {
command = `cd ${TARGET_DIR} && node ${file} < ${input}`;
case "javascript": {
command = `cd "${TARGET_DIR}" && node ${file} < ${input}`;
break;
}
case 'cpp': {
command = `cd ${TARGET_DIR} && g++ -o ${id} ${file} && ./${id} < ${input}`;
case "cpp": {
command = `cd "${TARGET_DIR}" && g++ -o ${id} ${file} && ./${id} < ${input}`;
break;
}
case 'python': {
command = `cd ${TARGET_DIR} && python ${file} < ${input}`;
case "python": {
command = `cd "${TARGET_DIR}" && python ${file} < ${input}`;
break;
}
case 'java': {
case "java": {
let className = await this.extractJavaClassName(code);
className = className.split(/\s/).join('');
console.log('class ', className);
command = `cd ${TARGET_DIR} && javac ${file} && java ${className} < ${input}`;
className = className.split(/\s/).join("");
console.log("class ", className);
command = `cd "${TARGET_DIR}" && javac ${file} && java ${className} < ${input}`;
break;
}
case 'c': {
command = `cd ${TARGET_DIR} && gcc -o ${id} ${file} && ./${id} < ${input}`;
case "c": {
command = `cd "${TARGET_DIR}" && gcc -o ${id} ${file} && ./${id} < ${input}`;
break;
}
default: {
throw { message: 'Invalid language' };
throw { message: "Invalid language" };
}
}

const containerName = `${id}container`;

const runCode = `docker exec ${containerName} sh -c "${command}"`;

const runContainer = `docker run -it -d --name ${containerName} -v ${VOL_NAME}:${TARGET_DIR} ${IMAGE_NAME}`;
const runContainer = `docker run -it -d --name ${containerName} -v "${VOL_NAME}":${TARGET_DIR} ${IMAGE_NAME}`;

return { runCode, runContainer };
}

async execChild(runCode, runContainer, id, file, inputFile, lang, code) {
return new Promise((resolve, reject) => {
const execCont = exec(`${runContainer}`);
execCont.on('error', err => {
throw { status: '404', message: err };
execCont.on("error", (err) => {
throw { status: "404", message: err };
});
execCont.stdout.on('data', () => {
execCont.stdout.on("data", () => {
exec(`${runCode}`, async (error, stdout, stderr) => {
await this.endContainer(id);
await this.deleteFiles(file, inputFile, lang, id, code);
Expand All @@ -164,26 +164,26 @@ class CodeService {
}

async deleteFiles(fileName, inputName, lang, id, code) {
fs.unlinkSync(path.join(SOURCE_DIR, fileName), err => {
fs.unlinkSync(path.join(SOURCE_DIR, fileName), (err) => {
if (err) throw { message: err };
});
if (inputName) {
fs.unlinkSync(path.join(SOURCE_DIR, inputName), err => {
fs.unlinkSync(path.join(SOURCE_DIR, inputName), (err) => {
if (err) throw { message: err };
});
}
if (lang == 'cpp' || lang == 'c') {
if (lang == "cpp" || lang == "c") {
if (fs.existsSync(path.join(SOURCE_DIR, id)))
fs.unlinkSync(path.join(SOURCE_DIR, id), err => {
fs.unlinkSync(path.join(SOURCE_DIR, id), (err) => {
if (err) throw err;
});
}
if (lang == 'java') {
if (lang == "java") {
let className = await this.extractJavaClassName(code);
className = className.split(/\s/).join('');
console.log('delete', className);
className = className.split(/\s/).join("");
console.log("delete", className);
if (fs.existsSync(path.join(SOURCE_DIR, `${className}.class`)))
fs.unlinkSync(path.join(SOURCE_DIR, `${className}.class`), err => {
fs.unlinkSync(path.join(SOURCE_DIR, `${className}.class`), (err) => {
if (err) throw err;
});
}
Expand All @@ -195,25 +195,25 @@ class CodeService {
exec(`${exit}`, (error, stdout, stderr) => {
if (error) {
console.log(error);
} else console.log('Container stoped and deleted');
} else console.log("Container stoped and deleted");
});
}

async extractJavaClassName(s) {
let prefix = 'class';
let suffix = '{';
let prefix = "class";
let suffix = "{";
let i = s.indexOf(prefix);
if (i >= 0) {
s = s.substring(i + prefix.length);
} else {
return '';
return "";
}
if (suffix) {
i = s.indexOf(suffix);
if (i >= 0) {
s = s.substring(0, i);
} else {
return '';
return "";
}
}
return s;
Expand Down

0 comments on commit 0fe1a61

Please sign in to comment.