-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlistener.js
171 lines (128 loc) · 4.67 KB
/
listener.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
"use strict";
/**
* TODO:
* * Add length checking everywhere in teh program to stop invalid packets breaking the app
* * Add a funciton to send a broadcast packet to prompt discovery replies
* * Implement a way to broadcast out to >1 port because VLANs
*/
var protobuf = require("protocol-buffers"),
dgram = require("dgram"),
fs = require("fs"),
bignum = require("bignum"),
events = require("events"),
BufferAppender = require("./BufferAppender.js"),
multiSocket = require("./multisocket.js");
var seqNum = 1,
clientId = Math.ceil(Math.random() * 100000000);
var messageTypes = protobuf(fs.readFileSync("steamdiscover.proto"));
var packetMagicBytes = new Buffer([0xff, 0xff, 0xff, 0xff, 0x21, 0x4c, 0x5f, 0xa0]);
function getOptions(opts) {
opts = opts || {};
if (opts.allInterfaces === undefined) {
opts.allInterfaces = true;
}
return opts;
//opts.addresses = ["10.0.0.40"]
}
class Listener extends events {
constructor(opts) {
super();
this.opts = getOptions(opts);
this.initSocket();
}
initSocket() {
// CREATE MULTISOCKER HERE
this.server = new multiSocket({defaultPort: this.opts.port || 27036});
//this.server = dgram.createSocket("udp4");
this.server.on("message", this.receiveMessage.bind(this));
// this.server.bind(this.opts.port || 27036, this.opts.ip || "10.0.0.100", () => {
// this.server.setBroadcast(true);
setTimeout(() => {
this.emit("connected");
}, 500);
// });
}
// Given a whole packet, read the header, and do stuff!
receiveMessage(m, rinfo) {
this.emit("raw_message", {message: m, sender: rinfo });
if (!m || m.length < 20) {
this.emit("invalid_packet", {reason: "too short", message: m});
}
var offset = 0,
mlength = m.length;
var indicator = m.slice(0,8);
if (indicator.compare(packetMagicBytes) !== 0) {
this.emit("invalid_packet", {reason: "magic bytes mismatch", message: m});
return;
}
offset += 8;
var headerLength = m.readUInt32LE(offset);
offset += 4; // 32 bit
if (mlength < offset + headerLength) return;
var header = m.slice(offset, offset + headerLength);
var headerContent = messageTypes.CMsgRemoteClientBroadcastHeader.decode(header);
offset += headerLength;
if (mlength < offset + 4) return;
var bodyLength = m.readUInt32LE(offset);
offset += 4; // 32 bit
if (mlength < offset + bodyLength) return;
if (headerContent.msg_type == messageTypes.ERemoteClientBroadcastMsg.k_ERemoteClientBroadcastMsgStatus) {
var bodyData = m.slice(offset, offset + bodyLength);
var bodyParsed = this.parseBody(bodyData, bodyLength);
if (bodyParsed) {
bodyParsed["ip_address"] = rinfo.address;
this.emit("client_seen", bodyParsed);
}
}
else
{
// Useful for debugging - type 0 is a discovery packet from another client
//if (headerContent.msg_type === 0) {
// var body_data = m.slice(offset, offset + bodyLength);
// var discoveryPacket = messageTypes.CMsgRemoteClientBroadcastDiscovery.decode(body_data);
//}
}
}
// Given a buffer containing an k_ERemoteClientBroadcastMsgStatus, parse the Steam ID
parseBody(bodyBuffer, length) {
var bodyContent = messageTypes.CMsgRemoteClientBroadcastStatus.decode(bodyBuffer);
var firstUser = bodyContent.users[0];
var steamIdBuffer = (firstUser || {} ).steamid;
if (steamIdBuffer) {
firstUser.steamid = bignum.fromBuffer(steamIdBuffer, { endian: "little", size: 'auto'} ).toString();
return bodyContent;
}
}
sendBroadcast(buf) {
this.server.send(buf, 27036, '255.255.255.255');
}
broadcastDiscovery() {
console.info("sending discovery packet - client broadcast discovery");
var body = messageTypes.CMsgRemoteClientBroadcastDiscovery.encode({
seq_num: seqNum++
});
this.sendSteamMessage(0, body);
}
sendSteamMessage(messageType, body) {
var headerBuf = messageTypes.CMsgRemoteClientBroadcastHeader.encode({
client_id: Math.ceil(Math.random() * 100000000),
msg_type: messageType
});
var int32Length = 4;
var writeOffset = 0;
var totalMessageLength = 8 + int32Length + headerBuf.length + int32Length + body.length;
var messageBuf = new BufferAppender(totalMessageLength);
messageBuf.appendBuffer(packetMagicBytes);
messageBuf.appendUInt32LE(headerBuf.length);
messageBuf.appendBuffer(headerBuf);
messageBuf.appendUInt32LE(body.length);
messageBuf.appendBuffer(body);
this.sendBroadcast(messageBuf.buffer);
}
}
function create(opts) {
return new Listener(opts);
}
module.exports = {
create: create
};