Skip to content

Commit d68b0db

Browse files
authored
refactor: get_working_proxy
1 parent f39578e commit d68b0db

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

install_windows.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,10 @@ def get_document_path() -> str:
128128
return path
129129

130130

131-
def can_connect(url, timeout=2):
131+
def can_connect(url: str, timeout: float = 2) -> bool:
132132
try:
133-
response = requests.head(url, timeout=timeout)
134-
return response.status_code >= 200 and response.status_code < 400
135-
except requests.exceptions.RequestException:
133+
return requests.head(url, timeout=timeout).ok
134+
except requests.RequestException:
136135
return False
137136

138137

@@ -461,28 +460,26 @@ def install_plugin_store(file_path):
461460
print(f"安装插件商店发生错误: {e}\n请尝试手动安装")
462461

463462

464-
def check_proxy(proxy):
465-
try:
466-
proxy_url = f"{proxy}/https://github.com"
467-
response = requests.head(proxy_url, timeout=5)
468-
if response.ok:
469-
return proxy
470-
except requests.exceptions.RequestException:
471-
pass
472-
return None
463+
def check_proxy(url: str, timeout: float = 5) -> tuple[str, bool]:
464+
return url, can_connect(
465+
f"{url}/https://github.com/Mzdyl/LiteLoaderQQNT_Install/archive/refs/heads/main.zip",
466+
timeout,
467+
)
473468

474469

475-
def get_working_proxy():
470+
def get_working_proxy() -> str:
476471
proxies = get_github_proxy_urls()
477-
with ThreadPoolExecutor(max_workers=len(proxies)) as executor:
478-
future_to_proxy = {
479-
executor.submit(check_proxy, proxy): proxy for proxy in proxies
480-
}
481-
for future in as_completed(future_to_proxy):
482-
result = future.result()
483-
if result is not None:
484-
return result
485-
return None
472+
executor = ThreadPoolExecutor()
473+
try:
474+
for future in as_completed(
475+
[executor.submit(check_proxy, proxy) for proxy in proxies]
476+
):
477+
proxy, result = future.result()
478+
if result:
479+
return proxy
480+
return ""
481+
finally:
482+
executor.shutdown(wait=False, cancel_futures=True)
486483

487484

488485
def get_download_url(url: str) -> str:

0 commit comments

Comments
 (0)