Skip to content

Instantly share code, notes, and snippets.

@MichaelRix
Last active July 17, 2017 08:07
Show Gist options
  • Save MichaelRix/d7fc3e0563ee1cff5a07694b6ea09dc4 to your computer and use it in GitHub Desktop.
Save MichaelRix/d7fc3e0563ee1cff5a07694b6ea09dc4 to your computer and use it in GitHub Desktop.
Python shadowsocks script
#!/usr/bin/env bash
appdir=$HOME/.ssocks
function check_env() {
if [ ! -d $appdir ]; then
echo "[MESSAGE] '$appdir' does not exist, creating"
mkdir $appdir && echo "[MESSAGE] '$appdir' created"
[ $? != 0 ] && echo "[ERROR] Creation of '$appdir' failed" && exit 1
fi
[ ! -w $appdir ] && echo "[ERROR] '$appdir' is not writtable for $USER ($UID)." && exit 1
ver=$(sslocal --version)
[ -z "$ver" ] && echo "[ERROR] Cannot find shadowsocks python" && exit 1
}
function is_daemon_running() {
t=$(ps -x | grep 'sslocal' | grep $appdir)
[ -n "$t" ]
return $?
}
function start_daemon() {
if [ -f $1 ]; then
cfg=$(realpath $1);
if [ -f $appdir/sslocal.stop ]; then
is_daemon_running && echo '[ERROR] Daemon not stopped' && exit 1
rm -f $appdir/sslocal.stop && echo '[MESSAGE] Flag is removed'
fi
sslocal -c $cfg -d start --pid-file=$appdir/sslocal.pid --log-file=$appdir/shadowsocks-python.log &&
echo "sslocal -c $cfg -d stop --pid-file=$appdir/sslocal.pid" > $appdir/sslocal.stop &&
echo "[OK] Started <-- $cfg"
[ $? != 0 ] && echo "[ERROR] Something unexpected occurred" && exit 127
else
echo "[ERROR] '$1' is not present"
fi
}
function stop_daemon() {
if [ -f $appdir/sslocal.stop ]; then
bash $appdir/sslocal.stop && rm -f $appdir/sslocal.stop
if [ $? != 0 ]; then
echo "[MESSAGE] Something unexpected occurred"
rm -f $appdir/sslocal.stop
exit 127
fi
else
echo "[ERROR] not running"
fi
}
function get_template() {
echo '{
"server": "",
"server_port": 8388,
"local_address": "127.0.0.1",
"local_port": 1080,
"password": "",
"timeout": 300,
"method": "aes-256-cfb",
"fast_open": false,
"workers": 1
}'
}
function usage() {
echo "Usage: `basename $0` <path_to_config_json|'template'|'stop'>"
echo ' template : Get a config.json template redirected to stdout'
echo ' stop : Stops the shadowsocks daemon'
echo "Python $ver"
exit 1
}
check_env
if [ $# != 1 ]; then
usage
fi
case $1 in
-h | --help)
usage;;
stop)
stop_daemon;;
template)
get_template;;
*)
start_daemon $1;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment