-
Notifications
You must be signed in to change notification settings - Fork 1
/
setproxy.py
237 lines (215 loc) · 10.6 KB
/
setproxy.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
#This code is written by Priyanshu Dwivedi, Indian Institute of Information Technology, Design and Manufacturing, Jabalpur
#Source: https://github.com/coderpd/Ubuntu-Systemwide-Proxy-Setter
import sys
import re
import fileinput
import os
if os.geteuid() != 0:
os.execvp("sudo", ["sudo"] + ["python"] + sys.argv)
def toHex(s):
lst = []
for ch in s:
hv = hex(ord(ch)).replace('0x', '')
if len(hv) == 1:
hv = '0'+hv
lst.append('%')
lst.append(hv)
return reduce(lambda x,y:x+y, lst)
print("=======================================================================================")
print("WARNING: This script will touch system variables proceed with your own risk!")
print("Note: Leave the username and password empty if your proxy doesn't need credentials")
print("Note: This script will change the system variables to use proxy, browsers uses gsetting")
print("proxy. To use the proxy in browser, put the same proxy also in Settings by yourself")
print("=======================================================================================")
print("\n\t\t\tPROXY SETTER")
print("\n1) Press 1 to set proxy to system variables")
print("2) Press 2 to remove proxy from system variables")
print("0) Press 0 to exit")
inp=raw_input("\tInput: ")
if inp==1:
proxy=raw_input("\n\tHost: ")
port=raw_input("\tPort: ")
user_string=raw_input("\tUsername: ")
password_string=raw_input("\tPassword: ")
print("\ntouching apt configurations")
os.system('sudo touch /etc/apt/apt.conf')
if len(user_string)>0:
user=toHex(user_string)
password=toHex(password_string)
flag = 0
print("modifying environment variables")
for line in fileinput.input("/etc/environment", inplace=1):
if "proxy" in line:
flag = 1
line = re.sub(r'(.*)_proxy=(.*)', r'\1_proxy="\1://'+user+':'+password+'@'+proxy+':'+port+"/\"\n", line.rstrip())
sys.stdout.write(line)
if flag == 0:
file1 = open("/etc/environment", "w")
file1.write("PATH=\"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games\"\n")
file1.write("http_proxy=\"http://"+user+":"+password+"@"+proxy+":"+port+"/\"\n")
file1.write("https_proxy=\"https://"+user+":"+password+"@"+proxy+":"+port+"/\"\n")
file1.write("ftp_proxy=\"ftp://"+user+":"+password+"@"+proxy+":"+port+"/\"\n")
file1.write("socks_proxy=\"socks://"+user+":"+password+"@"+proxy+":"+port+"/\"\n")
file1.close()
flag = 0
print("modifying apt configurations")
for line in fileinput.input("/etc/apt/apt.conf", inplace=1):
if "Acquire::" in line and "Cache" not in line:
flag = 1
line = re.sub(r'Acquire::(.*)::proxy (.*)', r'Acquire::\1::proxy "\1://'+user+":"+password+"@"+proxy+":"+port+"/\";\n", line.rstrip())
sys.stdout.write(line)
if flag == 0:
file1 = open("/etc/apt/apt.conf", "w")
file1.write("Acquire::http::proxy \"http://"+user+":"+password+"@"+proxy+":"+port+"/\";\n")
file1.write("Acquire::https::proxy \"https://"+user+":"+password+"@"+proxy+":"+port+"/\";\n")
file1.write("Acquire::ftp::proxy \"ftp://"+user+":"+password+"@"+proxy+":"+port+"/\";\n")
file1.write("Acquire::http::No-Cache \"True\";\n")
file1.write("Acquire::socks::proxy \"socks://"+user+":"+password+"@"+proxy+":"+port+"/\";\n")
file1.close()
flag = 0
print("modifying bash variables")
for line in fileinput.input("/etc/bash.bashrc", inplace=1):
if "export" in line:
flag = 1
line = re.sub(r'export (.*)_proxy=(.*)', r'export \1_proxy=\1://'+user+':'+password+'@'+proxy+':'+port+'\n', line.rstrip())
sys.stdout.write(line)
if flag == 0:
file1 = open("/etc/bash.bashrc", "r+")
l=file1.readlines()
file1.close()
file1 = open("/etc/bash.bashrc", "a")
if not l[-1][-1]=='\n':
file1.write("\n")
file1.write("export http_proxy=http://"+user+":"+password+"@"+proxy+":"+port+"\n")
file1.write("export https_proxy=https://"+user+":"+password+"@"+proxy+":"+port+"\n")
file1.write("export ftp_proxy=ftp://"+user+":"+password+"@"+proxy+":"+port+"\n")
file1.close()
flag = 0
print("modifying wgetrc variables")
for line in fileinput.input("/etc/wgetrc", inplace=1):
if not line.startswith("#") and "proxy" in line:
flag = 1
line = re.sub(r'(.*)_proxy=(.*)//(.*)', r'\1_proxy=\1://'+user+':'+password+'@'+proxy+':'+port+'\n', line.rstrip())
sys.stdout.write(line)
if flag == 0:
file1 = open("/etc/wgetrc", "r+")
l=file1.readlines()
file1.close()
file1 = open("/etc/wgetrc", "a")
if not l[-1][-1]=='\n':
file1.write("\n")
file1.write("http_proxy=http://"+user+":"+password+"@"+proxy+":"+port+"\n")
file1.write("https_proxy=https://"+user+":"+password+"@"+proxy+":"+port+"\n")
file1.write("ftp_proxy=ftp://"+user+":"+password+"@"+proxy+":"+port+"\n")
file1.close()
else:
flag = 0
print("modifying environment variables")
for line in fileinput.input("/etc/environment", inplace=1):
if "proxy" in line:
flag = 1
line = re.sub(r'(.*)_proxy=(.*)', r'\1_proxy="\1://'+proxy+':'+port+"/\"\n", line.rstrip())
sys.stdout.write(line)
if flag == 0:
file1 = open("/etc/environment", "w")
file1.write("PATH=\"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games\"\n")
file1.write("http_proxy=\"http://"+proxy+":"+port+"/\"\n")
file1.write("https_proxy=\"https://"+proxy+":"+port+"/\"\n")
file1.write("ftp_proxy=\"ftp://"+proxy+":"+port+"/\"\n")
file1.write("socks_proxy=\"socks://"+proxy+":"+port+"/\"\n")
file1.close()
flag = 0
print("modifying apt configurations")
for line in fileinput.input("/etc/apt/apt.conf", inplace=1):
if "Acquire::" in line and "Cache" not in line:
flag = 1
line = re.sub(r'Acquire::(.*)::proxy (.*)', r'Acquire::\1::proxy "\1://'+proxy+":"+port+"/\";\n", line.rstrip())
sys.stdout.write(line)
if flag == 0:
file1 = open("/etc/apt/apt.conf", "w")
file1.write("Acquire::http::proxy \"http://"+proxy+":"+port+"/\";\n")
file1.write("Acquire::https::proxy \"https://"+proxy+":"+port+"/\";\n")
file1.write("Acquire::ftp::proxy \"ftp://"+proxy+":"+port+"/\";\n")
file1.write("Acquire::http::No-Cache \"True\";\n")
file1.write("Acquire::socks::proxy \"socks://"+proxy+":"+port+"/\";\n")
file1.close()
flag = 0
print("modifying bash variables")
for line in fileinput.input("/etc/bash.bashrc", inplace=1):
if "export" in line:
flag = 1
line = re.sub(r'export (.*)_proxy=(.*)', r'export \1_proxy=\1://'+proxy+':'+port+'\n', line.rstrip())
sys.stdout.write(line)
if flag == 0:
file1 = open("/etc/bash.bashrc", "r+")
l=file1.readlines()
file1.close()
file1 = open("/etc/bash.bashrc", "a")
if not l[-1][-1]=='\n':
file1.write("\n")
file1.write("export http_proxy=http://"+proxy+":"+port+"\n")
file1.write("export https_proxy=https://"+proxy+":"+port+"\n")
file1.write("export ftp_proxy=ftp://"+proxy+":"+port+"\n")
file1.close()
flag = 0
print("modifying wgetrc variables")
for line in fileinput.input("/etc/wgetrc", inplace=1):
if not line.startswith("#") and "proxy" in line:
flag = 1
line = re.sub(r'(.*)_proxy=(.*)//(.*)', r'\1_proxy=\1://'+proxy+':'+port+'\n', line.rstrip())
sys.stdout.write(line)
if flag == 0:
file1 = open("/etc/wgetrc", "r+")
l=file1.readlines()
file1.close()
file1 = open("/etc/wgetrc", "a")
if not l[-1][-1]=='\n':
file1.write("\n")
file1.write("http_proxy=http://"+proxy+":"+port+"\n")
file1.write("https_proxy=https://"+proxy+":"+port+"\n")
file1.write("ftp_proxy=ftp://"+proxy+":"+port+"\n")
print("\t\t---Systemwide Proxy set---")
print("Please reboot your system now")
elif inp==2:
print("modifying bash variables")
with open("/etc/bash.bashrc","r+") as f:
new_f = f.readlines()
f.seek(0)
for line in new_f:
if "export http_proxy" not in line and "export https_proxy" not in line and "export ftp_proxy" not in line and "export socks_proxy" not in line:
f.write(line)
f.truncate()
f.close()
print("modifying wgetrc variables")
with open("/etc/wgetrc","r+") as f:
new_f = f.readlines()
f.seek(0)
for line in new_f:
if "http_proxy" not in line and "https_proxy" not in line and "ftp_proxy" not in line and "socks_proxy" not in line:
f.write(line)
f.truncate()
f.close()
print("modifying environment variables")
with open("/etc/environment","r+") as f:
new_f = f.readlines()
f.seek(0)
for line in new_f:
if "http_proxy" not in line and "https_proxy" not in line and "ftp_proxy" not in line and "socks_proxy" not in line:
f.write(line)
f.truncate()
f.close()
print("modifying apt variables")
with open("/etc/apt/apt.conf","r+") as f:
new_f = f.readlines()
f.seek(0)
for line in new_f:
if "Acquire::http" not in line and "Acquire::https" not in line and "Acquire::ftp" not in line and "Acquire::socks" not in line:
f.write(line)
f.truncate()
f.close()
print("modification completed...Proxy unset completed\nPlease reboot your system now")
elif inp==0:
print("exiting script")
else:
print("Wrong Input\nexiting script")
print("\n\n\t---programmed by PD")