-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazy-service.js
103 lines (94 loc) · 3.03 KB
/
lazy-service.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
var net = require('net');
var http = require('http');
var spawn = require('child_process').spawn;
var config = require('./config.json');
function getUntilReady(url, callback) {
http.get(url, function(res) {
if (res.statusCode == 200)
callback();
else {
console.warn("Got " + res.statusCode + " from [" + url + "]");
setTimeout(function() {
getUntilReady(url, callback);
}, config.testRetryFrequency);
}
}).on('error', function(e) {
console.error("Got error waiting for [" + url + "]: " + e.message);
setTimeout(function() {
getUntilReady(url, callback);
}, config.testRetryFrequency);
});
}
function runProxy(proxy) {
proxy.starting = false;
proxy.started = false;
proxy.clientHandler = net.createServer(function (clientSocket) {
var serverSocket = null;
var bufferedData = [];
if (!proxy.started && !proxy.starting) {
console.info("Loading " + proxy.name + " process");
proxy.starting = true;
proxy.serverProcess = spawn(proxy.command, proxy.arguments);
proxy.serverProcess.on("exit", function(code, signal) {
if (code !== null) {
console.warn("Lost " + proxy.name + " process with exit code " + code);
} else {
console.warn("Lost " + proxy.name + " process with signal " + signal);
}
proxy.starting = false;
proxy.started = false;
proxy.serverProcess = null;
});
}
getUntilReady(proxy.testUrl, function () {
serverSocket = new net.Socket();
serverSocket.connect(parseInt(proxy.serverPort), proxy.serverHost, function () {
proxy.started = true;
if (bufferedData.length > 0) {
var msg = Buffer.concat(bufferedData);
serverSocket.write(msg);
console.info("Sent " + msg.length + " bytes of buffered data");
bufferedData = [];
}
serverSocket.on("data", function (data) {
clientSocket.write(data);
});
serverSocket.on('end', function() {
console.info("Server hung up");
clientSocket.end();
});
serverSocket.on('disconnect', function() {
console.info("Server lost");
clientSocket.destroy();
});
});
});
clientSocket.on('data', function (msg) {
if (proxy.started)
serverSocket.write(msg);
else {
console.warn("Received " + msg.length + " bytes of client data before server was ready");
bufferedData.push(msg);
}
});
clientSocket.on('end', function() {
console.info("Client hung up");
serverSocket.end();
});
clientSocket.on('disconnect', function() {
console.info("Client lost");
serverSocket.destroy();
});
});
proxy.clientHandler.listen(proxy.clientPort);
console.info("Proxy for " + proxy.name + " is listening on " + proxy.clientPort);
}
function runProxies() {
for (var i in config.proxies) {
if (config.proxies.hasOwnProperty(i)) {
var proxy = config.proxies[i];
runProxy(proxy);
}
}
}
runProxies();