aboutsummaryrefslogtreecommitdiff
path: root/lib/MasterServer/TCP/BrowserHost.pm
diff options
context:
space:
mode:
authorDarkelarious <darkelarious@333networks.com>2017-08-22 11:00:13 +0200
committerDarkelarious <darkelarious@333networks.com>2017-08-22 11:00:13 +0200
commitc06322da38b4cb76b2036af1a5448083adb8ff20 (patch)
tree189c9f0fec3325be927f763aba23cf18aa68cfe4 /lib/MasterServer/TCP/BrowserHost.pm
parente0d727670cbeda0db0812c5c9efc503d75f8d0a4 (diff)
downloadMasterServer-Perl-2.4.0.tar.gz
MasterServer-Perl-2.4.0.zip
new server checking mechanism, complete recode of major functionsv2.4.0
Diffstat (limited to 'lib/MasterServer/TCP/BrowserHost.pm')
-rwxr-xr-xlib/MasterServer/TCP/BrowserHost.pm78
1 files changed, 54 insertions, 24 deletions
diff --git a/lib/MasterServer/TCP/BrowserHost.pm b/lib/MasterServer/TCP/BrowserHost.pm
index c53ae42..316a135 100755
--- a/lib/MasterServer/TCP/BrowserHost.pm
+++ b/lib/MasterServer/TCP/BrowserHost.pm
@@ -5,59 +5,89 @@ use warnings;
use AnyEvent::Socket;
use AnyEvent::Handle;
use Exporter 'import';
+our @EXPORT = qw| browser_host |;
-our @EXPORT = qw| browser_host clean_tcp_handle|;
+# keep handle alive and store authentication info
+my %conn = ();
################################################################################
## wait for incoming TCP connections from game clients and other masterservers.
-## respond with secure/validate, contact info and/or server lists.
+## respond with secure/validate and/or server lists.
## allow other masterservers to synchronize
################################################################################
sub browser_host {
my $self = shift;
my $browser = tcp_server undef, $self->{listen_port}, sub {
- my ($fh, $a, $p) = @_;
-
- # validated? yes = 1 no = 0
+ my ($fh, $addr, $port) = @_;
my $auth = 0;
- # debug -- new connection opened
- #$self->log("tcp","New connection from $a:$p");
-
- # prep a challenge
+ # prepare a secure/validate challenge
my $secure = $self->secure_string();
-
- # handle received data
- my $h; $h = AnyEvent::Handle->new(
+
+ # handle for client connection
+ my $client; $client = AnyEvent::Handle->new(
fh => $fh,
poll => 'r',
timeout => $self->{timeout_time},
- on_eof => sub {$self->log("tcp","eof on $a:$p" ); $self->clean_tcp_handle(@_)},
- on_error => sub {$self->error($!, "browser $a:$p"); $self->clean_tcp_handle(@_)},
- on_read => sub {$self->read_tcp_handle($h, $a, $p, $secure, @_)},
+ on_eof => sub {drop_handle($client);},
+ on_error => sub {drop_handle($client);$self->error($!, "client $addr:$port");},
+ on_read => sub {
+ # receive data
+ my $rx = $self->data2hashref($client->rbuf);$client->rbuf = "";
+
+ # Support echo: log, but don't respond (or recursive echo abuse)
+ $self->log("echo","msg $addr:$port: $rx->{echo}") if $rx->{echo};
+
+ # first check for validation info
+ if ($rx->{validate} && $rx->{gamename}) {
+ $auth = $self->auth_browser(
+ gamename => $rx->{gamename},
+ secure => $secure,
+ enctype => $rx->{enctype},
+ validate => $rx->{validate},
+ );
+ $conn{$client}[1] = $auth;
+ $self->log("secure", "client $addr:$port failed validation $rx->{gamename}") unless $auth;}
+
+ # list request with valid gamename / challenge
+ if ($auth && $rx->{gamename} && exists $rx->{list}) {
+ $client->push_write($self->generate_list($rx->{gamename}, $rx->{list})."\\final\\");
+ $self->log("list","$addr:$port retrieved the list for $rx->{gamename}");
+ drop_handle($client)}
+
+ # sync request with valid gamename / challenge
+ if ($auth && $rx->{sync}) {
+ $client->push_write($self->generate_sync($rx->{sync})."\\final\\");
+ $self->log("syncer","$addr:$port synchronized $rx->{sync}");
+ drop_handle($client)}
+
+ # request without valid gamename and/or authentication
+ if (!$auth && ($rx->{sync} || exists $rx->{list}) ) {
+ $client->push_write("\\echo\\You failed to authenticate. See 333networks.com for more info.\\final\\");
+ $self->log("warning","$addr:$port failed to authenticate before requesting a list/sync");
+ drop_handle($client);}
+ },
);
# part 1: send \basic\\secure\$key\
- $h->push_write("\\basic\\\\secure\\$secure\\final\\");
+ $client->push_write("\\basic\\\\secure\\$secure\\final\\");
- # keep handle alive longer and store authentication info
- $self->{browser_clients}->{$h} = [$h, $auth];
- return;
+ # keep handle alive and store authentication info
+ $conn{$client} = [$client, $auth];
};
# startup of TCP server complete
- $self->log("info", "Listening for TCP connections on port $self->{listen_port}.");
+ $self->log("info", "listening for TCP connections on port $self->{listen_port}");
return $browser;
}
################################################################################
## clean handles on timeouts, completed requests and/or errors
################################################################################
-sub clean_tcp_handle{
- my ($self, $c) = @_;
- # clean and close the connection
- delete ($self->{browser_clients}->{$c});
+sub drop_handle {
+ my $c = shift;
+ delete $conn{$c};
$c->destroy();
}