1
0
mirror of https://github.com/EV21/dynb.git synced 2025-12-26 16:39:32 +01:00

1 Commits
0.3.0 ... noip

Author SHA1 Message Date
1f31d943ca add DynDNS2 support for noip.com 2021-11-03 23:24:39 +01:00
3 changed files with 197 additions and 228 deletions

View File

@@ -158,7 +158,7 @@ body_process = ReSub(r'((^|\n)[A-Z]\w+(-\w+)*: .*(\n\s+.*)*)+$', r'') | strip
## ##
## Available constructs are those listed in ``body_process`` doc. ## Available constructs are those listed in ``body_process`` doc.
subject_process = (strip | subject_process = (strip |
ReSub(r'^([cC]hg|[fF]ix|[nN]ew|[dD]oc|[fF]eat|[aA]dd|[cC]hange)\s*:\s*((dev|use?r|pkg|test|doc)\s*:\s*)?([^\n@]*)(@[a-z]+\s+)*$', r'\4') | ReSub(r'^([cC]hg|[fF]ix|[nN]ew)\s*:\s*((dev|use?r|pkg|test|doc)\s*:\s*)?([^\n@]*)(@[a-z]+\s+)*$', r'\4') |
SetIfEmpty("No commit message.") | ucfirst | final_dot) SetIfEmpty("No commit message.") | ucfirst | final_dot)

View File

@@ -1,16 +0,0 @@
name: Run shellcheck with reviewdog
on: [pull_request]
jobs:
shellcheck:
name: runner / shellcheck
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: shellcheck
uses: reviewdog/action-shellcheck@v1.9.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
reporter: github-pr-review
path: "."
pattern: "*.sh"
exclude: "./.git/*"

407
dynb.sh
View File

