Skip to content

Instantly share code, notes, and snippets.

@ChenyangGao
Last active August 3, 2023 03:22
Show Gist options
  • Save ChenyangGao/b5abf2de8bb5a91753764f9403adcc99 to your computer and use it in GitHub Desktop.
Save ChenyangGao/b5abf2de8bb5a91753764f9403adcc99 to your computer and use it in GitHub Desktop.
Shell脚本实现在Linux命令行开启代理
#? 设置代理 ip,请运行 proxy# 获取帮助信息
# AUTHOR='ChenyangGao <https://chenyanggao.github.io/>''
# VERSION=0.2
if [ -n "${MOD_PROXY_sourced+x}" ]; then
return 0
fi
# inetutils (apt)
# net-tools (yum)
-proxy-get-local-ipv4-using-hostname() {
hostname -I 2>&- | awk '{print $1}'
}
# iproute2
-proxy-get-local-ipv4-using-iproute2() {
# OR ip route get 1.2.3.4 | awk '{print $7}'
ip -4 route 2>&- | awk '{print $NF}' | grep -Eo --color=never '[0-9]+(\.[0-9]+){3}'
}
# net-tools
-proxy-get-local-ipv4-using-ifconfig() {
( ifconfig 2>&- || ip addr show 2>&- ) | grep -Eo '^\s+inet\s+\S+' | grep -Eo '[0-9]+(\.[0-9]+){3}' | grep -Ev '127\.0\.0\.1|0\.0\.0\.0'
}
# 获取本机 IPv4 地址
-proxy-get-local-ipv4() {
set -o pipefail
-proxy-get-local-ipv4-using-hostname || -proxy-get-local-ipv4-using-iproute2 || -proxy-get-local-ipv4-using-ifconfig
}
-proxy-get-local-ipv4-select() {
local ips=$(-proxy-get-local-ipv4)
local retcode=$?
if [ $retcode -ne 0 ]; then
return $retcode
fi
grep -m 1 "^192\." <<<"$ips" || \
grep -m 1 "^172\." <<<"$ips" || \
grep -m 1 "^10\." <<<"$ips" || \
head -n 1 <<<"$ips"
}
-proxy-get-remote-ssh-ip-using-ssh-env() {
( [ -n "$SSH_CLIENT" ] && awk '{print $1}' <<<"$SSH_CLIENT" ) || \
( [ -n "$SSH_CONNECTION" ] && awk '{print $1}' <<<"$SSH_CONNECTION" )
}
-proxy-get-remote-ssh-ip-using-who() {
who am i 2>&- | awk '{print $NF}' | grep '([^)]\+)' | grep -o '[^()]\+'
}
# 获取远程连接到本机的 ssh 客户端的 ip 地址
-proxy-get-remote-ssh-ip() {
-proxy-get-remote-ssh-ip-using-ssh-env || -proxy-get-remote-ssh-ip-using-who
}
# 默认的代理服务的主机地址,可以是 ip 或 域名
PROXY_HOST=$(( -proxy-get-local-ipv4-select | head -n 1 ) || echo 127.0.0.1)
# 默认的代理端口(clash 的默认端口是 7890),0-65535 之间的数字
PROXY_PORT=7890
-proxy-format-proxy() {
if [ -z "$1" ]; then
echo "$PROXY_HOST:$PROXY_PORT"
elif [[ $1 == \[* ]]; then
# as ipv6
if [[ $1 == *\] ]]; then
# as without port
echo "$1:$PROXY_PORT"
else
# as with port
echo "$1"
fi
elif [[ $1 =~ ^[0-9]+(\.[0-9]+){3}$ ]]; then
# as ipv4 (without port)
echo "$1:$PROXY_PORT"
elif [[ $1 =~ ^:[0-9]+$ ]]; then
# as bare port
echo "$PROXY_HOST$1"
elif [[ $1 == *:*:* && $1 =~ ^[0-9a-zA-Z:]+$ ]]; then
# as ipv6 (without port)
echo "[$1]:$PROXY_PORT"
elif [[ $1 != *:* ]]; then
# as hostname without port
echo "$1:$PROXY_PORT"
else
# no op, here may be a mistake
echo "$1"
fi
}
# 在 shell 中启用代理,默认采用本机上的 7890 端口的代理服务
proxy-set() {
local proxy=$(-proxy-format-proxy "$1")
export https_proxy=http://$proxy
export http_proxy=http://$proxy
export all_proxy=socks5://$proxy
export no_proxy='localhost,127.0.0.1,::1'
echo "proxy has been set to: $proxy"
## Get public ip in Linux command line
## NOTE curl ≈ wget -qO -
# curl api.ipify.org
# curl checkip.amazonaws.com
# curl cip.cc
# curl httpbin.org/ip
# curl ifconfig.cat # ipv6
# curl ifconfig.co # ipv6
# curl ifconfig.io # ipv6
# curl ifconfig.me
# curl ifconfig.net # ipv6
# curl inet-ip.info
# curl ipinfo.io # 👍
# curl ipv4.icanhazip.com
# curl ipv4.ident.me
# curl ipv4.ip.gs
# curl ipv4.ip.sb
# curl ip-api.com # 👍
# curl ip.threep.top
# curl ipecho.net/plain
# curl myip.dnsomatic.com # 👎
# curl myip.ipip.net
# curl https://checkipv4.dedyn.io
# curl https://ip.tool.lu
# dig +short myip.opendns.com @resolver1.opendns.com
# dig ANY +short @resolver2.opendns.com myip.opendns.com
curl ipinfo.io
}
alias proxy+=proxy-set
# 在 shell 中启用代理,代理服务来自 ssh 连接到这台机器的远程机器(仅适用于局域网)
proxy-set-from-remote() {
-proxy-get-remote-ssh-ip 1>&- && proxy-set $(-proxy-get-remote-ssh-ip)
}
alias proxy++=proxy-set-from-remote
# 在 shell 中停用代理
proxy-clear() {
unset http_proxy https_proxy all_proxy no_proxy
echo 'proxy has been cleared'
curl ipinfo.io
}
alias proxy-=proxy-clear
# 打印 export 命令,用于启用 shell 中的代理(来自这台机器,命令可在局域网中的其他机器上使用)
proxy-export() {
local proxy=$(-proxy-format-proxy "$1")
echo "\
# 在 shell 中启用代理,命令可在局域网中的其他机器上使用
export https_proxy=http://$proxy
export http_proxy=http://$proxy
export all_proxy=socks5://$proxy
export no_proxy='localhost,127.0.0.1,::1'
# 用以下命令停用代理
# unset http_proxy https_proxy all_proxy no_proxy"
}
alias proxy@=proxy-export
# 打印 alias 命令,用于启用和停用 shell 中的代理(来自这台机器,命令可在局域网中的其他机器上使用),其中:
# proxy+ 在 shell 中启用代理
# proxy- 在 shell 中停用代理
proxy-alias() {
local proxy=$(-proxy-format-proxy "$1")
cat << EOF
# 在 shell 中启用代理,命令可在局域网中的其他机器上使用
alias proxy+="\
export https_proxy=http://$proxy
export http_proxy=http://$proxy
export all_proxy=socks5://$proxy
export no_proxy='localhost,127.0.0.1,::1'
echo 'proxy has been set to: $proxy'
curl ipinfo.io"
# 在 shell 中停用代理,命令可在局域网中的其他机器上使用
alias proxy-="\
unset http_proxy https_proxy all_proxy no_proxy
echo 'proxy has been cleared'
curl ipinfo.io"
EOF
}
alias proxy@@=proxy-alias
alias proxy#='echo "\
[环境变量]
PROXY_HOST 默认的代理服务的主机地址,可以是 ip 或 域名
PROXY_PORT 默认的代理端口(clash 和 ssr(shadowsocksR,酸酸乳) 等科学上网软件的默认端口是 7890),0-65535 之间的数字
----------------------------------------
[命令]
proxy# 输出使用说明
proxy+ 在 shell 中启用代理,默认采用本机上的 7890 端口(clash 和 ssr 等科学上网软件的默认端口)的代理服务。如果要指定端口,运行形如 PROXY_PORT=<port> proxy+
proxy++ 在 shell 中启用代理,代理服务来自 ssh 连接到这台机器的远程机器(仅适用于局域网)。如果要指定端口,运行形如 PROXY_PORT=<port> proxy++
proxy- 停用 shell 中的代理
proxy@ 打印 export 命令,用于启用 shell 中的代理(来自这台机器,命令可在局域网中的其他机器上使用)
proxy@@ 打印 alias 命令,用于启用和停用 shell 中的代理(来自这台机器,命令可在局域网中的其他机器上使用)"'
MOD_PROXY_sourced=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment