Last active
June 2, 2021 04:11
-
-
Save tpokorra/73594e6c0d65dbead65a217dd5b541e9 to your computer and use it in GitHub Desktop.
dyndns mit Hostsharing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// zum Aktualisieren der IP Adresse: https://dyndns.example.org/dyndns.php?domain=meinedynamische.example.org | |
if (!isset($_GET['domain'])) { | |
die('Die Angabe der Domain fehlt.'); | |
} | |
$domain = $_GET['domain']; | |
$filename = "/home/pacs/xyz00/users/dyndns/doms/example.org/etc/pri.example.org"; | |
$current = file_get_contents($filename); | |
if (strpos($current, $domain) === false) die("<br/><br/>Fehler: Unbekannte Domain $domain"); | |
function resolve_hostname($domain) { | |
// see http://www.purplepixie.org/phpdns/ for instructions and download of phpdns | |
require("phpdns-1.05/dns.inc.php"); // Require API Source | |
$dns_server="dns1.hostsharing.net"; // Our DNS Server | |
$dns_query=new DNSQuery($dns_server); // create DNS Query object - there are other options we could pass here | |
$question=$domain; // the question we will ask | |
$type="A"; // the type of response(s) we want for this question | |
$result=$dns_query->Query($question,$type); // do the query | |
// Trap Errors | |
if ( ($result===false) || ($dns_query->error!=0) ) // error occured | |
{ | |
echo $dns_query->lasterror; | |
die(); | |
} | |
//Process Results | |
$result_count=$result->count; // number of results returned | |
for ($a=0; $a<$result_count; $a++) | |
{ | |
if ($result->results[$a]->typeid=="A") // only after A records | |
{ | |
//echo $question." has IP address ".$result->results[$a]->data."<br>"; | |
return $result->results[$a]->data; | |
} | |
} | |
$type = "AAAA"; | |
$result=$dns_query->Query($question,$type); // do the query | |
// Trap Errors | |
if ( ($result===false) || ($dns_query->error!=0) ) // error occured | |
{ | |
echo $dns_query->lasterror; | |
die(); | |
} | |
//Process Results | |
$result_count=$result->count; // number of results returned | |
for ($a=0; $a<$result_count; $a++) | |
{ | |
if ($result->results[$a]->typeid=="AAAA") // only after AAAA records | |
{ | |
//echo $question." has IP address ".$result->results[$a]->data."<br>"; | |
return $result->results[$a]->data; | |
} | |
} | |
die("invalid response from dns query"); | |
} | |
$current_ip = gethostbyname($domain); | |
$current_ip = resolve_hostname($domain); | |
$ip = $_SERVER['REMOTE_ADDR']; | |
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { | |
echo "Deine IPv4 Adresse: ".$ip."<br/>"; | |
$A = 'A'; | |
} | |
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { | |
echo "Deine IPv6 Adresse: ".$ip."<br/>"; | |
$A = 'AAAA'; | |
} | |
echo "Momentan ist eingestellt: IP Adresse für ".$domain.": ".$current_ip."<br/>"; | |
echo "Falls die IP Adressen noch nicht übereinstimmen, bitte ein paar Minuten warten, und diese Seite neu laden, um es zu kontrollieren.<br/>"; | |
$content = explode("\n", $current); | |
$output = ''; | |
foreach ($content as $line) | |
{ | |
if (strpos($line, $domain) !== false) { | |
$output .= $domain.". IN $A ".$_SERVER['REMOTE_ADDR']."\n"; | |
} else { | |
$output .= $line."\n"; | |
} | |
} | |
if (trim($current) != trim($output)) { | |
echo "<br/><br/>DNS wurde aktualisiert, bitte noch ein paar Minuten warten!"; | |
file_put_contents($filename, $output); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment