feat: automatic acceleration selection

This commit is contained in:
DDSRem
2024-08-05 15:52:58 +08:00
parent f7d583856f
commit 466b42bea7
2 changed files with 75 additions and 86 deletions
+75 -44
View File
@@ -116,55 +116,86 @@ function install_backend_and_download_resources() {
fi fi
} }
# 使用python进行URL解析,$1为PROXY_HOST function test_connectivity_pip() {
function parse_url() { pip uninstall -y pip-hello-world > /dev/null 2>&1
local result case "$1" in
result=$(python3 /app/parse_url.py ${1}) 0)
# 解析结果并提取各项 if [[ -n "${PIP_PROXY}" ]]; then
PROTOCOL=$(echo "$result" | grep "^SCHEME:" | awk '{print $2}') if pip install -i ${PIP_PROXY} pip-hello-world > /dev/null 2>&1; then
HOST=$(echo "$result" | grep "^HOST:" | awk '{print $2}') PIP_OPTIONS="-i ${PIP_PROXY}"
PORT=$(echo "$result" | grep "^PORT:" | awk '{print $2}') PIP_LOG="使用Pip镜像代理更新环境依赖"
return 0
fi
fi
return 1
;;
1)
if [[ -n "${PROXY_HOST}" ]]; then
if pip install --proxy=${PROXY_HOST} pip-hello-world > /dev/null 2>&1; then
PIP_OPTIONS="--proxy=${PROXY_HOST}"
PIP_LOG="使用全局代理更新环境依赖"
return 0
fi
fi
return 1
;;
2)
PIP_OPTIONS=""
PIP_LOG="不使用代理更新环境依赖"
return 0
;;
esac
}
function test_connectivity_github() {
case "$1" in
0)
if [[ -n "${GITHUB_PROXY}" ]]; then
if curl -sL "${GITHUB_PROXY}https://raw.githubusercontent.com/jxxghp/MoviePilot/main/README.md" > /dev/null 2>&1; then
PIP_LOG="使用Pip镜像代理更新环境依赖"
return 0
fi
fi
return 1
;;
1)
if [[ -n "${PROXY_HOST}" ]]; then
if curl -sL -x ${PROXY_HOST} https://raw.githubusercontent.com/jxxghp/MoviePilot/main/README.md > /dev/null 2>&1; then
CURL_OPTIONS="-sL -x ${PROXY_HOST}"
GITHUB_LOG="使用全局代理更新程序"
return 0
fi
fi
return 1
;;
2)
CURL_OPTIONS="-sL"
GITHUB_LOG="不使用代理更新程序"
return 0
;;
esac
} }
if [[ "${MOVIEPILOT_AUTO_UPDATE}" = "true" ]] || [[ "${MOVIEPILOT_AUTO_UPDATE}" = "release" ]] || [[ "${MOVIEPILOT_AUTO_UPDATE}" = "dev" ]]; then if [[ "${MOVIEPILOT_AUTO_UPDATE}" = "true" ]] || [[ "${MOVIEPILOT_AUTO_UPDATE}" = "release" ]] || [[ "${MOVIEPILOT_AUTO_UPDATE}" = "dev" ]]; then
# 解析代理地址,判断代理合法性
if [[ -n "${PROXY_HOST}" ]]; then
parse_url "${PROXY_HOST}"
INFO "检测到代理地址,开始解析..."
if [[ "${PROTOCOL}" =~ ^(http|https|socks4|socks4a|socks5|socks5h)$ ]] && [[ -n "${HOST}" && -n "${PORT}" ]]; then
PROXY_HOST_MODE="true"
else
PROXY_HOST_MODE="false"
WARN "【PROXY_HOST】代理地址不符合要求,无法使用全局代理环境,开始使用其他更新方式"
fi
fi
# 初始化变量
PIP_PROXY=${PIP_PROXY:-""}
GITHUB_PROXY=${GITHUB_PROXY:-""}
PIP_OPTIONS=""
CURL_OPTIONS="-sL"
# 优先级:镜像站 > 全局 > 不代理 # 优先级:镜像站 > 全局 > 不代理
# pip # pip
if [[ -n "${PIP_PROXY}" ]]; then retries=0
PIP_OPTIONS="-i ${PIP_PROXY}" while true; do
PIP_LOG="使用Pip镜像代理更新环境依赖" if test_connectivity_pip ${retries}; then
elif [[ -n "${PROXY_HOST}" && "${PROXY_HOST_MODE}" = "true" ]]; then break
PIP_OPTIONS="--proxy=${PROXY_HOST}" else
PIP_LOG="使用全局代理更新环境依赖" retries=$((retries + 1))
else fi
PIP_OPTIONS="" done
PIP_LOG="不使用代理更新环境依赖" # Github
fi retries=0
# GitHub while true; do
if [[ -n "${GITHUB_PROXY}" ]]; then if test_connectivity_github ${retries}; then
GITHUB_LOG="使用Github镜像代理更新程序" break
elif [[ -n "${PROXY_HOST}" && "${PROXY_HOST_MODE}" = "true" ]]; then else
CURL_OPTIONS="-sL -x ${PROXY_HOST}" retries=$((retries + 1))
GITHUB_LOG="使用全局代理更新程序" fi
else done
CURL_OPTIONS="-sL"
GITHUB_LOG="不使用代理更新程序"
fi
INFO "${PIP_LOG}${GITHUB_LOG}" INFO "${PIP_LOG}${GITHUB_LOG}"
if [ -n "${GITHUB_TOKEN}" ]; then if [ -n "${GITHUB_TOKEN}" ]; then
CURL_HEADERS="--oauth2-bearer ${GITHUB_TOKEN}" CURL_HEADERS="--oauth2-bearer ${GITHUB_TOKEN}"
-42
View File
@@ -1,42 +0,0 @@
import sys
from urllib.parse import urlparse
def parse_url(url):
parsed_url = urlparse(url)
# 提取各个部分
protocol = parsed_url.scheme or ""
hostname = parsed_url.hostname or ""
port = parsed_url.port or ""
if hostname:
hostname = hostname.lower()
if not port:
if protocol == "https":
port = 443
elif protocol == "http":
port = 80
elif protocol in {"socks5", "socks5h", "socks4", "socks4a"}:
port = 1080
if protocol:
protocol = protocol.lower()
# 打印提取的部分
print(f"SCHEME:{protocol}")
print(f"HOST:{hostname}")
print(f"PORT:{port}")
if __name__ == "__main__":
# 参数不全,直接返回空解析结果
if len(sys.argv) != 2:
print(f"SCHEME:''")
print(f"HOST:''")
print(f"PORT:''")
# 参数全,解析URL
else:
PROXY_HOST = sys.argv[1]
parse_url(url=PROXY_HOST)