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.

317 lines
8.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. 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_nft_gen() {
  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 2
  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 utpl -S -F "$tmp.json" "$ssrules_uc" >"$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 restart
  142. fi
  143. rm -f "$tmp.nft.chk"
  144. fi
  145. rm -f "$tmp.json"
  146. rm -f "$tmp.nft"
  147. }
  148. ss_rules_nft_reset() {
  149. if [ -f "$ssrules_nft" ]; then
  150. rm -f "$ssrules_nft"
  151. fw4 restart
  152. fi
  153. }
  154. ss_rules() {
  155. if ! ss_rules_nft_gen; then
  156. ss_rules_nft_reset
  157. fi
  158. }
  159. start_service() {
  160. local cfgtype
  161. mkdir -p "$ss_confdir"
  162. config_load shadowsocks-libev
  163. for cfgtype in ss_local ss_redir ss_server ss_tunnel; do
  164. config_foreach ss_xxx "$cfgtype" "$cfgtype"
  165. done
  166. ss_rules
  167. }
  168. stop_service() {
  169. ss_rules_nft_reset
  170. rm -rf "$ss_confdir"
  171. }
  172. service_triggers() {
  173. procd_add_reload_interface_trigger wan
  174. procd_add_reload_trigger shadowsocks-libev
  175. procd_open_validate
  176. validate_server_section
  177. validate_ss_local_section
  178. validate_ss_redir_section
  179. validate_ss_rules_section
  180. validate_ss_server_section
  181. validate_ss_tunnel_section
  182. procd_close_validate
  183. }
  184. ss_validate_mklocal() {
  185. local tuple opts
  186. shift 2
  187. for tuple in "$@"; do
  188. opts="${tuple%%:*} $opts"
  189. done
  190. [ -z "$opts" ] || echo "local $opts"
  191. }
  192. ss_validate() {
  193. uci_validate_section shadowsocks-libev "$@"
  194. }
  195. validate_common_server_options_() {
  196. local cfgtype="$1"; shift
  197. local cfg="$1"; shift
  198. local func="$1"; shift
  199. 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"'
  200. local aead_methods='"aes-128-gcm", "aes-192-gcm", "aes-256-gcm", "chacha20-ietf-poly1305", "xchacha20-ietf-poly1305"'
  201. "${func:-ss_validate}" "$cfgtype" "$cfg" "$@" \
  202. 'disabled:bool:0' \
  203. 'server:host' \
  204. 'server_port:port' \
  205. 'password:string' \
  206. 'key:string' \
  207. "method:or($stream_methods, $aead_methods)" \
  208. 'plugin:string' \
  209. 'plugin_opts:string'
  210. }
  211. validate_common_client_options_() {
  212. validate_common_options_ "$@" \
  213. 'server:uci("shadowsocks-libev", "@server")' \
  214. 'local_address:ipaddr:0.0.0.0' \
  215. 'local_port:port'
  216. }
  217. validate_common_options_() {
  218. local cfgtype="$1"; shift
  219. local cfg="$1"; shift
  220. local func="$1"; shift
  221. "${func:-ss_validate}" "$cfgtype" "$cfg" "$@" \
  222. 'disabled:bool:0' \
  223. 'fast_open:bool:0' \
  224. 'ipv6_first:bool:0' \
  225. 'no_delay:bool:0' \
  226. 'reuse_port:bool:0' \
  227. 'verbose:bool:0' \
  228. 'mode:or("tcp_only", "udp_only", "tcp_and_udp"):tcp_only' \
  229. 'mtu:uinteger' \
  230. 'timeout:uinteger' \
  231. 'user:string'
  232. }
  233. validate_server_section() {
  234. validate_common_server_options_ server "$1" "$2"
  235. }
  236. validate_ss_local_section() {
  237. validate_common_client_options_ ss_local "$1" "$2"
  238. }
  239. validate_ss_redir_section() {
  240. validate_common_client_options_ ss_redir "$1" "$2"
  241. }
  242. validate_ss_rules_section() {
  243. "${2:-ss_validate}" ss_rules "$1" \
  244. 'disabled:bool:0' \
  245. 'redir_tcp:uci("shadowsocks-libev", "@ss_redir")' \
  246. 'redir_udp:uci("shadowsocks-libev", "@ss_redir")' \
  247. 'src_ips_bypass:or(ipaddr,cidr)' \
  248. 'src_ips_forward:or(ipaddr,cidr)' \
  249. 'src_ips_checkdst:or(ipaddr,cidr)' \
  250. 'dst_ips_bypass_file:file' \
  251. 'dst_ips_bypass:or(ipaddr,cidr)' \
  252. 'dst_ips_forward_file:file' \
  253. 'dst_ips_forward:or(ipaddr,cidr)' \
  254. 'src_default:or("bypass", "forward", "checkdst"):checkdst' \
  255. 'dst_default:or("bypass", "forward"):bypass' \
  256. 'local_default:or("bypass", "forward", "checkdst"):bypass' \
  257. 'nft_tcp_extra:string' \
  258. 'nft_udp_extra:string' \
  259. 'ifnames:maxlength(15)'
  260. }
  261. validate_ss_server_section() {
  262. validate_common_server_options_ ss_server "$1" \
  263. validate_common_options_ \
  264. "$2" \
  265. 'local_address:ipaddr' \
  266. 'local_ipv4_address:ip4addr' \
  267. 'local_ipv6_address:ip6addr' \
  268. 'bind_address:ipaddr'
  269. }
  270. validate_ss_tunnel_section() {
  271. validate_common_client_options_ ss_tunnel "$1" \
  272. "$2" \
  273. 'tunnel_address:regex(".+\:[0-9]+")'
  274. }