forked from philhartung/valve-master
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalve-master.js
97 lines (79 loc) · 2.66 KB
/
valve-master.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
var dgram = require('dgram');
var valvemaster = function(timeout) {
timeout = timeout || 3000;
client = dgram.createSocket('udp4');
filter = '';
region = 0xFF;
cb = function() {};
inQuery = false;
socketLive = true;
timer = null;
ipArr = [];
var vm = this;
vm.US_EAST = 0x00;
vm.US_WEST = 0x01;
vm.SOUTCH_AMERICA = 0x02;
vm.EUROPE = 0x03;
vm.ASIA = 0x04;
vm.AUSTRALIA = 0x05;
vm.MIDDLE_EAST = 0x06;
vm.AFRICA = 0x07;
vm.WORLD = 0xFF;
client.on('message', function(bytes, rinfo) {
var buffer = new Buffer(bytes);
if (buffer.length >= 6 && buffer.toString('hex', 0, 6) == 'ffffffff660a' && inQuery === true) {
clearTimeout(timer);
var seed;
for (var i = 6; i < buffer.length; i += 6) {
port = buffer.readUInt16BE(i + 4);
ip = buffer.readUInt8(i) + '.' + buffer.readUInt8(i + 1) + '.' + buffer.readUInt8(i + 2) + '.' + buffer.readUInt8(i + 3);
seed = ip + ':' + port;
if (seed == '0.0.0.0:0') {
inQuery = false;
cb(null, ipArr);
return;
}
ipArr.push([seed, port]);
}
var next = new Buffer('1 ' + seed + '\0' + filter + '\0');
next[1] = region;
client.send(next, 0, next.length, rinfo.port, rinfo.address);
timer = setTimeout(function() {
inQuery = false;
cb('Timeout', null);
}, timeout);
}
});
vm.query = function(pfilter, pregion, address, port, pcb) {
if (inQuery === false && socketLive === true) {
inQuery = true;
filter = pfilter;
region = pregion;
cb = pcb;
ipArr = [];
buffer = new Buffer('1 0.0.0.0:0\0' + filter + '\0'),
buffer[1] = region;
client.send(buffer, 0, buffer.length, port, address);
timer = setTimeout(function() {
inQuery === false
cb('Timeout', null);
}, timeout);
} else if (inQuery === true) {
acb('You cannot do multiple queries at a time', null);
} else {
acb('Socket is closed', null);
}
}
vm.close = function(cb) {
cb = cb || function() {};
if (inQuery === true) {
cb('You cannot close the socket during query');
} else if (socketLive === true) {
socketLive = false;
client.close();
} else {
cb('Socket is already closed');
}
}
}
module.exports = valvemaster;