Skip to content

Commit

Permalink
Always return status started to DSM so that the app icon appears for …
Browse files Browse the repository at this point in the history
…configuration
  • Loading branch information
davidcava committed Nov 11, 2019
1 parent 57a74c8 commit 38a528a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 82 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ After installing the package, create configuration files and start the service(s
Note: configuration file(s) are stored into `/var/packages/shadowsocks-libev/etc`.
Names must be: `ss-local.json` `ss-tunnel.json` `ss-redir.json` `ss-server.json` `ss-manager.json`
Additional instances can be created with names: `ss-local-xxx.json` `ss-tunnel-xxx.json` etc.
When removing the package, the config files are kept in folder `/usr/syno/etc/packages/shadowsocks-libev`. They are reused if the package is reinstalled.

If ss-redir is used, then routing will be activated. The incoming non-local traffic will be routed to ss-redir through iptables. udp will/might not work (see limitation below).

# Limitation
- Service is considered "started" in DSM even though no shadowsocks service could be started. This is needed for DSM to show the shadowsocks config app in the app list.
- Only works on DSM 6.1 and 6.2
- I don't know how to compile v2ray-plugin using Synology's dev environment so I just copy the binaries provided in the project. Not really sure which one goes into which architecture so probably not working for all. If not working, just drop the v2ray-plugin executable into /var/packages/shadowsocks-libev/target/bin (and tell me what works for you).
- Graphical interface is still experimental: it works well on my model but might not on others. Use SSH and edit config files manually if any issue.
Expand Down
2 changes: 1 addition & 1 deletion synology/INFO.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
. /pkgscripts-ng/include/pkg_util.sh

package="shadowsocks-libev"
version="3.3.3-1"
version="3.3.3-2"
displayname="Shadowsocks-libev"
arch="$(pkg_get_platform) "
maintainer="David Cavallini"
Expand Down
163 changes: 82 additions & 81 deletions synology/scripts/start-stop-status
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ SCRIPT_TPROXY="$SCRIPT_DIR/tproxy.sh"

start_daemon ()
{
local sourcefiles
if [ "$1" = "" ]; then
sourcefiles="$CONFIG_DIR" # normal launch: start everything in the folder
else
sourcefiles="$1" # unit launch: only 1 file
fi
local sourcefiles
if [ "$1" = "" ]; then
sourcefiles="$CONFIG_DIR" # normal launch: start everything in the folder
else
sourcefiles="$1" # unit launch: only 1 file
fi

local CONF_FILE started retcode tcpredir udpredir
started=0
Expand Down Expand Up @@ -93,8 +93,8 @@ start_daemon ()
# At least 1 daemon could start, update the used ports toward DSM if needed
"$SCRIPT_DIR/gensc.sh"
else
echo 'Nothing started: check config file(s) ss-(local|server|redir|tunnel|manager)[-confname].json in: '"$CONFIG_DIR" >> "$SYNOPKG_TEMP_LOGFILE"
exit 1
echo 'No shadowsocks service is currently started: create or check config file(s) ss-(local|server|redir|tunnel|manager)[-confname].json using the user interface or directly in folder: '"$CONFIG_DIR" >> "$SYNOPKG_TEMP_LOGFILE"
exit 0 # Always return 0 because DSM does not show the config app if service is not considered started
fi

# Run network configuration scripts for ss-redir (iptables REDIRECT / TPROXY...)
Expand All @@ -119,18 +119,18 @@ stop_daemon ()
{
local PID_FILE

local sourcefiles
if [ "$1" = "" ]; then
sourcefiles="$PID_DIR" # normal launch: stop everything in the folder
else
sourcefiles="$PID_DIR/$1.pid" # unit stop: only 1 file
fi
local sourcefiles
if [ "$1" = "" ]; then
sourcefiles="$PID_DIR" # normal launch: stop everything in the folder
else
sourcefiles="$PID_DIR/$1.pid" # unit stop: only 1 file
fi

while read -r PID_FILE
do
kill "$(cat "$PID_FILE")"
wait_for_death 20 "$(cat "$PID_FILE")" || kill -9 "$(cat "$PID_FILE")"
rm -f "$PID_FILE"
kill "$(cat "$PID_FILE")"
wait_for_death 20 "$(cat "$PID_FILE")" || kill -9 "$(cat "$PID_FILE")"
rm -f "$PID_FILE"
done < <( find -L "$sourcefiles" -maxdepth 1 -regextype posix-extended -regex '.*/'"$PIDFILES_REGEX" -type f )
}

Expand All @@ -139,19 +139,19 @@ daemon_status_unit ()
local CONF_FILE SS_SERVER CONF_NAME PID_FILE

CONF_FILE="$1"
SS_SERVER=$(echo "$CONF_FILE" | sed -E 's,'"$CONFFILES_REGEX"',\2,')
CONF_NAME=$(echo "$CONF_FILE" | sed -E 's,'"$CONFFILES_REGEX"',\4,')

PID_FILE="$PID_DIR/$SS_SERVER${CONF_NAME:+-$CONF_NAME}.pid"

if [ -f "$PID_FILE" ] && kill -0 "$(cat "$PID_FILE")" > /dev/null 2>&1; then
[ -n "$2" ] || echo " $SS_SERVER $CONF_NAME is running"
return 0
else
[ -n "$2" ] || echo " $SS_SERVER $CONF_NAME is not running"
rm -f "$PID_FILE"
return 1
fi
SS_SERVER=$(echo "$CONF_FILE" | sed -E 's,'"$CONFFILES_REGEX"',\2,')
CONF_NAME=$(echo "$CONF_FILE" | sed -E 's,'"$CONFFILES_REGEX"',\4,')

PID_FILE="$PID_DIR/$SS_SERVER${CONF_NAME:+-$CONF_NAME}.pid"

if [ -f "$PID_FILE" ] && kill -0 "$(cat "$PID_FILE")" > /dev/null 2>&1; then
[ -n "$2" ] || echo " $SS_SERVER $CONF_NAME is running"
return 0
else
[ -n "$2" ] || echo " $SS_SERVER $CONF_NAME is not running"
rm -f "$PID_FILE"
return 1
fi
}

# Status is True if at least 1 ss-<server> is running, False if none is running
Expand All @@ -177,16 +177,16 @@ wait_for_death ()
local daemonpid=$2
while [ "${counter}" -gt 0 ]; do
kill -0 "$daemonpid" > /dev/null 2>&1 || return 0
counter=$((counter-1))
counter=$((counter-1))
sleep 1
done
return 1
}

