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.

137 lines
5.5 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright 2017-2020 Stan Grishin (stangri@melmac.net)
  3. # shellcheck disable=SC2039,SC1091,SC2086
  4. PKG_VERSION='dev-test'
  5. # shellcheck disable=SC2034
  6. START=94
  7. # shellcheck disable=SC2034
  8. USE_PROCD=1
  9. # shellcheck disable=SC2034
  10. extra_command "version" "Print version information"
  11. version() { echo "$PKG_VERSION"; }
  12. readonly __ERROR__='\033[0;31mERROR\033[0m'
  13. # shellcheck disable=SC2034
  14. serviceEnabled=0
  15. verbosity=2
  16. TID='200'
  17. IPSET='vpnbypass'
  18. FW_MARK='0x010000'
  19. FW_MASK='0xff0000'
  20. wan_if4=''
  21. wan_gw=''
  22. readonly packageName='vpnbypass'
  23. readonly serviceName="$packageName $PKG_VERSION"
  24. readonly sharedMemoryOutput="/dev/shm/$packageName-output"
  25. output() {
  26. # Can take a single parameter (text) to be output at any verbosity
  27. # Or target verbosity level and text to be output at specifc verbosity
  28. local msg memmsg logmsg
  29. if [ $# -ne 1 ]; then
  30. if [ $((verbosity & $1)) -gt 0 ] || [ "$verbosity" = "$1" ]; then shift; else return 0; fi
  31. fi
  32. [ -t 1 ] && printf "%b" "$1"
  33. msg="${1//$serviceName /service }";
  34. if [ "$(printf "%b" "$msg" | wc -l)" -gt 0 ]; then
  35. [ -s "$sharedMemoryOutput" ] && memmsg="$(cat "$sharedMemoryOutput")"
  36. logmsg="$(printf "%b" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
  37. logger -t "${packageName:-service} [$$]" "$(printf "%b" "$logmsg")"
  38. rm -f "$sharedMemoryOutput"
  39. else
  40. printf "%b" "$msg" >> "$sharedMemoryOutput"
  41. fi
  42. }
  43. load_package_config() {
  44. config_load "$packageName"
  45. config_get_bool serviceEnabled 'config' 'enabled' 1
  46. config_get verbosity 'config' 'verbosity' '2'
  47. if [ -z "${verbosity##*[!0-9]*}" ] || [ "$verbosity" -lt 0 ] || [ "$verbosity" -gt 2 ]; then
  48. verbosity=1
  49. fi
  50. . /lib/functions/network.sh
  51. }
  52. is_enabled() {
  53. local sleepCount=1
  54. load_package_config
  55. while : ; do
  56. network_find_wan wan_if4
  57. [ "$serviceEnabled" -gt 0 ] || return 1
  58. [ -n "$wan_if4" ] && network_get_gateway wan_gw "$wan_if4"
  59. if [ $sleepCount -ge 25 ] || [ -n "$wan_gw" ]; then break; fi
  60. output "$serviceName waiting for wan gateway...\\n"
  61. sleep 2; network_flush_cache; let "sleepCount+=1";
  62. done
  63. [ -n "$wan_gw" ] && return 0
  64. output "$__ERROR__: $serviceName failed to discover WAN gateway.\\n"; return 1;
  65. }
  66. is_ovpn() { local dev; dev=$(uci -q get network."$1".ifname); if [ "${dev:0:3}" = "tun" ] || [ "${dev:0:3}" = "tap" ] || [ -f "/sys/devices/virtual/net/${dev}/tun_flags" ]; then return 0; else return 1; fi; }
  67. is_wan() { if [ -n "$wan_if4" ] && [ "$1" = "$wan_if4" ]; then return 0; else return 1; fi; }
  68. is_supported_interface() { if is_wan "$1" || is_ovpn "$1"; then return 0; else return 1; fi; }
  69. ipt() {
  70. local d;
  71. d="${*//-A/-D}"; [ "$d" != "$*" ] && iptables $d >/dev/null 2>&1
  72. d="${*//-I/-D}"; [ "$d" != "$*" ] && iptables $d >/dev/null 2>&1
  73. d="${*//-N/-F}"; [ "$d" != "$*" ] && iptables $d >/dev/null 2>&1
  74. d="${*//-N/-X}"; [ "$d" != "$*" ] && iptables $d >/dev/null 2>&1
  75. d="$*"; iptables $d >/dev/null 2>&1 || output "\\n$__ERROR__: iptables $d\\n"
  76. }
  77. start_service() {
  78. local ll lports rports routes ranges
  79. is_enabled || return 1
  80. config_get lports 'config' 'localport'
  81. config_get rports 'config' 'remoteport'
  82. config_get routes 'config' 'remotesubnet'
  83. config_get ranges 'config' 'localsubnet'
  84. procd_open_instance "main"
  85. procd_set_param command /bin/true
  86. procd_set_param stdout 1
  87. procd_set_param stderr 1
  88. procd_close_instance
  89. ipt -t mangle -D PREROUTING -m mark --mark 0x00/${FW_MASK} -g VPNBYPASS >/dev/null 2>&1
  90. { ipt -t mangle -N VPNBYPASS; ipt -t mangle -A PREROUTING -m mark --mark 0x00/${FW_MASK} -g VPNBYPASS; } >/dev/null 2>&1
  91. ipt -t mangle -A VPNBYPASS -m set --match-set $IPSET dst -j MARK --set-mark ${FW_MARK}/${FW_MASK} >/dev/null 2>&1
  92. ip rule del fwmark "$FW_MARK" table "$TID" >/dev/null 2>&1;
  93. ipset -q flush "$IPSET"; ipset -q destroy "$IPSET";
  94. ip route flush table "$TID"; ip route flush cache;
  95. ip route add default via "$wan_gw" table "$TID"; ip route flush cache;
  96. ipset -q -exist create "$IPSET" hash:ip; ipset -q flush "$IPSET"
  97. ip rule add fwmark "$FW_MARK" table "$TID"
  98. { modprobe xt_set; modprobe ip_set; modprobe ip_set_hash_ip; } >/dev/null 2>&1
  99. for ll in ${ranges}; do ipt -t mangle -I VPNBYPASS 1 -j MARK --set-mark ${FW_MARK}/${FW_MASK} -s "$ll"; done
  100. for ll in ${lports}; do ipt -t mangle -I VPNBYPASS 1 -j MARK --set-mark ${FW_MARK}/${FW_MASK} -p tcp -m multiport --sport "${ll//-/:}"; done
  101. for ll in ${routes}; do ipt -t mangle -I VPNBYPASS 1 -j MARK --set-mark ${FW_MARK}/${FW_MASK} -d "$ll"; done
  102. for ll in ${rports}; do ipt -t mangle -I VPNBYPASS 1 -j MARK --set-mark ${FW_MARK}/${FW_MASK} -p tcp -m multiport --dport "${ll//-/:}"; done
  103. output "$serviceName started with TID: $TID; FW_MARK: $FW_MARK\\n"
  104. }
  105. stop_service() {
  106. load_package_config
  107. ip rule del fwmark "$FW_MARK" table "$TID" >/dev/null 2>&1;
  108. ipset -q flush "$IPSET"; ipset -q destroy "$IPSET";
  109. ip route flush table "$TID"; ip route flush cache;
  110. ipt -t mangle -D PREROUTING -m mark --mark 0x00/${FW_MASK} -g VPNBYPASS >/dev/null 2>&1
  111. { ipt -t mangle -F VPNBYPASS; ipt -t mangle -X VPNBYPASS; } >/dev/null 2>&1
  112. output "$serviceName stopped\\n"
  113. }
  114. service_triggers_load_interface() { is_supported_interface "$1" && ifaces="${ifaces}${1} "; }
  115. service_triggers() {
  116. local ifaces n
  117. config_load network; config_foreach service_triggers_load_interface 'interface';
  118. procd_add_reload_trigger 'firewall' 'openvpn' 'vpnbypass'
  119. procd_open_trigger
  120. for n in $ifaces; do procd_add_reload_interface_trigger "$n"; procd_add_interface_trigger "interface.*" "$n" /etc/init.d/vpnbypass reload; done;
  121. output "$serviceName monitoring interfaces: $ifaces\\n"
  122. procd_close_trigger
  123. }