This commit is contained in:
XiCoN-FJS- 2019-10-19 23:25:42 +02:00
parent 6df0e11148
commit c22d64fb50

41
xtr.pl
View File

@ -10,6 +10,8 @@ use Socket;
use DBI;
use File::Fetch;
use PerlIO::gzip;
use LWP::UserAgent;
use HTTP::Request;
use Data::Dumper;
### README
@ -19,6 +21,16 @@ use Data::Dumper;
### vars
my $dbfile = "ip2asn.db";
my $ip2asn_csv_url = 'http://iptoasn.com/data/ip2asn-v4-u32.tsv.gz';
my $master_server = "xtr-master.xicon.eu";
my $my_ip = get_my_ipv4();
my $my_ext_ip = ""; # set different ip, if access differs from default public ip
my $my_port = 3000;
my $my_ext_port = 0; # set different port, if public access port differs from app port
### set vars
set port => $my_port;
if($my_ext_ip eq "") { $my_ext_ip = $my_ip; }
if($my_ext_port eq 0 || $my_ext_port eq "") { $my_ext_port = $my_port; }
### connect to database
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","");
@ -39,12 +51,41 @@ get '/:ip' => sub {
return encode_json $trace;
};
### give the master server the info, that we are available
if(send_server_status($master_server,$my_ext_ip,$my_ext_port,"1"))
{
print "send the master server ".$master_server." the info, that we are online.\n";
}
else
{
print "Failed to send our status to the master server ".$master_server." - nobody knows we are online :(\n";
}
### catch "INT" signal to send the status to the master server
$SIG{'INT'} = sub { send_server_status($master_server,$my_ext_ip,$my_ext_port,"0"); exit; };
### start the loop
start;
### if you reach this, you're done
exit;
sub send_server_status
{
my ($master_server,$hostname,$port,$status) = @_;
my $url = "http://".$master_server."/v2/server/".$hostname."/".$port."/".$status;
my $ua = LWP::UserAgent->new(timeout => 10);
my $req = HTTP::Request->new("PUT", $url);
my $response = $ua->request($req);
return $response->is_success;
}
sub get_my_ipv4
{
my $ua = LWP::UserAgent->new(timeout => 10);
my $response = $ua->get("http://ipv4.xicon.eu/");
return $response->decoded_content;
}
sub traceit
{