Initial commit - v1.3.1-18

This commit is contained in:
Mab 974
2023-02-06 23:04:43 +04:00
commit 26f1ecc659
62 changed files with 10622 additions and 0 deletions

View File

@@ -0,0 +1,561 @@
package SrvMngr::Controller::Xt_geoip;
#----------------------------------------------------------------------
# heading : Security
# description : GeoIP IP filtering
# navigation : 5000 5610
# name : xt_geoip, method : get, url : /xt_geoip, ctlact : Xt_geoip#main
# name : xt_geoipd, method : post, url : /xt_geoip, ctlact : Xt_geoip#do_display
# name : xt_geoipc, method : get, url : /xt_geoipb, ctlact : Xt_geoip#do_display
# name : xt_geoipu, method : post, url : /xt_geoipb, ctlact : Xt_geoip#do_action
#
# routes : end
#----------------------------------------------------------------------
use strict;
use warnings;
use Mojo::Base 'Mojolicious::Controller';
use POSIX qw(strftime);
use Locale::gettext;
use SrvMngr::I18N;
use SrvMngr qw( theme_list init_session );
our $cdb = esmith::ConfigDB->open() or die "Couldn't open ConfigDB\n";
sub main {
my $c = shift;
$c->app->log->info($c->log_req);
my $title = $c->render_to_string(inline =>($c->l('xtg_FORM_TITLE')));
my %xtg_datas = ();
$xtg_datas{'choice'} = '';
$c->stash( title => $title, xtg_datas => \%xtg_datas);
$c->render('xt_geoip');
};
sub do_display {
my $c = shift;
$c->app->log->info($c->log_req);
my $title = $c->render_to_string(inline =>($c->l('xtg_FORM_TITLE')));
my $choice = $c->param('Choice');
my $result;
my %xtg_datas = ();
$xtg_datas{'choice'} = $choice;
if ( $choice eq 'LCOD' ) {
$result = $c->generateCodes();
# $c->stash( title => $title, modul => $result, xtg_datas => \%xtg_datas );
# return $c->render('xt_geoip_lst');
};
if ( $choice eq 'LF2B' ) {
$result = $c->generateStats( 'f2b' );
# $result = $c->render_to_string(inline => generateStats($c, 'f2b'));
# $c->stash( title => $title, modul => $result, xtg_datas => \%xtg_datas );
# return $c->render('xt_geoip_lst');
};
if ( $choice eq 'LSSH' ) {
$result = $c->generateStats( 'ssh' );
# $result = $c->render_to_string(inline => generateStats($c, 'ssh'));
# $c->stash( title => $title, modul => $result, xtg_datas => \%xtg_datas );
# return $c->render('xt_geoip_lst');
};
if ( $choice eq 'LIPT' ) {
$result = $c->generateStats( 'ipt' );
# $c->stash( title => $title, modul => $result, xtg_datas => \%xtg_datas );
# return $c->render('xt_geoip_lst');
};
if ( $choice eq 'LSRV' ) {
# $c->stash( title => $title, xtg_datas => \%xtg_datas );
# return $c->render('xt_geoip_lsrv');
}
if ( $choice eq 'UPDT' ) {
# $c->stash( title => $title, xtg_datas => \%xtg_datas );
# return $c->render('xt_geoip_updt');
}
if ( $choice eq 'UPDS' ) {
$xtg_datas{name} = $c->param('Name');
# $c->stash( title => $title, xtg_datas => \%xtg_datas );
# return $c->render('xt_geoip_upds');
}
if ( $choice eq 'REMS' ) {
$xtg_datas{name} = $c->param('Name');
# $c->stash( title => $title, xtg_datas => \%xtg_datas );
# return $c->render('xt_geoip_rems');
}
$c->stash( title => $title, modul => $result, xtg_datas => \%xtg_datas );
return $c->render( 'xt_geoip_lst' ) if ( $choice ~~ [ 'LCOD', 'LF2B', 'LSSH', 'LIPT' ] );
return $c->render( 'xt_geoip'.'_'.lc($choice) ) if ( $choice ~~ [ 'UPDT', 'UPDS', 'REMS', 'LSRV' ] );
$c->redirect_to('/xt_geoip');
};
sub do_action {
my $c = shift;
$c->app->log->info($c->log_req);
my $rt = $c->current_route;
my $title = $c->render_to_string(inline =>($c->l('xtg_FORM_TITLE')));
my %xtg_datas = ();
my $choice = $c->param('Choice');
$xtg_datas{'choice'} = $choice;
my ($res, $result) = '';
if ( $choice eq 'LSRV' ) {
#$result .= 'Blocked for testing ! Avoid updates for now ';
$res = '';
if ( ! $result ) {
$res = $c->do_otherServices();
$result .= $res unless $res eq 'OK';
if ( ! $result ) {
$result = $c->l('xtg_SERVICE_SUCCESS');
}
}
}
if ( $choice eq 'UPDT' ) {
$res = $c->must_exist();
$result .= $res unless $res eq 'OK';
#$result .= 'Blocked for testing ! Avoid updates for now ';
$res = '';
if ( ! $result ) {
$res = $c->change_settings();
$result .= $res unless $res eq 'OK';
if ( ! $result ) {
$result = $c->l('xtg_SUCCESS');
}
}
}
if ( $choice eq 'REMS' ) {
#$result .= 'Blocked for testing ! Avoid updates for now ';
my $name = $c->param('Name');
$xtg_datas{name} = $name;
$res = '';
if ( ! $result ) {
$res = $c->remove_serv();
$result .= $res unless $res eq 'OK';
if ( ! $result ) {
$result = $c->l('xtg_SUCCESSFULLY_DELETED_SERVICE');
}
}
}
if ( $choice eq 'UPDS' ) {
#$result .= 'Blocked for testing ! Avoid updates for now ';
my $name = $c->param('Name');
$xtg_datas{name} = $name;
$res = '';
if ( ! $result ) {
$res = $c->modify_serv();
$result .= $res unless $res eq 'OK';
if ( ! $result ) {
$result = $c->l('xtg_SERVICE_SUCCESS');
}
}
}
# common parts
if ($res ne 'OK') {
$c->stash( error => $result );
$c->stash( title => $title, xtg_datas => \%xtg_datas );
return $c->render( 'xt_geoip'.'_'.lc($choice) );
}
my $message = "xt_geoip updates $choice DONE";
$c->app->log->info($message);
$c->flash( success => $result );
## $c->flash( error => 'No changes applied !!' );
#return to 'xt_geoip' route !!!
$c->redirect_to('/xt_geoip');
};
sub get_badcountries {
my $c = shift;
my $full = shift;
my $badc = $cdb->get_prop("masq", "BadCountries") || "";
return $badc unless $full ;
my $rev = (($cdb->get_prop("masq", "XTGeoipRev")||"disabled") eq "enabled") ? "!=" : "==";
return "$rev $badc ";
}
sub get_geoip {
return $cdb->get_prop("masq", "GeoIP") || 'disabled';
}
sub get_reverse {
my $c = shift;
my $item = shift;
my $prop = shift;
$item = ($item eq 'masq') ? $item : $c->param('Name');
return $cdb->get_prop("$item", "$prop") || "disabled";
}
sub get_stat_geoip {
my $c = shift;
if ( system ( "/bin/test -f /lib/modules/`/bin/uname -r`/weak-updates/xtables-addons/xt_geoip.ko") != 0 ) {
return $c->l('xtg_ERROR_MISSING_MODULE');
} elsif ( system ( "/sbin/lsmod | grep 'xt_geoip' > /dev/null") != 0 ) {
return $c->l('xtg_ERROR_UNLOADED_MODULE');
} elsif ( get_geoip() eq 'enabled' && system ( "/sbin/iptables -L -n | grep 'XTGeoIP' > /dev/null") != 0 ) {
return $c->l('xtg_ERROR_FILTER_CHAIN_MISSING');
} else {
return '';
}
}
sub get_stat_license_key {
my $c = shift;
if (($cdb->get_prop( 'geoip','status' ) || 'disabled') eq 'enabled' ) {
if ( ($cdb->get_prop( 'geoip','LicenseKey' ) || '') ne '' ) {
return '';
}
}
return $c->l('xtg_ERROR_LICENSE_KEY');
}
sub get_date_update {
my $file = "/usr/share/xt_geoip/LE/A1.iv4";
my $filetime = ( -e $file ) ? (stat($file))[9] : 0;
return strftime("%Y/%m/%d %H:%M", localtime( $filetime )) || '';
}
sub get_srv_name {
my ($c) = @_;
return $c->param('Name');
}
sub get_services_table {
my $c = shift;
my $choice = shift || 'sel';
my @services;
my @serv_rec;
if ( $choice ne 'all' ) {
@services = split(/,/, $cdb->get_prop("masq", "XtServices"));
for ( @services ) {
my $rec = $cdb->get( $_ );
push @serv_rec, $rec if ( $rec && $rec->prop('TCPPort') );
}
} else {
for ($cdb->get_all_by_prop(type => 'service')) {
push @serv_rec, $_ if ( $_->prop('TCPPort') );
}
}
return \@serv_rec;
}
sub get_srv_badcountries {
my ( $c, $name, $full ) = @_;
my $badc = $cdb->get_prop($name, "BadCountries")||"";
return $badc unless $full ;
my $rev = (($cdb->get_prop($name, "XTGeoipRev")||"disabled") eq "enabled") ? "!=" : "==";
return "$rev $badc ";
}
#Subroutine to list statistics
sub generateStats {
my $c = shift;
my $stats_type = shift;
my $out = '';
# Untaint $name before use in system()
$stats_type =~ /(.+)/; $stats_type = $1;
if ($stats_type ne "ipt" and $stats_type ne "ssh" and $stats_type ne "f2b") {
$out .= sprintf("<h3>%s %s </h3>", $c->l('xtg_INVALID_STATS_TYPE'), $stats_type);
return $out;
}
# my $now_string = $c->gen_locale_date_string();
my $file = "/var/lib/xt_geoip/extA_" . $stats_type . "_country.lst";
my $filetime = ( -e $file ) ? (stat($file))[9] : 0;
my $date_string = strftime("%Y/%m/%d %H:%M", localtime( $filetime )) || '';
$out .= sprintf("<h3>%s %s %s</h3>", $c->l('xtg_STATS_GENERATED'), $stats_type, $date_string);
open( Xt_GEOIPSTATS, $file );
$out .= sprintf "<pre>";
while (<Xt_GEOIPSTATS>)
{
$out .= sprintf("%s", $_);
}
close Xt_GEOIPSTATS;
$out .= sprintf "</pre>";
$out .= sprintf("<h3>%s</h3>", $c->l('xtg_END_OF_STATS'));
return $out;
}
#Subroutine to list counries codes
sub generateCodes {
my $c = shift;
my $out = '';
# my $now_string = $c->gen_locale_date_string();
my $file = "/usr/share/xt_geoip/geoip_countries_list.txt";
unless ( -e $file ) {
$out .= "<br>" . $c->l('xtg_INVALID_CODES_LIST');
return $out;
}
my $filetime = ( -e $file ) ? (stat($file))[9] : 0;
my $date_string = strftime("%Y/%m/%d %H:%M", localtime( $filetime )) || '';
$out .= sprintf("<h3>%s %s </h3>", $c->l('xtg_COUNTRY_LIST'), $date_string);
open( Xt_GEOIPCODES, $file );
$out .= sprintf "<pre>";
while (<Xt_GEOIPCODES>) {
$out .= sprintf("%s", $_);
}
close Xt_GEOIPCODES;
$out .= sprintf "</pre>";
$out .= sprintf("<h3>%s</h3>", $c->l('xtg_END_OF_CODES'));
return $out;
}
#Subroutine to list other services codes
sub otherServices {
my ($c, $choice) = @_;
my %serv_ok = map { $_ => 1} split(/,/, $cdb->get_prop("masq", "XtServices"));
# unless $choice eq 'all';
my @serv_others = ();
for ($cdb->get_all_by_prop(type => 'service')) {
next unless $_->prop('TCPPort');
# if ( $_->prop('TCPPort')) {
# push @serv_others, $_->key unless exists( $serv_ok{$_->key});
push @serv_others, $_->key unless $choice eq 'sel' and not exists( $serv_ok{$_->key});
# }
}
return \@serv_others;
}
#Subroutine to update list services codes
sub do_otherServices {
my $c = shift;
my $serv1 = ($cdb->get_prop("masq", "XtServices")) || '';
my $serv2 = join( ',', @{$c->every_param('Selectedservices')} );
$cdb->set_prop("masq", "XtServices", $serv2) if ( $serv1 ne $serv2);
return 'OK';
}
sub change_settings {
my $c = shift;
my $mq_bc = get_badcountries();
my $mq_gp = get_geoip();
my $masq = $cdb->get('masq') || "disabled";
my $mq_rv = $masq->prop('XTGeoipRev') || 'disabled';
my $mq_ot = $masq->prop('XTGeoipOther') || 'disabled';
my $n_mq_bc = $c->param("Masq_badcountries");
my $n_mq_gp = $c->param("Masq_geoip") || $mq_gp;
my $n_upd_gp = $c->param("Update_geoip") || '';
my $n_mq_rv = $c->param("Masq_reverse") || $mq_rv ;
my $n_mq_ot = $c->param("Masq_others") || $mq_ot ;
if (($n_mq_bc eq $mq_bc) && ($n_mq_gp eq $mq_gp) && ($n_upd_gp eq 'NO') && ($n_mq_rv eq $mq_rv) && ($n_mq_ot eq $mq_ot)) {
return 'OK'
}
$cdb->set_prop("masq", "BadCountries", $n_mq_bc);
$cdb->set_prop("masq", "GeoIP", $n_mq_gp);
$cdb->set_prop("masq", "XTGeoipRev", $n_mq_rv);
$cdb->set_prop("masq", "XTGeoipOther", $n_mq_ot);
my $eventloc = "xt_geoip-modify";
$eventloc = "xt_geoip-update" if $n_upd_gp eq 'YES';
unless ( system ( "/sbin/e-smith/signal-event", $eventloc ) == 0 ) {
return $c->l("xtg_ERROR_UPDATING");
}
return 'OK';
}
=head2 valid_badcountries
subroutine to validate countries.
=cut
sub must_exist {
my $c = shift;
my $listerr = "";
my @mq_bcs = split /[,:]/, $c->param("Masq_badcountries");
if (@mq_bcs) {
my $ctr = @mq_bcs;
return $c->l('xtg_ERROR_COUNTRY_MAX', $ctr) if ($ctr > 50);
foreach my $bcs (@mq_bcs) {
my $file = "/usr/share/xt_geoip/LE/" . $bcs . ".iv4";
if (! -f $file) { $listerr .= $bcs . ","; }
}
return $c->l('xtg_ERROR_COUNTRY_NOT_EXIST', $listerr) if $listerr;
}
return 'OK';
}
sub remove_serv {
my ( $c ) = @_;
my $name = $c->param('Name');
# Untaint $name before use in system()
$name =~ /(.+)/; $name = $1;
if (my $serv = $cdb->get($name)) {
my $servBC = $serv->prop('BadCountries') || '';
if ($servBC ne '') {
my $tps = $cdb->get_prop_and_delete($name, "BadCountries");
$tps = $cdb->get_prop_and_delete($name, "XTGeoipRev");
unless (system ("/sbin/e-smith/signal-event", "xt_geoip-service") == 0) {
return $c->l("xtg_ERROR_WHILE_DELETING_SERVICE").' '.$name;
}
return 'OK';
}
} else {
return $c->l('xtg_CANT_FIND_SERV');
}
}
sub modify_serv {
my ($c) = @_;
my $name = $c->param('Name');
# Untaint $name before use in system()
$name =~ /(.+)/; $name = $1;
if (my $serv = $cdb->get($name)) {
my $servBC = $serv->prop('BadCountries') || '';
my $servRev = $serv->prop('XTGeoipRev') || 'disabled';
my $n_servBC = $c->param("Masq_srv_badcountries");
my $n_servRev = $c->param("Masq_srv_reverse") || $servRev;
if ($n_servBC eq $servBC && $n_servRev eq $servRev ) {
return 'OK';
}
$cdb->set_prop($name, "BadCountries", $n_servBC);
$cdb->set_prop($name, "XTGeoipRev", $n_servRev);
unless (system ( "/sbin/e-smith/signal-event", "xt_geoip-service" ) == 0 ) {
return $c->l("xtg_ERROR_UPDATING").' '.$name;
}
return 'OK';
} else {
return $c->l('xtg_CANT_FIND_SERV');
}
}
1;

