ipprobe/ws-test.sh
2024-02-09 21:21:20 +08:00

46 lines
1.4 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 定义文件名
IP_FILE="ip.txt"
IP_USE_FILE="ip_use.txt"
# Step 1: 使用 dig 命令获取 sniproxy.youoffer.net 的所有IP地址并写入到 ip.txt 文件中
dig +short sniproxy.youoffer.net > "$IP_FILE"
# 检查是否成功获取到了IP地址
if [[ ! -s "$IP_FILE" ]]; then
echo "Failed to retrieve IP addresses."
exit 1
fi
# 读取文件中的每个IP地址并对每个地址进行测试
while IFS= read -r IP_ADDRESS; do
# 忽略空行
if [[ -z "$IP_ADDRESS" ]]; then
continue
fi
echo Testing IP address: $IP_ADDRESS""
# Step 3: 使用 curl 测试 WebSocket 连接
HTTP_RESPONSE=$(curl --include \
--no-buffer \
--max-time 5 \
--resolve speed.cloudflare.com:443:"$IP_ADDRESS" \
--header "Connection: Upgrade" \
--header "Upgrade: websocket" \
--header "Host: speed.cloudflare.com" \
--header "Origin: https://speed.cloudflare.com" \
--header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" \
--header "Sec-WebSocket-Version: 13" \
"https://speed.cloudflare.com/" --silent --output /dev/null --write-out "%{http_code}")
# Step 4: 检查 HTTP 响应码
if [[ "$HTTP_RESPONSE" == "200" ]]; then
echo "$IP_ADDRESS" >> "$IP_USE_FILE"
echo "Success: HTTP 200 OK"
else
echo "Failed: HTTP response was $HTTP_RESPONSE or connection timed out "
fi
done < "$IP_FILE"