-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.js
57 lines (53 loc) · 1.51 KB
/
proxy.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
var util = require('util');
var net = require("net");
var EventEmitter = require('events').EventEmitter;
function start(options) {
var result = new EventEmitter();
net.createServer(accept).listen(options.proxyPort);
function accept(proxySocket) {
var client = {}; // tag for customer
result.emit('connect', client);
var connected = false;
var buffers = new Array();
var serviceSocket = new net.Socket();
serviceSocket.connect(options.servicePort, options.serviceHost,
function() {
connected = true;
if (buffers.length > 0) {
for (i = 0; i < buffers.length; i++) {
serviceSocket.write(buffers[i]);
}
}
});
proxySocket.on("error", function(e) {
serviceSocket.end();
});
serviceSocket.on("error", function(e) {
console.error("Could not connect to service at host "
+ options.serviceHost + ', port ' + options.servicePort);
proxySocket.end();
});
proxySocket.on("data", function(data) {
result.emit('clientToServer', client, data);
if (connected) {
serviceSocket.write(data);
} else {
buffers[buffers.length] = data;
}
});
serviceSocket.on("data", function(data) {
result.emit('serverToClient', client, data);
proxySocket.write(data);
});
proxySocket.on("close", function(had_error) {
console.log('connection closed by client');
serviceSocket.end();
});
serviceSocket.on("close", function(had_error) {
console.log('connection closed by server');
proxySocket.end();
});
}
return result;
}
exports.start = start;