shadowsocks-libev.init 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #!/bin/sh /etc/rc.common
  2. #
  3. # Copyright (C) 2017-2019 Yousong Zhou <[email protected]>
  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. [ -z "$acl" ] || json_add_string acl "$acl"
  72. json_dump -i >"$confjson"
  73. procd_open_instance "$cfgtype.$cfg"
  74. procd_set_param command "$bin" -c "$confjson"
  75. [ "$verbose" = 0 ] || procd_append_param command -v
  76. if [ -n "$bind_address" ]; then
  77. echo "$cfgtype $cfg: uci option bind_address deprecated, please switch to local_address" >&2
  78. procd_append_param command -b "$bind_address"
  79. fi
  80. procd_set_param file "$confjson"
  81. procd_set_param respawn
  82. procd_close_instance
  83. ss_rules_cb
  84. }
  85. ss_rules_cb() {
  86. local cfgserver server
  87. if [ "$cfgtype" = ss_redir ]; then
  88. config_get cfgserver "$cfg" server
  89. config_get server "$cfgserver" server
  90. ss_redir_servers="$ss_redir_servers $server"
  91. if [ "$mode" = tcp_only -o "$mode" = "tcp_and_udp" ]; then
  92. eval "ss_rules_redir_tcp_$cfg=$local_port"
  93. fi
  94. if [ "$mode" = udp_only -o "$mode" = "tcp_and_udp" ]; then
  95. eval "ss_rules_redir_udp_$cfg=$local_port"
  96. fi
  97. fi
  98. }
  99. ss_rules_nft_gen() {
  100. local cfg="ss_rules"
  101. local cfgtype
  102. local local_port_tcp local_port_udp
  103. local remote_servers
  104. [ -s "$ssrules_uc" ] || return 1
  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 2
  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. remote_servers="$(echo $ss_redir_servers \
  114. | tr ' ' '\n' \
  115. | sort -u \
  116. | xargs -n 1 resolveip \
  117. | sort -u)"
  118. local tmp="/tmp/ssrules"
  119. json_init
  120. json_add_string o_remote_servers "$remote_servers"
  121. json_add_int o_redir_tcp_port "$local_port_tcp"
  122. json_add_int o_redir_udp_port "$local_port_udp"
  123. json_add_string o_ifnames "$ifnames"
  124. json_add_string o_local_default "$local_default"
  125. json_add_string o_src_bypass "$src_ips_bypass"
  126. json_add_string o_src_forward "$src_ips_forward"
  127. json_add_string o_src_checkdst "$src_ips_checkdst"
  128. json_add_string o_src_default "$src_default"
  129. json_add_string o_dst_bypass "$dst_ips_bypass"
  130. json_add_string o_dst_forward "$dst_ips_forward"
  131. json_add_string o_dst_bypass_file "$dst_ips_bypass_file"
  132. json_add_string o_dst_forward_file "$dst_ips_forward_file"
  133. json_add_string o_dst_default "$dst_default"
  134. json_add_string o_nft_tcp_extra "$nft_tcp_extra"
  135. json_add_string o_nft_udp_extra "$nft_udp_extra"
  136. json_dump -i >"$tmp.json"
  137. if utpl -S -F "$tmp.json" "$ssrules_uc" >"$tmp.nft" \
  138. && ! cmp -s "$tmp.nft" "$ssrules_nft"; then
  139. echo "table inet chk {include \"$tmp.nft\";}" >"$tmp.nft.chk"
  140. if nft -f "$tmp.nft.chk" -c; then
  141. mv "$tmp.nft" "$ssrules_nft"
  142. fw4 restart
  143. fi
  144. rm -f "$tmp.nft.chk"
  145. fi
  146. rm -f "$tmp.json"
  147. rm -f "$tmp.nft"
  148. }
  149. ss_rules_nft_reset() {
  150. if [ -f "$ssrules_nft" ]; then
  151. rm -f "$ssrules_nft"
  152. fw4 restart
  153. fi
  154. }
  155. ss_rules() {
  156. if ! ss_rules_nft_gen; then
  157. ss_rules_nft_reset
  158. fi
  159. }
  160. start_service() {
  161. local cfgtype
  162. mkdir -p "$ss_confdir"
  163. config_load shadowsocks-libev
  164. for cfgtype in ss_local ss_redir ss_server ss_tunnel; do
  165. config_foreach ss_xxx "$cfgtype" "$cfgtype"
  166. done
  167. ss_rules
  168. }
  169. stop_service() {
  170. ss_rules_nft_reset
  171. rm -rf "$ss_confdir"
  172. }
  173. service_triggers() {
  174. procd_add_reload_interface_trigger wan
  175. procd_add_reload_trigger shadowsocks-libev
  176. procd_open_validate
  177. validate_server_section
  178. validate_ss_local_section
  179. validate_ss_redir_section
  180. validate_ss_rules_section
  181. validate_ss_server_section
  182. validate_ss_tunnel_section
  183. procd_close_validate
  184. }
  185. ss_validate_mklocal() {
  186. local tuple opts
  187. shift 2
  188. for tuple in "$@"; do
  189. opts="${tuple%%:*} $opts"
  190. done
  191. [ -z "$opts" ] || echo "local $opts"
  192. }
  193. ss_validate() {
  194. uci_validate_section shadowsocks-libev "$@"
  195. }
  196. validate_common_server_options_() {
  197. local cfgtype="$1"; shift
  198. local cfg="$1"; shift
  199. local func="$1"; shift
  200. 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"'
  201. local aead_methods='"aes-128-gcm", "aes-192-gcm", "aes-256-gcm", "chacha20-ietf-poly1305", "xchacha20-ietf-poly1305"'
  202. "${func:-ss_validate}" "$cfgtype" "$cfg" "$@" \
  203. 'disabled:bool:0' \
  204. 'server:host' \
  205. 'server_port:port' \
  206. 'password:string' \
  207. 'key:string' \
  208. "method:or($stream_methods, $aead_methods)" \
  209. 'plugin:string' \
  210. 'plugin_opts:string'
  211. }
  212. validate_common_client_options_() {
  213. validate_common_options_ "$@" \
  214. 'server:uci("shadowsocks-libev", "@server")' \
  215. 'local_address:ipaddr:0.0.0.0' \
  216. 'local_port:port'
  217. }
  218. validate_common_options_() {
  219. local cfgtype="$1"; shift
  220. local cfg="$1"; shift
  221. local func="$1"; shift
  222. "${func:-ss_validate}" "$cfgtype" "$cfg" "$@" \
  223. 'disabled:bool:0' \
  224. 'fast_open:bool:0' \
  225. 'ipv6_first:bool:0' \
  226. 'no_delay:bool:0' \
  227. 'reuse_port:bool:0' \
  228. 'verbose:bool:0' \
  229. 'mode:or("tcp_only", "udp_only", "tcp_and_udp"):tcp_only' \
  230. 'mtu:uinteger' \
  231. 'timeout:uinteger' \
  232. 'user:string'
  233. }
  234. validate_server_section() {
  235. validate_common_server_options_ server "$1" "$2"
  236. }
  237. validate_ss_local_section() {
  238. validate_common_client_options_ ss_local "$1" "$2" \
  239. 'acl:file'
  240. }
  241. validate_ss_redir_section() {
  242. validate_common_client_options_ ss_redir "$1" "$2"
  243. }
  244. validate_ss_rules_section() {
  245. "${2:-ss_validate}" ss_rules "$1" \
  246. 'disabled:bool:0' \
  247. 'redir_tcp:uci("shadowsocks-libev", "@ss_redir")' \
  248. 'redir_udp:uci("shadowsocks-libev", "@ss_redir")' \
  249. 'src_ips_bypass:or(ipaddr,cidr)' \
  250. 'src_ips_forward:or(ipaddr,cidr)' \
  251. 'src_ips_checkdst:or(ipaddr,cidr)' \
  252. 'dst_ips_bypass_file:file' \
  253. 'dst_ips_bypass:or(ipaddr,cidr)' \
  254. 'dst_ips_forward_file:file' \
  255. 'dst_ips_forward:or(ipaddr,cidr)' \
  256. 'src_default:or("bypass", "forward", "checkdst"):checkdst' \
  257. 'dst_default:or("bypass", "forward"):bypass' \
  258. 'local_default:or("bypass", "forward", "checkdst"):bypass' \
  259. 'nft_tcp_extra:string' \
  260. 'nft_udp_extra:string' \
  261. 'ifnames:maxlength(15)'
  262. }
  263. validate_ss_server_section() {
  264. validate_common_server_options_ ss_server "$1" \
  265. validate_common_options_ \
  266. "$2" \
  267. 'local_address:ipaddr' \
  268. 'local_ipv4_address:ip4addr' \
  269. 'local_ipv6_address:ip6addr' \
  270. 'bind_address:ipaddr' \
  271. 'acl:file'
  272. }
  273. validate_ss_tunnel_section() {
  274. validate_common_client_options_ ss_tunnel "$1" \
  275. "$2" \
  276. 'tunnel_address:regex(".+\:[0-9]+")'
  277. }