forked from rkoyama1623-2021/setup_my_environment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_config.sh
214 lines (188 loc) · 6.13 KB
/
proxy_config.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
#!/bin/sh
proxy-config () {
# For user
USER='USER-NAME'
PASS='PASSWORD'
PROXY_SERVER='example.com'
PORT='80'
DNS_SERVER_IP='0.0.0.0'
# For developper
SET_APT='false'
COMMAND='help'
function usage {
echo "proxy-config is a tool for setting up proxy server.
You can write ID/PASS in the script file, source the file, and run.
This function edit following files:
- ~/.wgetrc
- ~/.gitconfig
- ~/etc/apt/apt.conf (when --apt option was specified)
Usage:
proxy-config [<options>] command
command: set or unset
Options:
-h, --help print this
--id <ID> specify ID
--pass <PASS> specify Password
--dns <IP> specify DNS Server IP
--apt also set proxy for apt (require sudo)
Example:
$ source /path/to/setup_rd_environment/proxy_config.sh
$ proxy-config set
$ proxy-config set --user hoge --pass fuga --apt
$ proxy-config unset
"
}
while [ $# -gt 0 ];
do
case ${1} in
help|--help|-h)
COMMAND='help'
shift
;;
set)
COMMAND='set'
shift
;;
unset)
COMMAND='unset'
shift
;;
--id)
USER=${2}
shift 2
;;
--pass)
PASS=${2}
shift 2
;;
--dns)
DNS_SERVER_IP=${2}
shift
;;
--apt)
SET_APT=true
shift
;;
*)
usage
return 1
esac
done
function set_variables {
if [ ! -z "$PROXY_SERVER" ]; then
export proxy_server=${PROXY_SERVER}
export http_proxy="http://${USER}:${PASS}@${PROXY_SERVER}:${PORT}"
export https_proxy="http://${USER}:${PASS}@${PROXY_SERVER}:${PORT}"
export ftp_proxy="http://${USER}:${PASS}@${PROXY_SERVER}:${PORT}"
else
export proxy_server=''
export http_proxy=''
export https_proxy=''
export ftp_proxy=''
fi
}
function prepare_backup_dir {
BACKUP_DIR=${HOME}/.proxy/backup
if [ ! -e ${BACKUP_DIR} ]; then
mkdir -p ${BACKUP_DIR}
fi
}
function set_git {
cp ~/.gitconfig ${BACKUP_DIR}/dot.gitconfig
cp ~/.gitconfig ${BACKUP_DIR}/dot.gitconfig
git config --global http.proxy "${http_proxy}"
git config --global https.proxy "${https_proxy}"
git config --global ftp.proxy "${ftp_proxy}"
git config --global http.sslVerify false
}
function set_wget {
if [ ! -f ~/.wgetrc ]; then
touch ~/.wgetrc
fi
# Backup
cp ${HOME}/.wgetrc ${BACKUP_DIR}/dot.wgetrc
# Set use_proxy
if [ ! -z ${http_proxy} ]; then
if grep -q use_proxy ~/.wgetrc; then
sed -i ~/.wgetrc -e 's/use_proxy.*=.*off/use_proxy = on/g'
else
echo 'use_proxy = on' >> ~/.wgetrc
fi
else
if grep -q use_proxy ~/.wgetrc; then
sed -i -e 's/use_proxy.*=.*/use_proxy = off/g' ~/.wgetrc
fi
fi
regex_proxy_server=${proxy_server//\//\\/}
regex_proxy_server=${regex_proxy_server//\./\\.}
sed -i -e "s/proxy_user.*=.*/proxy_user = ${USER}/g" ~/.wgetrc
sed -i -e "s/proxy_passwd.*=.*/proxy_passwd = ${PASS}/g" ~/.wgetrc
if [ ! -z ${proxy_server} ]; then
sed -i -e "s/http_proxy.*=.*/http_proxy = http:\/\/${regex_proxy_server}:${PORT}/g" ~/.wgetrc
sed -i -e "s/https_proxy.*=.*/https_proxy = http:\/\/${regex_proxy_server}:${PORT}/g" ~/.wgetrc
sed -i -e "s/ftp_proxy.*=.*/ftp_proxy = http:\/\/${regex_proxy_server}:${PORT}/g" ~/.wgetrc
else
sed -i -e "s/http_proxy.*=.*/http_proxy = /g" ~/.wgetrc
sed -i -e "s/https_proxy.*=.*/https_proxy = /g" ~/.wgetrc
sed -i -e "s/ftp_proxy.*=.*/ftp_proxy = /g" ~/.wgetrc
fi
}
function set_apt {
if ! grep -q proxy /etc/apt/apt.conf; then
sudo sh -c 'echo Acquire::http::proxy >> /etc/apt/apt.conf'
sudo sh -c 'echo Acquire::https::proxy >> /etc/apt/apt.conf'
sudo sh -c 'echo Acquire::ftp::proxy >> /etc/apt/apt.conf'
fi
if [ ! -z ${proxy_server} ]; then
regex_proxy_server=${proxy_server//\//\\/}
regex_proxy_server=${regex_proxy_server//\./\\.}
sudo sed -i -e "s/Acquire::http::proxy.*/Acquire::http::proxy \"http:\/\/${USER}:${PASS}@${regex_proxy_server}:${PORT}\/\";/" /etc/apt/apt.conf
sudo sed -i -e "s/Acquire::https::proxy.*/Acquire::https::proxy \"http:\/\/${USER}:${PASS}@${regex_proxy_server}:${PORT}\/\";/" /etc/apt/apt.conf
sudo sed -i -e "s/Acquire::ftp::proxy.*/Acquire::ftp::proxy \"http:\/\/${USER}:${PASS}@${regex_proxy_server}:${PORT}\/\";/" /etc/apt/apt.conf
else
sudo sed -i -e "/^Acquire::http::proxy.*/d" /etc/apt/apt.conf
sudo sed -i -e "/^Acquire::https::proxy.*/d" /etc/apt/apt.conf
sudo sed -i -e "/^Acquire::ftp::proxy.*/d" /etc/apt/apt.conf
fi
}
case $COMMAND in
"help")
usage
;;
"set")
# Set sudo alias
alias sudo='sudo -E'
# Set Environment Variables
set_variables
# Change settings
prepare_backup_dir
set_git
set_wget
if [ ${SET_APT} = true ]; then set_apt; fi;
;;
"unset")
# Set sudo alias
alias sudo='sudo'
# Unset Environment Variables
USER=''
PASS=''
PROXY_SERVER=''
PORT=''
DNS_SERVER_IP=''
set_variables
# Change settings
prepare_backup_dir
set_git
set_wget
if [ ${SET_APT} = true ]; then set_apt; fi;
;;
*)
return 1
esac
unset usage
unset set_variables
unset prepare_backup_dir
unset set_git
unset set_wget
unset set_apt
}