Last active
August 20, 2018 01:07
-
-
Save yiichou/37bf9c07054b9d4ae467ae8e3fe96fe4 to your computer and use it in GitHub Desktop.
Management tool of kcptun server with ssh -D socks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
### BEGIN INIT INFO | |
# Provides: kcptun | |
# Required-Start: $network $local_fs $remote_fs | |
# Required-Stop: $network $local_fs $remote_fs | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Fast kcp tunnel proxy that helps you bypass firewalls | |
# Description: A secure socks5 proxy, designed to protect your Internet traffic via | |
# KCPTUN. KCPTUN is a simple UDP tunnel based on KCP. | |
# | |
### END INIT INFO | |
# Author: iChou <yiichou@gmail.com> | |
# PATH should only include /usr/* if it runs after the mountnfs.sh script | |
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin | |
DESC=kcptun-server | |
NAME=kcptund | |
DAEMON=/usr/local/bin/kcptund | |
USER=$( whoami ) | |
PIDFILE=/var/run/$NAME.pid | |
SCRIPTNAME=/etc/init.d/$NAME | |
LOGFILE=/var/log/$NAME.log | |
CONFILE=/etc/kcptun/server.json | |
# Exit if the package is not installed | |
[ -x $DAEMON ] || exit 0 | |
# Define LSB log_* functions. | |
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present. | |
. /lib/lsb/init-functions | |
# | |
# Function that starts the daemon/service | |
# | |
do_start() { | |
# Return | |
# 0 if daemon has been started | |
# 1 if daemon was already running | |
# 2 if daemon could not be started | |
start-stop-daemon --start --quiet --name $NAME --chuid $USER \ | |
--background --pidfile $PIDFILE --make-pidfile --no-close \ | |
--exec $DAEMON --test > /dev/null \ | |
|| return 1 | |
start-stop-daemon --start --quiet --name $NAME --chuid $USER \ | |
--background --pidfile $PIDFILE --make-pidfile --no-close \ | |
--exec $DAEMON -- -c $CONFILE >> $LOGFILE 2>&1 \ | |
|| return 2 | |
} | |
do_stop() { | |
# Return | |
# 0 if daemon has been stopped | |
# 1 if daemon was already stopped | |
# 2 if daemon could not be stopped | |
# other if a failure occurred | |
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE | |
RETVAL="$?" | |
[ "$RETVAL" = 2 ] && return 2 | |
# Many daemons don't delete their pidfiles when they exit. | |
rm -f $PIDFILE | |
return "$RETVAL" | |
} | |
check_socks5() { | |
if test $( netstat -lnp | grep 1080 | grep LISTEN | grep ssh | wc -l ) -eq 0 ; then | |
ssh -qTNnf -D 127.0.0.1:1080 $USER@localhost -p 3118 | |
if test "$?" -ne 0 ; then | |
echo "SSH -D socks could not be started" | |
exit 99 | |
fi | |
fi | |
} | |
case "$1" in | |
start) | |
check_socks5 | |
do_start | |
case "$?" in | |
0) echo "Kcptun has been started" ;; | |
1) echo "Kcptun was already running" ;; | |
2) echo "Kcptun could not be started" ;; | |
esac | |
;; | |
stop) | |
do_stop | |
case "$?" in | |
0) echo "Kcptun has been stopped" ;; | |
1) echo "Kcptun has not been started" ;; | |
2) echo "Kcptun could not be stopped" ;; | |
esac | |
pkill -f 'ssh -qTNnf -D 127.0.0.1:1080' | |
;; | |
restart) | |
do_stop | |
sleep 1 | |
check_socks5 | |
do_start | |
case "$?" in | |
0) echo "Kcptun has been restarted" ;; | |
*) echo "Kcptun could not be restarted" ;; | |
esac | |
;; | |
status) | |
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $? | |
;; | |
*) | |
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 | |
exit 3 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment