Site Overlay

一种更硬的 DDNS

面向用户的服务器:

flush_ip.php:

<?php

function res($data = null, $message = null, $code = 200){
    header("Content-Type: application/json");
    http_response_code($code);
    echo json_encode([
            "data" => $data,
            "code" => $code,
            "message" => $message,
        ]);
    exit(0);
}

function err($msg = 'error.', $code = 400){
    res(null, $msg, $code);
}

if(!isset($_GET['v'])){
    err("missing argument.");
}

$ip = trim($_GET['v']);

if(!filter_var($ip, FILTER_VALIDATE_IP)){
    err("invalid ip.");
}

file_put_contents("upstream_ip.txt", $ip);

res();

update_ip.sh

#!/bin/bash

# insert/update hosts entry
ip_address=`cat ip.txt`
host_name="你的DDNS域名"
# find existing instances in the host file and save the line numbers
matches_in_hosts="$(grep -n $host_name /etc/hosts | cut -f1 -d:)"
host_entry="${ip_address} ${host_name}"

echo "Please enter your password if requested."

if [ ! -z "$matches_in_hosts" ]
then
    echo "Updating existing hosts entry."
    # iterate over the line numbers on which matches were found
    while read -r line_number; do
        # replace the text of each line with the desired host entry
        sudo sed -i "${line_number}s/.*/${host_entry} /" /etc/hosts
    done <<< "$matches_in_hosts"
else
    echo "Adding new hosts entry."
    echo "$host_entry" | sudo tee -a /etc/hosts > /dev/null
fi

动态 IP 的服务器:

    # hostIp 自己获取
    curl -s -o /dev/null "xxxx/flush_ip.php?v=$hostIp"

主要是为了解决 DDNS 更新后,全网生效需要时间。于是自行对下游服务器提交新的公网 IP,从而让下游服务器马上直到新的 IP。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注