Initial Commit

This commit is contained in:
2018-12-26 18:40:06 +01:00
commit cb892d0d3b
6 changed files with 159 additions and 0 deletions

5
.env.example Normal file
View File

@@ -0,0 +1,5 @@
DOMAIN=the_domain
SUBDOMAIN=dyn
API_USER=servercow_username
API_PASS=servercow_password
API_BASE_URL=https://api.servercow.de/dns/v1/domains/

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
vendor
.env
log.txt

5
README.md Normal file
View File

@@ -0,0 +1,5 @@
# ServerCow DNS API
### How to use:
1. Clone the Project: ```git clone https://git.dev.fabian-thielen.io/fthielen/servercow-dns-api.git```
2. Install the dependency with composer: ```composer install```
3. Change the variables in the .env file

14
composer.json Normal file
View File

@@ -0,0 +1,14 @@
{
"name": "fthielen/servercow_api",
"type": "project",
"description": "PHP API Client for the ServerCow DNS API",
"version": "1.0.0",
"require": {
"vlucas/phpdotenv": "^2.5"
},
"scripts": {
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
}
}

68
composer.lock generated Normal file
View File

@@ -0,0 +1,68 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "c23147c781015eeac4ab37d9dcef94ff",
"packages": [
{
"name": "vlucas/phpdotenv",
"version": "v2.5.1",
"source": {
"type": "git",
"url": "https://github.com/vlucas/phpdotenv.git",
"reference": "8abb4f9aa89ddea9d52112c65bbe8d0125e2fa8e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/8abb4f9aa89ddea9d52112c65bbe8d0125e2fa8e",
"reference": "8abb4f9aa89ddea9d52112c65bbe8d0125e2fa8e",
"shasum": ""
},
"require": {
"php": ">=5.3.9"
},
"require-dev": {
"phpunit/phpunit": "^4.8.35 || ^5.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.5-dev"
}
},
"autoload": {
"psr-4": {
"Dotenv\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Vance Lucas",
"email": "vance@vancelucas.com",
"homepage": "http://www.vancelucas.com"
}
],
"description": "Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.",
"keywords": [
"dotenv",
"env",
"environment"
],
"time": "2018-07-29T20:33:41+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": []
}

64
dyndns.php Normal file
View 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);
}