-
Notifications
You must be signed in to change notification settings - Fork 11
/
addServerInfo.php
312 lines (273 loc) · 17 KB
/
addServerInfo.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
// Copyright (C) 2012 Mark Vejvoda, Titus Tscharntke and Tom Reynolds
// The MegaGlest Team, under GNU GPL v3.0
// ==============================================================
define( 'INCLUSION_PERMITTED', true );
require_once( 'registry.php' );
require_once( 'config.php' );
require_once( 'functions.php' );
// Consider using HTTP POST instead of HTTP GET here, data should always be sent via POST for privacy and security reasons
// Alternatively, do not retrieve (and transmit) this data at all via HTTP (other than the IP address the game servers advertises) but fetch it from the game server instead
// general info:
$glestVersion = (string) clean_str( $_GET['glestVersion'] );
$platform = (string) clean_str( $_GET['platform'] );
$binaryCompileDate = (string) clean_str( $_GET['binaryCompileDate'] );
if ( isset( $_GET['privacyPlease'] ) ) {
$privacyPlease = (int) $_GET['privacyPlease'];
}
else
{
$privacyPlease = 0;
}
// game info:
$serverTitle = (string) clean_str( $_GET['serverTitle'] );
$remote_ip = (string) clean_str( $_SERVER['REMOTE_ADDR'] );
// If the clients' IP address belongs to a RFC1918 IP range...
if ( strncmp( $remote_ip, get_localsubnet_ip_prefix(), strlen( get_localsubnet_ip_prefix() ) ) == 0 )
{
// ...then replace it by the master servers' public IP address.
$remote_ip = get_external_ip();
}
$service_port = (int) clean_str( $_GET['externalconnectport'] );
// If the game server port was not transmitted...
if ( $service_port == '' || $service_port == 0 )
{
// ..then assume the default port
$service_port = 61357;
// ... alternatively, refuse such servers
/*
header( 'Content-Type: text/plain; charset=utf-8' );
die( 'Invalid external connect port.');
*/
}
// game setup info:
$tech = (string) clean_str( $_GET['tech'] );
$map = (string) clean_str( $_GET['map'] );
$tileset = (string) clean_str( $_GET['tileset'] );
$activeSlots = (int) clean_str( $_GET['activeSlots'] );
$networkSlots = (int) clean_str( $_GET['networkSlots'] );
$connectedClients = (int) clean_str( $_GET['connectedClients'] );
$status = 0;
if(isset($_GET["gameStatus"])) {
$status = (int) clean_str( $_GET['gameStatus'] );
}
$gameCmd = "";
if(isset($_GET["gameCmd"])) {
$gameCmd = (string) clean_str( $_GET['gameCmd'] );
}
define( 'DB_LINK', db_connect() );
// consider replacing this by a cron job
cleanupServerList();
$gameUUID = "";
$whereClause = 'ip=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $remote_ip ) . '\' && externalServerPort=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $service_port ) . '\';';
if ( isset( $_GET['gameUUID'] ) ) {
$gameUUID = (string) clean_str( $_GET['gameUUID'] );
$whereClause = 'gameUUID=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $gameUUID ) . '\';';
}
// echo '#1 ' . $whereClause;
$server_in_db = @mysqli_query(Registry::$mysqliLink, 'SELECT ip, externalServerPort FROM glestserver WHERE ' . $whereClause );
$server = @mysqli_fetch_row( $server_in_db );
// Representation starts here (but it should really be starting much later, there is way too much logic behind this point)
header( 'Content-Type: text/plain; charset=utf-8' );
if ( (version_compare($glestVersion,"v3.4.0-dev","<") && $connectedClients == $networkSlots) || $gameCmd == "gameOver") // game servers' slots are all full
{
if($gameCmd == "gameOver" && $gameUUID != "")
{
// update database info on this game server; no checks are performed
mysqli_query(Registry::$mysqliLink, 'UPDATE glestserver SET ' .
'glestVersion=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $glestVersion ) . '\', ' .
'platform=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $platform ) . '\', ' .
'binaryCompileDate=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $binaryCompileDate ) . '\', ' .
'serverTitle=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $serverTitle ) . '\', ' .
'tech=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $tech ) . '\', ' .
'map=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $map ) . '\', ' .
'tileset=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $tileset ) . '\', ' .
'activeSlots=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $activeSlots ) . '\', ' .
'networkSlots=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $networkSlots ) . '\', ' .
'connectedClients=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $connectedClients ) . '\', ' .
'externalServerPort=\''. mysqli_real_escape_string(Registry::$mysqliLink, $service_port ) . '\', ' .
'status=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $status ) . '\', ' .
'lasttime=' . 'now()' . ' ' .
'WHERE ' . $whereClause);
}
else
{
// delete server; no checks are performed
mysqli_query(Registry::$mysqliLink, 'DELETE FROM glestserver WHERE ' . $whereClause );
}
echo 'OK' ;
} // game in progress
else
{
$game_host_ip = $server[0];
$game_host_port = $server[1];
if ( $gameUUID != "" ) {
$server_in_db_not_timedout = @mysqli_query(Registry::$mysqliLink, 'DELETE FROM glestserver WHERE ip=\'' .
mysqli_real_escape_string(Registry::$mysqliLink, $remote_ip ) . '\' AND externalServerPort=\'' .
mysqli_real_escape_string(Registry::$mysqliLink, $service_port ) . '\' AND gameUUID <> \'' .
mysqli_real_escape_string(Registry::$mysqliLink, $gameUUID ) . '\' AND status in (0,1,2);' );
}
if ( ($remote_ip == $game_host_ip && $service_port == $game_host_port) || $status == 2 ) // this server is contained in the database
{
if ( $remote_ip == $game_host_ip && $service_port == $game_host_port)
{
// update database info on this game server; no checks are performed
mysqli_query(Registry::$mysqliLink, 'UPDATE glestserver SET ' .
'glestVersion=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $glestVersion ) . '\', ' .
'platform=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $platform ) . '\', ' .
'binaryCompileDate=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $binaryCompileDate ) . '\', ' .
'serverTitle=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $serverTitle ) . '\', ' .
'tech=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $tech ) . '\', ' .
'map=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $map ) . '\', ' .
'tileset=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $tileset ) . '\', ' .
'activeSlots=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $activeSlots ) . '\', ' .
'networkSlots=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $networkSlots ) . '\', ' .
'connectedClients=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $connectedClients ) . '\', ' .
'externalServerPort=\''. mysqli_real_escape_string(Registry::$mysqliLink, $service_port ) . '\', ' .
'status=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $status ) . '\', ' .
'lasttime=' . 'now()' . ' ' .
'WHERE ' . $whereClause);
//updateServer($remote_ip, $service_port, $serverTitle, $connectedClients, $networkSlots);
echo 'OK';
}
else if ($status == 2)
{
if ( extension_loaded('geoip') ) {
if ( $privacyPlease == 0 )
{
$country = geoip_country_code_by_name( $remote_ip );
}
else
{
$country = '';
}
}
// cleanup old entrys with same remote port and ip
// I hope this fixes those double entrys of servers
mysqli_query(Registry::$mysqliLink, 'DELETE FROM glestserver WHERE '. $whereClause );
// insert new entry
mysqli_query(Registry::$mysqliLink, 'INSERT INTO glestserver SET ' .
'glestVersion=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $glestVersion ) . '\', ' .
'platform=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $platform ) . '\', ' .
'binaryCompileDate=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $binaryCompileDate ) . '\', ' .
'serverTitle=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $serverTitle ) . '\', ' .
'ip=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $remote_ip ) . '\', ' .
'tech=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $tech ) . '\', ' .
'map=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $map ) . '\', ' .
'tileset=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $tileset ) . '\', ' .
'activeSlots=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $activeSlots ) . '\', ' .
'networkSlots=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $networkSlots ) . '\', ' .
'connectedClients=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $connectedClients ) . '\', ' .
'externalServerPort=\''. mysqli_real_escape_string(Registry::$mysqliLink, $service_port ) . '\', ' .
'country=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $country ) . '\', ' .
'status=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $status ) . '\', ' .
'gameUUID=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $gameUUID ) . '\';'
);
echo 'OK';
}
}
else // this game server is not listed in the database, yet
{ // check whether this game server is available from the Internet; if it is, add it to the database
sleep(8); // was 3
$socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
if ( $socket < 0 ) {
echo 'socket_create() failed.' . PHP_EOL . ' Reason: ' . socket_strerror( $socket ) . PHP_EOL;
}
socket_set_nonblock( $socket )
or die( 'Unable to set nonblock on socket.' );
$timeout = 10; //timeout in seconds
echo 'Trying to connect to \'' . $remote_ip . '\' using port \'' . $service_port . '\'...' . PHP_EOL;
$canconnect = true;
$time = time();
error_reporting( E_ERROR );
for ( ; !@socket_connect( $socket, $remote_ip, $service_port ); )
{
$socket_last_error = socket_last_error( $socket );
if ( $socket_last_error == 115 || $socket_last_error == 114)
{
if ( ( time() - $time ) >= $timeout )
{
$canconnect = false;
echo 'socket_connect() failed.' . PHP_EOL . ' Reason: (' . $socket_last_error . ') ' . socket_strerror( $socket_last_error ) . PHP_EOL;
break;
}
sleep( 1 );
continue;
}
// for answers on this see: http://bobobobo.wordpress.com/2008/11/09/resolving-winsock-error-10035-wsaewouldblock/
else if($socket_last_error == 10035 || $socket_last_error == 10037) {
break;
}
$canconnect = false;
echo 'socket_connect() failed.' . PHP_EOL . ' Reason: (' . $socket_last_error . ') ' . socket_strerror( $socket_last_error ) . PHP_EOL;
break;
}
socket_set_block( $socket )
or die( 'Unable to set block on socket.' );
//echo "and now read ....";
//$buf = socket_read($socket, 161);
//echo $buf ."\n";
// Make sure its a glest server connecting
//
// struct Data{
// int8 messageType;
// NetworkString<maxVersionStringSize> versionString;
// NetworkString<maxNameSize> name;
// int16 playerIndex;
// int8 gameState;
// };
if ( $canconnect == true ) {
$data_from_server = socket_read( $socket, 1 );
}
socket_close( $socket );
error_reporting( E_ALL );
if ( $canconnect == false )
{
echo 'wrong router setup';
}
/*
else if ( $data_from_server != 1 ) // insert serious verification here
{
echo "invalid handshake!";
}
*/
else // connection to game server succeeded, protocol verification succeeded
{ // add this game server to the database
if ( extension_loaded('geoip') ) {
if ( $privacyPlease == 0 )
{
$country = geoip_country_code_by_name( $remote_ip );
}
else
{
$country = '';
}
}
// cleanup old entrys with same remote port and ip
// I hope this fixes those double entrys of servers
mysqli_query(Registry::$mysqliLink, 'DELETE FROM glestserver WHERE '. $whereClause );
// insert new entry
mysqli_query(Registry::$mysqliLink, 'INSERT INTO glestserver SET ' .
'glestVersion=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $glestVersion ) . '\', ' .
'platform=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $platform ) . '\', ' .
'binaryCompileDate=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $binaryCompileDate ) . '\', ' .
'serverTitle=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $serverTitle ) . '\', ' .
'ip=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $remote_ip ) . '\', ' .
'tech=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $tech ) . '\', ' .
'map=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $map ) . '\', ' .
'tileset=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $tileset ) . '\', ' .
'activeSlots=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $activeSlots ) . '\', ' .
'networkSlots=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $networkSlots ) . '\', ' .
'connectedClients=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $connectedClients ) . '\', ' .
'externalServerPort=\''. mysqli_real_escape_string(Registry::$mysqliLink, $service_port ) . '\', ' .
'country=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $country ) . '\', ' .
'status=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $status ) . '\', ' .
'gameUUID=\'' . mysqli_real_escape_string(Registry::$mysqliLink, $gameUUID ) . '\';'
);
echo 'OK';
//addLatestServer($remote_ip, $service_port, $serverTitle, $connectedClients, $networkSlots);
}
}
}
db_disconnect( Registry::$mysqliLink );
?>