This is an automated email from the git hooks/post-receive script.

x2go pushed a commit to branch master
in repository x2gobroker.

commit 447d72aed0e1fc124013e5277581d74912dd6ac7
Author: Stefan Baur <x2go-m...@baur-itcs.de>
Date:   Thu Mar 28 12:24:40 2024 +0000

    mini/x2gobroker-ssh-mini: add missing hostname/IP and port autodetection.
---
 debian/changelog         |  2 +
 mini/x2gobroker-ssh-mini | 96 ++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 90 insertions(+), 8 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index 3b07430..e0f2f7b 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -20,6 +20,8 @@ x2gobroker (0.0.4.4-0x2go1) UNRELEASED; urgency=medium
   [ Stefan Baur ]
   * New upstream version (0.0.4.4):
     - mini/x2gobroker-ssh-mini: typo fix referencing PORT variable.
+    - mini/x2gobroker-ssh-mini: add missing hostname/IP and port
+      autodetection.
 
  -- Mike Gabriel <sunwea...@debian.org>  Mon, 15 Nov 2021 15:33:29 +0100
 
diff --git a/mini/x2gobroker-ssh-mini b/mini/x2gobroker-ssh-mini
index a839393..7c2b5e8 100644
--- a/mini/x2gobroker-ssh-mini
+++ b/mini/x2gobroker-ssh-mini
@@ -127,20 +127,100 @@ if (echo -e "$PARAMLIST" | grep -q -- '--task 
listsessions'); then
 # check if we were asked to provide a server name/IP and port for a specific 
session
 elif (echo -e "$PARAMLIST" | grep -q -- '--task selectsession'); then
                SESSIONID=$(echo -e "$PARAMLIST" | awk '$1 == "--sid" { print 
$2 }')
+
                # search for the line with the corresponding session file in 
our stored list of files
                SESSIONFILE=$(grep "$SESSIONID" 
~/.x2go/brokersessionfile-${USER})
+
                # determine server name/IP and port from this file
                SERVER=$(awk -F '=' '$1 == "host" { print $2 }' $SESSIONFILE)
                PORT=$(awk -F '=' '$1 == "sshport" { print $2 }' $SESSIONFILE)
-               # if this failed, set default values
-               if [ -z "$SERVER" ] && [ -f 
/etc/x2go/x2gobroker-ssh-mini/defaulthost ]; then
-                       # determine default hostname/IP
-                       read DEFAULTHOST 
</etc/x2go/x2gobroker-ssh-mini/defaulthost
-                       SERVER=$DEFAULTHOST
-               fi
 
-               if [ -z "${PORT}" ]; then
-                       PORT=22
+               # if this failed for at least one of those two, we need to 
determine and set default values
+
+               if [ -z "${SERVER}" ] || [ -z "${PORT}" ]; then
+
+                       # let's check if we have a file for this
+                       if [ -f '/etc/x2go/x2gobroker-ssh-mini/defaulthost' ]; 
then
+
+                               # we have, so let's read it
+                               read DEFAULTFROMFILE 
<'/etc/x2go/x2gobroker-ssh-mini/defaulthost'
+
+                               # do a basic syntax check on the result to find 
out what we're dealing with
+                               if [ -z "${DEFAULTFROMFILE##*:*:*}" ] ; then
+
+                                       # two or more ":" -> IPv6 detected
+                                       if [ '[' = "${DEFAULTFROMFILE:0:1}" ] 
&& [ "${DEFAULTFROMFILE}" != "${DEFAULTFROMFILE%\]*}" ] ; then
+                                               # is likely a proper IPv6 
address (starts with square bracket, has a closing bracket somewhere)
+
+                                               # Determine Port - strip 
everything in front of "]:"
+                                               # If no port was given, the 
result will equal $DEFAULTFROMFILE
+                                               
DEFAULTPORTFROMFILE="${DEFAULTFROMFILE#*\]:}"
+                                               if [ "${DEFAULTPORTFROMFILE}" 
!= "${DEFAULTFROMFILE}" ]; then
+                                                       # not the same, so we 
have a valid port and may set it
+                                                       
PORT="${DEFAULTPORTFROMFILE}"
+                                               fi
+
+                                               # Determine IPv6 address 
(subtract colon and port, then remove brackets in step 2)
+                                               
DEFAULTFROMFILE="${DEFAULTFROMFILE%%:$DEFAULTPORT}"
+                                               
DEFAULTSERVERFROMFILE="${DEFAULTFROMFILE//[\[\]]}"
+
+                                               # do one last sanity check, in 
case someone messed up the square brackets in the file
+                                               if [ 
"[${DEFAULTSERVERFROMFILE}]" = "${DEFAULTFROMFILE}" ]; then
+                                                       # very likely that we 
have a valid IPv6 address, so let's use it
+                                                       
SERVER="${DEFAULTSERVERFROMFILE}"
+                                               fi
+                                       # else # TODO - add log message that 
file could not be parsed.
+                                       fi
+                               else
+                                       # one or no ":" -> IPv4 address or DNS 
name, with optional port
+
+                                       # Determine port - strip everything in 
front of ":"
+                                       # If no port was given, the result will 
equal $DEFAULTFROMFILE
+                                       
DEFAULTPORTFROMFILE="${DEFAULTFROMFILE#*:}"
+                                       if [ "${DEFAULTPORTFROMFILE}" != 
"${DEFAULTFROMFILE}" ]; then
+                                               # not the same, so we have a 
valid port and may set it
+                                               PORT="${DEFAULTPORTFROMFILE}"
+                                       fi
+
+                                       # not much to check for here
+                                       
DEFAULTSERVERFROMFILE="${DEFAULTHOST%:*}"
+                                       if [ -n "${DEFAULTSERVERFROMFILE}" ]; 
then
+                                               
SERVER="${DEFAULTSERVERFROMFILE}"
+                                       fi
+                               fi
+                       fi
+
+                       # If we have reached this point, and still lack at 
least one of the variables $SERVER and $PORT,
+                       # then either there was no default set in the file, or 
something went wrong parsing the file.
+                       # TODO: Add log message.
+
+                       # So if we still need a server name (or IP address), 
let's try what hostname -f has to say.
+                       if [ -z "$SERVER" ] ; then
+                               SERVERFQDNFROMHOSTNAME="$(hostname -f)"
+
+                               # let's check if the result contains at least 
one dot, so there's a chance it's an actual FQDN that our client knows
+                               # (there doesn't seem to be any use for short 
names - if we get a short name in reply, we're on a LAN, and can use the SSH IP 
address instead)
+                               if [ -z "${SERVERFQDNFROMHOSTNAME##*.*}" ]; then
+                                       # seems it is, so let's use it
+                                       SERVER="${SERVERFQDNFROMHOSTNAME}"
+                               fi
+                       fi
+
+                       # What, still no server name (or IP address), and/or no 
port?
+                       # Let's use what our SSH connection is using.  Note 
that this will fail when using NAT and "dialing in" from outside.
+                       SSHSERVERANDPORT="${SSH_CONNECTION#* * }"
+                       if [ -z "${SERVER}" ] ; then
+                               SERVER="${SSHSERVERANDPORT% *}"
+                       fi
+                       if [ -z "${PORT}" ]; then
+                               PORT="${SSHSERVERANDPORT#* }"
+                       fi
+
+                       # Last Resort - we should never reach this point
+
+                       if [ -z "$PORT" ]; then
+                               PORT='22'
+                       fi
                fi
 
                # output all data

--
Alioth's /home/x2go-admin/maintenancescripts/git/hooks/post-receive-email on 
/srv/git/code.x2go.org/x2gobroker.git
_______________________________________________
x2go-commits mailing list
x2go-commits@lists.x2go.org
https://lists.x2go.org/listinfo/x2go-commits

Reply via email to