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.

105 lines
3.0 KiB

  1. #!/bin/sh /etc/rc.common
  2. START=99
  3. USE_PROCD=1
  4. INDEX=0
  5. append_bool() {
  6. local section="$1"
  7. local option="$2"
  8. local value="$3"
  9. local _val
  10. config_get_bool _val "$section" "$option" '0'
  11. [ "$_val" -gt 0 ] && append args "$3"
  12. }
  13. append_string() {
  14. local section="$1"
  15. local option="$2"
  16. local value="$3"
  17. local _val
  18. config_get _val "$section" "$option"
  19. [ -n "$_val" ] && append args "$3$_val"
  20. }
  21. service_triggers () {
  22. procd_open_trigger
  23. procd_add_reload_trigger "eoip"
  24. procd_add_config_trigger "config.change" "eoip" /etc/init.d/eoip restart
  25. procd_add_config_trigger "config.change" "/etc/config/eoip" /etc/init.d/eoip restart
  26. procd_close_trigger
  27. }
  28. start_service() {
  29. config_load "eoip"
  30. echo "" > /etc/eoip.cfg
  31. echo "#!/bin/sh" > /etc/afterStart.sh
  32. echo "chkCount=0">> /etc/afterStart.sh
  33. echo "while [ \$chkCount -le 10 ];do">> /etc/afterStart.sh
  34. echo " chkStarted=\$(ip a | grep zeoip | wc -l)">> /etc/afterStart.sh
  35. echo " if [ \$chkStarted -eq 0 ]; then">> /etc/afterStart.sh
  36. echo " chkCount=\$((chkCount+1))">> /etc/afterStart.sh
  37. echo " sleep 2">> /etc/afterStart.sh
  38. echo " else">> /etc/afterStart.sh
  39. echo " chkCount=11">> /etc/afterStart.sh
  40. echo " fi">> /etc/afterStart.sh
  41. echo "done">> /etc/afterStart.sh
  42. echo "now=\$(ip a | grep \"@zeoip\" | awk '{print \$2}' | sed 's/.$//' | cut -d \"@\" -f 1)" >> /etc/afterStart.sh
  43. echo "IFS=\$'\n'" >> /etc/afterStart.sh
  44. echo "for s in \$now ; do" >> /etc/afterStart.sh
  45. echo " ip link delete link dev \$s" >> /etc/afterStart.sh
  46. echo "done" >> /etc/afterStart.sh
  47. echo "rm /etc/afterStart.sh" >> /etc/afterStart.sh
  48. chmod +x /etc/afterStart.sh
  49. config_foreach start_eoip eoip
  50. config_foreach start_eoip_vlan eoip
  51. if [ "$(cat /etc/eoip.cfg | grep zeoip)" != '' ]; then
  52. /etc/afterStart.sh&
  53. procd_open_instance
  54. args=" /etc/eoip.cfg"
  55. procd_set_param command /usr/bin/eoip $args
  56. procd_close_instance
  57. else
  58. rm /etc/afterStart.sh
  59. fi
  60. }
  61. stop_service() {
  62. killall eoip
  63. }
  64. start_eoip() {
  65. local section="$1" runas_root
  66. config_get_bool enabled "$section" enabled
  67. if [ "$enabled" -gt 0 ]; then
  68. config_get name "$section" name
  69. config_get idtun "$section" idtun
  70. config_get dst "$section" dst
  71. config_get_bool dynamic "$section" dynamic 0
  72. if [ "${name}" != '' ] && [ "${dst}" != '' ] && [ "${idtun}" != '' ]; then
  73. cnt=$(cat /etc/eoip.cfg | grep "zeoip${name}" | wc -l)
  74. if [ $cnt -eq 0 ]; then
  75. echo "[zeoip${name}]" >>/etc/eoip.cfg
  76. echo "id=${idtun}" >>/etc/eoip.cfg
  77. echo "dst=${dst}" >>/etc/eoip.cfg
  78. [ "$dynamic" -gt 0 ] && echo "dynamic=${dynamic}" >>/etc/eoip.cfg
  79. INDEX=$((INDEX+1))
  80. else
  81. result=$(uci delete /etc/config/eoip.@eoip[$INDEX])
  82. INDEX=$((INDEX+1))
  83. fi
  84. fi
  85. fi
  86. }
  87. start_eoip_vlan() {
  88. local section="$1" runas_root
  89. config_get name "$section" name
  90. config_list_foreach "$section" vlan handle_vlan ${name}
  91. }
  92. handle_vlan() {
  93. local value="$1"
  94. local name="$2"
  95. echo "ip link add link zeoip${name} name zeoip${name}.${value} type vlan id ${value}" >> /etc/afterStart.sh
  96. }