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