This repository has been archived by the owner on Mar 14, 2020. It is now read-only.
/
index.js
228 lines (191 loc) · 5.06 KB
/
index.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
var http = require('http');
var pump = require('pump');
var protocol = require('hms-protocol');
var parse = require('hms/lib/parse-remote');
var path = require('path');
var proc = require('child_process');
var fs = require('fs');
var xtend = require('xtend');
var pingable = require('pingable');
var os = require('os');
var once = require('once');
var JSONStream = require('JSONStream');
var param = require('param');
var split = require('split');
var concat = require('concat-stream');
var winston = require('winston');
var logfile = param("logfile");
if (logfile) {
winston.add(winston.transports.File, { filename: logfile, maxsize: 5242880, maxFiles: 10});
}
process.stdout.setMaxListeners(0);
process.stderr.setMaxListeners(0);
var help = param('help');
if (help) {
winston.info("Helps stuff");
process.exit(0);
}
var winstonStream = split(function(message) {
winston.info(message);
});
var HANDSHAKE =
'HTTP/1.1 101 Swiching Protocols\r\n'+
'Upgrade: hms-protocol\r\n'+
'Connection: Upgrade\r\n\r\n';
var POWERSHELL = path.join(__dirname, 'scripts');
var PS_SYS_NATIVE = 'c:\\windows\\sysnative\\windowspowershell\\v1.0\\powershell.exe';
var PS_SYS_32 = 'c:\\windows\\system32\\windowspowershell\\v1.0\\powershell.exe';
var PS = fs.existsSync(PS_SYS_NATIVE) ? PS_SYS_NATIVE : PS_SYS_32;
if (!fs.existsSync(PS)) throw new Error('powershell not found');
var ssh_key = param('i');
var tags = ['windows'].concat(param('tag') || []);
var remote = parse(param('terminal') || process.argv[2] || 'localhost:10002', {key:ssh_key});
var origin = param('id') || os.hostname();
var debug = param('debug');
if (debug) winston.info("Using remote:", remote);
var server = http.createServer(function(request, response) {
response.end('hms-windows-dock\n');
});
var tarServer = http.createServer(function(request, response) {
if (debug) winston.info('Fetching tar: ', request.url);
var req = http.request(xtend(remote, {
method:'GET',
path:request.url,
headers:{origin:origin}
}));
req.on('response', function(res) {
response.writeHead(res.statusCode, res.headers);
pump(res, response);
});
req.on('error', function() {
response.destroy();
});
req.end();
});
tarServer.listen(7001);
var ps = function(file, opts, cb) {
var params = ['-ExecutionPolicy', 'remotesigned', '-File', path.join(POWERSHELL, file+'.ps1')];
Object.keys(opts).forEach(function(key) {
params.push('-'+key, opts[key]);
});
if (debug) winston.info("Spawning ",PS, params);
var ch = proc.spawn(PS, params);
var errorStream = concat(function(error) {
if (error) {
return cb(new Error(error));
}
});
cb = once(cb);
if (debug) ch.stdout.pipe(winstonStream);
ch.stderr.pipe(process.stderr);
ch.stderr.pipe(errorStream);
ch.stdout.pipe(JSONStream.parse()).once('data', function(data) {
cb(null, data);
});
ch.on('close', function(code) {
if (!code ){
return cb();
}
return cb(new Error('Stream closed without data'));
});
};
var dropped = true;
var connect = function() {
var payload = xtend(remote, {
method:'CONNECT',
path:'/dock',
headers:{origin:origin}
})
if (debug) winston.info("Connect:", payload);
var req = http.request(payload);
var reconnect = once(function() {
winston.info("Reconnect");
if (dropped) return setTimeout(connect, 5000);
dropped = true;
return setTimeout(connect, 2500);
});
req.on('error', function(err) {
winston.error("Error:",err);
reconnect();
});
req.on('connect', function(res, socket, data) {
dropped = false;
var peer = protocol();
onpeer(peer);
var payload = {
id: origin,
type: 'dock',
tags: tags
}
if (debug) winston.info("Handshake:", payload);
peer.handshake(payload);
pump(socket, peer, socket, reconnect);
peer.write(data);
});
req.end();
};
connect();
var onpeer = function(peer) {
pingable(peer);
peer.on('add', function(id, opts, cb) {
ps('add', {
serviceName: id
}, cb);
});
peer.on('remove', function(id, cb) {
ps('remove', {
serviceName: id
}, cb);
});
peer.on('sync', function(id, service, cb) {
ps('sync', {
serviceName: id,
fetchTar: path.join(__dirname, 'fetch-tar.js'),
tarball: "http://localhost:7001/" + id
}, cb);
});
peer.on('update', function(id, service, cb) { // do nothing right now
cb();
});
peer.on('restart', function(id, cb) {
ps('restart', {
serviceName: id
}, cb);
});
peer.on('start', function(id, cb) {
ps('restart', {
serviceName: id
}, cb);
});
peer.on('stop', function(id, cb) {
ps('stop', {
serviceName: id
}, cb);
});
peer.on('list', function(cb) {
ps('list', {}, function(err, data) {
if (err) {
return cb(err, null);
}
return cb(null, [].concat(data || []));
});
});
peer.on('ps', function(cb) {
ps('ps', {}, function(err, data) {
if (err) {
return cb(err, null);
}
return cb(null, [{ id: origin, list: [].concat(data || [])}]);
});
});
};
server.on('connect', function(request, socket, data) {
var peer = protocol();
socket.write(HANDSHAKE);
pump(socket, peer, socket);
peer.write(data);
peer.on('handshake', function(handshake) {
onpeer(peer);
});
});
server.listen(10002);