@@ -27,9 +27,9 @@ function __construct($argv)
2727
2828 $ this ->apiKey = (string ) $ argv [2 ]; // CF Global API Key
2929 $ hostname = (string ) $ argv [3 ]; // example: example.com.uk---sundomain.example1.com---example2.com
30- $ this ->ip = (string ) $ argv [ 4 ] ;
30+ $ this ->ip = (string ) $ this -> getIpAddressIpify () ;
3131
32- $ this ->validateIpV4 ($ this ->ip );
32+ $ this ->validateIp ($ this ->ip );
3333
3434 $ arHost = explode ('--- ' , $ hostname );
3535 if (empty ($ arHost )) {
@@ -61,9 +61,9 @@ function makeUpdateDNS()
6161 $ this ->badParam ('empty host list ' );
6262 }
6363
64- foreach ($ this ->hostList as $ arHost ) {
64+ foreach ($ this ->hostList as $ arHost ) {
6565 $ post = [
66- 'type ' => ' A ' ,
66+ 'type ' => $ this -> getZoneTypeByIp ( $ this -> ip ) ,
6767 'name ' => $ arHost ['fullname ' ],
6868 'content ' => $ this ->ip ,
6969 'ttl ' => 1 ,
@@ -85,13 +85,30 @@ function badParam($msg = '')
8585 exit ();
8686 }
8787
88- function validateIpV4 ($ ip )
88+ function validateIp ($ ip )
8989 {
90- if (!filter_var ($ ip , FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 )) {
91- $ this ->badParam ('invalid ip-address, only ipv4 ' );
90+ if (!filter_var ($ ip , FILTER_VALIDATE_IP )) {
91+ $ this ->badParam ('invalid ip-address ' );
9292 }
9393 return true ;
9494 }
95+ /*
96+ * get ip from ipify.org
97+ */
98+ function getIpAddressIpify () {
99+ return file_get_contents ('https://api64.ipify.org ' );
100+ }
101+
102+ /*
103+ * IPv4 = zone A, IPv6 = zone AAAA
104+ * @link https://www.cloudflare.com/en-au/learning/dns/dns-records/dns-a-record/
105+ */
106+ function getZoneTypeByIp ($ ip ) {
107+ if (filter_var ($ ip , FILTER_VALIDATE_IP , FILTER_FLAG_IPV6 )) {
108+ return 'AAAA ' ;
109+ }
110+ return 'A ' ;
111+ }
95112
96113 /**
97114 * Set ZoneID for each hosts
0 commit comments