summaryrefslogtreecommitdiff
path: root/ar/.local/bin/statusbar/sb-internet
blob: 896a31bb4f32b292ffbec1a10655aeb21e8e6a75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/sh

# Show wifi 🛜 and percent strength or 📡 if none.
# Show 🌐 if connected to ethernet or ❎ if none.
# Show 🛰 if a vpn connection is active
# Show 🇰🇷 country flag via geoip lookup

# Toggle each section: 1 to show, 0 to hide
SHOW_WIFI=1
SHOW_ETH=1
SHOW_VPN=1
SHOW_IPLOC=0

eth_con="$(nmcli -t -f NAME,TYPE,DEVICE connection show |
  awk -F: '$2=="ethernet" && $3!="" { print $1; exit }')"

wifi_con="$(nmcli -t -f NAME,TYPE connection show |
  awk -F: '$2=="wifi" { print $1; exit }')"

if [ -n "$eth_con" ]; then
  nmcli connection modify "$eth_con" ipv4.method auto
  nmcli connection modify "$eth_con" ipv4.never-default no
  nmcli connection modify "$eth_con" ipv4.dns-priority -42
  nmcli connection modify "$eth_con" ipv4.route-metric 100
  nmcli connection modify "$eth_con" connection.autoconnect yes
fi

if [ -n "$wifi_con" ]; then
  nmcli connection modify "$wifi_con" ipv4.dns-priority 100
  nmcli connection modify "$wifi_con" ipv4.route-metric 600
fi

case $BLOCK_BUTTON in
1)
  "$TERMINAL" -e nmtui
  pkill -RTMIN+6 dwmblocks
  ;;
2)
  wifi_dev="$(nmcli -t -f DEVICE,TYPE device |
    awk -F: '$2=="wifi" { print $1; exit }')"

  wifi_state="$(nmcli -t -f DEVICE,STATE device |
    awk -F: -v dev="$wifi_dev" '$1==dev { print $2 }')"

  if [ "$wifi_state" = "connected" ]; then
    nmcli radio wifi off
    notify-send "Wi-Fi" "Wi-Fi disconnected"
  else
    nmcli radio wifi on
    notify-send "Wi-Fi" "Wi-Fi connected"
  fi

  pkill -RTMIN+6 dwmblocks
  ;;
3) notify-send "🌐 Internet module" "\- Left click to connect
- Middle click to toggle wifi
❌: wifi disabled
📡: no wifi connection
🛜: wifi connection with quality
❎: no ethernet
🌐: ethernet working
🛰: vpn is active
🏳: ip geolocation (country flag)
" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac

icons=""

# IP Geolocation
if [ "$SHOW_IPLOC" = 1 ]; then
  ip="$(curl -4sfm 3 ifconfig.me 2>/dev/null)"
  if [ -n "$ip" ]; then
    addr="$(geoiplookup "$ip" 2>/dev/null)"
  else
    ip="$(curl -6sfm 3 ifconfig.me 2>/dev/null)"
    [ -n "$ip" ] && addr="$(geoiplookup6 "$ip" 2>/dev/null)"
  fi
  case "$addr" in *"not found"*) addr="" ;; esac
  if [ -n "$addr" ]; then
    cc="$(echo "$addr" | sed 's/.*: \([A-Z][A-Z]\),.*/\1/')"
    if [ -n "$cc" ]; then
      c1=$(printf '%d' "'$(echo "$cc" | cut -c1)")
      c2=$(printf '%d' "'$(echo "$cc" | cut -c2)")
      oct1=$(printf '%03o' "$((c1 + 101))")
      oct2=$(printf '%03o' "$((c2 + 101))")
      flag=$(printf "\\360\\237\\207\\${oct1}\\360\\237\\207\\${oct2}")
      [ -n "$flag" ] && icons="${icons}$flag "
    fi
  fi
fi

# Wifi
if [ "$SHOW_WIFI" = 1 ]; then
  if grep -q 'up' /sys/class/net/w*/operstate 2>/dev/null; then
    if grep -q '^\s*w' /proc/net/wireless; then
      wifiicon="$(awk '/^\s*w/ { print "🛜" int($3 * 100 / 70) "%" }' /proc/net/wireless)"
    else
      wifiicon="📡"
    fi
  elif grep -q 'down' /sys/class/net/w*/operstate 2>/dev/null; then
    wifiicon="❌"
  fi
  [ -n "$wifiicon" ] && icons="${icons}$wifiicon "
fi

# Ethernet
if [ "$SHOW_ETH" = 1 ]; then
  grep -q 'up' /sys/class/net/e*/operstate && ethericon="🌐" || ethericon="❎"
  [ -n "$ethericon" ] && icons="${icons}$ethericon "
fi

# TUN/VPN
if [ "$SHOW_VPN" = 1 ]; then
  [ -n "$(cat /sys/class/net/tun*/operstate 2>/dev/null)" ] && tunicon="🛰"
  [ -n "$tunicon" ] && icons="${icons}$tunicon "
fi

printf "%s\n" "${icons% }"