//view.php,查看你域名的RecordId,更新解析的时候需要。
<?php
function GetUTCTime()
{
date_default_timezone_set('UTC');
return date('Y-m-d\TH:i:s\Z', time());
}
function Create_Guid()
{
if (function_exists ( 'com_create_guid' ))
{
return com_create_guid ();
}
else
{
$charid = strtoupper ( md5 ( uniqid ( rand (), true ) ) );
$hyphen = chr ( 45 );
$uuid = '' .
substr ( $charid, 0, 8 ) . $hyphen . substr ( $charid, 8, 4 ) . $hyphen . substr ( $charid, 12, 4 ) . $hyphen . substr ( $charid, 16, 4 ) . $hyphen . substr ( $charid, 20, 12 );
return $uuid;
}
}
function GetSignature($str, $key)
{
$signature = "";
if (function_exists('hash_hmac'))
{
$signature = base64_encode(hash_hmac("sha1", $str, $key, true));
}
return $signature;
}
function ProcessEncodeStr($str)
{
$tmp = str_replace('+','%20',$str);
$tmp = str_replace('*','%2A',$tmp);
$tmp = str_replace('@','%40',$tmp);
return str_replace('%7E','~',$tmp);
}
function percentEncode($str)
{
return ProcessEncodeStr(urlencode(mb_convert_encoding($str, "UTF-8")));
}
function SendAPI($str, $Secret)
{
$Signature = 'GET&'.percentEncode('/').'&'.percentEncode($str);
$Signature = getSignature($Signature, $Secret.'&');
$GetString = "http://dns.aliyuncs.com/?";
$GetString = $GetString.$str.'&Signature='.percentEncode($Signature);
return $GetString;// GetURL($GetString);
}
function DescribeDomainRecords($AccessKeyId, $DomainName)
{
$str = 'AccessKeyId='.$AccessKeyId.'&';
$str = $str.'Action=DescribeDomainRecords&';
$str = $str.'DomainName='.$DomainName.'&';
$str = $str.'Format=xml&';
$str = $str.'SignatureMethod=HMAC-SHA1&';
$str = $str.'SignatureNonce='.strtolower(Create_Guid()).'&';
$str = $str.'SignatureVersion=1.0&';
$str = $str.'Timestamp='.percentEncode(GetUTCTime()).'&';
$str = $str.'Version=2015-01-09';
return $str;
}
function UpdateDomainRecord($AccessKeyId, $Value, $HostName, $RecordId)
{
$str = 'AccessKeyId='.$AccessKeyId.'&';
$str = $str.'Action=UpdateDomainRecord&';
$str = $str.'Format=xml&';
$str = $str.'Line=default&';
$str = $str.'Priority=1&';
$str = $str.'RR='.percentEncode($HostName).'&';
$str = $str.'RecordId='.$RecordId.'&';
$str = $str.'SignatureMethod=HMAC-SHA1&';
$str = $str.'SignatureNonce='.strtolower(Create_Guid()).'&';
$str = $str.'SignatureVersion=1.0&';
$str = $str.'TTL=600&';
$str = $str.'Timestamp='.percentEncode(GetUTCTime()).'&';
$str = $str.'Type=A&';
$str = $str.'Value='.percentEncode($Value).'&';
$str = $str.'Version=2015-01-09';
return $str;
}
//前面的所有函数都是为了算出阿里云API的参数,不用管他。
/*
AccessKeyId 和 Secret 在你的阿里云账户里申请。
假设你建了一个域名:www.test.com,下面的参数就这么写
$DomainName = "test.com";
查询结果里看到<RecordId>xxxxxxxxxx</RecordId>这样的就是了,xxxxxxxxxx是一串数字
*/
$AccessKeyId = "你的AccessKeyId";
$Secret = "你的Secret";
$DomainName = "你的域名";
$Data = SendAPI(DescribeDomainRecords($AccessKeyId, $DomainName), $Secret);
$str= file_get_contents($Data);
echo htmlentities($str,ENT_QUOTES,"UTF-8");
?>
//--------------------------------分割线---------------------------
//update.php,更新解析记录。
<?php
function GetUTCTime()
{
date_default_timezone_set('UTC');
return date('Y-m-d\TH:i:s\Z', time());
}
function Create_Guid()
{
if (function_exists ( 'com_create_guid' ))
{
return com_create_guid ();
}
else
{
$charid = strtoupper ( md5 ( uniqid ( rand (), true ) ) );
$hyphen = chr ( 45 );
$uuid = '' .
substr ( $charid, 0, 8 ) . $hyphen . substr ( $charid, 8, 4 ) . $hyphen . substr ( $charid, 12, 4 ) . $hyphen . substr ( $charid, 16, 4 ) . $hyphen . substr ( $charid, 20, 12 );
return $uuid;
}
}
function GetSignature($str, $key)
{
$signature = "";
if (function_exists('hash_hmac'))
{
$signature = base64_encode(hash_hmac("sha1", $str, $key, true));
}
return $signature;
}
function ProcessEncodeStr($str)
{
$tmp = str_replace('+','%20',$str);
$tmp = str_replace('*','%2A',$tmp);
$tmp = str_replace('@','%40',$tmp);
return str_replace('%7E','~',$tmp);
}
function percentEncode($str)
{
return ProcessEncodeStr(urlencode(mb_convert_encoding($str, "UTF-8")));
}
function SendAPI($str, $Secret)
{
$Signature = 'GET&'.percentEncode('/').'&'.percentEncode($str);
$Signature = getSignature($Signature, $Secret.'&');
$GetString = "http://dns.aliyuncs.com/?";
$GetString = $GetString.$str.'&Signature='.percentEncode($Signature);
return $GetString;// GetURL($GetString);
}
function DescribeDomainRecords($AccessKeyId, $DomainName)
{
$str = 'AccessKeyId='.$AccessKeyId.'&';
$str = $str.'Action=DescribeDomainRecords&';
$str = $str.'DomainName='.$DomainName.'&';
$str = $str.'Format=xml&';
$str = $str.'SignatureMethod=HMAC-SHA1&';
$str = $str.'SignatureNonce='.strtolower(Create_Guid()).'&';
$str = $str.'SignatureVersion=1.0&';
$str = $str.'Timestamp='.percentEncode(GetUTCTime()).'&';
$str = $str.'Version=2015-01-09';
return $str;
}
function UpdateDomainRecord($AccessKeyId, $Value, $HostName, $RecordId)
{
$str = 'AccessKeyId='.$AccessKeyId.'&';
$str = $str.'Action=UpdateDomainRecord&';
$str = $str.'Format=xml&';
$str = $str.'Line=default&';
$str = $str.'Priority=1&';
$str = $str.'RR='.percentEncode($HostName).'&';
$str = $str.'RecordId='.$RecordId.'&';
$str = $str.'SignatureMethod=HMAC-SHA1&';
$str = $str.'SignatureNonce='.strtolower(Create_Guid()).'&';
$str = $str.'SignatureVersion=1.0&';
$str = $str.'TTL=600&';
$str = $str.'Timestamp='.percentEncode(GetUTCTime()).'&';
$str = $str.'Type=A&';
$str = $str.'Value='.percentEncode($Value).'&';
$str = $str.'Version=2015-01-09';
return $str;
}
//前面的所有函数都是为了算出阿里云API的参数,不用管他。
/*
AccessKeyId 和 Secret 在你的阿里云账户里申请。
假设你建了一个域名:www.test.com,下面的参数就这么写
$DomainName = "test.com";
$RR="www";
*/
$AccessKeyId = "你的AccessKeyId";
$Secret = "你的Secret";
$DomainName = "你的域名";
$RR="你的主机名";
$RecordId = "你申请域名的RecordId"; //运行view.php就能看到,
//获取原IP地址
$Data = SendAPI(DescribeDomainRecords($AccessKeyId, $DomainName), $Secret);
$oldip = file_get_contents($Data);
$olds=strpos($oldip,"<Value>");
$olde=strpos($oldip,"</Value>");
$oldip=substr($oldip,$olds+7,$olde-$olds-7);
//获取当前公网地址
$source=file_get_contents('http://2017.ip138.com/ic.asp'); //通过ip138获取
$strs=strpos($source,"[");
$stre=strpos($source,"]");
$updateip=substr($source,$strs+1,$stre-$strs-1);
//对比地址,如果不同就更新解析记录。
if ($updateip==$oldip)
{
echo "地址未改变,不需要更新";
}
else
{
$Udata = SendAPI(UpdateDomainRecord($AccessKeyId, $updateip, $RR, $RecordId), $Secret); //构造api请求地址和参数
echo file_get_contents($Udata);
}
?>
说明:
在阿里云注册一个域名(top域名2元一年),添加一条解析记录,ip地址随便填,在自己的账号管理里面申请AccessKeyId 和 Secret,这是API必须的参数。
复制上面的代码,保存成对应的view.php和update.php文件,如果使用记事本保存请在保存时选择所有文件,不然就成了TXT格式。更改红字处的参数。
在浏览器里打开view.php,比如: http://192.168.1.10/php/view.php(假设你的web station地址是http://192.168.1.10,find.php放web/php目录),
如果运行正常就能看到<RecordId>xxxxxxxxxx</RecordId>这样的字段,这个就是你域名的<RecordId>,更改update.php对应参数。
然后在群晖的计划任务里新建一个自定义脚本的任务,运行时间选10分钟一次,运行命令里填 “php /volume1/web/php/update.php“,
(假设你的update.php放在volume1/web/php目录,引号不要,具体位置可以在file station里选中update.php文件查看属性)。
你也可以直接在浏览器运行,如:http://192.168.1.10/php/update.php 也可以更新解析记录。
web station和PHP套件要安装,php5和7随便装一个,如果不行就两个都装试试,红色字的部分是需要改成你自己的。这个也是我改别人的,改之前很多
人都说失败,后来发现是部分函数不支持造成的,比如原程序里的curl我的黑裙上就不支持,我换成了file_get_contents()。本人对php不是很了解,
可能有些地方改的不好,不过在我的黑裙上使用没问题。一直被ddns困扰的可以试试。
|
|