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.

307 lines
8.5 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. ssrules_uc="/usr/share/ss-rules/ss-rules.uc"
  13. ssrules_nft="/etc/nftables.d/90-ss-rules.nft"
  14. ss_mkjson_server_conf() {
  15. local cfgserver
  16. config_get cfgserver "$cfg" server
  17. [ -n "$cfgserver" ] || return 1
  18. eval "$(validate_server_section "$cfg" ss_validate_mklocal)"
  19. validate_server_section "$cfgserver" || return 1
  20. [ "$disabled" = 0 ] || return 1
  21. ss_mkjson_server_conf_ "$cfgserver"
  22. }
  23. ss_mkjson_server_conf_() {
  24. [ -n "$server_port" ] || return 1
  25. [ -z "$server" ] || json_add_string server "$server"
  26. json_add_int server_port "$server_port"
  27. [ -z "$method" ] || json_add_string method "$method"
  28. [ -z "$key" ] || json_add_string key "$key"
  29. [ -z "$password" ] || json_add_string password "$password"
  30. [ -z "$plugin" ] || json_add_string plugin "$plugin"
  31. [ -z "$plugin_opts" ] || json_add_string plugin_opts "$plugin_opts"
  32. }
  33. ss_mkjson_ss_local_conf() {
  34. ss_mkjson_server_conf
  35. }
  36. ss_mkjson_ss_redir_conf() {
  37. ss_mkjson_server_conf
  38. }
  39. ss_mkjson_ss_server_conf() {
  40. ss_mkjson_server_conf_
  41. }
  42. ss_mkjson_ss_tunnel_conf() {
  43. ss_mkjson_server_conf || return 1
  44. [ -n "$tunnel_address" ] || return 1
  45. json_add_string tunnel_address "$tunnel_address"
  46. }
  47. ss_xxx() {
  48. local cfg="$1"
  49. local cfgtype="$2"
  50. local bin="$ss_bindir/${cfgtype/_/-}"
  51. local confjson="$ss_confdir/$cfgtype.$cfg.json"
  52. [ -x "$bin" ] || return
  53. eval "$("validate_${cfgtype}_section" "$cfg" ss_validate_mklocal)"
  54. "validate_${cfgtype}_section" "$cfg" || return
  55. [ "$disabled" = 0 ] || return
  56. json_init
  57. ss_mkjson_${cfgtype}_conf || return
  58. json_add_boolean use_syslog 1
  59. json_add_boolean ipv6_first "$ipv6_first"
  60. json_add_boolean fast_open "$fast_open"
  61. json_add_boolean reuse_port "$reuse_port"
  62. json_add_boolean no_delay "$no_delay"
  63. [ -z "$local_address" ] || json_add_string local_address "$local_address"
  64. [ -z "$local_port" ] || json_add_int local_port "$local_port"
  65. [ -z "$local_ipv4_address" ] || json_add_string local_ipv4_address "$local_ipv4_address"
  66. [ -z "$local_ipv6_address" ] || json_add_string local_ipv6_address "$local_ipv6_address"
  67. [ -z "$mode" ] || json_add_string mode "$mode"
  68. [ -z "$mtu" ] || json_add_int mtu "$mtu"
  69. [ -z "$timeout" ] || json_add_int timeout "$timeout"
  70. [ -z "$user" ] || json_add_string user "$user"
  71. json_dump -i >"$confjson"
  72. procd_open_instance "$cfgtype.$cfg"
  73. procd_set_param command "$bin" -c "$confjson"
  74. [ "$verbose" = 0 ] || procd_append_param command -v
  75. if [ -n "$bind_address" ]; then
  76. echo "$cfgtype $cfg: uci option bind_address deprecated, please switch to local_address" >&2
  77. procd_append_param command -b "$bind_address"
  78. fi
  79. procd_set_param file "$confjson"
  80. procd_set_param respawn
  81. procd_close_instance
  82. ss_rules_cb
  83. }
  84. ss_rules_cb() {
  85. local cfgserver server
  86. if [ "$cfgtype" = ss_redir ]; then
  87. config_get cfgserver "$cfg" server
  88. config_get server "$cfgserver" server
  89. ss_redir_servers="$ss_redir_servers $server"
  90. if [ "$mode" = tcp_only -o "$mode" = "tcp_and_udp" ]; then
  91. eval "ss_rules_redir_tcp_$cfg=$local_port"
  92. fi
  93. if [ "$mode" = udp_only -o "$mode" = "tcp_and_udp" ]; then
  94. eval "ss_rules_redir_udp_$cfg=$local_port"
  95. fi
  96. fi
  97. }
  98. ss_rules() {
  99. local cfg="ss_rules"
  100. local cfgtype
  101. local local_port_tcp local_port_udp
  102. local remote_servers
  103. [ -s "$ssrules_uc" ] || return 1
  104. config_get cfgtype "$cfg" TYPE
  105. [ "$cfgtype" = ss_rules ] || return 1
  106. eval "$(validate_ss_rules_section "$cfg" ss_validate_mklocal)"
  107. validate_ss_rules_section "$cfg" || return 1
  108. [ "$disabled" = 0 ] || return 0
  109. eval local_port_tcp="\$ss_rules_redir_tcp_$redir_tcp"
  110. eval local_port_udp="\$ss_rules_redir_udp_$redir_udp"
  111. [ -n "$local_port_tcp" -o -n "$local_port_udp" ] || return 1
  112. remote_servers="$(echo $ss_redir_servers \
  113. | tr ' ' '\n' \
  114. | sort -u \
  115. | xargs -n 1 resolveip \
  116. | sort -u)"
  117. local tmp="/tmp/ssrules"
  118. json_init
  119. json_add_string o_remote_servers "$remote_servers"
  120. json_add_int o_redir_tcp_port "$local_port_tcp"
  121. json_add_int o_redir_udp_port "$local_port_udp"
  122. json_add_string o_ifnames "$ifnames"
  123. json_add_string o_local_default "$local_default"
  124. json_add_string o_src_bypass "$src_ips_bypass"
  125. json_add_string o_src_forward "$src_ips_forward"
  126. json_add_string o_src_checkdst "$src_ips_checkdst"
  127. json_add_string o_src_default "$src_default"
  128. json_add_string o_dst_bypass "$dst_ips_bypass"
  129. json_add_string o_dst_forward "$dst_ips_forward"
  130. json_add_string o_dst_bypass_file "$dst_ips_bypass_file"
  131. json_add_string o_dst_forward_file "$dst_ips_forward_file"
  132. json_add_string o_dst_default "$dst_default"
  133. json_add_string o_nft_tcp_extra "$nft_tcp_extra"
  134. json_add_string o_nft_udp_extra "$nft_udp_extra"
  135. json_dump -i >"$tmp.json"
  136. if ucode -S -i "$ssrules_uc" -E "$tmp.json" >"$tmp.nft" \
  137. && ! cmp -s "$tmp.nft" "$ssrules_nft"; then
  138. echo "table inet chk {include \"$tmp.nft\";}" >"$tmp.nft.chk"
  139. if nft -f "$tmp.nft.chk" -c; then
  140. mv "$tmp.nft" "$ssrules_nft"
  141. fw4 reload
  142. fi
  143. rm -f "$tmp.nft.chk"
  144. fi
  145. rm -f "$tmp.json"
  146. rm -f "$tmp.nft"
  147. }
  148. start_service() {
  149. local cfgtype
  150. mkdir -p "$ss_confdir"
  151. config_load shadowsocks-libev
  152. for cfgtype in ss_local ss_redir ss_server ss_tunnel; do
  153. config_foreach ss_xxx "$cfgtype" "$cfgtype"
  154. done
  155. ss_rules
  156. }
  157. stop_service() {
  158. if [ -f "$ssrules_nft" ]; then
  159. rm -f "$ssrules_nft"
  160. fw4 reload
  161. fi
  162. rm -rf "$ss_confdir"
  163. }
  164. service_triggers() {
  165. procd_add_reload_interface_trigger wan
  166. procd_add_reload_trigger shadowsocks-libev
  167. procd_open_validate
  168. validate_server_section
  169. validate_ss_local_section
  170. validate_ss_redir_section
  171. validate_ss_rules_section
  172. validate_ss_server_section
  173. validate_ss_tunnel_section
  174. procd_close_validate
  175. }
  176. ss_validate_mklocal() {
  177. local tuple opts
  178. shift 2
  179. for tuple in "$@"; do
  180. opts="${tuple%%:*} $opts"
  181. done
  182. [ -z "$opts" ] || echo "local $opts"
  183. }
  184. ss_validate() {
  185. uci_validate_section shadowsocks-libev "$@"
  186. }
  187. validate_common_server_options_() {
  188. local cfgtype="$1"; shift
  189. local cfg="$1"; shift
  190. local func="$1"; shift
  191. 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"'
  192. local aead_methods='"aes-128-gcm", "aes-192-gcm", "aes-256-gcm", "chacha20-ietf-poly1305", "xchacha20-ietf-poly1305"'
  193. "${func:-ss_validate}" "$cfgtype" "$cfg" "$@" \
  194. 'disabled:bool:0' \
  195. 'server:host' \
  196. 'server_port:port' \
  197. 'password:string' \
  198. 'key:string' \
  199. "method:or($stream_methods, $aead_methods)" \
  200. 'plugin:string' \
  201. 'plugin_opts:string'
  202. }
  203. validate_common_client_options_() {
  204. validate_common_options_ "$@" \
  205. 'server:uci("shadowsocks-libev", "@server")' \
  206. 'local_address:ipaddr:0.0.0.0' \
  207. 'local_port:port'
  208. }
  209. validate_common_options_() {
  210. local cfgtype="$1"; shift
  211. local cfg="$1"; shift
  212. local func="$1"; shift
  213. "${func:-ss_validate}" "$cfgtype" "$cfg" "$@" \
  214. 'disabled:bool:0' \
  215. 'fast_open:bool:0' \
  216. 'ipv6_first:bool:0' \
  217. 'no_delay:bool:0' \
  218. 'reuse_port:bool:0' \
  219. 'verbose:bool:0' \
  220. 'mode:or("tcp_only", "udp_only", "tcp_and_udp"):tcp_only' \
  221. 'mtu:uinteger' \
  222. 'timeout:uinteger' \
  223. 'user:string'
  224. }
  225. validate_server_section() {
  226. validate_common_server_options_ server "$1" "$2"
  227. }
  228. validate_ss_local_section() {
  229. validate_common_client_options_ ss_local "$1" "$2"
  230. }
  231. validate_ss_redir_section() {
  232. validate_common_client_options_ ss_redir "$1" "$2"
  233. }
  234. validate_ss_rules_section() {
  235. "${2:-ss_validate}" ss_rules "$1" \
  236. 'disabled:bool:0' \
  237. 'redir_tcp:uci("shadowsocks-libev", "@ss_redir")' \
  238. 'redir_udp:uci("shadowsocks-libev", "@ss_redir")' \
  239. 'src_ips_bypass:or(ipaddr,cidr)' \
  240. 'src_ips_forward:or(ipaddr,cidr)' \
  241. 'src_ips_checkdst:or(ipaddr,cidr)' \
  242. 'dst_ips_bypass_file:file' \
  243. 'dst_ips_bypass:or(ipaddr,cidr)' \
  244. 'dst_ips_forward_file:file' \
  245. 'dst_ips_forward:or(ipaddr,cidr)' \
  246. 'src_default:or("bypass", "forward", "checkdst"):checkdst' \
  247. 'dst_default:or("bypass", "forward"):bypass' \
  248. 'local_default:or("bypass", "forward", "checkdst"):bypass' \
  249. 'nft_tcp_extra:string' \
  250. 'nft_udp_extra:string' \
  251. 'ifnames:maxlength(15)'
  252. }
  253. validate_ss_server_section() {
  254. validate_common_server_options_ ss_server "$1" \
  255. validate_common_options_ \
  256. "$2" \
  257. 'local_address:ipaddr' \
  258. 'local_ipv4_address:ip4addr' \
  259. 'local_ipv6_address:ip6addr' \
  260. 'bind_address:ipaddr'
  261. }
  262. validate_ss_tunnel_section() {
  263. validate_common_client_options_ ss_tunnel "$1" \
  264. "$2" \
  265. 'tunnel_address:regex(".+\:[0-9]+")'
  266. }