diff --git a/update b/update index f0375e21..2d2422d3 100644 --- a/update +++ b/update @@ -41,8 +41,8 @@ function install_backend_and_download_resources() { if download_and_unzip "${GITHUB_PROXY}https://github.com/jxxghp/MoviePilot/archive/refs/${1}" "App"; then INFO "后端程序下载成功" INFO "依赖安装中..." - pip install ${PIP_OPTIONS} --upgrade --root-user-action pip > /dev/null - if pip install ${PIP_OPTIONS} --root-user-action -r /tmp/App/requirements.txt > /dev/null; then + pip install ${PIP_OPTIONS} --upgrade --root-user-action=ignore pip > /dev/null + if pip install ${PIP_OPTIONS} --root-user-action=ignore -r /tmp/App/requirements.txt > /dev/null; then INFO "安装依赖成功" frontend_version=$(curl ${CURL_OPTIONS} "https://api.github.com/repos/jxxghp/MoviePilot-Frontend/releases/latest" ${CURL_HEADERS} | jq -r .tag_name) if [[ "${frontend_version}" == *v* ]]; then @@ -80,7 +80,7 @@ function install_backend_and_download_resources() { # 插件仓库 rsync -av --remove-source-files /tmp/Plugins/plugins/* /app/app/plugins/ > /dev/null # 提前安装插件依赖 - find /app/app/plugins -name requirements.txt -exec pip install --root-user-action ${PIP_OPTIONS} -r {} \; > /dev/null + find /app/app/plugins -name requirements.txt -exec pip install --root-user-action=ignore ${PIP_OPTIONS} -r {} \; > /dev/null # 清理临时目录 rm -rf /tmp/* INFO "插件更新成功" @@ -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}" diff --git a/urlparse.py b/urlparse.py deleted file mode 100644 index 2bf277da..00000000 --- a/urlparse.py +++ /dev/null @@ -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)