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