-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
194 lines (168 loc) · 4.15 KB
/
server.js
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
var express= require('express');
const {query}=require('express');
var crypto = require('crypto');
const {Server } = require('http');
var fs= require('fs');
const util = require('./util');
const wbs = require('./websocket');
const index = fs.readFileSync('./index.html','utf-8');
const clientSoc = fs.readFileSync('./clientSoc.js', 'utf-8');
const clientscript = fs.readFileSync('./clientscript.js', 'utf-8');
// const favicon = fs.readFileSync('./favicon.ico', 'utf-8');
const bodyParser = require("body-parser");
const {spawn} =require('child_process');
const exe = require("child_process").execSync;
var dataRes= "";
var hostIP ="192.168.29.173";
function genRandom()
{
var id =crypto.randomBytes(3).toString('hex');
console.log(id);
return id;
}
function compile()
{
try
{
// var res=spawn('g++',[' ctes.cpp']);
// Timeout()
var res=exe('g++ ctes.cpp');
// res.stdout.pipe('data', (data)=>{
// console.log(' $(data)');
// return 1;
// });
// res.stderr.pipe('data', (data)=>{
// console.log('$(data)');
// return 0;
// });
console.log("com"+res.toString('utf-8'));
if(res.toString('utf-8').length >1)
{
// res.kill();
return 0;
}
return 1;
}
catch (err){
dataRes = err.stderr.toString('utf-8');
console.log(err);
}
}
function pass(){}
function exec(userInput,ress){
// var res= exe(' ./a.out');
var res=spawn('./a.out',{detached: false});
dataRes= "";
res.stdin.write(userInput+"\n");
res.stdin.end();
// setTimeout(pass, 12000);
// await new Promise(resolve => setTimeout(resolve, 5000));
// dataRes= res.toString('utf-8');
console.log(res.toString('utf-8'));
let done = 0;
res.stdout.on('data', (data)=>{
console.log("dat" + data.toString('utf-8'));
dataRes= data.toString('utf-8');
// console.log(dataRes);
// console.log(`stdout: ${data}`);
done =1;
ress.end("Output:\n"+dataRes);
// return 1;
});
res.stderr.on('data', (data)=>{
console.log('$(data)');
done =1;
// ress.end("<HTML><HEAD></HEAD><BODY><b>Output:</b><br>"+dataRes+"</BODY></HTML>");
ress.end("Output:\n"+dataRes);
// return 0;
});
// while(!done)
// {
// await new Promise(resolve => setTimeout(resolve, 5000));
// console.log("RE" + dataRes.toString('utf-8'));
// dataRes= res.toString('utf-8');
// return -1;
// if(done == 1)
// return 1;
// if(res.toString('utf-8').length >1) return 0;
// }
}
var app = express();
var publicDir = require('path').join(__dirname,'/public');
app.use(express.static(publicDir));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.get('/',(req,res)=>{
// res.end(index);
let ranURL = genRandom();
res.redirect("/edit/"+ranURL);
});
app.get('/edit/clientSoc.js',(req,res)=>{
res.end(clientSoc);
});
app.get('/edit/clientscript.js',(req,res)=>{
res.end(clientscript);
});
app.post('/edit/submit',(req,res)=>{
// let lang = req.query['lang'];
let code=req.body.code;
let userInput = req.body.user;
console.log(code);
const fd =fs.openSync('ctes.cpp','w+');
// fs.ftruncate(fd,(err)=>{
// if(err)
// console.log(err);
// });document.getElementById('code').value;
// fs.write(fd,code,0,code.length,null, function(err){
// if(err)
// console.log("write failed");
// });
fs.writeFile(fd, code, err => {
if (err) {
console.error(err)
return
}
});
// fs.close();
let compileSucess=0;
let exeSuccess=-1;
let state = 'processing';
compileSucess=compile();
console.log("Compi "+ compileSucess);
if(compileSucess == 1)
{
state='executing';
exeSuccess = exec(userInput,res);
if(exeSuccess == 1)
{
state="success fully executed"
}
else
{
state="execution failed";
}
}
else
{
state = "Compilation failed:";
console.log("fail");
// res.end("<HTML><HEAD></HEAD><BODY><b>Output:</b><br></BODY></HTML>"+ dataRes);
res.end(state + " \n"+ dataRes);
console.log("fai" + dataRes);
}
console.log("REs" + dataRes);
// res.end(dataRes + " \n"+state);
});
app.get('/edit/*',(req,res)=>{
// console.log(req.params[0]);
// var data= mapData[req.params[0]];
// res.send({data:"sending11"});
res.end(index);
// console.log("wb")
});
var server=app.listen(81,hostIP,()=>{
console.log('listening');
});
wbs;