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.

84 lines
3.8 KiB

  1. #!/bin/sh /etc/rc.common
  2. PKG_VERSION=
  3. START=94
  4. USE_PROCD=1
  5. readonly TID="200" FW_MARK="0x010000" IPSET="vpnbypass"
  6. output() {
  7. [ -n "$2" ] && [ ! $(($verbosity & $2)) -gt 0 ] && return 0;
  8. [ -t 1 ] && echo -e -n "$1"
  9. [ $(echo -e -n "$1" | wc -l) -gt 0 ] && logger -t "${PKG_NAME:-service} [$$]" "$(echo -e -n ${logmsg}${1})" && logmsg='' || logmsg=${logmsg}${1}
  10. }
  11. vpnbypass_enabled() {
  12. config_load vpnbypass
  13. config_get_bool enabled 'config' 'enabled' 0
  14. config_get verbosity 'config' 'verbosity' '2'
  15. PKG_NAME="${PKG_NAME:-vpnbypass}"
  16. [ -n "$PKG_VERSION" ] && p_name="$PKG_NAME $PKG_VERSION" || p_name="$PKG_NAME"
  17. [ -t 1 ] || p_name='service'
  18. source /lib/functions/network.sh
  19. [ "$enabled" -ne "0" ] && return 0
  20. output "$p_name is not enabled in the config file!\n"
  21. output "To enable, run 'uci set $PKG_NAME.config.enabled=1; uci commit $PKG_NAME'\n"
  22. return 1
  23. }
  24. iptables_reset(){ [ -z "$PKG_NAME" ] && return 1; iptables-save | grep -Fv -- "$PKG_NAME" | iptables-restore; lsmod | grep -q ip6table_nat && ip6tables-save | grep -Fv -- "$PKG_NAME" | ip6tables-restore; }
  25. boot() { ubus -t 30 wait_for network.interface.wan && { rc_procd start_service; rc_procd service_triggers; } || output "ERROR: $p_name failed to settle network interface!\n"; }
  26. start_service() {
  27. local ll c=1
  28. config_load vpnbypass
  29. vpnbypass_enabled || return 1
  30. config_get lports 'config' 'localport'
  31. config_get rports 'config' 'remoteport'
  32. config_get routes 'config' 'remotesubnet'
  33. config_get ranges 'config' 'localsubnet'
  34. config_get domains 'config' 'domain'
  35. procd_open_instance
  36. procd_set_param stdout 1
  37. procd_set_param stderr 1
  38. procd_close_instance
  39. while : ; do network_get_ipaddr wanip wan; network_get_gateway gwip wan; [ $c -ge 15 ] && break || let "c+=1"; [ -n "$wanip" -a -n "$gwip" ] && break || output "$p_name waiting for wan gateway...\n"; sleep 2; network_flush_cache; done
  40. [ -z "$wanip" -o -z "$gwip" ] && output "ERROR: $p_name could not get wan interface IP: $wanip or gateway: $gwip!\n" && exit 0
  41. for ll in ${routes}; do ip route del $ll; ip route add $ll via $gwip; done
  42. ip rule del fwmark "$FW_MARK" table "$TID" >/dev/null 2>&1; iptables_reset; ipset -q flush "$IPSET"; ipset -q destroy "$IPSET";
  43. ip route flush table "$TID"; ip route flush cache;
  44. ip route add default via "$gwip" table "$TID"; ip route flush cache;
  45. { modprobe xt_set; modprobe ip_set; modprobe ip_set_hash_ip; } >/dev/null 2>&1
  46. ipset -q -exist create "$IPSET" hash:ip; ipset -q flush "$IPSET"
  47. 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 "$PKG_NAME"; done
  48. 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 "$PKG_NAME"; done
  49. for ll in ${ranges}; do iptables -t mangle -I PREROUTING -s "$ll" -j MARK --set-mark "$FW_MARK/$FW_MARK" -m comment --comment "$PKG_NAME"; done
  50. iptables -t mangle -A PREROUTING -m set --match-set "$IPSET" dst -j MARK --set-mark "$FW_MARK/$FW_MARK" -m comment --comment "$PKG_NAME"
  51. ip rule add fwmark "$FW_MARK" table "$TID"
  52. output "$p_name started with TID: $TID; FW_MARK: $FW_MARK\n"
  53. }
  54. stop_service() {
  55. local ll
  56. vpnbypass_enabled || return 1
  57. config_get routes 'config' 'remotesubnet'
  58. for ll in ${routes}; do ip route del "$ll"; done
  59. ip rule del fwmark "$FW_MARK" table "$TID" >/dev/null 2>&1; iptables_reset; ipset -q flush "$IPSET"; ipset -q destroy "$IPSET";
  60. ip route flush table "$TID"; ip route flush cache;
  61. output "$p_name stopped\n"
  62. }
  63. reload_service() { start_service; }
  64. service_triggers() {
  65. procd_add_reload_trigger 'vpnbypass'
  66. procd_open_trigger
  67. procd_add_config_trigger "config.change" "vpnbypass" /etc/init.d/vpnbypass reload
  68. procd_add_interface_trigger "interface.*" "wan" /etc/init.d/vpnbypass reload
  69. procd_close_trigger
  70. }