forked from OpenRA/OpenRAMasterServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.php
227 lines (199 loc) · 9.77 KB
/
ping.php
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
218
219
220
221
222
223
224
225
226
227
<?php
date_default_timezone_set('UTC');
// === configuration ===
define('DEBUG', 0);
define('PORT_CHECK_TIMEOUT', 3);
ini_set('display_errors', DEBUG);
error_reporting(DEBUG ? E_ALL : 0);
header('Content-type: text/plain');
// Define strings to blacklist in server names
$blacklist_servernames = "/official/";
// Servers (IP) excluded from blacklisting
$whitelist_servers = array("104.238.136.90", "144.76.186.56");
// === functions ===
function check_port($ip, $port)
{
return @fsockopen($ip, $port, $errno, $errstr, PORT_CHECK_TIMEOUT);
}
function check_servername($servername)
{
return preg_match($blacklist_servernames, $servername);
}
function updatedbinfo($gameinfo) {
global $db;
$fields = array_keys($gameinfo);
$query = $db->prepare('INSERT OR ABORT INTO `servers` ('.implode(', ', $fields).')
VALUES (:'.implode(', :', $fields).')
');
$result = $query->execute($gameinfo);
if (!$result) {
$query = $db->prepare('UPDATE OR FAIL `servers` SET '.implode('=?, ', $fields).'=? WHERE address = :address');
$result = $query->execute(array_merge(array_values($gameinfo), array(':address' => $gameinfo['address'])));
}
if (DEBUG) $query->debugDumpParams();
if (!isset($_REQUEST['clients']) || $_REQUEST['clients'] == "")
return true;
$query = $db->prepare('DELETE FROM clients WHERE address = :addr');
$query->bindValue(':addr', $gameinfo['address'], PDO::PARAM_STR);
$query->execute();
$clients = explode(",", $_REQUEST['clients']);
foreach ($clients as $client)
{
$query = $db->prepare("INSERT INTO clients ('address','client','ts')
VALUES (:addr, :client, :ts)");
$query->bindValue(':addr', $gameinfo['address'], PDO::PARAM_STR);
$query->bindValue(':client', base64_decode($client), PDO::PARAM_STR);
$query->bindValue(':ts', time(), PDO::PARAM_INT);
$query->execute();
}
return true;
}
// === body ===
// make sure everything required is actually set.
foreach(array('port', 'name', 'state', 'map', 'mods', 'players') as $key)
if(!isset($_REQUEST[$key]))
die('field "'.$key.'" is not set');
try
{
$ip = $_SERVER['REMOTE_ADDR'];
$port = $_REQUEST['port'];
$addr = $ip.':'.$port;
// don't get spammed so easily
if ($_REQUEST['state'] == 1)
if (!check_port($ip, $port))
die('[001] game server "'.$addr.'" does not respond');
$name = urldecode($_REQUEST['name']);
$started = '';
// Check if server is using blacklisted names
if (check_servername($name))
if (!in_array($ip, $whitelist_servers))
die('[002] game server "'.$addr.'" contains blacklisted name');
$version_arr = explode('@', $_REQUEST['mods']);
$game_mod = array_shift($version_arr);
$version = implode('@', $version_arr);
$db = new PDO('sqlite:db/openra.db');
if ($_REQUEST['state'] == 2)
{
$query = $db->prepare('SELECT * FROM servers WHERE address = :addr');
$query->bindValue(':addr', $addr, PDO::PARAM_STR);
$query->execute();
$result = $query->fetchAll();
foreach ($result as $row)
{
if ($row['state'] == 1)
{
$started = date('Y-m-d H:i:s');
$insert = $db->prepare("INSERT INTO started ('game_id','name','address','map','game_mod','version','protected','started','players','spectators','bots')
VALUES (:game_id, :name, :address, :map, :game_mod, :version, :protected, :started, :players, :spectators, :bots)"
);
$insert->bindValue(':game_id', $row['id'], PDO::PARAM_INT);
$insert->bindValue(':name', htmlspecialchars($_REQUEST['name']), PDO::PARAM_STR);
$insert->bindValue(':address', $addr, PDO::PARAM_STR);
$insert->bindVAlue(':map', $_REQUEST['map'], PDO::PARAM_STR);
$insert->bindValue(':game_mod', htmlspecialchars($game_mod), PDO::PARAM_STR);
$insert->bindValue(':version', htmlspecialchars($version), PDO::PARAM_STR);
$insert->bindValue(':protected', isset($_REQUEST['protected']) ? $_REQUEST['protected'] : 0, PDO::PARAM_STR);
$insert->bindValue(':started', $started, PDO::PARAM_STR);
$insert->bindValue(':players', htmlspecialchars($_REQUEST['players']), PDO::PARAM_INT);
$insert->bindValue(':spectators', isset($_REQUEST['spectators']) ? htmlspecialchars($_REQUEST['spectators']) : 0, PDO::PARAM_INT);
$insert->bindValue(':bots', isset($_REQUEST['bots']) ? htmlspecialchars($_REQUEST['bots']) : 0, PDO::PARAM_INT);
$insert->execute();
if (DEBUG) $insert->debugDumpParams();
}
else
$started = $row['started'];
break;
}
}
if ($_REQUEST['state'] == 3)
{
$query = $db->prepare('SELECT id,started FROM servers WHERE address = :addr');
$query->bindValue(':addr', $addr, PDO::PARAM_STR);
$query->execute();
$result = $query->fetchAll();
foreach ($result as $row)
{
if ($row['started'] == '')
break;
$insert = $db->prepare("INSERT INTO finished ('game_id','name','address','map','game_mod','version','protected','started','finished')
VALUES (:game_id, :name, :address, :map, :game_mod, :version, :protected, :started, :finished)"
);
$insert->bindValue(':game_id', $row['id'], PDO::PARAM_INT);
$insert->bindValue(':name', htmlspecialchars($_REQUEST['name']), PDO::PARAM_STR);
$insert->bindValue(':address', $addr, PDO::PARAM_STR);
$insert->bindVAlue(':map', $_REQUEST['map'], PDO::PARAM_STR);
$insert->bindValue(':game_mod', $game_mod, PDO::PARAM_STR);
$insert->bindValue(':version', $version, PDO::PARAM_STR);
$insert->bindValue(':protected', isset($_REQUEST['protected']) ? $_REQUEST['protected'] : 0, PDO::PARAM_STR);
$insert->bindValue(':started', $row['started'], PDO::PARAM_STR);
$insert->bindValue(':finished', date('Y-m-d H:i:s'), PDO::PARAM_STR);
$insert->execute();
if (DEBUG) $insert->debugDumpParams();
$played_counter = 1;
$query = $db->prepare('SELECT played_counter FROM map_stats WHERE map = :map');
$query->bindValue(':map', $_REQUEST['map'], PDO::PARAM_STR);
$query->execute();
$result = $query->fetchAll();
if ($result)
{
foreach ($result as $row)
{
$played_counter = $row['played_counter'] + 1;
break;
}
$insert = $db->prepare('UPDATE map_stats SET played_counter = :played_counter, last_change = :last_change WHERE map = :map');
}
else
{
$insert = $db->prepare("INSERT INTO map_stats ('map','played_counter','last_change')
VALUES (:map, :played_counter, :last_change)"
);
}
$insert->bindValue(':map', $_REQUEST['map'], PDO::PARAM_STR);
$insert->bindValue(':played_counter', $played_counter, PDO::PARAM_INT);
$insert->bindValue(':last_change', date('Y-m-d H:i:s'), PDO::PARAM_STR);
$insert->execute();
if (DEBUG) $insert->debugDumpParams();
break;
}
$remove = $db->prepare('DELETE FROM `servers` WHERE address = :addr');
$remove->bindValue(':addr', $addr, PDO::PARAM_STR);
$remove->execute();
$db->query('DELETE FROM servers WHERE (' . time() . ' - ts > 300)');
if (isset($_REQUEST['clients']))
{
$remove = $db->prepare('DELETE FROM clients WHERE address = :addr OR (' . time() . ' - ts > 300)');
$remove->bindValue(':addr', $addr, PDO::PARAM_STR);
$remove->execute();
}
unset($db);
exit;
}
if (isset($_REQUEST['new']))
{
$games = file_get_contents("games.txt");
file_put_contents("games.txt", $games + 1);
}
updatedbinfo(
array(
'name' => htmlspecialchars($name),
'address' => $addr,
'players' => htmlspecialchars($_REQUEST['players']),
'state' => $_REQUEST['state'],
'ts' => time(),
'map' => $_REQUEST['map'],
'mods' => htmlspecialchars($_REQUEST['mods']),
'bots' => isset($_REQUEST['bots']) ? htmlspecialchars($_REQUEST['bots']) : 0,
'spectators'=> isset($_REQUEST['spectators']) ? htmlspecialchars($_REQUEST['spectators']) : 0,
'maxplayers'=> isset($_REQUEST['maxplayers']) ? htmlspecialchars($_REQUEST['maxplayers']) : 0,
'protected' => isset($_REQUEST['protected']) ? $_REQUEST['protected'] : 0,
'started' => $started,
)
);
unset($db);
}
catch (PDOException $e)
{
die($e->getMessage());
}
?>