setvars_unit_file ()
{
UNIT_SERVER=$(basename "$1" .json)
UNIT_FILE="$CONFIG_DIR/$UNIT_SERVER.json"
UNIT_SERVER=$(basename "$1" .json)
UNIT_FILE="$CONFIG_DIR/$UNIT_SERVER.json"
}

# script is run from unexisting home directory of homeless user, which bugs some functions, so go to /tmp instead
Expand All @@ -195,57 +195,58 @@ cd "${TMPDIR-/tmp}"

case $1 in
start)
if [ "$2" = "" ]; then
echo "Starting $DNAME ..."
start_daemon
else
setvars_unit_file "$2"
if [ ! -f "$UNIT_FILE" ]; then
echo "$UNIT_FILE not found"
exit 1
elif daemon_status_unit "$UNIT_FILE" 0; then
echo "$DNAME $UNIT_SERVER is already running"
else
echo "Starting $DNAME $UNIT_SERVER ..."
start_daemon "$UNIT_FILE"
fi
fi
;;
if [ "$2" = "" ]; then
echo "Starting $DNAME ..."
start_daemon
exit 0 # Always return 0 because DSM does not show the config app if service is not considered started
else
setvars_unit_file "$2"
if [ ! -f "$UNIT_FILE" ]; then
echo "$UNIT_FILE not found"
exit 1
elif daemon_status_unit "$UNIT_FILE" 0; then
echo "$DNAME $UNIT_SERVER is already running"
else
echo "Starting $DNAME $UNIT_SERVER ..."
start_daemon "$UNIT_FILE"
fi
fi
;;
stop)
if [ "$2" = "" ]; then
if daemon_status 0; then
echo "Stopping $DNAME ..."
stop_daemon
else
echo "$DNAME is not running"
fi
else
setvars_unit_file "$2"
if daemon_status_unit "$UNIT_FILE" 0; then
echo "Stopping $DNAME $UNIT_SERVER ..."
stop_daemon "$UNIT_SERVER"
else
echo "$DNAME $UNIT_SERVER is not running"
fi
fi
if [ "$2" = "" ]; then
if daemon_status 0; then
echo "Stopping $DNAME ..."
stop_daemon
else
echo "$DNAME is not running"
fi
else
setvars_unit_file "$2"
if daemon_status_unit "$UNIT_FILE" 0; then
echo "Stopping $DNAME $UNIT_SERVER ..."
stop_daemon "$UNIT_SERVER"
else
echo "$DNAME $UNIT_SERVER is not running"
fi
fi
;;
status)
if [ "$2" = "" ]; then
if daemon_status; then
exit 0
else
echo "$DNAME is not running"
exit 3
fi
else
setvars_unit_file "$2"
if daemon_status_unit "$UNIT_FILE"; then
exit 0
else
exit 3
fi
fi
;;
if [ "$2" = "" ]; then
if daemon_status; then
exit 0
else
echo "$DNAME is not running"
exit 0 # Always return 0 because DSM does not show the config app if service is not considered started
fi
else
setvars_unit_file "$2"
if daemon_status_unit "$UNIT_FILE"; then
exit 0
else
exit 3
fi
fi
;;
log)
echo "${LOG_FILE}"
;;
Expand Down

0 comments on commit 38a528a

Please sign in to comment.