fix: 绿联接口支持可配置SSL证书校验

This commit is contained in:
doumao
2026-02-28 22:55:47 +08:00
parent 66e199d516
commit efc68ae701
4 changed files with 153 additions and 7 deletions

View File

@@ -48,6 +48,7 @@ class Api:
"_language",
"_ug_agent",
"_timeout",
"_verify_ssl",
)
def __init__(
@@ -57,6 +58,7 @@ class Api:
language: str = "zh-CN",
ug_agent: str = "PC/WEB",
timeout: int = 20,
verify_ssl: bool = True,
):
self._host = self._normalize_base_url(host)
self._session = Session()
@@ -73,6 +75,8 @@ class Api:
self._language = language
self._ug_agent = ug_agent
self._timeout = timeout
# 是否校验证书,默认开启;仅在用户明确配置时才应关闭。
self._verify_ssl = bool(verify_ssl)
@property
def host(self) -> str:
@@ -167,7 +171,7 @@ class Api:
params=params,
json=json_data,
timeout=self._timeout,
verify=False,
verify=self._verify_ssl,
)
else:
resp = self._session.get(
@@ -175,7 +179,7 @@ class Api:
headers=headers,
params=params,
timeout=self._timeout,
verify=False,
verify=self._verify_ssl,
)
return resp.json()
except Exception as err:
@@ -219,7 +223,7 @@ class Api:
headers=headers,
json={"username": username},
timeout=self._timeout,
verify=False,
verify=self._verify_ssl,
)
check_json = check_resp.json()
except Exception as err:
@@ -378,7 +382,7 @@ class Api:
headers=req.headers,
params=req.params,
timeout=self._timeout,
verify=False,
verify=self._verify_ssl,
)
except Exception:
pass

View File

@@ -26,6 +26,7 @@ class Ugreen:
_library_paths: dict[str, str] = {}
_sync_libraries: List[str] = []
_scan_type: int = 2
_verify_ssl: bool = True
_api: Optional[Api] = None
@@ -38,6 +39,7 @@ class Ugreen:
sync_libraries: Optional[list] = None,
scan_mode: Optional[Union[str, int]] = None,
scan_type: Optional[Union[str, int]] = None,
verify_ssl: Optional[Union[bool, str, int]] = True,
**kwargs,
):
if not host or not username or not password:
@@ -51,6 +53,8 @@ class Ugreen:
# 绿联媒体库扫描模式:
# 1 新添加和修改、2 补充缺失、3 覆盖扫描
self._scan_type = self.__resolve_scan_type(scan_mode=scan_mode, scan_type=scan_type)
# HTTPS 证书校验开关:默认开启,仅兼容自签证书等场景下可关闭。
self._verify_ssl = self.__resolve_verify_ssl(verify_ssl)
if play_host:
self._playhost = UrlUtils.standardize_base_url(play_host).rstrip("/")
@@ -144,7 +148,7 @@ class Ugreen:
self.__remove_persisted_session()
return False
api = Api(host=self._host)
api = Api(host=self._host, verify_ssl=self._verify_ssl)
if not api.import_session_state(cached):
api.close()
self.__remove_persisted_session()
@@ -174,7 +178,7 @@ class Ugreen:
self.get_librarys()
return True
self._api = Api(host=self._host)
self._api = Api(host=self._host, verify_ssl=self._verify_ssl)
if self._api.login(self._username, self._password) is None:
self.__remove_persisted_session()
return False
@@ -454,6 +458,19 @@ class Ugreen:
}
return mode_map.get(mode, 2)
@staticmethod
def __resolve_verify_ssl(verify_ssl: Optional[Union[bool, str, int]]) -> bool:
if isinstance(verify_ssl, bool):
return verify_ssl
if verify_ssl is None:
return True
value = str(verify_ssl).strip().lower()
if value in {"1", "true", "yes", "on"}:
return True
if value in {"0", "false", "no", "off"}:
return False
return True
def __scan_library(self, library_id: str, scan_type: Optional[int] = None) -> bool:
if not self._api:
return False
@@ -561,7 +578,7 @@ class Ugreen:
if not username or not password or not self._host:
return None
api = Api(self._host)
api = Api(self._host, verify_ssl=self._verify_ssl)
try:
return api.login(username, password)
finally: