-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.sh
More file actions
executable file
·151 lines (129 loc) · 4.6 KB
/
Copy pathbootstrap.sh
File metadata and controls
executable file
·151 lines (129 loc) · 4.6 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
#!/usr/bin/env bash
set -euo pipefail
REPO_OWNER="GHUNLIL"
REPO_NAME="network-bbr-optimizer"
REPO_BRANCH="${BBR_REPO_BRANCH:-${CN_REPO_BRANCH:-main}}"
DEFAULT_PROXY_URL="${BBR_GITHUB_PROXY_URL:-https://gh-proxy.com/}"
GITHUB_PROXY="${BBR_GITHUB_PROXY:-${CN_GITHUB_PROXY:-auto}}"
SCRIPT_URL="https://raw.githubusercontent.com/${REPO_OWNER}/${REPO_NAME}/${REPO_BRANCH}/bbr.sh"
BOOTSTRAP_WORK_DIR=""
usage() {
cat <<'EOF'
network-bbr-optimizer bootstrap
自动下载最新版 bbr.sh 并执行。默认 auto 模式会识别中国大陆网络,
大陆服务器优先走 GitHub 代理,非大陆服务器优先直连;失败会自动换下一个地址。
用法:
bash <(curl -fsSL https://gh-proxy.com/https://raw.githubusercontent.com/GHUNLIL/network-bbr-optimizer/main/bootstrap.sh) [bbr.sh 参数]
常用:
bash <(curl -fsSL https://gh-proxy.com/https://raw.githubusercontent.com/GHUNLIL/network-bbr-optimizer/main/bootstrap.sh)
bash <(curl -fsSL https://gh-proxy.com/https://raw.githubusercontent.com/GHUNLIL/network-bbr-optimizer/main/bootstrap.sh) --quick
bash <(curl -fsSL https://gh-proxy.com/https://raw.githubusercontent.com/GHUNLIL/network-bbr-optimizer/main/bootstrap.sh) --wgmimic-required
bash <(curl -fsSL https://gh-proxy.com/https://raw.githubusercontent.com/GHUNLIL/network-bbr-optimizer/main/bootstrap.sh) --relay-audit
bash <(curl -fsSL https://gh-proxy.com/https://raw.githubusercontent.com/GHUNLIL/network-bbr-optimizer/main/bootstrap.sh) --mimic-native --dry-run
bash <(curl -fsSL https://gh-proxy.com/https://raw.githubusercontent.com/GHUNLIL/network-bbr-optimizer/main/bootstrap.sh) --china-whitelist
环境变量:
BBR_GITHUB_PROXY=auto 默认;自动识别大陆网络并选择代理/直连
BBR_GITHUB_PROXY=direct 强制直连 GitHub
BBR_GITHUB_PROXY=https://gh-proxy.com/ 强制使用指定 GitHub 代理前缀
BBR_GITHUB_PROXIES="https://gh-proxy.com/ direct"
BBR_REPO_BRANCH=main
兼容变量:
CN_GITHUB_PROXY / CN_GITHUB_PROXIES / CN_REPO_BRANCH
EOF
}
proxy_url() {
local raw_url="$1"
local proxy="$2"
case "${proxy}" in
""|direct|none)
printf '%s\n' "${raw_url}"
;;
*/)
printf '%s%s\n' "${proxy}" "${raw_url}"
;;
*)
printf '%s/%s\n' "${proxy}" "${raw_url}"
;;
esac
}
require_command() {
local command_name="$1"
if ! command -v "${command_name}" >/dev/null 2>&1; then
echo "缺少命令:${command_name}" >&2
exit 1
fi
}
country_probe() {
local body
body="$(curl -fsSL --connect-timeout 2 --max-time 4 https://ipapi.co/country 2>/dev/null || true)"
if [[ "${body}" =~ ^CN ]]; then
return 0
fi
body="$(curl -fsSL --connect-timeout 2 --max-time 4 https://ifconfig.co/country-iso 2>/dev/null || true)"
if [[ "${body}" =~ ^CN ]]; then
return 0
fi
body="$(curl -fsSL --connect-timeout 2 --max-time 4 https://myip.ipip.net 2>/dev/null || true)"
if [[ "${body}" == *"中国"* || "${body}" == *"China"* || "${body}" == *"CN"* ]]; then
return 0
fi
return 1
}
proxy_candidates() {
local candidates mode
candidates="${BBR_GITHUB_PROXIES:-${CN_GITHUB_PROXIES:-}}"
if [[ -n "${candidates}" ]]; then
printf '%s\n' "${candidates//,/ }"
return 0
fi
mode="${GITHUB_PROXY}"
case "${mode}" in
""|auto)
if country_probe; then
echo "检测到中国大陆网络,优先使用 GitHub 代理。" >&2
printf '%s direct\n' "${DEFAULT_PROXY_URL}"
else
echo "未检测到中国大陆网络,优先直连 GitHub;失败会自动尝试代理。" >&2
printf 'direct %s\n' "${DEFAULT_PROXY_URL}"
fi
;;
direct|none)
printf 'direct\n'
;;
*)
printf '%s direct\n' "${mode}"
;;
esac
}
download_script() {
local target="$1"
local proxy url
for proxy in $(proxy_candidates); do
url="$(proxy_url "${SCRIPT_URL}" "${proxy}")"
echo "正在下载:${url}" >&2
if curl -fL --connect-timeout 15 --max-time 120 --retry 2 --retry-delay 1 -o "${target}" "${url}"; then
return 0
fi
echo "下载失败,尝试下一个地址。" >&2
done
echo "无法下载 bbr.sh,请检查网络,或设置 BBR_GITHUB_PROXY=https://gh-proxy.com/" >&2
return 1
}
main() {
case "${1:-}" in
-h|--help|help)
usage
return 0
;;
esac
require_command curl
require_command mktemp
local script_path
BOOTSTRAP_WORK_DIR="$(mktemp -d)"
script_path="${BOOTSTRAP_WORK_DIR}/bbr.sh"
trap '[[ -n "${BOOTSTRAP_WORK_DIR:-}" ]] && rm -rf "${BOOTSTRAP_WORK_DIR}"' EXIT
download_script "${script_path}"
chmod +x "${script_path}"
exec bash "${script_path}" "$@"
}
main "$@"