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.

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