• 83 1 分 21 秒 dns Synology network

    🛡️ Hosts 自动更新脚本

    📜 脚本功能

    自动更新系统 hosts 文件,有效防止 DNS 污染,支持:

    • 自定义域名解析更新
    • GitHub 专用 hosts 更新
    • 自定义 IP 映射规则
    • 多平台支持 (Linux/Synology)

    ⚙️ 配置说明

    编辑脚本开头配置区域:

    脚本功能

    #!/bin/sh
    # 自动更新 hosts 列表,支持自定义添加 host,有效防止 DNS 污染
    
    ### 配置区域 (按需修改) ###
    CUSTOM_IP[1]='18.173.219.5 api.themoviedb.org'  # 自定义 IP 映射
    DOMAIN_HOST=1  # 启用域名解析更新 (1=开启, 0=关闭)
    GITHUB_HOST=1  # 启用 GitHub hosts 更新
    
    ### 域名列表 ###
    DOMAIN_LIST=(
        api.themoviedb.org
        image.tmdb.org
        www.opensubtitles.org
        # 在此添加更多域名...
    )
    
    ### 常量定义 ###
    GITHUB_HOST_URL="https/hosts.gitcdn.top/hosts.txt"
    DNS_API="https/networkcalc.com/api/dns/lookup"
    
    ### 执行逻辑 (无需修改) ###
    REMOTE_GITHUB_HOST_AT=$(curl -s $GITHUB_HOST_URL | grep -w "# last fetch time:")
    LOCAL_GITHUB_HOST_AT=$(grep -w "# last fetch time:" /etc/hosts)
    
    check_domain_host() {
        # 检查域名解析函数
        for DOMAIN in "${DOMAIN_LIST[@]}"; do
            DOMAIN_IP_LIST=$(curl -sX GET "$DNS_API/$DOMAIN" | jq -r '.records.A[].address')
            [ -z "$DOMAIN_IP_LIST" ] && continue
    
            for DOMAIN_IP in $DOMAIN_IP_LIST; do
                if ! grep -q "$DOMAIN_IP" /etc/hosts; then
                    sed -i "/# fetch-domain-hosts begin/Q" /etc/hosts
                    break 2
                fi
            done
        done
    }
    
    write_domain_host() {
        # 写入域名解析函数
        if ! grep -q "fetch-domain-hosts" /etc/hosts; then
            echo -e "\n# fetch-domain-hosts begin" >> /etc/hosts
            for DOMAIN in "${DOMAIN_LIST[@]}"; do
                curl -sX GET "$DNS_API/$DOMAIN" | 
                    jq -r '.records.A[].address + " " + "'"$DOMAIN"'"' >> /etc/hosts
            done
            echo "# fetch-domain-hosts end" >> /etc/hosts
    
            echo -e "\n# fetch-custom-hosts begin" >> /etc/hosts
            for ip_entry in "${CUSTOM_IP[@]}"; do
                echo "$ip_entry" >> /etc/hosts
            done
            echo "# fetch-custom-hosts end" >> /etc/hosts
        fi
    }
    
    ### 主执行流程 ###
    [ "$DOMAIN_HOST" = 1 ] && { check_domain_host; write_domain_host; }
    
    if [ "$GITHUB_HOST" = 1 ] && [ -n "$REMOTE_GITHUB_HOST_AT" ]; then
        if ! grep -q "fetch-github-hosts" /etc/hosts; then
            curl -s "$GITHUB_HOST_URL" >> /etc/hosts
        elif [ "$REMOTE_GITHUB_HOST_AT" != "$LOCAL_GITHUB_HOST_AT" ]; then
            sed -i "/# fetch-github-hosts begin/Q" /etc/hosts
            curl -s "$GITHUB_HOST_URL" >> /etc/hosts
        fi
    fi
    
    ### 输出结果 ###
    cp /etc/hosts /volume2/RunSrcipt/hosts.txt
    

    • 留言板:
    • 暂无评论