From e0ada80f8582cf3b28e70b8f18de10aa505159ae Mon Sep 17 00:00:00 2001 From: Darkelarious Date: Tue, 10 Feb 2015 18:22:08 +0100 Subject: Postgresql beacon receiving procedure complete and working --- lib/MasterServer/UDP/BeaconCatcher.pm | 4 +- lib/MasterServer/UDP/BeaconChecker.pm | 180 ++++++++++++++++++++++++++++++ lib/MasterServer/UDP/DatagramProcessor.pm | 169 ++++++++++++++++++++++++++++ 3 files changed, 351 insertions(+), 2 deletions(-) create mode 100755 lib/MasterServer/UDP/BeaconChecker.pm create mode 100755 lib/MasterServer/UDP/DatagramProcessor.pm (limited to 'lib/MasterServer/UDP') 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; -- cgit v1.2.3