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.

115 lines
2.2 KiB

  1. #!/bin/sh /etc/rc.common
  2. START=90
  3. STOP=15
  4. SERVICE_USE_PID=1
  5. SERVICE_WRITE_PID=1
  6. SERVICE_DAEMONIZE=1
  7. EXTRA_COMMANDS="rules"
  8. CONFIG_FILE=/var/etc/shadowsocks-libev.json
  9. get_config() {
  10. config_get_bool enable $1 enable
  11. config_get server $1 server
  12. config_get server_port $1 server_port
  13. config_get local_port $1 local_port
  14. config_get password $1 password
  15. config_get timeout $1 timeout
  16. config_get encrypt_method $1 encrypt_method
  17. config_get ignore_list $1 ignore_list
  18. config_get udp_relay $1 udp_relay
  19. config_get_bool tunnel_enable $1 tunnel_enable
  20. config_get tunnel_port $1 tunnel_port
  21. config_get tunnel_forward $1 tunnel_forward
  22. config_get lan_ac_mode $1 lan_ac_mode
  23. config_get lan_ac_ip $1 lan_ac_ip
  24. config_get wan_bp_ip $1 wan_bp_ip
  25. config_get wan_fw_ip $1 wan_fw_ip
  26. config_get ipt_ext $1 ipt_ext
  27. : ${tunnel_port:=5300}
  28. : ${tunnel_forward:=8.8.4.4:53}
  29. }
  30. start_rules() {
  31. local ac_args
  32. if [ -n "$lan_ac_ip" ]; then
  33. case $lan_ac_mode in
  34. 1) ac_args="w$lan_ac_ip"
  35. ;;
  36. 2) ac_args="b$lan_ac_ip"
  37. ;;
  38. esac
  39. fi
  40. /usr/bin/ss-rules \
  41. -s "$server" \
  42. -l "$local_port" \
  43. -i "$ignore_list" \
  44. -a "$ac_args" \
  45. -b "$wan_bp_ip" \
  46. -w "$wan_fw_ip" \
  47. -e "$ipt_ext" \
  48. -o $udp
  49. return $?
  50. }
  51. start_redir() {
  52. service_start /usr/bin/ss-redir \
  53. -c "$CONFIG_FILE" $udp
  54. return $?
  55. }
  56. start_tunnel() {
  57. service_start /usr/bin/ss-tunnel \
  58. -c "$CONFIG_FILE" \
  59. -l "$tunnel_port" \
  60. -L "$tunnel_forward" \
  61. -u
  62. return $?
  63. }
  64. rules() {
  65. config_load shadowsocks-libev
  66. config_foreach get_config shadowsocks-libev
  67. [ "$enable" = 1 ] || exit 0
  68. [ "$udp_relay" = 1 ] && udp="-u"
  69. mkdir -p $(dirname $CONFIG_FILE)
  70. : ${server:?}
  71. : ${server_port:?}
  72. : ${local_port:?}
  73. : ${password:?}
  74. : ${encrypt_method:?}
  75. cat <<-EOF >$CONFIG_FILE
  76. {
  77. "server": "$server",
  78. "server_port": $server_port,
  79. "local_address": "0.0.0.0",
  80. "local_port": $local_port,
  81. "password": "$password",
  82. "timeout": $timeout,
  83. "method": "$encrypt_method"
  84. }
  85. EOF
  86. start_rules
  87. }
  88. boot() {
  89. until iptables-save -t nat | grep -q "^:zone_lan_prerouting"; do
  90. sleep 1
  91. done
  92. start
  93. }
  94. start() {
  95. rules && start_redir
  96. [ "$tunnel_enable" = 1 ] && start_tunnel
  97. }
  98. stop() {
  99. /usr/bin/ss-rules -f
  100. service_stop /usr/bin/ss-redir
  101. service_stop /usr/bin/ss-tunnel
  102. rm -f $CONFIG_FILE
  103. }