Add guess_credentials to parent Control module

pull/3553/merge
Isaac Connor 2025-08-14 09:09:44 -04:00
parent 46dc4f3f17
commit b8fc2d08d5
1 changed files with 53 additions and 0 deletions

View File

@ -379,6 +379,59 @@ sub credentials {
@$self{'username', 'password'} = @_;
}
sub guess_credentials {
my $self = shift;
require URI;
# Extract the username/password host/port from ControlAddress
if ($self->{Monitor}{ControlAddress}
and
$self->{Monitor}{ControlAddress} ne 'user:pass@ip'
and
$self->{Monitor}{ControlAddress} ne 'user:port@ip'
) {
Debug("Using ControlAddress for credentials: $self->{Monitor}{ControlAddress}");
$uri = URI->new($self->{Monitor}->{ControlAddress});
$uri = URI->new('http://'.$self->{Monitor}->{ControlAddress}) if ref($uri) eq 'URI::_foreign';
$$self{host} = $uri->host();
if ( $uri->userinfo()) {
@$self{'username','password'} = $uri->userinfo() =~ /^(.*):(.*)$/;
} else {
$$self{username} = $self->{Monitor}->{User};
$$self{password} = $self->{Monitor}->{Pass};
}
# Check if it is a host and port or just a host
if ( $$self{host} =~ /([^:]+):(.+)/ ) {
$$self{host} = $1;
$$self{port} = $2 ? $2 : $$self{port};
}
$$self{uri} = $uri;
} elsif ($self->{Monitor}{Path}) {
Debug("Using Path for credentials: $self->{Monitor}{Path}");
if (($self->{Monitor}->{Path} =~ /^(?<PROTOCOL>(https?|rtsp):\/\/)?(?<USERNAME>[^:@]+)?:?(?<PASSWORD>[^\/@]+)?@(?<ADDRESS>[^:\/]+)/)) {
$$self{username} = $+{USERNAME} if $+{USERNAME} and !$$self{username};
$$self{password} = $+{PASSWORD} if $+{PASSWORD} and !$$self{password};
$$self{host} = $+{ADDRESS} if $+{ADDRESS};
} elsif (($self->{Monitor}->{Path} =~ /^(?<PROTOCOL>(https?|rtsp):\/\/)?(?<ADDRESS>[^:\/]+)/)) {
$$self{host} = $+{ADDRESS} if $+{ADDRESS};
$$self{username} = $self->{Monitor}->{User} if $self->{Monitor}->{User} and !$$self{username};
$$self{password} = $self->{Monitor}->{Pass} if $self->{Monitor}->{Pass} and !$$self{password};
} else {
$$self{username}= $self->{Monitor}->{User} if $self->{Monitor}->{User} and !$$self{username};
$$self{password} = $self->{Monitor}->{Pass} if $self->{Monitor}->{Pass} and !$$self{password};
}
$uri = URI->new($self->{Monitor}->{Path});
$uri->scheme('http');
$uri->port(80);
$uri->path('');
$$self{host} = $uri->host();
$$self{uri} = $uri;
} else {
Debug('Unable to guess credentials');
}
}
sub get_realm {
my $self = shift;
my $url = shift;