Skip to content

Commit

Permalink
Merge pull request #13 from mrikirill/feature-ipv6-support
Browse files Browse the repository at this point in the history
ipv6 support
  • Loading branch information
mrikirill authored Jul 13, 2021
2 parents 50fa65d + 62373f9 commit 8573957
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions cloudflare.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ function __construct($argv)

$this->apiKey = (string) $argv[2]; // CF Global API Key
$hostname = (string) $argv[3]; // example: example.com.uk---sundomain.example1.com---example2.com
$this->ip = (string) $argv[4];
$this->ip = (string) $this->getIpAddressIpify();

$this->validateIpV4($this->ip);
$this->validateIp($this->ip);

$arHost = explode('---', $hostname);
if (empty($arHost)) {
Expand Down Expand Up @@ -61,9 +61,9 @@ function makeUpdateDNS()
$this->badParam('empty host list');
}

foreach ($this->hostList as $arHost) {
foreach ($this->hostList as $arHost) {
$post = [
'type' => 'A',
'type' => $this->getZoneTypeByIp($this->ip),
'name' => $arHost['fullname'],
'content' => $this->ip,
'ttl' => 1,
Expand All @@ -85,13 +85,30 @@ function badParam($msg = '')
exit();
}

function validateIpV4($ip)
function validateIp($ip)
{
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$this->badParam('invalid ip-address, only ipv4');
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
$this->badParam('invalid ip-address');
}
return true;
}
/*
* get ip from ipify.org
*/
function getIpAddressIpify() {
return file_get_contents('https://api64.ipify.org');
}

/*
* IPv4 = zone A, IPv6 = zone AAAA
* @link https://www.cloudflare.com/en-au/learning/dns/dns-records/dns-a-record/
*/
function getZoneTypeByIp($ip) {
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
return 'AAAA';
}
return 'A';
}

/**
* Set ZoneID for each hosts
Expand Down

0 comments on commit 8573957

Please sign in to comment.