-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect_node.py
167 lines (145 loc) · 5.69 KB
/
connect_node.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import json
import os
from settings import LAST_CONNECT, CONFIG_DIR, PLATFORM, clean_blocks, get_node_str, load_default_config
from fill_config import config
import click
config_path = CONFIG_DIR+'config.json'
log_path = CONFIG_DIR+'connect.log'
def print_node():
cnt = 1
sub_path = CONFIG_DIR+'groups.json'
con_path = CONFIG_DIR+'connections.json'
conn = {}
sub = {}
if (not os.path.exists(sub_path)) or (not os.path.exists(con_path)):
print('No node currently')
return
with open(sub_path, 'r', encoding='utf-8') as f:
sub = json.load(f)
with open(con_path, 'r', encoding='utf-8') as f:
conn = json.load(f)
# format {"sub_name":[node_name_list]}
info = {}
for item in sub.keys():
connections = sub[item]['connections']
cnt = 1
for i in range(len(connections)):
connections[i] = str(cnt)+'. '+conn[connections[i]]['displayName']
cnt += 1
info[sub[item]['displayName']] = connections
click.echo_via_pager(json.dumps(info, indent=4, ensure_ascii=False))
def disconnect():
if PLATFORM == 'linux':
result = os.system("pkill v2ray")
if os.path.exists(CONFIG_DIR+'connect.log'):
os.remove(CONFIG_DIR+'connect.log')
if result == 0:
print("Disconnect successfully")
else:
print("Failed to disconnect, please kill the process manually")
elif PLATFORM == 'win32':
result = os.system('taskkill /f /im v2ray.exe >nul 2>nul')
if result != 0:
print('No such task or failed to disconnect!')
else:
print("Disconnect successfully")
def current():
con_path = CONFIG_DIR+'connections.json'
if ((not os.path.exists(LAST_CONNECT)) or (not os.path.getsize(LAST_CONNECT))):
print("No connection currently!")
return
with open(LAST_CONNECT, 'r', encoding='utf-8') as f:
last_dict = json.load(f)
if last_dict['node'] == '':
print("No connection currently!")
else:
with open(con_path, mode='r') as f:
node_dict = json.load(f)
node = last_dict['node']
if node == "":
print('Error!')
return
print("Current connect: "+node_dict[node]['displayName'])
def connect_by_nodestr(node_str, path="/usr/bin/v2ray", http_port=8889, socks_port=11223):
path = path.replace("\\", '/')
if (("/" in path) and (not os.path.exists(path))):
print("No such file!")
return
if PLATFORM == 'linux':
if os.system('pgrep -x v2ray >/dev/null 2>&1') == 0:
result = os.system("pkill v2ray")
if result != 0:
print("Port occupied or no executable program")
return
elif PLATFORM == 'win32':
if os.system('tasklist -v | findstr v2ray > NUL') != 1:
result = os.system('taskkill /f /im v2ray.exe >nul 2>nul')
if result != 0:
print('Port occupied or no executable program')
return
node_name = ''
connect_info = {"node": node_str, "path": path,
"http_port": http_port, "socks_port": socks_port}
with open(CONFIG_DIR+'lastconnect.json', 'w') as f:
f.write(json.dumps(connect_info, indent=4))
if node_str == "":
print('Invalid choice')
return
with open(CONFIG_DIR+'connections.json') as f:
node_name = json.load(f)[node_str]['displayName']
pass
config(node_str, http_port, socks_port)
if PLATFORM == 'linux':
os.system("exec %s -config %s > %s 2>&1 &" %
(path, config_path, log_path))
if os.system('pgrep -x v2ray >/dev/null 2>&1') == 0:
print("Connect %s successfully" % (node_name))
return
elif PLATFORM == 'win32':
filepath, fullflname = os.path.split(path)
os.chdir(filepath)
os.system("start /b %s -config %s > %s 2>&1 &" %
(fullflname, config_path, log_path))
if os.system('tasklist -v | findstr v2ray > NUL') != 1:
print("Connect %s successfully" % (node_name))
return
print("Connect failed,check connect.log for details")
def connect_default():
if os.path.exists(CONFIG_DIR+'lastconnect.json'):
with open(CONFIG_DIR+'lastconnect.json', 'r', encoding='utf-8') as f:
info = json.load(f)
path = info['path']
path = path.replace("\\", '/')
if (("/" in path) and (not os.path.exists(path))):
print("No such file!")
return
if not os.path.exists(config_path):
print('No default config file')
return
if PLATFORM == 'linux':
os.system("exec %s -config %s > %s 2>&1 &" %
(path, config_path, log_path))
if os.system('pgrep -x v2ray >/dev/null 2>&1') == 0:
print("Connect successfully")
return
elif PLATFORM == 'win32':
os.system("start /b %s -config %s > %s 2>&1 &" %
(path, config_path, log_path))
if os.system('tasklist -v | findstr v2ray > NUL') != 1:
print("Connect successfully")
return
print('Connect failed,check connect.log for details')
else:
print('No default config file')
def connect(blocks: list):
clean_blocks(blocks)
if not blocks:
connect_default()
elif len(blocks) < 2 or len(blocks) > 2:
print('Error format,should be `connect group nodeName` ')
return
else:
conf = load_default_config()
node_str = get_node_str(blocks[len(blocks)-1])
connect_by_nodestr(
node_str, conf['path'], conf['http_port'], conf['socks_port'])