summaryrefslogtreecommitdiff
path: root/sh
diff options
context:
space:
mode:
authorXiao Pan <xyz@flylightning.xyz>2025-01-11 21:03:52 -0800
committerXiao Pan <xyz@flylightning.xyz>2025-01-11 21:03:52 -0800
commitc1b7b022611bca8c470c2ebc3678675a436390f3 (patch)
treefcf48432ae3ef4dce87a8a0bcc9b2ba197105fc2 /sh
parent80d5c54225e115ec0f260c253f14db25fbc7d117 (diff)
wireguard switch to use cfg repo branches as names
Diffstat (limited to 'sh')
-rwxr-xr-xsh/sbar10
-rwxr-xr-xsh/vip25
-rwxr-xr-xsh/vpn22
3 files changed, 18 insertions, 39 deletions
diff --git a/sh/sbar b/sh/sbar
index 9614b6a..9f82c67 100755
--- a/sh/sbar
+++ b/sh/sbar
@@ -15,15 +15,7 @@ update_time () {
update_net () {
net="$(if nmcli -t --fields type,state device | grep -q '^\(ethernet\|wifi\):connected$'; then echo 1; else echo 0; fi)"
- case "$(nmcli -t --fields device,state device)" in
- *wg_aa:connected*) wg=aa;;
- *wg_ca:connected*) wg=ca;;
- *wg_ia:connected*) wg=ia;;
- *wg_ib:connected*) wg=ib;;
- *wg_mydefault:connected*) wg=1;;
- *wg_studio:connected*) wg=st;;
- *) wg=0;;
- esac
+ wg="$(nmcli -t --fields device,state device | awk -F_ '/wg_.*:connected/{print substr($2,0,2)}')"
}
update_vol () {
diff --git a/sh/vip b/sh/vip
index 95941be..bce93c5 100755
--- a/sh/vip
+++ b/sh/vip
@@ -1,25 +1,12 @@
#!/bin/sh
# Vpn IP
-case "$(nmcli -t --fields device,state device)" in
- *wg_aa:connected*) echo aa;;
- *wg_ca:connected*) echo ca;;
- *wg_ia:connected*) echo ia;;
- *wg_ib:connected*) echo ib;;
- *wg_mydefault:connected*) echo mydefault;;
- *wg_studio:connected*) echo studio;;
- *) echo none;;
-esac
+# seperate with _ and : , see `man awk.1p` "Otherwise, the string value of FS
+# shall be considered to be an extended regular expression. ...", so -F if
+# multiple chars means it is regex? so [_:] inside [] there's _ and : char
+# which in regex means _ and : char? More maybe see:
+# https://www.gnu.org/software/gawk/manual/html_node/Regexp-Field-Splitting.html
+nmcli -t --fields device,state device | awk -F'[_:]' '/wg_.*:connected/{print $2}'
printf 'default: '; curl -m1 ifconfig.co
printf '4: '; curl -4m1 ifconfig.co
printf '6: '; curl -6m1 ifconfig.co
-
-# the old way, slower and more code
-#nm_device_state="$(nmcli -t --fields device,state device)"
-#if echo "$nm_device_state" | grep -q '^wg_ia:connected$'; then
-# echo ia
-#elif echo "$nm_device_state" | grep -q '^wg_studio:connected$'; then
-# echo studio
-#else
-# echo insp
-#fi
diff --git a/sh/vpn b/sh/vpn
index 7898e57..8be4657 100755
--- a/sh/vpn
+++ b/sh/vpn
@@ -1,9 +1,9 @@
#!/bin/sh
# toggle VPN
-down_not_mydefault () {
- for v in wg_studio wg_ia wg_aa wg_ca wg_ib; do
- nmcli connection down "$v"
+down_not_master () {
+ git -C "$HOME/programs/config_local_arch" branch | awk '!/pp/{print ($1=="*")?$2:$1}' | while read -r v; do
+ nmcli connection down "wg_$v"
done
}
@@ -17,26 +17,26 @@ wg_aa_restore_default () {
esac
}
-# no toggle wg_mydefault, I want wg_mydefault to always on by default
-if ! [ "$1" ] || [ "$1" = mydefault ]; then
+# no toggle wg_master, I want wg_master to always on by default
+if ! [ "$1" ] || [ "$1" = master ]; then
wg_aa_restore_default
- down_not_mydefault
- nmcli connection up wg_mydefault
+ down_not_master
+ nmcli connection up wg_master
notify-send -u critical 'VPN disabled'
exit
fi
if nmcli -t --fields device,state device | grep -q "^wg_$1:connected$"; then
wg_aa_restore_default
- down_not_mydefault
- nmcli connection up wg_mydefault
+ down_not_master
+ nmcli connection up wg_master
notify-send -u critical "$1 VPN disabled"
else
if [ "$1" = aa ]; then
/usr/bin/alacritty -T aa --hold -e sh -c 'echo "Wait for aa sudo prompt and enter password, then manually close this window"; ssh -t aawg "sudo systemctl stop wg-quick@wg_ca && sudo systemctl start wg-quick@wg0"'
fi
- down_not_mydefault
- nmcli connection down wg_mydefault
+ down_not_master
+ nmcli connection down wg_master
nmcli connection up "wg_$1"
notify-send "$1 VPN enabled"
fi