This repository was archived by the owner on Feb 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathclash.py
More file actions
60 lines (51 loc) · 1.78 KB
/
clash.py
File metadata and controls
60 lines (51 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import subprocess
import sys
import signal
import psutil
from function import *
class Clash(object):
def __init__(self):
self._process = None
self._pid = None
def start_client(self):
try:
# 检查端口是否被占用,如果被占用则终止对应进程
self._check_and_terminate_process(self, 7890)
self._check_and_terminate_process(self, 9090)
client = f'{os.getcwd()}/mihomo'
self._process = subprocess.Popen([f"{client}", '-f', './sub.yaml'], text=True)
logger.info("启动mihomo客户端")
sleep(30)
except Exception as e:
logger.error(str(e))
sys.exit(1)
def _before_stop_client(self):
pass
def stop_client(self):
self._before_stop_client()
if self._process is not None:
self._process.send_signal(signal.SIGINT)
self._check_and_terminate_process(self, 7890)
self._check_and_terminate_process(self, 9090)
# 等待进程退出
self._process.wait()
self._process = None
logger.info("Client Stop!")
@staticmethod
def _check_and_terminate_process(self, port):
try:
# 查找占用指定端口的进程
for conn in psutil.net_connections(kind='inet'):
if conn.laddr.port == port:
pid = conn.pid
process = psutil.Process(pid)
process.terminate()
logger.info(f"端口{port}占用已解除,PID:{pid}")
except Exception as e:
logger.error(f"错误: {str(e)}")
if __name__ == '__main__':
pass
# clash = Clash()
# clash.start_client()
# clash.stop_client()