433147a724
git-svn-id: svn://svn.openwrt.org/openwrt/packages@21111 3c298f89-4303-0410-b956-a3cf2f4a3e73
75 lines
2.7 KiB
Bash
Executable File
75 lines
2.7 KiB
Bash
Executable File
#!/bin/sh /etc/rc.common
|
|
|
|
START=99
|
|
STOP=01
|
|
|
|
PIDFILE="/tmp/run/sshtunnel"
|
|
|
|
load_tunnel() {
|
|
config_get user $1 user
|
|
config_get hostname $1 hostname
|
|
config_get identity $1 identity
|
|
config_get remoteport $1 remoteport
|
|
config_get localport $1 localport
|
|
config_get options $1 options '-o ServerAliveCountMax=3 -o ServerAliveInterval=20 -o StrictHostKeyChecking=false'
|
|
config_get retrydelay $1 retrydelay "10"
|
|
[ "$cfgtype" = "tunnelL" ] && {
|
|
config_get localaddress $1 localaddress "127.0.0.1"
|
|
config_get remoteaddress $1 remoteaddress "*"
|
|
}
|
|
[ "$cfgtype" = "tunnelR" ] && {
|
|
config_get localaddress $1 localaddress "*"
|
|
config_get remoteaddress $1 remoteaddress "127.0.0.1"
|
|
}
|
|
|
|
local error
|
|
[ -f "$identity" ] || error="Identity file $identity not accessible"
|
|
[ -n "$user" ] || error="please set user option"
|
|
[ -n "$hostname" ] || error="please set hostname option"
|
|
[ "$remoteport" -gt 0 -a "$localport" -gt 0 -a "$retrydelay" -ge 0 ] || error="invalid configuration"
|
|
[ -n "$error" ] && { logger -p user.err -t "sshtunnel" "$cfgtype $1 not started - $error"; return; }
|
|
|
|
[ "$cfgtype" = "tunnelL" ] && {
|
|
args="-N -i $identity -o PasswordAuthentication=no -o ExitOnForwardFailure=yes $options -L $localaddress:$localport:$remoteaddress:$remoteport $user@$hostname"
|
|
}
|
|
[ "$cfgtype" = "tunnelR" ] && {
|
|
args="-N -i $identity -o PasswordAuthentication=no -o ExitOnForwardFailure=yes $options -R $remoteaddress:$remoteport:$localaddress:$localport $user@$hostname"
|
|
}
|
|
|
|
/usr/bin/sshtunnel.sh "$args" "$retrydelay" &
|
|
echo $! >> "$PIDFILE".pids
|
|
logger -p user.info -t "sshtunnel" "started new $cfgtype $1 (pid=$!;retrydelay=$retrydelay)"
|
|
}
|
|
|
|
stop() {
|
|
if [ -f "$PIDFILE".pids ]
|
|
then
|
|
logger -p user.info -t "sshtunnel" "stopping all tunnels"
|
|
|
|
while read pid
|
|
do
|
|
start-stop-daemon -K -p "$PIDFILE"_"$pid".pid
|
|
kill $pid
|
|
logger -p daemon.info -t "sshtunnel[$pid]" "tunnel stopped"
|
|
done < "$PIDFILE".pids
|
|
|
|
rm "$PIDFILE".pids
|
|
|
|
logger -p user.info -t "sshtunnel" "all tunnels stopped"
|
|
else
|
|
logger -p user.info -t "sshtunnel" "no tunnels running"
|
|
fi
|
|
}
|
|
|
|
start() {
|
|
[ -f "$PIDFILE".pids ] && stop
|
|
|
|
logger -p user.info -t "sshtunnel" "starting all tunnels"
|
|
|
|
config_load sshtunnel
|
|
config_foreach load_tunnel tunnelR
|
|
config_foreach load_tunnel tunnelL
|
|
|
|
logger -p user.info -t "sshtunnel" "all tunnels started"
|
|
}
|