82352cc7a699077a2d39139b276f1de28e9d9336
[feisty_meow.git] / scripts / security / screened_tunneler.sh
1 #!/bin/bash
2 # this script manages one ssh tunnel inside a named 'screen' session.
3
4 ##############
5
6 # this section scavenges and documents the command line parameters...
7
8 # set this to the user and hostname that serve the tunnel on the remote
9 # side.  for example: geoffrey@chaucertales.com
10 TUNNEL_USER_PLUS_HOST="$1"; shift
11
12 # a tunnel command for ssh that gives us a link between here and there.
13 # this should be in the form:
14 #   "-L ${sourcePort}:${tunnelHost}:${destinationPort}"
15 # such as this example that connects a local port 12000 to the remote port
16 # of 25 in order to create a tunnel for smtp traffic:
17 #   "-L 12000:localhost:25"
18 # other ssh commands can be used here if they are properly formatted.
19 TUNNEL_LINK="$1"; shift
20
21 # this variable should be set to the name for the tunnel.  one can then
22 # open the tunnel screen with: screen -r -S "name"
23 TUNNEL_SCREEN_NAME="$1"; shift
24
25 # set this to your key file, plus the -i flag, such as: 
26 #   SECURITY_KEY="-i $HOME/.ssh/id_rsa" 
27 # if you do not have a special one set up or the default is fine, then just
28 # pass a blank parameter (e.g. "").
29 TUNNEL_SECURITY_KEY="$1"; shift
30
31 # if this is set, it means we're through the script the second time, inside
32 # a screen session, and we need to actually do the work now.
33 LAUNCH_IT="$1"; shift
34
35 ##############
36
37 function print_instructions()
38 {
39   echo "\
40 $(basename $0): This script requires at least three parameters and can\n\
41 take up to five.  The parameters are (1) tunnel user at hostname, (2) ssh tunnel\n\
42 link command, (3) tunnel screen name, (4) tunnel security key, (5) the launch\n\
43 command 'go'.  An example command is shown below, but many more details are\n\
44 explained inside this script:\n\
45   $(basename $0) "geoffrey@chaucertales.com" "-L 12000:localhost:25" \\\n\
46 "tunz" "-i mykey.pem"\n\
47 The fifth flag is really only needed internally, but often the other four\n\
48 parameters are specified."
49 }
50
51 ##############
52
53 # make sure the required parameters are provided.
54 if [ -z "$TUNNEL_USER_PLUS_HOST" -o -z "$TUNNEL_LINK" -o -z "$TUNNEL_SCREEN_NAME" ]; then
55   print_instructions
56   exit 1
57 fi
58
59 ##############
60
61 # translate command line parameters if desired.
62 LAUNCHING_TUNNEL=0
63 if [ "$LAUNCH_IT" == "go" ]; then
64   LAUNCHING_TUNNEL=1
65 fi
66
67 ##############
68
69 #hmmm: these variables should be configurable from plug-ins someday.
70
71 TUNNEL_ALERT_SOUND=$FEISTY_MEOW_APEX/infobase/sounds/woouoo.wav
72 if [ ! -z "$1" ]; then
73   TUNNEL_ALERT_SOUND=$1
74 fi
75
76 # how often to play sounds when reconnecting.
77 NOISE_PERIOD=180
78
79 # when we last played a sound.
80 LAST_SOUND_TIME=0
81
82 ##############
83
84 play_sound_periodically()
85 {
86   CURRENT_TIME=$(date +"%s")
87   if (( $CURRENT_TIME - $LAST_SOUND_TIME >= $NOISE_PERIOD )); then
88     echo playing sound now.
89     bash $FEISTY_MEOW_SCRIPTS/multimedia/sound_play.sh $TUNNEL_ALERT_SOUND &>/dev/null </dev/null &
90     LAST_SOUND_TIME=$CURRENT_TIME
91   fi
92 }
93
94 ##############
95
96 function main_tunnely_loop()
97 {
98   while true; do
99     echo "Connecting tunnel to destination..."
100     ssh -2 -N -v "$TUNNEL_LINK" "$TUNNEL_SECURITY_KEY" "$TUNNEL_USER_PLUS_HOST"
101     echo "Got dumped from tunnels; re-establishing connection."
102     play_sound_periodically
103     echo "Note: if you're being asked for a password, then you haven't provided\nan RSA key that works yet."
104     sleep 1
105   done
106 }
107
108 if [ $LAUNCHING_TUNNEL -eq 1 ]; then
109   # this version is already ready to tunnel already, so let's tunnel.
110   main_tunnely_loop
111   # loop does not exit on its own.
112 else
113   # this version re-launches the script but tells it to start the tunnel.
114   existingScreens="$(screen -ls | grep "$TUNNEL_SCREEN_NAME")"
115   if [ ! -z "$existingScreens" ]; then
116     echo "This script is already running a screen for: $TUNNEL_SCREEN_NAME"
117     echo "Connect to that and zap it first before we try to start a new one,"
118     echo "e.g.: screen -r -S \"$TUNNEL_SCREEN_NAME\""
119     exit 1
120   fi
121   screen -L -S "$TUNNEL_SCREEN_NAME" -d -m bash $0 "$TUNNEL_USER_PLUS_HOST" "$TUNNEL_LINK" "$TUNNEL_SCREEN_NAME" "$TUNNEL_SECURITY_KEY" go
122 fi
123