diff options
| -rwxr-xr-x | data/database/tables-SQLite.sql | 71 | ||||
| -rwxr-xr-x | data/masterserver-config.pl | 8 | ||||
| -rwxr-xr-x | lib/MasterServer/Core/Core.pm | 7 | ||||
| -rwxr-xr-x | lib/MasterServer/Core/Logging.pm | 5 | ||||
| -rwxr-xr-x | lib/MasterServer/Database/Pg/dbBeacon.pm | 66 | ||||
| -rwxr-xr-x | lib/MasterServer/Database/Pg/dbCore.pm | 2 | ||||
| -rwxr-xr-x | lib/MasterServer/Database/Pg/dbServerlist.pm | 67 | ||||
| -rwxr-xr-x | lib/MasterServer/Database/SQLite/dbCore.pm | 64 | ||||
| -rwxr-xr-x | lib/MasterServer/UDP/BeaconCatcher.pm | 4 | ||||
| -rwxr-xr-x | lib/MasterServer/UDP/BeaconChecker.pm | 180 | ||||
| -rwxr-xr-x | lib/MasterServer/UDP/DatagramProcessor.pm | 169 |
11 files changed, 608 insertions, 35 deletions
diff --git a/data/database/tables-SQLite.sql b/data/database/tables-SQLite.sql new file mode 100755 index 0000000..8ed9471 --- /dev/null +++ b/data/database/tables-SQLite.sql @@ -0,0 +1,71 @@ +CREATE TABLE serverlist( + id INTEGER PRIMARY KEY AUTOINCREMENT, + ip VARCHAR(15) NOT NULL DEFAULT '0.0.0.0', + port INTEGER NOT NULL DEFAULT 0, + gamename VARCHAR(100) NOT NULL DEFAULT ' ', + gamever VARCHAR(50) NOT NULL DEFAULT ' ', + hostname VARCHAR(100) NOT NULL DEFAULT ' ', + hostport INTEGER NOT NULL DEFAULT 0, + country VARCHAR(5), + b333ms BOOLEAN NOT NULL DEFAULT 0, + blacklisted BOOLEAN NOT NULL DEFAULT 0, + added timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP, + beacon timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE pending( + id INTEGER PRIMARY KEY AUTOINCREMENT, + ip VARCHAR(15) NOT NULL DEFAULT '0.0.0.0', + beaconport INTEGER NOT NULL DEFAULT 0, + heartbeat INTEGER NOT NULL DEFAULT 0, + gamename VARCHAR(25) NOT NULL DEFAULT ' ', + secure VARCHAR(12) NOT NULL DEFAULT ' ', + enctype INTEGER NOT NULL DEFAULT 0, + added timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE server_info( + FOREIGN KEY(server_id) REFERENCES serverlist(id), + minnetver INTEGER NOT NULL DEFAULT 400, + gamever INTEGER NOT NULL DEFAULT 400, + location INTEGER NOT NULL DEFAULT 0, + listenserver BOOLEAN NOT NULL DEFAULT 1, + hostport INTEGER NOT NULL DEFAULT 7777, + hostname varchar(200) NOT NULL DEFAULT '', + adminname varchar(200) NOT NULL DEFAULT '', + adminemail varchar(300) NOT NULL DEFAULT '', + password BOOLEAN NOT NULL DEFAULT 0, + gametype varchar(50) NOT NULL DEFAULT '', + gamestyle varchar(50) NOT NULL DEFAULT 'Normal', + changelevels BOOLEAN NOT NULL DEFAULT 0, + maptitle varchar(100) NOT NULL DEFAULT 'Unknown', + mapname varchar(100) NOT NULL DEFAULT '', + numplayers INTEGER NOT NULL DEFAULT 0, + maxplayers INTEGER NOT NULL DEFAULT 0, + minplayers INTEGER NOT NULL DEFAULT 0, + botskill varchar(30) NOT NULL DEFAULT 'Novice', + balanceteams BOOLEAN NOT NULL DEFAULT 0, + playersbalanceteams BOOLEAN NOT NULL DEFAULT 0, + friendlyfire varchar(10) NOT NULL DEFAULT '0%', + maxteams INTEGER NOT NULL DEFAULT 4, + timelimit INTEGER NOT NULL DEFAULT 0, + goalteamscore INTEGER NOT NULL DEFAULT 0, + fraglimit INTEGER NOT NULL DEFAULT 0, + mutators TEXT NOT NULL DEFAULT 'None' +); + +CREATE TABLE player_info( + server_id INTEGER PRIMARY KEY AUTOINCREMENT, + player varchar(40) NOT NULL DEFAULT 'Player', + team INTEGER NOT NULL DEFAULT 255, + frags INTEGER NOT NULL DEFAULT 0, + mesh varchar(100) NOT NULL DEFAULT '', + skin varchar(100) NOT NULL DEFAULT '', + face varchar(100) NOT NULL DEFAULT '', + ping INTEGER NOT NULL DEFAULT 0, + ngsecret varchar(10) NOT NULL DEFAULT 'false', + updated timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP +); + + diff --git a/data/masterserver-config.pl b/data/masterserver-config.pl index f6e0ad7..d9f29c9 100755 --- a/data/masterserver-config.pl +++ b/data/masterserver-config.pl @@ -54,7 +54,7 @@ our %S = ( printlog => 1, # which messages do you NOT want to see in the logs (and screen)? - suppress => " module debug ", + suppress => "debug_spam load hostname udp update add remove", ################################################################################ # Network settings # @@ -81,8 +81,8 @@ our %S = ( # Disable checks, all games pass as validated. (0=validate, 1=allow all) debug_validate => 0, - # accept only servers that pass the secure/validate challenge - require_secure_beacons => 0, + # accept only servers that pass the secure/validate challenge, takes longer + require_secure_beacons => 1, # ignore keys from games that use multiple keys or do not support keys at all ignore_beacon_key => "deusex ut", @@ -109,7 +109,7 @@ our %S = ( # "info". Execute at least twice per hour, to avoid time-outs in own data. # disabling may break support for certain games. beacon_checker_enabled => 1, - beacon_checker_time => [80, 0.25, 1800], + beacon_checker_time => [80, 0.5, 1800], # Collect server information for the 333networks main site. Identical # mechanism as the Beacon Checker. Disable when not interested in UT info. diff --git a/lib/MasterServer/Core/Core.pm b/lib/MasterServer/Core/Core.pm index d49e36b..02ec32a 100755 --- a/lib/MasterServer/Core/Core.pm +++ b/lib/MasterServer/Core/Core.pm @@ -65,13 +65,13 @@ sub main { if ( "Pg SQLite" =~ m/$db_type[1]/i) { # inform us what DB we try to load - $self->log("loader","Loading $db_type[1] database module."); + $self->log("load","Loading $db_type[1] database module."); # load dbd and tables/queries for this db type MasterServer::load_recursive("MasterServer::Database::$db_type[1]"); # Connect to database - $self->{dbh} = $self->database_login(); #FIXME!!!! + $self->{dbh} = $self->database_login(); } else { # raise error and halt @@ -84,6 +84,9 @@ sub main { # start the listening service (listen for UDP beacons) $self->{scope}->{beacon_catcher} = $self->beacon_catcher(); + # start the beacon checker service (query entries from the pending list) + $self->{scope}->{beacon_checker} = $self->beacon_checker() if ($self->{beacon_checker_enabled}); + $self->log("info", "All modules loaded. Starting..."); diff --git a/lib/MasterServer/Core/Logging.pm b/lib/MasterServer/Core/Logging.pm index efd2123..312a0f8 100755 --- a/lib/MasterServer/Core/Logging.pm +++ b/lib/MasterServer/Core/Logging.pm @@ -25,10 +25,7 @@ sub log { my $daily = strftime('-%Y-%m-%d',localtime); # is the message suppressed in config? - if (defined $type && $self->{suppress} =~ m/$type/i){ - print "[$time] [SUPPRESSED] [$type] $msg\n"; #FIXME - return; # return if <$> - } + return if (defined $type && $self->{suppress} =~ m/$type/i); # determine filename my $f = "MasterServer-333networks"; diff --git a/lib/MasterServer/Database/Pg/dbBeacon.pm b/lib/MasterServer/Database/Pg/dbBeacon.pm index a234b25..e396810 100755 --- a/lib/MasterServer/Database/Pg/dbBeacon.pm +++ b/lib/MasterServer/Database/Pg/dbBeacon.pm @@ -5,7 +5,12 @@ use strict; use warnings; use Exporter 'import'; -our @EXPORT = qw| add_beacon get_pending_beacon remove_pending set_direct_beacon |; +our @EXPORT = qw| add_beacon + remove_pending + set_direct_beacon + get_pending_beacon + get_pending_info + get_next_pending |; ## Update beacon in serverlist or pending list. Add if beacon does not exist in ## either list. Return 0,1,2 if success in adding or -1 on error @@ -24,7 +29,7 @@ sub add_beacon { undef, lc $gamename, $ip, $heartbeat); # notify - $self->log("updated", "beacon heartbeat for $ip:$heartbeat") if ($u > 0); + $self->log("update", "beacon heartbeat for $ip:$heartbeat") if ($u > 0); # if serverlist was updated return 0 return 0 if ($u > 0); @@ -41,7 +46,7 @@ sub add_beacon { undef, $beaconport, lc $gamename, $secure, $ip, $heartbeat); # notify - $self->log("updated", "beacon heartbeat $ip:$beaconport pending $gamename:$heartbeat") if ($u > 0); + $self->log("update", "beacon heartbeat $ip:$beaconport pending $gamename:$heartbeat") if ($u > 0); # beacon was already in pending list and was updated return 1 if ($u > 0); @@ -53,7 +58,7 @@ sub add_beacon { undef, $ip, $beaconport, $heartbeat, lc $gamename, $secure); # notify - $self->log("added", "beacon heartbeat $ip:$beaconport pending $gamename:$heartbeat") if ($u > 0); + $self->log("add", "beacon heartbeat $ip:$beaconport pending $gamename:$heartbeat") if ($u > 0); # it was added to pending return 2 if ($u > 0); @@ -63,20 +68,6 @@ sub add_beacon { return -1; } - -## Get pending server by ip, beacon port. -sub get_pending_beacon { - my ($self, $ip, $port) = @_; - - # if address is in list, update the timestamp - return $self->{dbh}->selectall_arrayref( - "SELECT * FROM pending - WHERE ip = ? - AND beaconport = ?", - undef, $ip, $port)->[0]; -} - - ## server checks out, remove entry from the pending list. sub remove_pending { my ($self, $id) = @_; @@ -108,7 +99,7 @@ sub set_direct_beacon { undef, $ip, $port); # notify - $self->log("updated", "$ip:$port is a direct beacon.") if ($u > 0); + $self->log("update", "$ip:$port is a direct beacon.") if ($u > 0); # if found, updated; done return 0 if ($u > 0); @@ -120,4 +111,41 @@ sub set_direct_beacon { +## Get pending server by ip, beacon port. +sub get_pending_beacon { + my ($self, $ip, $port) = @_; + + # if address is in list, update the timestamp + return $self->{dbh}->selectall_arrayref( + "SELECT * FROM pending + WHERE ip = ? + AND beaconport = ?", + undef, $ip, $port)->[0]; +} + +## Same as get_pending_beacon, but with heartbeat port as identifier +sub get_pending_info { + my ($self, $ip, $port) = @_; + + # if address is in list, update the timestamp + return $self->{dbh}->selectall_arrayref( + "SELECT * FROM pending + WHERE ip = ? + AND heartbeat = ?", + undef, $ip, $port)->[0]; +} + +## Get server info from any entry with an id higher than the provided one. +sub get_next_pending { + my ($self, $id) = @_; + + # get 1 pending id that is older than 15s + return $self->{dbh}->selectall_arrayref( + "SELECT id, ip, heartbeat, secure FROM pending + WHERE added < (NOW() - INTERVAL '15 SECONDS') + AND id > ? + ORDER BY id ASC LIMIT 1", undef, $id)->[0]; +} + + 1; diff --git a/lib/MasterServer/Database/Pg/dbCore.pm b/lib/MasterServer/Database/Pg/dbCore.pm index c6e3182..f89cc68 100755 --- a/lib/MasterServer/Database/Pg/dbCore.pm +++ b/lib/MasterServer/Database/Pg/dbCore.pm @@ -21,7 +21,7 @@ sub database_login { # verify that the database connected if (defined $dbh) { # log the event - $self->log("database","Connected to the Postgres database."); + $self->log("load","Connected to the Postgres database."); # turn on error printing $dbh->{printerror} = 1; diff --git a/lib/MasterServer/Database/Pg/dbServerlist.pm b/lib/MasterServer/Database/Pg/dbServerlist.pm index 8d1a2b2..b1a787e 100755 --- a/lib/MasterServer/Database/Pg/dbServerlist.pm +++ b/lib/MasterServer/Database/Pg/dbServerlist.pm @@ -5,7 +5,9 @@ use strict; use warnings; use Exporter 'import'; -our @EXPORT = qw| add_to_serverlist |; +our @EXPORT = qw| add_to_serverlist + update_serverlist + get_next_server |; ## beacon was verified or otherwise accepted and will noe now be added to the ## serverlist. @@ -20,7 +22,7 @@ sub add_to_serverlist { undef, $ip, $port); # notify - $self->log("updated", "$ip:$port timestamp updated") if ($u > 0); + $self->log("update", "$ip:$port timestamp updated") if ($u > 0); # if found, updated; done return 0 if ($u > 0); @@ -31,7 +33,7 @@ sub add_to_serverlist { undef, $ip, $port, $gamename, $self->ip2country($ip)); # notify - $self->log("added", "$ip:$port added to serverlist") if ($u > 0); + $self->log("add", "$ip:$port added to serverlist") if ($u > 0); # return added return 1 if ($u > 0); @@ -41,4 +43,63 @@ sub add_to_serverlist { return -1; } +################################################################################ +## +## Subroutine update_serverlist +## +## Same as add_to_serverlist (above), but does not add the server to serverlist +## if it does not exist in serverlist. +## +## Args: ip, port %info +################################################################################ +sub update_serverlist { + my ($self, $ip, $port, $s) = @_; + + # update server info + my $u = $self->{dbh}->do( + 'UPDATE serverlist + SET updated = NOW(), + gamename = ?, + gamever = ?, + hostname = ?, + hostport = ? + WHERE ip = ? + AND port = ?', undef, + $s->{gamename}, $s->{gamever}, $s->{hostname}, $s->{hostport}, + $ip, $port); + + # notify + $self->log("update", "server $ip:$port was updated: $s->{hostname}") if ($u > 0); + + # return 0 if updated + return 0 if ($u > 0); + + # or else report error + $self->log("error", "an error occurred updating server $ip:$port in the serverlist"); + return -1; +} + +################################################################################ +## +## Subroutine get_next_server +## +## Get a server address of the next server in line to be +## queried for game info. Query must be older than 30 seconds (in case it just +## got added) and not older than 3 hours. +## +## Args: $id --> id of a server address entry +## Returns: hash {id, ip, port} of the NEXT entry in line. +################################################################################ +sub get_next_server { + my ($self, $id) = @_; + + return $self->{dbh}->selectall_arrayref( + "SELECT id, ip, port FROM serverlist + WHERE added < (NOW() - INTERVAL '15 SECONDS') + AND updated > (NOW() - INTERVAL '3 HOUR') + AND id > ? + AND NOT blacklisted + ORDER BY id ASC LIMIT 1", undef, $id)->[0]; +} + 1; diff --git a/lib/MasterServer/Database/SQLite/dbCore.pm b/lib/MasterServer/Database/SQLite/dbCore.pm new file mode 100755 index 0000000..a75f7a0 --- /dev/null +++ b/lib/MasterServer/Database/SQLite/dbCore.pm @@ -0,0 +1,64 @@ + +package MasterServer::Database::SQLite::dbCore; + +use strict; +use warnings; +use Exporter 'import'; + +our @EXPORT = qw| database_login |; + +################################################################################ +## database_login +## login to the database with credentials provided in the config file. +## returns dbh object +################################################################################ +sub database_login { + my $self = shift; + + # check if database file exists + my $db_file = [split(':', $self->{dblogin}->[0])]->[2]; + $db_file =~ s/dbname=//i; + + unless (-e $db_file) { + # fatal error + $self->log("fatal", "Database file $db_file does not exist!"); + + # end program + $self->halt(); + } + + # create the dbi object + my $dbh = DBI->connect(@{$self->{dblogin}}, {PrintError => 0}); + + # verify that the database connected + if (defined $dbh) { + # log the event + $self->log("database","Connected to the SQLite database."); + + # turn on error printing + $dbh->{printerror} = 1; + + # synchronous read/writing to the sql file OFF. That means: when the script + # shuts down unexpectedly, i.e. because of power failure or a crash, changes + # to the database are NOT SAVED. However, if this setting is not turned OFF, + # it takes too long to write to the database, which means that new beacons, + # requests and servers cannot be processed. You don't have a choice, really.. + $dbh->do("PRAGMA synchronous = OFF"); + + # return the dbi object for further use + return $dbh; + } + else { + # fatal error + $self->log("fatal", "$DBI::errstr!"); + + # end program + $self->halt(); + } + + # unreachable + return undef; + +} + +1; diff --git a/lib/MasterServer/UDP/BeaconCatcher.pm b/lib/MasterServer/UDP/BeaconCatcher.pm index 7fdc630..b3d2c18 100755 --- a/lib/MasterServer/UDP/BeaconCatcher.pm +++ b/lib/MasterServer/UDP/BeaconCatcher.pm @@ -16,7 +16,7 @@ sub beacon_catcher { my $self = shift; # module startup log - $self->log("loader","Loading UDP Beacon Catcher."); + $self->log("load","Loading UDP Beacon Catcher."); # UDP server my $udp_server; @@ -30,7 +30,7 @@ sub beacon_catcher { ); # display that the server is up and listening for beacons - $self->log("ok", "Listening for UT Beacons on port $self->{beacon_port}."); + $self->log("info", "Listening for UT Beacons on port $self->{beacon_port}."); # allow object to exist beyond this scope. Objects have ambitions too. return $udp_server; diff --git a/lib/MasterServer/UDP/BeaconChecker.pm b/lib/MasterServer/UDP/BeaconChecker.pm new file mode 100755 index 0000000..5fc2b38 --- /dev/null +++ b/lib/MasterServer/UDP/BeaconChecker.pm @@ -0,0 +1,180 @@ + +package MasterServer::UDP::BeaconChecker; + +use strict; +use warnings; +use Encode; +use AnyEvent::Handle::UDP; +use Exporter 'import'; + +our @EXPORT = qw| beacon_checker query_udp_server|; + +################################################################################ +## +## Beacon Checker Module +## +## When addresses are stored in the 'pending' list, they are supposed to be +## queried immediately with the secure/validate challenge to testify that +## the server is genuine and alive. +## +## Some servers do not support the secure-challenge on the Uplink port. These +## servers are verified with a secure-challenge on their heartbeat ports, +## which are designed to respond to secure queries, as well as status queries. +## +## Querying pending servers should only happen when beacons are already +## several seconds old. We do not want to interfere with the existing +## construction where servers respond immediately. +## +## Addresses collected by other scripts, whether from the UCC applet or manual +## input via the website, are added to the pending query table. It is more +## important to verify pending beacons and new server addresses, than to +## update the status of existing addresses. Therefore, pending addresses are +## prioritized. +## +################################################################################ +sub beacon_checker { + my $self = shift; + $self->log("load", "UDP Beacon Checker is loaded."); + + # queue -- which address is next in line? + my %q = ( pending_id => 0, + server_id => 0, + start_time => time+$self->{beacon_checker_time}[0]-1, #time+grace + ); + + # go through all servers one by one, new and old + my $server_info = AnyEvent->timer ( + after => $self->{beacon_checker_time}[0], + interval => $self->{beacon_checker_time}[1], + cb => sub { + + # first of all, check whether we exceeded our time cap limit + if ( (time - $q{start_time}) >= $self->{beacon_checker_time}[2] ){ + # reset queue variables + $q{pending_id} = 0; + $q{server_id} = 0; + $q{start_time} = time; + } + + # id, ip, port reference of the server to be queried + my $n; + + # + # Pending Servers + # + # See if there are pending servers, and use existing secure string for + # the challenge. + $n = $self->get_next_pending($q{pending_id}); + + # if any entries were found, proceed + if ( $n->[0] ) { + + # next pending id will be > $n + $q{pending_id} = $n->[0]; + + # query the server + $self->query_udp_server($n->[1], $n->[2], $n->[3]); + + # work done. Wait for the next round for the next task. + return; + } + + # if no pending servers left, update the other entries + $n = $self->get_next_server($q{server_id}); + + # if any entries were found, proceed + if ( $n->[0] ) { + + # next server id will be > $n + $q{server_id} = $n->[0]; + + # query the server + $self->query_udp_server($n->[1], $n->[2], ""); + + # work done. Wait for the next round for the next task. + return; + } + + # At this point, we are out of server entries. When new servers are + # added, they are immediately queried on the next round. + # From here on, just count down until the cycle is complete. + + # DEBUG (spams badly) + $self->log("debug_spam", "Checker timer: t=".(time - $q{start_time})); + } + ); + + $self->log("info", "Verifying servers every $self->{beacon_checker_time}[2] seconds."); + + # return the timer to keep it alive outside of scope + return $server_info; +} + + +################################################################################ +## +## UDP Server Query function +## +## Get the server status from any server over UDP and store the +## received information in the database. +## +## $secure determines the type of query: secure/pending or information +## +## Args: $ip, $port, payload +## Function is called by AnyEvent->timer() above +################################################################################ +sub query_udp_server { + my ($self, $ip, $port, $secure) = @_; + my $buf = ""; + + # debug spamming + $self->log("debug_spam", "Query server $ip:$port"); + + # connect with UDP server + my $udp_client; $udp_client = AnyEvent::Handle::UDP->new( + # Bind to this host and port + connect => [$ip, $port], + timeout => 1, + on_timeout => sub {$udp_client->destroy();}, # don't bother reporting timeouts + on_error => sub {$udp_client->destroy();}, # or errors + on_recv => sub { + + # add packet to buffer + $buf .= $_[0]; + + # if validate, assume that we sent a \basic\secure request. + if ($buf =~ m/\\validate\\/){ + $self->process_udp_validate($buf, $ip, undef, $port); + } + # if gamename, ver, hostname and hostport are available, it should have been \basic\info + elsif ($buf =~ m/\\gamename\\/ && $buf =~ m/\\gamever\\/ + && $buf =~ m/\\hostname\\/ && $buf =~ m/\\hostport\\/) { + $self->process_query_response($buf, $ip, $port); + } + # else partial information received. wait for more. + else{ } + }, + ); + + # + # Send secure message or status, depending on provided variables + # + + # secure servers enabled and secure key provided + if ($secure ne "" && $self->{require_secure_beacons} > 0) { + # send secure + $udp_client->push_send("\\basic\\\\secure\\$secure"); + + # and log that we sent it + $self->log("udp", "sending secure=\"$secure\" to $ip:$port"); + } + else { + # send information request + $udp_client->push_send("\\basic\\\\info\\"); + + # and log that we sent it + $self->log("udp","sending basic request to $ip:$port"); + } +} + +1; diff --git a/lib/MasterServer/UDP/DatagramProcessor.pm b/lib/MasterServer/UDP/DatagramProcessor.pm new file mode 100755 index 0000000..53f5228 --- /dev/null +++ b/lib/MasterServer/UDP/DatagramProcessor.pm @@ -0,0 +1,169 @@ + +package MasterServer::UDP::DatagramProcessor; + +use strict; +use warnings; +use Encode; +use AnyEvent::Handle::UDP; +use Exporter 'import'; + +our @EXPORT = qw| process_udp_beacon process_udp_validate process_query_response |; + + +## process beacons that have a \heartbeat\ and \gamename\ format +sub process_udp_beacon { + # $self, handle, packed address, udp data, peer ip address, $port + my ($self, $udp, $pa, $buf, $peer_addr, $port) = @_; + + # received heartbeat in $buf: \heartbeat\7778\gamename\ut\ + my %r; + $buf = encode('UTF-8', $buf); + $buf =~ s/\\([^\\]+)\\([^\\]+)/$r{$1}=$2/eg; + + # check whether the beacon has a gamename that is supported in our list + if (defined $r{gamename} && exists $self->{game}->{lc $r{gamename}}) { + # log the beacon + $self->log("beacon", "$peer_addr:$r{heartbeat} for $r{gamename}"); + + # some games (like bcommander) have a default port and don't send a + # heartbeat port. + if ($r{heartbeat} == 0) { + # assuming a default port exists + if (exists $self->{game}->{lc $r{gamename}}->{port}) { + $r{heartbeat} = $self->{game}->{lc $r{gamename}}->{port}; + } + } + + # + # verify valid server address (ip+port) + if ($self->valid_address($peer_addr,$r{heartbeat})) { + + # generate a new secure string + my $secure = $self->secure_string(); + + # update beacon in serverlist if it already exists, otherwise update + # or add to pending with new secure string. + my $auth = $self->add_beacon($peer_addr, $port, $r{heartbeat}, $r{gamename}, $secure); + + # send secure string back + if ($auth > 0) { + + # verify that this is a legitimate client by sending the "secure" query + $udp->push_send("\\secure\\$secure\\final\\", $pa); + + # log this as a new beacon + $self->log("secure", "challenged new beacon $peer_addr:$port with $secure."); + } + } + + # invalid ip+port combination, like \heartbeat\0\ or local IP + else { + # Log that beacon had incorrect information, such as port 0 or so. Spams log! + $self->log("invalid","$peer_addr:$r{heartbeat} ($r{heartbeat}) had bad information"); + } + } + + # gamename not valid or not found in supportedgames.pl + else { + # log + $self->log("support", "received unknown beacon \"$r{gamename}\" from $peer_addr:$r{heartbeat}"); + } +} + + +## process the received validate query and determine whether the server is allowed in our database +sub process_udp_validate { + # $self, udp data, ip, port + my ($self, $buf, $peer_addr, $port, $heartbeat) = @_; + + # received heartbeat in $b: \validate\string\queryid\99.9\ + my %r; + $buf = encode('UTF-8', $buf); + $buf =~ s/\\([^\\]+)\\([^\\]+)/$r{$1}=$2/eg; + + # get our existing knowledge about this server from the database + # if the heartbeat/queryport known? then use that instead as beacon ports --> may vary after server restarts! + my $pending = (defined $heartbeat) ? $self->get_pending_info($peer_addr, $heartbeat) : $self->get_pending_beacon($peer_addr, $port); + + # if indeed in the pending list, check -- if this entry is not (longer) in the list, it + # was either removed by the BeaconChecker or cleaned out in maintenance (after X hours). + if (defined $pending) { + + #determine if it uses any enctype + my $enc = (defined $r{enctype}) ? $r{enctype} : 0; + + # database may not contain the correct gamename (ucc applet, incomplete beacon, change of gameserver) + $pending->[4] = (defined $r{gamename} && exists $self->{game}->{lc $r{gamename}}) ? $r{gamename} : $pending->[4]; + + # verify challenge gamename secure enctype validate_response + my $val = $self->validated_beacon($pending->[4], $pending->[5], $enc, $r{validate}); + + # log challenge results (compensate if $port was not provided) + $port = (defined $port) ? $port : $heartbeat; + $self->log("secure", "$peer_addr:$port validated with $val for $pending->[4]"); + + # if validated, add to db + if ($val > 0) { + + # successfully added? ip, query port, gamename + my $sa = $self->add_to_serverlist($pending->[1], $pending->[3], $pending->[4]); + + # remove the entry from pending if successfully added + $self->remove_pending($pending->[0]) if ( $sa >= 0); + + # and set as direct beacon + $self->set_direct_beacon($pending->[1], $pending->[3]); + } + else { + # else failed validation + $self->log("error","beacon $peer_addr:$port failed validation for $pending->[4] (details: $pending->[5] sent, got $r{validate})"); + } + } +} + +## process query data that was obtained with \basic\ and/or \info\ +sub process_query_response { + # $self, udp data, ip, port + my ($self, $buf, $ip, $port) = @_; + + #process datastream + my %s; + $buf = encode('UTF-8', $buf); + $buf =~ s/\\([^\\]+)\\([^\\]+)/$s{$1}=$2/eg; + + # check whether the gamename is supported in our db + if (defined $s{gamename} && exists $self->{game}->{lc $s{gamename}}) { + + # parse variables + my %nfo = (); + + $nfo{gamename} = lc $s{gamename}; + $nfo{gamever} = exists $s{gamever} ? $s{gamever} : ""; + $nfo{hostname} = exists $s{hostname} ? $s{hostname} : "$ip:$port"; + $nfo{hostport} = exists $s{hostport} ? $s{hostport} : 0; + + # some mor0ns have values longer than 100 characters + $nfo{hostname} = substr $nfo{hostname}, 0, 99 if (length $nfo{hostname} >= 99); + + # log results + $self->log("hostname", "$ip:$port\t is now known as\t $nfo{hostname}"); + + # if only validated servers are allowed in the list + if ($self->{require_secure_beacons} > 0) { + # only update in database + $self->update_serverlist($ip, $port, \%nfo); + } + # otherwise also add the server to serverlist if required + else{ + # add to serverlist and update anyway + $self->add_to_serverlist($ip, $port, $nfo{gamename}); + $self->update_serverlist($ip, $port, \%nfo); + + # if address is in pending list, remove it + my $pending = $self->get_pending_info($ip, $port); + $self->remove_pending($pending->[0]) if $pending; + } + } +} + +1; |
