使用 DnPod 简单实现域名 DDNS

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

效果图

计划任务

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

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

Bash

源代码

<?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;
}