Skip to content
This repository was archived by the owner on Jan 18, 2020. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions update.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* https://github.com/mattes/
*
* Copyright 2012 Matthias Kadenbach
* Update 2018 Felix Kretschmer
* Released under the MIT license
*
* Examples:
Expand Down Expand Up @@ -42,10 +43,10 @@
define("IP_HTML_PAGE", ROOT . "ip.html");

// set an username
define("USERNAME", "aladin");
define("USERNAME", "dyndns");

// set a secure password
define("PASSWORD", "magic");
define("PASSWORD", "magicKey");

// lock user after number of failed login attempts
// if user got locked empty data.json manually
Expand Down Expand Up @@ -87,7 +88,7 @@ function send_status_and_exit($code, $text) {
// load and restore values from data file
$data = json_decode(@file_get_contents(DATA_FILE_PATH), true);
if(!$data) {
$data = array("current_ip4addr" => null, "current_ip6addr" => null, "last_update_timestamp" => null, "failed_logins" => 0);
$data = array("domain" => null, "current_ip4addr" => null, "current_ip6addr" => null, "last_update_timestamp" => null, "failed_logins" => 0);
}

// check failed logins and if username or password are not correct
Expand All @@ -108,7 +109,10 @@ function send_status_and_exit($code, $text) {
$_GET["ip4addr"] = "0.0.0.0";
$_GET["ip6addr"] = "0:0:0:0:0:0:0:0";
}


// get the domain
$domain = $_GET["domain"];

// get ip4 and ip6 address
$ip4addr = filter_var((!empty($_GET["ip4addr"]) ? $_GET["ip4addr"] : null), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
$ip6addr = filter_var((!empty($_GET["ip6addr"]) ? $_GET["ip6addr"] : null), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
Expand All @@ -121,30 +125,43 @@ function send_status_and_exit($code, $text) {
if($ip4addr == $data["current_ip4addr"] && ipv(4)) send_status_and_exit(200, "nochg " . $ip4addr);
if($ip6addr == $data["current_ip6addr"] && ipv(6)) send_status_and_exit(200, "nochg " . $ip6addr);

// fritzbox sends $_GET["domain"], too. ignore it.

// write changes to data file
$data["last_update_timestamp"] = time();
$data["current_ip4addr"] = $ip4addr;
$data["current_ip6addr"] = $ip6addr;
$data["domain"] = $domain;

if(!@file_put_contents(DATA_FILE_PATH, json_encode($data))) send_status_and_exit(500, "911 server is unable to write data file");

// create ip html page
if(IP_HTML_PAGE) {
$html = @file_get_contents(ROOT . "ip.template.html");
if($html === false) send_status_and_exit(500, "911 server is unable to read ip.template.html");

$html = str_replace(array("%DOMAIN%"), array($domain), $html);
$html = str_replace(array("%TIME%"), array(date("Y-m-d H:i:s",$data["last_update_timestamp"])), $html);

if(ipv(4)) $html = str_replace(array("%IP%"), array($ip4addr), $html);
if(ipv(6)) $html = str_replace(array("%IP%"), array($ip6addr), $html);
if(ipv(4)) {
$html = str_replace(array("%IPv4%"), array($ip4addr), $html);
} else {
$html = str_replace(array("%IPv4%"), array("no IPv4"), $html);
}
if(ipv(6)) {
$html = str_replace(array("%IPv6%"), array($ip6addr), $html);
} else {
$html = str_replace(array("%IPv6%"), array("no IPv6"), $html);
}

if(!@file_put_contents(IP_HTML_PAGE, $html)) send_status_and_exit(500, "911 server is unable to create ip html page");
}

// ready to broadcast the ip somewhere else ...

// @todo
// multi-tenant
// fix v6 support


// success!
if(ipv(4)) send_status_and_exit(200, "good " . $ip4addr);
if(ipv(6)) send_status_and_exit(200, "good " . $ip6addr);
if(ipv(6)) send_status_and_exit(200, "good " . $ip6addr);