-
Notifications
You must be signed in to change notification settings - Fork 22
/
ipset-adder.sh
executable file
·368 lines (305 loc) · 14 KB
/
ipset-adder.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/bin/bash
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
generate_ipset_list() {
local ip_file="$1"
local ipset_file="$2"
local ipset_name="$3"
if [[ -f "$ip_file" ]]; then
echo -e " - ${YELLOW}$ip_file${NC}"
: > "$ipset_file"
while IFS= read -r ip; do
echo "add $ipset_name $ip -exist" >> "$ipset_file"
done < "$ip_file"
else
echo -e "${RED}IP файл $ip_file не найден${NC}"
fi
}
mode="${1:-}"
if [[ "$mode" == "auto" ]]; then
ipset_name="unblock"
echo -e "\n${GREEN}Запущен режим ${YELLOW}auto${GREEN}. Используем IPset лист: ${YELLOW}${ipset_name}${NC}"
main_ip_list_file="./main_domains/discord-main-ip-list"
main_ipset_list_file="./main_domains/discord-main-ipset-list"
voice_ip_list_file="./voice_domains/discord-voice-ip-list"
voice_ipset_list_file="./voice_domains/discord-voice-ipset-list"
echo -e "${GREEN}Генерируем списки в формате IPset из:${NC}"
generate_ipset_list "$main_ip_list_file" "$main_ipset_list_file" "$ipset_name"
generate_ipset_list "$voice_ip_list_file" "$voice_ipset_list_file" "$ipset_name"
if ! ipset list "$ipset_name" > /dev/null 2>&1; then
ipset create "$ipset_name" hash:ip
echo -e "${GREEN}IPset лист ${YELLOW}$ipset_name${GREEN} создан${NC}"
else
echo -e "${GREEN}Работаем дальше...${NC}"
fi
existing_ips=$(ipset list "$ipset_name" | sed -n '/^Members:/,$p' | tail -n +2 | awk '{ print $1 }' | sort)
declare -A existing_ips_array
while IFS= read -r ip; do
ip="${ip// }"
if [[ -n "$ip" ]]; then
existing_ips_array["$ip"]=1
fi
done <<< "$existing_ips"
selected_ipset_files=("$main_ipset_list_file" "$voice_ipset_list_file")
tmp_ipset_restore_file=$(mktemp)
for ipset_file in "${selected_ipset_files[@]}"; do
while IFS= read -r line; do
ip=$(echo "$line" | awk '{print $3}')
ip="${ip// }"
if [[ -z "$ip" ]]; then
continue
fi
if [[ -z "${existing_ips_array["$ip"]-}" ]]; then
echo "add $ipset_name $ip -exist" >> "$tmp_ipset_restore_file"
existing_ips_array["$ip"]=1
fi
done < "$ipset_file"
done
if [[ -s "$tmp_ipset_restore_file" ]]; then
ipset restore < "$tmp_ipset_restore_file"
count=$(wc -l < "$tmp_ipset_restore_file")
echo -e "${GREEN}Загружено ${YELLOW}$count${GREEN} IP адреса(ов) в IPset лист ${YELLOW}$ipset_name${NC}"
else
echo -e "${RED}Нет новых IP адресов для добавления в IPset${NC}"
fi
rm -f "$tmp_ipset_restore_file"
exit 0
elif [[ "$mode" == "list" ]]; then
ipset_name="${2:-}"
if [[ -z "$ipset_name" ]]; then
echo -e "${RED}Имя IPset листа не указано. Выполни: ./ipset-adder.sh list <имя_листа>${NC}"
exit 1
fi
echo -e "\n${GREEN}Запущен режим ${YELLOW}list${GREEN}. Используем IPset лист: ${YELLOW}${ipset_name}${NC}"
main_ip_list_file="./main_domains/discord-main-ip-list"
main_ipset_list_file="./main_domains/discord-main-ipset-list"
voice_ip_list_file="./voice_domains/discord-voice-ip-list"
voice_ipset_list_file="./voice_domains/discord-voice-ipset-list"
echo -e "${GREEN}Генерируем списки в формате IPset из:${NC}"
generate_ipset_list "$main_ip_list_file" "$main_ipset_list_file" "$ipset_name"
generate_ipset_list "$voice_ip_list_file" "$voice_ipset_list_file" "$ipset_name"
if ! ipset list "$ipset_name" > /dev/null 2>&1; then
ipset create "$ipset_name" hash:ip
echo -e "${GREEN}IPset лист ${YELLOW}$ipset_name${GREEN} создан${NC}"
else
echo -e "${GREEN}Работаем дальше...${NC}"
fi
existing_ips=$(ipset list "$ipset_name" | sed -n '/^Members:/,$p' | tail -n +2 | awk '{ print $1 }' | sort)
declare -A existing_ips_array
while IFS= read -r ip; do
ip="${ip// }"
if [[ -n "$ip" ]]; then
existing_ips_array["$ip"]=1
fi
done <<< "$existing_ips"
selected_ipset_files=("$main_ipset_list_file" "$voice_ipset_list_file")
tmp_ipset_restore_file=$(mktemp)
for ipset_file in "${selected_ipset_files[@]}"; do
while IFS= read -r line; do
ip=$(echo "$line" | awk '{print $3}')
ip="${ip// }"
if [[ -z "$ip" ]]; then
continue
fi
if [[ -z "${existing_ips_array["$ip"]-}" ]]; then
echo "add $ipset_name $ip -exist" >> "$tmp_ipset_restore_file"
existing_ips_array["$ip"]=1
fi
done < "$ipset_file"
done
if [[ -s "$tmp_ipset_restore_file" ]]; then
ipset restore < "$tmp_ipset_restore_file"
count=$(wc -l < "$tmp_ipset_restore_file")
echo -e "${GREEN}Загружено ${YELLOW}$count${GREEN} IP адреса(ов) в IPset лист ${YELLOW}$ipset_name${NC}"
else
echo -e "${RED}Нет новых IP адресов для добавления в IPset${NC}"
fi
rm -f "$tmp_ipset_restore_file"
exit 0
elif [[ "$mode" == "noipset" ]]; then
echo -e "\n${GREEN}Запущен режим ${YELLOW}noipset${NC}"
ipset_name="unblock"
main_ip_list_file="./main_domains/discord-main-ip-list"
main_ipset_list_file="./main_domains/discord-main-ipset-list"
voice_ip_list_file="./voice_domains/discord-voice-ip-list"
voice_ipset_list_file="./voice_domains/discord-voice-ipset-list"
echo -e "${GREEN}Генерируем списки в формате IPset из:${NC}"
generate_ipset_list "$main_ip_list_file" "$main_ipset_list_file" "$ipset_name"
generate_ipset_list "$voice_ip_list_file" "$voice_ipset_list_file" "$ipset_name"
for region_dir in ./regions/*; do
if [[ -d "$region_dir" ]]; then
region=$(basename "$region_dir")
ip_list_file="$region_dir/${region}-voice-ip"
ipset_file="$region_dir/${region}-voice-ipset"
generate_ipset_list "$ip_list_file" "$ipset_file" "$ipset_name"
fi
done
exit 0
else
existing_ipsets=$(ipset list -n | grep -vE '(^_NDM|^_UPNP)' || true)
if [[ -n "$existing_ipsets" ]]; then
mapfile -t ipset_list <<< "$existing_ipsets"
echo -e "${GREEN}\nПодготовка к генерации...${NC}"
echo -e "${GREEN}Существующие IPset листы:${NC}"
for i in "${!ipset_list[@]}"; do
echo -e "${YELLOW}$((i+1)). ${ipset_list[$i]}${NC}"
done
echo -e "${YELLOW}0. Создать новый IPset лист${NC}"
while true; do
read -rp "Выбери в какой IPset лист выполняем импорт: " ipset_option
if [[ "$ipset_option" =~ ^[0-9]+$ ]]; then
if [[ "$ipset_option" -eq 0 ]]; then
read -rp "Введите имя для IPset листа: " user_ipset_name
ipset_name=${user_ipset_name:-unblock}
break
elif (( ipset_option >= 1 && ipset_option <= ${#ipset_list[@]} )); then
ipset_name="${ipset_list[$((ipset_option-1))]}"
break
else
echo -e "${RED}Неверный номер варианта. Попробуйте снова${NC}"
fi
else
echo -e "${YELLOW}Пожалуйста, введите номер варианта${NC}"
fi
done
else
echo -e "${GREEN}\nПодготовка к генерации...${NC}"
echo -e "${RED}IPset листы отсутствуют!${NC}"
read -rp "Введите имя для нового IPset листа: " user_ipset_name
ipset_name=${user_ipset_name:-unblock}
fi
echo -e "${GREEN}Используем IPset лист: ${YELLOW}${ipset_name}${NC}"
main_ip_list_file="./main_domains/discord-main-ip-list"
main_ipset_list_file="./main_domains/discord-main-ipset-list"
voice_ip_list_file="./voice_domains/discord-voice-ip-list"
voice_ipset_list_file="./voice_domains/discord-voice-ipset-list"
echo -e "${GREEN}Генерируем списки в формате IPset из:${NC}"
generate_ipset_list "$main_ip_list_file" "$main_ipset_list_file" "$ipset_name"
generate_ipset_list "$voice_ip_list_file" "$voice_ipset_list_file" "$ipset_name"
for region_dir in ./regions/*; do
if [[ -d "$region_dir" ]]; then
region=$(basename "$region_dir")
ip_list_file="$region_dir/${region}-voice-ip"
ipset_file="$region_dir/${region}-voice-ipset"
generate_ipset_list "$ip_list_file" "$ipset_file" "$ipset_name"
fi
done
if ! ipset list "$ipset_name" > /dev/null 2>&1; then
ipset create "$ipset_name" hash:ip
echo -e "${GREEN}IPset лист ${YELLOW}$ipset_name${GREEN} создан${NC}"
else
echo -e "${GREEN}Работаем дальше...${NC}"
fi
existing_ips=$(ipset list "$ipset_name" | sed -n '/^Members:/,$p' | tail -n +2 | awk '{ print $1 }' | sort)
declare -A existing_ips_array
while IFS= read -r ip; do
ip="${ip// }"
if [[ -n "$ip" ]]; then
existing_ips_array["$ip"]=1
fi
done <<< "$existing_ips"
echo -e "${GREEN}Выбери списки для загрузки:${NC}"
echo -e "${YELLOW}1. Список с основными серверами${NC}"
echo -e "${YELLOW}2. Список с основными и всеми голосовыми серверами${NC}"
echo -e "${YELLOW}3. Список с основными и конкретными голосовыми серверами по регионам${NC}"
while true; do
read -rp "Введите номер варианта (1-3): " list_option
if [[ "$list_option" =~ ^[1-3]$ ]]; then
break
else
echo -e "${RED}Ты ошибся. Давай снова${NC}"
fi
done
selected_ipset_files=("$main_ipset_list_file")
case "$list_option" in
1)
# Вариант 1: только основные серверы
;;
2)
# Вариант 2: основные серверы и все голосовые серверы
selected_ipset_files+=("$voice_ipset_list_file")
;;
3)
# Вариант 3: основные серверы и выбранные регионы
regions=()
for region_dir in ./regions/*; do
if [[ -d "$region_dir" ]]; then
region=$(basename "$region_dir")
regions+=("$region")
fi
done
selected_regions=()
while true; do
echo -e "${YELLOW}0. Далее${NC}"
for i in "${!regions[@]}"; do
region="${regions[$i]}"
if [[ " ${selected_regions[@]} " =~ " $region " ]]; then
echo -e "${YELLOW}$((i+1)). $region ${GREEN}- Уже выбран${NC}"
else
echo -e "${YELLOW}$((i+1)). $region${NC}"
fi
done
read -rp "Выбери номер региона: " region_option
if [[ "$region_option" == "0" ]]; then
break
elif [[ "$region_option" =~ ^[0-9]+$ ]] && (( region_option >= 1 && region_option <= ${#regions[@]} )); then
region="${regions[$((region_option-1))]}"
if [[ ! " ${selected_regions[@]} " =~ " $region " ]]; then
selected_regions+=("$region")
else
echo -e "${GREEN}Регион ${YELLOW}$region${GREEN} уже выбран${NC}"
fi
else
echo -e "${RED}Похоже, что ты ошибся. Снова${NC}"
fi
done
for region in "${selected_regions[@]}"; do
ipset_file="./regions/$region/${region}-voice-ipset"
if [[ -f "$ipset_file" ]]; then
selected_ipset_files+=("$ipset_file")
else
echo -e "${RED}IPset файл для региона '$region' не найден${NC}"
fi
done
;;
esac
missing_files=()
for file in "${selected_ipset_files[@]}"; do
if [[ ! -f "$file" ]]; then
missing_files+=("$file")
fi
done
if [[ ${#missing_files[@]} -gt 0 ]]; then
echo -e "${RED}Следующие файлы не найдены:${NC}"
for file in "${missing_files[@]}"; do
echo " - $file"
done
echo -e "${RED}Завершение скрипта${NC}"
exit 1
fi
tmp_ipset_restore_file=$(mktemp)
for ipset_file in "${selected_ipset_files[@]}"; do
while IFS= read -r line; do
ip=$(echo "$line" | awk '{print $3}')
ip="${ip// }"
if [[ -z "$ip" ]]; then
continue
fi
if [[ -z "${existing_ips_array["$ip"]-}" ]]; then
echo "add $ipset_name $ip -exist" >> "$tmp_ipset_restore_file"
existing_ips_array["$ip"]=1
fi
done < "$ipset_file"
done
if [[ -s "$tmp_ipset_restore_file" ]]; then
ipset restore < "$tmp_ipset_restore_file"
count=$(wc -l < "$tmp_ipset_restore_file")
echo -e "${GREEN}Загружено ${YELLOW}$count${GREEN} IP адреса(ов) в IPset лист ${YELLOW}$ipset_name${NC}"
else
echo -e "${RED}Нет новых IP адресов для добавления в IPset${NC}"
fi
rm -f "$tmp_ipset_restore_file"
fi