Advertisement
Guest User

Untitled

a guest
Aug 10th, 2022
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -u
  4. # Debug
  5. # set -x
  6.  
  7. # This script should run only once at startup.
  8. if [[ -e '/tmp/openvpn/_done' ]]; then
  9. exit
  10. fi
  11.  
  12. # We need to know default route anyway, so proceeding without the knowledge
  13. # is useless.
  14. timer=0
  15. while true; do
  16. if ! ip r | grep default; then
  17. [[ $timer -ge 120 ]] && exit 1
  18. sleep 2
  19. (( timer=timer+2 ))
  20. else
  21. break
  22. fi
  23. done
  24. unset timer
  25.  
  26. # Mount net_cls cgroup and setup it.
  27. mkdir -p /sys/fs/cgroup/net_cls
  28. mount -t cgroup -onet_cls net_cls /sys/fs/cgroup/net_cls
  29. cgcreate -t root:novpn -a root:novpn -d 775 -f 664 -s 664 -g net_cls:novpn
  30. echo 0x00110011 > /sys/fs/cgroup/net_cls/novpn/net_cls.classid
  31.  
  32. # Apply nftables rules.
  33. nft -f /etc/nftables.conf
  34.  
  35. # Find out default gateway and its device. The first matched
  36. #'default' will be used.
  37. read -r ip dev <<<"$(ip r | awk '/default/ {
  38. split($0, pieces, "[[:space:]]")
  39. for (i=1;i <= length(pieces);i++) {
  40. if (pieces[i] == "via") {
  41. ipaddr=pieces[i+1]
  42. } else
  43. if (pieces[i] == "dev") {
  44. phydev=pieces[i+1]
  45. }
  46. }
  47. printf "%s %s", ipaddr, phydev
  48. exit 0
  49. }')"
  50. # Check in case something unexpected occured (
  51. # happened to me before).
  52. if [[ -z $ip ]] || [[ -z $dev ]]; then
  53. echo "Retrieving default gateway and/or device" \
  54. "failed for some reason." 1>&2
  55. exit 1
  56. fi
  57.  
  58. # Configure additional routing table
  59. ip route add default via "$ip" table novpn
  60. ip route add 192.168.1.0/24 dev "$dev" table novpn
  61. ip rule add fwmark 11 table novpn
  62.  
  63. # If successful, mark as run.
  64. mkdir -p /tmp/openvpn
  65. touch /tmp/openvpn/_done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement