forked from JJJ4n/node-samp-query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
samp-query.js
217 lines (147 loc) · 6.46 KB
/
samp-query.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
var dgram = require('dgram')
var query = function (options, callback) {
var self = this
if(typeof options === 'string') options.host = options
options.port = options.port || 7777
options.timeout = options.timeout || 1000
if(!options.host)
return callback.apply(options, [ 'Invalid host' ])
if(!isFinite(options.port) || options.port < 1 || options.port > 65535)
return callback.apply(options, [ 'Invalid port' ])
var response = {}
request.call(self, options, 'i', function(error, information) {
if(error) return callback.apply(options, [ error ])
response.address = options.host
response.port = options.port
response.hostname = information.hostname
response.gamemode = information.gamemode
response.mapname = information.mapname
response.passworded = information.passworded === 1
response.maxplayers = information.maxplayers
response.online = information.players
request.call(self, options, 'r', function(error, rules) {
if(error) return callback.apply(options, [ error ])
rules.lagcomp = rules.lagcomp === 'On'
rules.weather = parseInt(rules.weather, 10)
response.rules = rules
if(response.online > 100) {
response.players = []
return callback.apply(options, [ false, response ])
}
else {
request.call(self, options, 'd', function(error, players) {
if(error) return callback.apply(options, [ error ])
response.players = players
return callback.apply(options, [ false, response ])
})
}
})
})
}
var request = function(options, opcode, callback) {
var socket = dgram.createSocket("udp4")
var packet = new Buffer(10 + opcode.length)
packet.write('SAMP')
for(var i = 0; i < 4; ++i)
packet[i + 4] = options.host.split('.')[i]
packet[8] = options.port & 0xFF
packet[9] = options.port >> 8 & 0xFF
packet[10] = opcode.charCodeAt(0)
try {
socket.send(packet, 0, packet.length, options.port, options.host, function(error, bytes) {
if(error) return callback.apply(options, [ error ])
})
} catch(error) {
return callback.apply(options, [ error ])
}
var controller = undefined
var onTimeOut = function() {
socket.close()
return callback.apply(options, [ 'Host unavailable' ])
}
controller = setTimeout(onTimeOut, options.timeout)
socket.on('message', function (message) {
if(controller)
clearTimeout(controller)
if(message.length < 11) return callback.apply(options, [ true ])
else {
socket.close()
message = message.slice(11)
var object = {}
var array = []
var strlen = 0
var offset = 0
try {
if(opcode == 'i') {
object.passworded = message.readUInt8(offset)
offset += 1
object.players = message.readUInt16LE(offset)
offset += 2
object.maxplayers = message.readUInt16LE(offset)
offset += 2
strlen = message.readUInt16LE(offset)
offset += 4
object.hostname = decode(message.slice(offset, offset += strlen))
strlen = message.readUInt16LE(offset)
offset += 4
object.gamemode = decode(message.slice(offset, offset += strlen))
strlen = message.readUInt16LE(offset)
offset += 4
object.mapname = decode(message.slice(offset, offset += strlen))
return callback.apply(options, [ false, object ])
}
if(opcode == 'r') {
var rulecount = message.readUInt16LE(offset)
offset += 2
var property, value = undefined
while(rulecount) {
strlen = message.readUInt8(offset)
++offset
property = decode(message.slice(offset, offset += strlen))
strlen = message.readUInt8(offset)
++offset
value = decode(message.slice(offset, offset += strlen))
object[property] = value
--rulecount
}
return callback.apply(options, [ false, object ])
}
if(opcode == 'd') {
var playercount = message.readUInt16LE(offset)
offset += 2
var player = undefined;
while(playercount) {
player = {}
player.id = message.readUInt8(offset)
++offset
strlen = message.readUInt8(offset)
++offset
player.name = decode(message.slice(offset, offset += strlen))
player.score = message.readUInt16LE(offset)
offset += 4
player.ping = message.readUInt16LE(offset)
offset += 4
array.push(player)
--playercount
}
return callback.apply(options, [ false, array ])
}
} catch (exception) {
return callback.apply(options, [ exception ])
}
}
})
}
var decode = function(buffer) {
var charset = ''
for (var i = 0; i < 128; i++) charset += String.fromCharCode(i)
charset += '€�‚ƒ„…†‡�‰�‹�����‘’“”•–—�™�›���� ΅Ά£¤¥¦§¨©�«¬®―°±²³΄µ¶·ΈΉΊ»Ό½ΎΏΐΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡ�ΣΤΥΦΧΨΩΪΫάέήίΰαβγδεζηθικλμνξοπρςστυφχψωϊϋόύώ�'
var charsetBuffer = new Buffer(charset, 'ucs2')
var decodeBuffer = new Buffer(buffer.length * 2)
for(var i = 0; i < buffer.length; i++) {
decodeBuffer[i * 2] = charsetBuffer[buffer[i] * 2]
decodeBuffer[i * 2 + 1] = charsetBuffer[buffer[i] * 2 + 1]
}
return decodeBuffer.toString('ucs2')
}
module.exports = query