diff --git a/xtrd.pl b/xtrd.pl index 9254b74..a851e51 100644 --- a/xtrd.pl +++ b/xtrd.pl @@ -12,16 +12,70 @@ 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 libdata-validate-ip-perl -### vars -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; +### README ######################################################################################################################################### +# This is the server for the XTR (XiCoN Trace Route). It's more of a master browser for the clients, so an web frontend can get a list of all the +# available clients combined from one auto-updated source. It opens a port and listening to HTTP requests (GET and PUT). It can list all online and +# offline clients. It also accepts new client entries via a PUT request (target client address in database has to be the request ip). +# +# The server keeps the list of clients up2date with checking their availablity with each request of all online clients. +# +# This software uses the iptoasn database (https://iptoasn.com/) to determine the AS for each client. To keep track of all ip to asn relations, +# this software creates a local database file "ip2asn.db", which is a simple SQLite3 database with an index for faster searches. +# +# For now, only IPv4 clients are supported. This will change in the future. +###################################################################################################################################################### + + +### CHANGELOG ###################################################################################################################################### +# v0.4 (2019-08-22) +# * ADD: added better documentation +# * ADD: list of API commands via API +# * FIX: replaced bug-driven LWP::* with HTTP::Tiny +# * FIX: limited adding of clients to source ip of target entry (only add a client which you are coming from) +###################################################################################################################################################### + + +### INSTALL ######################################################################################################################################## +# This is a standalone software which usually runs in the "foreground". Starting it in a screen session or via init.d/systemd/rc.d is +# highly recommended. For testing, starting this script in a screen session is also fine. +# +# Either install all listed modules with "cpan -i " or use your system's package manager (apt, yum, yast). +# +# On Debian just install there packages: +# 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'; +my $server_protocol_version = 3; +my $min_client_version = 4; +set port => 12110; +###################################################################################################################################################### + + + + + + + + + + + + + + +###################################################################################################################################################### +### NOTHING TO CHANGE BELOW HERE ################################################################################################################### +###################################################################################################################################################### ### connect to database my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","",""); @@ -64,13 +118,29 @@ get '/v3/server/info/version' => sub { return $server_protocol_version; }; +get '/v3/server/info/commands' => sub { + my $commands = "/v".$server_protocol_version."/server/list/online\n"; + $commands .= "/v".$server_protocol_version."/server/list/offline\n"; + $commands .= "/v".$server_protocol_version."/server/list/all\n"; + $commands .= "/v".$server_protocol_version."/server/add///\n"; + $commands .= "/v".$server_protocol_version."/server/info/version\n"; + $commands .= "/v".$server_protocol_version."/server/info/commands\n"; + return $commands; +}; + put '/v3/server/add/:ip/:port/:state' => sub { + ### collect parameters my $ip = route_parameters->get('ip'); my $port = route_parameters->get('port'); my $state = route_parameters->get('state'); + ### get client ip + my $client_ip = request->address; + my $request_ffa = request->forwarded_for_address; + if(is_ipv4($request_ffa)) { $client_ip = $request_ffa; } + ### check input - if(!is_ipv4($ip)) { status('bad_request'); return "invalid IP address"; } + if(!is_ipv4($ip) || $client_ip ne $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"; }