mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-05 23:47:41 +08:00
增加PROXY_SUPPLEMENT变量
1、增加proxychains4模块,用于解决pip在socks5代理时,pip无法使用全局代理的问题 2、增加`PROXY_SUPPLEMENT`变量,可以手动控制,实现使用镜像站进行更新时,但缺少pip镜像站或者GitHub镜像站时,可以使用全局代理补全缺失的代理
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import secrets
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import sys
|
||||
import threading
|
||||
from pathlib import Path
|
||||
@@ -148,6 +150,8 @@ class Settings(BaseSettings):
|
||||
GITHUB_PROXY: Optional[str] = ''
|
||||
# pip镜像站点,格式:https://pypi.tuna.tsinghua.edu.cn/simple
|
||||
PIP_PROXY: Optional[str] = ''
|
||||
# 代理补全开关,支持 True、False, 在没有配置镜像站的时候,True 使用 PROXY_HOST 补全镜像站缺失,False 在没有配置镜像站的时候,不使用全局补全
|
||||
PROXY_SUPPLEMENT: bool = True
|
||||
# 大内存模式
|
||||
BIG_MEMORY_MODE: bool = False
|
||||
|
||||
@@ -226,6 +230,58 @@ class Settings(BaseSettings):
|
||||
}
|
||||
return None
|
||||
|
||||
@property
|
||||
def PROXY_URLPARSE(self, url: str = PROXY_HOST):
|
||||
"""
|
||||
解析地址组成 - 允许其他地址调用
|
||||
"""
|
||||
if url:
|
||||
# 解析 URL
|
||||
parsed_url = urlparse(url)
|
||||
# 协议
|
||||
protocol = parsed_url.scheme or ""
|
||||
# 用户名
|
||||
username = parsed_url.username or ""
|
||||
# 密码
|
||||
password = parsed_url.password or ""
|
||||
# 主机
|
||||
host = parsed_url.hostname or ""
|
||||
# 端口:
|
||||
port = parsed_url.port or ""
|
||||
# 路径
|
||||
path = parsed_url.path or ""
|
||||
# 用户名:密码@主机:端口
|
||||
netloc = parsed_url.netloc or ""
|
||||
# 查询参数: ?key=value
|
||||
query = parsed_url.query or ""
|
||||
# 使用;分割的参数
|
||||
params = parsed_url.params or ""
|
||||
# 片段: #fragment
|
||||
fragment = parsed_url.fragment or ""
|
||||
|
||||
if not port:
|
||||
if protocol == "https":
|
||||
port = 443
|
||||
elif protocol == "http":
|
||||
port = 80
|
||||
elif protocol in {"socks5", "socks5h", "socks4", "socks4a"}:
|
||||
port = 1080
|
||||
|
||||
# 返回解析结果
|
||||
return {
|
||||
"protocol": protocol,
|
||||
"username": username,
|
||||
"password": password,
|
||||
"host": host,
|
||||
"port": port,
|
||||
"path": path,
|
||||
"netloc": netloc,
|
||||
"query": query,
|
||||
"params": params,
|
||||
"fragment": fragment
|
||||
}
|
||||
return None
|
||||
|
||||
@property
|
||||
def PROXY_SERVER(self):
|
||||
if self.PROXY_HOST:
|
||||
|
||||
+46
-2
@@ -222,8 +222,52 @@ class PluginHelper(metaclass=Singleton):
|
||||
# 插件目录下如有requirements.txt则安装依赖
|
||||
requirements_file = plugin_dir / "requirements.txt"
|
||||
if requirements_file.exists():
|
||||
PIP_PROXY = f" -i {settings.PIP_PROXY} " if settings.PIP_PROXY else ""
|
||||
SystemUtils.execute(f"pip install -r {requirements_file} {PIP_PROXY} > /dev/null 2>&1")
|
||||
# 初始化
|
||||
protocol = host = port = username = password = PROXY_CHAINS = PIP_PROXY = ""
|
||||
# 返回json格式解析结果
|
||||
parsed_url = settings.PROXY_URLPARSE()
|
||||
if parsed_url:
|
||||
protocol = parsed_url.get("scheme", "").lower()
|
||||
username = parsed_url.get("username", "")
|
||||
password = parsed_url.get("password", "")
|
||||
host = parsed_url.get("host", "").lower()
|
||||
port = parsed_url.get("port", "")
|
||||
|
||||
# 全局优先,镜像站不存在时,使用全局代理
|
||||
if settings.PROXY_SUPPLEMENT:
|
||||
|
||||
# 检查settings.PROXY_HOST的协议类型,http或https
|
||||
if protocol in {"http", "https"}:
|
||||
if settings.PIP_PROXY:
|
||||
PIP_PROXY = f" -i {settings.PIP_PROXY} " if settings.PIP_PROXY else ""
|
||||
else:
|
||||
# 有主机名与端口号的时候
|
||||
if host and port:
|
||||
PIP_PROXY = f" --proxy={settings.PROXY_HOST} " if settings.PROXY_HOST else ""
|
||||
|
||||
# Todo:目前proxychains4的临时调用命令不支持socks5h和socks4a,需要生成临时配置文件才能解决,后面考虑支持一下
|
||||
elif protocol in {"socks4", "socks4a", "socks5", "socks5h"}:
|
||||
# 没有主机名,端口号
|
||||
if not host or not port:
|
||||
PIP_PROXY = f" -i {settings.PIP_PROXY} " if settings.PIP_PROXY else ""
|
||||
# 将拓展的socks协议转换为proxychains4支持的socks4和socks5
|
||||
else:
|
||||
if protocol in {"socks5", "socks5h"}:
|
||||
protocol = "socks5"
|
||||
elif protocol in {"socks4", "socks4a"}:
|
||||
protocol = "socks4"
|
||||
# 生成配置
|
||||
PROXY_CHAINS = f"proxychains4 -f <( echo -e '[ProxyList]\n{protocol} {host} {port} {username} {password}')"
|
||||
|
||||
# 不支持的协议类型
|
||||
else:
|
||||
PIP_PROXY = f" -i {settings.PIP_PROXY} " if settings.PIP_PROXY else ""
|
||||
|
||||
# 本地优先,镜像站不存在时,不使用全局代理
|
||||
else:
|
||||
PIP_PROXY = f" -i {settings.PIP_PROXY} " if settings.PIP_PROXY else ""
|
||||
|
||||
SystemUtils.execute(f"{PROXY_CHAINS} pip install -r {requirements_file} {PIP_PROXY} > /dev/null 2>&1")
|
||||
# 安装成功后统计
|
||||
self.install_reg(pid)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user