-
Notifications
You must be signed in to change notification settings - Fork 1
/
ubproxy
executable file
·268 lines (241 loc) · 8.25 KB
/
ubproxy
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
#!/usr/bin/python
#
# "Ubproxy"
# without GTK Interface
#
# Ubuntu proxy setting tool.
# This sets the apt,bash and profile config-files.
# Network proxy-settings with user-name and password can only be configured with this tool .
#
# Supports Ubuntu 8.04 - Ubuntu 11.10 (Latest as of Jan 2012)
# An inevitable tool to configure proxy-settings in universities and office environmment.
# Eliminates the need of repetitive editing of system files that is prone to frequent manual errors.
# Atleast 3 dfferent individual config files needs to be edited to configure proxy settings,
# especially the network proxy-settings with user-name and password.
# This can be used in an environment where all the Three - ("http","https" and the "ftp") proxies have the same settings.
#
# Visit The project url at http://goo.gl/qjR9s
# Author: Sadhanandh Iyer
#
# E-mail: sadhanandhiyer[@]gmail[.]com
#
# Date: 1/2/2011
#
# Profile: http://goo.gl/IVg3g
#
# Meta:
# Sadhanandh Iyer
# Linux Community
# ABV - Indian Institute of Information Technology and Management - Gwalior
# """With power comes responsibility, so 'sudo' with care"""
#
# Powered By:
# Ubuntu 11.10 & Vim 7.3.35
#
#
# Ver. - 2.2.13
#
#Adapted by Santiago Caamanho to minimal functionality
#and invoke this script with parameters that modify its functionality
#valid parameters enable or disabled
import gtk
import os
import re
import sys
from datetime import datetime
import ConfigParser
#from Crypto.Cipher import ARC4
filenames = ["/etc/bash.bashrc", "/etc/environment"]
filename2 = "/etc/apt/apt.conf"
logs = "/var/log/proxychangerlog"
class UProxy:
def __init__(self, config_file):
self.opentext()
# Load configuration file
self.config = ConfigParser.RawConfigParser()
self.config.read(config_file)
# Set proxy values
self.host = self.config.get('ProxyConfig', 'proxy.host')
self.port = self.config.get('ProxyConfig', 'proxy.port')
if self.config.get('ProxyConfig', 'proxy.auth') == 'True':
self.auth = True
else:
self.auth = False
print ('init:')
print(self.auth)
self.uname = self.config.get('ProxyConfig', 'proxy.user')
self.passw = self.config.get('ProxyConfig', 'proxy.password')
'''
cypher = ARC4.new('01234567')
print "password:"+self.passw
self.passw = cypher.decrypt(self.passw)
print "password:"+self.passw
'''
def destroy(self, widget, data=None):
if (self.flog != None):
self.flog.close()
#Modify config files to set proxy enabled (insert proxy entries)
def enable_proxy(self):
backup([filenames[0], filenames[1], filename2])
self.flog.write("Files have been backed up in '~/.Ubuntu-Proxy/' with .backup extension \n")
clean(filename2, filenames)
# self.fin()
print ('set:')
print(self.auth)
if (self.auth):
filewrite4(self.host, self.port, self.uname, self.passw)
self.flog.write("4 var proxy changed \n")
else:
filewrite2(self.host, self.port)
self.flog.write("2 var proxy changed \n")
"""
self.mbx = gtk.MessageDialog(None, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE,
"Proxy Successfully Set")
self.mbx.run()
self.mbx.destroy()
"""
#Modify config files to set proxy disabled (delete proxy entries)
def disable_proxy(self):
backup([filenames[0], filenames[1], filename2])
self.flog.write("Files have been backed up in '~/.Ubuntu-Proxy/' with .backup extension \n")
clean(filename2, filenames)
"""
self.flog.write("Old Proxy-Settings removed \n")
self.mbx = gtk.MessageDialog(None, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE,
"Proxy Successfully Removed")
self.mbx.run()
self.mbx.destroy()
"""
#Check root permisions and open log file
def opentext(self):
try:
self.flog = open(logs, "a")
self.flog.write(str(datetime.now()) + "\n")
except:
self.flog = None
md = gtk.MessageDialog(None, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE,
"You are not a Root user --Run This as 'sudo'")
md.run()
md.destroy()
sys.exit(0)
#Outher functions
def filewrite2(srv, port):
lin = []
typ0 = ("http", "ftp", "https")
for filenam in filenames:
fil = open(filenam, "a")
for x in typ0:
if (filenam.find("bash") != -1):
lin.append("export %s_proxy=\"%s://%s:%s\"\n" % (x, x, srv, port))
else:
lin.append("%s_proxy=\"%s://%s:%s\"\n" % (x, x, srv, port))
for l in lin:
fil.write(l)
fil.close()
lin = []
lin2 = []
fil = open(filename2, "w")
for x1 in typ0:
lin2.append('Acquire::%s::proxy "%s://%s:%s/";\n' % (x1, x1, srv, port))
for l1 in lin2:
fil.write(l1)
fil.close()
def filewrite4(srv, port, name, pasw):
lin = []
typ0 = ("http", "ftp", "https")
for filenam in filenames:
fil = open(filenam, "a")
for x in typ0:
if (filenam.find("bash") != -1):
lin.append("export %s_proxy=\"%s://%s:%s@%s:%s\"\n" % (x, x, name, pasw, srv, port))
else:
lin.append("%s_proxy=\"%s://%s:%s@%s:%s\"\n" % (x, x, name, pasw, srv, port))
for l in lin:
fil.write(l)
fil.close()
lin = []
lin2 = []
fil = open(filename2, "w")
for x1 in typ0:
lin2.append('Acquire::%s::proxy "%s://%s:%s@%s:%s/";\n' % (x1, x1, name, pasw, srv, port))
for l1 in lin2:
fil.write(l1)
fil.close()
def backup(files):
homefol = os.getenv('HOME')
folder = homefol + "/.Ubuntu-Proxy/"
try:
os.mkdir(folder)
except:
pass
filstr = datetime.now().strftime('%Y%h%d_%H%M%S')
newfolder = folder + filstr + "/"
try:
os.mkdir(newfolder)
except:
pass
for fil in files:
try:
f1 = open(fil, "r")
l = f1.read()
f1.close()
if (fil.find(".") == -1):
newname = fil[fil.rfind('/') + 1:] + ".backup"
else:
fil = fil[fil.rfind('/') + 1:]
newname = fil[:fil.find(".")] + ".backup"
newname = newfolder + newname
f2 = open(newname, "w")
f2.write(l)
f2.close()
except:
pass
def clean(file2, files, para=False):
if (para):
for file1 in files:
try:
f = open(file1, 'r')
l = f.read()
l = re.sub(r'\n?(.*http_proxy\s*=\s*".*")', r'\n# \g<1>', l)
l = re.sub(r'\n?(.*https_proxy\s*=\s*".*")', r'\n# \g<1>', l)
l = re.sub(r'\n?(.*ftp_proxy\s*=\s*".*")', r'\n# \g<1>', l)
f.close()
f = open(file1, 'w')
f.write(l)
f.close()
except:
pass
else:
for file1 in files:
try:
f = open(file1, 'r')
l = f.read()
l = re.sub(r'\n?(.*http_proxy\s*=\s*".*")', r'', l)
l = re.sub(r'\n?(.*https_proxy\s*=\s*".*")', r'', l)
l = re.sub(r'\n?(.*ftp_proxy\s*=\s*".*")', r'', l)
f.close()
f = open(file1, 'w')
f.write(l)
f.close()
except:
pass
try:
f = open(file2, "r")
l = f.read()
l = re.sub(r'\n?(.*Acquire.*".*?";)', "", l)
f.close()
f = open(file2, 'w')
f.write(l)
f.close()
except:
pass
if __name__ == "__main__":
#Retrieve command line args
action = sys.argv[1]
config_file = sys.argv[2]
#Enable / Disable proxy
base = UProxy(config_file)
if action == 'enable':
base.enable_proxy()
else:
base.disable_proxy()