# FILE_LOCATION: /etc/init.d/shadowsocks-whitelist # DESCRIPTION: Service script for customized shadowsocks-whitelist #!/bin/sh /etc/rc.common # # Copyright (C) 2014-2017 Jian Chang # # This is free software, licensed under the GNU General Public License v3. # See /LICENSE for more information. # START=90 STOP=15 NAME=shadowsocks CONFIG_FILE=/var/etc/$NAME-whitelist.json uci_get_by_name() { local ret=$(uci get $NAME.$1.$2 2>/dev/null) echo ${ret:=$3} } uci_get_by_type() { local ret=$(uci get $NAME.@$1[0].$2 2>/dev/null) echo ${ret:=$3} } uci_bool_by_name() { case "$(uci_get_by_name $1 $2)" in 1|on|true|yes|enabled) return 0;; esac return 1 } valid_server() { [ "$(uci get $NAME.$1 2>/dev/null)" = "servers" ] } get_arg_ota() { uci_bool_by_name $1 auth && echo "-A" } get_plugin_config() { local plugin=$(uci_get_by_name $1 plugin) local plugin_opts=$(uci_get_by_name $1 plugin_opts) [ -n "$plugin" -a -n "$plugin_opts" ] && echo " \"plugin\": \"$plugin\", \"plugin_opts\": \"$plugin_opts\"," } gen_config_file() { cat <<-EOF >$CONFIG_FILE { "server": "$(uci_get_by_name $1 server)", "server_port": $(uci_get_by_name $1 server_port), "local_address": "0.0.0.0",$(get_plugin_config $1) "password": "$(uci_get_by_name $1 password)", "timeout": $(uci_get_by_name $1 timeout 60), "method": "$(uci_get_by_name $1 encrypt_method)" } EOF } start_redir() { valid_server $1 || return 1 gen_config_file $1 ss-redir -c $CONFIG_FILE $2 $(get_arg_ota $1) \ -l $(($(uci_get_by_type transparent_proxy local_port 1234) + 1)) \ -f /var/run/ss-redir$3.pid } start() { mkdir -p /var/run /var/etc start_redir "@servers[1]" -u "-whitelist" rm -f $CONFIG_FILE } delay_start() { (sleep $1 && start >/dev/null 2>&1) & } boot() { local delay=$(uci_get_by_type general startup_delay 0) if [ "$delay" -gt 0 ]; then delay_start $delay else start fi return 0 } kill_all() { kill $(pidof $@) >/dev/null 2>&1 } stop() { kill_all ss-redir }