Skip to content

Commit

Permalink
feat: automatic acceleration selection
Browse files Browse the repository at this point in the history
  • Loading branch information
DDSRem committed Aug 5, 2024
1 parent f7d5838 commit 466b42b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 86 deletions.
119 changes: 75 additions & 44 deletions update
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
}

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】代理地址不符合要求,无法使用全局代理环境,开始使用其他更新方式"
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
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
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="不使用代理更新环境依赖"
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
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="不使用代理更新程序"
fi
return 0
;;
esac
}

if [[ "${MOVIEPILOT_AUTO_UPDATE}" = "true" ]] || [[ "${MOVIEPILOT_AUTO_UPDATE}" = "release" ]] || [[ "${MOVIEPILOT_AUTO_UPDATE}" = "dev" ]]; then
# 优先级:镜像站 > 全局 > 不代理
# pip
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}"
Expand Down
42 changes: 0 additions & 42 deletions urlparse.py

This file was deleted.

0 comments on commit 466b42b

Please sign in to comment.