Adding whitelist control.

This commit is contained in:
sergiotarxz 2024-07-19 20:01:15 +02:00
parent 87299898fc
commit 2e21e6122f
6 changed files with 88 additions and 43 deletions

View File

@ -65,6 +65,7 @@ sub startup ($self) {
$routes->post('/vpn/user/:id/download')->to('Main#download_file'); $routes->post('/vpn/user/:id/download')->to('Main#download_file');
$routes->post('/vpn/user/:id/enable')->to('Main#enable_user'); $routes->post('/vpn/user/:id/enable')->to('Main#enable_user');
$routes->post('/vpn/user/:id/disable')->to('Main#disable_user'); $routes->post('/vpn/user/:id/disable')->to('Main#disable_user');
# $routes->post('/vpn/save')->to('Main#save_vpn_settings'); $routes->post('/whitelist/add')->to('Main#whitelist_add');
$routes->post('/whitelist/:id/remove')->to('Main#whitelist_remove');
} }
1; 1;

View File

@ -15,7 +15,11 @@ use Path::Tiny;
sub main($self) { sub main($self) {
my $resultset = VPNManager::Schema->Schema->resultset('VPNUser'); my $resultset = VPNManager::Schema->Schema->resultset('VPNUser');
my @users = $resultset->search( {} ); my @users = $resultset->search( {} );
my $resultset_console = VPNManager::Schema->Schema->resultset('WhitelistConsole');
my @whitelist_users = $resultset_console->search({});
warn @whitelist_users;
$self->stash( users => \@users ); $self->stash( users => \@users );
$self->stash( whitelist_users => \@whitelist_users );
$self->render( template => 'main/index' ); $self->render( template => 'main/index' );
} }
@ -162,34 +166,20 @@ sub disable_user($self) {
return $self->redirect_to('/'); return $self->redirect_to('/');
} }
#sub save_vpn_settings($self) { sub whitelist_add($self) {
# my $out_dir = path(__FILE__)->parent->parent->parent->parent->child('out'); my $resultset = VPNManager::Schema->Schema->resultset('WhitelistConsole');
# $out_dir->mkpath; my $username = $self->param('username');
# system 'chmod', '700', $out_dir; eval {
# my $config = $self->config; $resultset->populate([{username => $username}]);
# my $vpn_config = <<"EOF"; };
#[Interface] if ($@) {
#Address = @{[$config->{vpn}{host}]}/@{[$config->{vpn}{submask}]} warn $@;
#MTU = @{[$config->{vpn}{mtu}]} }
#SaveConfig = false return $self->redirect_to('/');
#ListenPort = @{[$config->{vpnclients}{server_port}]} }
#PrivateKey = @{[$config->{vpn}{privkey}]} sub whitelist_remove($self) {
#EOF my $resultset = VPNManager::Schema->Schema->resultset('WhitelistConsole');
# my $resultset = VPNManager::Schema->Schema->resultset('VPNUser'); my $id = $self->param('id');
# my @users = $resultset->search( {} ); $resultset->search({id => $id})->delete;
# return $self->redirect_to('/');
# for my $user (@users) { }1;
# next if !$user->is_enabled;
#
# $vpn_config .= <<"EOF";
#
#[Peer]
#PublicKey = @{[$user->publickey]}
#AllowedIPs = @{[$user->ip_to_text]}/32
#Endpoint = @{[$config->{vpn}{endpoint}]}:@{[$config->{vpnclients}{server_port}]}
#EOF
# }
# $out_dir->child('wg0.conf')->spew_raw($vpn_config);
# return $self->redirect_to('/');
#}
1;

View File

@ -31,6 +31,10 @@ sub MIGRATIONS {
'ALTER TABLE vpn_users rename column is_protected to is_protected_old;', 'ALTER TABLE vpn_users rename column is_protected to is_protected_old;',
'ALTER TABLE vpn_users add is_protected NOT NULL DEFAULT false;', 'ALTER TABLE vpn_users add is_protected NOT NULL DEFAULT false;',
'UPDATE vpn_users set is_protected = is_protected_old;', 'UPDATE vpn_users set is_protected = is_protected_old;',
'CREATE TABLE whitelist_console (
id INTEGER PRIMARY KEY,
username TEXT NOT NULL UNIQUE
);',
); );
} }
1; 1;

