Updates to address code review comment.
This commit is contained in:
parent
050b98eca1
commit
70d7ff3f4e
1 changed files with 31 additions and 42 deletions
73
ddclient
73
ddclient
|
|
@ -942,18 +942,12 @@ sub update_nics {
|
|||
if (exists $iplist{$use}{$arg_ip}{$arg_fw}{$arg_if}{$arg_web}{$arg_cmd}) {
|
||||
$ip = $iplist{$use}{$arg_ip}{$arg_fw}{$arg_if}{$arg_web}{$arg_cmd};
|
||||
} else {
|
||||
my $reply = get_ip($use, $h) // '';
|
||||
if (!$reply) {
|
||||
my $ip = get_ip($use, $h) // '';
|
||||
if (!$ip) {
|
||||
warning("unable to determine IP address")
|
||||
if !$daemon || opt('verbose');
|
||||
next;
|
||||
}
|
||||
if (!($ip = ipv4_match($reply))) {
|
||||
if (!($ip = ipv6_match($reply))) {
|
||||
warning("malformed IP address (%s)", $ip);
|
||||
next;
|
||||
}
|
||||
}
|
||||
$iplist{$use}{$arg_ip}{$arg_fw}{$arg_if}{$arg_web}{$arg_cmd} = $ip;
|
||||
}
|
||||
$config{$h}{'wantip'} = $ip;
|
||||
|
|
@ -1900,11 +1894,7 @@ sub check_value {
|
|||
return undef if $value eq "";
|
||||
|
||||
} elsif ($type eq T_IP) {
|
||||
if (!ipv6_match($value)) {
|
||||
return undef if !($value = ipv4_match($value));
|
||||
} else {
|
||||
$value = ipv6_match($value);
|
||||
}
|
||||
return undef if !is_ipv4($value) && !is_ipv6($value);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
|
@ -2166,7 +2156,7 @@ sub get_ip {
|
|||
$arg = '' unless $arg;
|
||||
|
||||
if ($use eq 'ip') {
|
||||
$ip = opt('ip', $h);
|
||||
$reply = opt('ip', $h);
|
||||
$arg = 'ip';
|
||||
|
||||
} elsif ($use eq 'if') {
|
||||
|
|
@ -2263,15 +2253,8 @@ sub get_ip {
|
|||
$skip =~ s/ /\\s/is;
|
||||
$reply =~ s/^.*?${skip}//is;
|
||||
}
|
||||
if (defined($ip)) {
|
||||
# no need to parse $reply
|
||||
} elsif ($ip = ipv4_match($reply)) {
|
||||
# got an IPv4 address
|
||||
} elsif ($ip = ipv6_match($reply)) {
|
||||
# got a IPv6 address
|
||||
} else {
|
||||
warning("found neither ipv4 nor ipv6 address");
|
||||
}
|
||||
$ip //= ipv4_extract($reply) // ipv6_extract($reply);
|
||||
warning("found neither IPv4 nor IPv6 address") if !defined($ip);
|
||||
if ($use ne 'ip' && ($ip // '') eq '0.0.0.0') {
|
||||
$ip = undef;
|
||||
}
|
||||
|
|
@ -2287,17 +2270,19 @@ sub get_ip {
|
|||
######################################################################
|
||||
sub is_ipv4 {
|
||||
my ($value) = @_;
|
||||
return (length($value // '') != 0) && ($value eq (ipv4_match($value) // ''));
|
||||
return (length($value // '') != 0) && ($value eq (ipv4_extract($value) // ''));
|
||||
}
|
||||
|
||||
######################################################################
|
||||
## ipv4_match() extracts the first valid IPv4 address from given string.
|
||||
## ipv4_extract() extracts the first valid IPv4 address from given string.
|
||||
## Accepts leading zeros in the address but removes them in returned value
|
||||
######################################################################
|
||||
sub ipv4_match {
|
||||
(shift // '') =~ /\b((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3} ## first three bytes
|
||||
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\b/xai; ## last one
|
||||
return ($1 // '') =~ s/\b0+\B//gr; ## remove leading zeros
|
||||
sub ipv4_extract{
|
||||
(shift // '') =~ /\b((?:(?<octet>25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3} ## first three bytes
|
||||
(?&octet))\b/xa; ## last one (without period)
|
||||
my $ip = ($1 // '');
|
||||
$ip =~ s/\b0+\B//g; ## remove leading zeros
|
||||
return $ip;
|
||||
}
|
||||
|
||||
######################################################################
|
||||
|
|
@ -2306,18 +2291,18 @@ sub ipv4_match {
|
|||
######################################################################
|
||||
sub is_ipv6 {
|
||||
my ($value) = @_;
|
||||
return (length($value // '') != 0) && ($value eq (ipv6_match($value) // ''));
|
||||
return (length($value // '') != 0) && ($value eq (ipv6_extract($value) // ''));
|
||||
}
|
||||
|
||||
######################################################################
|
||||
## ipv6_match() extracts the first valid IPv6 address from given string
|
||||
## ipv6_extract() extracts the first valid IPv6 address from given string
|
||||
## IPv6 must be in standard or compressed format.
|
||||
## Mixed IPv6/IPv4 not supported.
|
||||
## Accepts leading zeros in the address but removes them in returned value
|
||||
######################################################################
|
||||
sub ipv6_match {
|
||||
(shift // '') =~ /(?<![:.\w]) ## Negative Look behind for word boundry
|
||||
( (?:[0-9A-F]{1,4}:){7}[0-9A-F]{1,4} ## Standard format
|
||||
sub ipv6_extract{
|
||||
(shift // '') =~ /(?<![:.\w]) ## Negative lookbehind for word boundry
|
||||
( (?:[0-9A-F]{1,4}:){7}[0-9A-F]{1,4} ## Is in standard format
|
||||
|(?=(?:[0-9A-F]{0,4}:){0,7}[0-9A-F]{0,4} ## OR compressed with at most 7 colons
|
||||
(?![:.\w])) ## and negative lookahead for boundry
|
||||
(?:(?:[0-9A-F]{1,4}:){1,7}|:)(?:(?::[0-9A-F]{1,4}){1,7}|:) ## and only 1 double-colon
|
||||
|
|
@ -2325,18 +2310,20 @@ sub ipv6_match {
|
|||
|:(?::[0-9A-F]{1,4}){7} ## OR compressed with 8 colons (double at front)
|
||||
) ## End of first capture group
|
||||
(?![:.\w])/xai; ## Negative lookahead for boundry
|
||||
return ($1 // '') =~ s/\b0+\B//gr; ## Remove leading zeros
|
||||
my $ip = ($1 // '');
|
||||
$ip =~ s/\b0+\B//g; ## remove leading zeros
|
||||
return $ip;
|
||||
}
|
||||
|
||||
######################################################################
|
||||
## ipv6_match_gua() extracts the first valid IPv6 Global Unicast Address (GUA)
|
||||
## ipv6_extract_gua() extracts the first valid IPv6 Global Unicast Address (GUA)
|
||||
## from given string. IPv6 must be in standard or compressed format.
|
||||
## Mixed IPv6/IPv4 not supported.
|
||||
## Accepts leading zeros in the address but removes them in returned value
|
||||
######################################################################
|
||||
sub ipv6_match_gua {
|
||||
(shift // '') =~ /(?<![:.\w]) ## Negative Look behind for word boundry
|
||||
( [23][A-F0-9]{3}: ## Starts 2xxx: or 3xxx:
|
||||
sub ipv6_extract_gua {
|
||||
(shift // '') =~ /(?<![:.\w]) ## Negative lookbehind for word boundry
|
||||
( [23][A-F0-9]{3}: ## Starts with 2xxx: or 3xxx:
|
||||
(?:[0-9A-F]{1,4}:){6}[0-9A-F]{1,4} ## in standard format
|
||||
|(?=[23][A-F0-9]{3}: ## OR starts with 2xxx: or 3xxx:
|
||||
(?:[0-9A-F]{0,4}:){0,6}[0-9A-F]{0,4} ## in compressed with at most 7 colons
|
||||
|
|
@ -2346,7 +2333,9 @@ sub ipv6_match_gua {
|
|||
(?:[0-9A-F]{1,4}:){6}: ## compressed with 8 colons (double at end)
|
||||
) ## End of first capture group
|
||||
(?![:.\w])/xai; ## Negative lookahead for boundry
|
||||
return ($1 // '') =~ s/\b0+\B//gr; ## Remove leading zeros
|
||||
my $ip = ($1 // '');
|
||||
$ip =~ s/\b0+\B//g; ## remove leading zeros
|
||||
return $ip;
|
||||
}
|
||||
|
||||
######################################################################
|
||||
|
|
@ -4254,7 +4243,7 @@ sub nic_nsupdate_update {
|
|||
my $zone = $config{$h}{'zone'};
|
||||
my $ip = $config{$h}{'wantip'};
|
||||
my $recordtype = '';
|
||||
if (ipv6_match($ip)) {
|
||||
if (is_ipv6($ip)) {
|
||||
$recordtype = 'AAAA';
|
||||
} else {
|
||||
$recordtype = 'A';
|
||||
|
|
@ -4403,7 +4392,7 @@ sub nic_cloudflare_update {
|
|||
|
||||
# Get DNS record ID
|
||||
$url = "https://$config{$key}{'server'}/zones/$zone_id/dns_records?";
|
||||
if (ipv6_match($ip)) {
|
||||
if (is_ipv6($ip)) {
|
||||
$url .= "type=AAAA&name=$domain";
|
||||
} else {
|
||||
$url .= "type=A&name=$domain";
|
||||
|
|
|
|||
Loading…
Reference in a new issue