From 14addfc9ff18dce5f3fc1af7b09b453ff73749ad Mon Sep 17 00:00:00 2001 From: Konano Date: Mon, 27 Nov 2023 16:40:01 +0800 Subject: [PATCH] Add net alias and source user_utils file --- files/.user_utils | 54 +++++++++++++++++++++++++++++++++++++++++++++++ files/.zshrc | 3 +++ init_user.sh | 1 + 3 files changed, 58 insertions(+) create mode 100644 files/.user_utils diff --git a/files/.user_utils b/files/.user_utils new file mode 100644 index 0000000..53896f0 --- /dev/null +++ b/files/.user_utils @@ -0,0 +1,54 @@ +#!/bin/zsh + +__is_public_ipv4() { + local ip=$1 + if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + IFS='.' read -r -A ADDR <<< "$ip" + if [[ ${ADDR[1]} -eq 10 ]]; then + return 1 + elif [[ ${ADDR[1]} -eq 172 && ${ADDR[2]} -ge 16 && ${ADDR[2]} -le 31 ]]; then + return 1 + elif [[ ${ADDR[1]} -eq 192 && ${ADDR[2]} -eq 168 ]]; then + return 1 + else + return 0 + fi + else + return 1 + fi +} + +__get_ipv4() { + local ipv4=$(ip route get 1.1.1.1 | head -1 | awk '{print $7}') + if __is_public_ipv4 $ipv4; then echo "$ipv4"; return 0; fi + ipv4=$(ip route get 1.1.1.1 | head -1 | awk '{print $NF}') + if __is_public_ipv4 $ipv4; then echo "$ipv4"; return 0; fi + ipv4=$(curl -4 -s ifconfig.me) + if __is_public_ipv4 $ipv4; then echo "$ipv4"; return 0; fi + ipv4=$(host -4 myip.opendns.com resolver1.opendns.com | grep "myip.opendns.com has" | awk '{print $4}') + if __is_public_ipv4 $ipv4; then echo "$ipv4"; return 0; fi + echo "No public IPv4 address found" + return 1 +} + +__get_ipv6() { + if [[ "$(ip -6 route get 2001:4860:4860::8888 2>&1 | awk '{print $NF}')" == "unreachable" ]]; then + echo "No IPv6 connectivity" + return 1 + fi + local ipv6=$(ip route get 2001:4860:4860::8888 | head -1 | awk '{print $11}') + if [[ $ipv6 =~ ^([0-9a-fA-F]{0,4}:){7}[0-9a-fA-F]{0,4}$ ]]; then echo "$ipv6"; return 0; fi + ipv6=$(ip route get 2001:4860:4860::8888 | head -1 | awk '{print $NF}') + if [[ $ipv6 =~ ^([0-9a-fA-F]{0,4}:){7}[0-9a-fA-F]{0,4}$ ]]; then echo "$ipv6"; return 0; fi + ipv6=$(curl -6 -s ifconfig.me) + if [[ $ipv6 =~ ^([0-9a-fA-F]{0,4}:){7}[0-9a-fA-F]{0,4}$ ]]; then echo "$ipv6"; return 0; fi + echo "No public IPv6 address found" + return 1 +} + +__get_ip() { + __get_ipv4 + __get_ipv6 +} + +__get_ipv4 diff --git a/files/.zshrc b/files/.zshrc index 9dba9b9..0a9fcd2 100644 --- a/files/.zshrc +++ b/files/.zshrc @@ -151,6 +151,9 @@ fi command_exists docker && alias gost='docker run --rm -p 9443:9443 --cpus=0.2 --memory=256mb gogost/gost -L socks5://nano:QpuwAqcsP8Xp@:9443' +source ~/.user_utils +alias net='__get_ip' + _systemctl_unit_state() { typeset -gA _sys_unit_state _sys_unit_state=( $(__systemctl list-unit-files '$PREFIX*' | awk '{print $1, $2}') ) diff --git a/init_user.sh b/init_user.sh index 3ff93f2..1c5656c 100755 --- a/init_user.sh +++ b/init_user.sh @@ -79,6 +79,7 @@ configure_nodejs() { # ============================================================ update_file $scriptdir/files/ys-simple.zsh-theme ~/.oh-my-zsh/themes/ys-simple.zsh-theme +update_file $scriptdir/files/.user_utils ~/.user_utils update_authorized_keys if [ $# -ne 0 ]; then