├── ACFST2HAProxy.sh ├── README.md ├── haproxy.cfg.bk ├── ip.txt └── port80.png /ACFST2HAProxy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # $ bash ACFST2HAProxy.sh 80 6 8 230 3 | export LANG=zh_CN.UTF-8 4 | 5 | port=80 #自定义测速端口 不能为空!!! 6 | STcount=6 #测速数量 7 | speedlower=8 #自定义下载速度下限,单位为mb/s 8 | STmax=230 #测速延迟允许的最大上限 9 | ###############################################################以下脚本内容,勿动####################################################################### 10 | HAProxyport=9000 #HAProxy负载均衡端口,默认9000 11 | speedqueue_max=2 #自定义测速IP冗余量 12 | lossmax=0.75 #自定义丢包几率上限;只输出低于/等于指定丢包率的 IP,范围 0.00~1.00,0 过滤掉任何丢包的 IP 13 | speedtestMB=50 #测速文件大小 单位MB,文件过大会拖延测试时长,过小会无法测出准确速度 14 | speedurl="speed.cloudflare.com/__down?bytes=$((speedtestMB * 1000000))" #官方测速链接,开头不要带https: 15 | proxygithub="https://ghproxy.com/" #反代github加速地址,如果不需要可以将引号内容删除,如需修改请确保/结尾 例如"https://ghproxy.com/" 16 | ports=(443 2053 2083 2087 2096 8443) #判断协议使用,勿动 17 | 18 | # 选择客户端 CPU 架构 19 | archAffix(){ 20 | case "$(uname -m)" in 21 | i386 | i686 ) echo '386' ;; 22 | x86_64 | amd64 ) echo 'amd64' ;; 23 | armv8 | arm64 | aarch64 ) echo 'arm64' ;; 24 | s390x ) echo 's390x' ;; 25 | * ) red "不支持的CPU架构!" && exit 1 ;; 26 | esac 27 | } 28 | 29 | # 读取/etc/os-release文件中的ID字段 30 | os_id=$(awk -F= '/^ID=/{print $2}' /etc/os-release) 31 | Ubuntu=0 32 | # 检查不同的操作系统类型 33 | if [ "$os_id" == "ubuntu" ]; then 34 | #echo "Ubuntu系统" 35 | Ubuntu=1 36 | else 37 | echo "非Ubuntu系统,尝试使用openwrt环境运行" 38 | fi 39 | 40 | #带有自定义测速端口参数 41 | if [ -n "$1" ]; then 42 | port="$1" 43 | fi 44 | 45 | #带有测速数量参数 46 | if [ -n "$2" ]; then 47 | STcount="$2" 48 | fi 49 | 50 | #带有自定义下载速度下限参数 51 | if [ -n "$3" ]; then 52 | speedlower="$3" 53 | fi 54 | 55 | #带测速延迟允许的最大上限参数 56 | if [ -n "$4" ]; then 57 | STmax="$4" 58 | fi 59 | 60 | update_gengxinzhi=0 61 | apt_update() { 62 | if [ "$update_gengxinzhi" -eq 0 ]; then 63 | 64 | if [ "$Ubuntu" -eq 1 ]; then 65 | sudo apt update 66 | else 67 | opkg update 68 | fi 69 | 70 | update_gengxinzhi=$((update_gengxinzhi + 1)) 71 | fi 72 | } 73 | 74 | # 检测并安装软件函数 75 | apt_install() { 76 | 77 | if ! command -v "$1" &> /dev/null; then 78 | echo "$1 未安装,开始安装..." 79 | apt_update 80 | 81 | if [ "$Ubuntu" -eq 1 ]; then 82 | sudo apt install "$1" -y 83 | else 84 | opkg install "$1" 85 | fi 86 | 87 | echo "$1 安装完成!" 88 | fi 89 | 90 | 91 | } 92 | 93 | apt_install curl 94 | apt_install jq 95 | apt_install haproxy 96 | 97 | download_CloudflareST() { 98 | # 发送 API 请求获取仓库信息(替换 ) 99 | latest_version=$(curl -s https://api.github.com/repos/XIU2/CloudflareSpeedTest/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') 100 | echo "最新版本号: $latest_version" 101 | # 下载文件到当前目录 102 | if [ -z "$latest_version" ]; then 103 | latest_version="v2.2.4" 104 | fi 105 | curl -L -o CloudflareST.tar.gz "${proxygithub}https://github.com/XIU2/CloudflareSpeedTest/releases/download/$latest_version/CloudflareST_linux_$(archAffix).tar.gz" 106 | # 解压CloudflareST文件到当前目录 107 | tar -xvf CloudflareST.tar.gz CloudflareST -C / 108 | rm CloudflareST.tar.gz 109 | 110 | } 111 | 112 | # 尝试次数 113 | max_attempts=5 114 | current_attempt=1 115 | 116 | while [ $current_attempt -le $max_attempts ]; do 117 | # 检查是否存在CloudflareST文件 118 | if [ -f "CloudflareST" ]; then 119 | echo "CloudflareST 准备就绪。" 120 | break 121 | else 122 | echo "CloudflareST 未准备就绪。" 123 | echo "第 $current_attempt 次下载 CloudflareST ..." 124 | download_CloudflareST 125 | fi 126 | 127 | ((current_attempt++)) 128 | done 129 | 130 | if [ $current_attempt -gt $max_attempts ]; then 131 | echo "连续 $max_attempts 次下载失败。请检查网络环境时候可以访问github后重试。" 132 | exit 1 133 | fi 134 | 135 | download_file() { 136 | local file_name="$1" 137 | 138 | if [ -f "$file_name" ]; then 139 | echo "$file_name 准备就绪。" 140 | else 141 | echo "$file_name 未准备就绪,正在下载 ..." 142 | curl -o "$file_name" "${proxygithub}https://raw.githubusercontent.com/cmliu/AutoCloudflareSpeedTest2HAProxy/main/$file_name" 143 | fi 144 | } 145 | 146 | download_file ip.txt 147 | download_file haproxy.cfg.bk 148 | 149 | speedurlhttp="http://" # 默认为http 150 | for p in "${ports[@]}"; do 151 | if [ "$port" -eq "$p" ]; then 152 | speedurlhttp="https://" 153 | break # 找到匹配的端口后可以提前结束循环 154 | fi 155 | done 156 | 157 | # 检测log文件夹是否存在 158 | if [ ! -d "log" ]; then 159 | mkdir -p "log" 160 | fi 161 | 162 | local_IP=$(curl -m 10 -s 4.ipw.cn) 163 | #全球IP地理位置API请求和响应示例 164 | local_IP_geo=$(curl -m 10 -s http://ip-api.com/json/${local_IP}?lang=zh-CN) 165 | # 使用jq解析JSON响应并提取所需的信息 166 | status=$(echo "$local_IP_geo" | jq -r '.status') 167 | 168 | if [ "$status" = "success" ]; then 169 | countryCode=$(echo "$local_IP_geo" | jq -r '.countryCode') 170 | country=$(echo "$local_IP_geo" | jq -r '.country') 171 | regionName=$(echo "$local_IP_geo" | jq -r '.regionName') 172 | city=$(echo "$local_IP_geo" | jq -r '.city') 173 | # 如果status等于success,则显示地址信息 174 | # echo "您的地址是 ${country}${regionName}${city}" 175 | # 判断countryCode是否等于CN 176 | if [ "$countryCode" != "CN" ]; then 177 | echo "你的IP地址是 $local_IP ${country}${regionName}${city} 经确认本机网络使用了代理,请关闭代理后重试。" 178 | exit 1 # 在不是中国的情况下强行退出脚本 179 | else 180 | echo "你的IP地址是 $local_IP ${country}${regionName}${city} 经确认本机网络未使用代理..." 181 | fi 182 | else 183 | echo "你的IP地址是 $local_IP 地址判断请求失败,请自行确认为本机网络未使用代理..." 184 | fi 185 | 186 | speedurl=${speedurlhttp}${speedurl} 187 | result_csv="log/${port}.csv" 188 | Require="测速端口${port}, 需求${STcount}个优选IP, 下载速度至少${speedlower}mb/s, 延迟不超过${STmax}ms" 189 | echo $Require 190 | #./CloudflareST -tp 80 -url http://speed.cloudflare.com/__down?bytes=90000000 -dn 10 -tl 280 -p 0 -sl 10 191 | ./CloudflareST -tp $port -url $speedurl -dn $((STcount + speedqueue_max)) -tl $STmax -tlr $lossmax -p 0 -sl $speedlower -o $result_csv 192 | 193 | if [ -f "haproxy.cfg" ]; then 194 | echo "haproxy.cfg 文件已存在,删除后重新配置" 195 | rm "haproxy.cfg" 196 | fi 197 | 198 | cp "haproxy.cfg.bk" "haproxy.cfg" 199 | echo -e "listen ${HAProxyport}\n mode tcp\n bind 0.0.0.0:${HAProxyport}" >> haproxy.cfg 200 | 201 | # 创建临时文件并将要插入的内容写入临时文件 202 | tmpfile=$(mktemp) 203 | listenport=9001 204 | sed -n "2,$((STcount + 1))p" $result_csv | while read line 205 | do 206 | echo -e " server ${line%%,*}:${port} ${line%%,*}:${port} weight 5 check inter 1500 rise 1 fall 3 " >> haproxy.cfg 207 | echo -e "listen ${listenport}\n mode tcp\n bind 0.0.0.0:${listenport}\n server ${line%%,*}:${port} ${line%%,*}:${port} weight 5 check inter 1500 rise 1 fall 3 \n" >> "$tmpfile" 208 | ((listenport++)) 209 | done 210 | # 将临时文件的内容追加到 haproxy.cfg 211 | cat "$tmpfile" >> haproxy.cfg 212 | 213 | # 删除临时文件 214 | rm "$tmpfile" 215 | 216 | if [ "$Ubuntu" -eq 1 ]; then 217 | sudo cp haproxy.cfg /etc/haproxy/ 218 | else 219 | cp haproxy.cfg /etc/ 220 | fi 221 | 222 | # 检查haproxy服务的状态 223 | service haproxy status >/dev/null 2>&1 224 | clear 225 | # 检查服务的返回状态码 226 | if [ $? -eq 0 ]; then 227 | echo "HAProxy服务正在运行,重启服务" 228 | service haproxy restart 229 | else 230 | echo "HAProxy服务没有运行,启动服务" 231 | service haproxy start 232 | fi 233 | 234 | # 检查haproxy服务的状态 235 | service haproxy status >/dev/null 2>&1 236 | 237 | LocalIP=$(ip addr show | grep "inet " | grep -v 127.0.0.1 | grep -v "inet 172.17." | awk '{print $2}' | cut -d '/' -f 1 | head -n 1) 238 | 239 | # 检查服务的返回状态码 240 | if [ $? -eq 0 ]; then 241 | #clear 242 | echo "CloudflareSpeedTest 测速任务完成" 243 | echo $Require 244 | echo "HAProxy负载均衡 启动成功" 245 | echo "负载均衡详细信息面板 http://${LocalIP}:8999" 246 | echo "负载均衡IP端口: ${LocalIP}:${HAProxyport}" 247 | listenport=9001 248 | for ((i = 1; i <= STcount; i++)); do 249 | echo "${i}号优选IP端口: ${LocalIP}:${listenport}" 250 | ((listenport++)) 251 | done 252 | fi 253 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cmliu/AutoCloudflareSpeedTest2HAProxy 2 | 自动CloudflareSpeedTest后将优选IP配置部署HAProxy负载均衡 3 | 4 | 测试运行环境ubuntu-18.04-standard_18.04.1-1_amd64 5 | 6 | **优势: 无需准备域名, 可使用非TLS协议, 无敌不失联!** 7 | 8 | **劣势: 只能部署在有软路由的内网环境下, 外网无法使用** 9 | 10 | ## 一键脚本 11 | 12 | ``` bash 13 | wget -N -P ACFST2HAProxy https://raw.githubusercontent.com/cmliu/AutoCloudflareSpeedTest2HAProxy/main/ACFST2HAProxy.sh && cd ACFST2HAProxy && chmod +x ACFST2HAProxy.sh && bash ACFST2HAProxy.sh 14 | ``` 15 | 16 | 自定义测速命令 17 | ``` bash 18 | bash ACFST2HAProxy.sh [自定义测速端口] [测速数量] [下载速度下限] [测速延迟] 19 | ``` 20 | 21 | | 参数名| 中文解释| 一键脚本参数必填项 | 备注(注意!参数必须按顺序填写) | 22 | |--------------------------|----------------|-----------------|-----------------| 23 | | port | 自定义测速端口 | × | 80、8080、8880、2052、2082、2086、2095、443、2053、2083、2087、2096、8443,默认 80 **(实测下来80端口速度最快)**| 24 | | STcount |测速数量 | × | 默认 6 | 25 | | speedlower |下载速度下限 | × | 默认 8mb/s | 26 | | STmax |测速延迟 | × | 默认 230ms | 27 | 28 | ``` 29 | 例如: 30 | bash ACFST2HAProxy.sh 2052 8 8 200 31 | 32 | CloudflareSpeedTest 测速任务完成 33 | 测速端口2052, 需求8个优选IP, 下载速度至少8mb/s, 延迟不超过200ms 34 | HAProxy负载均衡 启动成功 35 | 负载均衡详细信息面板 http://内网IP:8999 36 | 负载均衡IP端口: 内网IP:9000 37 | 1号优选IP端口: 内网IP:9001 38 | 2号优选IP端口: 内网IP:9002 39 | 3号优选IP端口: 内网IP:9003 40 | 4号优选IP端口: 内网IP:9004 41 | 5号优选IP端口: 内网IP:9005 42 | 6号优选IP端口: 内网IP:9006 43 | 7号优选IP端口: 内网IP:9007 44 | 8号优选IP端口: 内网IP:9008 45 | ``` 46 | 47 | 有条件的可以自行准备脚本目录下的ip.txt文件,可实现第三方IP测速。 48 | 49 | 50 | ## 定时任务 51 | ``` bash 52 | cd /root/ACFST2HAProxy && chmod +x ACFST2HAProxy.sh && bash ACFST2HAProxy.sh 53 | ``` 54 | 55 | ## 实测效果 56 | ![alt](./port80.png) 57 | 58 | ## 文件结构 59 | ``` 60 | ACFST2HAProxy 61 | ├─ ACFST2HAProxy.sh #脚本本体 62 | ├─ CloudflareST #CloudflareST测速程序 63 | ├─ haproxy.cfg.bk #HAProxy负载均衡配置模版 64 | ├─ haproxy.cfg #HAProxy负载均衡配置 65 | ├─ ip.txt #测速IP库 66 | └─ log #测速结果 67 | ├─ 80.csv #对应端口的测速结果 68 | ... 69 | └─ 2052.csv 70 | 71 | ``` 72 | 73 | 74 | # 感谢 75 | [XIU2](https://github.com/XIU2/CloudflareSpeedTest)等 76 | -------------------------------------------------------------------------------- /haproxy.cfg.bk: -------------------------------------------------------------------------------- 1 | global 2 | log 127.0.0.1 local2 3 | chroot /root 4 | pidfile /tmp/haproxy.pid 5 | maxconn 4000 6 | user root 7 | daemon 8 | defaults 9 | mode tcp #TCP模式 10 | log global 11 | option httplog 12 | option dontlognull 13 | option http-server-close 14 | option forwardfor except 127.0.0.0/8 15 | option redispatch 16 | retries 2 17 | timeout http-request 10s 18 | timeout queue 1m 19 | timeout connect 2s #上游TCP服务器连接等待时间 20 | timeout client 1m 21 | timeout server 1m 22 | timeout http-keep-alive 10s 23 | timeout check 10s 24 | maxconn 3000 25 | 26 | listen console 27 | bind 0.0.0.0:8999 #HAProxy负载均衡详细信息面板 28 | mode http 29 | stats refresh 30s 30 | stats uri / 31 | stats admin if TRUE 32 | 33 | #listen 9000 34 | # mode tcp 35 | # bind 0.0.0.0:9000 36 | # server 172.67.213.83:80 172.67.213.83:80 weight 5 check inter 1500 rise 1 fall 3 37 | # server 104.21.77.244:80 104.21.77.244:80 weight 5 check inter 1500 rise 1 fall 3 38 | -------------------------------------------------------------------------------- /port80.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cmliu/AutoCloudflareSpeedTest2HAProxy/02c120e3f769fd9d021ca69ba05a5c10df0d6e79/port80.png --------------------------------------------------------------------------------