aboutsummaryrefslogtreecommitdiff
path: root/net/shadowsocks-libev/files
diff options
context:
space:
mode:
authorYousong Zhou <yszhou4tech@gmail.com>2019-02-18 08:00:08 +0000
committerYousong Zhou <yszhou4tech@gmail.com>2019-02-19 21:08:23 +0800
commit9cfa29b8732669ef3065e4c4bd493a801d06cbb8 (patch)
tree7f0fb9a9a6d88f31e226da9af502d6310685da75 /net/shadowsocks-libev/files
parent89df405736156440baccae49d47c4630a60f17a9 (diff)
shadowsocks-libev: use jshn.sh for making json config
As suggested by Jeffery To in openwrt/packages#8233 Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
Diffstat (limited to 'net/shadowsocks-libev/files')
-rw-r--r--net/shadowsocks-libev/files/shadowsocks-libev.init94
1 files changed, 30 insertions, 64 deletions
diff --git a/net/shadowsocks-libev/files/shadowsocks-libev.init b/net/shadowsocks-libev/files/shadowsocks-libev.init
index 9ab1051c8..e6a7b062c 100644
--- a/net/shadowsocks-libev/files/shadowsocks-libev.init
+++ b/net/shadowsocks-libev/files/shadowsocks-libev.init
@@ -11,26 +11,6 @@ START=99
ss_confdir=/var/etc/shadowsocks-libev
ss_bindir=/usr/bin
-q='"'
-
-ss_mkjson() {
- echo "{" >"$confjson"
- if ss_mkjson_ "$@" >>$confjson; then
- sed -i -e '/^\s*$/d' -e '2,$s/^/\t/' -e '$s/,$//' "$confjson"
- echo "}" >>"$confjson"
- else
- rm -f "$confjson"
- return 1
- fi
-}
-
-ss_mkjson_() {
- local func
-
- for func in "$@"; do
- "$func" || return 1
- done
-}
ss_mkjson_server_conf() {
local cfgserver
@@ -45,32 +25,11 @@ ss_mkjson_server_conf() {
ss_mkjson_server_conf_() {
[ -n "$server_port" ] || return 1
- password="${password//\"/\\\"}"
- cat <<-EOF
- ${server:+${q}server${q}: ${q}$server${q},}
- "server_port": $server_port,
- ${method:+${q}method${q}: ${q}$method${q},}
- ${key:+${q}key${q}: ${q}$key${q},}
- ${password:+${q}password${q}: ${q}$password${q},}
- EOF
-}
-
-ss_mkjson_common_conf() {
- [ "$ipv6_first" = 0 ] && ipv6_first=false || ipv6_first=true
- [ "$fast_open" = 0 ] && fast_open=false || fast_open=true
- [ "$reuse_port" = 0 ] && reuse_port=false || reuse_port=true
- cat <<-EOF
- "use_syslog": true,
- "ipv6_first": $ipv6_first,
- "fast_open": $fast_open,
- "reuse_port": $reuse_port,
- ${local_address:+${q}local_address${q}: ${q}$local_address${q},}
- ${local_port:+${q}local_port${q}: $local_port,}
- ${mode:+${q}mode${q}: ${q}$mode${q},}
- ${mtu:+${q}mtu${q}: $mtu,}
- ${timeout:+${q}timeout${q}: $timeout,}
- ${user:+${q}user${q}: ${q}$user${q},}
- EOF
+ [ -z "$server" ] || json_add_string server "$server"
+ json_add_int server_port "$server_port"
+ [ -z "$method" ] || json_add_string method "$method"
+ [ -z "$key" ] || json_add_string key "$key"
+ [ -z "$password" ] || json_add_string password "$password"
}
ss_mkjson_ss_local_conf() {
@@ -88,9 +47,7 @@ ss_mkjson_ss_server_conf() {
ss_mkjson_ss_tunnel_conf() {
ss_mkjson_server_conf || return 1
[ -n "$tunnel_address" ] || return 1
- cat <<-EOF
- ${tunnel_address:+${q}tunnel_address${q}: ${q}$tunnel_address${q},}
- EOF
+ json_add_string tunnel_address "$tunnel_address"
}
ss_xxx() {
@@ -101,23 +58,32 @@ ss_xxx() {
[ -x "$bin" ] || return
eval "$("validate_${cfgtype}_section" "$cfg" ss_validate_mklocal)"
- "validate_${cfgtype}_section" "$cfg" || return 1
+ "validate_${cfgtype}_section" "$cfg" || return
[ "$disabled" = 0 ] || return
- if ss_mkjson \
- ss_mkjson_common_conf \
- ss_mkjson_${cfgtype}_conf \
- ; then
- procd_open_instance "$cfgtype.$cfg"
- procd_set_param command "$bin" -c "$confjson"
- [ "$verbose" = 0 ] || procd_append_param command -v
- [ "$no_delay" = 0 ] || procd_append_param command --no-delay
- [ -z "$bind_address" ] || procd_append_param command -b "$bind_address"
- procd_set_param file "$confjson"
- procd_set_param respawn
- procd_close_instance
- ss_rules_cb
- fi
+ json_init
+ ss_mkjson_${cfgtype}_conf || return
+ json_add_boolean use_syslog 1
+ json_add_boolean ipv6_first "$ipv6_first"
+ json_add_boolean fast_open "$fast_open"
+ json_add_boolean reuse_port "$reuse_port"
+ [ -z "$local_address" ] || json_add_string local_address "$local_address"
+ [ -z "$local_port" ] || json_add_int local_port "$local_port"
+ [ -z "$mode" ] || json_add_string mode "$mode"
+ [ -z "$mtu" ] || json_add_int mtu "$mtu"
+ [ -z "$timeout" ] || json_add_int timeout "$timeout"
+ [ -z "$user" ] || json_add_string user "$user"
+ json_dump -i >"$confjson"
+
+ procd_open_instance "$cfgtype.$cfg"
+ procd_set_param command "$bin" -c "$confjson"
+ [ "$verbose" = 0 ] || procd_append_param command -v
+ [ "$no_delay" = 0 ] || procd_append_param command --no-delay
+ [ -z "$bind_address" ] || procd_append_param command -b "$bind_address"
+ procd_set_param file "$confjson"
+ procd_set_param respawn
+ procd_close_instance
+ ss_rules_cb
}
ss_rules_cb() {