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.

102 lines
4.8 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() { [[ $# -ne 1 ]] && { [[ ! $((verbosity & $1)) -gt 0 ]] && return 0 || shift; }; local msg; msg=$(echo -n "${1/$serviceName /service }" | sed 's|\\033\[[0-9]\?;\?[0-9]\?[0-9]\?m||g'); [[ -t 1 ]] && echo -e -n "$1"; [[ $(echo -e -n "$msg" | wc -l) -gt 0 ]] && logger -t "${packageName:-service} [$$]" "$(echo -e -n ${logmsg}${msg})" && logmsg='' || logmsg=${logmsg}${msg}; }
  13. readonly packageName='vpnbypass'
  14. readonly serviceName="$packageName $PKG_VERSION"
  15. load_package_config() {
  16. config_load "$packageName"
  17. config_get_bool serviceEnabled 'config' 'enabled' 1
  18. config_get verbosity 'config' 'verbosity' '2'
  19. source /lib/functions/network.sh
  20. }
  21. is_enabled() {
  22. local sleepCount=1
  23. load_package_config
  24. while : ; do
  25. network_find_wan wan_if4
  26. [ $serviceEnabled -gt 0 ] || return 1
  27. [ -n "$wan_if4" ] && network_get_gateway wan_gw $wan_if4
  28. [[ $sleepCount -ge 25 || -n "$wan_gw" ]] && break
  29. output "$serviceName waiting for wan gateway...\n"
  30. sleep 2; network_flush_cache; let "sleepCount+=1";
  31. done
  32. [ -n "$wan_gw" ] && return 0
  33. output "$__ERROR__: $serviceName failed to discover WAN gateway.\n"; return 1;
  34. }
  35. 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; }
  36. is_wan() { if [ -n "$wan_if4" ] && [ "$1" == "$wan_if4" ]; then return 0; else return 1; fi; }
  37. is_supported_interface() { if is_wan "$1" || is_ovpn "$1"; then return 0; else return 1; fi; }
  38. ipt() {
  39. local d; d=$(echo "$*" | sed s/-A/-D/g);
  40. [ "$d" != "$*" ] && iptables $d >/dev/null 2>&1
  41. d=$(echo "$*" | sed s/-I/-D/g)
  42. [ "$d" != "$*" ] && iptables $d >/dev/null 2>&1
  43. d=$(echo "$*" | sed s/-N/-F/g)
  44. [ "$d" != "$*" ] && iptables $d >/dev/null 2>&1
  45. d=$(echo "$*" | sed s/-N/-X/g)
  46. [ "$d" != "$*" ] && iptables $d >/dev/null 2>&1
  47. d="$*"
  48. iptables $d >/dev/null 2>&1 || output "\n$__ERROR__: iptables $d\n"
  49. }
  50. start_service() {
  51. local ll lports rports routes ranges
  52. is_enabled || return 1
  53. config_get lports 'config' 'localport'
  54. config_get rports 'config' 'remoteport'
  55. config_get routes 'config' 'remotesubnet'
  56. config_get ranges 'config' 'localsubnet'
  57. iptables -t mangle -D PREROUTING -m mark --mark 0x00/${FW_MASK} -g VPNBYPASS >/dev/null 2>&1
  58. ipt -t mangle -N VPNBYPASS; ipt -t mangle -A PREROUTING -m mark --mark 0x00/${FW_MASK} -g VPNBYPASS;
  59. ipt -t mangle -A VPNBYPASS -m set --match-set $IPSET dst -j MARK --set-mark ${FW_MARK}/${FW_MASK}
  60. ip rule del fwmark "$FW_MARK" table "$TID" >/dev/null 2>&1; ipset -q flush "$IPSET"; ipset -q destroy "$IPSET";
  61. ip route flush table "$TID"; ip route flush cache;
  62. ip route add default via "$wan_gw" table "$TID"; ip route flush cache;
  63. ipset -q -exist create "$IPSET" hash:ip; ipset -q flush "$IPSET"
  64. ip rule add fwmark "$FW_MARK" table "$TID"
  65. { modprobe xt_set; modprobe ip_set; modprobe ip_set_hash_ip; } >/dev/null 2>&1
  66. for ll in ${ranges}; do ipt -t mangle -I VPNBYPASS 1 -j MARK --set-mark ${FW_MARK}/${FW_MASK} -s "$ll"; done
  67. 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
  68. for ll in ${routes}; do ipt -t mangle -I VPNBYPASS 1 -j MARK --set-mark ${FW_MARK}/${FW_MASK} -d "$ll"; done
  69. 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
  70. output "$serviceName started with TID: $TID; FW_MARK: $FW_MARK\n"
  71. }
  72. stop_service() {
  73. load_package_config
  74. ip rule del fwmark "$FW_MARK" table "$TID" >/dev/null 2>&1; ipset -q flush "$IPSET"; ipset -q destroy "$IPSET";
  75. ip route flush table "$TID"; ip route flush cache;
  76. ipt -t mangle -D PREROUTING -m mark --mark 0x00/${FW_MASK} -g VPNBYPASS
  77. ipt -t mangle -F VPNBYPASS; ipt -t mangle -X VPNBYPASS;
  78. output "$serviceName stopped\n"
  79. }
  80. service_triggers_load_interface() { is_supported_interface "$1" && ifaces="${ifaces}${1} "; }
  81. service_triggers() {
  82. local ifaces n
  83. config_load network; config_foreach service_triggers_load_interface 'interface';
  84. procd_add_reload_trigger 'firewall'
  85. procd_add_reload_trigger 'openvpn'
  86. procd_open_trigger
  87. for n in $ifaces; do procd_add_reload_interface_trigger "$n"; procd_add_interface_trigger "interface.*" "$n" /etc/init.d/${packageName} reload; done;
  88. output 2 "$serviceName monitoring interfaces: $ifaces $_OK_\n"
  89. procd_close_trigger
  90. }