-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathssh_server.js
More file actions
149 lines (126 loc) · 4.23 KB
/
Copy pathssh_server.js
File metadata and controls
149 lines (126 loc) · 4.23 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
const express = require("express");
const fs = require("fs");
const http = require("http");
var cors = require('cors');
var SSHClient = require("ssh2").Client;
var utf8 = require("utf8");
var Docker = require('dockerode');
const { disconnect } = require("process");
var docker = new Docker();
const app = express();
var serverPort = 4000;
var server = http.createServer(app);
const container_options = {
Image: 'ubuntu/tet:for-a-compile',
AttachStdin: false,
AttachStdout: true,
AttachStderr: true,
Tty: true,
OpenStdin: false,
StdinOnce: false,
Cmd: ['/bin/bash']
};
const exec_options = {
"AttachStdout": true,
"AttachStderr": true,
"AttachStdin": true,
"Tty": true,
Cmd: ['/bin/bash']
};
const exec_start_options = {
'Tty': true,
stream: true,
stdin: true,
stdout: true,
stderr: true,
// fix vim
hijack: true
};
//set the template engine ejs
app.use(express.json());
app.set("view engine", "ejs");
//middlewares
app.use(express.static("public"));
function CORS_(res){
console.log('************ option ************');
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers',
'Content-Type, Authorization, Content-Length, X-Requested-With');
res.send();
}
var gcontainer
var gstream;
var gsocket;
var all_data = '';
var get_data = '';
var send_mode = true;
app.options('/source', (req, res) => {
CORS_(res);
});
// =================================== routes ===================================
app.post('/source', (req, res) => {
let source = (req.body['code'].replace(/\t/gi,''));
//CORS_(res);
send_mode = false;
console.log('================ send_mode : ' , send_mode);
all_data = '';
gstream.write('\x03\n');
gstream.write(`cd ~/ && rm -rf ./* && echo '${source}' >> main.cpp && g++ main.cpp -o main && clear\n`, function(){
gstream.write('./main\n', function(){
send_mode = true;
});
});
console.log('================ send_mode : ' , send_mode)
});
app.get("/", (req, res) => {
res.render("index");
});
// =================================== //routes ===================================
server.listen(serverPort);
const io = require("socket.io")(server);
//Socket Connection
io.on("connection", function(socket) {
console.log('connection com');
socket.on('exec', function (w, h) {
docker.createContainer(container_options, function (err, container) {
console.log("container", container)
gcontainer = container;
if (err) { console.log(err);}
container.start(function(err, data){
container.exec(exec_options, (err, exec) => {
console.log('exec', exec);
if (err) console.log(err);
exec.start(exec_start_options, (err, stream) => {
gstream = stream;
console.log('stream : ', stream);
var dimensions = { h, w };
if (dimensions.h != 0 && dimensions.w != 0)
exec.resize(dimensions, () => { });
stream.on('data', (chunk) => {
all_data += chunk.toString();
if (send_mode === true)
socket.emit('show', chunk.toString());
});
socket.on('cmd', (data) => {
get_data += data;
stream.write(data);
if(data == '?')
console.log('all : ',all_data, ' vs ', get_data);
});
});
});
});
});
});
socket.on('disconnect', function(){
try {
gcontainer.remove({force: true}, function (err, data) {
console.log('remove success!!');
});
}
catch {
console.log('can\'t disconnect : ');
}
})
});