From 04015f4b2cb0a029732017ddf764a9ed860c6797 Mon Sep 17 00:00:00 2001 From: Teddysun Date: Thu, 7 Jun 2018 21:22:35 +0900 Subject: [PATCH] Support multiple users with ss-manager start or stop the Shadowsocks-libev server to support multiple users with ss-manager Signed-off-by: Teddysun --- shadowsocks-manager | 129 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 shadowsocks-manager diff --git a/shadowsocks-manager b/shadowsocks-manager new file mode 100644 index 00000000..afcfc1ac --- /dev/null +++ b/shadowsocks-manager @@ -0,0 +1,129 @@ +#!/usr/bin/env bash +# chkconfig: 2345 90 10 +# description: A secure socks5 proxy, designed to protect your Internet traffic. + +### BEGIN INIT INFO +# Provides: Shadowsocks-libev +# Required-Start: $network $syslog +# Required-Stop: $network +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Fast tunnel proxy that helps you bypass firewalls +# Description: Start or Stop the Shadowsocks-libev server to support multiple users with ss-manager +### END INIT INFO + +# Author: Teddysun + +if [ -f /usr/local/bin/ss-manager ]; then + MANAGER=/usr/local/bin/ss-manager +elif [ -f /usr/bin/ss-manager ]; then + MANAGER=/usr/bin/ss-manager +fi + +if [ -f /usr/local/bin/ss-server ]; then + DAEMON=/usr/local/bin/ss-server +elif [ -f /usr/bin/ss-server ]; then + DAEMON=/usr/bin/ss-server +fi + +NAME=Shadowsocks-manager +CONF=/etc/shadowsocks-manager/config.json +PID_DIR=/var/run +PID_FILE=$PID_DIR/shadowsocks-manager.pid +SOCK_FILE=$PID_DIR/shadowsocks-manager.sock +WK_DIR=~/.shadowsocks +WK_PID_LST=($WK_DIR/.*pid) + +RET_VAL=0 + +[ -x $MANAGER ] || exit 0 +[ -x $DAEMON ] || exit 0 + +if [ ! -d $PID_DIR ]; then + mkdir -p $PID_DIR + if [ $? -ne 0 ]; then + echo "Creating PID directory $PID_DIR failed" + RET_VAL=1 + fi +fi + +if [ ! -f $CONF ]; then + echo "$NAME config file $CONF not found" + RET_VAL=1 +fi + +check_running() { + if [ -r $PID_FILE ]; then + read PID < $PID_FILE + if [ -d "/proc/$PID" ]; then + return 0 + else + rm -f $PID_FILE + return 1 + fi + else + return 2 + fi +} + +do_status() { + check_running + case $? in + 0) + echo "$NAME (pid $PID) is running..." + ;; + 1|2) + echo "$NAME is stopped" + RET_VAL=1 + ;; + esac +} + +do_start() { + if check_running; then + echo "$NAME (pid $PID) is already running..." + return 0 + fi + $MANAGER --manager-address $SOCK_FILE --executable $DAEMON -c $CONF -f $PID_FILE + if check_running; then + echo "Starting $NAME success" + else + echo "Starting $NAME failed" + RET_VAL=1 + fi +} + +do_stop() { + if check_running; then + kill -9 $PID + rm -f $PID_FILE + rm -f $SOCK_FILE + for i in "${WK_PID_LST[@]}"; do + [ -r $i ] && read WK_PID < $i + [ -d "/proc/$WK_PID" ] && kill -9 $WK_PID + rm -f $i + done + echo "Stopping $NAME success" + else + echo "$NAME is stopped" + RET_VAL=1 + fi +} + +do_restart() { + do_stop + sleep 0.5 + do_start +} + +case "$1" in + start|stop|restart|status) + do_$1 + ;; + *) + echo "Usage: $0 { start | stop | restart | status }" + RET_VAL=1 + ;; +esac + +exit $RET_VAL