Wire-G/wire_g

271 lines
11 KiB
Plaintext
Raw Normal View History

2023-10-28 21:38:26 +02:00
#!/bin/bash
2023-11-01 18:05:46 +01:00
########################################################
########################################################
# 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
2023-11-01 18:05:46 +01:00
# i18n - Internationalization - Internationalisierung
2023-11-01 18:05:46 +01:00
2023-11-01 21:46:24 +01:00
export TEXTDOMAIN=wire-g
export TEXTDOMAINDIR="/usr/share/locale"
2023-11-01 18:05:46 +01:00
2023-10-28 21:38:26 +02:00
############################################################################################################
############################################################################################################
2023-11-10 16:41:54 +01:00
version=3.1.3
2023-10-28 21:38:26 +02:00
# 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
2023-11-01 13:10:57 +01:00
nmcli connection show | grep -iPo "(.*)(wireguard)" | sed 's/ .*//' > ${wg_wdir}.tunnel.txt
2023-10-28 21:38:26 +02:00
# 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/ .*//')
############################################################################################################
2023-11-08 09:03:02 +01:00
############################################################################################################
2023-10-28 21:38:26 +02:00
wg_notify(){
2023-11-01 07:57:13 +01:00
2023-10-28 21:38:26 +02:00
a_user=$(whoami)
active=$(nmcli connection show --active | grep -iPo "(.*)(wireguard)" | sed 's/ .*//')
wg_pic="/home/$a_user/.icons/"
2023-11-08 19:49:09 +01:00
case $LANG in
2023-11-10 23:07:50 +01:00
de_DE.UTF-8) open=öffnen ;;
2023-11-08 19:49:09 +01:00
2023-11-10 23:07:50 +01:00
*) open=open ;;
2023-11-08 19:49:09 +01:00
esac
2023-11-02 20:58:08 +01:00
notify-send --icon=${wg_pic}wg-vpn.png $"$active powered"
2023-10-28 21:38:26 +02:00
# autoconnect wird hier abgeschalten damit auch neu importierte nach einem Systemneustart nicht aktiv sind
2023-11-07 19:55:03 +01:00
nmcli con mod $active connection.autoconnect no
2023-11-10 22:50:33 +01:00
yad --notification \
2023-11-10 23:00:59 +01:00
--image="${wg_pic}wg-vpn.png" \
2023-11-10 22:50:33 +01:00
--icon-size=32 --no-middle \
2023-11-10 23:07:50 +01:00
--text=$"$active activ" \
--menu="Wire-G $open!/sbin/wire_g
2023-11-10 23:00:59 +01:00
|$active stop!/sbin/wg_stop" \
2023-11-10 22:50:33 +01:00
--command="menu"
2023-11-11 19:05:06 +01:00
2023-10-28 21:38:26 +02:00
}
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/"
2023-11-08 17:53:22 +01:00
if wgconf=$(yad --file --separator=" \n" \
2023-11-02 21:22:08 +01:00
--button="OK" --button=$"Cancel" \
2023-11-09 20:23:46 +01:00
--width=1200 --height=800 --no-klick \
2023-11-01 13:10:57 +01:00
--window-icon=${wg_pic}wg-import.png \
2023-11-01 17:41:32 +01:00
--title=$"Wireguard .conf Select file")
2023-11-09 16:53:37 +01:00
2023-11-08 17:53:22 +01:00
then
case "$wgconf" in
2023-11-08 17:05:12 +01:00
2023-11-08 17:53:22 +01:00
*_*.conf) if grep -i 'PEER\|PublicKey' $wgconf > /dev/null && grep -i 'Interface\|PrivateKey' $wgconf > /dev/null
2023-11-09 17:14:02 +01:00
then
2023-11-08 17:53:22 +01:00
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
2023-11-11 20:15:10 +01:00
wg_notify
2023-11-09 16:53:37 +01:00
else
2023-11-11 21:02:41 +01:00
pkill yad
2023-11-08 17:53:22 +01:00
nmcli connection import type wireguard file $wgconf &> ${wg_wdir}.tmp.txt
wg_notify
fi
2023-11-11 20:15:10 +01:00
wire_g
2023-11-08 17:53:22 +01:00
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 \
2023-11-08 20:23:22 +01:00
--text $"<b>Oh, something went wrong.</b> \n
2023-11-08 09:11:31 +01:00
<b>No valid Wireguard file.</b> \n
2023-11-08 17:53:22 +01:00
<b>Here is an example:</b><span color='#0fad0a'><b> my_wireguard.conf</b></span> \n"
import_wg ;;
2023-11-10 20:48:17 +01:00
esac
fi }
2023-10-28 21:38:26 +02:00
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/ .*//')
2023-11-01 13:10:57 +01:00
nmcli connection show --active | grep -iPo "(.*)(wireguard)" | sed 's/ .*//' &> ${wg_wdir}.wg_noactive.txt
active=$(cat ${wg_wdir}.wg_noactive.txt)
2023-10-28 21:38:26 +02:00
2023-11-10 22:20:33 +01:00
if selection=$(yad --no-click --list --center \
--window-icon=${wg_pic}wg-trash.png \
--height=200 --width=60 --undecorated \
--buttons-layout=center --separator="" \
--button=$"OK" --button=$"Cancel" \
--borders=8 \
--column=$"Delete" $tunnel)
then
2023-11-07 19:55:03 +01:00
nmcli connection delete $selection &> ${wg_wdir}.tmp.txt
2023-11-07 19:47:05 +01:00
if success=$(grep -i 'erfolgreich gelöscht\|successfully deleted' ${wg_wdir}.tmp.txt)
2023-11-11 20:15:10 +01:00
then
2023-11-01 13:10:57 +01:00
yad --window-icon=${wg_pic}wg-trash.png \
--image-on-top --image=${wg_pic}wg-info.png \
2023-11-07 19:55:03 +01:00
--text=$"Your tunnel $selection was successfully deleted." \
2023-11-01 17:41:32 +01:00
--text-align=center --button=$"OK" --title "Wire-G Trash" \
2023-11-14 12:18:06 +01:00
--center --buttons-layout=center --borders=8
2023-11-14 12:26:22 +01:00
killall wire_g
2023-11-14 12:18:06 +01:00
wire_g
else
2023-11-01 13:10:57 +01:00
yad --window-icon=${wg_pic}wg-trash.png \
--image-on-top --image=${wg_pic}wg-info.png \
2023-11-01 17:41:32 +01:00
--text=$"Oh something went wrong.\nPlease delete tunnel in the network manager." \
--text-align=center --button=$"OK" --title "Wire-G Trash" \
2023-11-11 20:15:10 +01:00
--center --buttons-layout=center --borders=8
fi
2023-11-14 12:12:37 +01:00
2023-11-11 19:21:03 +01:00
if [ "$selection" = "$active" ]
then
pkill wire_g
pkill yad
2023-11-11 19:21:03 +01:00
fi
2023-11-10 22:20:33 +01:00
fi }
2023-11-14 12:31:52 +01:00
2023-11-10 22:20:33 +01:00
2023-10-28 21:38:26 +02:00
# function end
############################################################################################################
############################################################################################################
#funktion for active tunnel a stop
2023-11-07 20:37:53 +01:00
up_or_down(){
2023-11-01 13:21:30 +01:00
a_user=$(whoami)
wg_pic="/home/$a_user/.icons/"
active=$(nmcli connection show --active | grep -iPo "(.*)(wireguard)" | sed 's/ .*//')
2023-11-01 13:10:57 +01:00
if [[ -z $active ]]
then
2023-11-01 13:10:57 +01:00
yad --image-on-top --image=${wg_pic}wg-info.png \
2023-11-02 20:58:08 +01:00
--text=$"There is no active tunnel that you could stop." \
2023-11-01 17:41:32 +01:00
--text-align=center --button=$"OK" --title "Wire-G" \
2023-11-09 20:41:36 +01:00
--center --buttons-layout=center --borders=8
else
wg_stop
wire_g
fi }
2023-11-01 10:11:42 +01:00
export -f up_or_down
# funktion end
############################################################################################################
############################################################################################################
# function button "more"
about(){
a_user=$(whoami)
wg_pic="/home/$a_user/.icons/"
2023-11-10 16:41:54 +01:00
version=3.1.3
2023-11-11 21:02:41 +01:00
yad --title="Wire-G" --center \
2023-11-09 20:38:28 +01:00
--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
2023-11-07 19:55:03 +01:00
<span color='#626ff1'><b>Version: $version </b></span> \n
<span color='#626ff1'><b>Use without warranty.</b></span> \n
2023-11-11 21:02:41 +01:00
<a href='https://git.lunix.dedyn.io/punix/Wire-G'>Download Wire-G </a> \n"
}
export -f about
2023-11-01 07:57:13 +01:00
# function end
############################################################################################################
############################################################################################################
2023-10-28 21:38:26 +02:00
# funktion wireguard enable select Tunnel
connect(){
2023-11-01 16:16:38 +01:00
if success=$(grep -i 'erfolgreich aktiviert\|successfully activated' ${wg_wdir}.tmp.txt)
2023-10-28 21:38:26 +02:00
then
2023-11-11 21:02:41 +01:00
pkill yad
2023-11-07 19:55:03 +01:00
nmcli con mod $selection connection.autoconnect no
2023-11-11 19:51:44 +01:00
wg_notify
wire_g
fi }
2023-10-28 21:38:26 +02:00
# function end
############################################################################################################
############################################################################################################ # --text "<b>Aktiver Tunnel:</b> <span color='#0fad0a'><b>$active</b></span> # mit <b>gewünschtes wort</b> wird fett geschrieben.
2023-11-01 07:57:13 +01:00
# Farbe für Variable $active festgelegt mit fetter Schrift. (bei --text zwischen " ")
2023-10-28 21:38:26 +02:00
2023-11-10 21:05:43 +01:00
if selection=$(yad --no-klick --list --center \
--height=300 --width=60 --title="Wire-G" \
--buttons-layout=center \
2023-11-10 22:20:33 +01:00
--button=$"Start:0" --button=$"Stop":"bash -c up_or_down" \
2023-11-10 21:05:43 +01:00
--button=$"Import":"bash -c import_wg" \
2023-11-14 12:31:52 +01:00
--button=$"Remove:4" --separator="" \
2023-11-10 21:05:43 +01:00
--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>
2023-10-28 21:38:26 +02:00
<b>----------------------------------------------------------------------------------</b>
2023-11-01 07:57:13 +01:00
<b>If no tunnels are listed,</b>
2023-11-11 19:23:52 +01:00
<b>so they have to import their tunnel beforehand.</b>")
2023-11-11 21:02:41 +01:00
then
2023-11-07 19:55:03 +01:00
if [[ -n $active ]]
then
wg_stop
nmcli connection up $selection &> ${wg_wdir}.tmp.txt
connect
else
2023-11-07 19:55:03 +01:00
nmcli connection up $selection &> ${wg_wdir}.tmp.txt
connect
fi
fi
2023-11-14 12:31:52 +01:00
ret=$?
[[ $? -eq 1 ]] && exit 0
if [[ $ret -eq 4 ]]
then
remove
fi
if [[ $ret -eq 5 ]]
then
about
fi
2023-10-28 21:38:26 +02:00
2023-11-01 07:57:13 +01:00
############################################ END ##########################################################
2023-10-28 21:38:26 +02:00
2023-11-01 13:10:57 +01:00