aboutsummaryrefslogtreecommitdiffstats
path: root/roles/shadowsocks/files/shadowsocks
blob: 2c5c970e64f908996fd2ae8df8d6a7d14e4257d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/sh
#
# Aaron LI
# 2018-06-24
#
# PROVIDE: shadowsocks
# REQUIRE: DAEMON
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable shadowsocks:
#
# shadowsocks_enable (bool):	Set it to "YES" to enable shadowsocks
#				Default is "NO".
# shadowsocks_config (path):	Set the path to the config file
#				Default is "/usr/local/etc/shadowsocks-libev/config.json",
#				or "/usr/local/etc/shadowsocks-libev/${profile}.json".
# shadowsocks_pidfile (path):	Set the path to the pid file
#				Default is "/var/run/shadowsocks.pid", or
#				"/var/run/shadowsocks-${profile}.pid".
# shadowsocks_mode (str):	Set the shadowsocks mode (server or client).
#				Default is "server".
# shadowsocks_flags (str):	Set the shadowsocks command line arguments
#				Default is "".
#
# If you would like to have multiple shadowsocks instances running, you can
# define multiple profiles:
#
# shadowsocks_profiles (str):	Set the list of shadowsocks profiles
#				Default is "".
#
# For each profile you can then define different options (except for
# shadowsocks_enable) using the syntax shadowsocks_<profile>_<option>
#

. /etc/rc.subr

name="shadowsocks"
rcvar="${name}_enable"

load_rc_config ${name}

_configdir="/usr/local/etc/shadowsocks-libev"

: ${shadowsocks_enable="NO"}
: ${shadowsocks_config="${_configdir}/config.json"}
: ${shadowsocks_pidfile="/var/run/${name}.pid"}
: ${shadowsocks_mode="server"}
: ${shadowsocks_flags=""}

is_profile() {
	local profile

	for profile in ${shadowsocks_profiles}; do
		if [ "${profile}" = "$1" ]; then
			return 0
		fi
	done

	return 1
}

if [ -n "${shadowsocks_profiles}" ]; then
	if [ -n "$2" ]; then
		profile="$2"
		if ! is_profile ${profile}; then
			echo "$0: no such profile defined in shadowsocks_profiles."
		        exit 1
		fi
		eval shadowsocks_config=\${shadowsocks_${profile}_config:-"${_configdir}/${profile}.json"}
		eval shadowsocks_pidfile=\${shadowsocks_${profile}_pidfile:-"/var/run/${name}-${profile}.pid"}
		eval shadowsocks_mode=\${shadowsocks_${profile}_mode:-"${shadowsocks_mode}"}
		eval shadowsocks_flags=\${shadowsocks_${profile}_flags:-"${shadowsocks_flags}"}
	elif [ -n "$1" ]; then
		for profile in ${shadowsocks_profiles}; do
			echo "Processing ${name} profile: ${profile}"
			/usr/local/etc/rc.d/${name} $1 ${profile}
		done
		exit 0
	fi
fi

pidfile="${shadowsocks_pidfile}"
required_files="${shadowsocks_config}"

if [ "${shadowsocks_mode}" = "server" ]; then
	command="/usr/local/bin/ss-server" 
else
	command="/usr/local/bin/ss-local" 
fi
command_args="-c ${shadowsocks_config} -f ${pidfile} ${shadowsocks_flags}"

stop_postcmd=stop_postcmd

stop_postcmd()
{
	rm -f ${pidfile}
}

run_rc_command "$1"