使用 DnsPod 简单实现域名 DDNS

最近拿到了自家宽带光猫的超管权限,通过光猫、小米路由器,把 20~10000 这个范围内的端口全映射到了 NAS 上面;简单测试一下,除了 80、443 端口不能用以外,其它均正常。

目前我还没搞清楚这个光猫换公网 IP 的时间机制,于是乎我写个简单的 PHP 脚本,配合 DNSpod,实现域名动态解析,方便我不在家时远程控制。

DNSpod 免费版 TTL 最短时间是 10 分钟,配合宝塔的计划任务,挂个间隔 10 分钟的任务刚刚好。

计划任务
把脚本放到合适的路径,添加一个 10 分钟的计划任务

/usr/bin/php /www/wwwroot/dnspod/main.php

图片[1]-使用 DnsPod 简单实现域名 DDNS-村少博客

源代码

<?php
// Dnspod token,获取地址 https://console.dnspod.cn/account/token/token
$token     = '235678,f485d8729df9397c719e0fe8f5802xxx';

// 需要解析的域名
$domain    = 'xxx.com';

// 解析记录
$record    = 'panel.nas';
$api       = 'https://dnsapi.cn/Record.List';
$post      = [
    'login_token' => $token,
    'format'      => 'json',
    'lang'        => 'cn',
    'domain'      => domain,
];
$record_id = null;
$res       = getCurl($api, ['post' => $post]);
$data      = json_decode($res, true);

// 获取记录ID
foreach ($data['records'] as $itm) {
    if ($record == $itm['name']) {
        $record_id = $itm['id'];
    }
}

if (empty($record_id)) {
    exit("记录 [$record] 未找到,请检查!\n");
}

// 获取公网IP
$ip = file_get_contents('https://www.bt.cn/Api/getIpAddress');
if (empty($ip)) {
    exit("公网IP获取失败 \n");
}

echo "当前公网IP:$ip \n";

// 修改域名A记录
$api  = 'https://dnsapi.cn/Record.Modify';
$post = [
    'login_token' => $token,
    'format'      => 'json',
    'lang'        => 'cn',
    'domain'      => $domain,
    'record_id'   => $record_id,
    'sub_domain'  => $record,
    'record_type' => 'A',
    'record_line' => '默认',
    'value'       => $ip,
    'mx'          => 20,
];
$res  = getCurl($api, ['post' => $post]);
$data = json_decode($res, true);
if ($data['status']['code']) {
    echo "修改成功:{$data['status']['message']} \n";
}

function getCurl($url, $opt = [])
{
    $cookie = '';
    if (is_array($opt['cookie'])) {
        foreach ($opt['cookie'] as $k => $v) {
            $cookie .= $k . '=' . $v . '; ';
        }
    }

    $cookie = (mb_substr($cookie, 0, mb_strlen($cookie) - 2));

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_ENCODING, '');
    curl_setopt($ch, CURLOPT_HTTPHEADER, ["Expect:"]);
    curl_setopt($ch, CURLOPT_NOBODY, $opt['nobody']);
    curl_setopt($ch, CURLOPT_HEADER, $opt['header'] ?? false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $opt['headers'] ?? []);
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, $opt['rtime'] ?? 10000);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $opt['ctime'] ?? 10000);
    curl_setopt($ch, CURLOPT_REFERER, $opt['refer'] ?? 'https://user.qzone.qq.com/');
    curl_setopt($ch, CURLOPT_USERAGENT,
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36');
    if (isset($opt['post'])) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($opt['post']) ? http_build_query($opt['post']) : $opt['post']);
    }
    if (isset($opt['proxy']) && is_array($opt['proxy'])) {
        curl_setopt($ch, CURLOPT_PROXY, $opt['proxy']['ip']);
        curl_setopt($ch, CURLOPT_PROXYPORT, $opt['proxy']['port']);
    }
    $res   = curl_exec($ch);
    $error = curl_error($ch);
    $code  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($opt['detail']) {
        return ['code' => $code, 'error' => $error, 'resp' => $res,];
    }

    return $res;
}
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
相关推荐
评论 抢沙发

请登录后发表评论