-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsimple.sh
More file actions
244 lines (224 loc) · 8.26 KB
/
simple.sh
File metadata and controls
244 lines (224 loc) · 8.26 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/bin/bash
set -e
TAG="WR_RULE"
SRC_PORT=4500
DST_PORT=4500
DST_IP=$(getent ahostsv4 engage.cloudflareclient.com | awk '{print $1; exit}')
SRC_IP=$(curl -4s --max-time 3 ifconfig.me 2>/dev/null ||
curl -4s --max-time 3 icanhazip.com 2>/dev/null ||
curl -4s --max-time 3 api.ipify.org 2>/dev/null)
# NFT
FIREWALL_TYPE=""
NFT_CONF="/etc/nftables.conf"
check_nftables() {
command -v nft >/dev/null 2>&1 || return 1
nft add table ip test 2>/dev/null && nft delete table ip test 2>/dev/null
return $?
}
detect_firewall() {
if check_nftables; then
FIREWALL_TYPE="nftables"
echo "[*] Обнаружен nftables, будет использоваться nftables"
return 0
elif command -v iptables >/dev/null 2>&1; then
FIREWALL_TYPE="iptables"
echo "[*] Обнаружен iptables, будет использоваться iptables"
return 0
else
echo "[!] Не найден ни nftables, ни iptables. Будет выполнена установка..."
return 1
fi
}
install_dependencies() {
if detect_firewall; then
if command -v apt &>/dev/null; then
export DEBIAN_FRONTEND=noninteractive
apt update -qq
apt install -y -qq curl
if [ "$FIREWALL_TYPE" = "iptables" ]; then
apt install -y -qq netfilter-persistent
SAVE_CMD="netfilter-persistent save"
fi
elif command -v dnf &>/dev/null; then
dnf install -y -q curl
if [ "$FIREWALL_TYPE" = "iptables" ]; then
dnf install -y -q iptables-services
SAVE_CMD="service iptables save"
fi
elif command -v yum &>/dev/null; then
yum install -y -q curl
if [ "$FIREWALL_TYPE" = "iptables" ]; then
yum install -y -q iptables-services
SAVE_CMD="service iptables save"
fi
elif command -v pacman &>/dev/null; then
pacman -Syu --noconfirm curl
if [ "$FIREWALL_TYPE" = "iptables" ]; then
mkdir -p /etc/iptables
SAVE_CMD="iptables-save > /etc/iptables/iptables.rules"
fi
elif command -v apk &>/dev/null; then
apk add --no-cache curl
if [ "$FIREWALL_TYPE" = "iptables" ]; then
mkdir -p /etc/iptables
SAVE_CMD="iptables-save > /etc/iptables/rules.v4"
fi
fi
if [ -n "${SAVE_CMD:-}" ]; then
export SAVE_CMD
fi
return 0
fi
if command -v apt &>/dev/null; then
export DEBIAN_FRONTEND=noninteractive
apt update -qq
apt install -y -qq nftables curl
if check_nftables; then
FIREWALL_TYPE="nftables"
echo "[*] Установлен nftables"
else
apt install -y -qq iptables netfilter-persistent
FIREWALL_TYPE="iptables"
SAVE_CMD="netfilter-persistent save"
export SAVE_CMD
fi
elif command -v dnf &>/dev/null; then
dnf install -y -q nftables curl
if check_nftables; then
FIREWALL_TYPE="nftables"
else
dnf install -y -q iptables iptables-services curl
FIREWALL_TYPE="iptables"
SAVE_CMD="service iptables save"
export SAVE_CMD
fi
elif command -v yum &>/dev/null; then
yum install -y -q nftables curl
if check_nftables; then
FIREWALL_TYPE="nftables"
else
yum install -y -q iptables iptables-services curl
FIREWALL_TYPE="iptables"
SAVE_CMD="service iptables save"
export SAVE_CMD
fi
elif command -v pacman &>/dev/null; then
pacman -Syu --noconfirm nftables curl
if check_nftables; then
FIREWALL_TYPE="nftables"
else
pacman -Syu --noconfirm iptables curl
mkdir -p /etc/iptables
FIREWALL_TYPE="iptables"
SAVE_CMD="iptables-save > /etc/iptables/iptables.rules"
export SAVE_CMD
fi
elif command -v apk &>/dev/null; then
apk add --no-cache nftables curl
if check_nftables; then
FIREWALL_TYPE="nftables"
else
apk add --no-cache iptables curl
mkdir -p /etc/iptables
FIREWALL_TYPE="iptables"
SAVE_CMD="iptables-save > /etc/iptables/rules.v4"
export SAVE_CMD
fi
else
echo "Не поддерживаемый менеджер пакетов. Пожалуйста, установите iptables или nftables и curl самостоятельно."
exit 1
fi
if [ "$FIREWALL_TYPE" = "nftables" ]; then
echo "[*] Используется nftables"
if [ ! -f "$NFT_CONF" ]; then
echo '#!/usr/sbin/nft -f' > "$NFT_CONF"
echo 'flush ruleset' >> "$NFT_CONF"
fi
if command -v systemctl >/dev/null 2>&1; then
systemctl enable nftables 2>/dev/null || true
systemctl start nftables 2>/dev/null || true
elif command -v rc-update >/dev/null 2>&1; then
rc-update add nftables default 2>/dev/null || true
fi
else
echo "[*] Используется iptables"
fi
}
enable_ip_forwarding() {
echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/ipv4-forwarding.conf
sysctl -w net.ipv4.ip_forward=1
}
# IPT
clean_iptables_rules() {
iptables -t nat -S | grep "${TAG}" | sed 's/^-A/-D/' | while read -r rule; do
eval iptables -t nat "$rule" 2>/dev/null || true
done
iptables -S | grep "${TAG}" | sed 's/^-A/-D/' | while read -r rule; do
eval iptables "$rule" 2>/dev/null || true
done
}
apply_iptables_rules() {
clean_iptables_rules
iptables -t nat -A PREROUTING \
-d ${SRC_IP} -p udp --dport ${SRC_PORT} \
-j DNAT --to-destination ${DST_IP}:${DST_PORT} \
-m comment --comment "${TAG}"
iptables -t nat -A POSTROUTING \
-p udp -d ${DST_IP} --dport ${DST_PORT} \
-j MASQUERADE \
-m comment --comment "${TAG}"
iptables -A FORWARD -p udp -d ${DST_IP} --dport ${DST_PORT} -j ACCEPT -m comment --comment "${TAG}"
iptables -A FORWARD -p udp -s ${DST_IP} --sport ${DST_PORT} -j ACCEPT -m comment --comment "${TAG}"
}
save_iptables_rules() {
if [ -n "$SAVE_CMD" ]; then
eval "$SAVE_CMD"
fi
}
# NFT
clean_nftables_rules() {
for table in nat filter; do
for chain in prerouting postrouting forward; do
nft -a list chain ip "$table" "$chain" 2>/dev/null | \
grep -B1 "comment \"$TAG\"" | \
grep -o 'handle [0-9]*' | \
awk '{print $2}' | \
while read -r handle; do
if [[ "$handle" =~ ^[0-9]+$ ]]; then
nft delete rule ip "$table" "$chain" handle "$handle" 2>/dev/null || true
fi
done
done
done
}
apply_nftables_rules() {
clean_nftables_rules
nft add table ip nat 2>/dev/null || true
nft add table ip filter 2>/dev/null || true
nft add chain ip nat prerouting { type nat hook prerouting priority -100 \; } 2>/dev/null || true
nft add chain ip nat postrouting { type nat hook postrouting priority 100 \; } 2>/dev/null || true
nft add chain ip filter forward { type filter hook forward priority filter \; } 2>/dev/null || true
nft add rule ip nat prerouting ip daddr $SRC_IP udp dport $SRC_PORT dnat to $DST_IP:$DST_PORT comment \"$TAG\"
nft add rule ip nat postrouting ip daddr $DST_IP udp dport $DST_PORT masquerade comment \"$TAG\"
nft add rule ip filter forward ip daddr $DST_IP udp dport $DST_PORT accept comment \"$TAG\"
nft add rule ip filter forward ip saddr $DST_IP udp sport $DST_PORT accept comment \"$TAG\"
}
save_nftables_rules() {
nft list ruleset > "$NFT_CONF"
if command -v systemctl >/dev/null 2>&1; then
systemctl restart nftables 2>/dev/null || true
fi
}
main() {
install_dependencies
enable_ip_forwarding
if [ "$FIREWALL_TYPE" = "nftables" ]; then
apply_nftables_rules
save_nftables_rules
else
apply_iptables_rules
save_iptables_rules
fi
echo "Все правила добавлены!"
}
main