forked from seishun/node-steam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.js
54 lines (42 loc) · 1.14 KB
/
connection.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
module.exports = Connection;
var util = require('util');
var Socket = require('net').Socket;
var MAGIC = 'VT01';
util.inherits(Connection, Socket);
function Connection() {
Socket.call(this);
this.on('readable', this._readPacket.bind(this));
}
Connection.prototype.send = function(data) {
// encrypt
if (this.sessionKey) {
data = require('steam-crypto').symmetricEncrypt(data, this.sessionKey);
}
var buffer = new Buffer(4 + 4 + data.length);
buffer.writeUInt32LE(data.length, 0);
buffer.write(MAGIC, 4);
data.copy(buffer, 8);
this.write(buffer);
};
Connection.prototype._readPacket = function() {
if (!this._packetLen) {
var header = this.read(8);
if (!header) {
return;
}
this._packetLen = header.readUInt32LE(0);
}
var packet = this.read(this._packetLen);
if (!packet) {
this.emit('debug', 'incomplete packet');
return;
}
delete this._packetLen;
// decrypt
if (this.sessionKey) {
packet = require('steam-crypto').symmetricDecrypt(packet, this.sessionKey);
}
this.emit('packet', packet);
// keep reading until there's nothing left
this._readPacket();
};