VPNManager/script/installer.pl

53 lines
1.3 KiB
Perl
Raw Normal View History

2024-07-16 23:49:24 +02:00
#!/usr/bin/env perl
use v5.38.2;
use strict;
use warnings;
use File::Basename qw/dirname/;
use Cwd 'abs_path';
if ($> != 0) {
die 'You must be root.';
}
while (1) {
eval {
install_if_new_wireguard();
2024-07-19 20:01:15 +02:00
install_if_new_whitelist();
2024-07-16 23:49:24 +02:00
sleep 15;
};
if ($@) {
warn $@;
}
}
2024-07-19 20:01:15 +02:00
sub install_from_script($script, $output_file) {
my $script_abs = abs_path(dirname(__FILE__). '/'. $script);
2024-07-19 20:35:28 +02:00
my $user = 'vpnmanager';
2024-07-19 20:01:15 +02:00
open my $fh, '-|', 'sudo', '-i', '-u', $user, 'perl', $script_abs;
2024-07-16 23:49:24 +02:00
my $contents = join '', <$fh>;
my $output_exists;
open $fh, '<', $output_file and ($output_exists = 1);
my $contents_output_file = '';
$contents_output_file = join '', <$fh> if $output_exists;
if ($contents ne $contents_output_file) {
say 'Writting new file';
2024-07-19 20:01:15 +02:00
say "Writting new file for $script -> $output_file";;
system 'mkdir', '-p', dirname($output_file);
2024-07-16 23:49:24 +02:00
open $fh, '>', $output_file;
print $fh $contents;
2024-07-19 20:01:15 +02:00
return 1;
2024-07-16 23:49:24 +02:00
}
2024-07-19 20:01:15 +02:00
say "Files equal for $script -> $output_file";;
2024-07-20 05:07:58 +02:00
return;
2024-07-19 20:01:15 +02:00
}
sub install_if_new_wireguard {
2024-08-07 21:37:24 +02:00
system 'systemctl', 'reload', 'wg-quick@wg0' if install_from_script('get_wg_config.pl', '/etc/wireguard/wg0.conf');
2024-07-19 20:01:15 +02:00
}
sub install_if_new_whitelist {
install_from_script('get_whitelist_json.pl', '/etc/geyser-console/whitelist.json');
2024-07-16 23:49:24 +02:00
}