-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (146 loc) · 5.13 KB
/
Copy pathindex.js
File metadata and controls
157 lines (146 loc) · 5.13 KB
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
var dgram = require('dgram');
var events = require('events');
var util = require('util');
var net = require('net');
var UdpProxy = function (options) {
"use strict";
var proxy = this;
var localUdpType = 'udp4';
var serverPort = options.localport || 0;
var serverHost = options.localaddress || '0.0.0.0';
var proxyHost = options.proxyaddress || '0.0.0.0';
this.tOutTime = options.timeOutTime || 10000;
this.family = 'IPv4';
this.udpType = 'udp4';
this.host = options.address || 'localhost';
this.port = options.port || 41234;
this.connections = {};
if (options.ipv6) {
this.udpType = 'udp6';
this.family = 'IPv6';
proxyHost = net.isIPv6(options.proxyaddress) ? options.proxyaddress : '::0';
}
this._details = {
target: {
address: this.host,
family: this.family,
port: this.port
}
};
this._detailKeys = Object.keys(this._details);
if (options.localipv6) {
localUdpType = 'udp6';
serverHost = net.isIPv6(options.localaddress) ? options.localaddress : '::0';
}
this.middleware = options.middleware;
this._server = dgram.createSocket(localUdpType);
this._server.on('listening', function () {
var details = proxy.getDetails({server: this.address()});
setImmediate(function() {
proxy.emit('listening', details);
});
}).on('message', function (msg, sender) {
var client = proxy.createClient(msg, sender);
if (!client._bound) client.bind(0, proxyHost);
else client.emit('send', msg, sender);
}).on('error', function (err) {
this.close();
proxy.emit('error', err);
}).on('close', function () {
proxy.emit('close');
}).bind(serverPort, serverHost);
};
util.inherits(UdpProxy, events.EventEmitter);
UdpProxy.prototype.getDetails = function getDetails(initialObj) {
var self = this;
return this._detailKeys.reduce(function (obj, key) {
obj[key] = self._details[key];
return obj;
}, initialObj);
};
UdpProxy.prototype.hashD = function hashD(address) {
return (address.address + address.port).replace(/\./g, '');
};
UdpProxy.prototype.send = function send(msg, port, address, callback) {
this._server.send(msg, 0, msg.length, port, address, callback);
};
UdpProxy.prototype.createClient = function createClient(msg, sender) {
var senderD = this.hashD(sender);
var proxy = this;
var client;
if (this.connections.hasOwnProperty(senderD)) {
client = this.connections[senderD];
clearTimeout(client.t);
client.t = null;
return client;
}
client = dgram.createSocket(this.udpType);
client.once('listening', function () {
var details = proxy.getDetails({route: this.address(), peer: sender});
this.peer = sender;
this._bound = true;
proxy.emit('bound', details);
this.emit('send', msg, sender);
}).on('message', function (msg, sender) {
if (proxy.middleware) {
var self = this;
proxy.middleware.proxyMsg(msg, sender, this.peer, function (msg,sender,peer) {
proxy.handleProxyMsg(self, proxy, msg, sender, peer);
});
}
else {
proxy.handleProxyMsg(this, proxy, msg, sender, this.peer);
}
}).on('close', function () {
proxy.emit('proxyClose', this.peer);
this.removeAllListeners();
delete proxy.connections[senderD];
}).on('error', function (err) {
this.close();
proxy.emit('proxyError', err);
}).on('send', function (msg, sender) {
if (proxy.middleware) {
var self = this;
proxy.middleware.message(msg, sender, function (msg,sender) {
proxy.handleMessage(self, proxy, msg, sender);
});
}
else {
proxy.handleMessage(this, proxy, msg, sender);
}
});
this.connections[senderD] = client;
return client;
};
UdpProxy.prototype.handleProxyMsg = function handleProxyMsg(socket, proxy, msg, sender, peer) {
proxy.send(msg, peer.port, peer.address, function (err, bytes) {
if (err) socket.emit('proxyError', err);
});
proxy.emit('proxyMsg', msg, sender, peer);
};
UdpProxy.prototype.handleMessage = function handleMessage(socket, proxy, msg, sender) {
proxy.emit('message', msg, sender);
socket.send(msg, 0, msg.length, proxy.port, proxy.host, function (err, bytes) {
if (err) proxy.emit('proxyError', err);
if (!socket.t) socket.t = setTimeout(function () {
socket.close();
}, proxy.tOutTime);
});
};
UdpProxy.prototype.close = function close(callback) {
// close clients
var proxyConnections = this.connections;
Object.keys(proxyConnections).forEach(function (senderD) {
var client = proxyConnections[senderD];
if (client.t) {
clearTimeout(client.t);
client.t = null;
client.close();
}
});
this.connections = {};
this._server.close(callback || function () {});
};
exports.createServer = function (options) {
return new UdpProxy(options);
};