Initial draft of njalla support

Draft of changes for making quietreply work.
Added decode_json for njalla.
Fix support for quietreply.
This commit is contained in:
Dimitris Paraskevopoulos 2022-09-14 21:42:19 +03:00
parent 5cf3e295e4
commit a8a25b0239

View file

@ -762,6 +762,17 @@ my %services = (
'zone' => setv(T_FQDN, 1, 0, undef, undef), 'zone' => setv(T_FQDN, 1, 0, undef, undef),
}, },
}, },
'njalla' => {
'updateable' => undef,
'update' => \&nic_njalla_update,
'examples' => \&nic_njalla_examples,
'variables' => {
%{$variables{'service-common-defaults'}},
'login' => setv(T_STRING, 0, 0, 'unused', undef),
'server' => setv(T_FQDNP, 1, 0, 'njal.la', undef),
'quietreply' => setv(T_BOOL, 0, 1, 0, undef)
},
},
'noip' => { 'noip' => {
'updateable' => undef, 'updateable' => undef,
'update' => \&nic_noip_update, 'update' => \&nic_noip_update,
@ -1706,7 +1717,7 @@ sub init_config {
$proto = opt('protocol') if !defined($proto); $proto = opt('protocol') if !defined($proto);
load_sha1_support($proto) if (grep (/^$proto$/, ("freedns", "nfsn"))); load_sha1_support($proto) if (grep (/^$proto$/, ("freedns", "nfsn")));
load_json_support($proto) if (grep (/^$proto$/, ("1984", "cloudflare", "gandi", "godaddy", "hetzner", "yandex", "nfsn"))); load_json_support($proto) if (grep (/^$proto$/, ("1984", "cloudflare", "gandi", "godaddy", "hetzner", "yandex", "nfsn", "njalla")));
if (!exists($services{$proto})) { if (!exists($services{$proto})) {
warning("skipping host: %s: unrecognized protocol '%s'", $h, $proto); warning("skipping host: %s: unrecognized protocol '%s'", $h, $proto);
@ -5041,6 +5052,126 @@ sub nic_nfsn_update {
###################################################################### ######################################################################
######################################################################
## nic_njalla_examples
######################################################################
sub nic_njalla_examples {
return <<"EoEXAMPLE";
o 'njalla'
The 'njalla' protocol is used by DNS service offered by njal.la.
Configuration variables applicable to the 'njalla' protocol are:
protocol=njalla ##
password=service-password ## Generated password for your dynamic DNS record
quietreply=no|yes ## If yes return empty response on success with status 200 but print errors
domain ## subdomain to update, use @ for base domain name, * for catch all
Example ${program}.conf file entries:
## single host update
protocol=njalla \\
password=njal.la-key
quietreply=no
hostname
EoEXAMPLE
}
######################################################################
## nic_njalla_update
##
## written by satrapes
##
## based on https://njal.la/docs/ddns/
## needs this url to update:
## https://njal.la/update?h=host_name&k=domain_password&a=your_ip
## response contains "code 200" on succesful completion
######################################################################
sub nic_njalla_update {
debug("\nnic_njalla_update -------------------");
foreach my $h (@_) {
my $ipv4 = delete $config{$h}{'wantipv4'};
my $ipv6 = delete $config{$h}{'wantipv6'};
my $quietreply = delete $config{$h}{'quietreply'};
my $ip_output = '';
my $url;
$url = "https://$config{$h}{'server'}/update/";
$url .= "?h=";
$url .= $h;
$url .= "&k=";
$url .= $config{$h}{'password'};
my $auto = 1;
foreach my $ip ($ipv4, $ipv6) {
next if (!$ip);
$auto = 0;
my $ipv = ($ip eq ($ipv6 // '')) ? '6' : '4';
my $type = ($ip eq ($ipv6 // '')) ? 'aaaa' : 'a';
$ip_output .= ' IP v' . $ipv . ': '. $ip .',';
$url .= '&' . $type . '=' . $ip;
}
$url .= (($auto eq 1)) ? '&auto' : '';
$url .= (($quietreply eq 1)) ? '&quiet' : '';
info("setting address to%s for %s", ($ip_output eq '') ? ' auto' : $ip_output, $h);
verbose("UPDATE:", "updating %s", $h);
debug("url: %s", $url);
# Try to get URL
my $reply = geturl(proxy => opt('proxy'), url => $url);
$reply =~ qr/{(?:[^{}]*|(?R))*}/mp;
my $response = '';
if ($quietreply) {
$reply =~ qr/^invalid host or key/mp;
$response = ${^MATCH};
} else {
$response = eval {decode_json(${^MATCH})};
}
next if !header_ok($h, $reply);
verbose("After getting url-*-*-*-*-*-*-*-*-*-\n\n");
# verbose($reply);
# verbose($response);
verbose("\n\nAfter printing reply , response.-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-\n\n");
# my @reply = split /\n/, $reply;
# my $returned = pop(@reply);
if ($quietreply) {
if (!$response) {
success("updating %s: good: IP address set to %s", $h, $ip_output);
}
elsif ($response =~ /invalid host or key/) {
# verbose($response);
failed("Invalid host or key");
# success("Invalid host or key");
} else {
# verbose($response);
failed("Unknown response");
# success("Unknown response");
}
} else {
# No response, declare as failed
if (!defined($reply) || !$reply) {
failed("updating %s: Could not connect to %s.", $h, $config{$h}{'server'});
} else {
# Strip header
verbose("\n\n\nNo quietreply\n\n\n");
# info($response);
# verbose($response);
print($response->{status});
print($response->{message});
verbose("\n\n\nNo quietreply after printing messges\n\n\n");
if ($response->{status} == 401 && $response->{message} =~ /invalid host or key/) {
failed("Invalid host or key");
} elsif ($response->{status} == 200 && $response->{message} =~ /record updated/) {
success("updating %s: good: IP address set to %s", $h, $response->{value}->{A});
} else {
failed("Unknown response");
}
}
}
}
}
###################################################################### ######################################################################
## nic_sitelutions_examples ## nic_sitelutions_examples
###################################################################### ######################################################################