forked from rancher/console-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.js
executable file
·229 lines (196 loc) · 5.27 KB
/
agent.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env node
var urllib = require('url');
var net = require('net');
var spawn = require('child_process').spawn;
var fs = require('fs');
var WS = require('ws');
var request = require('request');
var JWT = require('jsonwebtoken');
var argv = require('minimist')(process.argv.slice(2),{default: {
'help': false,
'bind': '0.0.0.0',
'port': 8001,
'key': null,
'docker': 'http://unix:/var/run/docker.sock:/v1.15',
}, alias: {
'help': 'h',
'bind': 'b',
'port': 'p',
'key': 'k',
'docker': 'd',
}
});
if ( argv.help )
{
help();
process.exit(1);
}
// Strip trailing slashes
argv.docker = argv.docker.replace(/\/+$/,'');
// Get the public key
var publicKey = '';
if (argv.key)
{
if (argv.key.indexOf('-----BEGIN CERTIFICATE-----') === 0)
{
publicKey = argv.key;
}
else
{
try
{
publicKey = fs.readFileSync(argv.key,'utf8');
}
catch(e)
{
console.error('Public key not found:', e);
process.exit(1);
}
}
}
else
{
console.error('Public key not provided');
help();
process.exit(1);
}
// ------------
var server = new WS.Server({
port: argv.port,
host: argv.bind
});
server.on('connection', connection);
/*
process.on('SIGINT', function() {
server.close();
});
*/
console.error('Listening on', argv.bind+':'+argv.port, 'for Docker at', argv.docker);
// ------------
function connection(client) {
var ip = client.upgradeReq.connection.remoteAddress;
userLog('Opened connection');
var parsed = urllib.parse(client.upgradeReq.url, true);
var rawToken = parsed.query.token;
if (!rawToken)
{
return void userError('No token');
}
JWT.verify(rawToken, publicKey, function(err, token) {
if ( err )
{
return void userError('Invalid token:',err);
}
userLog('Verified token for', token.exec.Container);
exec(token.exec, function(err, execId) {
if ( err )
{
return void userError(err);
}
start(execId);
});
});
function exec(body, cb)
{
request({
url: argv.docker +'/containers/'+ encodeURIComponent(body.Container) +'/exec',
method: 'POST',
json: true,
body: body
}, done);
function done(err, res, body) {
if ( err )
{
userError('Error in exec 1:', err);
return void cb(err);
}
if ( res.statusCode === 201 )
{
userLog('Created exec', body.Id);
cb(null, body.Id);
}
else
{
userError('Error in exec 2:', body);
cb(new Error(body));
}
}
}
function start(execId)
{
userLog('Starting ('+ execId +')');
var docker = request({
url: argv.docker+'/exec/'+ execId +'/start',
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
'Detach': false,
'Tty': true
})
});
docker.on('data', function(data) {
// userLog('From docker ('+ execId +'):', data.toString('utf8'));
if ( client.readyState == WS.OPEN )
{
client.send(data.toString('base64'), {binary: false});
}
});
docker.on('error', function(err) {
userLog('Docker error ('+ execId +'):', err);
});
docker.on('end', function() {
userLog('Docker closing ('+ execId +')');
if ( client.readyState == WS.OPEN )
{
client.terminate();
}
});
client.on('message', function(data) {
// userLog('From client ('+ execId +'):', data.toString('utf8'));
docker.write(new Buffer(data,'base64'));
});
client.on('end', function() {
userLog('end');
});
client.on('close', function() {
userLog('Client closing ('+ execId +')');
// Send a random ctrl-D to attempt to kill the bash process.
// see https://github.com/docker/docker/issues/9098
// and https://github.com/docker/docker/pull/9167
// @TODO remove when this is fixed in Docker
docker.write('\u0004');
docker.end();
});
}
function userError(msg)
{
var args = Array.prototype.slice.call(arguments);
args.unshift('['+ ip +']');
args.unshift('['+ (new Date()).toISOString() +']');
console.error.apply(null,args);
client.send('ERROR:'+msg, {binary: false}, function() {
client.terminate();
});
}
function userLog(/*arguments*/)
{
var args = Array.prototype.slice.call(arguments);
args.unshift('['+ ip +']');
args.unshift('['+ (new Date()).toISOString() +']');
console.log.apply(null,args);
}
}
// ------------
function help()
{
console.log('Usage:', process.argv[1],'--key <path|PEM-encoded string> [--port <num>] [--bind <ip address>] [--docker <url for docker API>]');
console.log(' -h, --help: Show this message, but you already know that.');
console.log(' -k --key=<path|PEM-encoded string>: Path to a PEM-encoded public key, or the actual key as a PEM-encoded string');
console.log(' -p, --port=<num>: TCP port to listen on (default: 8001');
console.log(' -b, --bind=<ip address>: IP address to bind to/listen on (default: 0.0.0.0');
console.log(' -d, --docker=<url>: URL to connect to the docker remote API:');
console.log(' e.g. remote host: "http://docker.host:2375"');
console.log(' e.g. local socket: "http://unix:/var/run/docker.sock:/v1.15" (default)');
}