/
master_server.php
195 lines (172 loc) · 6.38 KB
/
master_server.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
<?php
/*
* Doomsday Web API: Master Server
* Copyright (c) 2016 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* License: GPL v2+
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses/gpl.html
*/
/*
* The Master Server has the following responsibilities:
*
* - Receive server announcements (in JSON via HTTP POST), parse them, and store
* them in a database.
* - Answer HTTP GET queries about which servers are currently running
* (also JSON).
* - Remove expired entries from the database.
*/
require_once('api_config.inc.php'); // database config
define('DB_TABLE', 'servers');
define('EXPIRE_SECONDS', 900);
define('DEFAULT_PORT', 13209);
// Opens the database connection.
// @return MySQLi object.
function db_open()
{
return new mysqli(DENG_DB_HOST, DENG_DB_USER, DENG_DB_PASSWORD, DENG_DB_NAME);
}
function db_query(&$db, $sql)
{
$result = $db->query($sql);
if (!$result)
{
echo "Database error: " . $db->error;
echo "Query was: " . $sql;
exit;
}
return $result;
}
// Initializes the Servers database table.
function db_init()
{
$table = DB_TABLE;
$db = db_open();
db_query($db, "DROP TABLE IF EXISTS $table");
$sql = "CREATE TABLE $table ("
. "timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP "
."ON UPDATE CURRENT_TIMESTAMP, "
. "address INT UNSIGNED NOT NULL, "
. "port SMALLINT UNSIGNED NOT NULL, "
. "domain VARCHAR(100), "
. "name VARCHAR(100) NOT NULL, "
. "description VARCHAR(500), "
. "version VARCHAR(30) NOT NULL, "
. "compat INT NOT NULL, "
. "plugin VARCHAR(50), "
. "packages TEXT, "
. "game_id VARCHAR(50) NOT NULL, "
. "game_config VARCHAR(200), "
. "map VARCHAR(50), "
. "player_count SMALLINT, "
. "player_max SMALLINT NOT NULL, "
. "player_names TEXT, "
. "flags INT UNSIGNED NOT NULL, "
. "PRIMARY KEY (address, port)) CHARACTER SET utf8";
db_query($db, $sql);
$db->close();
}
function is_valid_host($ip)
{
if (($ip & 0xff000000) == 0x7f000000) return false; // No loopback.
if (($ip & 0xff) == 0xff) return false; // Broadcast address.
return true;
}
function parse_announcement($json_data)
{
$server_info = json_decode($json_data);
if ($server_info == NULL) return; // JSON parse error.
$address = ip2long($_SERVER['REMOTE_ADDR']);
if (!is_valid_host($address))
{
echo 'Remote host has an invalid address';
exit;
}
$domain = urlencode($server_info->dom);
$port = (int) $server_info->port;
$name = urlencode($server_info->name);
$description = urlencode($server_info->desc);
$version = urlencode($server_info->ver);
$compat = (int) $server_info->cver;
$plugin = urlencode($server_info->plugin);
$packages = urlencode(json_encode($server_info->pkgs));
$game_id = urlencode($server_info->game);
$game_config = urlencode($server_info->cfg);
$map = urlencode($server_info->map);
$player_count = (int) $server_info->pnum;
$player_max = (int) $server_info->pmax;
$player_names = urlencode(json_encode($server_info->plrs));
$flags = (int) $server_info->flags;
if ($port == 0) $port = DEFAULT_PORT;
$db = db_open();
$table = DB_TABLE;
db_query($db, "DELETE FROM $table WHERE address = $address AND port = $port");
db_query($db, "INSERT INTO $table (address, port, domain, name, description, version, compat, plugin, packages, game_id, game_config, map, player_count, player_max, player_names, flags) "
. "VALUES ($address, $port, '$domain', '$name', '$description', '$version', $compat, '$plugin', '$packages', '$game_id', '$game_config', '$map', $player_count, $player_max, '$player_names', $flags)");
$db->close();
}
function fetch_servers()
{
$servers = array();
$db = db_open();
$table = DB_TABLE;
// Expire old announcements.
$expire_ts = time() - EXPIRE_SECONDS;
db_query($db, "DELETE FROM $table WHERE UNIX_TIMESTAMP(timestamp) < $expire_ts");
// Get all the remaining servers.
$result = db_query($db, "SELECT * FROM $table");
while ($row = $result->fetch_assoc())
{
$sv = array(
"__obj__" => "Record",
"host" => long2ip($row['address']),
"port" => (int) $row['port'],
"dom" => urldecode($row['domain']),
"name" => urldecode($row['name']),
"desc" => urldecode($row['description']),
"ver" => urldecode($row['version']),
"cver" => (int) $row['compat'],
"plugin" => urldecode($row['plugin']),
"pkgs" => json_decode(urldecode($row['packages'])),
"game" => urldecode($row['game_id']),
"cfg" => urldecode($row['game_config']),
"map" => urldecode($row['map']),
"pnum" => (int) $row['player_count'],
"pmax" => (int) $row['player_max'],
"plrs" => json_decode(urldecode($row['player_names'])),
"flags" => (int) $row['flags']
);
$servers[] = $sv;
}
$db->close();
return $servers;
}
//---------------------------------------------------------------------------------------
if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
// GET requests are for querying information about servers.
$op = $_GET['op'];
if ($op == 'list')
{
$servers = fetch_servers();
echo json_encode($servers);
}
else if (DENG_SETUP_ENABLED && $op == 'setup')
{
echo "Initializing database...";
db_init();
}
}
else if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
// POST requests are for submitting info about running servers.
parse_announcement(file_get_contents("php://input"));
}