This commit is contained in:
XiCoN-FJS- 2019-10-19 23:29:16 +02:00
parent bff7d4e7b4
commit 22d6609a55

110
xtrd.pl
View File

@ -7,16 +7,21 @@ use Socket;
use DBI;
use File::Fetch;
use PerlIO::gzip;
use LWP::UserAgent;
use HTTP::Request;
use Data::Validate::IP qw(is_ipv4);
use Data::Dumper;
### README
# to install needed modules on debian:
# * apt-get install libperlio-gzip-perl libdancer2-perl libdbi-perl libdbd-sqlite3-perl
# * apt-get install libperlio-gzip-perl libdancer2-perl libdbi-perl libdbd-sqlite3-perl libdata-validate-ip-perl
### vars
my $dbfile = "xtrd.db";
my $ip2asn_csv_url = 'http://iptoasn.com/data/ip2asn-v4-u32.tsv.gz';
set port => 12110;
my $dbfile = 'xtrd.db';
my $ip2asn_csv_url = 'http://iptoasn.com/data/ip2asn-v4-u32.tsv.gz';
my $server_protocol_version = 3;
my $min_client_version = 3;
set port => 12110;
### connect to database
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","");
@ -39,17 +44,38 @@ hook 'before' => sub {
};
### main get route
get '/v2/server' => sub {
get '/v3/server/list/online' => sub {
refresh_server_status($dbh,"1");
my $server = get_server($dbh,"1");
return encode_json $server;
};
put '/v2/server/:ip/:port/:status' => sub {
get '/v3/server/list/offline' => sub {
my $server = get_server($dbh,"0");
return encode_json $server;
};
get '/v3/server/list/all' => sub {
my $server = get_server($dbh,"");
return encode_json $server;
};
get '/v3/server/info/version' => sub {
return $server_protocol_version;
};
put '/v3/server/add/:ip/:port/:state' => sub {
my $ip = route_parameters->get('ip');
my $port = route_parameters->get('port');
my $status = route_parameters->get('status');
my $result = add_server($dbh,$ip,$port,$status);
return encode_json $result;
my $state = route_parameters->get('state');
### check input
if(!is_ipv4($ip)) { status('bad_request'); return "invalid IP address"; }
if(!($port > 0 and $port < 65536)) { status('bad_request'); return "invalid port"; }
if(!($state >= 0 and $state < 256)) { status('bad_request'); return "invalid status"; }
my $result = add_server($dbh,$ip,$port,$state);
return "OK";
};
### start the loop
@ -58,6 +84,17 @@ start;
### if you reach this, you're done
exit;
sub check_server_status
{
my ($hostname,$port) = @_;
my $url = "http://".$hostname.":".$port."/v3/client/info/version";
my $ua = LWP::UserAgent->new(timeout => 2);
my $req = HTTP::Request->new("GET", $url);
my $response = $ua->request($req);
my @return_code = ($response->is_success, $response->decoded_content);
return @return_code;
}
sub add_server
{
my ($dbh,$ip,$port,$status) = @_;
@ -66,8 +103,6 @@ sub add_server
{
# update server status
set_server_status($dbh,$id,$status);
my $return_code = {"status" => "1"};
return $return_code;
}
else
{
@ -78,8 +113,6 @@ sub add_server
my $add_server_cmd = "INSERT INTO server VALUES ('".$ip."','".$port."','".$as."','".$status."');";
my $add_server_sth = $dbh->prepare($add_server_cmd);
$add_server_sth->execute();
my $return_code = {"status" => "1"};
return $return_code;
}
}
@ -103,14 +136,55 @@ sub get_server_id
return $result->[0] || 0;
}
sub refresh_server_status
{
my ($dbh,$status) = @_;
my $temp_data = get_server($dbh,$status);
### check all servers and update status
foreach my $item (@$temp_data)
{
my ($success,$version) = check_server_status($item->[0],$item->[1]);
if($success)
{
if($version >= $min_client_version)
{
my $id = get_server_id($dbh, $item->[0],$item->[1]);
set_server_status($dbh, $id, "1");
}
else
{
my $id = get_server_id($dbh, $item->[0],$item->[1]);
set_server_status($dbh, $id, "0");
}
}
else
{
my $id = get_server_id($dbh, $item->[0],$item->[1]);
set_server_status($dbh, $id, "0");
}
}
}
sub get_server
{
my ($dbh,$status) = @_;
if($status eq "") { $status = 1; }
my $get_server_cmd = "SELECT * FROM server WHERE server_status = '".$status."';";
my $get_server_sth = $dbh->prepare($get_server_cmd);
$get_server_sth->execute();
my $result = $get_server_sth->fetchall_arrayref;;
my $get_server_cmd = "";
if($status eq "")
{
$get_server_cmd = "SELECT * FROM server;";
}
else
{
$get_server_cmd = "SELECT * FROM server WHERE server_status = '".$status."';";
}
my $get_server_sth = $dbh->prepare($get_server_cmd);
$get_server_sth->execute();
my $result = $get_server_sth->fetchall_arrayref;
return $result;
}