-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathproxydriver.sh
executable file
·312 lines (256 loc) · 10.9 KB
/
proxydriver.sh
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/bin/bash
# This script will set gnome/KDE/shell proxy configuration for each SSID
# Version: 1.61
#
# Current script maintainer:
# - Julien Blitte <julien.blitte at gmail.com>
#
# Authors and contributors:
# - Berend Deschouwer <berend.deschouwer at ucs-software.co.za>
# - Ivan Gusev <ivgergus at gmail.com>
# - Jean-Baptiste Masurel <jbmasurel at gmail.com>
# - Julien Blitte <julien.blitte at gmail.com>
# - Milos Pejovic <pejovic at gmail.com>
# - Sergiy S. Kolesnikov <kolesnik at fim.uni-passau.de>
# - Tom Herrmann <mail at herrmann-tom.de>
# - Ulrik Stervbo <ulrik.stervbo at gmail.com>
#
# To install this file, place it in directory (with +x mod):
# /etc/NetworkManager/dispatcher.d
#
# For each new SSID, after a first connection, complete the generated file
# /etc/proxydriver.d/<ssid_name>.conf and then re-connect to AP, proxy is now set!
#
conf_dir='/etc/proxydriver.d'
log_tag='proxydriver'
running_device='/var/run/proxydriver.device'
proxy_env='/var/lib/proxydriver/environment.sh'
logger -p user.debug -t $log_tag "script called: $*"
# vpn disconnection handling
if [ "$2" == "up" ]
then
echo "$1" > "$running_device"
elif [ "$2" == "vpn-down" ]
then
set -- `cat "$running_device"` "up"
fi
if [ "$2" == "up" -o "$2" == "vpn-up" ]
then
logger -p user.notice -t $log_tag "interface '$1' now up, will try to setup proxy configuration..."
[ -d "$conf_dir" ] || mkdir --parents "$conf_dir"
if type -P nmcli &>/dev/null
then
## working with: nmcli tool, version 1.4.2
networkID=`nmcli -t -f active,ssid dev wifi | egrep '^yes' | cut -d':' -f2`
else
# try ESSID if nmcli is not installed
logger -p user.notice -t $log_tag "nmcli not detected, will use essid"
networkID=`iwgetid --scheme`
[ $? -ne 0 ] && networkID='default'
fi
# we did not get solve network name
[ -z "$networkID" ] && networkID='default'
# strip out anything hostile to the file system
networkID=`echo "$networkID" | tr -c '[:alnum:]-' '_' | sed 's/.$/\n/'`
logger -p user.notice -t $log_tag "network ID: $networkID"
conf="$conf_dir/$networkID.conf"
logger -p user.notice -t $log_tag "using configuration file '$conf'"
if [ ! -e "$conf" ]
then
logger -p user.notice -t $log_tag "configuration file empty! generating skeleton..."
touch "$conf"
cat <<EOF > "$conf"
# configuration file for proxydriver
# file auto-generated, please complete me!
# manage proxy configuration for network interface when an event is dispatched
# for the current network configuration
# if value is ['true'|'1'|'yes'] then proxy configuration will be updated
# else the proxy configuration will not be updated
managed='true'
# proxy active or not
enabled='false'
# change proxy details only for environment
only_env='false'
# proxy configuration is given by HTTP proxy auto-config (PAC)
# if used, remove comment char '#' at begin of the line
# autoconfig_url=''
# main proxy settings
# if not HTTP proxy auto-config
proxy='proxy.domain.com'
port=8080
# use same proxy for all protocols
same='true'
# protocols other than http
# if not proxy auto-config and if same is set to 'false'
https_proxy='proxy.domain.com'
https_port=8080
ftp_proxy='proxy.domain.com'
ftp_port=8080
socks_proxy='proxy.domain.com'
socks_port=8080
# authentication for Gnome
# for KDE, it is detected automaticaly
auth='false'
login='admin'
pass='pass'
# ignore-list
ignorelist='localhost,127.0.0.0/8,10.0.0.0/8,192.168.0.0/16,172.16.0.0/12'
EOF
chown root:dip "$conf"
chmod 0664 "$conf"
fi
# read configfile
logger -p user.notice -t $log_tag "reading configuration file '$conf'"
source "$conf"
# check is network is unmanaged
if [ "$managed" != 'true' -a "$managed" != '1' -a "$managed" != 'yes' ]
then
logger -p user.notice -t $log_tag "proxy configuation handling skipped for unmanaged network '$networkID'"
exit 0
fi
# select mode using enabled value
logger -p user.notice -t $log_tag "selecting mode"
if [ "$enabled" == 'true' -o "$enabled" == '1' -o "$enabled" == 'yes' ]
then
enabled='true' # gnome enable
kde_mode='1' # kde fixed proxy
if [ -n "$autoconfig_url" ]
then
gnome_mode='auto' # gnome autoconfig
kde_mode='2' # kde autoconfig
else
gnome_mode='manual' # gnome manual
fi
else
enabled='false'
kde_mode='0' # kde disabled
gnome_mode='none' # gnome disabled
fi
#kde_mode> 0: No proxy - 1: Manual - 2: Config url - 3: Automatic (DHCP?) - 4: Use env variables
#gnome_mode> 'none': No proxy - 'manual': Manual - 'auto': Config url
if [ "$same" == 'true' -o "$same" == '1' -o "$same" == 'yes' -o -z "$same" ]
then
same='true'
https_proxy="$proxy"
https_port="$port"
ftp_proxy="$proxy"
ftp_port="$port"
socks_proxy="$proxy"
socks_port="$port"
fi
if [ "$auth" == 'true' -o "$auth" == '1' -o "$auth" == 'yes' ]
then
auth='true'
shell_auth="$login:$pass@"
else
auth='false'
login=''
pass=''
shell_auth=''
fi
ignorelist=`echo $ignorelist | sed 's/^\[\(.*\)\]$/\1/'`
# gnome2 needs [localhost,127.0.0.0/8]
# gnome3 needs ['localhost','127.0.0.0/8']
# neither works with the other's settings
quoted_ignorelist=`echo $ignorelist | sed "s/[^,]\+/'\0'/g"`
gnome2_ignorelist="[${ignorelist}]"
gnome3_ignorelist="[${quoted_ignorelist}]"
# Gnome likes *.example.com; kde likes .example.com:
kde_ignorelist=`echo "${ignorelist}" | sed -e 's/\*\./\./g'`
# wait if no users are logged in (up to 5 minutes)
#connect_timer=0
#while [ -z "$(users)" -a $connect_timer -lt 300 ]
#do
# let connect_timer=connect_timer+10
# logger -p user.notice -t $log_tag "sleeping for 10 seconds"
# sleep 10
#done
# a user just logged in; give some time to settle things down
#if [ $connect_timer -gt 0 -a $connect_timer -lt 300 ]
#then
# logger -p user.notice -t $log_tag "sleeping for 15 seconds"
# sleep 15
#fi
if [ "$only_env" == 'false' -o "$only_env" == '0' -o "$only_env" == 'no' ]
then
machineid=$(dbus-uuidgen --get)
for user in `users | tr ' ' '\n' | sort --unique`
do
logger -p user.notice -t $log_tag "setting configuration for '$user'"
cat <<EOS | su -l "$user"
export \$(DISPLAY=':0.0' dbus-launch --autolaunch="$machineid")
# active or not
gconftool-2 --type bool --set /system/http_proxy/use_http_proxy "$enabled"
gsettings set org.gnome.system.proxy.http enabled "$enabled"
gconftool-2 --type string --set /system/proxy/mode "$gnome_mode"
gsettings set org.gnome.system.proxy mode "$gnome_mode"
kwriteconfig --file kioslaverc --group 'Proxy Settings' --key ProxyType "${kde_mode}"
# proxy settings
gconftool-2 --type string --set /system/http_proxy/host "$proxy"
gsettings set org.gnome.system.proxy.http host '"$proxy"'
gconftool-2 --type int --set /system/http_proxy/port "$port"
gsettings set org.gnome.system.proxy.http port "$port"
kwriteconfig --file kioslaverc --group 'Proxy Settings' --key httpProxy "http://${proxy} ${port}/"
gconftool-2 --type bool --set /system/http_proxy/use_same_proxy "$same"
gsettings set org.gnome.system.proxy use-same-proxy "$same"
# KDE handles 'same' in the GUI configuration, not the backend.
gconftool-2 --type string --set /system/proxy/secure_host "$https_proxy"
gsettings set org.gnome.system.proxy.https host '"$https_proxy"'
gconftool-2 --type int --set /system/proxy/secure_port "$https_port"
gsettings set org.gnome.system.proxy.https port "$https_port"
kwriteconfig --file kioslaverc --group 'Proxy Settings' --key httpsProxy "http://${https_proxy} ${https_port}/"
gconftool-2 --type string --set /system/proxy/ftp_host "$ftp_proxy"
gsettings set org.gnome.system.proxy.ftp host '"$ftp_proxy"'
gconftool-2 --type int --set /system/proxy/ftp_port "$ftp_port"
gsettings set org.gnome.system.proxy.ftp port "$ftp_port"
kwriteconfig --file kioslaverc --group 'Proxy Settings' --key ftpProxy "ftp://${ftp_proxy} ${ftp_port}/"
gconftool-2 --type string --set /system/proxy/socks_host "$socks_proxy"
gsettings set org.gnome.system.proxy.socks host '"$socks_proxy"'
gconftool-2 --type int --set /system/proxy/socks_port "$socks_port"
gsettings set org.gnome.system.proxy.socks port "$socks_port"
kwriteconfig --file kioslaverc --group 'Proxy Settings' --key socksProxy "http://${socks_proxy} ${socks_port}/"
# authentication
gconftool-2 --type bool --set /system/http_proxy/use_authentication "$auth"
gsettings set org.gnome.system.proxy.http use-authentication "$auth"
gconftool-2 --type string --set /system/http_proxy/authentication_user "$login"
gsettings set org.gnome.system.proxy.http authentication-user "$login"
gconftool-2 --type string --set /system/http_proxy/authentication_password "$pass"
gsettings set org.gnome.system.proxy.http authentication-password "$pass"
# KDE Prompts 'as needed'
kwriteconfig --file kioslaverc --group 'Proxy Settings' --key Authmode 0
# ignore-list
gconftool-2 --type list --list-type string --set /system/http_proxy/ignore_hosts "${gnome2_ignorelist}"
gsettings set org.gnome.system.proxy ignore-hosts "${gnome3_ignorelist}"
kwriteconfig --file kioslaverc --group 'Proxy Settings' --key NoProxyFor "${kde_ignorelist}"
# gconftool-2 --type string --set /system/proxy/autoconfig_url "${autoconfig_url}"
# gsettings set org.gnome.system.proxy autoconfig-url "${autoconfig_url}"
kwriteconfig --file kioslaverc --group 'Proxy Settings' --key 'Proxy Config Script' "${autoconfig_url}"
# When you modify kioslaverc, you need to tell KIO.
dbus-send --type=signal /KIO/Scheduler org.kde.KIO.Scheduler.reparseSlaveConfiguration string:''
EOS
done
else
logger -p user.notice -t $log_tag "skipped GNOME/KDE configuration"
fi
# setup shell variables
# this script should be called from /etc/bash.bashrc
logger -p user.notice -t $log_tag "building configuration script for shell"
[ -d `dirname "$proxy_env"` ] || mkdir --parents `dirname "$proxy_env"`
echo "# shell proxy configuration script for '$networkID'" > "$proxy_env"
# delete current values
echo 'unset http_proxy HTTP_PROXY https_proxy HTTPS_PROXY ftp_proxy FTP_PROXY all_proxy ALL_PROXY no_proxy NO_PROXY' >> "$proxy_env"
if [ "$enabled" == 'true' -a -z "$autoconfig_url" ]
then
echo "export http_proxy='http://${shell_auth}${proxy}:${port}/'" >> "$proxy_env"
echo "export HTTP_PROXY='http://${shell_auth}${proxy}:${port}/'" >> "$proxy_env"
echo "export https_proxy='http://${shell_auth}${https_proxy}:${https_port}/'" >> "$proxy_env"
echo "export HTTPS_PROXY='http://${shell_auth}${https_proxy}:${https_port}/'" >> "$proxy_env"
echo "export ftp_proxy='http://${shell_auth}${ftp_proxy}:${ftp_port}/'" >> "$proxy_env"
echo "export FTP_PROXY='http://${shell_auth}${ftp_proxy}:${ftp_port}/'" >> "$proxy_env"
echo "export all_proxy='socks://${shell_auth}${socks_proxy}:${socks_port}/'" >> "$proxy_env"
echo "export ALL_PROXY='socks://${shell_auth}${socks_proxy}:${socks_port}/'" >> "$proxy_env"
echo "export no_proxy='${ignorelist}'" >> "$proxy_env"
echo "export NO_PROXY='${ignorelist}'" >> "$proxy_env"
fi
logger -p user.notice -t $log_tag "configuration done."
fi