Skip to content

Instantly share code, notes, and snippets.

@souloss
Last active May 18, 2023 06:28
Show Gist options
  • Save souloss/a8a264274f5cb674120f4631b9494eb3 to your computer and use it in GitHub Desktop.
Save souloss/a8a264274f5cb674120f4631b9494eb3 to your computer and use it in GitHub Desktop.
wsl-ssr-proxy script
# 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
@souloss
Copy link
Author

souloss commented Nov 5, 2021

  1. execute source ssr-proxy enter ssr environment.
  2. execute print-proxy check proxy server if it available.
  3. execute curl google.com --connect-timeout 1 check work.
  4. execute non-proxy commands by non-proxy curl google.com --connect-timeout 1
  5. execute exit by deactivate-proxy

@souloss
Copy link
Author

souloss commented Nov 5, 2021

[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]# ****

@iiiceoo
Copy link

iiiceoo commented Nov 8, 2021

👍👍👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment