-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_vpn.py
83 lines (65 loc) · 2.23 KB
/
set_vpn.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
# TODO: fix the exist fucntion
def exists(path, proxy_line):
with open(path, 'r') as file:
line = file.readlines()
flag = False
for i in line:
if proxy_line in i:
flag = True
break
else:
flag = False
file.close()
return flag
def deactivate(path, proxy_line):
with open(path, 'r+') as file:
line = file.readlines()
final_file = [i for i in line]
if exists(path, proxy_line):
for i in final_file:
if proxy_line in i:
del final_file[final_file.index(i)]
final_file.append(f"#{proxy_line}")
print("proxy has been DEACTIVATED successfully!")
file.truncate(0)
file.seek(0)
file.writelines(final_file)
else:
print("Proxy Config not found! disabled config added")
final_file.append(f"\n#{proxy_line}")
file.truncate(0)
file.seek(0)
file.writelines(final_file)
file.close()
def activate(path, proxy_line):
with open(path, 'r+') as file:
line = file.readlines()
final_file = [i for i in line]
if exists(path, proxy_line):
for i in final_file:
if proxy_line in i:
del final_file[final_file.index(i)]
final_file.append(proxy_line)
print("proxy has been ACTIVATED successfully!")
file.truncate(0)
file.seek(0)
file.writelines(final_file)
else:
print("Proxy Config not found! activated config added")
final_file.append("\n" + proxy_line)
file.truncate(0)
file.seek(0)
file.writelines(final_file)
file.close()
def main():
file_path = "/etc/environment"
proxy = "export http_proxy=\"http://127.0.0.1:2081/\""
user_input = input("[a]ctivate, [d]isable: ").strip().lower()
if user_input == 'a':
activate(file_path, proxy)
elif user_input == 'd':
deactivate(file_path, proxy)
else:
print('Not an option!')
if __name__ == "__main__":
main()