TAP -> TUN, handle multiple peers behind one NAT

This commit is contained in:
2026-07-23 11:41:01 +02:00
parent fabe2614a5
commit 4d710fb72b
3 changed files with 49 additions and 35 deletions

View File

@@ -16,6 +16,7 @@ import (
"github.com/songgao/water"
)
// TODO: parse some sort of config+key file
const (
SERVER_IP = "172.20.12.47"
IFACE_NAME = "baalvpn"
@@ -69,13 +70,14 @@ func register() {
}
pubKey := privKey.PublicKey()
// TODO: this probably should be repeated until we get RESP_REGISTER
send(shared.BuildPkt(shared.REQ_REGISTER, pubKey.Bytes()))
}
func receivePackets() {
buffer := make([]byte, 50000)
for {
buffer := make([]byte, 50000)
n, addr, err := conn.ReadFromUDP(buffer)
n, addr, err := conn.ReadFromUDP(buffer[:])
if err != nil {
log.Println(err)
continue
@@ -88,7 +90,9 @@ func receivePackets() {
continue
}
go handleIncomingPkt(buffer[:n])
req := make([]byte, n)
copy(req, buffer[:n])
go handleIncomingPkt(req)
}
}
@@ -195,6 +199,7 @@ func getOrEstablishSessionKey(ip string) []byte {
}
// TODO: this definitely shouldnt block the main thread
// TODO: this should try like 10 times tops since server doesnt respond at all if it doesnt have the pubkey
for {
// request pubkey every 50ms
time.Sleep(50 * time.Millisecond)
@@ -233,7 +238,7 @@ func getOrEstablishSessionKey(ip string) []byte {
}
func setupInterface() {
config := water.Config{DeviceType: water.TAP}
config := water.Config{DeviceType: water.TUN}
config.Name = IFACE_NAME
var err error
@@ -261,16 +266,14 @@ func relayPackets() {
}
pkt = pkt[:n]
etherType := binary.BigEndian.Uint16(pkt[12:14])
payload := pkt[14:]
if etherType != 0x86dd { // IPv6
version := pkt[0] >> 4
if version != 6 {
continue
}
destIP := net.IP(payload[24:40]).String()
destIP := net.IP(pkt[24:40]).String()
if payload[24] == 0xff { // multicast
if pkt[24] == 0xff { // multicast
encryptedPkt, err := EncryptSym(multicastKey, pkt)
if err != nil {
panic(err)