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

119
update
View File

@@ -116,55 +116,86 @@ function install_backend_and_download_resources() {
fi
}
# 使用python进行URL解析$1为PROXY_HOST
function parse_url() {
local result
result=$(python3 /app/parse_url.py ${1})
# 解析结果并提取各项
PROTOCOL=$(echo "$result" | grep "^SCHEME:" | awk '{print $2}')
HOST=$(echo "$result" | grep "^HOST:" | awk '{print $2}')
PORT=$(echo "$result" | grep "^PORT:" | awk '{print $2}')
function test_connectivity_pip() {
pip uninstall -y pip-hello-world > /dev/null 2>&1
case "$1" in
0)
if [[ -n "${PIP_PROXY}" ]]; then
if pip install -i ${PIP_PROXY} pip-hello-world > /dev/null 2>&1; then
PIP_OPTIONS="-i ${PIP_PROXY}"
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 [[ -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
if [[ -n "${PIP_PROXY}" ]]; then
PIP_OPTIONS="-i ${PIP_PROXY}"
PIP_LOG="使用Pip镜像代理更新环境依赖"
elif [[ -n "${PROXY_HOST}" && "${PROXY_HOST_MODE}" = "true" ]]; then
PIP_OPTIONS="--proxy=${PROXY_HOST}"
PIP_LOG="使用全局代理更新环境依赖"
else
PIP_OPTIONS=""
PIP_LOG="不使用代理更新环境依赖"
fi
# GitHub
if [[ -n "${GITHUB_PROXY}" ]]; then
GITHUB_LOG="使用Github镜像代理更新程序"
elif [[ -n "${PROXY_HOST}" && "${PROXY_HOST_MODE}" = "true" ]]; then
CURL_OPTIONS="-sL -x ${PROXY_HOST}"
GITHUB_LOG="使用全局代理更新程序"
else
CURL_OPTIONS="-sL"
GITHUB_LOG="不使用代理更新程序"
fi
retries=0
while true; do
if test_connectivity_pip ${retries}; then
break
else
retries=$((retries + 1))
fi
done
# Github
retries=0
while true; do
if test_connectivity_github ${retries}; then
break
else
retries=$((retries + 1))
fi
done
INFO "${PIP_LOG}${GITHUB_LOG}"
if [ -n "${GITHUB_TOKEN}" ]; then
CURL_HEADERS="--oauth2-bearer ${GITHUB_TOKEN}"

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)