Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ipv6 support #13

Merged
merged 2 commits into from
Jul 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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