forked from SukkaW/zsh-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzsh-proxy.plugin.zsh
executable file
·340 lines (299 loc) · 9.24 KB
/
zsh-proxy.plugin.zsh
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/bin/bash
# ______ _____ _ _ _____
# |___ // ____| | | | | __ \
# / /| (___ | |__| | | |__| ) __ _____ ___ _
# / / \___ \| __ | | ___/ '__/ _ \ \/ | | | |
# / /__ ____) | | | | | | | | | (_) > <| |_| |
# /_____|_____/|_| |_| |_| |_| \___/_/\_\\__, |
# __/ |
# |___/
# -------------------------------------------------
# A proxy plugin for zsh
# Sukka (https://skk.moe)
__read_proxy_config() {
__ZSHPROXY_STATUS=$(cat "${ZDOTDIR:-${HOME}}/.zsh-proxy/status")
__ZSHPROXY_SOCKS5=$(cat "${ZDOTDIR:-${HOME}}/.zsh-proxy/socks5")
__ZSHPROXY_HTTP=$(cat "${ZDOTDIR:-${HOME}}/.zsh-proxy/http")
__ZSHPROXY_NO_PROXY=$(cat "${ZDOTDIR:-${HOME}}/.zsh-proxy/no_proxy")
__ZSHPROXY_GIT_PROXY_TYPE=$(cat "${ZDOTDIR:-${HOME}}/.zsh-proxy/git_proxy_type")
}
__check_whether_init() {
if [ ! -f "${ZDOTDIR:-${HOME}}/.zsh-proxy/status" ] || [ ! -f "${ZDOTDIR:-${HOME}}/.zsh-proxy/http" ] || [ ! -f "${ZDOTDIR:-${HOME}}/.zsh-proxy/socks5" ] || [ ! -f "${ZDOTDIR:-${HOME}}/.zsh-proxy/no_proxy" ]; then
echo "----------------------------------------"
echo "You should run following command first:"
echo "$ init_proxy"
echo "----------------------------------------"
else
__read_proxy_config
fi
}
__check_ip() {
echo "========================================"
echo "Check what your IP is"
echo "----------------------------------------"
ipv4=$(curl -s -k -m3 https://api-ipv4.ip.sb/ip -H 'user-agent: zsh-proxy')
if [[ "$ipv4" != "" ]]; then
echo "IPv4: $ipv4"
else
echo "IPv4: -"
fi
if command -v python >/dev/null; then
geoip=$(curl -s -k -m3 https://api.ip.sb/geoip -H 'user-agent: zsh-proxy')
if [[ "$geoip" != "" ]]; then
echo "----------------------------------------"
echo "Info: "
echo "$geoip" | python -m json.tool
fi
fi
echo "========================================"
}
__config_proxy() {
echo "========================================"
echo "ZSH Proxy Plugin Config"
echo "----------------------------------------"
echo -n "[socks5 proxy] {Default as 127.0.0.1:1080}
(address:port): "
read -r __read_socks5
echo -n "[socks5 type] Select the proxy type you want to use {Default as socks5}:
1. socks5
2. socks5h (resolve DNS through the proxy server)
(1 or 2): "
read -r __read_socks5_type
echo -n "[http proxy] {Default as 127.0.0.1:8080}
(address:port): "
read -r __read_http
echo -n "[no proxy domain] {Default as 'localhost,127.0.0.1,localaddress,.localdomain.com'}
(comma separate domains): "
read -r __read_no_proxy
echo -n "[git proxy type] {Default as socks5}
(socks5 or http): "
read -r __read_git_proxy_type
echo "========================================"
if [ -z "${__read_socks5}" ]; then
__read_socks5="127.0.0.1:1080"
fi
if [ -z "${__read_socks5_type}" ]; then
__read_socks5_type="1"
fi
if [ -z "${__read_http}" ]; then
__read_http="127.0.0.1:8080"
fi
if [ -z "${__read_no_proxy}" ]; then
__read_no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
fi
if [ -z "${__read_git_proxy_type}" ]; then
__read_git_proxy_type="socks5"
fi
echo "http://${__read_http}" >"${ZDOTDIR:-${HOME}}/.zsh-proxy/http"
if [ "${__read_socks5_type}" = "2" ]; then
echo "socks5h://${__read_socks5}" >"${ZDOTDIR:-${HOME}}/.zsh-proxy/socks5"
else
echo "socks5://${__read_socks5}" >"${ZDOTDIR:-${HOME}}/.zsh-proxy/socks5"
fi
echo "${__read_no_proxy}" >"${ZDOTDIR:-${HOME}}/.zsh-proxy/no_proxy"
echo "${__read_git_proxy_type}" >"${ZDOTDIR:-${HOME}}/.zsh-proxy/git_proxy_type"
__read_proxy_config
}
# ==================================================
# Proxy for APT
__enable_proxy_apt() {
if [ -d "/etc/apt/apt.conf.d" ]; then
sudo touch /etc/apt/apt.conf.d/proxy.conf
echo -e "Acquire::http::Proxy \"${__ZSHPROXY_HTTP}\";" | sudo tee -a /etc/apt/apt.conf.d/proxy.conf >/dev/null
echo -e "Acquire::https::Proxy \"${__ZSHPROXY_HTTP}\";" | sudo tee -a /etc/apt/apt.conf.d/proxy.conf >/dev/null
echo "- apt"
fi
}
__disable_proxy_apt() {
if [ -d "/etc/apt/apt.conf.d" ]; then
sudo rm -rf /etc/apt/apt.conf.d/proxy.conf
fi
}
# Proxy for pip
# pip can read -r http_proxy & https_proxy
# Proxy for terminal
__enable_proxy_all() {
# http_proxy
export http_proxy="${__ZSHPROXY_HTTP}"
export HTTP_PROXY="${__ZSHPROXY_HTTP}"
# https_proxy
export https_proxy="${__ZSHPROXY_HTTP}"
export HTTPS_PROXY="${__ZSHPROXY_HTTP}"
# ftp_proxy
export ftp_proxy="${__ZSHPROXY_HTTP}"
export FTP_PROXY="${__ZSHPROXY_HTTP}"
# rsync_proxy
export rsync_proxy="${__ZSHPROXY_HTTP}"
export RSYNC_PROXY="${__ZSHPROXY_HTTP}"
# all_proxy
export ALL_PROXY="${__ZSHPROXY_SOCKS5}"
export all_proxy="${__ZSHPROXY_SOCKS5}"
export no_proxy="${__ZSHPROXY_NO_PROXY}"
}
__disable_proxy_all() {
unset http_proxy
unset HTTP_PROXY
unset https_proxy
unset HTTPS_PROXY
unset ftp_proxy
unset FTP_PROXY
unset rsync_proxy
unset RSYNC_PROXY
unset ALL_PROXY
unset all_proxy
unset no_proxy
}
# Proxy for Git
__enable_proxy_git() {
if [ "${__ZSHPROXY_GIT_PROXY_TYPE}" = "http" ]; then
git config --global http.proxy "${__ZSHPROXY_HTTP}"
git config --global https.proxy "${__ZSHPROXY_HTTP}"
else
git config --global http.proxy "${__ZSHPROXY_SOCKS5}"
git config --global https.proxy "${__ZSHPROXY_SOCKS5}"
fi
}
__disable_proxy_git() {
git config --global --unset http.proxy
git config --global --unset https.proxy
}
# Clone with SSH can be sfind at https://github.com/comwrg/FUCK-GFW#git
# NPM
__enable_proxy_npm() {
if command -v npm >/dev/null; then
npm config set proxy "${__ZSHPROXY_HTTP}"
npm config set https-proxy "${__ZSHPROXY_HTTP}"
echo "- npm"
fi
if command -v yarn >/dev/null; then
yarn config set proxy "${__ZSHPROXY_HTTP}" >/dev/null 2>&1
yarn config set https-proxy "${__ZSHPROXY_HTTP}" >/dev/null 2>&1
echo "- yarn"
fi
if command -v pnpm >/dev/null; then
pnpm config set proxy "${__ZSHPROXY_HTTP}" >/dev/null 2>&1
pnpm config set https-proxy "${__ZSHPROXY_HTTP}" >/dev/null 2>&1
echo "- pnpm"
fi
}
__disable_proxy_npm() {
if command -v npm >/dev/null; then
npm config delete proxy
npm config delete https-proxy
fi
if command -v yarn >/dev/null; then
yarn config delete proxy >/dev/null 2>&1
yarn config delete https-proxy >/dev/null 2>&1
fi
if command -v pnpm >/dev/null; then
pnpm config delete proxy >/dev/null 2>&1
pnpm config delete https-proxy >/dev/null 2>&1
fi
}
# ==================================================
__enable_proxy() {
if [ -z "${__ZSHPROXY_STATUS}" ] || [ -z "${__ZSHPROXY_SOCKS5}" ] || [ -z "${__ZSHPROXY_HTTP}" ]; then
echo "========================================"
echo "zsh-proxy can not read -r configuration."
echo "You may have to reinitialize and reconfigure the plugin."
echo "Use following commands would help:"
echo "$ init_proxy"
echo "$ config_proxy"
echo "$ proxy"
echo "========================================"
else
echo "========================================"
echo -n "Resetting proxy... "
__disable_proxy_all
__disable_proxy_git
__disable_proxy_npm
if groups $USER | grep -q '\bsudo\b'; then
__disable_proxy_apt
fi
echo "Done!"
echo "----------------------------------------"
echo "Enable proxy for:"
echo "- shell"
__enable_proxy_all
echo "- git"
__enable_proxy_git
# npm & yarn & pnpm"
__enable_proxy_npm
# apt"
if groups $USER | grep -q '\bsudo\b'; then
echo "- apt"
__enable_proxy_apt
fi
echo "Done!"
fi
}
__disable_proxy() {
__disable_proxy_all
__disable_proxy_git
__disable_proxy_npm
if groups $USER | grep -q '\bsudo\b'; then
__disable_proxy_apt
fi
}
__auto_proxy() {
if [ "${__ZSHPROXY_STATUS}" = "1" ]; then
__enable_proxy_all
fi
}
__zsh_proxy_update() {
__NOW_PATH=$(
cd "$(dirname "$0")" || exit
pwd
)
cd "$HOME/.oh-my-zsh/custom/plugins/zsh-proxy" || exit
git fetch --all
git reset --hard origin/master
source "${ZDOTDIR:-${HOME}}/.zshrc"
cd "${__NOW_PATH}" || exit
}
# ==================================================
init_proxy() {
mkdir -p "${ZDOTDIR:-${HOME}}/.zsh-proxy"
touch "${ZDOTDIR:-${HOME}}/.zsh-proxy/status"
echo "0" >"${ZDOTDIR:-${HOME}}/.zsh-proxy/status"
touch "${ZDOTDIR:-${HOME}}/.zsh-proxy/http"
touch "${ZDOTDIR:-${HOME}}/.zsh-proxy/socks5"
touch "${ZDOTDIR:-${HOME}}/.zsh-proxy/no_proxy"
touch "${ZDOTDIR:-${HOME}}/.zsh-proxy/git_proxy_type"
echo "----------------------------------------"
echo "Great! The zsh-proxy is initialized"
echo ""
echo -E ' ______ _____ _ _ _____ '
echo -E ' |___ // ____| | | | | __ \ '
echo -E ' / /| (___ | |__| | | |__| ) __ _____ ___ _ '
echo -E " / / \___ \| __ | | ___/ '__/ _ \ \/ | | | |"
echo -E ' / /__ ____) | | | | | | | | | (_) > <| |_| |'
echo -E ' /_____|_____/|_| |_| |_| |_| \___/_/\_\\__, |'
echo -E ' __/ |'
echo -E ' |___/ '
echo "----------------------------------------"
echo "Now you might want to run following command:"
echo "$ config_proxy"
echo "----------------------------------------"
}
config_proxy() {
__config_proxy
}
proxy() {
echo "1" >"${ZDOTDIR:-${HOME}}/.zsh-proxy/status"
__enable_proxy
__check_ip
}
noproxy() {
echo "0" >"${ZDOTDIR:-${HOME}}/.zsh-proxy/status"
__disable_proxy
__check_ip
}
myip() {
__check_ip
}
zsh_proxy_update() {
__zsh_proxy_update
}
__check_whether_init
__auto_proxy