Skip to content

Commit 19746b1

Browse files
authored
Add files via upload
1 parent 0541491 commit 19746b1

File tree

2 files changed

+73
-23
lines changed

2 files changed

+73
-23
lines changed

format.sh

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
#!/bin/bash
22

3-
# 检查是否提供了文件名作为参数
3+
# =================配置区域=================
4+
# 输出文件 1: 通用格式 (一行一个 URL)
5+
output_file="formatted_trackers.txt"
6+
# 输出文件 2: Aria2 格式 (逗号分隔,无换行)
7+
output_file_aria2="formatted_trackers_aria2.txt"
8+
# =========================================
9+
10+
# 1. 检查参数
411
if [ "$#" -ne 1 ]; then
512
echo "Usage: $0 <file>"
613
exit 1
714
fi
815

916
input_file="$1"
10-
output_file="formatted_trackers.txt"
1117

12-
# 检查文件是否存在
18+
# 2. 检查输入文件
1319
if [ ! -f "$input_file" ]; then
14-
echo "Error: File not found."
20+
echo "Error: File not found: $input_file"
1521
exit 1
1622
fi
1723

18-
# 使用tr将逗号替换为换行符,然后使用grep和sed来处理文件
19-
# - 使用grep -oP 来匹配以http://, https://, udp://, wss://开头并且以/announce结尾的内容
20-
# - 不进行贪婪匹配
21-
# - 使用sed来省略默认端口
22-
# - 过滤掉包含blackstr.txt中恶意IP的URL
24+
echo "Processing trackers..."
25+
26+
# 3. 处理核心逻辑
27+
# - tr: 将逗号转为换行(兼容输入本身就是逗号分隔的情况)
28+
# - grep: 提取 http/udp/wss 协议,自动忽略行内其他杂质(如 bt-tracker= 前缀)
29+
# - sed: 去除默认端口 (80/443),去除行尾空白
30+
# - blackstr.txt: 黑名单过滤
31+
# - sort -u: 去重并排序
2332
tr ',' '\n' < "$input_file" | \
2433
grep -oP '(http|https|udp|wss)://[^/]+/announce' | \
2534
sed -E 's#(http://[^/]+):80/announce#\1/announce#; s#(https://[^/]+):443/announce#\1/announce#' | \
@@ -30,6 +39,26 @@ sed 's/[ \t]*$//' | \
3039
else
3140
cat
3241
fi
33-
} > "$output_file"
42+
} | sort -u > "$output_file"
43+
44+
# 4. 生成 Aria2 格式
45+
# 检查第一个输出文件是否有内容
46+
if [ -s "$output_file" ]; then
47+
# 使用 paste 命令将所有行合并为一行,用逗号分隔
48+
paste -sd "," "$output_file" > "$output_file_aria2"
49+
else
50+
# 如果结果为空,清空 Aria2 文件
51+
> "$output_file_aria2"
52+
echo "Warning: No valid trackers found."
53+
fi
3454

35-
echo "Formatted trackers have been saved to $output_file"
55+
# 5. 输出结果提示
56+
echo "------------------------------------------------"
57+
echo "Processing Complete."
58+
echo ""
59+
echo "1. [Standard Format] (Line-separated):"
60+
echo " -> $output_file"
61+
echo ""
62+
echo "2. [Aria2 Format] (Comma-separated):"
63+
echo " -> $output_file_aria2"
64+
echo "------------------------------------------------"

test_trackers.sh

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ if [ "$#" -ne 1 ]; then
77
fi
88

99
input_file="$1"
10+
# 输出文件定义
1011
output_file_main="trackers_best.txt"
1112
output_file_http="trackers_best_http.txt"
1213
output_file_https="trackers_best_https.txt"
1314
output_file_udp="trackers_best_udp.txt"
1415
output_file_wss="trackers_best_wss.txt"
16+
output_file_aria2="trackers_best_aria2.txt" # 新增 Aria2 输出文件
1517

1618
# 检查文件是否存在
1719
if [ ! -f "$input_file" ]; then
@@ -25,65 +27,76 @@ fi
2527
> "$output_file_https"
2628
> "$output_file_udp"
2729
> "$output_file_wss"
30+
> "$output_file_aria2"
2831

