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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2022 Sxmo Contributors
# include common definitions
# shellcheck source=configs/default_hooks/sxmo_hook_icons.sh
. sxmo_hook_icons.sh
# shellcheck source=scripts/core/sxmo_common.sh
. "$(dirname "$0")/sxmo_common.sh"
set -e
VPNDEVICE="$(nmcli con show --active | grep -E 'wireguard|vpn' | awk '{ print $1 }')"
nofail() {
"$@" || return 0
}
stderr() {
sxmo_log "$*"
}
menu() {
dmenu -i "$@"
}
connections() {
nmcli -c no -f device,type,name -t c show | \
sed "s/802-11-wireless:/$icon_wif /" | \
sed "s/gsm:/$icon_mod /" | \
sed "s/vpn*:/$icon_key /" |\
sed "s/wireguard*:/$icon_key /" |\
sed "s/802-3-ethernet:/$icon_usb /" | \
sed "s/^:/$icon_dof /" |\
sed "s/^cdc-wdm.*:/$icon_don /" |\
sed "s/^$VPNDEVICE.*:/$icon_don /" |\
sed "s/^wlan.*:/$icon_don /" |\
sed "s/^wwan.*:/$icon_don /" |\
sed "s/^eth.*:/$icon_don /" |\
sed "s/^usb.*:/$icon_don /"
}
toggleconnection() {
CONNLINE="$1"
if echo "$CONNLINE" | grep -q "^$icon_don "; then
CONNNAME="$(echo "$CONNLINE" | cut -d' ' -f3-)"
RES="$(nofail nmcli c down "$CONNNAME" 2>&1)"
else
CONNNAME="$(echo "$CONNLINE" | cut -d' ' -f3-)"
rfkill list wifi | grep -q "yes" || WIFI_ENABLED=1
if [ "$(echo "$CONNLINE" | cut -d' ' -f2)" = "$icon_wif" ] && [ -z "$WIFI_ENABLED" ]; then
notify-send "Enabling wifi first."
doas sxmo_wifitoggle.sh
fi
RES="$(nofail nmcli c up "$CONNNAME" 2>&1)"
fi
notify-send "$RES"
stderr "$RES"
}
deletenetworkmenu() {
CHOICE="$(
printf %b "$icon_cls Close Menu\n$(connections)" |
menu -p "Delete Network"
)"
[ -z "$CHOICE" ] && return
echo "$CHOICE" | grep -q "Close Menu" && return
CONNNAME="$(echo "$CHOICE" | cut -d' ' -f3-)"
RES="$(nofail nmcli c delete "$CONNNAME" 2>&1)"
notify-send "$RES"
stderr "$RES"
}
getifname() {
IFTYPE="$1"
IFNAME="$(nmcli d | grep -m 1 "$IFTYPE" | cut -d' ' -f1)"
if [ -z "$IFNAME" ]; then
notify-send "No interface with type $IFTYPE found"
stderr "No interface with type $IFTYPE found"
IFNAME=lo
fi
echo "$IFNAME"
}
addnetworkgsmmenu() {
CONNNAME="$(
echo "$icon_cls Close Menu" |
sxmo_dmenu_with_kb.sh -p "Alias"
)"
[ -z "$CONNNAME" ] && return
echo "$CONNNAME" | grep -q "Close Menu" && return
APN="$(
echo "$icon_cls Close Menu" |
sxmo_dmenu_with_kb.sh -p "APN"
)"
[ -z "$APN" ] && return
echo "$APN" | grep -q "Close Menu" && return
USERNAME="$(printf "None\n%s Close Menu\n" "$icon_cls" | menu -p "Username")"
case "$USERNAME" in
""|"$icon_cls Close Menu")
return
;;
None)
unset USERNAME
;;
esac
PASSWORD="$(printf "None\n%s Close Menu\n" "$icon_cls" | menu -p "Password")"
case "$PASSWORD" in
""|"$icon_cls Close Menu")
return
;;
None)
unset PASSWORD
;;
esac
RES="$(nofail nmcli c add \
type gsm \
ifname "$(getifname gsm)" \
con-name "$CONNNAME" \
apn "$APN" \
${PASSWORD:+gsm.password "$PASSWORD"} \
${USERNAME:+gsm.username "$USERNAME"} \
2>&1)"
stderr "$RES"
notify-send "$RES"
}
addnetworkwpamenu() {
SSID="$(cat <<EOF | sxmo_dmenu_with_kb.sh -p "SSID"
$icon_cls Close Menu
$(nmcli d wifi list | tail -n +2 | grep -v '^\*' | awk -F' ' '{ print $6 }' | grep -v '\-\-')
EOF
)"
[ -z "$SSID" ] && return
echo "$SSID" | grep -q "Close Menu" && return
PASSPHRASE="$(cat <<EOF | sxmo_dmenu_with_kb.sh -p "Passphrase"
$icon_cls Close Menu
None
EOF
)"
if [ -z "$PASSPHRASE" ] || [ "None" = "$PASSPHRASE" ]; then
unset PASSPHRASE
fi
echo "$PASSPHRASE" | grep -q "Close Menu" && return
RES="$(nofail nmcli c add \
type wifi \
ifname wlan0 \
con-name "$SSID" \
ssid "$SSID" \
${PASSPHRASE:+802-11-wireless-security.key-mgmt wpa-psk 802-11-wireless-security.psk "$PASSPHRASE"} 2>&1)"
stderr "$RES"
notify-send "$RES"
}
addhotspotusbmenu() {
CONNNAME="$(
echo "$icon_cls Close Menu" |
sxmo_dmenu_with_kb.sh -p "Alias"
)"
[ -z "$CONNNAME" ] && return
echo "$CONNNAME" | grep -q "Close Menu" && return
# TODO: restart udhcpd after disconnecting on postmarketOS
RES="$(nofail nmcli c add \
type ethernet \
ifname usb0 \
con-name "$CONNNAME" \
ipv4.method shared 2>&1)"
stderr "$RES"
notify-send "$RES"
}
addhotspotwifimenu() {
SSID="$(
echo "$icon_cls Close Menu" |
sxmo_dmenu_with_kb.sh -p "SSID"
)"
[ -z "$SSID" ] && return
echo "$SSID" | grep -q "Close Menu" && return
key="$(
echo "$icon_cls Close Menu" |
sxmo_dmenu_with_kb.sh -p "Passphrase"
)"
[ -z "$key" ] && return
echo "$key" | grep -q "Close Menu" && return
key1="$(
echo "$icon_cls Close Menu" |
sxmo_dmenu_with_kb.sh -p "Confirm"
)"
[ -z "$key1" ] && return
echo "$key1" | grep -q "Close Menu" && return
if [ "$key" != "$key1" ]; then
notify-send "key mismatch"
stderr "key mismatch"
return
fi
# valid keys for WPA networks have a lengths of 8-63 characters
keylen=$( echo "$key" | tr -d '\n' | wc -c )
keylen=$(( keylen ))
if [ $keylen -lt 8 ]; then
notify-send "key too short ($keylen < 8)"
stderr "key too short ($keylen < 8)"
return
elif [ $keylen -gt 63 ]; then
notify-send "key too long ($keylen > 63)"
stderr "key too long ($keylen > 63)"
return
fi
channel="$(
printf "%s Close Menu\n11\n" "$icon_cls" | sxmo_dmenu_with_kb.sh -p "Channel"
)"
[ -z "$channel" ] && return
echo "$channel" | grep -q "Close Menu" && return
RES="$(nofail nmcli device wifi hotspot ifname wlan0 \
con-name "Hotspot $SSID" \
ssid "$SSID" \
channel "$channel" \
band bg password "$key")"
stderr "$RES"
notify-send "$RES"
}
networksmenu() {
while true; do
CHOICE="$(
rfkill list wifi | grep -q "yes" || WIFI_ENABLED=1
grep . << EOF | sxmo_dmenu.sh -p "Networks"
$icon_cls Close Menu
$(
if [ -z "$WIFI_ENABLED" ]; then
connections | grep -v "$icon_wif"
else
connections
fi
)
$icon_mod Add a GSM Network
$([ -z "$WIFI_ENABLED" ] || printf "%s Add a WPA Network\n" "$icon_wif")
$([ -z "$WIFI_ENABLED" ] || printf "%s Add a Wifi Hotspot\n" "$icon_wif")
$icon_usb Add a USB Hotspot
$icon_cls Delete a Network
$(
if [ -z "$WIFI_ENABLED" ]; then
printf "%s Enable Wifi\n" "$icon_wif"
else
printf "%s Disable Wifi\n" "$icon_wif"
fi
)
$icon_cfg Nmtui
$icon_cfg Ifconfig
$([ -z "$WIFI_ENABLED" ] || printf "%s Scan Wifi Networks\n" "$icon_wif")
EOF
)" || exit
case "$CHOICE" in
*"Close Menu" )
exit
;;
*"Add a GSM Network" )
addnetworkgsmmenu
;;
*"Add a WPA Network" )
addnetworkwpamenu
;;
*"Add a Wifi Hotspot" )
addhotspotwifimenu
;;
*"Add a USB Hotspot")
addhotspotusbmenu
;;
*"Delete a Network" )
deletenetworkmenu
;;
*"Nmtui" )
sxmo_terminal.sh nmtui || continue # Killeable
;;
*"Ifconfig" )
sxmo_terminal.sh watch -n 2 ip a || continue # Killeable
;;
*"Scan Wifi Networks" )
sxmo_terminal.sh watch -n 2 nmcli d wifi list || continue # Killeable
;;
*"Disable Wifi"|*"Enable Wifi" )
doas sxmo_wifitoggle.sh
;;
*)
toggleconnection "$CHOICE"
;;
esac
done
}
if [ $# -gt 0 ]; then
"$@"
else
networksmenu
fi
|