From 397789cea60237f357476c86df96cd234e7b37eb Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sat, 17 Sep 2022 16:57:12 +0700 Subject: all: add script wg-activate.sh The wg-activate.sh is script that activate only one wireguard connection from list of configuration in /etc/wireguard/*.conf or turning off all of them if no parameter is given. --- AUR/.SRCINFO | 4 ++-- AUR/PKGBUILD | 4 ++-- Makefile | 17 +++++++++------ README | 5 +++++ bin/wg-activate.sh | 46 +++++++++++++++++++++++++++++++++++++++ etc/bash_completion.d/wg-activate | 24 ++++++++++++++++++++ 6 files changed, 89 insertions(+), 11 deletions(-) create mode 100755 bin/wg-activate.sh create mode 100644 etc/bash_completion.d/wg-activate diff --git a/AUR/.SRCINFO b/AUR/.SRCINFO index b2a2e1e..cffea12 100644 --- a/AUR/.SRCINFO +++ b/AUR/.SRCINFO @@ -1,13 +1,13 @@ pkgbase = bin.sh-git pkgdesc = A collection of shell scripts - pkgver = r1.94e5916 + pkgver = r2.23c8d78 pkgrel = 1 arch = any license = GPL3 makedepends = git provides = bin.sh conflicts = bin.sh - source = bin.sh::git+file:///home/ms/src/bin.sh + source = bin.sh::git+https://git.sr.ht/~shulhan/bin.sh md5sums = SKIP pkgname = bin.sh-git diff --git a/AUR/PKGBUILD b/AUR/PKGBUILD index 58d0edd..c21e338 100644 --- a/AUR/PKGBUILD +++ b/AUR/PKGBUILD @@ -1,6 +1,6 @@ # Maintainer: Shulhan pkgname=bin.sh-git -pkgver=r1.94e5916 +pkgver=r2.23c8d78 pkgrel=1 pkgdesc="A collection of shell scripts" arch=('any') @@ -30,7 +30,7 @@ pkgver() { package() { cd "$srcdir/${pkgname%-git}" - make DESTDIR="$pkgdir/" install + make DESTDIR="$pkgdir" install } ## SPDX-FileCopyrightText: 2022 M. Shulhan diff --git a/Makefile b/Makefile index 4a33a63..9be6e01 100644 --- a/Makefile +++ b/Makefile @@ -4,13 +4,16 @@ .PHONY: install uninstall install: - install -d $(DESTDIR)usr/bin - install bin/tmux-session.sh $(DESTDIR)usr/bin/ + install -d $(DESTDIR)/usr/bin + install bin/tmux-session.sh $(DESTDIR)/usr/bin/ + install bin/wg-activate.sh $(DESTDIR)/usr/bin/ - install -d $(DESTDIR)etc/bash_completion.d - install --mode=0644 etc/bash_completion.d/tmux-session \ - $(DESTDIR)/etc/bash_completion.d/ + install -d $(DESTDIR)/etc/bash_completion.d + install --mode=0644 etc/bash_completion.d/tmux-session $(DESTDIR)/etc/bash_completion.d/ + install --mode=0644 etc/bash_completion.d/wg-activate $(DESTDIR)/etc/bash_completion.d/ uninstall: - rm -f $(DESTDIR)etc/bash_completion.d/tmux-session - rm -f $(DESTDIR)bin/tmux-session.sh + rm -f $(DESTDIR)/etc/bash_completion.d/wg-activate + rm -f $(DESTDIR)/etc/bash_completion.d/tmux-session + rm -f $(DESTDIR)/usr/bin/wg-activate.sh + rm -f $(DESTDIR)/usr/bin/tmux-session.sh diff --git a/README b/README index b36e0f3..c5d348e 100644 --- a/README +++ b/README @@ -8,6 +8,11 @@ A collection of shell scripts. Script to open new tmux session with start directory based on configuration in `~/.tmux.session`. +*wg-activate.sh*:: +Script that activate only one wireguard connection from list of +configuration in /etc/wireguard/*.conf or turning off all of them if no +parameter is given. + == Installation diff --git a/bin/wg-activate.sh b/bin/wg-activate.sh new file mode 100755 index 0000000..90dcff3 --- /dev/null +++ b/bin/wg-activate.sh @@ -0,0 +1,46 @@ +#!/bin/sh +## SPDX-FileCopyrightText: 2022 M. Shulhan +## SPDX-License-Identifier: GPL-3.0-or-later + +## depends=wireguard-tools +## +## Script that activate only one wireguard connection from list of +## configuration in /etc/wireguard/*.conf or turning off all of them if no +## parameter is given. +## +## For example, given three configurations, +## +## $ ls /etc/wireguard +## wg0.conf wg1.conf wg2.conf +## +## Assume that current wg0 is activate, to activate wg1 and deactivate the +## rest, run +## +## $ sudo wg-activate.sh wg1 +## + +exp=$1 +confs=$(find -L /etc/wireguard -maxdepth 1 -type f -name "*.conf" -print) +activate= + +downs=() +for f in $confs; do + got=$(basename "${f%.conf}") + if [[ "$exp" == "$got" ]]; then + activate=$exp + else + downs+=("$got") + fi +done + +if [[ -z "$activate" ]]; then + echo "Turning off all connections." +fi + +for f in "${downs[@]}"; do + wg-quick down $f +done + +if [[ -n "$activate" ]]; then + wg-quick up $activate +fi diff --git a/etc/bash_completion.d/wg-activate b/etc/bash_completion.d/wg-activate new file mode 100644 index 0000000..a0f5486 --- /dev/null +++ b/etc/bash_completion.d/wg-activate @@ -0,0 +1,24 @@ +#/usr/bin/env bash +## SPDX-FileCopyrightText: 2022 M. Shulhan +## SPDX-License-Identifier: GPL-3.0-or-later + +_wg-activate_completions() +{ + confs=$(find -L /etc/wireguard -maxdepth 1 -type f -name "*.conf" -print) + + names=() + for f in $confs; do + got=$(basename "${f%.conf}") + names+=("$got") + done + + if [[ -z "${COMP_WORDS[1]}" ]]; then + COMPREPLY=("${names[@]}") + return + fi + + words="${names[@]}" + COMPREPLY=($(compgen -W "$words" -- "${COMP_WORDS[1]}")) +} + +complete -F _wg-activate_completions wg-activate.sh -- cgit v1.3