forked from xzion/dota2-gsi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
163 lines (143 loc) · 4.33 KB
/
index.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
var express = require('express');
var bodyParser = require('body-parser');
var eventEmitter = require('events').EventEmitter;
var events = new eventEmitter();
var clients = [];
function gsi_client (ip, auth) {
this.ip = ip;
this.auth = auth;
this.gamestate = {};
}
gsi_client.prototype.__proto__ = eventEmitter.prototype;
function Check_client(req, res, next) {
// Check if this IP is already talking to us
for (var i = 0; i < clients.length; i++) {
if (clients[i].ip == req.ip) {
req.client = clients[i];
return next();
}
}
// Create a new client
clients.push(new gsi_client(req.ip, req.body.auth));
req.client = clients[clients.length - 1];
req.client.gamestate = req.body;
// Notify about the new client
events.emit('newclient', clients[clients.length - 1]);
next();
}
function Emit_all(prefix, obj, emitter) {
Object.keys(obj).forEach(function(key) {
// console.log("Emitting '"+prefix+key+"' - " + obj[key]);
emitter.emit(prefix+key, obj[key]);
});
}
function Recursive_emit(prefix, changed, body, emitter) {
Object.keys(changed).forEach(function(key) {
if (typeof(changed[key]) == 'object') {
if (body[key] != null) { // safety check
Recursive_emit(prefix+key+":", changed[key], body[key], emitter);
}
} else {
// Got a key
if (body[key] != null) {
if (typeof body[key] == 'object') {
// Edge case on added:item/ability:x where added shows true at the top level
// and doesn't contain each of the child keys
Emit_all(prefix+key+":", body[key], emitter);
} else {
// console.log("Emitting '"+prefix+key+"' - " + body[key]);
emitter.emit(prefix+key, body[key]);
}
}
}
});
}
function Process_changes(section) {
return function(req, res, next) {
if (req.body[section]) {
// console.log("Starting recursive emit for '" + section + "'");
Recursive_emit("", req.body[section], req.body, req.client);
}
next();
}
}
function Update_gamestate(req, res, next) {
req.client.gamestate = req.body;
next();
}
function New_data(req, res) {
req.client.emit('newdata', req.body);
res.end();
}
function Check_auth(tokens) {
return function(req, res, next) {
if (tokens) {
if (req.body.auth) {
if(tokens.constructor === String) {
if(check_auth_single(req.body.auth, tokens)) {
next();
} else {
console.log("Dropping message from IP: " + req.ip + ", invalid auth token");
res.end();
}
}
else if(tokens.constructor === Array) {
if(check_auth_multi(req.body.auth, tokens)) {
next();
} else {
console.log("Dropping message from IP: " + req.ip + ", invalid auth token from token list");
res.end();
}
}
} else {
// Not a valid auth, drop the message
console.log("Dropping message from IP: " + req.ip + ", no auth tokens provided");
res.end();
}
} else {
next();
}
}
}
function check_auth_single(auth, tokens) {
let keys = Object.keys(auth)
for(var i = 0; i < keys.length; i++) {
if(auth[keys[i]] == tokens){
return true
}
}
return false
}
function check_auth_multi(auth, tokens) {
let keys = Object.keys(auth)
for(var i = 0; i < keys.length; i++) {
for(var j = 0; j < tokens.length; j++) {
if(tokens[j] == auth[keys[i]]) {
return true
}
}
}
return false
}
var d2gsi = function(options) {
options = options || {};
var port = options.port || 3000;
var tokens = options.tokens || null;
var ip = options.ip || "0.0.0.0";
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.post('/',
Check_auth(tokens),
Check_client,
Update_gamestate,
Process_changes('previously'),
Process_changes('added'),
New_data);
var server = app.listen(port, ip, function() {
console.log('Dota 2 GSI listening on port ' + server.address().port);
});
this.events = events;
return this;
}
module.exports = d2gsi;