aiop-openvpn 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/bin/sh
  2. source /jffs/scripts/aiop/aiop-func
  3. route_vpn () {
  4. IFACE="${1}"
  5. IFACE_TUN="${2}"
  6. if [ `ifconfig ${IFACE_TUN} 2&> /dev/null | grep -ice "UP"` -ne 1 ]; then
  7. logger "openvpn: ${IFACE_TUN} not UP"
  8. return
  9. fi
  10. if [ `ifconfig ${IFACE} 2&> /dev/null | grep -ice "UP"` -ne 1 ]; then
  11. logger "dnsmasq-dhcp: Unknown interface ${IFACE}."
  12. return
  13. fi
  14. IFACE_INET_ADDR="$(ifconfig "${IFACE}" | sed -ne's/.*inet addr:\([^ ]*\).*$/\1/p')"
  15. IFACE_NWRK_ADDR="${IFACE_INET_ADDR%.*}.0"
  16. RTABLE=`echo ${IFACE_TUN} | sed -E 's/([a-z]+)([0-9]+).*/\2/'`
  17. CIDR=24
  18. if [ $# -eq 3 ]; then
  19. CIDR="${3}"
  20. fi
  21. ip route show table main | grep -Ev ^default | while read ROUTE; do
  22. # Remote interface != ${IFACE_TUN} && != ${IFACE} entries from this table.
  23. if [ $(echo "${ROUTE}" | grep -ice "tun") -eq 0 ] || [ $(echo "${ROUTE}" | grep -ice "${IFACE_TUN}") -ne 0 ]; then
  24. if [ $(echo "${ROUTE}" | grep -icE "wl[0-9]\.[0-9]") -eq 0 ] || [ $(echo "${ROUTE}" | grep -ice "${IFACE}") -ne 0 ]; then
  25. cmd_run "ip-route" "ip route add table ${RTABLE} ${ROUTE}" "-nolog"
  26. fi
  27. fi
  28. done
  29. cmd_run "ip-route" "ip route add default dev ${IFACE_TUN} table ${RTABLE}"
  30. cmd_run "ip-rule" "ip rule add dev ${IFACE} table ${RTABLE}"
  31. cmd_run "ip-route" "ip route flush cache"
  32. iptable "${IFACE}" "I" "FORWARD" "ACCEPT" "${IFACE_TUN}"
  33. iptable "${IFACE_TUN}" "I" "${IFACE_NWRK_ADDR}/${CIDR}"
  34. }
  35. unroute_vpn () {
  36. IFACE="${1}"
  37. IFACE_TUN="${2}"
  38. if [ `ifconfig ${IFACE} 2&> /dev/null | grep -ice "UP"` -ne 1 ]; then
  39. logger "dnsmasq-dhcp: Unknown interface ${IFACE}."
  40. return
  41. fi
  42. IFACE_INET_ADDR="$(ifconfig "${IFACE}" | sed -ne's/.*inet addr:\([^ ]*\).*$/\1/p')"
  43. IFACE_NWRK_ADDR="${IFACE_INET_ADDR%.*}.0"
  44. RTABLE=`echo ${IFACE_TUN} | sed -E 's/([a-z]+)([0-9]+).*/\2/'`
  45. CIDR=24
  46. if [ $# -eq 3 ]; then
  47. CIDR="${3}"
  48. fi
  49. cmd_run "ip-route" "ip route flush table ${RTABLE}" "-nolog"
  50. iptable "${IFACE}" "D" "FORWARD" "ACCEPT" "${IFACE_TUN}"
  51. iptable "${IFACE_TUN}" "D" "${IFACE_NWRK_ADDR}/${CIDR}"
  52. }