This commit is contained in:
Roy Olav Purser 2021-02-25 13:17:46 +01:00
parent 896d1a7a3c
commit 8dc1ac818a
No known key found for this signature in database
GPG Key ID: 0BA77797F072BC52
2 changed files with 27 additions and 19 deletions

View File

@ -1,13 +1,21 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import subprocess,os import subprocess,os
newenv = os.environ.copy() newenv = os.environ.copy()
newenv["LD_PRELOAD"] = "/snacks/wireguard/bin/resolve.so" newenv["LD_PRELOAD"] = "/snacks/wireguard/bin/wireguard-resolve.so"
def default_devices():
with open("/proc/1/net/dev", "r") as f:
return f.read()
def vpn_devices():
with open("/proc/self/net/dev", "r") as f:
return f.read()
def wireguard(): def wireguard():
try: try:
os.mkdir("/run/netns") os.mkdir("/run/netns")
except FileExistsError: except FileExistsError:
pass pass
try: try:
os.symlink("/run/vpn/net", "/run/netns/vpn") os.symlink("/run/vpn/net", "/run/netns/vpn")
os.symlink("/proc/1/ns/net", "/run/netns/default") os.symlink("/proc/1/ns/net", "/run/netns/default")
@ -21,34 +29,30 @@ def wireguard():
with open("/proc/sys/net/ipv4/ping_group_range", "w") as f: with open("/proc/sys/net/ipv4/ping_group_range", "w") as f:
f.write("0 2147483647") f.write("0 2147483647")
with open("/proc/self/net/dev", "r") as f: if "veth-inner" in vpn_devices():
vpn_devices = f.read()
with open("/proc/1/net/dev", "r") as f:
default_devices = f.read()
if "veth-inner" in vpn_devices:
subprocess.run(["ip", "link", "del", "dev", "veth-inner"]) subprocess.run(["ip", "link", "del", "dev", "veth-inner"])
if "veth-outer" in vpn_devices: if "veth-outer" in vpn_devices():
subprocess.run(["ip", "link", "del", "dev", "veth-outer"]) subprocess.run(["ip", "link", "del", "dev", "veth-outer"])
if "vpn" in vpn_devices: if "vpn" in vpn_devices():
subprocess.run(["ip", "link", "del", "dev", "vpn"]) subprocess.run(["ip", "link", "del", "dev", "vpn"])
if "mynet0" not in vpn_devices():
subprocess.run(["ip", "link", "add", "name", "mynet0", "type", "bridge"])
if "veth-inner" in default_devices: if "veth-inner" in default_devices():
subprocess.run(["nsenter", "--net=/proc/1/ns/net", "ip", "link", "del", "dev", "veth-inner"]) subprocess.run(["nsenter", "--net=/proc/1/ns/net", "ip", "link", "del", "dev", "veth-inner"])
if "veth-outer" in default_devices: if "veth-outer" in default_devices():
subprocess.run(["nsenter", "--net=/proc/1/ns/net", "ip", "link", "del", "dev", "veth-outer"]) subprocess.run(["nsenter", "--net=/proc/1/ns/net", "ip", "link", "del", "dev", "veth-outer"])
if "vpn" in default_devices: if "vpn" in default_devices():
subprocess.run(["nsenter", "--net=/proc/1/ns/net", "ip", "link", "del", "dev", "vpn"]) subprocess.run(["nsenter", "--net=/proc/1/ns/net", "ip", "link", "del", "dev", "vpn"])
subprocess.run(["modprobe", "wireguard"]) subprocess.run(["modprobe", "wireguard"])
subprocess.run(["nsenter", "--net=/proc/1/ns/net", "ip", "link", "add", "dev", "vpn", "type", "wireguard"]) subprocess.run(["nsenter", "--net=/proc/1/ns/net", "ip", "link", "add", "dev", "vpn", "type", "wireguard"])
subprocess.run(["nsenter", "--net=/proc/1/ns/net", "ip", "link", "set", "dev", "vpn", "netns", "vpn"]) subprocess.run(["nsenter", "--net=/proc/1/ns/net", "ip", "link", "set", "dev", "vpn", "netns", "vpn"])
subprocess.run(["nsenter", "--net=/run/vpn/net", "/snacks/wireguard/scripts/inner.sh"], env=newenv)
subprocess.run(["nsenter", "--net=/run/vpn/net", "/snacks/wireguard/bin/inner.sh"], newenv) subprocess.run(["nsenter", "--net=/proc/1/ns/net", "/snacks/wireguard/scripts/outer.sh"], env=newenv)
subprocess.run(["nsenter", "--net=/proc/1/ns/net", "/snacks/wireguard/bin/outer.sh"], newenv)
try: try:
self_ns = os.readlink("/proc/1/ns/net") self_ns = os.readlink("/proc/self/ns/net")
vpn_ns = os.readlink("/run/vpn/net") vpn_ns = os.readlink("/run/vpn/net")
except Exception as e: except Exception as e:
print(e) print(e)

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env bash
ip link add name mynet0 type bridge
ip link add veth-inner type veth peer name veth-outer ip link add veth-inner type veth peer name veth-outer
ip link set dev veth-outer netns default
ip link set dev veth-inner master mynet0 ip link set dev veth-inner master mynet0
ip link set dev veth-inner up ip link set dev veth-inner up
@ -12,7 +12,11 @@ ip link set dev vpn up
ip addr flush dev vpn ip addr flush dev vpn
ip route flush dev vpn ip route flush dev vpn
echo ${VPN_IPV4_ADDRESS}
echo ${VPN_IPV6_ADDRESS}
ip addr add ${VPN_IPV4_ADDRESS} dev vpn ip addr add ${VPN_IPV4_ADDRESS} dev vpn
ip addr add ${VPN_IPV6_ADDRESS} dev vpn ip addr add ${VPN_IPV6_ADDRESS} dev vpn
ip -4 route add default dev vpn ip -4 route add default dev vpn
ip -6 route add default dev vpn ip -6 route add default dev vpn