aiop-openvpn 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/bin/sh
  2. source /jffs/scripts/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. if [ `ip route show table "${RTABLE}" | grep -ice "${ROUTE}"` -eq 0 ]; then
  23. cmd_run "ip-route" "ip route add table ${RTABLE} ${ROUTE}"
  24. fi
  25. done
  26. cmd_run "ip-route" "ip route add default dev ${IFACE_TUN} table ${RTABLE}"
  27. cmd_run "ip-rule" "ip rule add dev ${IFACE} table ${RTABLE}"
  28. cmd_run "ip-route" "ip route flush cache"
  29. iptable "${IFACE}" "I" "FORWARD" "ACCEPT" "${IFACE_TUN}"
  30. iptable "${IFACE_TUN}" "I" "${IFACE_NWRK_ADDR}/${CIDR}"
  31. }
  32. unroute_vpn () {
  33. IFACE="${1}"
  34. IFACE_TUN="${2}"
  35. if [ `ifconfig ${IFACE} 2&> /dev/null | grep -ice "UP"` -ne 1 ]; then
  36. logger "dnsmasq-dhcp: Unknown interface ${IFACE}."
  37. return
  38. fi
  39. IFACE_INET_ADDR="$(ifconfig "${IFACE}" | sed -ne's/.*inet addr:\([^ ]*\).*$/\1/p')"
  40. IFACE_NWRK_ADDR="${IFACE_INET_ADDR%.*}.0"
  41. RTABLE=`echo ${IFACE_TUN} | sed -E 's/([a-z]+)([0-9]+).*/\2/'`
  42. CIDR=24
  43. if [ $# -eq 3 ]; then
  44. CIDR="${3}"
  45. fi
  46. cmd_run "ip-route" "ip route flush table ${RTABLE}"
  47. cmd_run "ip-rule" "ip rule flush table ${RTABLE}"
  48. iptable "${IFACE}" "D" "FORWARD" "ACCEPT" "${IFACE_TUN}"
  49. iptable "${IFACE_TUN}" "D" "${IFACE_NWRK_ADDR}/${CIDR}"
  50. }