require "${General::swroot}/network-functions.pl";
+# Function to remove duplicates from an array
+sub uniq { my %seen; grep !$seen{$_}++, @_ }
+
#
# log ("message") use default 'ipcop' tag
# log ("tag","message") use your tag
chop;
# Skip comments.
- next if ($_ =~ /\#/);
+ next if ($_ =~ /^#/);
($var, $val) = split /=/, $_, 2;
if ($var)
if($ownnet ne 'ipsec'){
&General::readhasharray("${General::swroot}/vpn/config", \%ipsecconf);
foreach my $key (keys %ipsecconf){
- if ($ipsecconf{$key}[11] ne ''){
+ if ($ipsecconf{$key}[11] ne '' && $ipsecconf{$key}[36] eq ""){
foreach my $ipsecsubitem (split(/\|/, $ipsecconf{$key}[11])) {
my ($ipsecip,$ipsecsub) = split (/\//, $ipsecconf{$key}[11]);
$ipsecsub=&iporsubtodec($ipsecsub);
return 0 if ( substr($parts[1],-1,1) eq '.' );
#check first addresspart (before '@' sign)
- return 0 if ( $parts[0] !~ m/^[a-zA-Z0-9\.!\-\+#]+$/ );
+ return 0 if ( $parts[0] !~ m/^[a-zA-Z0-9\.!\-\_\+#]+$/ );
#check second addresspart (after '@' sign)
return 0 if ( $parts[1] !~ m/^[a-zA-Z0-9\.\-]+$/ );
return $status;
}
+sub number_cpu_cores() {
+ open my $cpuinfo, "/proc/cpuinfo" or die "Can't open cpuinfo: $!\n";
+ my $cores = scalar (map /^processor/, <$cpuinfo>);
+ close $cpuinfo;
+
+ return $cores;
+}
+
+# Tiny function to grab a single IP-address from a given file.
+sub grab_address_from_file($) {
+ my ($file) = @_;
+
+ my $address;
+
+ # Check if the given file exists.
+ if(-f $file) {
+ # Open the file for reading.
+ open(FILE, $file) or die "Could not read from $file. $!\n";
+
+ # Read the address from the file.
+ $address = <FILE>;
+
+ # Close filehandle.
+ close(FILE);
+
+ # Remove newlines.
+ chomp($address);
+
+ # Check if the obtained address is valid.
+ if (&validip($address)) {
+ # Return the address.
+ return $address;
+ }
+ }
+
+ # Return nothing.
+ return;
+}
+
+# Function to get all configured and enabled nameservers.
+sub get_nameservers () {
+ my %settings;
+ my %servers;
+
+ my @nameservers;
+
+ # Read DNS configuration.
+ &readhash("$General::swroot/dns/settings", \%settings);
+
+ # Read configured DNS servers.
+ &readhasharray("$General::swroot/dns/servers", \%servers);
+
+ # Check if the ISP assigned server should be used.
+ if ($settings{'USE_ISP_NAMESERVERS'} eq "on") {
+ # Assign ISP nameserver files.
+ my @ISP_nameserver_files = ( "/var/run/dns1", "/var/run/dns2" );
+
+ # Loop through the array of ISP assigned DNS servers.
+ foreach my $file (@ISP_nameserver_files) {
+ # Grab the IP address.
+ my $address = &grab_address_from_file($file);
+
+ # Check if an address has been grabbed.
+ if ($address) {
+ # Add the address to the array of nameservers.
+ push(@nameservers, $address);
+ }
+ }
+ }
+
+ # Check if DNS servers are configured.
+ if (%servers) {
+ # Loop through the hash of configured DNS servers.
+ foreach my $id (keys %servers) {
+ my $address = $servers{$id}[0];
+ my $status = $servers{$id}[2];
+
+ # Check if the current processed server is enabled.
+ if ($status eq "enabled") {
+ # Add the address to the array of nameservers.
+ push(@nameservers, $address);
+ }
+ }
+ }
+
+ # Return the array.
+ return &uniq(@nameservers);
+}
+
+# Function to format a string containing the amount of bytes to
+# something human-readable.
+sub formatBytes {
+ # Private array which contains the units.
+ my @units = qw(B KB MB GB TB PB);
+
+ my $bytes = shift;
+ my $unit;
+
+ # Loop through the array of units.
+ foreach my $element (@units) {
+ # Assign current processed element to unit.
+ $unit = $element;
+
+ # Break loop if the bytes are less than the next unit.
+ last if $bytes < 1024;
+
+ # Divide bytes amount with 1024.
+ $bytes /= 1024;
+ }
+
+ # Return the divided and rounded bytes count and the unit.
+ return sprintf("%.2f %s", $bytes, $unit);
+}
+
+# Cloud Stuff
+
+sub running_in_cloud() {
+ return &running_on_ec2() || &running_on_gcp();
+}
+
+sub running_on_ec2() {
+ if (-e "/var/run/aws-instance-id") {
+ return 1;
+ }
+
+ return 0;
+}
+
+sub running_on_gcp() {
+ if (-e "/var/run/gcp-instance-id") {
+ return 1;
+ }
+
+ return 0;
+}
1;