-
Notifications
You must be signed in to change notification settings - Fork 26
/
set_proxy.sh
executable file
·196 lines (181 loc) · 5.18 KB
/
set_proxy.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
#! /bin/sh
# A script to set system-wide proxy in Ubuntu / Debian
# created by thealphadollar
# Contributions from TheMousePotato and Ayushk4
if [ `id -u` -ne 0 ]
then echo "Error: needs to be run as sudo!!"
exit 1
fi
exit_with_usage ()
{
echo "setproxy: invalid option"
echo "Usage: ./set_proxy.sh -h [PROXY HOST] -p [PROXY PORT]"
echo "Try './set_proxy.sh --help' for more information."
# Help to be added later when script functions increases
exit 1
}
reset_proxy ()
{
echo "resetting proxy.."
gsettings set org.gnome.system.proxy mode none
truncate -s 0 /etc/profile.d/proxy.sh
sed -i.bak "/Acquire::/d" /etc/apt/apt.conf
sed -i.bak "/Acquire::/,+10d" /etc/apt/apt.conf.d/70debconf
sed -i "/proxy/d" /etc/environment
sed -i "/PROXY/d" /etc/environment
if hash git 2>/dev/null; then
git config --global --unset http.proxy
git config --global --unset https.proxy
fi
if [ -a /etc/systemd/system/docker.service.d/proxy.conf ]; then
sudo rm -rf /etc/systemd/system/docker.service.d/proxy.conf
fi
sudo systemctl daemon-reload
sudo systemctl restart docker.service
echo "done"
}
# setting system wide proxy
set_systemwide_proxy ()
{
gsettings set org.gnome.system.proxy mode 'manual'
gsettings set org.gnome.system.proxy.http host "$PROXY_HOST"
gsettings set org.gnome.system.proxy.http port "$PROXY_PORT"
gsettings set org.gnome.system.proxy.https host "$PROXY_HOST"
gsettings set org.gnome.system.proxy.https port "$PROXY_PORT"
gsettings set org.gnome.system.proxy.ftp host "$PROXY_HOST"
gsettings set org.gnome.system.proxy.ftp port "$PROXY_PORT"
gsettings set org.gnome.system.proxy ignore-hosts "['localhost', '127.0.0.0/8', '127.0.0.1/8', '::1', '10.*.*.*']"
echo "systemwide proxy set"
}
# setting APT-conf proxy
## in /etc/apt/conf **deprecated**
set_apt_proxy_old ()
{
sed -i.bak '/http[s]::proxy/Id' /etc/apt/apt.conf
tee -a /etc/apt/apt.conf <<EOF
Acquire::http::proxy "http://${PROXY_HOST}:${PROXY_PORT}";
Acquire::https::proxy "http://${PROXY_HOST}:${PROXY_PORT}";
Acquire::ftp::proxy "http://${PROXY_HOST}:${PROXY_PORT}";
EOF
echo "APT proxy set"
}
## in /etc/apt/apt.conf.d/70debconf
set_apt_proxy ()
{
sed -i.bak '/http[s]::proxy/Id' /etc/apt/apt.conf.d/70debconf
tee -a /etc/apt/apt.conf.d/70debconf <<EOF
Acquire::http::proxy "http://${PROXY_HOST}:${PROXY_PORT}";
Acquire::https::proxy "http://${PROXY_HOST}:${PROXY_PORT}";
Acquire::ftp
{
Proxy "ftp://${PROXY_HOST}:${PROXY_PORT}";
ProxyLogin
{
"USER $(SITE_USER)@$(SITE)";
"PASS $(SITE_PASS)";
}
}
EOF
echo "APT proxy set"
}
# setting environment proxy
set_environment_proxy ()
{
sed -i.bak '/http[s]_proxy/Id' /etc/environment
tee -a /etc/environment <<EOF
http_proxy="http://${PROXY_HOST}:${PROXY_PORT}"
https_proxy="http://${PROXY_HOST}:${PROXY_PORT}"
ftp_proxy="http://${PROXY_HOST}:${PROXY_PORT}"
HTTP_PROXY="http://${PROXY_HOST}:${PROXY_PORT}"
HTTPS_PROXY="http://${PROXY_HOST}:${PROXY_PORT}"
FTP_PROXY="http://${PROXY_HOST}:${PROXY_PORT}"
no_proxy=localhost,127.0.0.0/8,::1,10.0.0.0/8,127.0.0.1/8
EOF
echo "Environment proxy set"
}
# setting bash profile proxy
set_profile_proxy ()
{
touch /etc/profile.d/proxy.sh
tee -a /etc/profile.d/proxy.sh <<EOF
export http_proxy="http://${PROXY_HOST}:${PROXY_PORT}"
export https_proxy="http://${PROXY_HOST}:${PROXY_PORT}"
export HTTP_PROXY="http://${PROXY_HOST}:${PROXY_PORT}"
export HTTPS_PROXY="http://${PROXY_HOST}:${PROXY_PORT}"
EOF
echo "Profile proxy set"
}
# application specific proxy
set_docker_proxy()
{
if [ -a /etc/systemd/system/docker.service.d/proxy.conf ]; then
sudo rm -rf /etc/systemd/system/docker.service.d/proxy.conf
fi
if [ ! -d /etc/systemd/system/docker.service.d ]; then
sudo mkdir -p /etc/systemd/system/docker.service.d
fi
sudo touch /etc/systemd/system/docker.service.d/proxy.conf
a='"'
echo "[Service]" >> /etc/systemd/system/docker.service.d/proxy.conf
echo "Environment=${a}HTTP_PROXY=http://${PROXY_HOST}:${PROXY_PORT}/${a}" >> /etc/systemd/system/docker.service.d/proxy.conf
echo "Environment=${a}HTTPS_PROXY=http://${PROXY_HOST}:${PROXY_PORT}/${a}" >> /etc/systemd/system/docker.service.d/proxy.conf
echo 'Environment="NO_PROXY=localhost,127.0.0.1,::1' >> /etc/systemd/system/docker.service.d/proxy.conf
sudo systemctl daemon-reload
sudo systemctl restart docker.service
echo "Docker proxy set"
}
# setting Git proxy
set_git_proxy ()
{
if hash git 2>/dev/null; then
git config --global http.proxy "http://${PROXY_HOST}:${PROXY_PORT}"
git config --global https.proxy "http://${PROXY_HOST}:${PROXY_PORT}"
fi
echo "Git proxy set"
}
if [ "$#" -eq 1 ]; then
case $1 in
-u| --unset)
reset_proxy
exit 0
;;
*)
exit_with_usage
;;
esac
fi
if [ "$#" -eq 4 ]; then
while [ "$1" != "" ]; do
case $1 in
-h| --host)
shift
PROXY_HOST=$1
echo $PROXY_HOST
shift
case $1 in
-p| --port)
shift
PROXY_PORT=$1
echo $PROXY_PORT
shift
;;
*)
exit_with_usage
;;
esac
;;
*)
exit_with_usage
;;
esac
done
else
exit_with_usage
fi
set_systemwide_proxy
set_environment_proxy
set_apt_proxy_old
set_apt_proxy
set_profile_proxy
set_git_proxy
set_docker_proxy