Last active
May 18, 2023 06:28
-
-
Save souloss/a8a264274f5cb674120f4631b9494eb3 to your computer and use it in GitHub Desktop.
wsl-ssr-proxy script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 2021年12月22日 更新使得接受外部参数,参数 1 表示代理IP,参数 2 表示端口 | |
set-proxy(){ | |
export hostip=${1:- $(cat /etc/resolv.conf |grep -oP '(?<=nameserver\ ).*'|head -n1)} | |
export hostport=${2:- 1080} | |
export https_proxy="http://${hostip}:${hostport}" | |
export http_proxy="http://${hostip}:${hostport}" | |
} | |
unset-proxy(){ | |
export https_proxy="" | |
export http_proxy="" | |
} | |
# 2021年12月22日 更新使得避免覆盖 python 虚拟环境的 deactivate 函数 | |
deactivate-proxy () { | |
unset_proxy | |
# This should detect bash and zsh, which have a hash command that must | |
# be called to get it to forget past commands. Without forgetting | |
# past commands the $PATH changes we made may not be respected | |
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then | |
hash -r | |
fi | |
if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then | |
PS1="${_OLD_VIRTUAL_PS1:-}" | |
export PS1 | |
unset _OLD_VIRTUAL_PS1 | |
fi | |
if [ ! "$1" = "nondestructive" ] ; then | |
# Self destruct! | |
unset -f deactivate | |
fi | |
} | |
non-proxy(){ | |
unset_proxy | |
$@ | |
set_proxy | |
} | |
print-proxy(){ | |
echo "https-proxy: ${https_proxy}" | |
echo "http-proxy: ${http_proxy}" | |
echo "all-proxy: ${all_proxy}" | |
</dev/tcp/${hostip}/${hostport} | |
if [ $? == 0 ]; | |
then | |
echo "/dev/tcp/${hostip}/${hostport} successfully!" | |
else | |
echo "/dev/tcp/${hostip}/${hostport} failed!" | |
fi | |
} | |
# unset irrelevant variables | |
deactivate-proxy nondestructive | |
_OLD_VIRTUAL_PS1="${PS1:-}" | |
PS1="${PS1:-} ✈ " | |
export PS1 | |
set-proxy | |
# This should detect bash and zsh, which have a hash command that must | |
# be called to get it to forget past commands. Without forgetting | |
# past commands the $PATH changes we made may not be respected | |
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then | |
hash -r | |
fi |
[root@DESKTOP-G15J72J buildpacks]# source /bin/ssr-proxy
[root@DESKTOP-G15J72J buildpacks]# ✈ print-proxy
https-proxy: http://172.19.144.1:1080
http-proxy: http://172.19.144.1:1080
/dev/tcp/172.19.144.1/1080 successfully!
[root@DESKTOP-G15J72J buildpacks]# ✈ curl google.com --connect-timeout 1
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
[root@DESKTOP-G15J72J buildpacks]# ✈ non-proxy curl google.com --connect-timeout 1
curl: (28) Connection timed out after 1000 milliseconds
[root@DESKTOP-G15J72J buildpacks]# ✈ deactivate-proxy
[root@DESKTOP-G15J72J buildpacks]# ****
👍👍👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
source ssr-proxy
enter ssr environment.print-proxy
check proxy server if it available.curl google.com --connect-timeout 1
check work.non-proxy curl google.com --connect-timeout 1
deactivate-proxy