最近发现托管的免费动态IP受政策管制、商业收费策略、技术平台稳定性等等,变得非常不稳定。自己正好是新网代理资质,也有API和自己的域名,便尝试使用自己的方案更新域名。这样家里的服务器就能使用FTP,以及其他私有NAS服务了。
看了下xinnet新网API文档,很简单。(但不得不说,新网的API技术协议真是写得像屎一样,数据结构别扭,既不是xml也不是json,就是自己的大白话,而且关键数据结构表述不清,甚至错误。真是谁用谁知道!)
框架说明:
说明:
- 家里是中国电信宽带,使用pppoe拨号。
- Xinnet新网API,需要事先登记一个固定IP。只有这个IP才能调用xinnet新网API。代理能申请5个。固定IP用的也是新网最低的虚拟主机Linux版本(有PHP即可)。放一个很简单php程序——fresh.php
- 把虚拟主机的IP地址填入允许IP的名单,否则API调用出错。
固定IP中转端(虚拟主机端)设置
Xinnet新网API开发。非常简单,注意MD5加密字符串为,METHOD+CLIENT+PASSWORD+domainName
<?php
$client = "agentXXXX";
$password = "YourPassword";
$enc ="E";
$dn = "home.yourpersonaldomain.cn";
$name = "yourpersonaldomain.cn";
$recordname = "home.yourpersonaldomain.cn";
$type="A";
$content=""; //NewIP
$oldcontent="192.168.0.88"; //oldIP
$ttl=600;
//METHOD+CLIENT+PASSWORD+name
$checksum=MD5("DomainDNSQuery".$client.$password.$name);
$w_URL = "http://api.xinnet.com/domain/api.gb?method=DomainDNSQuery&charset=utf8";
$w_Parameters = "&enc=E&client=".$client.
"&checksum=".$checksum.
"&name=".$name.
"&type=".$type;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $w_URL);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $w_Parameters);
echo $w_URL."\n";
echo $w_Parameters."\n";
$data = curl_exec($curl);
curl_close($curl);
echo $data."\n";
$ary_result = split('&',$data);
$arrlength = count($ary_result);
$isOK = False;
for($x=0;$x<$arrlength;$x++) {
list($key,$value)=split('=',$ary_result[$x]);
if ($key=='ret' and $value == 100 ){
$isOK = True;
}
if ($key=='content1'){
$oldcontent = $value;
}
}
if ($oldcontent == $_SERVER["REMOTE_ADDR"]) {
exit(0);
}
//我只有一个A记录,如果你有多个A记录,请自行修改程序
//原始文档:http://agent.xinnet.com/Modules/downloads/api/xinnet-domain-api.pdf
//P94-95
//METHOD+CLIENT+PASSWORD+name
$content=$_SERVER["REMOTE_ADDR"];
$checksum=MD5("DomainDNSMod".$client.$password.$name);
$w_URL = "http://api.xinnet.com/domain/api.gb?method=DomainDNSMod&charset=utf8";
$w_Parameters = "&enc=E&client=".$client.
"&checksum=".$checksum.
"&name=".$name.
"&recordname=".$recordname.
"&type=".$type.
"&content=".$content.
"&oldcontent=".$oldcontent.
"&ttl=".$ttl;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $w_URL);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $w_Parameters);
$data = curl_exec($curl);
echo $w_URL."\n";
echo $w_Parameters."\n";
echo $data."\n";
curl_close($curl);
?>
注意:dns TTL 设置为 600秒,缓存时间 10分钟。
家里服务器设置(Linux)
建立一个脚本,/root/script/refreshXinnetDNS.sh
#!/bin/bash
#/root/script/refreshXinnetDNS.sh
myDomain=home.yourpersonaldomain.cn
myStaticIP=XXX.XXX.XXX.XXX
if ping -c1 $myDomain|grep PING >/dev/null; then
remoteIP=`ping -c1 $myDomain | grep PING | cut -d'(' -f2 | cut -d')' -f1`
else
exit 1
fi
if ifconfig ppp0>/dev/null ; then
localIP=`ifconfig ppp0 | grep 'inet addr' | cut -d':' -f2 | cut -d' ' -f1`
else
exit 1;
fi
echo "remoteIP:"$remoteIP
echo "localIP:"$localIP
if [ $remoteIP != $localIP ];then
wget -q -O- "http://"$myStaticIP"/refreshIP.php" > /dev/null
fi
在Redhat体系下,可以拨号后自动添加动态域名,把脚本加入
/sbin/ifup-local
[root@sh sbin]# cat /sbin/ifup-local
#!/bin/bash
export PATH=$PATH:/usr/sbin:/usr/bin
if [ $1 == "ppp0" ] ;then
/root/script/refreshXinnetDNS.sh
fi
可以实现拨号后自动更新。外加设置一个crontab 10分钟监测一次。
这样 home.yourpernaldomain.cn 就是我家里的服务器了。电信封闭了http80端口,但是没有ftp还能用。平时把自己的私有云设置在其他端口上,就可以正常使用了。
很简单的方案,没有加密什么的,也就自己用。需要的可以自行添加,非常简单。其他万网API或其他API原理也一样。