Changeset 7530


Ignore:
Timestamp:
Feb 25, 2010, 6:27:14 PM (15 years ago)
Author:
richardvm
Message:

New version of sshtun for the proxyfactory

Location:
2.0/nanobsd/nanobsd/files/usr/local
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified 2.0/nanobsd/nanobsd/files/usr/local/etc/rc.d/sshtun

    r7469 r7530  
    11#!/bin/sh
     2#
     3# PROVIDE: sshtun
     4# REQUIRE: DAEMON
     5# KEYWORD: shutdown
    26#
    37# Add the following line to /etc/rc.conf to enable ssh-tun:
    48#
    59# sshtun_enable="YES"
    6 #
    7 # PROVIDE: sshtun
    8 # REQUIRE: LOGIN
    9 
     10#
    1011. /etc/rc.subr
    1112
     
    1314rcvar=`set_rcvar`
    1415
    15 flags=${sshtun_flags}
    16 
     16command_interpreter=/bin/sh
    1717command=/usr/local/sshtun/sshtun.sh
    1818
    19 pidfile=/var/run/${name}.pid
     19command_args="${sshtun_options} &"
    2020
    2121load_rc_config ${name}
     
    2323sshtun_enable=${sshtun_enable-"NO"}
    2424
    25 stop_cmd="sshtun_stop"
    26 start_cmd="sshtun_start"
    27 
    28 sshtun_start()
    29 {
    30   ${command} start ${flags} &
    31 }
    32 
    33 sshtun_stop()
    34 {
    35   ${command} stop
    36 }
    37 
     25sshtun_pidfile=${sshtun_pidfile-"/var/run/sshtun.pid"}
     26pidfile="${sshtun_pidfile}"
    3827
    3928run_rc_command "$1"
    4029
    41 
  • TabularUnified 2.0/nanobsd/nanobsd/files/usr/local/sshtun/sshtun.sh

    r7472 r7530  
    11#!/bin/sh
     2# Simple deamon which does remote connecting to a sertain host for tunnel
     3# forwarding
     4#
     5# Created by Richard van Mansom - Jan 2010
     6# Make deamon friendly by Rick van der Zwet  - Feb 2010
     7
    28
    39# Define some vars
    4 DIR=`dirname $0`
    5 CMD=$1
     10PWD=`dirname $0`
     11SSH_FLAGS=$@
     12
    613HOST="sshtun.wirelessleiden.nl"
    7 REMOTEPORT=$2
    8 LOCALPORT="22"
    914USER="ssh-tun"
    10 SSHKEY="${DIR}/sshkey"
    11 WAIT="10"
     15SSHKEY="${PWD}/sshkey"
     16PID='/var/run/sshtun.pid'
     17LOGFILE='/var/log/sshtun.log'
     18
     19# Program internal variables
     20WAIT="10"
    1221REMOTEWAIT=`expr 3600 \* 24 \* 365`
    13 echo ${REMOTEPORT}
     22
     23log() {
     24  echo `date "+%b %e %T"` ": " $* >> ${LOGFILE}
     25}
     26
     27# Store script PID in right location
     28echo $$ > $PID || exit 1
     29
     30SSHPIDFILE=`mktemp -t $(basename $0)`
     31
     32trap_exit() {
     33   if [ -s $SSHPID ]; then
     34     kill `cat $SSHPID`
     35     rm $SSHPID
     36   fi
     37   exit
     38}
     39
     40# Don't leave the ssh client process behind
     41trap "trap_exit" 0 1 2 15
    1442
    1543# Make sure a port is specified
    16 if [ -n "$REMOTEPORT" ]; then
     44if [ -z "$SSH_FLAGS" ]; then
     45  echo "Usage: $0 <SSH_CLIENT_FLAGS>"
     46  exit 64
     47fi
    1748
    18   case $1 in
    19     start)
    20  
    21       PROCESS=`ps -ax | grep localhost | grep sshtun | grep ${REMOTEPORT} | grep -v grep | awk '{print $1}'`
    22       if [ -z "$PROCESS" ]; then
    23  
    24         sleep 1
     49# Main program
     50log "[INFO] Connecting with args: $SSH_FLAGS"
    2551
    26         echo "Connecting using port $REMOTEPORT"
    27  
    28         # Make sure the script never dies
    29         while [ true ]
    30         do
    31        
    32           # Connect to remote site
    33           ssh -R ${REMOTEPORT}:localhost:${LOCALPORT} -i ${SSHKEY} ${USER}@${HOST} -o ServerAliveInterval=5 -o ExitOnForwardFailure=yes  -o BatchMode=yes  -o StrictHostKeyChecking=no -n -N "sh -c 'sleep ${REMOTEWAIT}'"
    34           # Echo some stuff
    35           date
    36           echo "Sleeping $WAIT seconds"
    37           echo ""
    38        
    39           # Go to sleep
    40           sleep ${WAIT}
    41         done
    42  
    43       else
    44         echo "Tunnel already running"
    45         echo "Pid: "
    46         echo ${PROCESS}
    47       fi
    48  
    49     ;;
    50     stop)
    51  
    52       # Killing the tunnel
    53       PROCESS=`ps -ax | grep -v stop | grep -E '(ssh-tun|sshtun)' | grep ${REMOTEPORT} | grep -v grep | awk '{print $1}'`
    54  
    55       if [ -n "$PROCESS"  ]; then
    56         echo "Killing session with port $REMOTEPORT :
    57   $PROCESS"
    58         echo ${PROCESS} | xargs kill -9
    59       fi
    60     ;;
    61  
    62     *)
    63       # Display usage info
    64       echo "Usage: ./sshtun <ACTION> <PORT>"
    65     ;;
    66   esac
    67 fi
     52# Make sure the tunnel keeps on reconnecting
     53while true
     54do
     55  # Connect to remote site
     56  ssh ${SSH_FLAGS} -i ${SSHKEY} ${USER}@${HOST} -o ServerAliveInterval=5 \
     57    -o ExitOnForwardFailure=yes -o BatchMode=yes  -o StrictHostKeyChecking=no \
     58    -n -N "sh -c 'sleep ${REMOTEWAIT}'" &
     59
     60  # Save the sshtun PID
     61  SSHPID=$!
     62  echo ${SSHPID} > ${SSHPIDFILE}
     63  log "[NOTICE] Used PID: $SSHPID"
     64
     65  # As long the client is running make sure to sleep and relax, don't make it very
     66  # long as a sleeping shell script cannot receive sigtals
     67  while [ `ps ax | awk '{ print $1 }' | grep "^$SSHPID\$"` ]; do
     68     sleep 10
     69  done
     70  log "[NOTICE] SSHTUN Disconnected ${SSHPID}"
     71
     72  # Mark process disconnected
     73  echo "" > ${SSHPIDFILE}
     74
     75  log "[INFO] Sleeping $WAIT seconds before reconnect"
     76  sleep ${WAIT}
     77done
     78
Note: See TracChangeset for help on using the changeset viewer.