eibgrad

tomato-ovpn-remote-access.sh

May 18th, 2020 (edited)
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.30 KB | None | 0 0
  1. #!/bin/sh
  2. #DEBUG=; set -x # comment/uncomment to disable/enable debug mode
  3.  
  4. #         name: tomato-ovpn-remote-access.sh
  5. #      version: 1.0.0, 18-may-2020, by eibgrad
  6. #      purpose: enable remote access over wan w/ active openvpn client
  7. #  script type: init (autostart)
  8. #  installation:
  9. #    1. enable jffs (administration->jffs)
  10. #    2. enable syslog (administration->logging->syslog)
  11. #    3. use shell (telnet/ssh) to execute one of the following commands:
  12. #         curl -kLs bit.ly/tomato-installer|tr -d '\r'|sh -s UUUT8GiW init
  13. #       or
  14. #         wget -qO - bit.ly/tomato-installer|tr -d '\r'|sh -s UUUT8GiW init
  15. #    4. modify options (minimally DDNS_DOMAIN_NAMES) using vi editor:
  16. #         vi /jffs/etc/config/tomato-ovpn-remote-access.init
  17. #    5. reboot
  18. #
  19. {
  20. # ------------------------------ BEGIN OPTIONS ------------------------------- #
  21.  
  22. # "roaming" ddns domain name(s)
  23. DDNS_DOMAIN_NAMES="
  24. myhostname.duckdns.org
  25. #myhostname2.duckdns.org
  26. #myhostname3.duckdns.org
  27. "
  28.  
  29. # time (in secs) between checks for ddns updates
  30. UPDATE_INTERVAL=300
  31.  
  32. # optional: well-known static routes
  33. STATIC_ROUTES="
  34. #171.190.59.0/24 # workplace
  35. #230.139.191.67 # vacation home
  36. #215.126.219.216 # local wifi cafe
  37. "
  38.  
  39. # optional: some servers may update faster and/or more reliably than others
  40. #DNS_SERVER=1.1.1.1 # cloudflare
  41. #DNS_SERVER=8.8.8.8 # google
  42. #DNS_SERVER=9.9.9.9 # quad9
  43.  
  44. # ------------------------------- END OPTIONS -------------------------------- #
  45.  
  46. # ---------------------- DO NOT CHANGE BELOW THIS LINE ----------------------- #
  47.  
  48. # function get_ip( domain-name [server] )
  49. get_ip() {
  50.     nslookup $1 $2 2>/dev/null | \
  51.         awk '/^Name:/,0 {if (/^Addr[^:]*: [0-9]{1,3}\./) print $3}'
  52. }
  53.  
  54. # wait for wan availability
  55. while ! ping -qc1 -w3 8.8.8.8 >/dev/null 2>&1; do sleep 10; done
  56.  
  57. # periodically update routing table
  58. while :; do
  59.     gateway_ip="$(nvram get wan_gateway)"
  60.     static_ip_list=""
  61.     curr_ddns_ip_list=""
  62.     routing_change=false
  63.  
  64.     # set internal field separator to newline
  65.     OIFS="$IFS"; IFS=$'\n'
  66.  
  67.     # add well-known static route(s)
  68.     for ip in $STATIC_ROUTES; do
  69.         # skip comments and blank lines
  70.         echo $ip | grep -Eq '^[[:space:]]*(#|$)' && continue
  71.  
  72.         # isolate ip address (treat the rest as comments)
  73.         ip="$(echo $ip | awk '{print $1}')"
  74.  
  75.         # track static ips
  76.         static_ip_list="$ip $static_ip_list"
  77.  
  78.         if ! ip route | grep -q "^$ip "; then
  79.             if ip route add $ip via $gateway_ip; then
  80.                 routing_change=true
  81.                 echo "info: route added: $ip"
  82.             fi
  83.         fi
  84.     done
  85.  
  86.     # add current ddns static route(s)
  87.     for dom in $DDNS_DOMAIN_NAMES; do
  88.         # skip comments and blank lines
  89.         echo $dom | grep -Eq '^[[:space:]]*(#|$)' && continue
  90.  
  91.         # determine public ip (if any) bound to domain name
  92.         ip="$(get_ip $dom $(echo $DNS_SERVER | awk '{print $1}'))"
  93.  
  94.         [ $ip ] || { echo "error: cannot resolve $dom"; continue; }
  95.  
  96.         # skip duplicates
  97.         echo "$curr_ddns_ip_list" | grep -q "$ip " && continue
  98.  
  99.         # track ddns ips
  100.         curr_ddns_ip_list="$ip $curr_ddns_ip_list"
  101.  
  102.         if ! ip route | grep -q "^$ip "; then
  103.             if ip route add $ip via $gateway_ip; then
  104.                 routing_change=true
  105.                 echo "info: route added: $ip"
  106.             fi
  107.         fi
  108.     done
  109.  
  110.     # reset internal field separator
  111.     IFS="$OIFS"
  112.  
  113.     # delete previous ddns static route(s)
  114.     for ip in $prev_ddns_ip_list; do
  115.         if ! echo "$static_ip_list" | grep -q "$ip "; then
  116.             if ! echo "$curr_ddns_ip_list" | grep -q "$ip "; then
  117.                 if ip route | grep -q "^$ip "; then
  118.                     if ip route del $ip via $gateway_ip; then
  119.                         routing_change=true
  120.                         echo "info: route deleted: $ip"
  121.                     fi
  122.                 fi
  123.             fi
  124.         fi
  125.     done
  126.  
  127.     # force routing system to recognize changes
  128.     [[ $routing_change == true ]] && ip route flush cache
  129.  
  130.     # save current ddns ips
  131.     prev_ddns_ip_list="$curr_ddns_ip_list"
  132.  
  133.     # wait awhile and repeat
  134.     sleep $UPDATE_INTERVAL
  135. done
  136.  
  137. } 2>&1 | logger $([ ${DEBUG+x} ] && echo "-p user.debug") \
  138.     -t $(echo $(basename $0) | grep -Eo '^.{0,23}')[$$] &
Add Comment
Please, Sign In to add comment