View File

@@ -0,0 +1,69 @@
'xtg_FORM_TITLE' => 'XTABLES-ADDONS Administration<br>GeoIP',
'xtg_ERROR_UPDATING' => 'There was an error while updating xt_geoip.',
'xtg_SUCCESS' => 'Successful change... Now IP filtering takes into account the countries entered.',
'xtg_SERVICE_SUCCESS' => 'Successful change... Now IP filtering takes into account the new services list.',
'xtg_XT_GEOIP_STATUS_DESCRIPTION' => ' <ul><li>IP filtering can be enabled or disabled with the appropriate button</li>
<li>Codes of the countries to be banished can be entered in the corresponding field</li>
<li>An immediate update of the table can be requested here</li></ul>
<p><i>Be careful not to ban the IP addresses needed to run your server !</i></p> ',
'xtg_NO_CHANGE' => 'No change... no update !',
'xtg_LABEL_GEOIP_STATUS' => 'Current GEOIP filtering : ',
'xtg_LABEL_GEOIP' => 'GEOIP filtering desired : ',
'xtg_DESC_GEOIP' => 'Should filtering by country of origin be activated ?',
'xtg_ERROR_STATUS_GEOIP' => ' <font color="red">GEOIP filtering is <b>inactive</b></font> (iptables) ',
'xtg_LABEL_BADCOUNTRIES_STATUS' => 'Current list of banished country codes : ',
'xtg_ERROR_COUNTRY_NOT_EXIST' => 'Country code(s) does not exist on the server: [_1]',
'xtg_LABEL_BADCOUNTRIES' => 'New country codes to be used :',
'xtg_DESC_BADCOUNTRIES' => 'List of country codes (uppercase et comma-separated).',
'xtg_LABEL_DATE_UPDATE_STATUS' => 'Last update of GeoIP table :',
'xtg_LABEL_UPDATE' => 'Force the update of GeoIP table :',
'xtg_DESC_UPDATE' => 'GeoIP table is updated every week, but you can ...',
'xtg_IPT_LIST_DESCRIPTION' => '<ul><li>Click here to see the statistics of the prevented connections</li></ul> ',
'xtg_IPT_LIST' => 'Filtered',
'xtg_SSH_LIST_DESCRIPTION' => '<ul><li>Click here to see the statistics of SSH errors for non blocked access</li></ul> ',
'xtg_SSH_LIST' => 'SSH errors',
'xtg_F2B_LIST_DESCRIPTION' => '<ul><li>Click here to see the statistics of the connections banned by fail2ban</li></ul> ',
'xtg_F2B_LIST' => 'F2b banned',
'xtg_STATS_GENERATED' => 'Statistics generated',
'xtg_END_OF_STATS' => 'End of Statistics',
'xtg_INVALID_STATS_TYPE' => 'Invalid type of statistics',
'xtg_STATS_DESCRIPTION' => '<HR class="sme-copyrightbar"> <h3>Statistics for Xt_GeoIP</h3><ul>
<li>For 3 periods : Day, Week and Month</li>
<li>Banned Ips by country sorted by number</li>
<li>Ssh errors by country sorted by number</li>
<li>Fail2ban banned IPs by country sorted by number</li>
</ul><p><i>XX means Country not found !</i></p> ',
'xtg_COUNTRY_LIST_DESCRIPTION' => '<ul><li>Click here to see a <b>list of available country codes</b></li></ul> ',
'xtg_LABEL_COUNTRY_LIST' => 'Country codes : ',
'xtg_COUNTRY_LIST' => 'Country codes',
'xtg_CNTRY_LIST' => 'List',
'xtg_END_OF_CODES' => 'End of code list',
'xtg_SERVICE_DESCRIPTION' => 'Per service filtering for Xtables GeoIP',
'xtg_SERVICE_DESCRIPTION2' => '<ul><li>If you want different filtering based on services</li></ul>',
'xtg_LABEL_SERVICE' => 'Service name : ',
'xtg_PER_SERVICE_GEOIP' => 'Services',
'xtg_ADD_SERVICE' => 'Add or modify a per service filtering',
'xtg_ADD_DESC' => 'You are choosing a particular country filtering for this service',
'xtg_REMOVE_SERVICE' => 'Delete a per service filtering',
'xtg_REMOVE_DESC' => 'You are deleting a filtering by service. The general filtering will then apply.',
'xtg_SERV_NOT_BAN' => 'Unfiltered service.',
'xtg_NO_SERVICES' => 'No services.',
'xtg_LABEL_SERV_BADCOUNTRIES_STATUS' => 'List of rejected country codes for the service : ',
'xtg_SUCCESSFULLY_DELETED_SERVICE' => 'Per service filtering successfully deleted... New filtering taken into account.',
'xtg_BADCOUNTRIES' => 'Blacklist',
'xtg_ERROR_COUNTRY_MAX' => 'Too many countries chosen: [_1]',
'xtg_LABEL_REVERSE_MATCH' => 'Reject if',
'xtg_DESC_REVERSE_MATCH' => 'The following option allow to chose if you want reject visitors from the country list (==) which is the default behaviour, or if you want to only let them in (!=).',
'xtg_LABEL_OTHERS' => 'General filter only for services without rules',
'xtg_DESC_OTHERS' => 'Choose if you want to have the general filter to apply to all incoming connections or if you do not want to filter ports already defined with a specific service rule. This would allow you to have a service less restricted than the general rule if you enable this.',
'xtg_SERVICE_LIST_DESCRIPTION' => '<ul><li>Click here to select <b>new </b>services among an <b>available services list</b></li></ul> ',
'xtg_LABEL_SERVICE_LIST' => 'Service codes : ',
'xtg_SERV_LIST' => 'List',
'xtg_DESC_AVAILABLE_SERVICES' => ' <h2> New Services selection</h2>
<ul><li>Select here among the other services not yet managed individually.
</li><li>You may select one or more elements. Generally <b>enabled and public</b> ones.</li></ul> ',
'xtg_LABEL_AVAILABLE_SERVICES' => 'Available Services',
'xtg_ERROR_LICENSE_KEY' => 'GEOIP license key unavailable. Downloading is <b>inactive</b>',
'xtg_ERROR_MISSING_MODULE' => 'Module xt_geoip is missing. GeoIP based filtering is <b>inactive</b>',
'xtg_ERROR_UNLOADED_MODULE' => 'Module xt_geoip not loaded. GeoIP based filtering is <b>inactive</b>',
'xtg_ERROR_FILTER_CHAIN_MISSING' => 'Filtering chain XTGeoIP is not in use. GeoIP based filtering is <b>inactive</b>',