View File

@ -0,0 +1,23 @@
#!/usr/bin/env perl
use v5.38.2;
use strict;
use warnings;
use Moo;
use File::Basename qw/dirname/;
use lib dirname(dirname(__FILE__)).'/lib';
use VPNManager::Schema;
use JSON::PP;
sub get_json($self) {
require VPNManager;
my $config = VPNManager->new->config;
my $resultset = VPNManager::Schema->Schema->resultset('WhitelistConsole');
my @users = map { $_->username } $resultset->search( {} );
my $json = JSON::PP->new;
$json->canonical([1]);
$json->pretty([1]);
print $json->encode([@users]);
}
__PACKAGE__->new->get_json;

View File

@ -13,7 +13,8 @@ if ($> != 0) {
while (1) { while (1) {
eval { eval {
install_if_new(); # install_if_new_wireguard();
install_if_new_whitelist();
sleep 15; sleep 15;
}; };
if ($@) { if ($@) {
@ -21,22 +22,30 @@ while (1) {
} }
} }
sub install_if_new { sub install_from_script($script, $output_file) {
my $script_get_wg_config = abs_path(dirname(__FILE__).'/get_wg_config.pl'); my $script_abs = abs_path(dirname(__FILE__). '/'. $script);
my $user = 'vpnmanager'; my $user = 'sergio';
open my $fh, '-|', 'sudo', '-i', '-u', $user, 'perl', $script_get_wg_config; open my $fh, '-|', 'sudo', '-i', '-u', $user, 'perl', $script_abs;
my $contents = join '', <$fh>; my $contents = join '', <$fh>;
my $output_file = '/etc/wireguard/wg0.conf';
my $output_exists; my $output_exists;
open $fh, '<', $output_file and ($output_exists = 1); open $fh, '<', $output_file and ($output_exists = 1);
my $contents_output_file = ''; my $contents_output_file = '';
$contents_output_file = join '', <$fh> if $output_exists; $contents_output_file = join '', <$fh> if $output_exists;
if ($contents ne $contents_output_file) { if ($contents ne $contents_output_file) {
say 'Writting new file'; say 'Writting new file';
say "Writting new file for $script -> $output_file";;
system 'mkdir', '-p', dirname($output_file);
open $fh, '>', $output_file; open $fh, '>', $output_file;
print $fh $contents; print $fh $contents;
system 'systemctl', 'restart', 'wg-quick@wg0'; return 1;
return;
} }
say 'Files equal'; say "Files equal for $script -> $output_file";;
}
sub install_if_new_wireguard {
system 'systemctl', 'restart', 'wg-quick@wg0' if install_from_script('get_wg_config.pl', '/etc/wireguard/wg5.conf');
}
sub install_if_new_whitelist {
install_from_script('get_whitelist_json.pl', '/etc/geyser-console/whitelist.json');
} }

View File

@ -4,6 +4,7 @@
<link rel="stylesheet" href="/style.css"/> <link rel="stylesheet" href="/style.css"/>
</head> </head>
<body class="main"> <body class="main">
<h2>VPN Users</h2>
<p><a href="/vpn/create-user">Create new vpn user</a>.</p> <p><a href="/vpn/create-user">Create new vpn user</a>.</p>
% if (defined $users && @$users) { % if (defined $users && @$users) {
<ul> <ul>
@ -15,8 +16,25 @@
% } % }
% } % }
</ul> </ul>
<form action="/vpn/save" method="post"> <h2>Console whitelist</h2>
<input type="submit" value="Save VPN Settings"/> <h3>Add a user to the console whitelist</h3>
<form action="/whitelist/add" method="post">
<div>
<label for="name">Name</label>
<input name="username"/>
</div>
<input type="submit" value="Submit"/>
</form> </form>
% my $whitelist_users = stash 'whitelist_users';
% if (defined $whitelist_users && @$whitelist_users) {
<h3>List of console users in whitelist</h3>
<ul>
% for my $user (@$whitelist_users) {
<li><%=$user->username%> <form class="inline" method="post" action="/whitelist/<%=$user->id%>/remove">
<input type="submit" value="Delete"/>
</form></li>
% }
</ul>
% }
</body> </body>
</html> </html>