This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 610
/
v2rayL.py
executable file
·271 lines (234 loc) · 10.2 KB
/
v2rayL.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# -*- coding:utf-8 -*-
import sys
import subprocess
import pickle
from sub2conf import Sub2Conf
from time import sleep
class V2rayL(object):
def __init__(self):
try:
with open("/etc/v2rayL/current", "rb") as f:
self.current, self.url, self.auto = pickle.load(f)
except:
self.current = "未连接VPN"
self.url = None
self.auto = False
self.subs = Sub2Conf(subs_url=self.url)
if self.auto:
try:
self.subs.update()
except:
print("\n自动更新失败: 地址错误或不存在,请更换订阅地址。")
self.update()
print("\r------------------------------------------")
print("当前状态: {}".format(self.current))
print("自动更新: {}".format("开启" if self.auto else "关闭"))
print("订阅地址:{}".format(self.url if self.url else "无"))
print("\r------------------------------------------")
def run(self):
print("\n1. 连接VPN\n2. 断开VPN\n3. 配置\n4. 订阅\n5. 查看当前状态\n0. 退出\n")
choice = input("请输入 >> ")
#choice = sys.stdin.readline().strip()
if choice == "1":
self.connect()
elif choice == "2":
self.disconnect()
elif choice == "3":
self.cgeconf()
elif choice == "4":
# self.update(flag=True)
self.subscribe()
elif choice == "5":
self.status()
elif choice == "0":
exit()
else:
print("请输入正确的选项...........\n")
self.run()
def cgeconf(self):
print("\r\n------------------------------------------\n")
print("1. 添加配置\n2. 删除配置\n0. 返回上一层\n")
choice = input("请输入 >> ")
if choice == "1":
self.addconf()
elif choice == "2":
self.delconf()
elif choice == "0":
self.run()
else:
print("\n请输入正确的选项...........\n")
self.run()
def subscribe(self):
print("\r\n------------------------------------------\n")
print("1. 开启自动更新订阅\n2. 关闭自动更新订阅\n3. 修改订阅地址\n0. 返回上一层\n")
choice = input("请输入 >> ")
if choice == "1":
with open("/etc/v2rayL/current", "wb") as jf:
self.auto = True
pickle.dump((self.current, self.url, self.auto), jf)
print("\n已开启自动更新订阅,下次进入生效\n")
print("\r------------------------------------------")
self.run()
elif choice == "2":
with open("/etc/v2rayL/current", "wb") as jf:
self.auto = False
pickle.dump((self.current, self.url, self.auto), jf)
print("\n已关闭自动更新订阅,下次进入生效\n")
print("\r------------------------------------------")
self.run()
elif choice == "3":
self.update()
elif choice == "0":
self.run()
else:
print("\n请输入正确的选项...........\n")
self.run()
def status(self):
print("\r------------------------------------------")
print("当前状态: {}".format(self.current))
print("自动更新: {}".format("开启" if self.auto else "关闭"))
print("订阅地址:{}".format(self.url if self.url else "无"))
print("\r------------------------------------------")
self.run()
def connect(self):
print("\r------------------------------------------\n")
t = self.subs.conf.items()
if not t:
print("无可连接的VPN,请输入订阅地址更新\n")
print("\r------------------------------------------\n")
self.run()
else:
print("{:^5}\t\t{:<10}\t{:<20}\n".format("序号", "协议","地区"))
tmp = dict()
num = 1
for k, v in self.subs.conf.items():
print("{:^5}\t\t{:<10}\t{:<20}\n".format(num, v["prot"], k))
tmp[num] = k
num +=1
print("{:^5}\t\t{:<10}\n".format(0, "返回上一层"))
choice = input("请输入 >> ")
if choice == "0":
self.run()
elif choice in [str(i) for i in range(num)]:
self.subs.setconf(tmp[int(choice)])
try:
print("\r\n正在连接................\n")
output = subprocess.getoutput(["sudo systemctl status v2rayL.service"])
if "Active: active" in output:
subprocess.call(["sudo systemctl restart v2rayL.service"], shell=True)
else:
subprocess.call(["sudo systemctl start v2rayL.service"], shell=True)
except:
print("连接失败,请尝试更新订阅后再次连接......")
self.run()
else:
sleep(2)
print("成功连接到VPN:{}\n".format(tmp[int(choice)]))
print("\r------------------------------------------\n")
self.current = tmp[int(choice)]
with open("/etc/v2rayL/current", "wb") as jf:
pickle.dump((self.current, self.url, self.auto), jf)
else:
print("\r------------------------------------------\n")
print("请输入正确的选项...........\n")
self.connect()
def disconnect(self):
try:
output = subprocess.getoutput(["sudo systemctl status v2rayL.service"])
if "Active: active" in output:
print("\r\n正在断开连接...............................\n")
subprocess.call(["sudo systemctl stop v2rayL.service"], shell=True)
sleep(2)
print("VPN连接已断开..............\n")
print("\r------------------------------------------\n")
self.current = "未连接至VPN"
with open("/etc/v2rayL/current", "wb") as jf:
pickle.dump((self.current, self.url, self.auto), jf)
else:
print("\r------------------------------------------\n")
print("服务未开启,无需断开连接................\n")
print("\r------------------------------------------\n")
self.run()
except Exception as e:
print("\r------------------------------------------\n")
print(e)
print("服务出错,请稍后再试.................\n")
print("\r------------------------------------------\n")
#
# def update(self, flag):
def update(self):
# if flag:
print("\r\n------------------------------------------\n")
url = input("请输入地址,输入 0 返回上一层>> ")
if url == "0":
self.subscribe()
else:
print("\r\n正在更新订阅地址............................\n")
self.subs = Sub2Conf(subs_url=url)
self.subs.update()
print("订阅地址更新完成,VPN已更新.....\n")
print("\r------------------------------------------\n")
with open("/etc/v2rayL/current", "wb") as jf:
pickle.dump((self.current, url, self.auto), jf)
self.run()
# else:
# try:
# with open("/etc/v2rayL/current", "rb") as f:
# _, self.url = pickle.load(f)
# except:
# print("\r------------------------------------------\n")
# print("\r当前不存在订阅地址,请输入订阅地址\n")
# self.update(flag=True)
# else:
# print("\r------------------------------------------\n")
# print("\r\n正在更新................\n")
# self.subs = Sub2Conf(self.url)
# print("订阅地址更新完成,VPN已更新\n")
# print("\r------------------------------------------\n")
def addconf(self):
print("\r------------------------------------------\n")
url = input("请输入配置信息链接, 0返回上一层\n(目前支持 vmess://和ss://)>>> ")
if url == "0":
self.run()
else:
print("\r\n正在添加配置...................\n")
self.subs = Sub2Conf(conf_url=url)
self.subs.add_conf_by_uri()
print("配置添加成功,VPN已更新.....\n")
self.run()
def delconf(self):
print("\r------------------------------------------\n")
t = self.subs.conf.items()
if not t:
print("当前无可连接VPN.\n")
print("\r------------------------------------------\n")
self.run()
else:
print("{:^5}\t\t{:<10}\t{:<20}\n".format("序号", "协议","地区"))
tmp = dict()
num = 1
for k, v in self.subs.conf.items():
print("{:^5}\t\t{:<10}\t{:<20}\n".format(num, v["prot"], k))
tmp[num] = k
num +=1
print("{:^5}\t\t{:<10}\n".format(0, "返回上一层"))
choice = input("请输入需要删除的序号 >> ")
if choice == "0":
self.run()
elif choice in [str(i) for i in range(num)]:
self.subs.delconf(tmp[int(choice)])
print("成功删除配置:{}\n".format(tmp[int(choice)]))
print("\r------------------------------------------\n")
self.run()
else:
print("\r------------------------------------------\n")
print("请输入正确的选项...........\n")
self.delconf()
if __name__ == '__main__':
v = V2rayL()
v.run()
# t = subprocess.getoutput(["sudo systemctl status v2rayL.service"])
# print("===================")
# print(t)
# print("=====================")
# print("Active: active" in t)