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.

146 lines
5.8 KiB

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