-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
123 lines (106 loc) · 3.87 KB
/
main.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
// Generated by CoffeeScript 1.3.3
(function() {
var EventEmitter, PACKETS, RESPONSES, SrcDS, dgram, packet,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
dgram = require("dgram");
packet = require("packet");
EventEmitter = require('events').EventEmitter;
PACKETS = {
info: new Buffer(["0xff", "0xff", "0xff", "0xff", "0x54", "0x53", "0x6f", "0x75", "0x72", "0x63", "0x65", "0x20", "0x45", "0x6e", "0x67", "0x69", "0x6e", "0x65", "0x20", "0x51", "0x75", "0x65", "0x72", "0x79", "0x00"])
};
RESPONSES = {
info: "x32,\nb8|chr() => type,\nb8 => version,\nb8z|utf8() => serverName,\nb8z|utf8() => map,\nb8z|utf8() => gameType,\nb8z|utf8() => gameName,\nl16 => appID,\nb8 => numPlayers,\nb8 => maxPlayers,\nb8 => numBots,\nb8|chr() => dedicated,\nb8|chr() => os,\nb8 => password,\nb8 => secure,\nb8z|utf8() => gameVersion"
};
SrcDS = (function(_super) {
__extends(SrcDS, _super);
function SrcDS(ip, port, options) {
var parser, _base, _ref,
_this = this;
if (options == null) {
options = {};
}
this.onMsg = __bind(this.onMsg, this);
if (this === global) {
return new SrcDS(ip, port, options);
}
_ref = [ip, port, options], this.ip = _ref[0], this.port = _ref[1], this.options = _ref[2];
this.client = dgram.createSocket('udp4');
parser = new packet.Parser();
parser._transforms.chr = function(parsing, field, value) {
if (parsing) {
return String.fromCharCode(value);
} else {
return value.charCodeAt();
}
};
parser.extract(RESPONSES.info, function(msg) {
return _this.onMsg(msg);
});
this.client.on('message', function(msg, rinfo) {
_this.ip = rinfo.address;
_this.port = rinfo.port;
return parser.write(msg);
});
(_base = this.options).timeout || (_base.timeout = 10000);
}
SrcDS.prototype.send = function(packet, cb) {
var _this = this;
if (cb == null) {
cb = function() {};
}
return this.client.send(packet, 0, packet.length, this.port, this.ip, function(err) {
var msgcb, timeout;
if (err) {
return cb(err);
} else {
timeout = null;
msgcb = function(msg) {
clearTimeout(timeout);
return cb(null, msg);
};
_this.once('message', msgcb);
return timeout = setTimeout(function() {
_this.removeListener('message', msgcb);
return cb(new Error("Request timed out"));
}, _this.options.timeout);
}
});
};
SrcDS.prototype.info = function(cb) {
return this.send(PACKETS.info, cb);
};
SrcDS.prototype.onMsg = function(msg, rinfo) {
var decoded;
decoded = msg;
decoded.ip = this.ip;
decoded.port = this.port;
switch (decoded.os) {
case "l":
decoded.os = "Linux";
break;
case "w":
decoded.os = "Windows";
}
switch (decoded.dedicated) {
case "d":
decoded.dedicated = "dedicated";
break;
case "l":
decoded.dedicated = "listen";
break;
case "p":
decoded.dedicated = "SourceTV";
}
decoded.pw = decoded.pw === 1;
decoded.secure = decoded.secure === 1;
return this.emit("message", decoded);
};
SrcDS.prototype.close = function() {
return this.client.close();
};
return SrcDS;
})(EventEmitter);
module.exports = SrcDS;
}).call(this);