You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
7.9 KiB

  1. #!/bin/sh /etc/rc.common
  2. #
  3. # Copyright (C) 2017-2019 Yousong Zhou <yszhou4tech@gmail.com>
  4. #
  5. # This is free software, licensed under the GNU General Public License v3.
  6. # See /LICENSE for more information.
  7. #
  8. USE_PROCD=1
  9. START=99
  10. ss_confdir=/var/etc/shadowsocks-libev
  11. ss_bindir=/usr/bin
  12. ss_mkjson_server_conf() {
  13. local cfgserver
  14. config_get cfgserver "$cfg" server
  15. [ -n "$cfgserver" ] || return 1
  16. eval "$(validate_server_section "$cfg" ss_validate_mklocal)"
  17. validate_server_section "$cfgserver" || return 1
  18. [ "$disabled" = 0 ] || return 1
  19. ss_mkjson_server_conf_ "$cfgserver"
  20. }
  21. ss_mkjson_server_conf_() {
  22. [ -n "$server_port" ] || return 1
  23. [ -z "$server" ] || json_add_string server "$server"
  24. json_add_int server_port "$server_port"
  25. [ -z "$method" ] || json_add_string method "$method"
  26. [ -z "$key" ] || json_add_string key "$key"
  27. [ -z "$password" ] || json_add_string password "$password"
  28. [ -z "$plugin" ] || json_add_string plugin "$plugin"
  29. [ -z "$plugin_opts" ] || json_add_string plugin_opts "$plugin_opts"
  30. }
  31. ss_mkjson_ss_local_conf() {
  32. ss_mkjson_server_conf
  33. }
  34. ss_mkjson_ss_redir_conf() {
  35. ss_mkjson_server_conf
  36. }
  37. ss_mkjson_ss_server_conf() {
  38. ss_mkjson_server_conf_
  39. }
  40. ss_mkjson_ss_tunnel_conf() {
  41. ss_mkjson_server_conf || return 1
  42. [ -n "$tunnel_address" ] || return 1
  43. json_add_string tunnel_address "$tunnel_address"
  44. }
  45. ss_xxx() {
  46. local cfg="$1"
  47. local cfgtype="$2"
  48. local bin="$ss_bindir/${cfgtype/_/-}"
  49. local confjson="$ss_confdir/$cfgtype.$cfg.json"
  50. [ -x "$bin" ] || return
  51. eval "$("validate_${cfgtype}_section" "$cfg" ss_validate_mklocal)"
  52. "validate_${cfgtype}_section" "$cfg" || return
  53. [ "$disabled" = 0 ] || return
  54. json_init
  55. ss_mkjson_${cfgtype}_conf || return
  56. json_add_boolean use_syslog 1
  57. json_add_boolean ipv6_first "$ipv6_first"
  58. json_add_boolean fast_open "$fast_open"
  59. json_add_boolean reuse_port "$reuse_port"
  60. json_add_boolean no_delay "$no_delay"
  61. [ -z "$local_address" ] || json_add_string local_address "$local_address"
  62. [ -z "$local_port" ] || json_add_int local_port "$local_port"
  63. [ -z "$local_ipv4_address" ] || json_add_string local_ipv4_address "$local_ipv4_address"
  64. [ -z "$local_ipv6_address" ] || json_add_string local_ipv6_address "$local_ipv6_address"
  65. [ -z "$mode" ] || json_add_string mode "$mode"
  66. [ -z "$mtu" ] || json_add_int mtu "$mtu"
  67. [ -z "$timeout" ] || json_add_int timeout "$timeout"
  68. [ -z "$user" ] || json_add_string user "$user"
  69. json_dump -i >"$confjson"
  70. procd_open_instance "$cfgtype.$cfg"
  71. procd_set_param command "$bin" -c "$confjson"
  72. [ "$verbose" = 0 ] || procd_append_param command -v
  73. if [ -n "$bind_address" ]; then
  74. echo "$cfgtype $cfg: uci option bind_address deprecated, please switch to local_address" >&2
  75. procd_append_param command -b "$bind_address"
  76. fi
  77. procd_set_param file "$confjson"
  78. procd_set_param respawn
  79. procd_close_instance
  80. ss_rules_cb
  81. }
  82. ss_rules_cb() {
  83. local cfgserver server
  84. if [ "$cfgtype" = ss_redir ]; then
  85. config_get cfgserver "$cfg" server
  86. config_get server "$cfgserver" server
  87. ss_redir_servers="$ss_redir_servers $server"
  88. if [ "$mode" = tcp_only -o "$mode" = "tcp_and_udp" ]; then
  89. eval "ss_rules_redir_tcp_$cfg=$local_port"
  90. fi
  91. if [ "$mode" = udp_only -o "$mode" = "tcp_and_udp" ]; then
  92. eval "ss_rules_redir_udp_$cfg=$local_port"
  93. fi
  94. fi
  95. }
  96. ss_rules() {
  97. local cfg="ss_rules"
  98. local bin="$ss_bindir/ss-rules"
  99. local cfgtype
  100. local local_port_tcp local_port_udp
  101. local args
  102. [ -x "$bin" ] || return 1
  103. "$bin" -f
  104. "$bin" -6 -f
  105. config_get cfgtype "$cfg" TYPE
  106. [ "$cfgtype" = ss_rules ] || return 1
  107. eval "$(validate_ss_rules_section "$cfg" ss_validate_mklocal)"
  108. validate_ss_rules_section "$cfg" || return 1
  109. [ "$disabled" = 0 ] || return 0
  110. eval local_port_tcp="\$ss_rules_redir_tcp_$redir_tcp"
  111. eval local_port_udp="\$ss_rules_redir_udp_$redir_udp"
  112. [ -n "$local_port_tcp" -o -n "$local_port_udp" ] || return 1
  113. ss_redir_servers="$(echo "$ss_redir_servers" | tr ' ' '\n' | sort -u)"
  114. [ "$dst_forward_recentrst" = 0 ] || args="$args --dst-forward-recentrst"
  115. ss_rules_call
  116. ss_rules_call -6
  117. }
  118. ss_rules_call() {
  119. "$bin" "$@" \
  120. -s "$ss_redir_servers" \
  121. -l "$local_port_tcp" \
  122. -L "$local_port_udp" \
  123. --src-default "$src_default" \
  124. --dst-default "$dst_default" \
  125. --local-default "$local_default" \
  126. --dst-bypass-file "$dst_ips_bypass_file" \
  127. --dst-forward-file "$dst_ips_forward_file" \
  128. --dst-bypass "$dst_ips_bypass" \
  129. --dst-forward "$dst_ips_forward" \
  130. --src-bypass "$src_ips_bypass" \
  131. --src-forward "$src_ips_forward" \
  132. --src-checkdst "$src_ips_checkdst" \
  133. --ifnames "$ifnames" \
  134. --ipt-extra "$ipt_args" \
  135. $args \
  136. || "$bin" "$@" -f
  137. }
  138. start_service() {
  139. local cfgtype
  140. mkdir -p "$ss_confdir"
  141. config_load shadowsocks-libev
  142. for cfgtype in ss_local ss_redir ss_server ss_tunnel; do
  143. config_foreach ss_xxx "$cfgtype" "$cfgtype"
  144. done
  145. ss_rules
  146. }
  147. stop_service() {
  148. local bin="$ss_bindir/ss-rules"
  149. [ -x "$bin" ] && {
  150. "$bin" -f
  151. "$bin" -6 -f
  152. }
  153. rm -rf "$ss_confdir"
  154. }
  155. service_triggers() {
  156. procd_add_reload_interface_trigger wan
  157. procd_add_reload_trigger shadowsocks-libev
  158. procd_open_validate
  159. validate_server_section
  160. validate_ss_local_section
  161. validate_ss_redir_section
  162. validate_ss_rules_section
  163. validate_ss_server_section
  164. validate_ss_tunnel_section
  165. procd_close_validate
  166. }
  167. ss_validate_mklocal() {
  168. local tuple opts
  169. shift 2
  170. for tuple in "$@"; do
  171. opts="${tuple%%:*} $opts"
  172. done
  173. [ -z "$opts" ] || echo "local $opts"
  174. }
  175. ss_validate() {
  176. uci_validate_section shadowsocks-libev "$@"
  177. }
  178. validate_common_server_options_() {
  179. local cfgtype="$1"; shift
  180. local cfg="$1"; shift
  181. local func="$1"; shift
  182. local stream_methods='"table", "rc4", "rc4-md5", "aes-128-cfb", "aes-192-cfb", "aes-256-cfb", "aes-128-ctr", "aes-192-ctr", "aes-256-ctr", "bf-cfb", "camellia-128-cfb", "camellia-192-cfb", "camellia-256-cfb", "salsa20", "chacha20", "chacha20-ietf"'
  183. local aead_methods='"aes-128-gcm", "aes-192-gcm", "aes-256-gcm", "chacha20-ietf-poly1305", "xchacha20-ietf-poly1305"'
  184. "${func:-ss_validate}" "$cfgtype" "$cfg" "$@" \
  185. 'disabled:bool:0' \
  186. 'server:host' \
  187. 'server_port:port' \
  188. 'password:string' \
  189. 'key:string' \
  190. "method:or($stream_methods, $aead_methods)" \
  191. 'plugin:string' \
  192. 'plugin_opts:string'
  193. }
  194. validate_common_client_options_() {
  195. validate_common_options_ "$@" \
  196. 'server:uci("shadowsocks-libev", "@server")' \
  197. 'local_address:ipaddr:0.0.0.0' \
  198. 'local_port:port'
  199. }
  200. validate_common_options_() {
  201. local cfgtype="$1"; shift
  202. local cfg="$1"; shift
  203. local func="$1"; shift
  204. "${func:-ss_validate}" "$cfgtype" "$cfg" "$@" \
  205. 'disabled:bool:0' \
  206. 'fast_open:bool:0' \
  207. 'ipv6_first:bool:0' \
  208. 'no_delay:bool:0' \
  209. 'reuse_port:bool:0' \
  210. 'verbose:bool:0' \
  211. 'mode:or("tcp_only", "udp_only", "tcp_and_udp"):tcp_only' \
  212. 'mtu:uinteger' \
  213. 'timeout:uinteger' \
  214. 'user:string'
  215. }
  216. validate_server_section() {
  217. validate_common_server_options_ server "$1" "$2"
  218. }
  219. validate_ss_local_section() {
  220. validate_common_client_options_ ss_local "$1" "$2"
  221. }
  222. validate_ss_redir_section() {
  223. validate_common_client_options_ ss_redir "$1" "$2"
  224. }
  225. validate_ss_rules_section() {
  226. "${2:-ss_validate}" ss_rules "$1" \
  227. 'disabled:bool:0' \
  228. 'redir_tcp:uci("shadowsocks-libev", "@ss_redir")' \
  229. 'redir_udp:uci("shadowsocks-libev", "@ss_redir")' \
  230. 'src_ips_bypass:or(ipaddr,cidr)' \
  231. 'src_ips_forward:or(ipaddr,cidr)' \
  232. 'src_ips_checkdst:or(ipaddr,cidr)' \
  233. 'dst_ips_bypass_file:file' \
  234. 'dst_ips_bypass:or(ipaddr,cidr)' \
  235. 'dst_ips_forward_file:file' \
  236. 'dst_ips_forward:or(ipaddr,cidr)' \
  237. 'src_default:or("bypass", "forward", "checkdst"):checkdst' \
  238. 'dst_default:or("bypass", "forward"):bypass' \
  239. 'local_default:or("bypass", "forward", "checkdst"):bypass' \
  240. 'dst_forward_recentrst:bool:0' \
  241. 'ifnames:maxlength(15)' \
  242. 'ipt_args:string'
  243. }
  244. validate_ss_server_section() {
  245. validate_common_server_options_ ss_server "$1" \
  246. validate_common_options_ \
  247. "$2" \
  248. 'local_address:ipaddr' \
  249. 'local_ipv4_address:ip4addr' \
  250. 'local_ipv6_address:ip6addr' \
  251. 'bind_address:ipaddr'
  252. }
  253. validate_ss_tunnel_section() {
  254. validate_common_client_options_ ss_tunnel "$1" \
  255. "$2" \
  256. 'tunnel_address:regex(".+\:[0-9]+")'
  257. }