利用阿里云的域名解析openapi实现ddns服务功能

2020-05-25 13:03:16

    目前市场上商用的ddns服务普片价格为500元/年,由于阿里云的ddns服务开放了openapi,可以通过openapi实现ddns服务的效果,这样就能省下一年500元的费用了。下面小沃就奉上小沃自己写的ddns服务源码:

<?php
function GetRealIp () {
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        return $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    if (isset($_SERVER['HTTP_CLIENT_IP'])) {
        return $_SERVER['HTTP_CLIENT_IP'];
    }
    return $_SERVER['REMOTE_ADDR'];
}

function RandomStr ($num) {
    $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $len = strlen($chars);
    $str = "";
    for ($i = 0 ; $i < $num ; $i++) {
        $str .= $chars[mt_rand(0, $len-1)];
    }
    return $str;
}

file_put_contents("ddns.log", date("Y-m-d H:i:s"), FILE_APPEND);

ob_start();
$realip = GetRealIp();
$ret = dns_get_record("companyhost.worldflying.cn");
if (count($ret) === 1 && $ret[0]["type"] === "A" && $ret[0]["ip"] === $realip) {
    echo "no need change";
    file_put_contents("ddns.log", " no need change\n", FILE_APPEND);
    header("Content-Length: ".ob_get_length());
    ob_end_flush();
    exit;
}

$ak = [用户自己的AccessKey ID];
$sk = [用户自己的AccessKey Secret];
// 删除主机下的全部解析
$params = array(
    "Action" => "DeleteSubDomainRecords",
    "DomainName" => "worldflying.cn",
    "RR" => "companyhost",
    "Format" => "JSON",
    "Version" => "2015-01-09",
    "AccessKeyId" => $ak,
    "SignatureMethod" => "HMAC-SHA1",
    "SignatureVersion" => "1.0",
    "SignatureNonce" => RandomStr(16),
    "Timestamp" => gmdate("Y-m-d\TH:i:s\Z")
);
ksort($params);
$CanonicalizedQueryString = "";
foreach ($params as $k => $v) {
    $CanonicalizedQueryString .= "&".urlencode($k)."=".urlencode($v);
}
$CanonicalizedQueryString = substr($CanonicalizedQueryString, 1);
$StringToSign = "GET&".urlencode("/")."&".urlencode($CanonicalizedQueryString);
$signature = base64_encode(hash_hmac("sha1", $StringToSign, $sk."&", true));
$url = "https://alidns.aliyuncs.com?".$CanonicalizedQueryString."&Signature=".urlencode($signature);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ret = curl_exec($ch);
curl_close($ch);
echo $ret."\n\n";
// 添加解析
$params = array(
    "Action" => "AddDomainRecord",
    "DomainName" => "worldflying.cn",
    "RR" => "companyhost",
    "Type" => "A",
    "Value" => $realip,
    "Format" => "JSON",
    "Version" => "2015-01-09",
    "AccessKeyId" => $ak,
    "SignatureMethod" => "HMAC-SHA1",
    "SignatureVersion" => "1.0",
    "SignatureNonce" => RandomStr(16),
    "Timestamp" => gmdate("Y-m-d\TH:i:s\Z")
);
ksort($params);
$CanonicalizedQueryString = "";
foreach ($params as $k => $v) {
    $CanonicalizedQueryString .= "&".urlencode($k)."=".urlencode($v);
}
$CanonicalizedQueryString = substr($CanonicalizedQueryString, 1);
$StringToSign = "GET&".urlencode("/")."&".urlencode($CanonicalizedQueryString);
$signature = base64_encode(hash_hmac("sha1", $StringToSign, $sk."&", true));
$url = "https://alidns.aliyuncs.com?".$CanonicalizedQueryString."&Signature=".urlencode($signature);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ret = curl_exec($ch);
curl_close($ch);
echo $ret."\n\n";

header("Content-Length: ".ob_get_length());
ob_end_flush();
file_put_contents("ddns.log", " change success, new ip is ".$realip."\n", FILE_APPEND);

参考资料:传送门


文章作者:沃航科技

联系我们
地址:武汉市东湖高新开发区光谷总部国际1栋2412室
QQ: 932773931
电话:027-59761089-806
手机:13397158231
邮箱:jevian_ma@worldflying.cn

沃航(武汉)科技股份有限公司版权所有

备案号:鄂ICP备16014230号-1

932773931
13397158231