forked from gsake/proxy-set-unset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.sh
executable file
·107 lines (81 loc) · 2.09 KB
/
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
#!/bin/bash
#
# Arguments: set or unset
#
_self="${0##*/}"
## or in usage() ##
usage(){
echo "/.$_self [set|unset]"
}
number_of_arg=$#
if [ $number_of_arg -lt 1 ]
then
usage
exit 1
fi
PROXY=http://172.29.38.124:8080
DOCKER_CONFIG_FILE=~/.docker/config.json
if [ $1 == "set" ]
then
# Set environment proxy
echo "Set proxy to the environment"
export http_proxy="${PROXY}"
export https_proxy="${PROXY}"
export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
# Set git proxy
if command -v git &> /dev/null
then
echo "Set proxy to git"
git config --global http.proxy ${PROXY}
git config --global https.proxy ${PROXY}
fi
# Set npm proxy
if command -v npm &> /dev/null
then
echo "Set proxy to npm"
npm config set proxy ${PROXY}
npm config set https-proxy ${PROXY}
fi
# Set proxy to docker
if command -v docker &> /dev/null
then
echo "Set proxy to docker. "
if [ -f "${DOCKER_CONFIG_FILE}" ]; then
q=`jq . ${DOCKER_CONFIG_FILE}`
echo $q | jq ". += {\"proxies\":{\"default\":{\"httpProxy\":\"${PROXY}\",\"httpsProxy\":\"${PROXY}\",\"noProxy\":\"localhost,localaddress,.localdomain.com,127.0.0.0/8\"}}}" > ${DOCKER_CONFIG_FILE}
else
touch ${DOCKER_CONFIG_FILE}
jq -n "{\"proxies\":{\"default\":{\"httpProxy\":\"${PROXY}\",\"httpsProxy\":\"${PROXY}\",\"noProxy\":\"localhost,localaddress,.localdomain.com,127.0.0.0/8\"}}}" > ${DOCKER_CONFIG_FILE}
fi
fi
elif [ $1 == "unset" ]
then
# Unset environment proxy
echo "Unset proxy from environment"
unset http_proxy
unset https_proxy
unset no_proxy
# Unset git proxy
if command -v git &> /dev/null
then
echo "Unset proxy from git"
git config --global --unset https.proxy
git config --global --unset http.proxy
fi
# Unset npm proxy
if command -v npm &> /dev/null
then
echo "Unset proxy from npm"
npm config delete proxy
npm config delete https-proxy
fi
# Unset docker proxy
if command -v docker &> /dev/null
then
# Unset proxy from docker
echo "Unset proxy from docker"
echo $(cat ${DOCKER_CONFIG_FILE} | jq 'del(.proxies)') > ${DOCKER_CONFIG_FILE}
fi
else
usage
fi