Initial Commit
This commit is contained in:
64
dyndns.php
Normal file
64
dyndns.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . "/vendor/autoload.php";
|
||||
|
||||
$oDotEnv = new Dotenv\Dotenv(__DIR__);
|
||||
$oDotEnv->load();
|
||||
|
||||
$sDynIP = gethostbyname(getenv("SUBDOMAIN") . "." . getenv("DOMAIN"));
|
||||
$sExternalIP = file_get_contents("http://ipv4.icanhazip.com");
|
||||
$sExternalIP = filter_var($sExternalIP, FILTER_SANITIZE_STRING);
|
||||
$sExternalIP = trim(preg_replace('/\s+/', '', $sExternalIP));
|
||||
|
||||
if ($sDynIP != $sExternalIP) {
|
||||
echo "IP has changed... changing ip from {$sDynIP} to {$sExternalIP}...\n";
|
||||
updateRecord(getenv("DOMAIN"), getenv("SUBDOMAIN"), "A", 60, $sExternalIP, $sDynIP);
|
||||
}
|
||||
|
||||
function updateRecord($sDomain, $sSubDomain, $sType = "A", $iTTL = 20, $sContent, $sOldIp)
|
||||
{
|
||||
try {
|
||||
$oCurl = curl_init();
|
||||
$aData = [
|
||||
"type" => $sType,
|
||||
"name" => $sSubDomain,
|
||||
"content" => $sContent,
|
||||
"ttl" => $iTTL,
|
||||
];
|
||||
|
||||
$oPayload = json_encode($aData);
|
||||
|
||||
curl_setopt_array($oCurl, [
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_URL => getenv("API_BASE_URL") . $sDomain,
|
||||
CURLOPT_HTTPHEADER => [
|
||||
'X-Auth-Username: ' . getenv("API_USER"),
|
||||
'X-Auth-Password: ' . getenv("API_PASS"),
|
||||
'Content-Type: application/json'
|
||||
],
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_POSTFIELDS => $oPayload,
|
||||
CURLINFO_HEADER_OUT => true,
|
||||
]);
|
||||
|
||||
$oResponse = curl_exec($oCurl);
|
||||
$aResponse = json_decode($oResponse);
|
||||
|
||||
curl_close($oCurl);
|
||||
|
||||
if ($aResponse->message == "ok") {
|
||||
logToFile("Changed IP from {$sOldIp} to {$sContent}!");
|
||||
} else {
|
||||
logToFile("Error: " . serialize($aResponse));
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
logToFile("Exception: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
function logToFile($sMessage)
|
||||
{
|
||||
$file = fopen("log.txt", "a+");
|
||||
fwrite($file, "[" . date("Y-m-d H:i:s") . "] " . $sMessage);
|
||||
fclose($file);
|
||||
}
|
||||
Reference in New Issue
Block a user