aiop-openvpn 1.8 KB

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