#!/bin/bash
###name:SSR动态IP更新
###info:更新远程SSR入流量规则，防止被封。
###filename:ss_updata.sh
curl --insecure https://api.mxin.moe/api/pv.php?shell=ss_updata
passwd=''
protocol='tcp'
hk_addr=$(curl -s https://api.mxisc.com/my_ip.php)
us_addr=$(curl -s http://us.mxisc.com/index.php)
ssh_hk() { sshpass -p $passwd ssh root@hk.mxisc.com $*; }
ssh_us() { sshpass -p $passwd ssh root@us.mxisc.com $*; }
rm -rf /home/ip_addr_hk
rm -rf /home/ip_addr_us
echo "$hk_addr">>/home/ip_addr_hk
echo "$us_addr">>/home/ip_addr_us
source /home/ip_addr_hk
echo "香港服务器IP检测"
echo $new_hk
echo $old_hk
if [ "$new_hk" = "$old_hk" ];then
	echo "新IP与旧IP相同，无需更新IP。"
else
	echo "新IP与旧IP不同，开始更新IP及规则。。。"
	echo "更新IP防火墙规则。。。"
	ssh_hk firewall-cmd --permanent --add-rich-rule=\"rule family=\"ipv4\" source address=\"$new_hk\" port protocol=\"$protocol\" port=\"9000\" accept\"
	echo "删除旧IP防火墙规则。。。"
	ssh_hk firewall-cmd --permanent --remove-rich-rule \"rule family=\"ipv4\" source address=\"$old_hk\" port port=\"9000\" protocol=\"$protocol\" accept\"
	echo "防火墙配置生效中。。。"
	ssh_hk firewall-cmd --reload
	echo "规则更新完成。"
fi

source /home/ip_addr_us
echo "西雅图服务器IP检测"
echo $new_us 
echo $old_us
if [ "$new_us" = "$old_us" ];then
        echo "新IP与旧IP相同，无需更新IP。"
else
        echo "新IP与旧IP不同，开始更新IP及规则。。。"
        echo "更新IP防火墙规则。。。"
        ssh_us firewall-cmd --permanent --add-rich-rule=\"rule family=\"ipv4\" source address=\"$new_us\" port protocol=\"$protocol\" port=\"9100\" accept\"
        echo "删除旧IP防火墙规则。。。"
        ssh_us firewall-cmd --permanent --remove-rich-rule \"rule family=\"ipv4\" source address=\"$old_us\" port port=\"9100\" protocol=\"$protocol\" accept\"
        echo "防火墙配置生效中。。。"
        ssh_us firewall-cmd --reload
        echo "规则更新完成。"
fi
