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.

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