-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.sh
140 lines (121 loc) · 3.54 KB
/
func.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
proxyUnset() {
unset http_proxy
unset HTTP_PROXY
unset https_proxy
unset HTTPS_PROXY
unset ftp_proxy
unset FTP_PROXY
unset all_proxy
unset ALL_PROXY
unset PIP_PROXY
unset no_proxy
unset NO_PROXY
unset MAVEN_OPTS
}
composeProxyAddr() {
if (( $# != 3 )) ; then
return 1;
fi
local proxyProtocol="${1}"
local proxyHost="${2}"
local proxyPort="${3}"
echo "${proxyProtocol}://${proxyHost}:${proxyPort}"
}
proxySet() {
if (( $# < 3 )) ; then
echo "Syntax: proxySet proxyProtocol proxyHost proxyPort [noProxy]"
return 1
fi
local proxyProtocol="${1}"
local proxyHost="${2}"
local proxyPort="${3}"
local noProxy="${4}"
local proxyAddr="$(composeProxyAddr ${proxyProtocol} ${proxyHost} ${proxyPort})"
export http_proxy="${proxyAddr}"
export HTTP_PROXY="${proxyAddr}"
export https_proxy="${proxyAddr}"
export HTTPS_PROXY="${proxyAddr}"
export ftp_proxy="${proxyAddr}"
export FTP_PROXY="${proxyAddr}"
export all_proxy="${proxyAddr}"
export ALL_PROXY="${proxyAddr}"
export PIP_PROXY="${proxyAddr}"
export no_proxy="${noProxy}"
export NO_PROXY="${noProxy}"
export MAVEN_OPTS="-Dhttp.proxyHost=${proxyHost} -Dhttp.proxyPort=${proxyPort} -Dhttps.proxyHost=${proxyHost} -Dhttps.proxyPort=${proxyPort}"
}
RESOLF='/etc/resolv.conf'
changeDNS() {
if (( $# < 1 )) ; then
return 1;
fi
local nameservers=("${(@s/,/)1}")
sudo truncate -s 0 "${RESOLF}"
for nameServerIP in ${nameservers[@]}; do
echo "nameserver ${nameServerIP}" | sudo tee -a "${RESOLF}" > /dev/null
done
}
changeWSLDNS() {
sudo chattr -i "${RESOLF}"
changeDNS "${1}"
sudo chattr +i "${RESOLF}"
}
proxyProbe() {
local matchDNS="dns"
local withDNS="${1}"
if nc -z -w 3 ${PROXY_HOST} ${PROXY_PORT} &> /dev/null; then
# echo "proxyProbe: Detected VPN, turning on proxy."
proxySet "${PROXY_PROTOCOL}" "${PROXY_HOST}" "${PROXY_PORT}" "${NOPROXY}"
if [[ "${(L)withDNS}" = "${matchDNS}" ]]; then
changeWSLDNS "${PROXY_DNS},${NO_PROXY_DNS}"
fi
else
# echo "proxyProbe: Detected normal network, turning off proxy."
proxyUnset
if [[ "${(L)withDNS}" = "${matchDNS}" ]]; then
changeWSLDNS "${NO_PROXY_DNS},${PROXY_DNS}"
fi
fi
}
awsProxy() {
local proxyArgs=("${AWS_PROXY_PROTOCOL}" "${AWS_PROXY_HOST}" "${AWS_PROXY_PORT}")
local proxyAddr="$(composeProxyAddr ${proxyArgs[@]})"
if [[ "${http_proxy}" != "${proxyAddr}" ]]; then
proxySet ${proxyArgs[@]}
else
proxyUnset
fi
}
changeCluster() {
local clusterName="${1:-$AWS_CLUSTER_NAME}"
export AWS_CLUSTER_NAME="${clusterName}"
aws eks update-kubeconfig --name "${AWS_CLUSTER_NAME}" --region "${AWS_REGION}"
}
wslSetDisplay() {
local ipconfig="/mnt/c/Windows/System32/ipconfig.exe"
local grepip=("grep" "-oP" '(?<=IPv4 Address(?:\.\s){11}:\s)((?:\d+\.){3}\d+)')
if [[ ! -d "/mnt/c/Windows" ]]; then
return
fi
local display=$("${ipconfig}" | grep -A 3 "${ENTERPRISE_DOMAIN}" | "${grepip[@]}")
if [[ -n "${display}" ]]; then
export DISPLAY="${display}:0.0"
return
fi
export DISPLAY=$("${ipconfig}" | grep -A 5 "vEthernet (WSL)" | "${grepip[@]}"):0.0
}
#SSH Reagent (http://tychoish.com/post/9-awesome-ssh-tricks/)
sshReagent () {
for agent in /tmp/ssh-*/agent.*; do
export SSH_AUTH_SOCK=$agent
if ssh-add -l 2>&1 > /dev/null; then
echo Found working SSH Agent:
ssh-add -l
return
fi
done
echo Cannot find ssh agent - maybe you should reconnect and forward it?
}
sshAgent() {
pgrep -x ssh-agent &> /dev/null && sshReagent &> /dev/null || eval $(ssh-agent) &> /dev/null
}