diff --git a/config.dist.php b/config.dist.php index e94429f..eea7875 100644 --- a/config.dist.php +++ b/config.dist.php @@ -28,6 +28,23 @@ // define('HOST', 'server'); +// Enter an URL to use to determine the public IPv4 address. +// [Optional; will be set to default value 'https://get-ipv4.steck.cc' if missing.] +define('IPV4_ADDRESS_URL', 'https://get-ipv4.steck.cc'); + +// Enter an URL to use as fallback to determine the public IPv4 address. +// [Optional; will be set to default value 'https://ipv4.seeip.org' if missing.] +define('IPV4_ADDRESS_URL_FALLBACK', 'https://ipv4.seeip.org'); + +// Enter an URL to use to determine the public IPv6 address. +// [Optional; will be set to default value 'https://get-ipv6.steck.cc' if missing.] +define('IPV6_ADDRESS_URL', 'https://get-ipv6.steck.cc'); + +// Enter an URL to use as fallback to determine the public IPv6 address. +// [Optional; will be set to default value 'https://v6.ident.me' if missing.] +define('IPV6_ADDRESS_URL_FALLBACK', 'https://v6.ident.me'); + + // If set to true, the script will check for your public IPv4 address and add it as an A-Record / change an existing A-Record for the host. // You may want to deactivate this, for example, when using a carrier grade NAT (CGNAT). // Most likely though, you should keep this active, unless you know otherwise. diff --git a/functions.php b/functions.php index 74a814a..4047fff 100644 --- a/functions.php +++ b/functions.php @@ -1,8 +1,8 @@ 1, + CURLOPT_USERAGENT => USERAGENT, CURLOPT_TIMEOUT => 30, CURLOPT_RETURNTRANSFER => 1, CURLOPT_FAILONERROR => 1, @@ -96,6 +97,7 @@ function initializeCurlHandlerGetIP($url) { $ch = curl_init($url); $curlOptions = array( + CURLOPT_USERAGENT => USERAGENT, CURLOPT_TIMEOUT => 30, CURLOPT_RETURNTRANSFER => 1, CURLOPT_FAILONERROR => 1 @@ -149,8 +151,9 @@ function retryCurlRequest($ch, $tryCount, $tryLimit) } // Sends $request to netcup Domain API and returns the result -function sendRequest($request) +function sendRequest($request, $apiSessionRetry = false) { + $ch = initializeCurlHandlerPostNetcupAPI($request); $result = curl_exec($ch); @@ -168,12 +171,29 @@ function sendRequest($request) exit(1); } + $result = json_decode($result, true); + + // Due to a bug in the netcup CCP DNS API, sometimes sessions expire too early (statuscode 4001, error message: "The session id is not in a valid format.") + // We work around this bug by trying to login again once. + // See Github issue #21. + if ($result['statuscode'] === 4001 && $apiSessionRetry === false) { + outputWarning("Received API error 4001: The session id is not in a valid format. Most likely the session expired. Logging in again and retrying once."); + $newApisessionid = login(CUSTOMERNR, APIKEY, APIPASSWORD); + + global $apisessionid; + $apisessionid = $newApisessionid; + + $request = json_decode($request, true); + $request['param']['apisessionid'] = $newApisessionid; + $request = json_encode($request); + + return sendRequest($request, true); + } + // If everything seems to be ok, proceed... curl_close($ch); unset($ch); - $result = json_decode($result, true); - return $result; } @@ -277,9 +297,9 @@ function getCurrentPublicIPv4() return $providedIPv4; } - outputStdout('Getting IPv4 address from API.'); + outputStdout('Getting IPv4 address from ' . IPV4_ADDRESS_URL . '.'); - $url = 'https://api.ipify.org'; + $url = IPV4_ADDRESS_URL; $ch = initializeCurlHandlerGetIP($url); $publicIP = trim(curl_exec($ch)); @@ -292,8 +312,8 @@ function getCurrentPublicIPv4() } if (!isIPV4Valid($publicIP) || $publicIP === false) { - outputWarning("https://api.ipify.org didn't return a valid IPv4 address (Try $retryCount / $retryLimit). Trying fallback API https://ipv4.seeip.org"); - $url = 'https://ipv4.seeip.org'; + outputWarning(IPV4_ADDRESS_URL . " didn't return a valid IPv4 address (Try $retryCount / $retryLimit). Trying fallback " . IPV4_ADDRESS_URL_FALLBACK); + $url = IPV4_ADDRESS_URL_FALLBACK; $ch = initializeCurlHandlerGetIP($url); $publicIP = trim(curl_exec($ch)); if (!wasCurlSuccessful($ch) || !isIPV4Valid($publicIP)) { @@ -324,9 +344,9 @@ function getCurrentPublicIPv6() return $providedIPv6; } - outputStdout('Getting IPv6 address from API.'); + outputStdout('Getting IPv6 address from ' . IPV6_ADDRESS_URL . '.'); - $url = 'https://ipv6.seeip.org'; + $url = IPV6_ADDRESS_URL; $ch = initializeCurlHandlerGetIP($url); $publicIP = trim(curl_exec($ch)); @@ -339,8 +359,8 @@ function getCurrentPublicIPv6() } if (!isIPV6Valid($publicIP) || $publicIP === false) { - outputWarning("https://ipv6.seeip.org didn't return a valid IPv6 address (Try $retryCount / $retryLimit). Trying fallback API https://v6.ident.me/"); - $url = 'https://v6.ident.me/'; + outputWarning(IPV6_ADDRESS_URL . " didn't return a valid IPv6 address (Try $retryCount / $retryLimit). Trying fallback " . IPV6_ADDRESS_URL_FALLBACK); + $url = IPV6_ADDRESS_URL_FALLBACK; $ch = initializeCurlHandlerGetIP($url); $publicIP = trim(curl_exec($ch)); if (!wasCurlSuccessful($ch) || !isIPV6Valid($publicIP)) { diff --git a/update.php b/update.php index 72eba1d..1513c1a 100755 --- a/update.php +++ b/update.php @@ -24,6 +24,22 @@ exit(1); } +if (!defined('IPV4_ADDRESS_URL')) { + define('IPV4_ADDRESS_URL', 'https://get-ipv4.steck.cc'); +} + +if (!defined('IPV4_ADDRESS_URL_FALLBACK')) { + define('IPV4_ADDRESS_URL_FALLBACK', 'https://ipv4.seeip.org'); +} + +if (!defined('IPV6_ADDRESS_URL')) { + define('IPV6_ADDRESS_URL', 'https://get-ipv6.steck.cc'); +} + +if (!defined('IPV6_ADDRESS_URL_FALLBACK')) { + define('IPV6_ADDRESS_URL_FALLBACK', 'https://v6.ident.me/'); +} + if (USE_IPV4 === true) { // Get current IPv4 address if (!$publicIPv4 = getCurrentPublicIPv4()) {