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.

76 lines
3.6 KiB

  1. #!/bin/sh /etc/rc.common
  2. START=90
  3. USE_PROCD=1
  4. TID="200"; FW_MARK="0x010000"; IPSET="vpnbypass";
  5. output() {
  6. [ -z "$verbosity" ] && config_get verbosity 'config' 'verbosity' '2'
  7. [ -n "$2" -a $((verbosity)) -ne $(($2)) ] && return 0;
  8. [ -t 1 ] && echo -e -n "$1"
  9. [ $(echo -e -n "$1" | wc -l) -gt 0 ] && logger -t "vpnbypass[$$]" "$(echo -e -n ${logmsg}${1})" && logmsg='' || logmsg=${logmsg}${1}
  10. }
  11. vpnbypass_enabled() {
  12. config_get_bool enabled 'config' 'enabled' 0
  13. [ $((enabled)) -gt 0 ] && return 0 || { output "VPNBypass is not enabled in the config file!\nTo enable, run 'uci set vpnbypass.config.enabled=1; uci commit vpnbypass'\n"; return 1; }
  14. }
  15. boot() { ubus -t 30 wait_for network.interface && rc_procd start_service || output 'ERROR: Failed to settle network interface!\n'; }
  16. start_service() {
  17. local ll
  18. config_load vpnbypass
  19. vpnbypass_enabled || return 1
  20. [ -d /etc/openvpn ] || return 1
  21. config_get lports 'config' 'localport'
  22. config_get rports 'config' 'remoteport'
  23. config_get routes 'config' 'remotesubnet'
  24. config_get ranges 'config' 'localsubnet'
  25. config_get domains 'config' 'domain'
  26. procd_open_instance
  27. procd_set_param stdout 1
  28. procd_set_param stderr 1
  29. procd_close_instance
  30. . /lib/functions/network.sh; network_get_ipaddr wanip wan; network_get_gateway gwip wan; network_get_ipaddr lanip lan
  31. [ ! "$wanip" ] && output 'ERROR: Could not get wan ip\n' && exit 0
  32. [ ! "$gwip" ] && output 'ERROR: Could not get wan gateway\n' && exit 0
  33. for ll in ${routes}; do (ip route del $ll; ip route add $ll via $gwip) >/dev/null 2>&1; done
  34. (ip rule del fwmark $FW_MARK table $TID; iptables -t mangle -F; ipset -F $IPSET; ipset -X $IPSET) >/dev/null 2>&1
  35. (ip route flush table $TID; ip route flush cache) >/dev/null 2>&1
  36. (ip route add default via $gwip table $TID; ip route flush cache) >/dev/null 2>&1
  37. (modprobe xt_set || modprobe ip_set; insmod ip_set_hash_ip) >/dev/null 2>&1
  38. (ipset -N $IPSET iphash -q; ipset -F $IPSET) >/dev/null 2>&1
  39. for ll in ${lports}; do iptables -t mangle -A PREROUTING -p tcp -m multiport --sport $ll -j MARK --set-mark $FW_MARK/$FW_MARK -m comment --comment "vpnbypass"; done
  40. for ll in ${rports}; do iptables -t mangle -A PREROUTING -p tcp -m multiport --dport $ll -j MARK --set-mark $FW_MARK/$FW_MARK -m comment --comment "vpnbypass"; done
  41. for ll in ${ranges}; do iptables -t mangle -I PREROUTING -s $ll -j MARK --set-mark $FW_MARK/$FW_MARK -m comment --comment "vpnbypass"; done
  42. iptables -t mangle -A PREROUTING -m set --match-set $IPSET dst -j MARK --set-mark $FW_MARK/$FW_MARK -m comment --comment "vpnbypass"
  43. ip rule add fwmark $FW_MARK table $TID
  44. output "vpnbypass started with TID: $TID FW_MARK: $FW_MARK\n"
  45. }
  46. stop_service() {
  47. local ll
  48. config_load vpnbypass
  49. vpnbypass_enabled || return 1
  50. config_get routes 'config' 'remotesubnet'
  51. [ -d /etc/openvpn ] || return 1
  52. rm -f /etc/hotplug.d/firewall/${START}-${IPSET}
  53. . /lib/functions/network.sh; network_get_ipaddr wanip wan; network_get_gateway gwip wan; network_get_ipaddr lanip lan
  54. [ ! "$wanip" ] && output 'ERROR: Could not get wan ip\n' && exit 0
  55. [ ! "$gwip" ] && output 'ERROR: Could not get wan gateway\n' && exit 0
  56. for ll in ${routes}; do ip route del $ll >/dev/null 2>&1; done
  57. # iptables-save | grep -Fv -- "vpnbypass" | iptables-restore
  58. (ip rule del fwmark $FW_MARK table $TID; iptables -t mangle -F; ipset -F $IPSET; ipset -X $IPSET) >/dev/null 2>&1
  59. (ip route flush table $TID; ip route flush cache) >/dev/null 2>&1
  60. output "vpnbypass stopped\n"
  61. }
  62. service_triggers() {
  63. procd_add_reload_trigger 'vpnbypass'
  64. procd_add_reload_interface_trigger 'wan'
  65. }