Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- set -x # uncomment/comment to enable/disable debug mode
- PROJECT_REPO="/jffs/scripts/x3mRouting"
- scr_name="$(basename "$0")[$$]"
- #Determine Caller
- case "$1" in
- "tun11")
- vpn_name="client1"
- ;;
- "tun12")
- vpn_name="client2"
- ;;
- "tun13")
- vpn_name="client3"
- ;;
- "tun14")
- vpn_name="client4"
- ;;
- "tun15")
- vpn_name="client5"
- ;;
- "tun21")
- vpn_name="server1"
- ;;
- "tun22")
- vpn_name="server2"
- ;;
- *)
- vpn_name=""
- ;;
- esac
- # Call appropriate script based on script_type
- vpn_script_name="vpn$vpn_name-$script_type"
- vpn_script_log="/tmp/vpn${vpn_name}_state"
- VPN_UNIT=$(echo "$dev" | awk '{ string=substr($0, 5, 5); print string; }')
- [ -s /jffs/addons/x3mRouting/updown-dns.sh ] && sh /jffs/addons/x3mRouting/updown-dns.sh "$VPN_UNIT" "$script_type"
- [ -s /jffs/addons/x3mRouting/x3mvpnrouting.sh ] && sh /jffs/addons/x3mRouting/x3mvpnrouting.sh "$VPN_UNIT" "$script_type"
- # Check script state
- vpn_script_state=$(cat $vpn_script_log 2>/dev/null)
- if [ "$vpn_script_name" = "$vpn_script_state" ]; then
- echo "VPN script $vpn_script_name already run" | logger -t "$scr_name"
- exit 0
- fi
- # Execute and log script state
- if [ -f "$PROJECT_REPO/$vpn_script_name" ]; then
- echo "$vpn_script_name" >"$vpn_script_log"
- sh "$PROJECT_REPO/$vpn_script_name" "$*" && logger -t "$scr_name" "Running $PROJECT_REPO/$vpn_script_name $*"
- #####################################################################################
- # only relevant for routed (tun) openvpn server route-up event
- [[ "${dev:0:4}" == 'tun2' && "$script_type" == 'up' ]] || exit_0
- # ensure every openvpn client's routing policy table ...
- for i in 1 2 3 4 5; do
- [ "$(ip route show table ovpnc${i})" ] || continue
- # ... contains the server's ip network on the tunnel
- while read route; do
- ip route add $route table ovpnc${i} 2>/dev/null && routing_changed=
- done << EOR
- $(ip route | grep $dev)
- EOR
- done
- # ensure every wireguardvpn client's routing policy table ...
- for i in 1 2 3 4 5; do
- [ "$(ip route show table 12${i})" ] || continue
- # ... contains the server's ip network on the tunnel
- while read route; do
- ip route add $route table 12${i} 2>/dev/null && routing_changed=
- done << EOR
- $(ip route | grep $dev)
- EOR
- done
- # force routing system to recognize any changes
- [ ${routing_changed+x} ] && ip route flush cache
- #####################################################################################
- else
- logger -t "$scr_name" "No scripts found to run for openvpn-event: $vpn_script_name"
- echo "${vpn_script_name}-NOSCRIPT" >"$vpn_script_log" # (or nvram set vpn_script_state="${vpn_script_name}-NOSCRIPT"")
- exit 0
- fi
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement