-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconsoleSocketServer.js
191 lines (157 loc) · 5.09 KB
/
consoleSocketServer.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
var spawn = require('child_process').spawn;
var debug = require('debug')('consoleSocketServer');
var socketIo = require('socket.io');
var fs = require('fs');
var getFreshJobs = require('./utils/getFreshJobs');
var randomJobId = require('./utils/random/randomJobId');
var SocketServer = function () {
this.server = null;
};
SocketServer.prototype.listen = function (httpServer) {
if (this.server) {
throw new Error('Already started!');
}
this.server = socketIo(httpServer);
this.server.on('connection', function (socket) {
var jobs = getFreshJobs();
debug('user connected');
var jobId = null;
var jobStrVariant = null;
var jobStrId = null;
var cmd = null;
socket.on('start job', function (jobDesc) {
if (!jobDesc.jobKey) {
socketErrorAndDisconnect(socket, 'Job description is missing jobKey.');
} else if (!jobDesc.jobVariantKey) {
socketErrorAndDisconnect(socket, 'Job description is missing jobVariantKey.');
} else if (!jobs.hasOwnProperty(jobDesc.jobKey)) {
socketErrorAndDisconnect(socket, 'Job key "' + jobDesc.jobKey + '" not found.');
} else if (!jobs[jobDesc.jobKey].variants.hasOwnProperty(jobDesc.jobVariantKey)) {
socketErrorAndDisconnect(socket, 'Job "' + jobDesc.jobKey + '" doesn\'t have variant "' + jobDesc.jobVariantKey + '".');
} else if (!jobs[jobDesc.jobKey].variants[jobDesc.jobVariantKey].command) {
socketErrorAndDisconnect(socket, 'Job "' + jobDesc.jobKey + '" variant "' + jobDesc.jobVariantKey + '" doesn\'t have command.');
} else {
jobId = randomJobId();
var job = jobs[jobDesc.jobKey];
var jobVariant = job.variants[jobDesc.jobVariantKey];
jobStrVariant = jobDesc.jobKey + '/' + jobDesc.jobVariantKey;
jobStrId = jobStrVariant + '(' + jobId + ')';
try {
var jobDescriptor = {
id: jobId,
command: jobVariant.command,
args: jobVariant.args || [],
cwd: fs.realpathSync(jobVariant.cwd || job.cwd || __dirname)
};
var uid = null;
if (job.hasOwnProperty('uid')) uid = job.uid;
if (jobVariant.hasOwnProperty('uid')) uid = jobVariant.uid;
if (uid != null) jobDescriptor.uid = uid;
var gid = undefined;
if (job.hasOwnProperty('gid')) gid = job.gid;
if (jobVariant.hasOwnProperty('gid')) gid = jobVariant.gid;
if (gid != null) jobDescriptor.gid = gid;
} catch (e) {
socketErrorAndDisconnect(socket, 'Error resolving process path: ' + e.message);
return;
}
socket.emit('job start', jobDescriptor);
try {
debug('Spawn process', jobDescriptor.command, jobDescriptor.args, jobDescriptor);
cmd = spawn(jobDescriptor.command, jobDescriptor.args, jobDescriptor);
} catch (e) {
debug('Spawn thrown error', e);
socketErrorAndDisconnect(socket, 'Starting process produced following error: ' + e.message);
return;
}
cmd.stdout.on('data', function (data) {
var str = data.toString();
// debug(jobStrId, 'stdout', str);
socket.emit('job stdout', str);
});
cmd.stderr.on('data', function (data) {
var str = data.toString();
// debug(jobStrId, 'stderr', str);
socket.emit('job stderr', str);
});
cmd.on('close', function (code) {
socket.emit('job end', {
id: jobId,
code: code
});
socket.disconnect();
cmd = null;
jobId = null;
});
cmd.on('error', function (e) {
debug('app error - ' + jobStrVariant, e.stack);
socketError(socket, 'Running process produced following error: ' + e.message);
});
socket.emit('job state', 'Job ' + jobDesc.jobKey + '/' + jobDesc.jobVariantKey + ' started on server.');
}
});
/*
// character
socket.on('c', function(char) {
if (!cmd) {
socketError(socket, 'Job is not running, ignoring input.');
} else {
cmd.stdin.write(char);
}
});
// backspace
socket.on('b', function() {
if (!cmd) {
socketError(socket, 'Job is not running, ignoring input.');
} else {
cmd.stdin.write(String.fromCharCode(8));
}
});
// newline
socket.on('l', function(buffer) {
if (!cmd) {
socketError(socket, 'Job is not running, ignoring input.');
} else {
cmd.stdin.write('\n');
}
});
*/
// line
socket.on('line', function(buffer) {
if (!cmd) {
socketError(socket, 'Job is not running, ignoring input.');
} else {
cmd.stdin.write(buffer + '\n');
}
});
socket.on('ctrl+c', function() {
if (!cmd) {
socketError(socket, 'Job is not running, ignoring input.');
} else {
cmd.kill();
debug('Job ' + jobStrId + ' killed.');
}
});
socket.on('disconnect', function () {
if (cmd) {
debug('User disconnected and job ' + jobStrId + ' is still running, killing it');
cmd.kill();
debug('Job ' + jobStrId + ' killed.');
} else {
debug('User disconnected.');
}
});
socket.on('error', function(e) {
debug('socket error', e);
})
});
};
function socketError(socket, err) {
socket.emit('api error', err);
}
function socketErrorAndDisconnect(socket, err) {
socketError(socket, err);
socket.disconnect();
}
var socketServerInstance = new SocketServer();
module.exports = socketServerInstance;