支持GoDaddy

This commit is contained in:
ywdblog 2019-01-11 11:15:52 +08:00
parent 5674f18cef
commit b5ef24d053
4 changed files with 192 additions and 1 deletions

View File

@ -8,7 +8,7 @@
certbot 提供了一个 hook可以编写一个 Shell 脚本,让脚本调用 DNS 服务商的 API 接口,动态添加 TXT 记录,这样就无需人工干预了。
在 certbot 官方提供的插件和 hook 例子中,都没有针对国内 DNS 服务器的样例,所以我编写了这样一个工具,目前支持阿里云 DNS 和腾讯云 DNS
在 certbot 官方提供的插件和 hook 例子中,都没有针对国内 DNS 服务器的样例,所以我编写了这样一个工具,目前支持阿里云 DNS、腾讯云 DNS、GoDaddycertbot 官方没有对应的插件)
### 自动申请通配符证书

View File

@ -91,6 +91,9 @@ class AliDns {
$arr[]=".me";
$arr[]=".int";
$arr[]=".edu";
$arr[]=".link";
$arr[]=".uk";
$arr[]=".hk";
//二级域名
$seconddomain ="";

22
augodaddy.sh Normal file
View File

@ -0,0 +1,22 @@
#!/bin/bash
# godaddy DNS Hook
PATH=$(cd `dirname $0`; pwd)
echo $PATH"/godaddydns.php"
# 调用 PHP 脚本,自动设置 DNS TXT 记录。
# 第一个参数:需要为那个域名设置 DNS 记录
# 第二个参数:需要为具体那个 RR 设置
# 第三个参数: letsencrypt 动态传递的 RR 值
echo $CERTBOT_DOMAIN"_acme-challenge"$CERTBOT_VALIDATION
/usr/bin/php $PATH"/godaddydns.php" $CERTBOT_DOMAIN "_acme-challenge" $CERTBOT_VALIDATION >"/var/log/certdebug.log"
# DNS TXT 记录刷新时间
/bin/sleep 20
echo "END"
###

166
godaddydns.php Normal file
View File

@ -0,0 +1,166 @@
<?php
date_default_timezone_set("GMT");
//accessKeyId 和 accessSecrec 在 https://developer.godaddy.com/getstarted 申请
define("accessKeyId", "");
define("accessSecrec", "");
$type = 'TXT';
$domainarray = GodaddyDns::getDomain($argv[1]);
//证书申请域名
$selfdomain = ($domainarray[0] == "") ? $argv[2] : $argv[2].".".$domainarray[0];
//根域名
$domain = $domainarray[1];
$obj = new GodaddyDns(accessKeyId, accessSecrec, $domain);
$data = $obj->GetDNSRecord($domain, $type);
$code = $data['httpCode'];
if ($code != 200) {
echo 'code='.$code;
echo '<br/>';
echo $data['result'];
exit;
}
$data_obj = json_decode($data['result']);
$count = count($data_obj);
if ($count <= 0) {
$r = $obj->CreateDNSRecord($domain, $selfdomain, $argv[3], $type);
} else {
$r = $obj->UpdateDNSRecord($domain, $selfdomain, $argv[3], $type); //$domain,$name,$value,$recordType='TXT
}
class GodaddyDns
{
private $accessKeyId = null;
private $accessSecrec = null;
private $DomainName = null;
private $Host = "";
private $Path = "";
public function __construct($accessKeyId, $accessSecrec, $domain = "")
{
$this->accessKeyId = $accessKeyId;
$this->accessSecrec = $accessSecrec;
$this->DomainName = $domain;
}
/*
根据域名返回主机名和二级域名
*/
public static function getDomain($domain)
{
//常见根域名 【https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains】
// 【http://www.seobythesea.com/2006/01/googles-most-popular-and-least-popular-top-level-domains/】
$arr[] = ".co.jp";
$arr[] = ".com.tw";
$arr[] = ".net";
$arr[] = ".com";
$arr[] = ".com.cn";
$arr[] = ".org";
$arr[] = ".cn";
$arr[] = ".gov";
$arr[] = ".net.cn";
$arr[] = ".io";
$arr[] = ".top";
$arr[] = ".me";
$arr[] = ".int";
$arr[] = ".edu";
$arr[] = ".link";
$arr[] = ".uk";
$arr[] = ".hk";
//二级域名
$seconddomain = "";
//子域名
$selfdomain = "";
//根域名
$rootdomain = "";
foreach ($arr as $k => $v) {
$pos = stripos($domain, $v);
if ($pos) {
$rootdomain = substr($domain, $pos);
$s = explode(".", substr($domain, 0, $pos));
$seconddomain = $s[count($s) - 1].$rootdomain;
for ($i = 0; $i < count($s) - 1; $i++)
$selfdomain .= $s[$i];
break;
}
}
//echo $seconddomain ;exit;
if ($rootdomain == "") {
$seconddomain = $domain;
$selfdomain = "";
}
return array($selfdomain, $seconddomain);
}
public function error($code, $str)
{
echo "操作错误:".$code.":".$str;
exit;
}
private function curl($url, $header = '', $data = '', $method = 'get')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //设置提交的字符串
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array(
'result' => $result,
'httpCode' => $httpCode
);
}
private function out($msg)
{
return json_decode($msg, true);
}
public function GetDNSRecord($domain, $recordType = 'TXT')
{
$url = "https://api.godaddy.com/v1/domains/$domain/records/$recordType/_acme-challenge";
$header = ['accept: application/json', 'authorization:sso-key '.$this->accessKeyId.':'.$this->accessSecrec];
return $this->curl($url, $header);
}
public function UpdateDNSRecord($domain, $name, $value, $recordType = 'TXT')
{
$url = "https://api.godaddy.com/v1/domains/$domain/records/$recordType/$name";
$header = ['accept: application/json', 'Content-Type: application/json',
'authorization:sso-key '.$this->accessKeyId.':'.$this->accessSecrec];
$data = array(
array(
'data' => $value,
'name' => $name,
'ttl' => 3600,
'type' => $recordType)
);
return $this->curl($url, $header, json_encode($data), 'put');
}
public function CreateDNSRecord($domain, $name, $value, $recordType = 'TXT')
{
$url = "https://api.godaddy.com/v1/domains/$domain/records";
$header = ['accept: application/json', 'Content-Type: application/json',
'authorization:sso-key '.$this->accessKeyId.':'.$this->accessSecrec];
$data = array(
array(
'data' => $value,
'name' => $name,
'ttl' => 3600,
'type' => $recordType)
);
return $this->curl($url, $header, json_encode($data), 'PATCH');
}
}