Move websocket functions to be always available

This commit is contained in:
Ilia Ross
2024-06-04 15:48:06 +03:00
parent 00ddfd4d05
commit 55b5939194
5 changed files with 104 additions and 106 deletions

File diff suppressed because one or more lines are too long

View File

@@ -60,7 +60,7 @@ $vers || usage();
"webmin-search-lib.pl", "WebminCore.pm",
"record-login.pl", "record-logout.pl", "record-failed.pl",
"robots.txt", "unauthenticated", "bin", "html-editor-lib.pl",
"switch_theme.cgi", "os_eol.json", "websockets-lib-funcs.pl",
"switch_theme.cgi", "os_eol.json",
);
if ($min) {
# Only those required by others

View File

@@ -13522,6 +13522,108 @@ my $dir = $var_directory."/locks/".$$;
return $dir;
}
# allocate_miniserv_websocket()
# Allocate a new websocket and
# stores it miniserv.conf file
sub allocate_miniserv_websocket
{
# Find ports already in use
&lock_file(&get_miniserv_config_file());
my %miniserv;
&get_miniserv_config(\%miniserv);
my %inuse;
foreach my $k (keys %miniserv) {
if ($k =~ /^websockets_/ && $miniserv{$k} =~ /port=(\d+)/) {
$inuse{$1} = 1;
}
}
# Pick a port and configure Webmin to proxy it
my $port = $config{'base_port'} || 555;
while(1) {
if (!$inuse{$port}) {
&open_socket("127.0.0.1", $port, my $fh, \$err);
last if ($err);
close($fh);
}
$port++;
}
my $module_theme_name = $module_name || $current_theme;
my $wspath = "/$module_theme_name/ws-".$port;
my $now = time();
$miniserv{'websockets_'.$wspath} = "host=127.0.0.1 port=$port wspath=/ user=$remote_user time=$now";
&put_miniserv_config(\%miniserv);
&unlock_file(&get_miniserv_config_file());
&reload_miniserv();
return $port;
}
# remove_miniserv_websocket(port)
# Remove old websocket
# from miniserv.conf
sub remove_miniserv_websocket
{
my ($port) = @_;
my %miniserv;
if ($port) {
&lock_file(&get_miniserv_config_file());
&get_miniserv_config(\%miniserv);
my $module_theme_name = $module_name || $current_theme;
my $wspath = "/$module_theme_name/ws-".$port;
if ($miniserv{'websockets_'.$wspath}) {
delete($miniserv{'websockets_'.$wspath});
&put_miniserv_config(\%miniserv);
&reload_miniserv();
}
&unlock_file(&get_miniserv_config_file());
}
}
# cleanup_miniserv_websockets([&skip-ports])
# Called by scheduled status collection to remove any
# websockets in miniserv.conf that are no longer used
sub cleanup_miniserv_websockets
{
my ($skip) = @_;
$skip ||= [ ];
&lock_file(&get_miniserv_config_file());
my %miniserv;
&get_miniserv_config(\%miniserv);
my $now = time();
my @clean;
my $module_theme_name = $module_name || $current_theme;
foreach my $k (keys %miniserv) {
$k =~ /^websockets_\/$module_theme_name\/ws-(\d+)$/ || next;
my $port = $1;
next if (&indexof($port, @$skip) >= 0);
my $when = 0;
if ($miniserv{$k} =~ /time=(\d+)/) {
$when = $1;
}
if ($now - $when > 60) {
# Has been open for a while, check if the port is still in use?
my $err;
&open_socket("127.0.0.1", $port, my $fh, \$err);
if ($err) {
# Closed now, can clean up
push(@clean, $k);
}
else {
# Still active
close($fh);
}
}
}
if (@clean) {
foreach my $k (@clean) {
delete($miniserv{$k});
}
&put_miniserv_config(\%miniserv);
&reload_miniserv();
}
&unlock_file(&get_miniserv_config_file());
}
$done_web_lib_funcs = 1;
1;

View File

@@ -1,103 +0,0 @@
# allocate_miniserv_websocket()
# Allocate a new websocket and
# stores it miniserv.conf file
sub allocate_miniserv_websocket
{
# Find ports already in use
&lock_file(&get_miniserv_config_file());
my %miniserv;
&get_miniserv_config(\%miniserv);
my %inuse;
foreach my $k (keys %miniserv) {
if ($k =~ /^websockets_/ && $miniserv{$k} =~ /port=(\d+)/) {
$inuse{$1} = 1;
}
}
# Pick a port and configure Webmin to proxy it
my $port = $config{'base_port'} || 555;
while(1) {
if (!$inuse{$port}) {
&open_socket("127.0.0.1", $port, my $fh, \$err);
last if ($err);
close($fh);
}
$port++;
}
my $module_theme_name = $module_name || $current_theme;
my $wspath = "/$module_theme_name/ws-".$port;
my $now = time();
$miniserv{'websockets_'.$wspath} = "host=127.0.0.1 port=$port wspath=/ user=$remote_user time=$now";
&put_miniserv_config(\%miniserv);
&unlock_file(&get_miniserv_config_file());
&reload_miniserv();
return $port;
}
# remove_miniserv_websocket(port)
# Remove old websocket
# from miniserv.conf
sub remove_miniserv_websocket
{
my ($port) = @_;
my %miniserv;
if ($port) {
&lock_file(&get_miniserv_config_file());
&get_miniserv_config(\%miniserv);
my $module_theme_name = $module_name || $current_theme;
my $wspath = "/$module_theme_name/ws-".$port;
if ($miniserv{'websockets_'.$wspath}) {
delete($miniserv{'websockets_'.$wspath});
&put_miniserv_config(\%miniserv);
&reload_miniserv();
}
&unlock_file(&get_miniserv_config_file());
}
}
# cleanup_miniserv_websockets([&skip-ports])
# Called by scheduled status collection to remove any
# websockets in miniserv.conf that are no longer used
sub cleanup_miniserv_websockets
{
my ($skip) = @_;
$skip ||= [ ];
&lock_file(&get_miniserv_config_file());
my %miniserv;
&get_miniserv_config(\%miniserv);
my $now = time();
my @clean;
my $module_theme_name = $module_name || $current_theme;
foreach my $k (keys %miniserv) {
$k =~ /^websockets_\/$module_theme_name\/ws-(\d+)$/ || next;
my $port = $1;
next if (&indexof($port, @$skip) >= 0);
my $when = 0;
if ($miniserv{$k} =~ /time=(\d+)/) {
$when = $1;
}
if ($now - $when > 60) {
# Has been open for a while, check if the port is still in use?
my $err;
&open_socket("127.0.0.1", $port, my $fh, \$err);
if ($err) {
# Closed now, can clean up
push(@clean, $k);
}
else {
# Still active
close($fh);
}
}
}
if (@clean) {
foreach my $k (@clean) {
delete($miniserv{$k});
}
&put_miniserv_config(\%miniserv);
&reload_miniserv();
}
&unlock_file(&get_miniserv_config_file());
}
1;

View File

@@ -4,7 +4,6 @@ BEGIN { push(@INC, ".."); };
use WebminCore;
&init_config();
our %access = &get_module_acl();
do "$root_directory/websockets-lib-funcs.pl";
# config_pre_load(mod-info-ref, [mod-order-ref])
# Check if some config options are conditional,