Wire-G/wire_g

265 lines
12 KiB
Bash
Executable File

#!/bin/bash
########################################################
########################################################
# Wire-G App for simple management a Wireguard Tunnel ##
# Author: Désiré Werner Menrath ##
# Email: polunga40@unity-mail.de ##
# Translate German to English with LibreTranslate ##
# Translatefiles edit with Poedit ##
# Use without warranty! ##
########################################################
########################################################
Encoding=UTF-8
# i18n - Internationalization - Internationalisierung
export TEXTDOMAIN=wire-g
export TEXTDOMAINDIR="/usr/share/locale"
############################################################################################################
############################################################################################################
version=3.1.2
# Angemeldeten Benutzer ermitteln
a_user=$(whoami)
# wg_workdir Arbeitsverzeichnis wg
wg_wdir="/home/$a_user/.config/wg_nmcli/"
# Pfad wg Icons
wg_pic="/home/$a_user/.icons/"
# Wireguard tunnel auslesen mit grep nur Wireguard zeigen
# mit sed alles nach dem ersten Leerzeichen bis Zeilenende weggeschneiden und .tunnel.txt schreiben
nmcli connection show | grep -iPo "(.*)(wireguard)" | sed 's/ .*//' > ${wg_wdir}.tunnel.txt
# Variable der verfügbaren Tunnelanzeige für Yad
tunnel=$(nmcli connection show | grep -iPo "(.*)(wireguard)" | sed 's/ .*//')
# Variable der aktiven Tunnelanzeige für Yad
active=$(nmcli connection show --active | grep -iPo "(.*)(wireguard)" | sed 's/ .*//')
############################################################################################################
############################################################################################################
wg_notify(){
a_user=$(whoami)
active=$(nmcli connection show --active | grep -iPo "(.*)(wireguard)" | sed 's/ .*//')
wg_pic="/home/$a_user/.icons/"
case $LANG in
de_DE.UTF-8) show=aktiv & open=öffnen ;;
*) show=activ & open=open ;;
esac
notify-send --icon=${wg_pic}wg-vpn.png $"$active powered"
# autoconnect wird hier abgeschalten damit auch neu importierte nach einem Systemneustart nicht aktiv sind
nmcli con mod $active connection.autoconnect no
# ab hier beginnt yad notify
#Pipe erstellen
PIPE="$HOME/.pipe.tmp"
rm $PIPE > /dev/null 2>&1
mkfifo $PIPE
exec 3<> $PIPE
#Yad Dialog erstellen
yad --notification --listen --command= --icon-size=32 --no-middle <&3 &
#Menüeinträge definieren
echo "menu:\
Wire-G open!/sbin/wire_g|
$active stop!/sbin/wg_stop| " >&3
#Icon des Menübuttons definieren
echo "icon:${wg_pic}wg-vpn.png" >&3
#Name des Menüs definieren
echo "tooltip:$active $show" >&3
}
export -f wg_notify
# function end
############################################################################################################
############################################################################################################
# funktion wireguard import
import_wg(){
a_user=$(whoami)
wg_pic="/home/$a_user/.icons/"
wg_wdir="/home/$a_user/.config/wg_nmcli/"
if wgconf=$(yad --file --separator=" \n" \
--button="OK" --button=$"Cancel" \
--width=1200 --height=800 --no-klick \
--window-icon=${wg_pic}wg-import.png \
--title=$"Wireguard .conf Select file")
then
case "$wgconf" in
*_*.conf) if grep -i 'PEER\|PublicKey' $wgconf > /dev/null && grep -i 'Interface\|PrivateKey' $wgconf > /dev/null
then
active=$(nmcli connection show --active | grep -iPo "(.*)(wireguard)" | sed 's/ .*//')
if [[ -n $active ]]
then
wg_stop
nmcli connection import type wireguard file $wgconf &> ${wg_wdir}.tmp.txt
wg_notify
else
nmcli connection import type wireguard file $wgconf &> ${wg_wdir}.tmp.txt
wg_notify
fi
fi ;;
*) yad --button=$"OK" --title=$"Wireguard .conf Select file" \
--window-icon=${wg_pic}wg-stop.png --image=${wg_pic}wg-info.png \
--center --buttons-layout=center --borders=8 --width=350 --height=60 \
--text $"<b>Oh, something went wrong.</b> \n
<b>No valid Wireguard file.</b> \n
<b>Here is an example:</b><span color='#0fad0a'><b> my_wireguard.conf</b></span> \n"
import_wg ;;
esac
fi
}
export -f import_wg
# function end
############################################################################################################
############################################################################################################
# funktion wireguard remove
remove(){
a_user=$(whoami)
wg_pic="/home/$a_user/.icons/"
wg_wdir="/home/$a_user/.config/wg_nmcli/"
tunnel=$(nmcli connection show | grep -iPo "(.*)(wireguard)" | sed 's/ .*//')
nmcli connection show --active | grep -iPo "(.*)(wireguard)" | sed 's/ .*//' &> ${wg_wdir}.wg_noactive.txt
active=$(cat ${wg_wdir}.wg_noactive.txt)
if selection=$(yad --no-click --list --center \
--window-icon=${wg_pic}wg-trash.png \
--height=300 --width=60 --title="Wire-G Trash" \
--buttons-layout=center --separator="" \
--button=$"OK" --button=$"Cancel" \
--borders=8 --image-on-top --image=${wg_pic}wg-active.png \
--column=$"Delete" $tunnel \
--text=$"<b>Activ Tunnel: </b> <span color='#0fad0a'><b>$active</b></span>
<b>----------------------------------------------------------------------------------</b>
<b>If no tunnels are listed,</b>
<b>so they have to import their tunnel beforehand.</b>")
then
nmcli connection delete $selection &> ${wg_wdir}.tmp.txt
if success=$(grep -i 'erfolgreich gelöscht\|successfully deleted' ${wg_wdir}.tmp.txt)
then
yad --window-icon=${wg_pic}wg-trash.png \
--image-on-top --image=${wg_pic}wg-info.png \
--text=$"Your tunnel $selection was successfully deleted." \
--text-align=center --button=$"OK" --title "Wire-G Trash" \
--center --buttons-layout=center --borders=8
else
yad --window-icon=${wg_pic}wg-trash.png \
--image-on-top --image=${wg_pic}wg-info.png \
--text=$"Oh something went wrong.\nPlease delete tunnel in the network manager." \
--text-align=center --button=$"OK" --title "Wire-G Trash" \
--center --buttons-layout=center --borders=8
fi
if [ "$selection" = "$active" ]
then
pkill wire_g
pkill yad
fi
fi }
export -f remove
# function end
############################################################################################################
############################################################################################################
#funktion for active tunnel a stop
up_or_down(){
a_user=$(whoami)
wg_pic="/home/$a_user/.icons/"
active=$(nmcli connection show --active | grep -iPo "(.*)(wireguard)" | sed 's/ .*//')
if [[ -z $active ]]
then
yad --image-on-top --image=${wg_pic}wg-info.png \
--text=$"There is no active tunnel that you could stop." \
--text-align=center --button=$"OK" --title "Wire-G" \
--center --buttons-layout=center --borders=8
else
wg_stop
fi }
export -f up_or_down
# funktion end
############################################################################################################
############################################################################################################
# function button "more"
about(){
a_user=$(whoami)
wg_pic="/home/$a_user/.icons/"
version=3.1.2
selection=$(yad --title="Wire-G" --center \
--borders=8 --close-on-unfocus \
--buttons-layout=center --text-align=center \
--button=$"OK" --fixed \
--separator="" --window-icon=${wg_pic}wg-vpn.png \
--text=$"<span color='#626ff1'><b>Wire-G Author: Désiré Werner Menrath</b></span> \n
<span color='#626ff1'><b>E-Mail: polunga40@unity-mail.de</b></span> \n
<span color='#626ff1'><b>At all tunnels, the auto start is disabled.</b></span> \n
<span color='#626ff1'><b>Version: $version </b></span> \n
<span color='#626ff1'><b>Use without warranty.</b></span> \n
<a href='https://git.lunix.dedyn.io/punix/Wire-G'>Download Wire-G </a> \n")
}
export -f about
# function end
############################################################################################################
############################################################################################################
# funktion wireguard enable select Tunnel
connect(){
if success=$(grep -i 'erfolgreich aktiviert\|successfully activated' ${wg_wdir}.tmp.txt)
then
nmcli con mod $selection connection.autoconnect no
wg_notify
fi }
# function end
############################################################################################################
############################################################################################################ # --text "<b>Aktiver Tunnel:</b> <span color='#0fad0a'><b>$active</b></span> # mit <b>gewünschtes wort</b> wird fett geschrieben.
# Farbe für Variable $active festgelegt mit fetter Schrift. (bei --text zwischen " ")
if selection=$(yad --no-click --list \
--center \
--height=300 --width=60 --title="Wire-G" \
--buttons-layout=center \
--button=$"Start:0" --button=$"Stop":"bash -c up_or_down" \
--button=$"Import":"bash -c wg_import" --button=$"Remove":"bash -c remove" --separator="" \
--button=$"About":"bash -c about"
--window-icon=${wg_pic}wg-vpn.png \
--borders=8 --image-on-top --image=${wg_pic}wg-active.png \
--column=$"Selection" $tunnel \
--text=$"<b>Activ Tunnel: </b> <span color='#0fad0a'><b>$active</b></span>
<b>----------------------------------------------------------------------------------</b>
<b>If no tunnels are listed,</b>
<b>so they have to import their tunnel beforehand.</b>")
then
if [[ -n $active ]]
then
wg_stop
nmcli connection up $selection &> ${wg_wdir}.tmp.txt
connect
else
nmcli connection up $selection &> ${wg_wdir}.tmp.txt
connect
fi
fi
############################################ END ##########################################################