From d9cfddbdf03cf6b70411708aa01ffd4ea050a8bc Mon Sep 17 00:00:00 2001 From: Vigilans Date: Sun, 21 Aug 2022 11:48:09 +0800 Subject: [PATCH] Retrieve outbound interface ip by `ip route get` --- ardnspod | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/ardnspod b/ardnspod index df5f654..cc9067e 100644 --- a/ardnspod +++ b/ardnspod @@ -38,10 +38,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") @@ -66,10 +68,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