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.

286 lines
7.4 KiB

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