Skip to content

Commit cebe7e6

Browse files
Add files via upload
1 parent 0faa280 commit cebe7e6

1 file changed

Lines changed: 31 additions & 17 deletions

File tree

python_v2ray/downloader.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,26 @@ def _get_asset_url(self, assets: list, name_prefix: str) -> Optional[str]:
5959
return None
6060

6161
def ensure_binary(self, name: str, target_dir: Path, repo: str) -> bool:
62-
exe_name = f"{name}.exe" if sys.platform == "win32" else name
62+
63+
exe_name = ""
64+
if sys.platform == "win32":
65+
exe_name = f"{name}.exe"
66+
elif sys.platform == "darwin":
67+
exe_name = f"{name}_macos"
68+
else: # Assuming Linux
69+
exe_name = f"{name}_linux"
70+
6371
target_file = target_dir / exe_name
6472

73+
6574
if target_file.is_file():
6675
print(f"* Binary '{exe_name}' already exists.")
6776
return True
6877

6978
print(f"! Binary '{exe_name}' not found. Downloading from '{repo}'...")
7079
try:
7180
if name == "hysteria":
72-
release_url = f"https://api.github.com/repos/{repo}/releases/tags/app%2Fv2.6.2" # ! Hardcoded to a specific stable version
81+
release_url = f"https://api.github.com/repos/{repo}/releases/tags/app%2Fv2.6.2"
7382
else:
7483
release_url = f"https://api.github.com/repos/{repo}/releases/latest"
7584

@@ -93,29 +102,34 @@ def ensure_binary(self, name: str, target_dir: Path, repo: str) -> bool:
93102
f.write(asset_response.content)
94103
else:
95104
with zipfile.ZipFile(io.BytesIO(asset_response.content)) as z:
105+
member_to_extract = ""
106+
possible_names = [f"{name}.exe", name, "xray"]
96107
for member_name in z.namelist():
97-
if Path(member_name).name.lower() == exe_name.lower():
98-
source = z.open(member_name)
99-
target = open(target_file, "wb")
100-
with source, target:
101-
target.write(source.read())
102-
103-
for dat_file in ["geoip.dat", "geosite.dat"]:
104-
if dat_file in z.namelist():
105-
with z.open(dat_file) as source_dat, open(
106-
target_dir / dat_file, "wb"
107-
) as target_dat:
108-
target_dat.write(source_dat.read())
109-
110-
print(f"* Successfully downloaded '{exe_name}'.")
108+
if Path(member_name).name.lower() in possible_names:
109+
member_to_extract = member_name
110+
break
111+
112+
if not member_to_extract:
113+
raise FileNotFoundError(f"Could not find executable for '{name}' inside the zip file.")
114+
115+
with z.open(member_to_extract) as source, open(target_file, "wb") as target:
116+
target.write(source.read())
117+
118+
for dat_file in ["geoip.dat", "geosite.dat"]:
119+
if dat_file in z.namelist():
120+
with z.open(dat_file) as source_dat, open(
121+
target_dir / dat_file, "wb"
122+
) as target_dat:
123+
target_dat.write(source_dat.read())
124+
125+
print(f"* Successfully downloaded and saved as '{exe_name}'.")
111126
if sys.platform != "win32":
112127
os.chmod(target_file, 0o755)
113128
return True
114129

115130
except Exception as e:
116131
print(f"! ERROR during download/extraction for '{name}': {e}")
117132
return False
118-
119133
def ensure_all(self):
120134
print("--- Checking for necessary binaries & databases ---")
121135
self.vendor_path.mkdir(exist_ok=True)

0 commit comments

Comments
 (0)