Instantly share code, notes, and snippets.

Embed
What would you like to do?
Self hosted Dynamic DNS / No-IP script in PHP
<?php
/**
* Simple Dynamic DNS / No-IP script for Route53
*
* Setup:
* 1) Setup a new Route53 zone to hold your dynamic DNS records (e.g. dynamic.mywebsite.net)
* 2) Make sure this instance has permission to modify this Route53 zone (e.g. instance IAM profile, .aws credentials or ENV variables)
* 3) Upload this PHP script to your instance (make sure vendor files are availalbe by running: composer require aws/aws-sdk-php)
* 4) Put the allowed hostnames in the list below
* 5) Visit this PHP script in a browser from your desired location (e.g. office, home, parents house, friends house) to set the dynamic IP entry
* 6) Include the hostname in the URL like so: https://yourwebsite.com/path/to/remote-ip.php?host=office
* 7) Use the new DNS entry to access your services remotely (e.g. office VPN or home CCTV footage)
*
* Todo: make better
* Todo: auth
*/
/**********************
* START CONFIGURATION
*/
// Your Zone ID from Route53
$hosted_zone_id = 'ABCD12345BCDAF';
// Your Zone name from Route53
$hosted_zone_name = 'dynamic.mywebsite.net';
// Allowed subdomains:
// e.g. allowing 'office' below will let you set 'office.dynamic.mywebsite.net'
// visit https://yourwebsite.com/path/to/remote-ip.php?host=office to set the 'office' IP address.
$allowed_hostnames = array(
'office',
'home',
'parents',
'weatherstation',
);
/*
* END CONFIGURATION
**********************/
ini_set('display_errors',true);
ini_set('error_reporting',E_ALL);
require 'vendor/autoload.php';
use Aws\Route53\Route53Client;
use Aws\Common\Credentials\Credentials;
if(isset($_GET['host']) && in_array($_GET['host'], $allowed_hostnames)){
$hostname = $_GET['host'] . '.' . $hosted_zone_name;
$ip_address = $_SERVER['REMOTE_ADDR'];
if(empty($_POST['go'])){
?>
<form action="" method="post">
<input type="hidden" name="go" value="yes">
Please confirm you wish to change the IP address for this DNS entry: <br/>
<code><?php echo $hostname;?></code> <br/>
to this new IP address: <code><?php echo $ip_address;?></code>
<br/>
<input type="submit" name="yes" value="Confirm">
</form>
<?php
}else{
$client = new Aws\Route53\Route53Client([
'version' => 'latest',
'region' => 'us-east-1',
]);
$result = $client->changeResourceRecordSets(array(
'HostedZoneId' => $hosted_zone_id,
'ChangeBatch' => array(
'Comment' => 'string',
'Changes' => array(
array(
'Action' => 'UPSERT',
'ResourceRecordSet' => array(
'Name' => $hostname,
'Type' => 'A',
'TTL' => 600,
'ResourceRecords' => array(
array(
'Value' => $ip_address,
),
),
),
),
),
),
));
echo "Sweet. <code>$hostname</code> should resolve to IP <code>$ip_address</code> shortly.";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment