Skip to content

Instantly share code, notes, and snippets.

@nqxcode
Last active April 22, 2020 23:29
Show Gist options
  • Save nqxcode/29d43e7d8c19aa7fbd0403cd4ae100a1 to your computer and use it in GitHub Desktop.
Save nqxcode/29d43e7d8c19aa7fbd0403cd4ae100a1 to your computer and use it in GitHub Desktop.
OpenWrt 19.07.2 daemon to create socks tunnels via ssh
#!/bin/bash
# /root/bin/socks_tunnel
trap "stop" SIGTERM SIGINT
if [[ -z $1 ]]; then
echo 'Please, specify server...'
exit 1
fi
if [[ -z $2 ]]; then
echo 'Please, specify proxy port...'
exit 1
fi
SERVER=$1
PROXY_PORT=$2
SSH_CONTROL_SOCKET="/tmp/${SERVER}.${RANDOM}.socket"
function pid() {
echo `ssh -S $SSH_CONTROL_SOCKET -O check $SERVER 2>&1 | awk -F '=' '{print $2}' | awk -F ')' '{print $1}'`
}
function stop() {
PID=`pid`
if [ ! -z "$PID" ]; then
kill $PID
rm $SSH_CONTROL_SOCKET 2> /dev/null
exit 0
fi
}
while true; do
PID=`pid`
if [ -z "$PID" ]; then
ssh -M -S $SSH_CONTROL_SOCKET -o "StrictHostKeyChecking=no" -4 -D *:$PROXY_PORT $SERVER -f -N
fi
sleep 3
done
# /etc/config/socks_tunnel
config tunnel diolserver
option server 'diolserver'
option proxy_port '9999'
config tunnel remote_server
option server 'remote_server'
option proxy_port '9998'
#!/bin/bash /etc/rc.common
# /etc/init.d/socks_tunnel
USE_PROCD=1
START=95
STOP=10
CONFIGURATION=socks_tunnel
handle_instance() {
local tunnel="$1"
local server
local proxy_port
config_get server $tunnel server
config_get proxy_port $tunnel proxy_port
procd_open_instance $tunnel
procd_set_param command /bin/bash "/root/bin/socks_tunnel" "$server" "$proxy_port"
procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
procd_set_param stdout 1
procd_set_param stderr 1
procd_close_instance
}
start_service() {
config_load "${CONFIGURATION}"
config_foreach handle_instance tunnel
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment