start() { ssh -NTCf -o ServerAliveInterval=30 \ -o ServerAliveCountMax=6 \ -o ExitOnForwardFailure=yes \ -o Tunnel=point-to-point \ -w "${CLIENT_TUNNEL#tun}:${SERVER_TUNNEL#tun}" \ root@${SERVER_SSH_IP} -p ${SERVER_SSH_PORT} if [ $? -ne 0 ]; thenexit 1; fi echo"ssh tunnel is working." ssh -T root@${SERVER_SSH_IP} -p ${SERVER_SSH_PORT} > /dev/null 2>&1 << eeooff ip route replace default via ${SERVER_GATEWAY} ip link set ${SERVER_TUNNEL} down ifconfig ${SERVER_TUNNEL} > /dev/null 2>&1 if [ $? -eq 0 ]; then echo 1 > /proc/sys/net/ipv4/ip_forward ip link set ${SERVER_TUNNEL} up ip addr add ${SERVER_TUN_IP}/32 peer ${CLIENT_TUN_IP} dev ${SERVER_TUNNEL} ip route replace default via ${SERVER_TUN_IP} fi exit eeooff if [ $? -ne 0 ]; thenexit 1; fi echo"remote start." ifconfig ${CLIENT_TUNNEL} > /dev/null 2>&1 if [ $? -eq 0 ]; then echo 1 > /proc/sys/net/ipv4/ip_forward ip linkset${CLIENT_TUNNEL} up ip addr add ${CLIENT_TUN_IP}/32 peer ${SERVER_TUN_IP} dev ${CLIENT_TUNNEL} iptables -t nat -A POSTROUTING -s ${SERVER_TUN_IP}/32 -o ${CLIENT_ETHERNET} -j MASQUERADE iptables -A FORWARD -p tcp --syn -s ${SERVER_TUN_IP}/32 -j TCPMSS --set-mss 1356 ping ${SERVER_TUN_IP} -i 60 > /dev/null 2>&1 & echo"local start." else exit 1 fi }
stop-srv() { ssh -T root@${SERVER_SSH_IP} -p ${SERVER_SSH_PORT} > /dev/null 2>&1 << eeooff ip route replace default via ${SERVER_GATEWAY} ip link set ${SERVER_TUNNEL} down exit eeooff if [ $? -eq 0 ]; thenecho"remote stop."; fi }
stop() { ip linkset${CLIENT_TUNNEL} down iptables -t nat -D POSTROUTING -s ${SERVER_TUN_IP}/32 -o ${CLIENT_ETHERNET} -j MASQUERADE iptables -D FORWARD -p tcp --syn -s ${SERVER_TUN_IP}/32 -j TCPMSS --set-mss 1356 CLIENT_SSH_PID=`ps -ef | grep 'ssh -NTCf -o' | grep -v grep | head -n1 | awk '{print $2}'` if [ -n "${CLIENT_SSH_PID}" ]; thenkill -9 ${CLIENT_SSH_PID}; fi if [ -n "`pidof ping`" ]; then pidof ping | xargs kill -9; fi } > /dev/null 2>&1