Files
update-homelab-ip/project/updateip.php
T

88 lines
2.8 KiB
PHP

<?php
$lines = [];
//$lines[] = "_____________________________";
//$lines[] = "____________start____________";
//$lines[] = "_____________________________";
$dateUpdate = date("Y-m-d H:i:s");
$lines[] = "[$dateUpdate]";
$canUpdate = true;
if (isset($_ENV['LOCAL']) && $_ENV['LOCAL']) {
$lines[] = "die in local";
$canUpdate = false;
}
$needEnvKeys = [
"REAL_PUBLIC_IP_SERVICE","DYNDNS_DOMAIN_UPDATE","DYNDNS_USER","DYNDNS_PASSWORD","DNS_UPDATE_SRC"
];
$emptyEnvKeys = [];
foreach ($needEnvKeys as $needEnvKey) {
if (!isset($_ENV[$needEnvKey])) {
$emptyEnvKeys[] = $needEnvKey;
}
}
if (count($emptyEnvKeys) > 0) {
$canUpdate = false;
$lines[] = "Empty env configuration!";
$lines[] = "The following env configuration keys are missing:\n";
$lines[] = implode(", ", $emptyEnvKeys);
}
if ($canUpdate) {
$toUpdateString = $_ENV['DYNDNS_DOMAIN_UPDATE'];
$user = $_ENV['DYNDNS_USER'];
$pass = $_ENV['DYNDNS_PASSWORD'];
if (!$toUpdateString || !$user || !$pass) {
$lines[] = "Empty ENV data!!!!!!!!!!!";
}
$toUpdateArray = explode(",", $toUpdateString);
$useragent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $_ENV['REAL_PUBLIC_IP_SERVICE']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
$content = curl_exec($ch);
if (curl_errno($ch)) {
$lines[] = "Error:" . curl_error($ch);
$serverIp = false;
} else {
$serverIp = $content;
}
curl_close($ch);
if ($serverIp) {
foreach ($toUpdateArray as $toUpdate) {
$toUpdate = trim($toUpdate);
$currentIp = trim(shell_exec("dig +short @208.67.222.222 $toUpdate A | head -n1"));
if ($currentIp == $serverIp) {
$lines[] = "$toUpdate: same (not update)";
continue;
}
$url = strtr($_ENV['DNS_UPDATE_SRC'],[
"{toUpdateHostname}" => $toUpdate,
"{realPublicIp}" => $serverIp,
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
$response = curl_exec($ch);
$lines[] = "$toUpdate ($currentIp): " . $response;
curl_close($ch);
}
} else {
$lines[] = "Server Error!!!!!!!!!!!!!";
}
}
//$lines[] = "_____________________________";
//$lines[] = "_____________end_____________";
//$lines[] = "_____________________________";
foreach ($lines as $line) {
echo $line . PHP_EOL;
}