Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- #DEBUG= # uncomment/comment to enable/disable debug mode
- # name: merlin-ovpn-sync-routes.sh
- # version: 2.0.0, 22-jul-2022, by eibgrad
- # purpose: synchronize openvpn client and server routing tables
- # type(s): openvpn-event
- # href: https://tinyurl.com/2jenwbe5
- # installation:
- # 1. enable jffs custom scripts and configs (administration->system)
- # 2. ssh to router and copy/paste the following command:
- # curl -kLs bit.ly/merlin-installer|tr -d '\r'|sh -s kTThBV46
- # 3. reboot
- SCRIPTS_DIR='/jffs/scripts'
- SCRIPT1="$SCRIPTS_DIR/merlin-ovpn-sync-routes.sh"
- SCRIPT2="$SCRIPTS_DIR/openvpn-event"
- mkdir -p $SCRIPTS_DIR
- # ---------------------- begin merlin-ovpn-sync-routes ----------------------- #
- cat << 'EOF' > $SCRIPT1
- #!/bin/sh
- #set -x # comment/uncomment to disable/enable debug mode
- {
- # required for serialization when reentry is possible
- LOCK="/tmp/var/lock/$(basename $0).lock"
- acquire_lock() { while ! mkdir $LOCK &>/dev/null; do sleep 2; done; }
- release_lock() { rmdir $LOCK &>/dev/null; }
- # exit (any concurrent instance(s) may now run)
- exit_0() { release_lock; exit 0; }
- # one instance at a time
- acquire_lock
- # only relevant for routed (tun) openvpn server 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
- # force routing system to recognize any changes
- [ ${routing_changed+x} ] && ip route flush cache
- exit_0
- } 2>&1 | logger -t $(basename $0 .sh)[$$]
- EOF
- [ ${DEBUG+x} ] && sed -ri '2 s/^#(set -x)/\1/' $SCRIPT1
- chmod +x $SCRIPT1
- echo "installed: $SCRIPT1"
- # ----------------------- end merlin-ovpn-sync-routes ------------------------ #
- # --------------------------- begin openvpn-event ---------------------------- #
- create_script() {
- cat << 'EOF' > $SCRIPT2
- #!/bin/sh
- #set -x # comment/uncomment to disable/enable debug mode
- {
- $SCRIPT1
- } 2>&1 | logger -t $(basename $0)[$$]
- EOF
- [ ${DEBUG+x} ] && sed -ri '2 s/^#(set -x)/\1/' $SCRIPT2
- sed "s:\$SCRIPT1:$SCRIPT1:g" -i $SCRIPT2
- chmod +x $SCRIPT2
- }
- if [ -f $SCRIPT2 ]; then
- echo "error: $SCRIPT2 already exists; requires manual installation"
- else
- create_script
- echo "installed: $SCRIPT2"
- fi
- # ---------------------------- end openvpn-event ----------------------------- #
Add Comment
Please, Sign In to add comment