-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
206 lines (172 loc) · 5.28 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
195
196
197
198
199
200
201
202
203
204
205
206
const PORT = 1234;
const util = require('./util');
const WSServer = require('websocket').server;
const http = require('http');
const fs = require('fs');
const uuid = require('uuid/v1');
const child_process = require('child_process');
let tool;
try {
tool = require('./tool');
} catch(e) {
console.log('Could not find tool definition.');
console.log(e);
process.exit(1);
}
const server = http.createServer((req, res) => {
res.statusCode = 404;
res.end();
});
server.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});
const wsServer = new WSServer({
httpServer: server,
maxReceivedFrameSize: 10 * 1024 * 1024,
maxReceivedMessageSize: 10 * 1024 * 1024,
autoAcceptConnections: false
});
wsServer.on('request', (req) => {
let haveProtocol = false;
for(let protocol of req.requestedProtocols) {
if(protocol === 'fmt-tool') haveProtocol = true;
}
if(!haveProtocol) {
req.reject(412, 'invalid protocol');
return;
}
const conn = req.accept('fmt-tool', req.origin);
conn.on('message', function(rawMessage) {
if(rawMessage.type !== 'utf8') {
conn.sendUTF(JSON.stringify({'type': 'error', 'errorDescription': 'can only accept text messages'}));
return;
}
let message;
try {
message = JSON.parse(rawMessage.utf8Data);
} catch(e) {
conn.sendUTF(JSON.stringify({'type': 'error', 'errorDescription': 'message must be valid JSON'}));
return;
}
if(message.type === undefined) {
conn.sendUTF(JSON.stringify({'type': 'error', 'errorDescription': 'message must include a type'}));
return;
}
switch(message.type) {
case 'submit':
submit(conn, message);
break;
default:
conn.sendUTF(JSON.stringify({'type': 'error', 'errorDescription': 'message type not recognized'}));
}
if(rawMessage.type === 'utf8') {
try {
const message = JSON.parse(rawMessage.utf8Data);
if(message.type === undefined) return;
switch(message.type) {
case 'submit':
if(message.data === undefined) return;
if(message.lang === undefined || LANGUAGES[message.lang] === undefined) return;
submit(conn, message.lang, message.data);
break;
}
} catch(e) {
console.log(e);
}
}
});
});
function validateFiles(files, depth) {
if(depth == util.MAX_PATH_DEPTH) {
return `file structure too nested: max depth is ${util.MAX_PATH_DEPTH}`;
}
if(typeof files === 'string') {
// For now there are no particular conditions on files; the whole WS message
// is limited to 10MiB.
} else if(typeof files === 'object') {
for(let [name, child] of Object.entries(files)) {
if(name.length > util.MAX_FILE_NAME_LENGTH) {
return 'filenames may not be longer than 64 characters';
}
if(!util.FILE_PATTERN.test(name)) {
return 'Filenames may only contain letters, numbers, - and _. They must have a non-empty extension or no extension.';
}
let err = validateFiles(child, depth+1);
if(err) {
return err;
}
}
} else {
return 'Invalid file structure';
}
}
function storeFilesSync(path, files) {
if(typeof files === 'string') {
fs.writeFileSync(path, files);
} else {
fs.mkdirSync(path);
for(let [name, child] of Object.entries(files)) {
storeFilesSync(path + '/' + name, child);
}
}
}
function deleteFilesSync(path, files) {
if(typeof files === 'string') {
fs.unlinkSync(path);
} else {
for(let [name, child] of Object.entries(files)) {
deleteFilesSync(path + '/' + name, child);
}
fs.rmdirSync(path);
}
}
function submit(conn, message) {
if(message.files === undefined) {
conn.sendUTF(JSON.stringify({'type': 'error', 'errorDescription': 'files attribute not present'}));
return;
}
if(message.arguments === undefined) {
conn.sendUTF(JSON.stringify({'type': 'error', 'errorDescription': 'arguments attribute not present'}));
return;
}
const files = message.files;
const arguments = message.arguments;
let err = validateFiles(files, 0);
if(err) {
conn.sendUTF(JSON.stringify({'type': 'error', 'errorDescription': err}));
return;
}
const id = uuid();
const path = '/tmp/' + id;
try {
storeFilesSync(path, files);
} catch(e) {
conn.sendUTF(JSON.stringify({'type': 'error', 'errorDescription': 'I/O error'}));
return;
}
let procDef;
try {
procDef = tool.getProcess(arguments, path, files);
} catch(e) {
conn.sendUTF(JSON.stringify({'type': 'error', 'errorDescription': e.message}));
return;
}
conn.sendUTF(JSON.stringify({'type': 'accept', 'id': id}));
procDef.options.detached = true;
let p;
try {
p = child_process.spawn(procDef.command, procDef.args, procDef.options);
} catch(e) {
conn.sendUTF(JSON.stringify({'type': 'error', 'errorDescription': e.message}));
}
p.stdout.on('data', (data) => {
conn.sendUTF(JSON.stringify({'type': 'stdout', 'id': id, 'data': data.toString()}));
});
p.stderr.on('data', (data) => {
conn.sendUTF(JSON.stringify({'type': 'stderr', 'id': id, 'data': data.toString()}));
});
p.on('close', (code) => {
conn.sendUTF(JSON.stringify({'type': 'finished', 'id': id, 'exitCode': code}));
deleteFilesSync(path, files);
});
}