diff --git a/bibVPN.sh b/bibVPN.sh index 4581ba3..0833631 100755 --- a/bibVPN.sh +++ b/bibVPN.sh @@ -23,7 +23,7 @@ ################### #### Variables #### ################### -VERSION=1.3.1 +VERSION='1.4.5' WORKDIR="/Users/$USER/.openconnect" mkdir -p $WORKDIR logfile="$WORKDIR/bibVPN.log" @@ -32,17 +32,30 @@ echo "Run $0 $@" >> $logfile PATH=$PATH:/usr/local/bin VPN_EXECUTABLE=$(which openconnect) OC_PIDFILE="$WORKDIR/vpn.bib.de.pid" -SETTINGSFILE="$WORKDIR/settings.csv" +SETTINGSFILE="$WORKDIR/settings.txt" ACCOUNTFILE="$WORKDIR/accounts.csv" VPN_USERNAME="$2" VPN_HOST="$3" #### Settings #### -SHOW_SETTINGS='ON' -SHOW_ICONS='ON' +SHOW_SETTINGS='true' +SHOW_ICON='true' +SHOW_COLOR='true' +DEBUG_OUTPUT='false' +if [ -f $SETTINGSFILE ] ; then + echo "Lade die Einstellungsdatei" >> $logfile + source $SETTINGSFILE +else + saveSettings +fi +if [ $SHOW_COLOR == 'true' ] ; then + COLOR_CONNECTED='green' + COLOR_UNCONNECTED='crimson' +else + COLOR_CONNECTED='none' + COLOR_UNCONNECTED='none' +fi NET_FILTER='inet 172.[123][0-9].1[67][80].' -FONT=( 'size=14' 'font=UbuntuMono' ) - # A command that will result in your VPN password. GET_VPN_PASSWORD="security find-generic-password -g -a $VPN_USERNAME 2>&1 >/dev/null | cut -d'\"' -f2" @@ -57,9 +70,21 @@ IP=$(ifconfig | egrep -A1 "$NET_FILTER" |cut -d' ' -f2) ################### #### Functions #### ################### +function saveSettings(){ + echo "Speichere die Einstellungen" >> $logfile + echo "SHOW_SETTINGS='$SHOW_SETTINGS' +SHOW_ICON='$SHOW_ICON' +SHOW_COLOR='$SHOW_COLOR' +DEBUG_OUTPUT=$DEBUG_OUTPUT" |tee $SETTINGSFILE + + echo "Lade die Einstellungsdatei" >> $logfile + source $SETTINGSFILE + open swiftbar://refreshplugin?name=$(basename $0) +} + function askFor(){ osascript="$1 buttons {\"Cancel\",\"OK\"} default button {\"OK\"} cancel button \"Cancel\" with title \"Neuen User für das VPN anlegen\" with icon caution" - #echo $osascript + #üecho $osascript >> $logfile results=$( /usr/bin/osascript -e "$osascript") if [[ $? != 0 ]]; then clmsg='display notification "" with title "OpenFortiVPN" subtitle "Beende mich" sound name "Submarine"' @@ -71,7 +96,11 @@ function askFor(){ theText=$( echo "$results" | /usr/bin/awk -F "text returned:" '{print $2}' ) if [[ $theButton == "OK" ]] ;then - echo $theText; + if [ -z $theText ];then + echo "OK" + else + echo $theText + fi elif [[ $theButton == "Cancel" ]] ; then clmsg='display notification "" with title "OpenFortiVPN" subtitle "Beende mich" sound name "Submarine"' osascript -e "$clmsg" @@ -81,17 +110,26 @@ function askFor(){ } function showSettings() -{ - if [[ $SHOW_SETTINGS == "ON" ]]; then +{ + echo '---' + echo "Neues **VPN** Profil anlegen | shell='$0' param1=newuser terminal=false refresh=true md=true" + if [[ $SHOW_SETTINGS == "true" ]]; then echo "---" echo "Settings" - echo "--Farbige Icons aus (not ready) | tooltip='Vielleicht klappt es irgendwann :)' checked=false" - echo "--Neues VPN Profil anlegen | shell='$0' param1=newuser terminal=false refresh=true" + echo "--Icons verwenden | shell='$0' terminal=false param1=toggleIcon tooltip='Icon im Menü anzeigen oder Text' checked=$SHOW_ICON" + echo "--Farbiges Icon/Schrift | shell='$0' terminal=false param1=toggleColor tooltip='Icon oder Schrift einfärben oder eintönig lassen' checked=$SHOW_COLOR" echo "---" echo "-- --- INFOS ---" - echo "--Logfile: $logfile | shell='$0' param1=openlog terminal=false refresh=true tooltip='Das Logfile öffnen'" + echo "--**Logfile:** *${logfile}* | shell='$0' param1=openlog terminal=false refresh=true tooltip='Das Logfile öffnen' md=true" echo "--${SETTINGSFILE} | shell='$0' param1=openDIR terminal=false refresh=true tooltip='Ordner öffnen'" echo "--Script: $(basename -- "$0") Version: **v$VERSION** | href="https://git.bib.de/itssct/OpenFortiVPN_macOS" terminal=false refresh=true tooltip='Die Projektwebsite im Browser öffnen' md=true" + if [[ $DEBUG_OUTPUT == 'true' ]] ; then + echo "-- --- DEBUG ---" + echo "--SHOW_ICON: $SHOW_ICON" + echo "--SHOW_COLOR: $SHOW_COLOR" + echo "--COLOR_CONNECTED: $COLOR_CONNECTED" + echo "--COLOR_UNCONNECTED: $COLOR_UNCONNECTED" + fi fi } ################### @@ -99,27 +137,31 @@ function showSettings() ################### case "$1" in connect) - echo "$GET_VPN_PASSWORD" >> $logfile 2>&1 + echo "$GET_VPN_PASSWORD" >> $logfile VPN_PASSWORD=$(eval "$GET_VPN_PASSWORD") # Passwort checken if [[ $VPN_PASSWORD =~ 'item could not be found in the keychain.' ]] ; then - echo "Es konnte kein Passwort für $VPN_USERNAME gefunden werden" >> $logfile 2>&1 + echo "Es konnte kein Passwort für $VPN_USERNAME gefunden werden" >> $logfile #errmsg_noPass='display notification "Es konnte kein Passwort für '$VPN_USERNAME' gefunden werden" with title "OpenFortiVPN" subtitle "Verbindungsversuch nicht erfolgreich" sound name "Brise"' #osascript -e "$errmsg_noPass" NEW_VPN_PASSWORD=$(askFor 'display dialog "Es ist kein Passwort im Schlüsselbund vorhanden. Wie lautet das Passwort für '$VPN_USERNAME'? " default answer "Mein Passwort..." with hidden answer') - echo $VPN_PASSWORD + if [[ $? != 0 ]]; then exit 2 ; fi # Beim Abbruch der Maske, gesamte Script beenden. - echo "security add-generic-password -a $VPN_USERNAME -s openconnect -w '$VPN_PASSWORD'" >> $logfile 2>&1 - security add-generic-password -a $VPN_USERNAME -s "openconnect" -T /usr/bin/security -w "$NEW_VPN_PASSWORD" + echo $VPN_PASSWORD VPN_PASSWORD=$NEW_VPN_PASSWORD + echo "security add-generic-password -a $VPN_USERNAME -s openconnect -w '$VPN_PASSWORD'" >> $logfile + SAVE_PASSWORD=$(askFor 'display dialog "Soll das Passwort, dauerhaft im Schlüsselbund gespeichert werden? "') + if [[ $SAVE_PASSWORD == "OK" ]]; then + security add-generic-password -a $VPN_USERNAME -s openconnect -T /usr/bin/security -w "$NEW_VPN_PASSWORD" + fi #else - #echo $VPN_PASSWORD >> $logfile 2>&1 + #echo "VPN PASSWORT: $VPN_PASSWORD" >> $logfile fi VPN_EXECUTABLE_PARAMS="--passwd-on-stdin --servercert $4 --protocol=fortinet --pid-file=$OC_PIDFILE --background" # Optional # Verbindung aufbauen - echo "$VPN_PASSWORD | sudo $VPN_EXECUTABLE $VPN_EXECUTABLE_PARAMS --user $VPN_USERNAME $VPN_HOST" >> $logfile 2>&1 + echo "| sudo $VPN_EXECUTABLE $VPN_EXECUTABLE_PARAMS --user $VPN_USERNAME $VPN_HOST" >> $logfile 2>&1 echo "$VPN_PASSWORD" | sudo $VPN_EXECUTABLE $VPN_EXECUTABLE_PARAMS --user $VPN_USERNAME $VPN_HOST>> $logfile 2>&1 if [[ $? != 0 ]]; then osascript -e 'display altert "Verbindungsaufbau Fehlgeschlagen. Bitte im Log prüfen, was die Ursache ist."' @@ -128,8 +170,14 @@ case "$1" in ii=0 while [ -z $IP ] && [[ $ii != 20 ]]; do echo "noch keine IP" ; sleep 0.5 ; IP=$(ifconfig | egrep -A1 "$NET_FILTER"|cut -d' ' -f2) ; ((ii++)) ; done msg='display notification "Erfolgreich verbunden \nConnected User: '$VPN_USERNAME'" with title "OpenFortiVPN" subtitle "Deine IP lautet: '$IP'" sound name "Brise"' - errmsg='display notification "Verbindungsversuch nicht erfolgreich" with title "OpenFortiVPN" subtitle "Schade" sound name "Brise"' - if [[ $IP =~ 172 ]] ; then osascript -e "$msg" ; else osascript -e "$errmsg" ; exit 5 ; fi + errmsg='display notification "Verbindungsversuch nicht erfolgreich. Vermutlich ist das Passwort falsch." with title "OpenFortiVPN" subtitle "Schade" sound name "Brise"' + if [[ $IP =~ 172 ]] ; then + osascript -e "$msg" ; + else + osascript -e "$errmsg" + echo "Vermutlich ist das Passwort falsch. Es konnte keine Verbindung hergestellt werden." >> $logfile + exit 5 ; + fi # Wait for connection so menu item refreshes instantly until eval "$VPN_CONNECTED"; do sleep 1; done ;; @@ -156,11 +204,15 @@ case "$1" in else echo -n "Es ist noch kein Passwort im Schlüsselbund hinterlegt. Bitte einmal das Passwort angeben : " ; NEW_VPN_PASSWORD=$(askFor 'display dialog "Es ist kein Passwort im Schlüsselbund vorhanden. Wie lautet das Passwort? " default answer "Mein Passwort..." with hidden answer') - security add-generic-password -a $NEW_VPN_USERNAME -s openconnect -w $NEW_VPN_PASSWORD + SAVE_PASSWORD=$(askFor 'display dialog "Soll das Passwort, dauerhaft im Schlüsselbund gespeichert werden? "') + if [[ $SAVE_PASSWORD == "OK" ]] ; then + security add-generic-password -a $NEW_VPN_USERNAME -s openconnect -w $NEW_VPN_PASSWORD + fi fi echo "$NEW_VPN_NAME,$NEW_VPN_USERNAME,$NEW_VPN_HOST,$NEW_VPN_PUBKEY" >> $logfile >> $ACCOUNTFILE # Refreshen, damit das Profil direkt angezeigt wird. - swiftbar://refreshallplugins + open swiftbar://refreshplugin?name=$(basename $0) + osascript -e 'display notification "VPN Profil '$NEW_VPN_NAME' erfolgreich angelegt" with title "OpenVPN" subtitle "'$NEW_VPN_HOST' - '$NEW_VPN_USERNAME'" sound name "Brise"' ;; openlog) open -a console $logfile @@ -168,10 +220,20 @@ case "$1" in openDIR) open ${WORKDIR} ;; + toggleColor) + if [ $SHOW_COLOR == 'false' ];then SHOW_COLOR='true';else SHOW_COLOR='false' ; fi + echo "Switche den Farbmodus ($SHOW_COLOR)" >> $logfile + saveSettings + ;; + toggleIcon) + if [ $SHOW_ICON == 'false' ];then SHOW_ICON='true';else SHOW_ICON='false' ; fi + echo "Switche den Iconmodus ($SHOW_ICON)" >> $logfile + saveSettings + ;; esac if [ -f $OC_PIDFILE ]; then - if [ $SHOW_ICONS == 'ON' ]; then echo "| sfimage=firewall.fill | sfcolor=green" ; else echo "VPN ✔ |$FONT color=green" ; fi + if [ $SHOW_ICON == 'true' ]; then echo ":firewall.fill:| sfcolor=$COLOR_CONNECTED sfsize=17" ; else echo "VPN ✔ | color=$COLOR_CONNECTED" ; fi echo '---' echo "Disconnect VPN | bash='$0' param1=disconnect terminal=false refresh=true" aUser=$(ps -ef | grep -e '--user\ ' | cut -d' ' -f 32) @@ -181,7 +243,7 @@ if [ -f $OC_PIDFILE ]; then showSettings exit else - if [ $SHOW_ICONS == 'ON' ] ; then echo "| sfimage=firewall" ; else echo "VPN ❌ | $FONT color=Crimson" ; fi + if [ $SHOW_ICON == 'true' ] ; then echo ":firewall:| sfcolor=$COLOR_UNCONNECTED sfsize=17" ; else echo "VPN X | color=$COLOR_UNCONNECTED" ; fi echo '---' # Alle User aus der accounts.csv auslesen und dann zur Auswahl anbieten. cat "$ACCOUNTFILE" | while IFS= read config;