@@ -85,66 +85,57 @@ reset_color_modification="\e[0m"
REGEX_IPv4="^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$" REGEX_IPv4="^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$"
REGEX_IPv6="^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$" REGEX_IPv6="^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$"
function is_IPv4_address function is_IPv4_address() {
{ if [[ $1 =~ $REGEX_IPv4 ]]
local ip=$1
if [[ $ip =~ $REGEX_IPv4 ]]
then return 0 then return 0
else return 1 else return 1
fi fi
} }
function is_IPv6_address function is_IPv6_address() {
{ if [[ $1 =~ $REGEX_IPv6 ]]
local ip=$1
if [[ $ip =~ $REGEX_IPv6 ]]
then return 0 then return 0
else return 1 else return 1
fi fi
} }
function loopMode function loopMode() {
{ if [[ $_loopMode -eq 1 ]]; then
if [[ $_loopMode -eq 1 ]] return 0
then return 0 else
else return 1 return 1
fi fi
} }
function debugMode function debugMode() {
{ if [[ $_debug -eq 1 ]]; then
if [[ $_debug -eq 1 ]] return 0
then return 0 else
else return 1 return 1
fi fi
} }
function infoMessage function infoMessage() {
{
echo -e "${green_color}$(logtime) INFO: $*${reset_color_modification}" echo -e "${green_color}$(logtime) INFO: $*${reset_color_modification}"
} }
function debugMessage function debugMessage() {
{ if debugMode; then
if debugMode echo -e "${yellow_color}$(logtime) DEBUG: ${*}${reset_color_modification}"
then echo -e "${yellow_color}$(logtime) DEBUG: ${*}${reset_color_modification}"
fi fi
} }
function errorMessage function errorMessage() {
{
echo -e "${red_color_bg}${bold}$(logtime) ERROR: $*${reset_color_modification}" >&2 echo -e "${red_color_bg}${bold}$(logtime) ERROR: $*${reset_color_modification}" >&2
} }
function logtime function logtime() {
{
LOGTIME=$(date "+%Y-%m-%d %H:%M:%S") LOGTIME=$(date "+%Y-%m-%d %H:%M:%S")
echo "[$LOGTIME]" echo "[$LOGTIME]"
} }
# The main domain as an identifier for the dns zone is required for the updateRecord call # The main domain as an identifier for the dns zone is required for the updateRecord call
function getMainDomain function getMainDomain() {
{
request=$( request=$(
echo "{}" | echo "{}" |
jq '(.method="nameserver.list")' | jq '(.method="nameserver.list")' |
@@ -163,8 +154,7 @@ function getMainDomain
_main_domain=$(echo "$_response" | jq --raw-output '.domain') _main_domain=$(echo "$_response" | jq --raw-output '.domain')
} }
function fetchDNSRecords function fetchDNSRecords() {
{
request=$( request=$(
echo "{}" | echo "{}" |
jq '(.method="'nameserver.info'")' | jq '(.method="'nameserver.info'")' |
@@ -188,34 +178,27 @@ function fetchDNSRecords
# requires parameter A or AAAA # requires parameter A or AAAA
# result to stdout # result to stdout
function getRecordID function getRecordID() {
{ echo "$_dns_records" | jq "select(.type == \"${1}\") | .id"
echo "$_dns_records" |
jq "select(.type == \"${1}\") | .id"
} }
# requires parameter A or AAAA # requires parameter A or AAAA
# result to stdout # result to stdout
function getDNSIP() { function getDNSIP() {
echo "$_dns_records" | echo "$_dns_records" | jq --raw-output "select(.type == \"${1}\") | .content"
jq --raw-output "select(.type == \"${1}\") | .content"
} }
# requires parameter # requires parameter
# 1. param: 4 or 6 for ip version # 1. param: 4 or 6 for ip version
# 2. param: IP check server address # 2. param: IP check server address
# result to stdout # result to stdout
function getRemoteIP function getRemoteIP() {
{ if [[ -n $_DNS_checkServer ]]; then
local ip_version=$1
local ip_check_server=$2
if [[ -n $_DNS_checkServer ]]
then
curl --silent "$_interface_str" --user-agent "$_userAgent" \ curl --silent "$_interface_str" --user-agent "$_userAgent" \
--ipv"${ip_version}" --dns-servers "$_DNS_checkServer" --location "${ip_check_server}" --ipv"${1}" --dns-servers "$_DNS_checkServer" --location "${2}"
else else
curl --silent "$_interface_str" --user-agent "$_userAgent" \ curl --silent "$_interface_str" --user-agent "$_userAgent" \
--ipv"${ip_version}" --location "${ip_check_server}" --ipv"${1}" --location "${2}"
fi fi
# shellcheck disable=2181 # shellcheck disable=2181
if [[ $? -gt 0 ]]; then if [[ $? -gt 0 ]]; then
@@ -226,21 +209,16 @@ function getRemoteIP
# requires parameter # requires parameter
# 1. param: 4 or 6 as ip version # 1. param: 4 or 6 as ip version
function updateRecord function updateRecord() {
{ if [[ ${1} == 4 ]]; then
local ip_version=$1
if [[ ${ip_version} == 4 ]]
then
ID=$(getRecordID A) ID=$(getRecordID A)
IP=$_new_IPv4 IP=$_new_IPv4
fi fi
if [[ ${ip_version} == 6 ]] if [[ ${1} == 6 ]]; then
then
ID=$(getRecordID AAAA) ID=$(getRecordID AAAA)
IP=$_new_IPv6 IP=$_new_IPv6
fi fi
if [[ $IP != "" ]] if [[ $IP != "" ]]; then
then
request=$( request=$(
echo "{}" | echo "{}" |
jq '(.method="nameserver.updateRecord")' | jq '(.method="nameserver.updateRecord")' |
@@ -264,8 +242,7 @@ function updateRecord
} }
# using DynDNS2 protocol # using DynDNS2 protocol
function dynupdate function dynupdate() {
{
# default parameter values # default parameter values
myip_str=myip myip_str=myip
myipv6_str=myipv6 myipv6_str=myipv6
@@ -274,6 +251,7 @@ function dynupdate
DESEC_DYNDNS_UPDATE_URL="https://update.dedyn.io/?" DESEC_DYNDNS_UPDATE_URL="https://update.dedyn.io/?"
DUCKDNS_DYNDNS_UPDATE_URL="https://www.duckdns.org/update?domains=$DYNB_DYN_DOMAIN&token=$DYNB_TOKEN&" DUCKDNS_DYNDNS_UPDATE_URL="https://www.duckdns.org/update?domains=$DYNB_DYN_DOMAIN&token=$DYNB_TOKEN&"
DYNV6_DYNDNS_UPDATE_URL="https://dynv6.com/api/update?zone=$DYNB_DYN_DOMAIN&token=$DYNB_TOKEN&" DYNV6_DYNDNS_UPDATE_URL="https://dynv6.com/api/update?zone=$DYNB_DYN_DOMAIN&token=$DYNB_TOKEN&"
NOIP_DYNDNS_UPDATE_URL="https://dynupdate.no-ip.com/nic/update?hostname=$DYNB_DYN_DOMAIN&"
case $DYNB_SERVICE_PROVIDER in case $DYNB_SERVICE_PROVIDER in
inwx* | INWX*) inwx* | INWX*)
@@ -282,6 +260,9 @@ function dynupdate
deSEC* | desec* | dedyn*) deSEC* | desec* | dedyn*)
dyndns_update_url="${DESEC_DYNDNS_UPDATE_URL}" dyndns_update_url="${DESEC_DYNDNS_UPDATE_URL}"
;; ;;
noip* | no-ip*)
dyndns_update_url="$NOIP_DYNDNS_UPDATE_URL"
;;
dynv6*) dynv6*)
dyndns_update_url="${DYNV6_DYNDNS_UPDATE_URL}" dyndns_update_url="${DYNV6_DYNDNS_UPDATE_URL}"
myip_str=ipv4 myip_str=ipv4
@@ -312,7 +293,7 @@ function dynupdate
## request ## ## request ##
case $DYNB_SERVICE_PROVIDER in case $DYNB_SERVICE_PROVIDER in
inwx* | INWX*) inwx* | INWX* | noip* | no-ip*)
_response=$(curl --silent "$_interface_str" \ _response=$(curl --silent "$_interface_str" \
--user-agent "$_userAgent" \ --user-agent "$_userAgent" \
--user "$DYNB_USERNAME":"$DYNB_PASSWORD" \ --user "$DYNB_USERNAME":"$DYNB_PASSWORD" \
@@ -334,6 +315,7 @@ function dynupdate
;; ;;
esac esac
case $_response in case $_response in
good* | OK* | "addresses updated") good* | OK* | "addresses updated")
if [[ $_response == "good 127.0.0.1" ]]; then if [[ $_response == "good 127.0.0.1" ]]; then
@@ -401,14 +383,12 @@ function dynupdate
esac esac
} }
function setStatus function setStatus() {
{
echo "_status=$1; _eventTime=$2; _errorCounter=$3; _statusHostname=$4; _statusUsername=$5; _statusPassword=$6" >/tmp/dynb.status echo "_status=$1; _eventTime=$2; _errorCounter=$3; _statusHostname=$4; _statusUsername=$5; _statusPassword=$6" >/tmp/dynb.status
} }
# handle errors from past update requests # handle errors from past update requests
function checkStatus function checkStatus() {
{
case $_status in case $_status in
nochg*) nochg*)
if [[ _errorCounter -gt 1 ]]; then if [[ _errorCounter -gt 1 ]]; then
@@ -421,7 +401,8 @@ function checkStatus
if [[ "$_statusHostname" == "$DYNB_DYN_DOMAIN" && ("$_statusUsername" == "$DYNB_USERNAME" || $_statusUsername == "$DYNB_TOKEN") ]]; then if [[ "$_statusHostname" == "$DYNB_DYN_DOMAIN" && ("$_statusUsername" == "$DYNB_USERNAME" || $_statusUsername == "$DYNB_TOKEN") ]]; then
errorMessage "Hostname supplied does not exist under specified account, enter new login credentials before performing an additional request." errorMessage "Hostname supplied does not exist under specified account, enter new login credentials before performing an additional request."
return 1 return 1
else rm "$_statusFile" else
rm "$_statusFile"
fi fi
return 0 return 0
;; ;;
@@ -429,7 +410,8 @@ function checkStatus
if [[ "$_statusUsername" == "$DYNB_USERNAME" && "$_statusPassword" == "$DYNB_PASSWORD" ]]; then if [[ "$_statusUsername" == "$DYNB_USERNAME" && "$_statusPassword" == "$DYNB_PASSWORD" ]]; then
errorMessage "Invalid username password combination." errorMessage "Invalid username password combination."
return 1 return 1
else rm "$_statusFile" else
rm "$_statusFile"
fi fi
return 0 return 0
;; ;;
@@ -453,20 +435,20 @@ function checkStatus
;; ;;
911 | 5*) 911 | 5*)
delta=$(($(date +%s) - _eventTime)) delta=$(($(date +%s) - _eventTime))
if [[ $delta -lt 1800 ]] if [[ $delta -lt 1800 ]]; then
then
errorMessage "$_status: The provider currently has an fatal error. DynB will wait for next update until 30 minutes have passed since last request, $(date --date=@$delta -u +%M) minutes already passed." errorMessage "$_status: The provider currently has an fatal error. DynB will wait for next update until 30 minutes have passed since last request, $(date --date=@$delta -u +%M) minutes already passed."
return 1 return 1
else rm "$_statusFile" else
rm "$_statusFile"
fi fi
return 0 return 0
;; ;;
*) *)
if [[ _errorCounter -gt 1 ]] if [[ _errorCounter -gt 1 ]]; then
then
errorMessage "An unknown response code has repeatedly been received. $_response" errorMessage "An unknown response code has repeatedly been received. $_response"
return 1 return 1
else return 0 else
return 0
fi fi
;; ;;
esac esac
@@ -474,28 +456,26 @@ function checkStatus
# requires parameter # requires parameter
# 1. param: 4 or 6 for IP version # 1. param: 4 or 6 for IP version
function ipHasChanged function ipHasChanged() {
{ case ${1} in
local ip_version=$1
case ${ip_version} in
4) 4)
remote_ip=$(getRemoteIP 4 $_ipv4_checker) remote_ip=$(getRemoteIP 4 $_ipv4_checker)
if ! is_IPv4_address "$remote_ip" if ! is_IPv4_address "$remote_ip"
then then
errorMessage "The response from the IP check server is not an IPv4 address: $remote_ip" errorMessage "The response from the IP check server is not an IPv4 address: $remote_ip"
return 1 return 0
fi fi
if [[ $DYNB_UPDATE_METHOD == domrobot ]] if [[ $DYNB_UPDATE_METHOD == domrobot ]]; then
then dns_ip=$(getDNSIP A) dns_ip=$(getDNSIP A)
else else
if [[ -n $_DNS_checkServer ]] if [[ -n $_DNS_checkServer ]]; then
then dig_response=$(dig @"${_DNS_checkServer}" in a +short "$DYNB_DYN_DOMAIN") dig_response=$(dig @"${_DNS_checkServer}" in a +short "$DYNB_DYN_DOMAIN")
else dig_response=$(dig in a +short "$DYNB_DYN_DOMAIN") else
dig_response=$(dig in a +short "$DYNB_DYN_DOMAIN")
fi fi
if [[ $dig_response == ";; connection timed out; no servers could be reached" ]] if [[ $dig_response == ";; connection timed out; no servers could be reached" ]]; then
then
errorMessage "DNS request failed $dig_response" errorMessage "DNS request failed $dig_response"
return 1 return 0
fi fi
# If the dns resolver lists multiple records in the answer section we filter the first line # If the dns resolver lists multiple records in the answer section we filter the first line
# using short option "-n" and not "--lines" because of alpines limited BusyBox head command # using short option "-n" and not "--lines" because of alpines limited BusyBox head command
@@ -509,20 +489,19 @@ function ipHasChanged
if ! is_IPv6_address "$remote_ip" if ! is_IPv6_address "$remote_ip"
then then
errorMessage "The response from the IP check server is not an IPv6 address: $remote_ip" errorMessage "The response from the IP check server is not an IPv6 address: $remote_ip"
return 1 return 0
fi fi
if [[ $DYNB_UPDATE_METHOD == domrobot ]] if [[ $DYNB_UPDATE_METHOD == domrobot ]]; then
then dns_ip=$(getDNSIP AAAA) dns_ip=$(getDNSIP AAAA)
else else
if [[ -n $_DNS_checkServer ]] if [[ -n $_DNS_checkServer ]]; then
then dig_response=$(dig @"${_DNS_checkServer}" in aaaa +short "$DYNB_DYN_DOMAIN") dig_response=$(dig @"${_DNS_checkServer}" in aaaa +short "$DYNB_DYN_DOMAIN")
else dig_response=$(dig in aaaa +short "$DYNB_DYN_DOMAIN") else
dig_response=$(dig in aaaa +short "$DYNB_DYN_DOMAIN")
fi fi
exitcode=$? if [[ $dig_response == ";; connection timed out; no servers could be reached" ]]; then
if [[ $exitcode -gt 0 ]] errorMessage "DNS request failed $dig_response"
then return 0
errorMessage "DNS request failed with exit code: $exitcode $dig_response"
return 1
fi fi
# If the dns server lists multiple records in the answer section we filter the first line # If the dns server lists multiple records in the answer section we filter the first line
dns_ip=$(echo "$dig_response" | head -n 1) dns_ip=$(echo "$dig_response" | head -n 1)
@@ -533,133 +512,138 @@ function ipHasChanged
*) ;; *) ;;
esac esac
if [[ "$remote_ip" == "$dns_ip" ]] if [[ "$remote_ip" == "$dns_ip" ]]; then
then return 1 return 0
else else
case ${ip_version} in case ${1} in
4) infoMessage "New IPv4: $_new_IPv4 old was: $dns_ip";; 4) infoMessage "New IPv4: $_new_IPv4 old was: $dns_ip";;
6) infoMessage "New IPv6: $_new_IPv6 old was: $dns_ip";; 6) infoMessage "New IPv6: $_new_IPv6 old was: $dns_ip";;
esac esac
return 0 return 1
fi fi
} }
function handleParameters ################
{ ## parameters ##
################
function handleParameters() {
# shellcheck disable=SC2154 # shellcheck disable=SC2154
if [[ $_arg_version == "on" ]] if [[ $_arg_version == "on" ]]; then
then echo $_version; exit 0 echo $_version
exit 0
fi fi
# shellcheck disable=SC2154 # shellcheck disable=SC2154
if [[ $_arg_link == "on" ]] if [[ $_arg_link == "on" ]]; then
then ln --verbose --symbolic "$(realpath "$0")" "$HOME/.local/bin/dynb"; exit 0 ln --verbose --symbolic "$(realpath "$0")" "$HOME/.local/bin/dynb"
exit 0
fi fi
# shellcheck disable=SC2154 # shellcheck disable=SC2154
if [[ $_arg_reset == "on" ]] && test -f "$_statusFile" if [[ $_arg_reset == "on" ]]; then
then rm --verbose "$_statusFile"; exit 0 rm --verbose "$_statusFile"
exit 0
fi fi
# shellcheck disable=SC2154 # shellcheck disable=SC2154
if [[ $_arg_debug == "on" ]] if [[ $_arg_debug == "on" ]]; then
then _debug=1 _debug=1
fi fi
# shellcheck disable=SC2154 # shellcheck disable=SC2154
if [[ $_arg_update_method != "" ]] if [[ $_arg_update_method != "" ]]; then
then DYNB_UPDATE_METHOD=$_arg_update_method DYNB_UPDATE_METHOD=$_arg_update_method
fi fi
# shellcheck disable=SC2154 # shellcheck disable=SC2154
if [[ $_arg_ip_mode != "" ]] if [[ $_arg_ip_mode != "" ]]; then
then DYNB_IP_MODE=$_arg_ip_mode DYNB_IP_MODE=$_arg_ip_mode
fi fi
# shellcheck disable=SC2154 # shellcheck disable=SC2154
if [[ $_arg_domain != "" ]] if [[ $_arg_domain != "" ]]; then
then DYNB_DYN_DOMAIN=$_arg_domain DYNB_DYN_DOMAIN=$_arg_domain
fi fi
# shellcheck disable=SC2154 # shellcheck disable=SC2154
if [[ $_arg_service_provider != "" ]] if [[ $_arg_service_provider != "" ]]; then
then DYNB_SERVICE_PROVIDER=$_arg_service_provider DYNB_SERVICE_PROVIDER=$_arg_service_provider
fi fi
# shellcheck disable=SC2154 # shellcheck disable=SC2154
if [[ $_arg_username != "" ]] if [[ $_arg_username != "" ]]; then
then DYNB_USERNAME=$_arg_username DYNB_USERNAME=$_arg_username
fi fi
# shellcheck disable=SC2154 # shellcheck disable=SC2154
if [[ $_arg_password != "" ]] if [[ $_arg_password != "" ]]; then
then DYNB_PASSWORD=$_arg_password DYNB_PASSWORD=$_arg_password
fi fi
# shellcheck disable=SC2154 # shellcheck disable=SC2154
if [[ $_arg_token != "" ]] if [[ $_arg_token != "" ]]; then
then DYNB_TOKEN=$_arg_token DYNB_TOKEN=$_arg_token
fi fi
# shellcheck disable=SC2154 # shellcheck disable=SC2154
if [[ $_arg_interval != "" ]] if [[ $_arg_interval != "" ]]; then
then DYNB_INTERVAL=$_arg_interval DYNB_INTERVAL=$_arg_interval
fi fi
if [[ -z $DYNB_INTERVAL ]] if [[ -z $DYNB_INTERVAL ]]; then
then _loopMode=0 _loopMode=0
elif [[ $DYNB_INTERVAL -lt _minimum_looptime ]] elif [[ $DYNB_INTERVAL -lt _minimum_looptime ]]; then
then
DYNB_INTERVAL=$_minimum_looptime DYNB_INTERVAL=$_minimum_looptime
_loopMode=1 _loopMode=1
else _loopMode=1 else
_loopMode=1
fi fi
if [[ $_network_interface != "" ]] if [[ $_network_interface != "" ]]; then
then _interface_str="--interface $_network_interface" _interface_str="--interface $_network_interface"
fi fi
if [[ $DYNB_IP_MODE == d* ]] if [[ $DYNB_IP_MODE == d* ]]; then
then
_is_IPv4_enabled=true _is_IPv4_enabled=true
_is_IPv6_enabled=true _is_IPv6_enabled=true
fi fi
if [[ $DYNB_IP_MODE == *4* ]] if [[ $DYNB_IP_MODE == *4* ]]; then
then _is_IPv4_enabled=true _is_IPv4_enabled=true
fi fi
if [[ $DYNB_IP_MODE == *6* ]] if [[ $DYNB_IP_MODE == *6* ]]; then
then _is_IPv6_enabled=true _is_IPv6_enabled=true
fi fi
if [[ $DYNB_DEBUG == true ]] if [[ $DYNB_DEBUG == true ]]; then
then _debug=1 _debug=1
fi fi
# shellcheck disable=SC2154 # shellcheck disable=SC2154
if [[ -n $DYNB_IPv4_CHECK_SITE ]] if [[ -n $DYNB_IPv4_CHECK_SITE ]]; then
then _ipv4_checker=$DYNB_IPv4_CHECK_SITE _ipv4_checker=$DYNB_IPv4_CHECK_SITE
fi fi
# shellcheck disable=SC2154 # shellcheck disable=SC2154
if [[ -n $DYNB_IPv6_CHECK_SITE ]] if [[ -n $DYNB_IPv6_CHECK_SITE ]]; then
then _ipv6_checker=$DYNB_IPv6_CHECK_SITE _ipv6_checker=$DYNB_IPv6_CHECK_SITE
fi fi
if [[ -n $DYNB_DNS_CHECK_SERVER ]] if [[ -n $DYNB_DNS_CHECK_SERVER ]]; then
then _DNS_checkServer=$DYNB_DNS_CHECK_SERVER _DNS_checkServer=$DYNB_DNS_CHECK_SERVER
fi fi
return 0 return 0
} }
function checkDependencies ##################
{ ## dependencies ##
##################
function checkDependencies() {
failCounter=0 failCounter=0
for i in curl dig; do for i in curl dig; do
if ! command -v $i >/dev/null 2>&1 if ! command -v $i >/dev/null 2>&1; then
then
errorMessage "could not find \"$i\", DynB depends on it. " errorMessage "could not find \"$i\", DynB depends on it. "
((failCounter++)) ((failCounter++))
fi fi
done done
[[ -x $(command -v jq 2>/dev/null) ]] || { [[ -x $(command -v jq 2>/dev/null) ]] || {
if [[ $DYNB_UPDATE_METHOD != dyndns* ]] if [[ $DYNB_UPDATE_METHOD != dyndns* ]]; then
then
echo "This script depends on jq and it is not available." >&2 echo "This script depends on jq and it is not available." >&2
((failCounter++)) ((failCounter++))
fi fi
} }
if [[ failCounter -gt 0 ]] if [[ failCounter -gt 0 ]]; then
then exit 1 exit 1
fi fi
} }
function doUnsets function doUnsets() {
{
unset _network_interface unset _network_interface
unset _DNS_checkServer unset _DNS_checkServer
unset _dns_records unset _dns_records
@@ -687,83 +671,83 @@ function doUnsets
unset DYNB_DEBUG unset DYNB_DEBUG
} }
function doDomrobotUpdates function doDomrobotUpdates() {
{
getMainDomain getMainDomain
fetchDNSRecords fetchDNSRecords
if [[ $_is_IPv4_enabled == true ]] if [[ $_is_IPv4_enabled == true ]]; then
then ipHasChanged 4
if ipHasChanged 4 if [[ $? == 1 ]]; then
then updateRecord 4 updateRecord 4
else debugMessage "Skip IPv4 record update, it is already up to date" else
debugMessage "Skip IPv4 record update, it is already up to date"
fi fi
fi fi
if [[ $_is_IPv6_enabled == true ]] if [[ $_is_IPv6_enabled == true ]]; then
then ipHasChanged 6
if ipHasChanged 6 if [[ $? == 1 ]]; then
then updateRecord 6 updateRecord 6
else debugMessage "Skip IPv6 record update, it is already up to date" else
debugMessage "Skip IPv6 record update, it is already up to date"
fi fi
fi fi
} }
function doDynDNS2Updates function doDynDNS2Updates() {
{
changed=0 changed=0
if [[ $_is_IPv4_enabled == true ]] && ipHasChanged 4 if [[ $_is_IPv4_enabled == true ]]; then
then ((changed += 1)) ipHasChanged 4
((changed += $?))
fi fi
if [[ $_is_IPv6_enabled == true ]] && ipHasChanged 6 if [[ $_is_IPv6_enabled == true ]]; then
then ((changed += 1)) ipHasChanged 6
((changed += $?))
fi fi
if [[ $changed -gt 0 ]] if [[ $changed -gt 0 ]]; then
then if checkStatus; then
if checkStatus
then
debugMessage "checkStatus has no errors, try update" debugMessage "checkStatus has no errors, try update"
if dynupdate if dynupdate; then
then debugMessage "DynDNS2 update success" debugMessage "DynDNS2 update success"
else else
debugMessage "Save new status after dynupdate has failed" debugMessage "Save new status after dynupdate has failed"
setStatus "$_response" "$(date +%s)" $((_errorCounter += 1)) "$DYNB_DYN_DOMAIN" "${DYNB_USERNAME}" "${DYNB_PASSWORD}${DYNB_TOKEN}" setStatus "$_response" "$(date +%s)" $((_errorCounter += 1)) "$DYNB_DYN_DOMAIN" "${DYNB_USERNAME}" "${DYNB_PASSWORD}${DYNB_TOKEN}"
fi fi
else debugMessage "Skip DynDNS2 update, checkStatus fetched previous error." else
debugMessage "Skip DynDNS2 update, checkStatus fetched previous error."
fi fi
else debugMessage "Skip DynDNS2 update, IPs are up to date or there is a connection problem" else
debugMessage "Skip DynDNS2 update, IPs are up to date or there is a connection problem"
fi fi
} }
function doUpdates function doUpdates() {
{ if [[ $DYNB_UPDATE_METHOD == "domrobot" ]]; then
if [[ $DYNB_UPDATE_METHOD == "domrobot" ]] doDomrobotUpdates
then doDomrobotUpdates fi
elif [[ $DYNB_UPDATE_METHOD == "dyndns" ]]
then doDynDNS2Updates if [[ $DYNB_UPDATE_METHOD == "dyndns" ]]; then
doDynDNS2Updates
fi fi
} }
function ipv6_is_not_working function ipv6_is_not_working() {
{ curl --ipv6 --head --silent --max-time 1 "$_internet_connectivity_test_server" > /dev/null
curl --ipv6 --head --silent --max-time 5 $_internet_connectivity_test_server > /dev/null
status_code=$? status_code=$?
if test $status_code -gt 0 if [[ $status_code -gt 0 ]]
then return 0 then return 0
else return 1 else return 1
fi fi
} }
function ipv4_is_not_working function ipv4_is_not_working() {
{ curl --ipv4 --head --silent --max-time 1 "$_internet_connectivity_test_server" > /dev/null
curl --ipv4 --head --silent --max-time 5 $_internet_connectivity_test_server > /dev/null
status_code=$? status_code=$?
if test $status_code -gt 0 if [[ $status_code -gt 0 ]]
then return 0 then return 0
else return 1 else return 1
fi fi
} }
function check_internet_connection function check_internet_connection() {
{
if [[ $_is_IPv4_enabled == true ]] if [[ $_is_IPv4_enabled == true ]]
then then
if ipv4_is_not_working if ipv4_is_not_working
@@ -782,26 +766,25 @@ function check_internet_connection
fi fi
} }
function main #################
{ ## MAIN method ##
#################
function dynb() {
# shellcheck disable=SC1091,SC1090 # shellcheck disable=SC1091,SC1090
source "$(dirname "$(realpath "$0")")/dynb-parsing.sh" source "$(dirname "$(realpath "$0")")/dynb-parsing.sh"
# shellcheck source=.env # shellcheck source=.env
if test -f "$_configFile" if test -f "$_configFile"; then
then
# shellcheck disable=SC1091 # shellcheck disable=SC1091
source "$_configFile" source "$_configFile"
else else
alternativeConfig="$(dirname "$(realpath "$0")")/.env" alternativeConfig="$(dirname "$(realpath "$0")")/.env"
if test -f "$alternativeConfig" if test -f "$alternativeConfig"; then
then
# shellcheck disable=SC1091 # shellcheck disable=SC1091
source "$alternativeConfig" source "$alternativeConfig"
fi fi
fi fi
if test -f "$_statusFile" if test -f "$_statusFile"; then
then
debugMessage "read previous status file" debugMessage "read previous status file"
# shellcheck disable=SC1090 # shellcheck disable=SC1090
source "$_statusFile" source "$_statusFile"
@@ -812,19 +795,21 @@ function main
checkDependencies checkDependencies
check_internet_connection check_internet_connection
if loopMode if loopMode; then
then while :; do
while :
do
doUpdates doUpdates
sleep $DYNB_INTERVAL sleep $DYNB_INTERVAL
done done
else doUpdates else
doUpdates
fi fi
doUnsets doUnsets
return 0 return 0
} }
######################
## END MAIN section ##
######################
main "${@}" dynb "${@}"
exit $? exit $?