125 lines
3.2 KiB
PHP
Executable File
125 lines
3.2 KiB
PHP
Executable File
<?php
|
|
|
|
//
|
|
// This script updates the DNS type A on porkbun.com so it is always pointing to your public IP address.
|
|
//
|
|
// Dependencies:
|
|
//
|
|
// - PorkbunAPI.php
|
|
// - pb-dydns.json (it will be created automatically the 1st time)
|
|
//
|
|
// How to use it:
|
|
//
|
|
// - from command line:
|
|
//
|
|
// $ php /path/to/pb-dydns.php domain_name
|
|
//
|
|
// It will retrieve all DNS records for the specificied domain_name and update the A type ones
|
|
// with your public IP address.
|
|
//
|
|
//- as a cron job, to add it to:
|
|
//
|
|
// $ crontab -e
|
|
//
|
|
// Append this line as many times as domains you want to automatically update:
|
|
//
|
|
// */10 * * * * php /path/to/pb-dydns.php domain_name > /dev/null
|
|
//
|
|
// And restart cron (I'm not sure if this is necessary):
|
|
//
|
|
// $ sudo systemctl restart cron
|
|
//
|
|
// Entries to a log file called "pb-dydns.log" will be added, to view it you can:
|
|
//
|
|
// $ cat /path/to/pb-dydns.log
|
|
//
|
|
|
|
require "PorkbunAPI.php";
|
|
|
|
// Make sure that pb-dydns.json exists:
|
|
$config_filename = __DIR__ . "/pb-dydns.json";
|
|
|
|
if (!file_exists($config_filename))
|
|
{
|
|
echo "The config file $config_filename does not exist." . PHP_EOL;
|
|
|
|
$config = json_encode(array
|
|
(
|
|
"url" => "https://porkbun.com/api/json/v3/",
|
|
"apikey" => "",
|
|
"secretapikey" => ""
|
|
), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
|
|
|
file_put_contents($config_filename, $config);
|
|
|
|
echo "It has been created but you must edit it with your API key and secret." . PHP_EOL;
|
|
echo "After that you can try again." . PHP_EOL;
|
|
|
|
exit(1);
|
|
}
|
|
|
|
// Domain name must be provided as argument.
|
|
// There should be 2 arguments as $argc includes the script name itself:
|
|
if ($argc != 2)
|
|
{
|
|
echo_to_cli("This script requires a domain name as argument:" . PHP_EOL);
|
|
echo_to_cli("php $argv[0] domain_name" . PHP_EOL);
|
|
|
|
exit(2);
|
|
}
|
|
|
|
// Take the arguments in individual variables:
|
|
$myDomain = $argv[1];
|
|
|
|
// Create an instance of PorkbunAPI:
|
|
$pbapi = new PorkbunAPI($config_filename);
|
|
|
|
// Test connection to Porkbun API in order to get the public IP:
|
|
$result = json_decode($pbapi->ping());
|
|
if ($result->status == "SUCCESS") $myIp = $result->yourIp;
|
|
else
|
|
{
|
|
echo_to_cli("There was an error trying to ping Porkbun." . PHP_EOL);
|
|
var_dump($result);
|
|
exit(3);
|
|
}
|
|
|
|
echo_to_cli("Your public IP address is $myIp" . PHP_EOL);
|
|
|
|
// Retrieve all DNS records associated with user's domain:
|
|
$records = $pbapi->retrieve($myDomain);
|
|
|
|
// Discard those records that are not type "A":
|
|
$data = json_decode($records);
|
|
$filteredRecords = array_filter($data->records, function ($record)
|
|
{
|
|
return $record->type == "A";
|
|
});
|
|
|
|
$log_filename = __DIR__ . "/pb-dydns.log";
|
|
|
|
// Update the records that passed the filter with the public IP:
|
|
foreach ($filteredRecords as $record)
|
|
{
|
|
echo_to_cli("Porkbun's DNS for $record->name is pointing to $record->content... ");
|
|
|
|
if ($record->content != $myIp)
|
|
{
|
|
echo_to_cli("Let's change that... ");
|
|
$name = rtrim(strstr($record->name, $myDomain, true), ".");
|
|
$result = json_decode($pbapi->edit($myDomain, $record->id, $myIp, $name));
|
|
if ($result->status == "SUCCESS") echo_to_cli("Done!" . PHP_EOL);
|
|
|
|
$now = date('Y-m-d H:i:s');
|
|
$message = "$now: Updated DNS on $record->name from $record->content to $myIp" . PHP_EOL;
|
|
file_put_contents($log_filename, $message, FILE_APPEND);
|
|
}
|
|
|
|
else
|
|
{
|
|
echo_to_cli("Nothing needs to be changed!" . PHP_EOL);
|
|
}
|
|
}
|
|
|
|
?>
|