Created
March 13, 2026 03:16
-
-
Save LeisureLinux/3bf71dfc7dfc7125761fce2a6891ff40 to your computer and use it in GitHub Desktop.
Multi-tier network connectivity check via 204 endpoints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # 本脚本需要部署在代理服务所在设备,用于对设备上内网代理服务的网络连通性检测 | |
| PROXY_SERVER="http://xxxx" | |
| lock=/tmp/.proxy.lock | |
| # URLs | |
| URL_DOMESTIC="http://connect.rom.miui.com/generate_204" | |
| URL_OVERSEAS_IP="http://1.1.1.1/generate_204" | |
| URL_GLOBAL_SVC="https://www.google.com/generate_204" | |
| # ========================================== | |
| # 探测函数 | |
| # ========================================== | |
| check_step() { | |
| local cmd=$1 | |
| local desc=$2 | |
| echo -n "正在检测 $desc... " | |
| local code=$(eval "$cmd") | |
| if [ "$code" -eq 204 ]; then | |
| echo -e "\e[32m[PASS]\e[0m ($code)" | |
| return 0 | |
| else | |
| echo -e "\e[31m[FAILED]\e[0m ($code)" | |
| return 1 | |
| fi | |
| } | |
| # --- 1. 国内直连探测 --- | |
| check_step "curl -q -I -s -o /dev/null -w '%{http_code}' --connect-timeout 5 $URL_DOMESTIC" "国内基础链路 (直连) | |
| " || exit 1 | |
| # --- 2. 跨境 IP 探测 --- | |
| # 验证代理转发及跨境 IP 连通性,注入 Host 避免 403 | |
| check_step "curl -q -I -s -o /dev/null -w '%{http_code}' --connect-timeout 5 -H 'Host:cp.cloudflare.com' $URL_OV | |
| ERSEAS_IP" "跨境 IP 路由 (Host注入)" || exit 1 | |
| # --- 3. 全球 HTTPS 服务探测 --- | |
| if check_step "curl -q -I -s -o /dev/null -w '%{http_code}' --connect-timeout 5 -x $PROXY_SERVER $URL_GLOBAL_SVC | |
| " "全球服务应用层 (Google)"; then | |
| echo "--------------------------------------" | |
| echo "所有检测阶段已通过,出海链路状态:Excellent" | |
| [ -f "$lock" ] && rm $lock && $HOME/bin/notify.sh "Home ss back to normal." | |
| exit 0 | |
| fi | |
| echo "Proxy failed, 如果依然失败,建议重启家里的路由器(即重新拨号)" | |
| systemctl --user restart sslocal kcptun && echo "Restarted sslocal+kcptun" | |
| # 如果 lock 文件存在就不再发送告警通知。 | |
| [ ! -f "$lock" ] && touch $lock && [ -x $HOME/bin/notify.sh ] && $HOME/bin/notify.sh "Home ss restarted." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment