diff --git a/shell_helper/README.md b/shell_helper/README.md new file mode 100644 index 0000000..3950ed2 --- /dev/null +++ b/shell_helper/README.md @@ -0,0 +1,40 @@ +INTRO +----- + +bash function to manage sslocal session and mac osx system proxy settings. + +- single bash function, minimum footprint +- enable/disable system proxy easily +- tab completion support + +INSTALL +----- + +copy `shadowsocks_helper.sh` to anywhere you like. + +edit the shell variables to reflect you configuration. + + NAME="shadowsocks" + VENVPATH="$HOME/.virtualenvs/sandbox/bin" + CONFFILE="$HOME/bin/lib/$NAME.config.json" + LOGFILE="/tmp/$NAME.log" + PIDFILE="/tmp/$NAME.pid" + + NETWORK_INTERFACE="Wi-Fi" + PROXY_HOST="127.0.0.1" + PROXY_PORT="3131" + + COMMAND=($VENVPATH/python $VENVPATH/sslocal -c $CONFFILE) + +if you don't use virtualenv, leave `VENVPATH` blank and change `COMMAND` to call sslocal directly. + +then add the following lines to .bashrc or .zshrc + + if [ -x ~/bin/lib/shadowsocks_helper.sh ]; then + eval "$(~/bin/lib/shadowsocks_helper.sh)" + fi + +USAGE +----- + + $ shadowsocks [status|start|stop|restart|system_proxy_(status|enable|disable)] diff --git a/shell_helper/shadowsocks_helper.sh b/shell_helper/shadowsocks_helper.sh new file mode 100755 index 0000000..b184bc8 --- /dev/null +++ b/shell_helper/shadowsocks_helper.sh @@ -0,0 +1,143 @@ +#!/bin/sh + +## TODO: completion + +## add following lines to .bashrc or .zshrc +## =-= +## shadowsocks proxy helper functions +#if [ -x ~/bin/lib/shadowsocks_helper.sh ]; then +# eval "$(~/bin/lib/shadowsocks_helper.sh)" +#fi +## =-= + +## CONFIG START ## +NAME="shadowsocks" +VENVPATH="$HOME/.virtualenvs/sandbox/bin" +CONFFILE="$HOME/bin/lib/$NAME.config.json" +LOGFILE="/tmp/$NAME.log" +PIDFILE="/tmp/$NAME.pid" + +NETWORK_INTERFACE="Wi-Fi" +PROXY_HOST="127.0.0.1" +PROXY_PORT="3131" + +COMMAND=($VENVPATH/python $VENVPATH/sslocal -c $CONFFILE) +## CONFIG END ## + +cat <&2 + return + } + nohup ${COMMAND[@]} > $LOGFILE & + PID=\$! + echo \$PID > $PIDFILE + if [ \$? -eq 0 ]; then + echo "[INFO] $NAME is running with pid \$PID." + sleep 3 + cat $LOGFILE + else + echo "[ERR] failed to start $NAME." >&2 + return 1 + fi + ;; + stop) + local PID=\$(shadowsocks _pid) + if [ \$PID -eq 0 ]; then + echo "[INFO] $NAME is not running." + else + kill \$PID + echo "[INFO] stopped $NAME running with pid \$PID." + #tail -n 5 $LOGFILE + fi + ;; + restart) + shadowsocks stop + shadowsocks start + ;; + system_proxy_status) + echo "[INFO] System Proxy Setting:" + networksetup -getsocksfirewallproxy $NETWORK_INTERFACE | awk '{print " " \$0}' + ;; + system_proxy_enable) + sudo networksetup -setsocksfirewallproxy $NETWORK_INTERFACE $PROXY_HOST $PROXY_PORT + sudo networksetup -setsocksfirewallproxystate $NETWORK_INTERFACE on + shadowsocks system_proxy_status + ;; + system_proxy_disable) + sudo networksetup -setsocksfirewallproxystate $NETWORK_INTERFACE off + shadowsocks system_proxy_status | head -n 2 + ;; + _pid) + if [ ! -r $PIDFILE ]; then + echo 0 + return + fi + local PID=\$(cat $PIDFILE) + ps -p \$PID | grep -q $NAME + if [ \$? -ne 0 ]; then + echo 0 + return + fi + echo \$PID + ;; + *) + shadowsocks status -s + echo "Usage: shadowsocks status|start|stop|restart" + echo " shadowsocks system_proxy_(status|enable|disable)" + ;; + esac +} +EOF + +SUBCOMMANDS="restart start status stop system_proxy_disable system_proxy_enable system_proxy_status" + +case $(basename $SHELL) in + bash) + cat <&2 + ;; +esac + +