iptables_proxy.sh 脚本内容:
#!/bin/bash
clear
# Linux 透明代理全局规则
# admin@cooluc.com
# 20200616
# 判断 root
if [ $EUID -ne 0 ];then
echo -e "\033[31mError\033[0m: The systemd must be run as root." 1>&2
exit 1;
fi
echo
echo "Linux 透明代理全局规则"
NOTE='\033[36m
==========================================================================
\r\n# 提示:本规则适合所有透明代理,默认通过IP-CIDR粗略过滤国内IP\r\n
国内:直连\r\n国外:代理\r\n
# v2ray 透明代理 inbounds 配置示例:
\033[0m\033[35m
"inbounds": [{
"tag": "transparent",
"listen": "127.0.0.1",
"port": 1080,
"protocol": "dokodemo-door",
"settings": {
"network": "tcp,udp",
"followRedirect": true
},
"streamSettings": null
}],\033[0m\r\n\033[36m
==========================================================================
\033[0m';
echo -e "$NOTE";
APPLY() (
echo -n "输入透明代理端口(范围 1-65535):"
read TRANSPORT
if [ -z $TRANSPORT ];then
echo 输入错误,设置默认透明代理端口:1080
TRANSPORT=1080
fi
echo -n "输入代理服务器IP(用于规则过滤):"
read PROXY_IP
if [ -z $PROXY_IP ];then
echo "输入错误"
exit 0;
else
RULE="\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\b"
CHECK_IP=`echo $PROXY_IP | egrep $RULE | wc -l`
if [ $CHECK_IP -eq 0 ];then
echo "IP 格式错误"
exit 0;
fi
fi
echo -n "路由转发状态:"
FORWARD_STATUS=`cat /proc/sys/net/ipv4/ip_forward`
if [ $FORWARD_STATUS '==' "0" ]; then
echo "未开启"
echo "正在开启路由转发功能"
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
elif [ $FORWARD_STATUS '==' "1" ]; then
echo "已开启"
fi
# 禁用 firewalld 服务
systemctl stop firewalld >/dev/null 2>&1
systemctl mask firewalld >/dev/null 2>&1
# 检查 iptables 是否安装
if [ -z `which iptables` ];then
echo
echo "iptables 未安装"
echo
exit 0;
else
if [ -f "/usr/bin/systemctl" ];then
systemctl restart iptables
else
service iptables restart
fi
fi
echo "添加 iptables 防火墙规则"
MYIP=$(ip addr | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | egrep -v "^192\.168|^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-2]\.|^10\.|^127\.|^255\.|^0\." | head -n 1)
if [[ "$MYIP" = "" ]]; then
MYIP=$(wget -qO- -t1 -T2 ipv4.icanhazip.com)
fi
# 清空 NAT 规则
iptables -t nat -F
iptables -t nat -X V2RAY >/dev/null 2>&1
# 添加 V2RAY 链
iptables -t nat -N V2RAY
# 对局域网其他设备进行透明代理
# iptables -t nat -A PREROUTING -p tcp -j V2RAY
# iptables -t nat -A PREROUTING -p udp -j V2RAY
# 对本机进行透明代理
iptables -t nat -A OUTPUT -p tcp -j V2RAY
# iptables -t nat -A OUTPUT -p udp -j V2RAY
# 过滤代理IP && 本机
iptables -t nat -A V2RAY -d $PROXY_IP/32 -j RETURN
iptables -t nat -A V2RAY -d $MYIP/32 -j RETURN
# 直连内网 & 国内IP段
iptables -t nat -A V2RAY -d 47.240.24.0/24 -j RETURN
iptables -t nat -A V2RAY -d 96.45.191.0/24 -j RETURN
iptables -t nat -A V2RAY -d 149.129.102.0/24 -j RETURN
iptables -t nat -A V2RAY -d 193.200.134.0/24 -j RETURN
iptables -t nat -A V2RAY -d 0.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 10.0.0.0/24 -j RETURN
iptables -t nat -A V2RAY -d 172.20.10.0/24 -j RETURN
iptables -t nat -A V2RAY -d 100.64.0.0/10 -j RETURN
iptables -t nat -A V2RAY -d 127.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 169.254.0.0/16 -j RETURN
iptables -t nat -A V2RAY -d 172.16.0.0/12 -j RETURN
iptables -t nat -A V2RAY -d 192.168.0.0/16 -j RETURN
iptables -t nat -A V2RAY -d 224.0.0.0/4 -j RETURN
iptables -t nat -A V2RAY -d 240.0.0.0/4 -j RETURN
iptables -t nat -A V2RAY -d 1.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 14.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 27.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 36.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 39.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 42.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 43.250.12.0/24 -j RETURN
iptables -t nat -A V2RAY -d 43.250.14.0/24 -j RETURN
iptables -t nat -A V2RAY -d 43.250.15.0/24 -j RETURN
iptables -t nat -A V2RAY -d 49.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 58.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 59.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 60.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 61.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 101.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 103.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 106.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 110.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 111.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 112.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 113.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 114.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 115.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 116.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 117.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 118.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 119.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 120.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 121.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 122.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 123.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 124.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 125.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 126.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 128.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 128.0.0.1/32 -j RETURN
iptables -t nat -A V2RAY -d 139.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 140.205.0.0/16 -j RETURN
iptables -t nat -A V2RAY -d 140.206.0.0/16 -j RETURN
iptables -t nat -A V2RAY -d 140.207.0.0/16 -j RETURN
iptables -t nat -A V2RAY -d 163.177.0.0/16 -j RETURN
iptables -t nat -A V2RAY -d 169.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 175.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 180.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 182.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 183.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 202.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 203.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 210.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 211.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 218.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 219.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 220.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 221.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 222.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 223.0.0.0/8 -j RETURN
# 流量转发
iptables -t nat -A V2RAY -p tcp -j REDIRECT --to-ports $TRANSPORT
# iptables -t nat -A V2RAY -p udp -j REDIRECT --to-ports $TRANSPORT
# 保存规则
iptables-save > /etc/sysconfig/iptables
# 访问测试
echo
echo "访问测试"
echo -n "国内站点:";echo -e "\033[36m$(curl -s http://ip.3322.net)\033[0m";
echo -n "国外站点:";echo -e "\033[36m$(curl -s http://45.32.164.128/ip.php)\033[0m";
echo
)
DISABLE() (
# 清空 NAT 规则
iptables -t nat -F
iptables -t nat -X V2RAY >/dev/null 2>&1
echo
echo "NAT规则已清除"
echo
# 访问测试
echo "访问测试"
echo -n "国内站点:";echo -e "\033[36m$(curl -s http://ip.3322.net)\033[0m";
echo -n "国外站点:";echo -e "\033[36m$(curl -s http://45.32.164.128/ip.php)\033[0m";
echo
)
RELAY() (
MYIP=$(ip addr | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | egrep -v "^192\.168|^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-2]\.|^10\.|^127\.|^255\.|^0\." | head -n 1)
if [[ "$MYIP" = "" ]]; then
MYIP=$(wget -qO- -t1 -T2 ipv4.icanhazip.com)
fi
echo
echo -n "请输入国外IP:";read GLOBAL
echo -n "请输入国外端口:";read GLOBAL_PROT
echo -n "请输入本机通讯端口:";read LOCAL_PROT
echo
echo -n "路由转发状态:"
FORWARD_STATUS=`cat /proc/sys/net/ipv4/ip_forward`
if [ $FORWARD_STATUS '==' "0" ]; then
echo "未开启"
echo "正在开启路由转发功能"
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
elif [ $FORWARD_STATUS '==' "1" ]; then
echo "已开启"
fi
# 端口转发
iptables -t nat -A PREROUTING -p tcp -m tcp --dport $LOCAL_PROT -j DNAT --to-destination $GLOBAL:$GLOBAL_PROT
iptables -t nat -A POSTROUTING -d $GLOBAL -p tcp -m tcp --dport $GLOBAL_PROT -j SNAT --to-source $MYIP
# 保存规则
iptables-save > /etc/sysconfig/iptables
echo -e "已经添加转发规则:\033[36m$MYIP\033[0m:\033[36m$LOCAL_PROT\033[0m -> \033[36m$GLOBAL\033[0m:\033[36m$GLOBAL_PROT\033[0m"
echo
)
echo
echo "> 请选择:"
echo
echo " 1 - 应用全局代理规则"
echo " 2 - 清除所有NAT规则"
echo
echo " 3 - iptables 中继国外服务器(TCP)"
echo
echo -n "请输入:"
read mode
case $mode in
[1]|[1-3]) ;;
*) echo -e '\n ...输入错误.';exit 0;;
esac
if [ -z $mode ]
then
echo -e '\n ...输入错误.';exit 0;
else
if [[ $mode == "1" ]];then
APPLY
elif [[ $mode == "2" ]];then
DISABLE
elif [[ $mode == "3" ]];then
RELAY
fi
fi
适用于所有透明代理
版权声明:本文为原创文章,版权归 Cooluc's Blog 所有,转载请注明出处!
本文链接:https://www.cooluc.com/archives/74.html
友情提示:如果博客出现404或链接失效,请留言或者联系博主修复!