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.

326 lines
8.0 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"
  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
  114. local server
  115. [ "$cfgtype" != ss_server ] || return
  116. config_get cfgserver "$cfg" server
  117. config_get server "$cfgserver" server
  118. ss_rules_servers="$ss_rules_servers $server"
  119. if [ "$cfgtype" = ss_redir ]; then
  120. if [ "$mode" = tcp_only -o "$mode" = "tcp_and_udp" ]; then
  121. eval "ss_rules_redir_tcp_$cfg=$local_port"
  122. fi
  123. if [ "$mode" = udp_only -o "$mode" = "tcp_and_udp" ]; then
  124. eval "ss_rules_redir_udp_$cfg=$local_port"
  125. eval "ss_rules_redir_server_udp_$cfg=$server"
  126. fi
  127. fi
  128. }
  129. ss_rules() {
  130. local cfg="ss_rules"
  131. local bin="$ss_bindir/ss-rules"
  132. local cfgtype
  133. local args local_port_tcp local_port_udp server_udp
  134. local i a_args d_args
  135. [ -x "$bin" ] || return 1
  136. config_get cfgtype "$cfg" TYPE
  137. [ "$cfgtype" = ss_rules ] || return 1
  138. eval "$(validate_ss_rules_section "$cfg" ss_validate_mklocal)"
  139. validate_ss_rules_section "$cfg"
  140. [ "$disabled" = 0 ] || return 1
  141. eval local_port_tcp="\$ss_rules_redir_tcp_$redir_tcp"
  142. eval local_port_udp="\$ss_rules_redir_udp_$redir_udp"
  143. eval server_udp="\$ss_rules_redir_server_udp_$redir_udp"
  144. [ -z "$local_port_udp" ] || args="$args -U"
  145. case "$local_default" in
  146. forward) args="$args -O" ;;
  147. checkdst) args="$args -o" ;;
  148. esac
  149. case "$src_default" in
  150. bypass) d_args=RETURN ;;
  151. forward) d_args=SS_SPEC_WAN_FW ;;
  152. checkdst) d_args=SS_SPEC_WAN_AC ;;
  153. esac
  154. ss_rules_servers="$(echo "$ss_rules_servers" | tr ' ' '\n' | sort -u)"
  155. for i in $src_ips_bypass; do a_args="b,$i $a_args"; done
  156. for i in $src_ips_forward; do a_args="g,$i $a_args"; done
  157. for i in $src_ips_checkdst; do a_args="n,$i $a_args"; done
  158. "$bin" \
  159. -s "$ss_rules_servers" \
  160. -l "$local_port_tcp" \
  161. -S "$server_udp" \
  162. -L "$local_port_udp" \
  163. -B "$dst_ips_bypass_file" \
  164. -W "$dst_ips_forward_file" \
  165. -b "$dst_ips_bypass" \
  166. -w "$dst_ips_forward" \
  167. -e "$ipt_args" \
  168. -a "$a_args" \
  169. -d "$d_args" \
  170. $args \
  171. || "$bin" -f
  172. }
  173. start_service() {
  174. local cfgtype="$1"
  175. mkdir -p "$ss_confdir"
  176. config_load shadowsocks-libev
  177. for cfgtype in ss_local ss_redir ss_server ss_tunnel; do
  178. config_foreach ss_xxx "$cfgtype" "$cfgtype"
  179. done
  180. ss_rules
  181. }
  182. stop_service() {
  183. local bin="$ss_bindir/ss-rules"
  184. [ -x "$bin" ] && "$bin" -f
  185. rm -rf "$ss_confdir"
  186. }
  187. service_triggers() {
  188. procd_add_reload_interface_trigger wan
  189. procd_add_reload_trigger shadowsocks-libev
  190. procd_open_validate
  191. validate_server_section
  192. validate_ss_local_section
  193. validate_ss_redir_section
  194. validate_ss_rules_section
  195. validate_ss_server_section
  196. validate_ss_tunnel_section
  197. procd_close_validate
  198. }
  199. ss_validate_mklocal() {
  200. local tuple opts
  201. shift 2
  202. for tuple in "$@"; do
  203. opts="${tuple%%:*} $opts"
  204. done
  205. [ -z "$opts" ] || echo "local $opts"
  206. }
  207. ss_validate() {
  208. uci_validate_section shadowsocks-libev "$@"
  209. }
  210. validate_common_server_options_() {
  211. local cfgtype="$1"; shift
  212. local cfg="$1"; shift
  213. local func="$1"; shift
  214. 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"'
  215. local aead_methods='"aes-128-gcm", "aes-192-gcm", "aes-256-gcm"'
  216. "${func:-ss_validate}" "$cfgtype" "$cfg" "$@" \
  217. 'disabled:bool:0' \
  218. 'server:host' \
  219. 'server_port:port' \
  220. 'password:string' \
  221. 'key:string' \
  222. "method:or($stream_methods, $aead_methods)"
  223. }
  224. validate_common_client_options_() {
  225. validate_common_options_ "$@" \
  226. 'server:uci("shadowsocks-libev", "@server")' \
  227. 'local_address:host:0.0.0.0' \
  228. 'local_port:port'
  229. }
  230. validate_common_options_() {
  231. local cfgtype="$1"; shift
  232. local cfg="$1"; shift
  233. local func="$1"; shift
  234. "${func:-ss_validate}" "$cfgtype" "$cfg" "$@" \
  235. 'disabled:bool:0' \
  236. 'fast_open:bool:0' \
  237. 'ipv6_first:bool:0' \
  238. 'reuse_port:bool:0' \
  239. 'verbose:bool:0' \
  240. 'mode:or("tcp_only", "udp_only", "tcp_and_udp")' \
  241. 'mtu:uinteger' \
  242. 'timeout:uinteger' \
  243. 'user:string'
  244. }
  245. validate_server_section() {
  246. validate_common_server_options_ server "$1" "${2}"
  247. }
  248. validate_ss_local_section() {
  249. validate_common_client_options_ ss_local "$1" "${2}"
  250. }
  251. validate_ss_redir_section() {
  252. validate_common_client_options_ ss_redir "$1" \
  253. "${2}" \
  254. 'disable_sni:bool:0'
  255. }
  256. validate_ss_rules_section() {
  257. "${2:-ss_validate}" ss_rules "$1" \
  258. 'disabled:bool:0' \
  259. 'redir_tcp:uci("shadowsocks-libev", "@ss_redir")' \
  260. 'redir_udp:uci("shadowsocks-libev", "@ss_redir")' \
  261. 'src_ips_bypass:list(ipaddr)' \
  262. 'src_ips_forward:list(ipaddr)' \
  263. 'src_ips_checkdst:list(ipaddr)' \
  264. 'dst_ips_bypass_file:file' \
  265. 'dst_ips_bypass:list(ipaddr)' \
  266. 'dst_ips_forward_file:file' \
  267. 'dst_ips_forward:list(ipaddr)' \
  268. 'src_default:or("bypass", "forward", "checkdst")' \
  269. 'local_default:or("bypass", "forward", "checkdst")' \
  270. 'ipt_args:string'
  271. }
  272. validate_ss_server_section() {
  273. validate_common_server_options_ ss_server "$1" \
  274. validate_common_options_ \
  275. "${2}" \
  276. 'bind_address:ipaddr' \
  277. 'manager_address:host'
  278. }
  279. validate_ss_tunnel_section() {
  280. validate_common_client_options_ ss_tunnel "$1" \
  281. "${2}" \
  282. 'tunnel_address:regex(".+\:[0-9]+")'
  283. }