-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxy.sh
More file actions
executable file
·121 lines (108 loc) · 2.61 KB
/
proxy.sh
File metadata and controls
executable file
·121 lines (108 loc) · 2.61 KB
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
#/bin/bash
OPTION=$1
USER=username
PASS=password
HTTP_PROXY_HOST=proxy.com.br
HTTP_PROXY_PORT=3128
HTTPS_PROXY_HOST=proxy.com.br
HTTPS_PROXY_PORT=3128
APT_FILE=/etc/apt/apt.conf
ENVIRONMENT_FILE=/etc/environment
###### Backup file
function backup_file
{
if [ -f "$1" ];
then
sudo sed -i.bak '/http[s]::proxy/Id' "$1"
fi
}
function proxy_gnome
{
gsettings set org.gnome.system.proxy mode manual
gsettings set org.gnome.system.proxy.http host "$HTTP_PROXY_HOST"
gsettings set org.gnome.system.proxy.http port "$HTTP_PROXY_PORT"
gsettings set org.gnome.system.proxy.http authentication-user "$USER"
gsettings set org.gnome.system.proxy.http authentication-password "$PASS"
gsettings set org.gnome.system.proxy.https host "$HTTPS_PROXY_HOST"
gsettings set org.gnome.system.proxy.https port "$HTTPS_PROXY_PORT"
gsettings set org.gnome.system.proxy ignore-hosts "['localhost', '127.0.0.0/8', '*.sjk.emb', '::1', '10.5.2.*']"
}
function proxy_apt_file
{
backup_file "$APT_FILE"
sudo tee -a "$APT_FILE" \
<<EOF
Acquire::http::proxy "http://$USER:$PASS@$HTTP_PROXY_HOST:$HTTP_PROXY_PORT/";
Acquire::https::proxy "http://$USER:$PASS@$HTTPS_PROXY_HOST:$HTTPS_PROXY_PORT/";
EOF
}
function proxy_environment_file
{
backup_file "$ENVIRONMENT_FILE"
sudo tee -a "$ENVIRONMENT_FILE" \
<<EOF
http_proxy="http://$USER:$PASS@$HTTP_PROXY_HOST:$HTTP_PROXY_PORT/"
https_proxy="http://$USER:$PASS@$HTTPS_PROXY_HOST:$HTTPS_PROXY_PORT/"
EOF
}
function proxy_git
{
git config --global http.proxy http://$USER:$PASS@$HTTP_PROXY_HOST:$HTTP_PROXY_PORT/
git config --global https.proxy http://$USER:$PASS@$HTTPS_PROXY_HOST:$HTTPS_PROXY_PORT/
}
###### Proxy start
function proxy_start
{
proxy_gnome
proxy_apt_file
proxy_environment_file
proxy_git
}
###### Proxy stop
function proxy_stop
{
gsettings set org.gnome.system.proxy mode none
sudo sed -i -e '/Acquire::http::proxy/d' "$APT_FILE"
sudo sed -i -e '/Acquire::https::proxy/d' "$APT_FILE"
sudo sed -i -e '/http_proxy/d' "$ENVIRONMENT_FILE"
sudo sed -i -e '/https_proxy/d' "$ENVIRONMENT_FILE"
unset http_proxy
unset https_proxy
npm config delete http-proxy
npm config delete https-proxy
export http_proxy=
export https_proxy=
http_proxy=
https_proxy=
git config --global --unset http.proxy
git config --global --unset https.proxy
}
###### Proxy switch case
function proxy
{
case $OPTION in
"start")
echo "Setting proxy..."
proxy_start
echo "Done."
;;
"stop")
echo "Unsetting proxy..."
proxy_stop
echo "Done."
;;
*)
echo "Invalid option!"
echo "Error."
;;
esac
}
# Main software
if [ -n "$OPTION" ];
then
proxy
else
echo 'Option is null'
echo 'Error.'
fi
exit