Merge pull request #109 from Vigilans/vigilans/interface-ip-from-route

Retrieve outbound interface ip by `ip route get`
This commit is contained in:
rehiy authored and GitHub committed 2022-08-23 09:08:22 +08:00
commit 378bf9c4eb
1 file changed
+17 -5
+17 -5
View File
@@ -43,10 +43,12 @@ arWanIp4() {
lanIps="$lanIps|(^169\.254\.[0-9]{1,3}\.[0-9]{1,3}$)"
lanIps="$lanIps|(^172\.(1[6-9]|2[0-9]|3[0-1])\.[0-9]{1,3}\.[0-9]{1,3}$)"
lanIps="$lanIps|(^192\.168\.[0-9]{1,3}\.[0-9]{1,3}$)"
lanIps="$lanIps|(^100\.(6[4-9]|[7-9][0-9])\.[0-9]{1,3}\.[0-9]{1,3}$)" # 100.64.x.x - 100.99.x.x
lanIps="$lanIps|(^100\.1([0-1][0-9]|2[0-7])\.[0-9]{1,3}\.[0-9]{1,3}$)" # 100.100.x.x - 100.127.x.x
case $(uname) in
'Linux')
hostIp=$(ip -o -4 addr list | grep -Ev '\s(docker|lo)' | awk '{print $4}' | cut -d/ -f1 | grep -Ev "$lanIps")
hostIp=$(ip -o -4 route get 100.64.0.1 | grep -oE 'src [0-9\.]+' | awk '{print $2}' | grep -Ev "$lanIps")
;;
Darwin|FreeBSD)
hostIp=$(ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' | grep -Ev "$lanIps")
@@ -71,10 +73,20 @@ arWanIp6() {
local hostIp
if type curl >/dev/null 2>&1; then
hostIp=$(curl -s 6.ipw.cn)
else
hostIp=$(wget -q -O- 6.ipw.cn)
local lanIps="(^$)|(^::1$)|(^fe[8-9,A-F])"
case $(uname) in
'Linux')
hostIp=$(ip -o -6 route get 100::1 | grep -oE 'src [0-9a-fA-F:]+' | awk '{print $2}' | grep -Ev "$lanIps")
;;
esac
if [ -z "$hostIp" ]; then
if type curl >/dev/null 2>&1; then
hostIp=$(curl -s 6.ipw.cn)
else
hostIp=$(wget -q -O- 6.ipw.cn)
fi
fi
if [ -z "$hostIp" ]; then