-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy_man.py
executable file
·163 lines (133 loc) · 6.56 KB
/
proxy_man.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
# Copyright (c) 2011-13 Rushi Agrawal.
# See the file license.txt for copying permission.
""" Class to help apply proxy values to the system."""
import os
import sys
UNSET_EXISTING_VARIABLES = 3
DO_NOTHING = 4
class ApplyProxy:
""" Applies the proxy credentials to the system."""
def __init__(self, cred):
if cred.data['noproxy'] is True:
self.clearproxy(UNSET_EXISTING_VARIABLES)
else:
self.clearproxy(DO_NOTHING)
self.update_files(cred)
self.status = 'success'
def clearproxy(self, state):
""" Removes existing proxy from the system.
if the value of state is UNSET_EXISTING_VARIABLES, it unsets the
existing proxy environment variables from the shells spawned after
this program is executed """
# Clearing /etc/bash.bashrc file
bashrc_file = open('/etc/bash.bashrc','r')
bashrc_contents = bashrc_file.readlines()
bashrc_file.close()
bashrc_file = open('/etc/bash.bashrc', 'w')
for i in range(len(bashrc_contents)):
if bashrc_contents[i].lower().startswith(
'export http_proxy') or \
bashrc_contents[i].lower().startswith(
'export https_proxy') or \
bashrc_contents[i].lower().startswith(
'export ftp_proxy') or \
bashrc_contents[i].lower().startswith(
'unset http_proxy') or \
bashrc_contents[i].lower().startswith(
'unset ftp_proxy') or \
bashrc_contents[i].lower().startswith(
'unset https_proxy'):
bashrc_contents[i] = ''
bashrc_file.write(''.join(bashrc_contents))
if state == UNSET_EXISTING_VARIABLES:
bashrc_file.write('unset http_proxy\n')
bashrc_file.write('unset https_proxy\n')
bashrc_file.write('unset ftp_proxy\n')
bashrc_file.close()
# Clearing /etc/environment file
env_file = open('/etc/environment','r')
env_contents = env_file.readlines()
env_file.close()
env_file = open('/etc/environment','w')
for i in range(len(env_contents)):
if env_contents[i].lower().startswith('http_proxy') or \
env_contents[i].lower().startswith('https_proxy') or \
env_contents[i].lower().startswith('ftp_proxy'):
env_contents[i] = ''
env_file.write(''.join(env_contents))
env_file.close()
# Clearing /etc/apt/apt.conf file
aptconf_file = open('/etc/apt/apt.conf', 'w')
aptconf_file.write('')
aptconf_file.close()
## clear proxy from synaptic
if os.path.exists('/root/.synaptic/synaptic.conf'):
sconf_file = open('/root/.synaptic/synaptic.conf', 'r')
sconf_contents = sconf_file.readlines()
sconf_file.close()
for i in range(len(sconf_contents)):
if sconf_contents[i].find('useProxy') > -1:
sconf_contents[i] = list(sconf_contents[i])
sconf_contents[i][12] = '0'
sconf_contents[i] = ''.join(sconf_contents[i])
break
sconf_file = open('/root/.synaptic/synaptic.conf', 'w')
sconf_file.writelines(sconf_contents)
sconf_file.close()
def update_files(self, cred):
"""
Updates all the four files with provided proxy values.
Assumes all four files is already clear of all previous proxies.
"""
combo_string = {}
for proxy in ['http', 'https', 'ftp']:
combo_string[proxy] = cred.data[proxy]['proxy'] +':'+ \
cred.data[proxy]['port']
if cred.data[proxy]['use_auth'] is True:
combo_string[proxy] = cred.data[proxy]['user'] +':'+ \
cred.data[proxy]['password'] +'@'+ \
combo_string[proxy]
# Update /etc/bash.bashrc, /etc/environment, and /etc/apt/apt.conf file
bashrc_file = open('/etc/bash.bashrc', 'a')
env_file = open('/etc/environment', 'a')
aptconf_file = open('/etc/apt/apt.conf', 'a')
protocols = ['http', 'https', 'ftp']
for i in protocols:
bashrc_file.write('export ' + i + '_proxy=' + i + '://' + combo_string[i] + '/\n')
env_file.write(i + '_proxy="' + i + '://' + combo_string[i] + '/"\n')
aptconf_file.write('Acquire::' + i + '::Proxy "' + i + '://' + combo_string[i] + '/";\n')
bashrc_file.close()
env_file.close()
aptconf_file.close()
# Update /root/.synaptic/synaptic.conf file
if os.path.exists('/root/.synaptic/synaptic.conf'):
sconf_file = open('/root/.synaptic/synaptic.conf', 'r')
sconf_contents = sconf_file.readlines()
sconf_file.close()
value = {
'useProxy' : '1',
'httpProxyUser' : cred.data['http']['user'],
'httpProxyPass' : cred.data['http']['password'],
'httpProxy' : cred.data['http']['proxy'],
'httpProxyPort' : cred.data['http']['port'],
'ftpProxy' : cred.data['ftp']['proxy'],
'ftpProxyPort' : cred.data['ftp']['port'],
}
def change_value(string):
"""
For a line 'string' of synaptic.conf file, it assigns to 'name' attribute a value 'value'
"""
split_string = string.split('"')
if len(split_string) == 3:
string_name = split_string[0].split()[0]
string_value = split_string[1]
if value.has_key(string_name) == True:
string_value = value[string_name]
updated_string = ' ' + string_name + ' "' + string_value + '";\n'
return updated_string
return string
for i in range(len(sconf_contents)):
sconf_contents[i] = change_value(sconf_contents[i])
sconf_file = open('/root/.synaptic/synaptic.conf', 'w')
sconf_file.writelines(sconf_contents)
sconf_file.close()