Skip to content

Commit

Permalink
modified config_proxy for wsl2 and ip will changed automaticly of WSL2
Browse files Browse the repository at this point in the history
  • Loading branch information
xiayh17 committed Aug 8, 2020
1 parent b346748 commit 47e0e58
Showing 1 changed file with 48 additions and 8 deletions.
56 changes: 48 additions & 8 deletions zsh-proxy.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,53 @@ __config_proxy() {
__read_proxy_config
}

# ==================================================

# Proxy for WSL

__getLANip() {
__hostip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
__wslip=$(hostname -I | awk '{print $1}')
}

__showLANip() {
echo "========================================"
echo "Check what your current host IP is"
echo -n "host_IP: "
__hostip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
echo "${__hostip}"
echo "========================================"
echo "Check what your current wsl2 IP is"
echo -n "wsl_IP: "
__wslip=$(hostname -I | awk '{print $1}')
echo "${__wslip}"
}

__check_whether_wsl2() {
if command -v wsl.exe >/dev/null; then
echo "----------------------------------------"
echo -n "Is this in WSL2 (y/n)? "
old_stty_cfg=$(stty -g)
stty raw -echo
answer=$( while ! head -c 1 | grep -i '[ny]' ;do true ;done )
stty $old_stty_cfg
if echo "$answer" | grep -iq "^y" ;then
__getLANip
__config_proxy2
__showLANip
else
__config_proxy
fi
fi
}

__change_ip_wsl2() {
if [ -f "${HOME}/.zsh-proxy/wsl2" ] && [ -f "${HOME}/.zsh-proxy/http_port" ] && [ -f "${HOME}/.zsh-proxy/socks5_port" ] && [ -f "${HOME}/.zsh-proxy/hostip" ];then
__getLANip
sed -i "s/"$(cat "${HOME}/.zsh-proxy/hostip")"/"${__hostip}"/" "${HOME}/.zsh-proxy/no_proxy"
echo "http://${__hostip}:$(cat "${HOME}/.zsh-proxy/http_port")" >"${HOME}/.zsh-proxy/http"
echo "socks5://${__hostip}:$(cat "${HOME}/.zsh-proxy/socks5_port")" >"${HOME}/.zsh-proxy/socks5"
fi
}

__config_proxy2() {
echo "========================================"
echo "ZSH Proxy Plugin Config for WSL2"
Expand Down Expand Up @@ -143,10 +177,18 @@ __config_proxy2() {
fi

echo "http://${__hostip}:${__read_http_port}" >"${HOME}/.zsh-proxy/http"
echo "${__read_http_port}" >"${HOME}/.zsh-proxy/http_port"
echo "socks5://${__hostip}:${__read_socks5_port}" >"${HOME}/.zsh-proxy/socks5"
echo "${__read_socks5_port}" >"${HOME}/.zsh-proxy/socks5_port"
echo "${__read_no_proxy}" >"${HOME}/.zsh-proxy/no_proxy"
echo "${__read_git_proxy_type}" >"${HOME}/.zsh-proxy/git_proxy_type"
echo "${__hostip}" > "${HOME}/.zsh-proxy/hostip"
echo "wsl2" > "${HOME}/.zsh-proxy/wsl2"

echo "----------------------------------------"
echo "You can check WSL2 ip by following command anytime:"
echo "$ showLANip"
echo "----------------------------------------"
__read_proxy_config
}

Expand Down Expand Up @@ -332,18 +374,15 @@ init_proxy() {
echo "----------------------------------------"
echo "Now you might want to run following command:"
echo "$ config_proxy"
echo "Or in WSL2 (Windows Subsystem for Linux 2)"
echo "$ config_proxy2"
echo "----------------------------------------"
}

config_proxy() {
__config_proxy
__check_whether_wsl2
}

config_proxy2() {
__getLANip
__config_proxy2
showLANip() {
__showLANip
}

proxy() {
Expand All @@ -367,4 +406,5 @@ zsh_proxy_update() {
}

__check_whether_init
__change_ip_wsl2
__auto_proxy

0 comments on commit 47e0e58

Please sign in to comment.