|
6 | 6 | from dataclasses import dataclass |
7 | 7 | import sys |
8 | 8 | import os |
| 9 | +import tempfile |
| 10 | +import socket, time |
9 | 11 |
|
10 | 12 | from conf.meile_config import MeileGuiConfig |
| 13 | +from helpers.helpers import wait_for_port |
11 | 14 |
|
12 | 15 | # Platform-conditional imports |
13 | 16 | if sys.platform == 'win32': |
|
22 | 25 | elif sys.platform.startswith('linux'): |
23 | 26 | import psutil |
24 | 27 | from typedef.konstants import ConfParams |
| 28 | + import threading |
25 | 29 |
|
26 | 30 | # --------------------------------------------------------------------------- |
27 | 31 | # V2RayHandler – one class per platform, selected at the bottom of this |
@@ -50,15 +54,47 @@ def fork_v2ray(self): |
50 | 54 |
|
51 | 55 | self.v2ray_pid = v2ray_srvc_proc.pid |
52 | 56 |
|
| 57 | + |
| 58 | + |
53 | 59 | def start_daemon(self): |
| 60 | + ''' |
54 | 61 |
|
55 | 62 | print("Starting v2ray service...") |
56 | 63 |
|
57 | 64 | multiprocessing.get_context('fork') |
58 | 65 | warp_fork = Process(target=self.fork_v2ray) |
59 | 66 | warp_fork.run() |
60 | | - sleep(1.5) |
61 | | - return True |
| 67 | + ok = wait_for_port(host="127.0.0.1", port=1080, timeout=120) |
| 68 | + if ok: |
| 69 | + return True |
| 70 | + else: |
| 71 | + return False |
| 72 | + ''' |
| 73 | + print("Starting v2ray service...") |
| 74 | + |
| 75 | + try: |
| 76 | + self.fork_v2ray() |
| 77 | + except Exception as e: |
| 78 | + print(f"[start_daemon] fork_v2ray failed: {e!r}") |
| 79 | + return False |
| 80 | + |
| 81 | + result = {"ok": False} |
| 82 | + |
| 83 | + def worker(): |
| 84 | + try: |
| 85 | + result["ok"] = wait_for_port("127.0.0.1", 1080, timeout=120) |
| 86 | + except Exception as e: |
| 87 | + print(f"[start_daemon] worker error: {e!r}") |
| 88 | + result["ok"] = False |
| 89 | + |
| 90 | + t = threading.Thread(target=worker, daemon=True) |
| 91 | + t.start() |
| 92 | + |
| 93 | + while t.is_alive(): |
| 94 | + print(".", end="", flush=True) |
| 95 | + time.sleep(0.3) |
| 96 | + |
| 97 | + return result["ok"] |
62 | 98 |
|
63 | 99 | def kill_daemon(self): |
64 | 100 | v2ray_daemon_cmd = ( |
@@ -253,8 +289,8 @@ def read_v2ray_config(self): |
253 | 289 | class _DarwinV2RayHandler: |
254 | 290 | v2ray_pid = 0 |
255 | 291 |
|
256 | | - def __init__(self, script, **kwargs): |
257 | | - self.script_path = script |
| 292 | + def __init__(self, script_path, **kwargs): |
| 293 | + self.script_path = script_path |
258 | 294 | self.processes = [] |
259 | 295 | self.MeileConfig = MeileGuiConfig() |
260 | 296 |
|
@@ -325,25 +361,33 @@ def run_cmd(self, cmd, background=False): |
325 | 361 |
|
326 | 362 | def start_daemon(self): |
327 | 363 | print("Starting v2ray service...") |
| 364 | + PLIST_DIR = os.path.expanduser("~/.meile-gui/launchd") |
| 365 | + xray_plist = f"{PLIST_DIR}/app.meile.xray.plist" |
| 366 | + tun2_plist = f"{PLIST_DIR}/app.meile.tun2socks.plist" |
328 | 367 |
|
329 | 368 | privileged_commands = [ |
330 | | - "launchctl bootstrap system" |
331 | | - " /Library/LaunchDaemons/app.meile.xray.plist" |
| 369 | + 'mkdir -p "/Library/Application Support/Meile/launchd"', |
| 370 | + f'cp "{xray_plist}" "/Library/Application Support/Meile/launchd/"', |
| 371 | + f'cp "{tun2_plist}" "/Library/Application Support/Meile/launchd/"', |
| 372 | + 'chown -R root:wheel "/Library/Application Support/Meile"', |
| 373 | + 'chmod 755 "/Library/Application Support/Meile" "/Library/Application Support/Meile/launchd"', |
| 374 | + 'chmod 644 "/Library/Application Support/Meile/launchd/"*.plist', |
| 375 | + "rm -rf /Library/LaunchDaemons/app.meile.tun2socks.plist", |
| 376 | + "rm -rf /Library/LaunchDaemons/app.meile.wireguard.plist", |
| 377 | + "rm -rf /Library/LaunchDaemons/app.meile.xray.plist", |
| 378 | + "launchctl enable system/app.meile.xray", |
| 379 | + 'launchctl bootstrap system "/Library/Application Support/Meile/launchd/app.meile.xray.plist"', |
332 | 380 | ] |
333 | 381 | privileged_commands.append("sleep 3") |
334 | 382 | privileged_commands.append( |
335 | 383 | "curl --preproxy socks5://localhost:1080" |
336 | 384 | " -s https://icanhazip.com" |
337 | 385 | ) |
338 | 386 | privileged_commands.append("sleep 1") |
339 | | - privileged_commands.append( |
340 | | - "launchctl bootstrap system" |
341 | | - " /Library/LaunchDaemons/app.meile.tun2socks.plist" |
342 | | - ) |
| 387 | + privileged_commands.append(f"launchctl enable system/app.meile.tun2socks") |
| 388 | + privileged_commands.append(f'launchctl bootstrap system "/Library/Application Support/Meile/launchd/app.meile.tun2socks.plist"') |
343 | 389 | privileged_commands.append("sleep 2") |
344 | | - privileged_commands.append( |
345 | | - "ifconfig utun123 198.18.0.1 198.18.0.1 up" |
346 | | - ) |
| 390 | + privileged_commands.append("ifconfig utun123 198.18.0.1 198.18.0.1 up") |
347 | 391 |
|
348 | 392 | networks = [ |
349 | 393 | "1.0.0.0/8", |
@@ -391,12 +435,7 @@ def kill_daemon(self): |
391 | 435 | privileged_commands.append( |
392 | 436 | "ifconfig utun123 198.18.0.1 198.18.0.1 down" |
393 | 437 | ) |
394 | | - privileged_commands.append( |
395 | | - "launchctl bootout system" |
396 | | - " /Library/LaunchDaemons/app.meile.xray.plist" |
397 | | - " ; launchctl bootout system" |
398 | | - " /Library/LaunchDaemons/app.meile.tun2socks.plist" |
399 | | - ) |
| 438 | + privileged_commands.append(f'launchctl bootout system "/Library/Application Support/Meile/launchd/app.meile.xray.plist" ; launchctl bootout system "/Library/Application Support/Meile/launchd/app.meile.tun2socks.plist" ; launchctl disable system/app.meile.xray ; launchctl disable system/app.meile.tun2socks') |
400 | 439 |
|
401 | 440 | self.run_privileged_script(privileged_commands) |
402 | 441 |
|
|
0 commit comments