forked from tas-unn/bypass_keenetic
-
Notifications
You must be signed in to change notification settings - Fork 6
/
100-unblock-vpn-v4.sh
94 lines (78 loc) · 3.95 KB
/
100-unblock-vpn-v4.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/sh
# 2023. Keenetic DNS bot / Проект: bypass_keenetic / Автор: tas_unn
# GitHub: https://github.com/tas-unn/bypass_keenetic
# Данный бот предназначен для управления обхода блокировок на роутерах Keenetic
# Демо-бот: https://t.me/keenetic_dns_bot
#
# Файл: 100-unblock-vpn-v4.sh, Версия 2.2.0, последнее изменение: 01.10.2023, 18:58
# Автор файла: NetworK (https://github.com/ziwork)
TAG="100-unblock-vpn.sh"
sleep 1
check_allow_vpn_in_config=$(grep "vpn_allowed" /opt/etc/bot_config.py | head -1 | sed 's/=/ /g' | tr -d '\"' | awk '{print $2}')
if [ -z "${check_allow_vpn_in_config}" ]; then
vpn_services="IKE|SSTP|OpenVPN|Wireguard|L2TP"
else
vpn_services=$(echo "$check_allow_vpn_in_config")
fi
vpn_check=$(curl -s localhost:79/rci/show/interface | grep -E "$vpn_services" | grep id | awk '{print $2}' | tr -d \", | uniq -u)
mkdir -p /opt/etc/iproute2
touch /opt/etc/iproute2/rt_tables
chmod 755 /opt/etc/iproute2/rt_tables
for vpn in $vpn_check ; do
#logger -t "$TAG" "$vpn"
if [ "$1" = "hook" ] && [ "$change" = "connected" ] && [ "$id" = "$vpn" ]; then
# shellcheck disable=SC2060
vpn_table=$(echo "$vpn" | tr [:upper:] [:lower:]) # sed 's/[A-Z]/\L&/g'
if grep -q "$vpn_table" /opt/etc/iproute2/rt_tables; then
echo "Таблица уже есть"
else
echo "Таблицы нет, создаем"
get_last_fwmark_id=$(tail -1 /opt/etc/iproute2/rt_tables | awk '{print $1}')
if [ -n "${get_last_fwmark_id}" ]; then counter_new=$(($get_last_fwmark_id + 1)); else counter_new=$((1000 + 1)); fi
vpn_table_file=$(echo "$counter_new" "$vpn_table")
echo "$vpn_table_file" >> /opt/etc/iproute2/rt_tables
fi
sleep 1
vpn_table_id=$(grep "$vpn_table" /opt/etc/iproute2/rt_tables | awk '{print $1}')
get_fwmark_id=$(grep "$vpn_table" /opt/etc/iproute2/rt_tables | awk '{print "0xd"$1}')
vpn_link_up=$(curl -s localhost:79/rci/show/interface/"$vpn"/connected | tr -d '"')
if [ "$vpn_link_up" = "no" ]; then
info=$(echo VPN "$vpn" OFF: правила обновлены)
logger -t "$TAG" "$info"
ip rule del from all table "$vpn_table_id" priority 1778 2>/dev/null
ip -4 rule del fwmark "$get_fwmark_id" lookup "$vpn_table_id" priority 1778 2>/dev/null
ip -4 route flush table "$vpn_table_id"
type=iptable table=nat /opt/etc/ndm/netfilter.d/100-redirect.sh
#cat /dev/null >| /opt/etc/iproute2/rt_tables
#sed -i '/"$vpn_table_file"/d' /opt/etc/iproute2/rt_tables
fi
sleep 2
if [ "$vpn_link_up" = "yes" ]; then
sleep 3
vpn_ip=$(curl -s localhost:79/rci/show/interface/"$vpn"/address | tr -d \")
vpn_type=$(ifconfig | grep "$vpn_ip" -B1 | head -1 |cut -d " " -f1)
vpn_name=$(curl -s localhost:79/rci/show/interface/"$vpn"/description | tr -d \")
unblockvpn=$(echo unblockvpn-"$vpn_name"-"$vpn")
ip -4 route add table "$vpn_table_id" default via "$vpn_ip" dev "$vpn_type" 2>/dev/null
ip -4 route show table main | grep -Ev ^default | while read -r ROUTE; do ip -4 route add table "$vpn_table_id" $ROUTE 2>/dev/null; done
ip -4 rule add fwmark "$get_fwmark_id" lookup "$vpn_table_id" priority 1778 2>/dev/null
ip -4 route flush cache
touch /opt/etc/unblock/vpn-"$vpn_name"-"$vpn".txt
chmod 0755 /opt/etc/unblock/vpn-"$vpn_name"-"$vpn".txt
info=$(echo VPN "$vpn" ON: "$vpn_name" "$vpn_ip" via "$vpn_type")
logger -t "$TAG" "$info"
if iptables-save 2>/dev/null | grep -q "$unblockvpn"; then
info_ipset=$(echo "ipset уже есть");
logger -t "$TAG" "$info_ipset";
else
ipset create "$unblockvpn" hash:net -exist 2>/dev/null;
fi
type=iptable table=nat /opt/etc/ndm/netfilter.d/100-redirect.sh
#/opt/bin/unblock_update.sh
#log="$(ip route show table 1001 | wc -l) ips added to route table 1000"
#echo "$log"
#logger "$log"
fi
fi # hook
done
exit 0