-
Notifications
You must be signed in to change notification settings - Fork 3
/
tcp2udp.js
31 lines (28 loc) · 936 Bytes
/
tcp2udp.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
module.exports = function(sock, debug){
sock.send = function(data, offset, length, port, host, callback){
var sendData = data.slice(offset, offset + length);
this.write(sendData);
if(debug){
console.log("TCP2UDP Send:");
console.log(sendData);
}
if(callback) callback();
};
sock.on('data', function(data){
var rinfo = {address: this.remoteAddress, port: this.remotePort};
if(!this.concatData) this.concatData = data;
else this.concatData = Buffer.concat([this.concatData, data]);
while(this.concatData.length >= 4) {
var len = this.concatData.readUInt16BE(2) + 20;
if(this.concatData.length < len) break;
var msg = this.concatData.slice(0, len);
if(debug){
console.log("TCP2UDP Message:");
console.log(msg);
}
this.emit('message', msg, rinfo);
this.concatData = this.concatData.slice(len);
}
});
return sock;
}