diff --git a/core/utils/encrypted_proxy/singbox.py b/core/utils/encrypted_proxy/singbox.py index 784e689..3589ade 100644 --- a/core/utils/encrypted_proxy/singbox.py +++ b/core/utils/encrypted_proxy/singbox.py @@ -198,5 +198,61 @@ class SingboxRunner: time.sleep(0.3) + for pid in pids: + try: + subprocess.run( + ['sudo', 'kill', '-9', str(pid)], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + stdin=subprocess.DEVNULL, + env={**os.environ, 'SUDO_ASKPASS': '/bin/false'}, + timeout=5, + ) + except (OSError, subprocess.TimeoutExpired): + pass + + except (OSError, ValueError, subprocess.TimeoutExpired): + pass + try: + result = subprocess.run( + ['pgrep', '-x', 'sing-box'], + capture_output=True, + text=True, + ) + + if result.returncode != 0: + return + + pids = [ + int(p) + for p in result.stdout.strip().splitlines() + if p.strip().isdigit() + ] + + if not pids: + return + + subprocess.run( + ['sudo', self._WRAPPER, '', 'stop'], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + stdin=subprocess.DEVNULL, + env={**os.environ, 'SUDO_ASKPASS': '/bin/false'}, + timeout=15, + ) + + deadline = time.monotonic() + 4.0 + + while time.monotonic() < deadline: + check = subprocess.run( + ['pgrep', '-x', 'sing-box'], + capture_output=True, + ) + + if check.returncode != 0: + return + + time.sleep(0.3) + except (OSError, ValueError, subprocess.TimeoutExpired): pass \ No newline at end of file