29-
# 过滤掉包含blackstr.txt中恶意IP的URL,然后逐行处理
32+
echo "Starting connectivity test..."
33+
34+
# 过滤黑名单并开始循环
3035
{
3136
if [ -f "blackstr.txt" ]; then
3237
grep -v -F -f blackstr.txt "$input_file"
3338
else
3439
cat "$input_file"
3540
fi
3641
} | while IFS= read -r tracker; do
42+
# 忽略空行
43+
[ -z "$tracker" ] && continue
44+
3745
protocol=$(echo "$tracker" | grep -oE '^[a-z]+')
38-
46+
is_alive=0
47+
3948
case $protocol in
4049
http)
4150
if curl -s -f -m 1 "$tracker" &>/dev/null; then
42-
echo "Success: $tracker"
51+
echo -e "\033[32mSuccess\033[0m: $tracker"
4352
echo "$tracker" >> "$output_file_main"
4453
echo "$tracker" >> "$output_file_http"
54+
is_alive=1
4555
else
46-
echo "Failed: $tracker"
56+
echo -e "\033[31mFailed\033[0m: $tracker"
4757
fi
4858
;;
4959
https)
5060
if curl -s -f -m 1 "$tracker" &>/dev/null; then
51-
echo "Success: $tracker"
61+
echo -e "\033[32mSuccess\033[0m: $tracker"
5262
echo "$tracker" >> "$output_file_main"
5363
echo "$tracker" >> "$output_file_https"
64+
is_alive=1
5465
else
55-
echo "Failed: $tracker"
66+
echo -e "\033[31mFailed\033[0m: $tracker"
5667
fi
5768
;;
5869
udp)
5970
host=$(echo "$tracker" | cut -d'/' -f3)
6071
port=$(echo "$host" | cut -d':' -f2)
6172
host=$(echo "$host" | cut -d':' -f1)
73+
# nc 增加 -w 1 超时
6274
if nc -zuv -w 1 "$host" "$port" &>/dev/null; then
63-
echo "Success: $tracker"
75+
echo -e "\033[32mSuccess\033[0m: $tracker"
6476
echo "$tracker" >> "$output_file_main"
6577
echo "$tracker" >> "$output_file_udp"
78+
is_alive=1
6679
else
67-
echo "Failed: $tracker"
80+
echo -e "\033[31mFailed\033[0m: $tracker"
6881
fi
6982
;;
7083
wss)
71-
# WSS tracker 测试 - 使用简单的 TCP 连接测试
7284
host=$(echo "$tracker" | sed 's|wss://||' | cut -d'/' -f1)
7385
port=$(echo "$host" | cut -d':' -f2)
7486
if [ "$port" = "$host" ]; then
75-
port=443 # WSS 默认端口
87+
port=443
7688
host=$(echo "$host" | cut -d':' -f1)
7789
else
7890
host=$(echo "$host" | cut -d':' -f1)
7991
fi
8092

8193
if nc -zv -w 1 "$host" "$port" &>/dev/null; then
82-
echo "Success: $tracker"
94+
echo -e "\033[32mSuccess\033[0m: $tracker"
8395
echo "$tracker" >> "$output_file_main"
8496
echo "$tracker" >> "$output_file_wss"
97+
is_alive=1
8598
else
86-
echo "Failed: $tracker"
99+
echo -e "\033[31mFailed\033[0m: $tracker"
87100
fi
88101
;;
89102
*)
@@ -92,4 +105,12 @@ fi
92105
esac
93106
done
94107

108+
# 生成 Aria2 格式 (将测试通过的列表合并为逗号分隔字符串)
109+
if [ -s "$output_file_main" ]; then
110+
echo "Generating Aria2 format..."
111+
paste -sd "," "$output_file_main" > "$output_file_aria2"
112+
fi
113+
95114
echo "Testing complete."
115+
echo "Best trackers saved to $output_file_main"
116+
echo "Aria2 format saved to $output_file_aria2"

0 commit comments

Comments
 (0)