From 40e04e053cae8b49b5b82c047660369f962d1975 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sun, 3 Feb 2019 19:20:14 +0100 Subject: [PATCH] Parse IPs properly This is done if we want to show a public IP, internal IP, or a VPN. In the VPN case, what we actually want is to display an indicator that a VPN is active, instead of the VPN IP itself. We parse the IP here anyway, because we want to save some specific code there. --- functions/utilities.zsh | 48 ++++++ powerlevel9k.zsh-theme | 76 +------- test/segments/ip.spec | 353 ++++++++++++++++++++------------------ test/segments/vpn_ip.spec | 33 +++- 4 files changed, 263 insertions(+), 247 deletions(-) diff --git a/functions/utilities.zsh b/functions/utilities.zsh index 127007fb..a15bf650 100755 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -371,3 +371,51 @@ function upsearch () { popd > /dev/null fi } + +# Parse IP address from ifconfig on OSX and from IP on Linux +# Parameters: +# $1 - string The desired Interface +# $2 - string A root prefix for testing purposes +function p9k::parseIp() { + local desiredInterface="${1}" + + if [[ -z "${desiredInterface}" ]]; then + desiredInterface="^[^ ]+" + fi + + local ROOT_PREFIX="${2}" + if [[ "$OS" == "OSX" ]]; then + # Get a plain list of all interfaces + local rawInterfaces="$(${ROOT_PREFIX}/sbin/ifconfig -l 2>/dev/null)" + # Parse into array (split by whitespace) + local -a interfaces + interfaces=(${=rawInterfaces}) + # Parse only relevant interface names + local pattern="${desiredInterface}[^ ]?" + local -a relevantInterfaces + for rawInterface in $interfaces; do + [[ "$rawInterface" =~ $pattern ]] && relevantInterfaces+=( $MATCH ) + done + local newline=$'\n' + for interfaceName in $relevantInterfaces; do + local interface="$(${ROOT_PREFIX}/sbin/ifconfig $interfaceName 2>/dev/null)" + # Check if interface is UP. + if [[ "${interface/${newline}/}" =~ "]*>(.*?)inet[ ]*([^ ]*)" ]]; then + echo "${match[3]}" + return 0 + fi + done + else + local -a interfaces + interfaces=( "${(f)$(${ROOT_PREFIX}/sbin/ip -brief -4 a show 2>/dev/null)}" ) + local pattern="^${desiredInterface}[ ]+UP[ ]+([^/ ]+)" + for interface in "${(@)interfaces}"; do + if [[ "$interface" =~ $pattern ]]; then + echo "${match[1]}" + return 0 + fi + done + fi + + return 1 +} diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 3db36050..d114fcc8 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -599,36 +599,9 @@ prompt_public_ip() { icon='PUBLIC_IP_ICON' # Check VPN is on if VPN interface is set if [[ -n $POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ]]; then - if [[ "$OS" == "OSX" ]]; then - # Get a plain list of all interfaces - local rawInterfaces="$(${ROOT_PREFIX}/sbin/ifconfig -l)" - # Parse into array (split by whitespace) - local -a interfaces - interfaces=(${=rawInterfaces}) - # Parse only relevant interface names - local pattern="${POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE}[^ ]?" - local -a relevantInterfaces - for rawInterface in $interfaces; do - [[ "$rawInterface" =~ $pattern ]] && relevantInterfaces+=( $MATCH ) - done - for interfaceName in $relevantInterfaces; do - local interface="$(${ROOT_PREFIX}/sbin/ifconfig $interfaceName)" - # Check if interface is UP. - if [[ "$interface" =~ " $FOLDER/sbin/ifconfig < mtu 1500 + inet ${INTERFACE1_IP} txqueuelen 1000 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + device interrupt 16 memory 0xe8200000-e8220000 +INNER + exit 0 +fi + +if [[ "\$*" =~ '${INTERFACE2}' ]]; then + cat < mtu 1500 + inet ${INTERFACE2_IP} txqueuelen 1000 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + device interrupt 16 memory 0xe8200000-e8220000 +INNER + exit 0 +fi + + +# If neither INTERFACE1 nor INTERFACE2 is queried, fake a offline (DOWN) interface. +# We assume if there is at least one argument, we queried for a specific interface. +if [[ "\$#" -gt 0 ]]; then + cat < mtu 1500 + inet 5.5.5.5 txqueuelen 1000 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + device interrupt 16 memory 0xe8200000-e8220000 +INNER + exit 0 +fi + +if [[ "\$#" -eq 0 ]]; then + cat < mtu 1500 + inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 + ether 02:42:8f:5c:ed:51 txqueuelen 0 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + +tun1: flags=4099 mtu 1500 + inet 10.20.30.40 txqueuelen 1000 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + device interrupt 16 memory 0xe8200000-e8220000 + +${INTERFACE1}: flags=4099 mtu 1500 + inet ${INTERFACE1_IP} txqueuelen 1000 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + device interrupt 16 memory 0xe8200000-e8220000 + +${INTERFACE2}: flags=4099 mtu 1500 + inet ${INTERFACE2_IP} txqueuelen 1000 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + device interrupt 16 memory 0xe8200000-e8220000 + +lo: flags=73 mtu 65536 + inet 127.0.0.1 netmask 255.0.0.0 + inet6 ::1 prefixlen 128 scopeid 0x10 + loop txqueuelen 1000 (Local Loopback) + RX packets 5136 bytes 328651 (320.9 KiB) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 5136 bytes 328651 (320.9 KiB) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 +INNER +exit 0 +fi +EOF + chmod +x $FOLDER/sbin/ifconfig +} + +function fakeIp() { + local INTERFACE1="${1}" + [[ -z "${INTERFACE1}" ]] && INTERFACE1="eth0" + local INTERFACE2="${2}" + [[ -z "${INTERFACE2}" ]] && INTERFACE2="disabled-if2" + cat > $FOLDER/sbin/ip < mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 + inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0 + valid_lft forever preferred_lft forever +INNER + fi + + if [[ "\$*" =~ 'show ${INTERFACE2}' ]]; then + cat < mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 + inet 1.2.3.4 brd 10.0.2.255 scope global eth0 + valid_lft forever preferred_lft forever +INNER + fi +EOF + + chmod +x $FOLDER/sbin/ip } function testIpSegmentPrintsNothingOnOsxIfNotConnected() { - local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS - POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip custom_world) - alias networksetup='echo "not connected"' - local POWERLEVEL9K_CUSTOM_WORLD='echo world' + cat > $FOLDER/sbin/ifconfig < $FOLDER/sbin/ip < mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default - link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 -2: eth0: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 - link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff"; - fi - - if [[ "$*" == '-4 a show eth0' ]]; then - echo '2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 - inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0 - valid_lft forever preferred_lft forever'; - fi - } - # Load Powerlevel9k source powerlevel9k.zsh-theme local OS='Linux' # Fake Linux - assertEquals "%K{006} %F{000}IP %f%F{000}10.0.2.15 %k%F{006}%f " "$(build_left_prompt)" + fakeIp "eth0" - unfunction ip -} - -function testIpSegmentWorksOnLinuxWithMultipleInterfacesSpecified() { - setopt aliases - local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS - POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip) - # That command is harder to test, as it is used at first - # to get all relevant network interfaces and then for - # getting the configuration of that segment.. - ip(){ - if [[ "$*" == 'link ls up' ]]; then - echo "1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default - link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 -2: eth0: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 - link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff -3: eth1: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 - link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff -4: wlan0: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 - link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff"; - fi - - if [[ "$*" == '-4 a show eth1' ]]; then - echo '3: eth1: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 - inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0 - valid_lft forever preferred_lft forever'; - fi - } - - # Load Powerlevel9k - source powerlevel9k.zsh-theme - local OS='Linux' # Fake Linux - - assertEquals "%K{006} %F{000}IP %f%F{000}10.0.2.15 %k%F{006}%f " "$(build_left_prompt)" - - unfunction ip + assertEquals "%K{006} %F{000}IP %f%F{000}1.2.3.4 " "$(prompt_ip left 1 false "$FOLDER")" } function testIpSegmentWorksOnLinuxWithInterfaceSpecified() { - local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS - POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip) - local POWERLEVEL9K_IP_INTERFACE='xxx' - ip(){ - echo '2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 -inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0 - valid_lft forever preferred_lft forever'; - } + fakeIp "eth3" # Load Powerlevel9k source powerlevel9k.zsh-theme local OS='Linux' # Fake Linux - assertEquals "%K{006} %F{000}IP %f%F{000}10.0.2.15 %k%F{006}%f " "$(build_left_prompt)" - - unfunction ip + assertEquals "%K{006} %F{000}IP %f%F{000}1.2.3.4 " "$(prompt_ip left 1 false "$FOLDER")" } source shunit2/shunit2 \ No newline at end of file diff --git a/test/segments/vpn_ip.spec b/test/segments/vpn_ip.spec index 3333fd07..13a3e60a 100755 --- a/test/segments/vpn_ip.spec +++ b/test/segments/vpn_ip.spec @@ -95,6 +95,15 @@ function fakeIp() { INNER fi + if [[ "\$*" =~ '-brief.*show' ]]; then + cat < mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 @@ -116,27 +125,33 @@ EOF } function testVpnIpSegmentPrintsNothingOnOsxIfNotConnected() { - alias ifconfig='echo "not connected"' - # Load Powerlevel9k source powerlevel9k.zsh-theme local OS="OSX" # Fake OSX - assertEquals "" "$(prompt_vpn_ip left 1 false "$FOLDER")" + cat > $FOLDER/sbin/ifconfig < $FOLDER/sbin/ip <