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.

123 lines
5.2 KiB

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