alydns.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. date_default_timezone_set("GMT");
  3. //这两个值需要去阿里云申请
  4. define("accessKeyId", "");
  5. define("accessSecrec", "");
  6. /*
  7. //$obj = new AliDns(accessKeyId, accessSecrec, "newyingyong.cn");
  8. //显示所有
  9. //$data = $obj->DescribeDomainRecords();
  10. //增加解析
  11. //$data= $obj->AddDomainRecord("TXT", "test", "test");
  12. //修改解析
  13. //$data = $obj->UpdateDomainRecord("3965724468724736","TXT", "test", "test2");
  14. //删除解析
  15. //$data = $obj->DescribeDomainRecords();
  16. //$data = $data["DomainRecords"]["Record"];
  17. //if (is_array($data)) {
  18. //foreach ($data as $v) {
  19. //if ($v["RR"] == "test") {
  20. //$obj->DeleteDomainRecord($v["RecordId"]);
  21. //}
  22. //}
  23. //}
  24. */
  25. /*
  26. example:
  27. php alydns.php "simplehttps.com" "test" "test2"
  28. */
  29. ########## 配合 cerbot 运行
  30. echo $argv[1] . "-" . $argv[2] . "-" . $argv[3];
  31. $domainarray = AliDns::getDomain($argv[1]);
  32. $selfdomain = ($domainarray[0]=="")?$argv[2]:$argv[2] . "." . $domainarray[0];
  33. $obj = new AliDns(accessKeyId, accessSecrec, $domainarray[1]);
  34. $data = $obj->DescribeDomainRecords();
  35. $data = $data["DomainRecords"]["Record"];
  36. if (is_array($data)) {
  37. foreach ($data as $v) {
  38. if ($v["RR"] == $selfdomain) {
  39. $res = $obj->DeleteDomainRecord($v["RecordId"]);
  40. }
  41. }
  42. }
  43. $res = $obj->AddDomainRecord("TXT", $selfdomain,$argv[3]);
  44. ############ Class 定义
  45. class AliDns {
  46. private $accessKeyId = null;
  47. private $accessSecrec = null;
  48. private $DomainName = null;
  49. public function __construct($accessKeyId, $accessSecrec, $domain) {
  50. $this->accessKeyId = $accessKeyId;
  51. $this->accessSecrec = $accessSecrec;
  52. $this->DomainName = $domain;
  53. }
  54. /*
  55. 根据域名返回主机名和二级域名
  56. */
  57. public static function getDomain($domain) {
  58. //https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains
  59. //常见根域名
  60. $arr[]=".co.jp";
  61. $arr[]=".com.tw";
  62. $arr[]=".net";
  63. $arr[]=".com";
  64. $arr[]=".com.cn";
  65. $arr[]=".org";
  66. $arr[]=".cn";
  67. $arr[]=".gov";
  68. $arr[]=".net.cn";
  69. $arr[]=".io";
  70. $arr[]=".top";
  71. $arr[]=".me";
  72. $arr[]=".int";
  73. $arr[]=".edu";
  74. //二级域名
  75. $seconddomain ="";
  76. //子域名
  77. $selfdomain = "";
  78. //根域名
  79. $rootdomain = "";
  80. foreach ($arr as $k=>$v) {
  81. $pos = stripos($domain,$v);
  82. if ($pos) {
  83. $rootdomain = substr($domain,$pos);
  84. $s = explode(".",substr($domain,0,$pos));
  85. $seconddomain = $s[count($s)-1] . $rootdomain;
  86. for ($i=0;$i<count($s)-1;$i++)
  87. $selfdomain .= $s[$i];
  88. break;
  89. }
  90. }
  91. //echo $seconddomain ;exit;
  92. if ($rootdomain=="") {
  93. $seconddomain = $domain;
  94. $selfdomain = "";
  95. }
  96. return array($selfdomain,$seconddomain);
  97. }
  98. public function DescribeDomainRecords() {
  99. $requestParams = array(
  100. "Action" => "DescribeDomainRecords"
  101. );
  102. $val = $this->send($requestParams);
  103. return $this->out($val);
  104. }
  105. public function UpdateDomainRecord($id, $type, $rr,$value){
  106. $requestParams = array(
  107. "Action" => "UpdateDomainRecord",
  108. "RecordId" => $id,
  109. "RR" => $rr,
  110. "Type" => $type,
  111. "Value" => $value,
  112. );
  113. $val = $this->send($requestParams);
  114. return $this->out($val);
  115. }
  116. public function DeleteDomainRecord($id) {
  117. $requestParams = array(
  118. "Action" => "DeleteDomainRecord",
  119. "RecordId" => $id,
  120. );
  121. $val = $this->send($requestParams);
  122. return $this->out($val);
  123. }
  124. public function AddDomainRecord($type, $rr, $value) {
  125. $requestParams = array(
  126. "Action" => "AddDomainRecord",
  127. "RR" => $rr,
  128. "Type" => $type,
  129. "Value" => $value,
  130. );
  131. $val = $this->send($requestParams);
  132. return $this->out($val);
  133. }
  134. private function send($requestParams) {
  135. $publicParams = array(
  136. "DomainName" => $this->DomainName,
  137. "Format" => "JSON",
  138. "Version" => "2015-01-09",
  139. "AccessKeyId" => $this->accessKeyId,
  140. "Timestamp" => date("Y-m-d\TH:i:s\Z"),
  141. "SignatureMethod" => "HMAC-SHA1",
  142. "SignatureVersion" => "1.0",
  143. "SignatureNonce" => substr(md5(rand(1, 99999999)), rand(1, 9), 14),
  144. );
  145. $params = array_merge($publicParams, $requestParams);
  146. $params['Signature'] = $this->sign($params, $this->accessSecrec);
  147. $uri = http_build_query($params);
  148. $url = 'http://alidns.aliyuncs.com/?'.$uri;
  149. return $this->curl($url);
  150. }
  151. private function sign($params, $accessSecrec, $method = "GET") {
  152. ksort($params);
  153. $stringToSign = strtoupper($method).'&'.$this->percentEncode('/').'&';
  154. $tmp = "";
  155. foreach($params as $key => $val){
  156. $tmp .= '&'.$this->percentEncode($key).'='.$this->percentEncode($val);
  157. }
  158. $tmp = trim($tmp, '&');
  159. $stringToSign = $stringToSign.$this->percentEncode($tmp);
  160. $key = $accessSecrec.'&';
  161. $hmac = hash_hmac("sha1", $stringToSign, $key, true);
  162. return base64_encode($hmac);
  163. }
  164. private function percentEncode($value = null){
  165. $en = urlencode($value);
  166. $en = str_replace("+", "%20", $en);
  167. $en = str_replace("*", "%2A", $en);
  168. $en = str_replace("%7E", "~", $en);
  169. return $en;
  170. }
  171. private function curl($url) {
  172. $ch = curl_init();
  173. curl_setopt($ch, CURLOPT_URL, $url );
  174. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
  175. $result = curl_exec ($ch);
  176. curl_close($ch);
  177. return $result;
  178. }
  179. private function out($msg) {
  180. return json_decode($msg, true);
  181. }
  182. }