<button id="g80sy"></button>
  • <abbr id="g80sy"></abbr>
  • <button id="g80sy"></button>
    <rt id="g80sy"><delect id="g80sy"></delect></rt>
  • Linux服務(wù)器 屏蔽國(guó)外IP訪(fǎng)問(wèn)親測(cè)可用

    程序猿 2023-02-09 15:24:50 9904瀏覽 加載中

    通過(guò)ssh遠(yuǎn)程登錄服務(wù)器內(nèi),運(yùn)行如下命令語(yǔ)句獲取國(guó)內(nèi)IP網(wǎng)段,會(huì)保存為/root/china_ssr.txt

    wget -q --timeout=60 -O- 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | awk -F\| '/CN\|ipv4/ { printf("%s/%d\n", $4, 32-log($5)/log(2)) }' > /root/china_ssr.txt

    將下面腳本保存為/root/allcn.sh ,并設(shè)置可執(zhí)行權(quán)限( 命令: chmod +x  allcn.sh)

    mmode=$1
    
    CNIP="/root/china_ssr.txt"
    
    gen_iplist() {
            cat <<-EOF
                    $(cat ${CNIP:=/dev/null} 2>/dev/null)
    EOF
    }
    
    flush_r() {
    iptables  -F ALLCNRULE 2>/dev/null
    iptables -D INPUT -p tcp -j ALLCNRULE 2>/dev/null
    iptables  -X ALLCNRULE 2>/dev/null
    ipset -X allcn 2>/dev/null
    }
    
    mstart() {
    ipset create allcn hash:net 2>/dev/null
    ipset -! -R <<-EOF 
    $(gen_iplist | sed -e "s/^/add allcn /")
    EOF
    
    iptables -N ALLCNRULE 
    iptables -I INPUT -p tcp -j ALLCNRULE 
    iptables -A ALLCNRULE -s 127.0.0.0/8 -j RETURN
    iptables -A ALLCNRULE -s 169.254.0.0/16 -j RETURN
    iptables -A ALLCNRULE -s 224.0.0.0/4 -j RETURN
    iptables -A ALLCNRULE -s 255.255.255.255 -j RETURN
    iptables -A ALLCNRULE -m set --match-set allcn  src -j RETURN 
    iptables -A ALLCNRULE -p tcp -j DROP 
    }
    if [ "$mmode" == "stop" ] ;then
    flush_r
    exit 0
    fi
    flush_r
    sleep 1
    mstart

    執(zhí)行如下命令將開(kāi)始攔截

    /root/allcn.sh

    執(zhí)行如下命令即可停止攔截

    /root/allcn.sh stop

    標(biāo)簽:
    最后修改:2025-07-03 11:20:28

    非特殊說(shuō)明,本博所有文章均為博主原創(chuàng)。

    主站蜘蛛池模板: 大冶市| 库伦旗| 满洲里市| 阜宁县| 罗城| 天祝| 阿尔山市| 喀喇沁旗| 绿春县| 萨嘎县| 新蔡县| 张家港市| 江山市| 蓝山县| 盈江县| 海林市| 长岛县| 通渭县| 德令哈市| 民勤县| 哈密市| 师宗县| 遂川县| 亳州市| 莫力| 莱芜市| 东乡族自治县| 乌拉特前旗| 莫力| 郓城县| 兴海县| 肇源县| 汶上县| 平泉县| 武城县| 莒南县| 胶南市| 溧水县| 神木县| 湟源县| 潢川县|