-
Notifications
You must be signed in to change notification settings - Fork 11
/
zetproxy
executable file
·236 lines (223 loc) · 7.17 KB
/
zetproxy
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
#!/bin/bash
# variables for protocols
hp=http_proxy
hsp=https_proxy
fp=ftp_proxy
np=no_proxy
sp=socks_proxy
HP=HTTP_PROXY
HSP=HTTPS_PROXY
FP=FTP_PROXY
NP=NO_PROXY
SP=SOCKS_PROXY
# function to set proxy in enviroment file
proxy_shell()
{
echo '## Setting environment variables in' ${1}
# if None proxy then this next line is enough
# pipe is in extended regular expr, reqs -r to enable it in GNU sed
# Doubt: Are capital proxies needed/recommended. Don't know yet ??
# Doubt: Socks proxy are not set rn, just removed if it exists
sed -r "/($hp|$HP|$hsp|$HSP|$fp|$FP|$np|$NP|$sp|$SP).*/d" $HOME/.${1}rc> ./.temp_file_env;
unset $hp
unset $hsp
unset $fp
unset $np
unset $sp
unset $HP
unset $HSP
unset $FP
unset $NP
unset $SP
if [ $WIFI_PROXY = "None" ]
then
hp_pr=
hsp_pr=
fp_pr=
np_pr=
elif [ ! $WIFI_PROXY = "None" ]
then
hp_pr=http://$WIFI_PROXY:$PORT/
hsp_pr=https://$WIFI_PROXY:$PORT/
fp_pr=ftp://$WIFI_PROXY:$PORT/
np_pr="localhost,127.0.0.1"
fi
printf "export $hp=\"$hp_pr\"\nexport $hsp=\"$hsp_pr\"\nexport $fp=\"$fp_pr\"\nexport $np=\"$np_pr\"\n" >> ./.temp_file_env;
printf "export $HP=\"$hp_pr\"\nexport $HSP=\"$hsp_pr\"\nexport $FP=\"$fp_pr\"\nexport $NP=\"$np_pr\"\n" >> ./.temp_file_env;
mv ./.temp_file_env $HOME/.${1}rc
echo ' -> Proxy set in' ${1}
}
# setting proxy for apt in file apt.conf
proxy_apt()
{
echo '## Setting proxy for apt'
if [ $WIFI_PROXY = "None" ]
then
echo ' -> Clearing apt proxy everywhere ...'
printf "Acquire::http::Proxy \"false\";" > ./.temp_file_apt;
else
printf "Acquire::http::Proxy \"$hp_pr\";\nAcquire::https::Proxy \"$hsp_pr\";\nAcquire::ftp::Proxy \"$fp_pr\";\n" > ./.temp_file_apt;
#echo Created apt.conf file
echo ' -> Proxy set in apt'
fi
sudo mv ./.temp_file_apt /etc/apt/apt.conf
}
# proxy for gsetting
# need thorough testing
proxy_gnome()
{
echo '## Handling gnome proxy'
gsettings set org.gnome.system.proxy.http host ''
gsettings set org.gnome.system.proxy.http port 0
gsettings set org.gnome.system.proxy.https host ''
gsettings set org.gnome.system.proxy.https port 0
gsettings set org.gnome.system.proxy.ftp host ''
gsettings set org.gnome.system.proxy.ftp port 0
gsettings set org.gnome.system.proxy.socks host ''
gsettings set org.gnome.system.proxy.socks port 0
gsettings set org.gnome.system.proxy mode 'manual'
if [[ $1 = "Socks" ]]
then
gsettings set org.gnome.system.proxy.socks host $WIFI_PROXY
gsettings set org.gnome.system.proxy.socks port $PORT
elif [[ $WIFI_PROXY != "None" ]]
then
gsettings set org.gnome.system.proxy.http host $WIFI_PROXY
gsettings set org.gnome.system.proxy.http port $PORT
gsettings set org.gnome.system.proxy.https host $WIFI_PROXY
gsettings set org.gnome.system.proxy.https port $PORT
gsettings set org.gnome.system.proxy.ftp host $WIFI_PROXY
gsettings set org.gnome.system.proxy.ftp port $PORT
gsettings set org.gnome.system.proxy.socks host $WIFI_PROXY
gsettings set org.gnome.system.proxy.socks port $PORT
gsettings set org.gnome.system.proxy mode 'manual'
fi
echo ' -> Setting proxy in gnome'
}
# proxy for git
proxy_git()
{ echo '## Setting git proxy'
if [[ $WIFI_PROXY = "None" ]]
then
git config --global http.proxy ''
git config --global https.proxy ''
else
git config --global http.proxy $WIFI_PROXY:$PORT
git config --global https.proxy $WIFI_PROXY:$PORT
fi
echo ' -> Proxy set in git'
}
#proxy for npm
proxy_npm()
{ echo '## Setting npm proxy'
if [[ $WIFI_PROXY = "None" ]]
then
npm config delete https-proxy
npm config delete proxy
else
npm config set https-proxy "http://$WIFI_PROXY:$PORT/"
npm config set proxy "http://$WIFI_PROXY:$PORT/"
fi
echo ' -> Proxy set in npm'
}
# proxy for kde 5.5
proxy_kde()
{
# SOCKS not working yet
echo '## Setting kde proxy'
kwriteconfig5 --file ~/.config/kioslaverc --group Proxy\ Settings --key ProxyType 1
kwriteconfig5 --file ~/.config/kioslaverc --group Proxy\ Settings --key ftpProxy ''
kwriteconfig5 --file ~/.config/kioslaverc --group Proxy\ Settings --key httpsProxy ''
kwriteconfig5 --file ~/.config/kioslaverc --group Proxy\ Settings --key httpProxy ''
kwriteconfig5 --file ~/.config/kioslaverc --group Proxy\ Settings --key fttpProxy ''
kwriteconfig5 --file ~/.config/kioslaverc --group Proxy\ Settings --key socksProxy ''
kwriteconfig5 --file ~/.config/kioslaverc --group Proxy\ Settings --key ReversedException false
if [[ $1 = "Socks" ]]
then
kwriteconfig5 --file ~/.config/kioslaverc --group Proxy\ Settings --key socksProxy socks://$WIFI_PROXY:$PORT
elif [[ $WIFI_PROXY != "None" ]]
then
kwriteconfig5 --file ~/.config/kioslaverc --group Proxy\ Settings --key ftpProxy $fp_pr
kwriteconfig5 --file ~/.config/kioslaverc --group Proxy\ Settings --key httpsProxy $hsp_pr
kwriteconfig5 --file ~/.config/kioslaverc --group Proxy\ Settings --key httpProxy $hp_pr
kwriteconfig5 --file ~/.config/kioslaverc --group Proxy\ Settings --key fttpProxy $fp_pr
fi
echo ' -> Proxy set in kde proxy module'
}
if [[ $1 = "None" ]]
then
echo '-------------------------------'
echo 'Clearing proxy'
echo '-------------------------------'
WIFI_PROXY=None
elif [[ $1 = "Socks" ]]
then
echo '-------------------------------'
echo 'Setting tor socks proxy'
echo '-------------------------------'
PORT=$2
WIFI_PROXY='127.0.0.1'
elif [[ $1 = "Proxy" ]]
then
TEMP=$2
WIFI_PROXY=$(echo $TEMP | sed 's/\(.*\):.*/\1/')
PORT=$(echo $TEMP | sed 's/.*:\(.*\)/\1/')
else
echo '----------------------------------'
echo 'Setting proxy automatically ...'
echo '----------------------------------'
echo ''
echo '## Evaluating fastest proxy'
PROXY_OP=$(python3 $HOME/.proxyhelper/surely_parallel.py | tail -1)
echo ' ' $PROXY_OP
phrase='-> Best proxy'
key=$(echo $PROXY_OP | sed 's/-> Best proxy : .*/phrase/')
#echo THE KEY IS $key
if [[ $key = phrase ]]
then
WIFI_PROXY=$(echo $PROXY_OP | sed 's/-> Best proxy : \(.*\)/\1/')
PORT=3128
else
WIFI_PROXY=None
fi
fi
# Function call to set environment variables
proxy_shell bash
if [ -x "$(command -v zsh)" ]
then
proxy_shell zsh
#zsh ~/.zshrc
fi
if [ -x "$(command -v git)" ]
then
proxy_git
fi
if [ -x "$(command -v npm)" ]
then
proxy_npm
fi
if [ -x "$(command -v apt)" ]
then
proxy_apt
fi
# Function call to set gnome proxy
# Required for firefox
if [ -x "$(command -v gsettings)" ]
then
proxy_gnome
fi
# Function call to set kde system proxy
if [ -x "$(command -v kwriteconfig5)" ]
then
proxy_kde
fi
# We don't want to update when we are uninstalling or just resetting proxy
if [[ $1 != "Proxy" && $1 != "None" && $1 != "Socks" ]]
then
home=$HOME
cd $home/.proxyhelper && git pull origin master && sh ./install.sh
echo '## Updating ProxyHelper'
fi
## Better method would be to source the config script
## But that doesn't work for unknown reason
# exec $SHELL