-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.sh
714 lines (592 loc) · 24.6 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
#!/usr/bin/env bash
# Proxy functions
function get_no_proxy() {
# hostname & hostip
[[ -z "${HOSTNAME}" ]] && HOSTNAME=$(uname -n 2>/dev/null)
[[ -z "${HOSTNAME}" ]] && HOSTNAME=$(hostname 2>/dev/null)
[[ -n "${HOSTNAME}" ]] && export HOSTNAME
[[ -z "${HOSTIP}" ]] && HOSTIP=$(hostname -I 2>/dev/null | cut -d' ' -f1)
[[ -n "${HOSTIP}" ]] && export HOSTIP
[[ -z "${HOSTIP_ALL}" ]] && HOSTIP_ALL=$(hostname -I 2>/dev/null)
[[ -n "${HOSTIP_ALL}" ]] && HOSTIP_ALL="${HOSTIP_ALL% }" && HOSTIP_ALL="${HOSTIP_ALL// /,}" && export HOSTIP_ALL
# no proxy lists
if [[ -z "${NO_PROXY_LIST[*]}" ]]; then
NO_PROXY_LIST=(
"127.0.0.1"
"::1"
".corp"
".internal"
".local"
".localdomain"
)
fi
for Target in "${NO_PROXY_LIST[@]}"; do
[[ -n "${GLOBAL_NO_PROXY}" ]] && GLOBAL_NO_PROXY="${GLOBAL_NO_PROXY},${Target}" || GLOBAL_NO_PROXY="${Target}"
done
[[ -n "${HOSTNAME}" ]] && GLOBAL_NO_PROXY="${GLOBAL_NO_PROXY},${HOSTNAME}"
[[ -n "${HOSTIP_ALL}" ]] && GLOBAL_NO_PROXY="${GLOBAL_NO_PROXY},${HOSTIP_ALL}"
export GLOBAL_NO_PROXY="${GLOBAL_NO_PROXY}"
}
function set_proxy() {
# PROTOCOL://USERNAME:PASSWORD@HOST:PORT
# http://127.0.0.1:8080
# socks5h://127.0.0.1:8080
# PASSWORD has special characters:
# [@ %40] [: %3A] [! %21] [# %23] [$ %24]
# F@o:o!B#ar$ -> F%40o%3Ao%21B%23ar%24
local PROXY_ADDRESS=${1:-""}
if [[ -z "${PROXY_ADDRESS}" && -n "${GLOBAL_PROXY_IP}" ]]; then
if [[ -n "${GLOBAL_PROXY_SOCKS_PORT}" ]]; then
PROXY_ADDRESS="${GLOBAL_PROXY_SOCKS_PROTOCOL}://${GLOBAL_PROXY_IP}:${GLOBAL_PROXY_SOCKS_PORT}"
elif [[ -n "${GLOBAL_PROXY_MIXED_PORT}" ]]; then
PROXY_ADDRESS="http://${GLOBAL_PROXY_IP}:${GLOBAL_PROXY_MIXED_PORT}"
fi
fi
[[ -z "${PROXY_ADDRESS}" ]] && PROXY_ADDRESS="http://127.0.0.1:8080"
export {http,https,ftp,all}_proxy="${PROXY_ADDRESS}"
export no_proxy="${GLOBAL_NO_PROXY}"
# export no_proxy="localhost,127.0.0.0/8,*.local"
# for curl
export {HTTP,HTTPS,FTP,ALL}_PROXY="${PROXY_ADDRESS}"
export NO_PROXY="${GLOBAL_NO_PROXY}"
}
function get_proxy() {
local proxy_output1 proxy_output2
[[ -n "${http_proxy}" ]] && colorEcho "${BLUE}http_proxy=${FUCHSIA}${http_proxy}"
[[ -n "${https_proxy}" ]] && colorEcho "${BLUE}https_proxy=${FUCHSIA}${https_proxy}"
[[ -n "${ftp_proxy}" ]] && colorEcho "${BLUE}ftp_proxy=${FUCHSIA}${ftp_proxy}"
[[ -n "${all_proxy}" ]] && colorEcho "${BLUE}all_proxy=${FUCHSIA}${all_proxy}"
[[ -n "${no_proxy}" ]] && colorEcho "${BLUE}no_proxy=${FUCHSIA}${no_proxy}"
echo ""
[[ -n "${HTTP_PROXY}" ]] && colorEcho "${BLUE}HTTP_PROXY=${FUCHSIA}${HTTP_PROXY}"
[[ -n "${HTTPS_PROXY}" ]] && colorEcho "${BLUE}HTTPS_PROXY=${FUCHSIA}${HTTPS_PROXY}"
[[ -n "${FTP_PROXY}" ]] && colorEcho "${BLUE}FTP_PROXY=${FUCHSIA}${FTP_PROXY}"
[[ -n "${ALL_PROXY}" ]] && colorEcho "${BLUE}ALL_PROXY=${FUCHSIA}${ALL_PROXY}"
[[ -n "${NO_PROXY}" ]] && colorEcho "${BLUE}NO_PROXY=${FUCHSIA}${NO_PROXY}"
if [[ -x "$(command -v git)" ]]; then
proxy_output1=$(git config --global --list 2>/dev/null | grep -E "http\.proxy|https\.proxy|http\.http|https\.http")
[[ -n "${proxy_output1}" ]] && colorEcho "\n${BLUE}git proxies:\n${FUCHSIA}${proxy_output1}"
fi
if [[ -x "$(command -v node)" && -x "$(command -v npm)" ]]; then
proxy_output1=$(npm config get proxy | grep -v "null")
proxy_output2=$(npm config get https-proxy | grep -v "null")
[[ -n "${proxy_output1}" ]] && colorEcho "\n${BLUE}npm proxies:\n${FUCHSIA}${proxy_output1}"
[[ -n "${proxy_output2}" ]] && colorEcho "${FUCHSIA}${proxy_output2}"
fi
if [[ -x "$(command -v yarn)" ]]; then
proxy_output1=$(yarn config get proxy | grep -Ev "null|undefined")
proxy_output2=$(yarn config get https-proxy | grep -Ev "null|undefined")
[[ -n "${proxy_output1}" ]] && colorEcho "\n${BLUE}yarn proxies:\n${FUCHSIA}${proxy_output1}"
[[ -n "${proxy_output2}" ]] && colorEcho "${FUCHSIA}${proxy_output2}"
fi
if [[ -s "/etc/apt/apt.conf.d/80proxy" ]]; then
proxy_output1=$(< "/etc/apt/apt.conf.d/80proxy")
[[ -n "${proxy_output1}" ]] && colorEcho "\n${BLUE}apt proxies:\n${FUCHSIA}${proxy_output1}"
fi
if [[ -s "/etc/yum.conf" ]]; then
proxy_output1=$(grep "proxy=" "/etc/yum.conf")
[[ -n "${proxy_output1}" ]] && colorEcho "\n${BLUE}yum proxies:\n${FUCHSIA}${proxy_output1}"
fi
if [[ -s "$HOME/.wgetrc" ]]; then
proxy_output1=$(< "$HOME/.wgetrc")
[[ -n "${proxy_output1}" ]] && colorEcho "\n${BLUE}wget proxies:\n${FUCHSIA}${proxy_output1}"
fi
if [[ -s "$HOME/.curlrc" ]]; then
proxy_output1=$(< "$HOME/.curlrc")
[[ -n "${proxy_output1}" ]] && colorEcho "\n${BLUE}curl proxies(.curlrc):\n${FUCHSIA}${proxy_output1}"
fi
if [[ -s "$HOME/.curl_socks5" ]]; then
proxy_output1=$(< "$HOME/.curl_socks5")
[[ -n "${proxy_output1}" ]] && colorEcho "\n${BLUE}curl proxies(.curl_socks5):\n${FUCHSIA}${proxy_output1}"
fi
if [[ -s "$HOME/.gradle/gradle.properties" ]]; then
proxy_output1=$(grep "systemProp.http" "$HOME/.gradle/gradle.properties")
[[ -n "${proxy_output1}" ]] && colorEcho "\n${BLUE}gradle proxies:\n${FUCHSIA}${proxy_output1}"
fi
if [[ -s "$HOME/.gemrc" ]]; then
proxy_output1=$(grep "http_proxy: " "$HOME/.gemrc")
[[ -n "${proxy_output1}" ]] && colorEcho "\n${BLUE}gem proxies:\n${FUCHSIA}${proxy_output1}"
fi
}
function clear_proxy() {
unset {http,https,ftp,all}_proxy
unset {HTTP,HTTPS,FTP,ALL}_PROXY
}
function clear_all_proxy() {
clear_proxy
set_git_proxy
# set_git_special_proxy "github.com,gitlab.com"
set_curl_socks_proxy
# [[ -s "$HOME/.curl_socks5" ]] && set_curl_socks_proxy "" "$HOME/.curl_socks5"
set_special_socks5_proxy
set_wget_proxy
}
function proxy_cmd() {
[[ -z $* ]] && colorEcho "${GREEN}Set proxy for specific command." && return 0
if [[ -n "${all_proxy}" ]]; then
colorEcho "${GREEN}Using proxy: ${FUCHSIA}${all_proxy}"
"$@"
else
if [[ -n "${GLOBAL_PROXY_IP}" ]]; then
if [[ -n "${GLOBAL_PROXY_SOCKS_PORT}" ]]; then
set_proxy "${GLOBAL_PROXY_SOCKS_PROTOCOL}://${GLOBAL_PROXY_IP}:${GLOBAL_PROXY_SOCKS_PORT}"
elif [[ -n "${GLOBAL_PROXY_MIXED_PORT}" ]]; then
set_proxy "http://${GLOBAL_PROXY_IP}:${GLOBAL_PROXY_MIXED_PORT}"
fi
fi
[[ -n "${all_proxy}" ]] && colorEcho "${GREEN}Using proxy: ${FUCHSIA}${all_proxy}"
"$@"
[[ -n "${all_proxy}" ]] && clear_proxy && colorEcho "${GREEN}Proxy clear."
fi
}
function proxy_http_cmd() {
local proxy_address
[[ -z $* ]] && colorEcho "${GREEN}No proxy for specific command." && return 0
if [[ -n "${GLOBAL_PROXY_MIXED_PORT}" ]]; then
proxy_address="http://${GLOBAL_PROXY_IP}:${GLOBAL_PROXY_MIXED_PORT}"
http_proxy="${proxy_address}" https_proxy="${proxy_address}" \
ftp_proxy="${proxy_address}" all_proxy="${proxy_address}" \
HTTP_PROXY="${proxy_address}" HTTPS_PROXY="${proxy_address}" \
FTP_PROXY="${proxy_address}" ALL_PROXY="${proxy_address}" \
"$@"
else
"$@"
fi
}
function noproxy_cmd() {
[[ -z $* ]] && colorEcho "${GREEN}No proxy for specific command." && return 0
if [[ -n "${all_proxy}" ]]; then
http_proxy="" https_proxy="" ftp_proxy="" all_proxy="" \
HTTP_PROXY="" HTTPS_PROXY="" FTP_PROXY="" ALL_PROXY="" \
"$@"
else
"$@"
fi
}
function proxy_socks5h_to_socks5() {
# fix: golang - proxyconnect tcp: dial tcp: lookup socks5h: no such host
# https://github.com/golang/go/issues/13454
# https://github.com/golang/go/issues/24135
[[ -z $* ]] && colorEcho "${GREEN}Use ${FUCHSIA}socks5${GREEN} proxy instead of ${BLUE}socks5h${GREEN} for specific command." && return 0
if echo "${all_proxy}" | grep -q 'socks5h'; then
colorEcho "${GREEN}Using proxy: ${FUCHSIA}${all_proxy/socks5h/socks5}"
http_proxy=${http_proxy/socks5h/socks5} \
https_proxy=${https_proxy/socks5h/socks5} \
ftp_proxy=${ftp_proxy/socks5h/socks5} \
all_proxy=${all_proxy/socks5h/socks5} \
HTTP_PROXY=${HTTP_PROXY/socks5h/socks5} \
HTTPS_PROXY=${HTTPS_PROXY/socks5h/socks5} \
FTP_PROXY=${FTP_PROXY/socks5h/socks5} \
ALL_PROXY=${ALL_PROXY/socks5h/socks5} \
"$@"
else
[[ -n "${all_proxy}" ]] && colorEcho "${GREEN}Using proxy: ${FUCHSIA}${all_proxy}"
"$@"
fi
}
# SET_PROXY_FOR=('brew' 'git' 'apm')
# for cmd in $SET_PROXY_FOR; do
# hash ${cmd} > /dev/null 2>&1 && alias ${cmd}="proxy_cmd ${cmd}"
# done
# Use proxy or mirror when some sites were blocked or low speed
function set_proxy_mirrors_env() {
if check_webservice_up www.google.com; then
export THE_WORLD_BLOCKED=false
else
export THE_WORLD_BLOCKED=true
fi
}
# test http request status using http_code: return 1 when DENIED, ERROR, NO RESPONSE
function check_http_request_status() {
local url=$1
local proxy_address=$2
local proxy_protocol=$3
local httpcode exitStatus=0
if [[ -z "${proxy_address}" ]]; then
httpcode=$(curl -fsL -I --connect-timeout 3 --max-time 5 -w "%{http_code}\\n" "${url}" -o /dev/null)
else
if [[ "${proxy_protocol}" == *"socks5"* ]]; then
httpcode=$(curl -fsL -I --connect-timeout 3 --max-time 5 --socks5-hostname "${proxy_address}" -w "%{http_code}\\n" "${url}" -o /dev/null)
elif [[ "${proxy_protocol}" == *"noproxy"* ]]; then
httpcode=$(curl -fsL -I --connect-timeout 3 --max-time 5 --noproxy "*" -w "%{http_code}\\n" "${url}" -o /dev/null)
else
httpcode=$(curl -fsL -I --connect-timeout 3 --max-time 5 --proxy "${proxy_address}" -w "%{http_code}\\n" "${url}" -o /dev/null)
fi
fi
case "${httpcode}" in
[2]*)
# UP
;;
[3]*)
# REDIRECT
;;
[4]*)
# 400 Bad Request
[[ "${httpcode}" != "400" ]] && exitStatus=4 # DENIED
;;
[5]*)
exitStatus=5 # ERROR
;;
*)
exitStatus=6 # NO RESPONSE
;;
esac
# echo "${url} ${httpcode} ${proxy_protocol} ${proxy_address}"
return ${exitStatus}
}
# test the availability of a socks5 proxy
function check_socks5_proxy_up() {
# How to use:
# if check_socks5_proxy_up 127.0.0.1:1080 www.google.com; then echo "ok"; else echo "something wrong"; fi
local PROXY_ADDRESS=${1:-""}
local webservice_url=${2:-"www.google.com"}
local exitStatus=0
if [[ -z "${PROXY_ADDRESS}" && -n "${GLOBAL_PROXY_IP}" && -n "${GLOBAL_PROXY_SOCKS_PORT}" ]]; then
PROXY_ADDRESS="${GLOBAL_PROXY_IP}:${GLOBAL_PROXY_SOCKS_PORT}"
fi
[[ -z "${PROXY_ADDRESS}" ]] && PROXY_ADDRESS="127.0.0.1:1080"
# curl -fsL -I --connect-timeout 3 --max-time 5 \
# --socks5-hostname "${PROXY_ADDRESS}" \
# "${webservice_url}" >/dev/null 2>&1 || exitStatus=$?
# if [[ "$exitStatus" -eq "0" ]]; then
# return 0
# else
# return 1
# fi
if check_http_request_status "${webservice_url}" "${PROXY_ADDRESS}" "socks5"; then
return 0
else
return 1
fi
}
# test the availability of a http proxy
function check_http_proxy_up() {
# How to use:
# if check_http_proxy_up 127.0.0.1:8080 www.google.com; then echo "ok"; else echo "something wrong"; fi
local PROXY_ADDRESS=${1:-""}
local webservice_url=${2:-"www.google.com"}
local exitStatus=0
if [[ -z "${PROXY_ADDRESS}" && -n "${GLOBAL_PROXY_IP}" && -n "${GLOBAL_PROXY_MIXED_PORT}" ]]; then
PROXY_ADDRESS="${GLOBAL_PROXY_IP}:${GLOBAL_PROXY_MIXED_PORT}"
fi
[[ -z "${PROXY_ADDRESS}" ]] && PROXY_ADDRESS="127.0.0.1:8080"
# curl -fsL -I --connect-timeout 3 --max-time 5 \
# --proxy "${PROXY_ADDRESS}" \
# "${webservice_url}" >/dev/null 2>&1 || exitStatus=$?
# if [[ "$exitStatus" -eq "0" ]]; then
# return 0
# else
# return 1
# fi
if check_http_request_status "${webservice_url}" "${PROXY_ADDRESS}"; then
return 0
else
return 1
fi
}
# Set global git proxy
function set_git_proxy() {
local PROXY_ADDRESS=$1
if [[ -z "$PROXY_ADDRESS" ]]; then
git config --global --unset http.proxy
git config --global --unset https.proxy
else
git config --global http.proxy "${PROXY_ADDRESS}"
git config --global https.proxy "${PROXY_ADDRESS}"
fi
}
# Set socks5 proxy for certain git repos
function set_git_special_proxy() {
# Usage: set_git_special_proxy github.com,gitlab.com 127.0.0.1:55880
local GIT_REPO_LIST=$1
local PROXY_ADDRESS=${2:-""}
local Url_List=() TargetUrl list
# Url_List=($(echo "${GIT_REPO_LIST}" | sed 's/,/ /g'))
while read -r list; do
Url_List+=("${list}")
done <<<"${GIT_REPO_LIST}"
for TargetUrl in "${Url_List[@]}"; do
[[ -z "${TargetUrl}" ]] && continue
if [[ -z "$PROXY_ADDRESS" ]]; then
git config --global --unset "http.https://${TargetUrl}.proxy"
git config --global --unset "https.https://${TargetUrl}.proxy"
else
git config --global "http.https://${TargetUrl}.proxy" "${PROXY_ADDRESS}"
git config --global "https.https://${TargetUrl}.proxy" "${PROXY_ADDRESS}"
fi
done
}
# Set apt proxy
function set_apt_proxy() {
local PROXY_ADDRESS=$1
local APT_PROXY_CONFIG=${2:-"/etc/apt/apt.conf.d/80proxy"}
[[ ! -x "$(command -v apt)" ]] && return 0
if [[ -n "$PROXY_ADDRESS" ]]; then
{
echo -e "Acquire::http::proxy \"http://${PROXY_ADDRESS}/\";"
echo -e "Acquire::https::proxy \"http://${PROXY_ADDRESS}/\";"
echo -e "Acquire::ftp::proxy \"http://${PROXY_ADDRESS}/\";"
} | sudo tee -a "$APT_PROXY_CONFIG" >/dev/null
else
# [[ -s "$APT_PROXY_CONFIG" ]] && sudo rm -f "$APT_PROXY_CONFIG"
{
echo 'Acquire::http::Proxy "false";'
echo 'Acquire::https::Proxy "false";'
echo 'Acquire::ftp::Proxy "false";'
} | sudo tee -a "$APT_PROXY_CONFIG" >/dev/null
fi
}
# Disable apt proxy
function disable_apt_proxy() {
local APT_PROXY_CONFIG=${1:-"/etc/apt/apt.conf.d/95disable-proxy"}
[[ ! -x "$(command -v apt)" ]] && return 0
echo -e 'Acquire::http::Proxy "false";' | sudo tee "$APT_PROXY_CONFIG" >/dev/null
echo -e 'Acquire::https::Proxy "false";' | sudo tee -a "$APT_PROXY_CONFIG" >/dev/null
echo -e 'Acquire::ftp::Proxy "false";' | sudo tee -a "$APT_PROXY_CONFIG" >/dev/null
}
# Set yum proxy
function set_yum_proxy() {
local PROXY_ADDRESS=${1:-"_none_"}
local YUM_PROXY_CONFIG=${2:-"/etc/yum.conf"}
[[ ! -x "$(command -v yum)" ]] && return 0
# sudo sed -i "s/[#]*[ ]*proxy.*/proxy=_none_/" "$YUM_PROXY_CONFIG"
sudo sed -i "/[#]*[ ]*proxy.*/d" "$YUM_PROXY_CONFIG"
echo "proxy=socks5://${PROXY_ADDRESS}" | sudo tee -a "$YUM_PROXY_CONFIG" >/dev/null
}
# Set wget proxy
function set_wget_proxy() {
local PROXY_ADDRESS=$1
local WGET_CONFIG=${2:-"$HOME/.wgetrc"}
[[ ! -x "$(command -v wget)" ]] && return 0
if [[ -s "$WGET_CONFIG" ]]; then
sed -i "/^use_proxy.*/d" "$WGET_CONFIG"
sed -i "/^http_proxy.*/d" "$WGET_CONFIG"
sed -i "/^https_proxy.*/d" "$WGET_CONFIG"
sed -i "/^ftp_proxy.*/d" "$WGET_CONFIG"
sed -i "/^no_proxy.*/d" "$WGET_CONFIG"
fi
if [[ -n "$PROXY_ADDRESS" ]]; then
echo "use_proxy=on" >> "$WGET_CONFIG"
echo "http_proxy=http://${PROXY_ADDRESS}/" >> "$WGET_CONFIG"
echo "https_proxy=http://${PROXY_ADDRESS}/" >> "$WGET_CONFIG"
echo "ftp_proxy=http://${PROXY_ADDRESS}/" >> "$WGET_CONFIG"
echo "no_proxy=${GLOBAL_NO_PROXY}" >> "$WGET_CONFIG"
fi
}
# Set curl socks proxy
function set_curl_socks_proxy() {
local PROXY_ADDRESS=$1
local CURL_CONFIG=${2:-"$HOME/.curlrc"}
[[ ! -x "$(command -v curl)" ]] && return 0
if [[ -s "$CURL_CONFIG" ]]; then
sed -i "/^socks5-hostname.*/d" "${CURL_CONFIG}"
sed -i "/^noproxy.*/d" "${CURL_CONFIG}"
fi
if [[ -n "$PROXY_ADDRESS" ]]; then
echo "socks5-hostname=${PROXY_ADDRESS}" >> "${CURL_CONFIG}"
echo "noproxy=${GLOBAL_NO_PROXY}" >> "${CURL_CONFIG}"
fi
}
# Set npm http proxy
function set_npm_proxy() {
local PROXY_ADDRESS=$1
[[ ! -x "$(command -v npm)" ]] && return 0
if [[ -n "$PROXY_ADDRESS" ]]; then
npm config set proxy "http://${PROXY_ADDRESS}"
npm config set https-proxy "http://${PROXY_ADDRESS}"
else
npm config delete proxy
npm config delete https-proxy
fi
}
# Set yarn http proxy
function set_yarn_proxy() {
local PROXY_ADDRESS=$1
[[ ! -x "$(command -v yarn)" ]] && return 0
if [[ -n "$PROXY_ADDRESS" ]]; then
yarn config set proxy "http://${PROXY_ADDRESS}"
yarn config set https-proxy "http://${PROXY_ADDRESS}"
else
yarn config delete proxy
yarn config delete https-proxy
fi
}
# Set gradle http proxy
function set_gradle_proxy() {
local PROXY_HOST=$1
local PROXY_PORT=$2
local GRADLE_CONFIG=${3:-"$HOME/.gradle/gradle.properties"}
[[ ! -x "$(command -v gradle)" ]] && return 0
if [[ -s "$GRADLE_CONFIG" ]]; then
sed -i "/^systemProp.http.proxyHost.*/d" "${GRADLE_CONFIG}"
sed -i "/^systemProp.http.proxyPort.*/d" "${GRADLE_CONFIG}"
sed -i "/^systemProp.https.proxyHost.*/d" "${GRADLE_CONFIG}"
sed -i "/^systemProp.https.proxyPort.*/d" "${GRADLE_CONFIG}"
fi
if [[ -n "$PROXY_HOST" && -n "$PROXY_PORT" ]]; then
echo "systemProp.http.proxyHost=${PROXY_HOST}" >> "${GRADLE_CONFIG}"
echo "systemProp.http.proxyPort=${PROXY_PORT}" >> "${GRADLE_CONFIG}"
echo "systemProp.https.proxyHost=${PROXY_HOST}" >> "${GRADLE_CONFIG}"
echo "systemProp.https.proxyPort=${PROXY_PORT}" >> "${GRADLE_CONFIG}"
fi
}
# Set ruby gem proxy
function set_gem_proxy() {
local PROXY_ADDRESS=$1
local GEM_CONFIG=${2:-"$HOME/.gemrc"}
[[ ! -x "$(command -v gem)" ]] && return 0
if [[ -s "$GEM_CONFIG" ]]; then
sed -i "/^http_proxy.*/d" "$GEM_CONFIG"
fi
if [[ -n "$PROXY_ADDRESS" ]]; then
echo "http_proxy: http://${PROXY_ADDRESS}" >> "$GEM_CONFIG"
fi
}
# Set global proxy
function set_global_proxy() {
local SOCKS_ADDRESS=${1:-""}
local HTTP_ADDRESS=${2:-""}
local SOCKS_PROTOCOL=${3:-"socks5"}
# clear all proxy first
clear_all_proxy
[[ -z "${SOCKS_ADDRESS}" && -z "${HTTP_ADDRESS}" ]] && return 1
if [[ -n "${HTTP_ADDRESS}" ]]; then
set_proxy "http://${HTTP_ADDRESS}"
colorEcho "${GREEN} :: Now using ${FUCHSIA}http://${HTTP_ADDRESS} ${GREEN}for global proxy!"
elif [[ -n "${SOCKS_ADDRESS}" ]]; then
set_proxy "${SOCKS_PROTOCOL}://${SOCKS_ADDRESS}"
set_curl_socks_proxy "${SOCKS_ADDRESS}"
colorEcho "${GREEN} :: Now using ${FUCHSIA}${SOCKS_PROTOCOL}://${SOCKS_ADDRESS} ${GREEN}for global proxy!"
fi
# wget must use http proxy
if [[ -n "${HTTP_ADDRESS}" ]]; then
set_wget_proxy "${HTTP_ADDRESS}"
# colorEcho "${GREEN} :: Now using ${FUCHSIA}${HTTP_ADDRESS} ${GREEN}for http proxy(wget etc.)!"
fi
return 0
}
# Check & set global proxy
function check_set_global_proxy() {
local SOCKS_PORT=${1:-"1080"}
local MIXED_PORT=${2:-"8080"}
local PROXY_IP
local PROXY_SOCKS=""
local SOCKS_PROTOCOL="socks5"
local PROXY_HTTP=""
local IP_LIST="127.0.0.1"
local IP_WSL
local PROXY_SOCKS_UP="NO"
local PROXY_HTTP_UP="NO"
local CMD_DIR
if check_os_wsl2; then
# WSL2
# Fix "Invalid argument" when executing Windows commands
CMD_DIR=$(dirname "$(which ipconfig.exe)")
IP_LIST=$(cd "${CMD_DIR}" && ipconfig.exe | grep -a "IPv4" \
| grep -Eo '([0-9]{1,3}[\.]){3}[0-9]{1,3}' \
| grep -Ev "^0\.|^127\.|^172\.")
## [Accessing network applications with WSL](https://learn.microsoft.com/en-us/windows/wsl/networking)
## [Advanced settings configuration in WSL](https://learn.microsoft.com/en-us/windows/wsl/wsl-config)
## networkingMode=NAT using `host IP`
# IP_WSL=$(grep -m1 nameserver /etc/resolv.conf | awk '{print $2}')
IP_WSL=$(ip route show | grep -i default | awk '{ print $3}')
## networkingMode=Mirrored using `127.0.0.1` or `host.docker.internal` with Docker Desktop
IP_LIST=$(echo -e "127.0.0.1\n${IP_WSL}\nhost.docker.internal\n${IP_LIST}" | uniq)
fi
# unset GLOBAL_PROXY_IP
# unset GLOBAL_PROXY_SOCKS_PROTOCOL
# unset GLOBAL_PROXY_SOCKS_PORT
# unset GLOBAL_PROXY_MIXED_PORT
# {
# echo ''
# echo '# Global proxy settings'
# echo 'export GLOBAL_PROXY_IP=192.168.0.1'
# echo 'export GLOBAL_PROXY_SOCKS_PROTOCOL=socks5'
# echo 'export GLOBAL_PROXY_SOCKS_PORT=7890'
# echo 'export GLOBAL_PROXY_MIXED_PORT=7890'
# } >> "$HOME/.zshenv"
if [[ -n "${GLOBAL_PROXY_IP}" ]]; then
IP_LIST=$(echo -e "${GLOBAL_PROXY_IP}\n${IP_LIST}" | uniq)
SOCKS_PROTOCOL="${GLOBAL_PROXY_SOCKS_PROTOCOL:-${SOCKS_PROTOCOL}}"
SOCKS_PORT="${GLOBAL_PROXY_SOCKS_PORT:-${SOCKS_PORT}}"
MIXED_PORT="${GLOBAL_PROXY_MIXED_PORT:-${MIXED_PORT}}"
fi
# Set global proxy
while read -r PROXY_IP; do
# if check_socks5_proxy_up "${PROXY_IP}:${MIXED_PORT}"; then
# SOCKS_PORT="${MIXED_PORT}"
# PROXY_UP="YES"
# else
# if check_socks5_proxy_up "${PROXY_IP}:${SOCKS_PORT}"; then
# if ! check_http_proxy_up "${PROXY_IP}:${MIXED_PORT}"; then
# MIXED_PORT=""
# fi
# PROXY_UP="YES"
# fi
# fi
# Use HTTP proxy by default
if check_http_proxy_up "${PROXY_IP}:${MIXED_PORT}"; then
PROXY_HTTP_UP="YES"
else
if check_socks5_proxy_up "${PROXY_IP}:${MIXED_PORT}"; then
SOCKS_PORT="${MIXED_PORT}"
PROXY_SOCKS_UP="YES"
elif check_socks5_proxy_up "${PROXY_IP}:${SOCKS_PORT}"; then
PROXY_SOCKS_UP="YES"
fi
fi
[[ "${PROXY_HTTP_UP}" == "YES" || "${PROXY_SOCKS_UP}" == "YES" ]] && break
done <<<"${IP_LIST}"
[[ "${PROXY_HTTP_UP}" == "NO" ]] && MIXED_PORT=""
[[ "${PROXY_SOCKS_UP}" == "NO" ]] && SOCKS_PORT=""
if [[ "${PROXY_HTTP_UP}" == "YES" || "${PROXY_SOCKS_UP}" == "YES" ]]; then
[[ -n "${SOCKS_PORT}" ]] && PROXY_SOCKS="${PROXY_IP}:${SOCKS_PORT}"
[[ -n "${MIXED_PORT}" ]] && PROXY_HTTP="${PROXY_IP}:${MIXED_PORT}"
if set_global_proxy "${PROXY_SOCKS}" "${PROXY_HTTP}" "${SOCKS_PROTOCOL}"; then
export GLOBAL_PROXY_IP="${PROXY_IP}"
export GLOBAL_PROXY_SOCKS_PROTOCOL="${SOCKS_PROTOCOL}"
export GLOBAL_PROXY_SOCKS_PORT="${SOCKS_PORT}"
export GLOBAL_PROXY_MIXED_PORT="${MIXED_PORT}"
check_os_wsl2 && export GLOBAL_WSL2_HOST_IP="${PROXY_IP}"
return 0
fi
else
set_global_proxy # clear global proxy
return 1
fi
}
# Set special app socks5 proxy (curl...)
function set_special_socks5_proxy() {
local SOCKS5_PROXY=${1:-""}
CURL_SPECIAL_CONFIG=${CURL_SPECIAL_CONFIG:-"$HOME/.curl_socks5"}
if [[ -n "$SOCKS5_PROXY" ]]; then
set_curl_socks_proxy "${SOCKS5_PROXY}" "${CURL_SPECIAL_CONFIG}"
else
# cat /dev/null > "${CURL_SPECIAL_CONFIG}"
[[ -f "${CURL_SPECIAL_CONFIG}" ]] && rm "${CURL_SPECIAL_CONFIG}"
fi
}
# download clash config & restart clash
function downloadClashConfig() {
# Usage: downloadClashConfig "https://transfer.sh/xxxx/clash.yaml.enc" "/srv/clash/clash.yaml.enc" "$HOME/keyfile.key" "/srv/clash/clash.yaml"
local download_url=$1
local download_filename=$2
local encrypt_keyfile=$3
local decrypt_filename=$4
if [[ ! -s "/srv/clash/clash" ]]; then
colorEcho "${RED} Please install and run ${FUCHSIA}clash${RED} first!"
return 1
fi
if downloadDecryptFile "${download_url}" "${download_filename}" "${encrypt_keyfile}" "${decrypt_filename}"; then
if /srv/clash/clash -f /srv/clash/public.yml -t; then
sudo cp -f "/srv/clash/public.yml" "/srv/clash/config.yaml"
sudo systemctl restart clash
sleep 3
if check_socks5_proxy_up "127.0.0.1:7890"; then
colorEcho "${GREEN}The configuration looks ok!"
else
sudo journalctl -u clash --since "1 minutes ago" -e
fi
fi
fi
}