Guest User

Untitled

a guest
May 21st, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. if ip netns list | grep "vpnspace" > /dev/null; then
  4. echo "Namespace already exists."
  5. exit 1
  6. fi
  7.  
  8. # IP address of your external interface
  9. INTERNET_IP=192.168.0.101
  10. # VPN configuration
  11. VPN_CONFIG=/etc/openvpn/pia.conf
  12. # IPs for the veths. Those should be from the same range.
  13. VETH0_IP=172.16.0.1
  14. VETH1_IP=172.16.0.2
  15. VETH_BITS=12
  16.  
  17. # Create the namespace called "vpnspace"
  18. echo "Creating namespace..."
  19. ip netns add vpnspace
  20.  
  21. # Bring up the loopback inside the namespace
  22. ip netns exec vpnspace ip addr add 127.0.0.1/8 dev lo
  23. ip netns exec vpnspace ip link set dev lo up
  24.  
  25. # Create linked, virtual interfaces
  26. echo "Creating linked veth..."
  27. ip link add veth0 type veth peer name veth1
  28.  
  29. # Move one linked eth to the namespace
  30. ip link set veth1 netns vpnspace
  31.  
  32. # Assign an IP address to the veth that will stay in global namespace and bring it up
  33. ip addr add $VETH0_IP/$VETH_BITS dev veth0
  34. ip link set dev veth0 up
  35.  
  36. # Do the same for the veth inside the namespace
  37. ip netns exec vpnspace ip addr add $VETH1_IP/$VETH_BITS dev veth1
  38. ip netns exec vpnspace ip link set dev veth1 up
  39.  
  40. # Set routing from namespace to general
  41. echo "Setting routing..."
  42. ip netns exec vpnspace ip route add default via $VETH0_IP dev veth1
  43.  
  44. # Set up SNAT to handle packets coming from namespace
  45. #iptables -D INPUT \! -i veth0 -s $VETH0_IP/28 -j DROP
  46. #iptables -t nat -D POSTROUTING -s $VETH0_IP/28 -o eth0 -j MASQUERADE
  47. iptables -t nat -A POSTROUTING -s $VETH0_IP/$VETH_BITS -o eth0 -j SNAT --to-source $INTERNET_IP
  48.  
  49. # Enable forwarding
  50. sysctl -q net.ipv4.ip_forward=1
  51.  
  52. # Configure DNS
  53. #mkdir -p /etc/netns/vpnspace
  54. #echo 'nameserver 8.8.8.8' > /etc/netns/vpnspace/resolv.conf
  55.  
  56.  
  57. # Start the VPN
  58. echo "Starting OpenVPN inside namespace..."
  59. ip netns exec vpnspace openvpn --daemon --config "$VPN_CONFIG"
  60.  
  61. echo "Done."
Add Comment
Please, Sign In to add comment