Skip to content

Instantly share code, notes, and snippets.

@MichaelRix
Last active August 13, 2017 14:56
Show Gist options
  • Save MichaelRix/61ea17247a40cd3a21cc8b1963e72ee7 to your computer and use it in GitHub Desktop.
Save MichaelRix/61ea17247a40cd3a21cc8b1963e72ee7 to your computer and use it in GitHub Desktop.
Shadowsocks-libev with simple-obfs script
#!/usr/bin/env bash
function check_env() {
libev_ver=$(ss-local -h | grep "shadowsocks-libev")
[ -z "$libev_ver" ] && echo "[Error] Cannot find shadowsocks-libev" && exit 1
obfs_ver=$(obfs-local -h | grep "simple-obfs")
}
function get_pid() {
pid=$(ps -x | grep "/tmp/ss-local.pid" | grep "ss-local -c" | grep -oP '^\s*\d+' | grep -oP '\d+')
echo "$pid"
}
function start() {
if [ -f $1 ]; then
cfg=$(realpath $1)
if [ -n "$(get_pid)" ]; then
echo "[Error] already running"
fi
cmdline="ss-local -c $cfg -f /tmp/ss-local.pid"
[ -n "$obfs_ver" ] && $cmdline="$cmdline --plugin obfs-local --plugin-opts 'obfs=http; obfs-host=www.baidu.com'"
$cmdline && echo "[OK] started <-- $cfg"
[ $? != 0 ] && echo "[Error] Something unexpected occurred" && exit 127
else
echo "[Error] '$1' is not present" && exit 2
fi
}
function stop() {
pid=$(get_pid)
if [ -n "$pid" ]; then
kill $pid && echo "[Info] $pid stopped"
[ $? != 0 ] && echo "[Error] it refused to die"
rm -f /tmp/ss-local.pid && echo "[Info] pid file clear"
else
echo "[Error] not running"
fi
}
function usage() {
echo "Usage: $(basename $0) <path_to_config_json|args>"
echo " args:"
echo " -h or --help prints usage;"
echo " -s or stop ends the shadowsocks-libev process;"
echo " -v prints service status;"
echo " -V or --version prints version info;"
}
function status() {
pid=$(get_pid)
if [ -n "$pid" ]; then
echo "Running PID = $pid"
echo $(ps -x | grep -oP "^\\s*$pid.*?$" | grep -oP 'ss-local.*?$')
fi
}
function version() {
echo "shadowsocks-libev : $libev_ver"
echo "simple-obfs : $obfs_ver"
}
check_env
if [ $# != 1 ]; then
usage
exit 1
fi
case $1 in
-h | --help)
usage;;
-s | stop)
stop;;
-v)
status;;
-V | --version)
version;;
*)
start $1;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment