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.

290 lines
7.6 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 "$mode" ] || json_add_string mode "$mode"
  64. [ -z "$mtu" ] || json_add_int mtu "$mtu"
  65. [ -z "$timeout" ] || json_add_int timeout "$timeout"
  66. [ -z "$user" ] || json_add_string user "$user"
  67. json_dump -i >"$confjson"
  68. procd_open_instance "$cfgtype.$cfg"
  69. procd_set_param command "$bin" -c "$confjson"
  70. [ "$verbose" = 0 ] || procd_append_param command -v
  71. [ -z "$bind_address" ] || procd_append_param command -b "$bind_address"
  72. procd_set_param file "$confjson"
  73. procd_set_param respawn
  74. procd_close_instance
  75. ss_rules_cb
  76. }
  77. ss_rules_cb() {
  78. local cfgserver server
  79. if [ "$cfgtype" = ss_redir ]; then
  80. config_get cfgserver "$cfg" server
  81. config_get server "$cfgserver" server
  82. ss_redir_servers="$ss_redir_servers $server"
  83. if [ "$mode" = tcp_only -o "$mode" = "tcp_and_udp" ]; then
  84. eval "ss_rules_redir_tcp_$cfg=$local_port"
  85. fi
  86. if [ "$mode" = udp_only -o "$mode" = "tcp_and_udp" ]; then
  87. eval "ss_rules_redir_udp_$cfg=$local_port"
  88. fi
  89. fi
  90. }
  91. ss_rules() {
  92. local cfg="ss_rules"
  93. local bin="$ss_bindir/ss-rules"
  94. local cfgtype
  95. local local_port_tcp local_port_udp
  96. local args
  97. [ -x "$bin" ] || return 1
  98. "$bin" -f
  99. "$bin" -6 -f
  100. config_get cfgtype "$cfg" TYPE
  101. [ "$cfgtype" = ss_rules ] || return 1
  102. eval "$(validate_ss_rules_section "$cfg" ss_validate_mklocal)"
  103. validate_ss_rules_section "$cfg" || return 1
  104. [ "$disabled" = 0 ] || return 0
  105. eval local_port_tcp="\$ss_rules_redir_tcp_$redir_tcp"
  106. eval local_port_udp="\$ss_rules_redir_udp_$redir_udp"
  107. [ -n "$local_port_tcp" -o -n "$local_port_udp" ] || return 1
  108. ss_redir_servers="$(echo "$ss_redir_servers" | tr ' ' '\n' | sort -u)"
  109. [ "$dst_forward_recentrst" = 0 ] || args="$args --dst-forward-recentrst"
  110. ss_rules_call
  111. ss_rules_call -6
  112. }
  113. ss_rules_call() {
  114. "$bin" "$@" \
  115. -s "$ss_redir_servers" \
  116. -l "$local_port_tcp" \
  117. -L "$local_port_udp" \
  118. --src-default "$src_default" \
  119. --dst-default "$dst_default" \
  120. --local-default "$local_default" \
  121. --dst-bypass-file "$dst_ips_bypass_file" \
  122. --dst-forward-file "$dst_ips_forward_file" \
  123. --dst-bypass "$dst_ips_bypass" \
  124. --dst-forward "$dst_ips_forward" \
  125. --src-bypass "$src_ips_bypass" \
  126. --src-forward "$src_ips_forward" \
  127. --src-checkdst "$src_ips_checkdst" \
  128. --ifnames "$ifnames" \
  129. --ipt-extra "$ipt_args" \
  130. $args \
  131. || "$bin" "$@" -f
  132. }
  133. start_service() {
  134. local cfgtype
  135. mkdir -p "$ss_confdir"
  136. config_load shadowsocks-libev
  137. for cfgtype in ss_local ss_redir ss_server ss_tunnel; do
  138. config_foreach ss_xxx "$cfgtype" "$cfgtype"
  139. done
  140. ss_rules
  141. }
  142. stop_service() {
  143. local bin="$ss_bindir/ss-rules"
  144. [ -x "$bin" ] && {
  145. "$bin" -f
  146. "$bin" -6 -f
  147. }
  148. rm -rf "$ss_confdir"
  149. }
  150. service_triggers() {
  151. procd_add_reload_interface_trigger wan
  152. procd_add_reload_trigger shadowsocks-libev
  153. procd_open_validate
  154. validate_server_section
  155. validate_ss_local_section
  156. validate_ss_redir_section
  157. validate_ss_rules_section
  158. validate_ss_server_section
  159. validate_ss_tunnel_section
  160. procd_close_validate
  161. }
  162. ss_validate_mklocal() {
  163. local tuple opts
  164. shift 2
  165. for tuple in "$@"; do
  166. opts="${tuple%%:*} $opts"
  167. done
  168. [ -z "$opts" ] || echo "local $opts"
  169. }
  170. ss_validate() {
  171. uci_validate_section shadowsocks-libev "$@"
  172. }
  173. validate_common_server_options_() {
  174. local cfgtype="$1"; shift
  175. local cfg="$1"; shift
  176. local func="$1"; shift
  177. 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"'
  178. local aead_methods='"aes-128-gcm", "aes-192-gcm", "aes-256-gcm", "chacha20-ietf-poly1305", "xchacha20-ietf-poly1305"'
  179. "${func:-ss_validate}" "$cfgtype" "$cfg" "$@" \
  180. 'disabled:bool:0' \
  181. 'server:host' \
  182. 'server_port:port' \
  183. 'password:string' \
  184. 'key:string' \
  185. "method:or($stream_methods, $aead_methods)" \
  186. 'plugin:string' \
  187. 'plugin_opts:string'
  188. }
  189. validate_common_client_options_() {
  190. validate_common_options_ "$@" \
  191. 'server:uci("shadowsocks-libev", "@server")' \
  192. 'local_address:host:0.0.0.0' \
  193. 'local_port:port'
  194. }
  195. validate_common_options_() {
  196. local cfgtype="$1"; shift
  197. local cfg="$1"; shift
  198. local func="$1"; shift
  199. "${func:-ss_validate}" "$cfgtype" "$cfg" "$@" \
  200. 'disabled:bool:0' \
  201. 'fast_open:bool:0' \
  202. 'ipv6_first:bool:0' \
  203. 'no_delay:bool:0' \
  204. 'reuse_port:bool:0' \
  205. 'verbose:bool:0' \
  206. 'mode:or("tcp_only", "udp_only", "tcp_and_udp"):tcp_only' \
  207. 'mtu:uinteger' \
  208. 'timeout:uinteger' \
  209. 'user:string'
  210. }
  211. validate_server_section() {
  212. validate_common_server_options_ server "$1" "$2"
  213. }
  214. validate_ss_local_section() {
  215. validate_common_client_options_ ss_local "$1" "$2"
  216. }
  217. validate_ss_redir_section() {
  218. validate_common_client_options_ ss_redir "$1" "$2"
  219. }
  220. validate_ss_rules_section() {
  221. "${2:-ss_validate}" ss_rules "$1" \
  222. 'disabled:bool:0' \
  223. 'redir_tcp:uci("shadowsocks-libev", "@ss_redir")' \
  224. 'redir_udp:uci("shadowsocks-libev", "@ss_redir")' \
  225. 'src_ips_bypass:or(ipaddr,cidr)' \
  226. 'src_ips_forward:or(ipaddr,cidr)' \
  227. 'src_ips_checkdst:or(ipaddr,cidr)' \
  228. 'dst_ips_bypass_file:file' \
  229. 'dst_ips_bypass:or(ipaddr,cidr)' \
  230. 'dst_ips_forward_file:file' \
  231. 'dst_ips_forward:or(ipaddr,cidr)' \
  232. 'src_default:or("bypass", "forward", "checkdst"):checkdst' \
  233. 'dst_default:or("bypass", "forward"):bypass' \
  234. 'local_default:or("bypass", "forward", "checkdst"):bypass' \
  235. 'dst_forward_recentrst:bool:0' \
  236. 'ifnames:maxlength(15)' \
  237. 'ipt_args:string'
  238. }
  239. validate_ss_server_section() {
  240. validate_common_server_options_ ss_server "$1" \
  241. validate_common_options_ \
  242. "$2" \
  243. 'bind_address:ipaddr'
  244. }
  245. validate_ss_tunnel_section() {
  246. validate_common_client_options_ ss_tunnel "$1" \
  247. "$2" \
  248. 'tunnel_address:regex(".+\:[0-9]+")'
  249. }