Skip to content

Commit b8fc2d0

Browse files
Add guess_credentials to parent Control module
1 parent 46dc4f3 commit b8fc2d0

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

scripts/ZoneMinder/lib/ZoneMinder/Control.pm

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,59 @@ sub credentials {
379379
@$self{'username', 'password'} = @_;
380380
}
381381

382+
sub guess_credentials {
383+
my $self = shift;
384+
385+
require URI;
386+
387+
# Extract the username/password host/port from ControlAddress
388+
if ($self->{Monitor}{ControlAddress}
389+
and
390+
$self->{Monitor}{ControlAddress} ne 'user:pass@ip'
391+
and
392+
$self->{Monitor}{ControlAddress} ne 'user:port@ip'
393+
) {
394+
Debug("Using ControlAddress for credentials: $self->{Monitor}{ControlAddress}");
395+
$uri = URI->new($self->{Monitor}->{ControlAddress});
396+
$uri = URI->new('http://'.$self->{Monitor}->{ControlAddress}) if ref($uri) eq 'URI::_foreign';
397+
$$self{host} = $uri->host();
398+
if ( $uri->userinfo()) {
399+
@$self{'username','password'} = $uri->userinfo() =~ /^(.*):(.*)$/;
400+
} else {
401+
$$self{username} = $self->{Monitor}->{User};
402+
$$self{password} = $self->{Monitor}->{Pass};
403+
}
404+
# Check if it is a host and port or just a host
405+
if ( $$self{host} =~ /([^:]+):(.+)/ ) {
406+
$$self{host} = $1;
407+
$$self{port} = $2 ? $2 : $$self{port};
408+
}
409+
$$self{uri} = $uri;
410+
} elsif ($self->{Monitor}{Path}) {
411+
Debug("Using Path for credentials: $self->{Monitor}{Path}");
412+
if (($self->{Monitor}->{Path} =~ /^(?<PROTOCOL>(https?|rtsp):\/\/)?(?<USERNAME>[^:@]+)?:?(?<PASSWORD>[^\/@]+)?@(?<ADDRESS>[^:\/]+)/)) {
413+
$$self{username} = $+{USERNAME} if $+{USERNAME} and !$$self{username};
414+
$$self{password} = $+{PASSWORD} if $+{PASSWORD} and !$$self{password};
415+
$$self{host} = $+{ADDRESS} if $+{ADDRESS};
416+
} elsif (($self->{Monitor}->{Path} =~ /^(?<PROTOCOL>(https?|rtsp):\/\/)?(?<ADDRESS>[^:\/]+)/)) {
417+
$$self{host} = $+{ADDRESS} if $+{ADDRESS};
418+
$$self{username} = $self->{Monitor}->{User} if $self->{Monitor}->{User} and !$$self{username};
419+
$$self{password} = $self->{Monitor}->{Pass} if $self->{Monitor}->{Pass} and !$$self{password};
420+
} else {
421+
$$self{username}= $self->{Monitor}->{User} if $self->{Monitor}->{User} and !$$self{username};
422+
$$self{password} = $self->{Monitor}->{Pass} if $self->{Monitor}->{Pass} and !$$self{password};
423+
}
424+
$uri = URI->new($self->{Monitor}->{Path});
425+
$uri->scheme('http');
426+
$uri->port(80);
427+
$uri->path('');
428+
$$self{host} = $uri->host();
429+
$$self{uri} = $uri;
430+
} else {
431+
Debug('Unable to guess credentials');
432+
}
433+
}
434+
382435
sub get_realm {
383436
my $self = shift;
384437
my $url = shift;

0 commit comments

Comments
 (0)