Advertisement
eibgrad

vpnrouting.sh

Nov 8th, 2024 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.98 KB | None | 0 0
  1. #!/bin/sh
  2. export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/home/root:
  3. #
  4. # VPN Client selective routing up down script
  5. #
  6. # Copyright by pedro 2019 - 2024
  7. #
  8.  
  9.  
  10. . nvram_ops
  11.  
  12. PID=$$
  13. IFACE=$dev
  14. SERVICE=$(echo $dev | sed 's/\(tun\|tap\)1/client/;s/\(tun\|tap\)2/server/')
  15. FIREWALL_ROUTING="/etc/openvpn/fw/$SERVICE-fw-routing.sh"
  16. DNSMASQ_IPSET="/etc/dnsmasq.ipset"
  17. RESTART_DNSMASQ=0
  18. RESTART_FW=0
  19. ID="0"
  20. LOGS="logger -t openvpn-vpnrouting.sh[$PID][$IFACE]"
  21. [ -d /etc/openvpn/fw ] || mkdir -m 0700 "/etc/openvpn/fw"
  22.  
  23.  
  24. find_iface() {
  25.     # These IDs were intentionally picked to avoid overwriting
  26.     # marks set by QoS. See qos.c
  27.     if [ "$SERVICE" == "client1" ]; then
  28.         ID="2304" # 0x900
  29.     elif [ "$SERVICE" == "client2" ]; then
  30.         ID="2560" # 0xA00
  31.     elif [ "$SERVICE" == "client3" ]; then
  32.         ID="2816" # 0xB00
  33.     else
  34.         $LOGS "Interface not found!"
  35.         exit 0
  36.     fi
  37.  
  38.     PIDFILE="/var/run/vpnrouting$ID.pid"
  39. }
  40.  
  41. initTable() {
  42.     local ROUTE
  43.     $LOGS "Creating VPN routing table (mode $VPN_REDIR)"
  44.  
  45.     [ "$VPN_REDIR" -eq 3 ] && {
  46.         ip route show table main dev $IFACE | while read ROUTE; do
  47.             ip route add table $ID $ROUTE dev $IFACE
  48.         done
  49.     }
  50.     # copy routes from main routing table (exclude vpns and default gateway)
  51.     [ "$VPN_REDIR" -eq 2 ] && {
  52.         ip route show table main | grep -Ev 'tun11|tun12|tun13|^default ' | while read ROUTE; do
  53.             ip route add table $ID $ROUTE
  54.         done
  55.     }
  56. }
  57.  
  58. stopRouting() {
  59.     $LOGS "Clean-up routing"
  60.  
  61.     ip route flush table $ID
  62.     ip route flush cache
  63.  
  64.     ip rule | grep "lookup $ID" && ip rule del fwmark $ID/0xf00 table $ID
  65.  
  66.     ipset destroy vpnrouting$ID
  67.  
  68.     sed -i "s/-A/-D/g" $FIREWALL_ROUTING
  69.     $FIREWALL_ROUTING
  70.     rm -f $FIREWALL_ROUTING &>/dev/null
  71.  
  72.     sed -i $DNSMASQ_IPSET -e "/vpnrouting$ID/d"
  73. }
  74.  
  75. startRouting() {
  76.     local DNSMASQ=0 i VAL1 VAL2 VAL3
  77.  
  78.     stopRouting
  79.     NS vpn_client"${ID#??}"_rdnsmasq=0
  80.  
  81.     $LOGS "Starting routing policy for openvpn-$SERVICE - Interface $IFACE - Table $ID"
  82.  
  83.     [ -n "$route_vpn_gateway" ] && {
  84.         ip route add table $ID default via $route_vpn_gateway dev $IFACE
  85.     } || {
  86.         ip route add table $ID default dev $IFACE
  87.     }
  88.     ip rule add fwmark $ID/0xf00 table $ID priority 90
  89.  
  90.     initTable
  91.     ipset create vpnrouting$ID hash:ip
  92.  
  93.     echo "#!/bin/sh" > $FIREWALL_ROUTING # new routing file
  94.     echo "iptables -t mangle -A PREROUTING -m set --match-set vpnrouting$ID dst,src -j MARK --set-mark $ID/0xf00" >> $FIREWALL_ROUTING
  95.  
  96.     # example of routing_val: 1<2<8.8.8.8<1>1<1<1.2.3.4<0>1<3<domain.com<0> (enabled<type<domain_or_IP<kill_switch>)
  97.     for i in $(echo "$(NG vpn_"$SERVICE"_routing_val)" | tr ">" "\n"); do
  98.         VAL1=$(echo $i | cut -d "<" -f1)
  99.         VAL2=$(echo $i | cut -d "<" -f2)
  100.         VAL3=$(echo $i | cut -d "<" -f3)
  101.  
  102.         # only if rule is enabled
  103.         [ "$VAL1" -eq 1 ] && {
  104.             case "$VAL2" in
  105.                 1)  # from source
  106.                     $LOGS "Type: $VAL2 - add $VAL3"
  107.                     [ "$(echo $VAL3 | grep -)" ] && { # range
  108.                         echo "iptables -t mangle -A PREROUTING -m iprange --src-range $VAL3 -j MARK --set-mark $ID/0xf00" >> $FIREWALL_ROUTING
  109.                     } || {
  110.                         echo "iptables -t mangle -A PREROUTING -s $VAL3 -j MARK --set-mark $ID/0xf00" >> $FIREWALL_ROUTING
  111.                     }
  112.                 ;;
  113.                 2)  # to destination
  114.                     $LOGS "Type: $VAL2 - add $VAL3"
  115.                     echo "iptables -t mangle -A PREROUTING -d $VAL3 -j MARK --set-mark $ID/0xf00" >> $FIREWALL_ROUTING
  116.                 ;;
  117.                 3)  # to domain
  118.                     $LOGS "Type: $VAL2 - add $VAL3"
  119.                     echo "ipset=/$VAL3/vpnrouting$ID" >> $DNSMASQ_IPSET
  120.                     # try to add ipset rule using forced query to DNS server
  121.                     #nslookup $VAL3 2>/dev/null
  122.  
  123.                     DNSMASQ=1
  124.                 ;;
  125.                 *) continue ;;
  126.             esac
  127.         }
  128.     done
  129.  
  130.     chmod 700 $FIREWALL_ROUTING
  131.     RESTART_FW=1
  132.  
  133.     [ "$DNSMASQ" -eq 1 ] && {
  134.         NS vpn_client"${ID#??}"_rdnsmasq=1
  135.         RESTART_DNSMASQ=1
  136.     }
  137.  
  138.     $LOGS "Completed routing policy configuration for openvpn-$SERVICE"
  139. }
  140.  
  141. checkRestart() {
  142.     [ "$RESTART_DNSMASQ" -eq 1 -o "$(NG "vpn_client"${ID#??}"_rdnsmasq")" -eq 1 ] && service dnsmasq restart
  143.     [ "$RESTART_FW" -eq 1 ] && service firewall restart
  144. }
  145.  
  146. checkPid() {
  147.     local PIDNO
  148.  
  149.     [ -f $PIDFILE ] && {
  150.         PIDNO=$(cat $PIDFILE)
  151.         cat "/proc/$PIDNO/cmdline" &>/dev/null
  152.  
  153.         [ $? -eq 0 ] && {
  154.             # priority has the last process
  155.             $LOGS "Killing previous process ..."
  156.             kill -9 $PIDNO
  157.             echo $PID > $PIDFILE
  158.  
  159.             [ $? -ne 0 ] && {
  160.                 $LOGS "Could not create PID file"
  161.                 exit 0
  162.             }
  163.         } || {
  164.             # process not found assume not running
  165.             echo $PID > $PIDFILE
  166.             [ $? -ne 0 ] && {
  167.                 $LOGS "Could not create PID file"
  168.                 exit 0
  169.             }
  170.         }
  171.     } || {
  172.         echo $PID > $PIDFILE
  173.         [ $? -ne 0 ] && {
  174.             $LOGS "Could not create PID file"
  175.             exit 0
  176.         }
  177.     }
  178. }
  179.  
  180.  
  181. ###################################################
  182.  
  183.  
  184. find_iface
  185. checkPid
  186. VPN_REDIR=$(NG vpn_"$SERVICE"_rgw)
  187.  
  188. [ "$script_type" == "route-up" -a "$VPN_REDIR" -lt 2 ] && {
  189.     $LOGS "Skipping, $SERVICE not in routing policy mode"
  190.     checkRestart
  191.     exit 0
  192. }
  193.  
  194. [ "$script_type" == "route-pre-down" ] && {
  195.     stopRouting
  196. }
  197.  
  198. [ "$script_type" == "route-up" ] && {
  199.     startRouting
  200. }
  201.  
  202. checkRestart
  203.  
  204. ip route flush cache
  205.  
  206. rm -f $PIDFILE &>/dev/null
  207.  
  208. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement