From 794147999459f94e15b53c50cd5d8d34b457efab Mon Sep 17 00:00:00 2001 From: Owen Date: Fri, 21 Nov 2025 15:07:19 -0500 Subject: [PATCH] Basic dns proxy working Former-commit-id: f0886d5ac6fe04eb92a86bcffa56e029fcffcbfa --- olm-binary.REMOVED.git-id | 1 + olm/dns_proxy.go | 42 ++++++++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 olm-binary.REMOVED.git-id diff --git a/olm-binary.REMOVED.git-id b/olm-binary.REMOVED.git-id new file mode 100644 index 0000000..7c4bcb9 --- /dev/null +++ b/olm-binary.REMOVED.git-id @@ -0,0 +1 @@ +c94f554cb06ba7952df7cd58d7d8620fd1eddc82 \ No newline at end of file diff --git a/olm/dns_proxy.go b/olm/dns_proxy.go index ce8e55a..24e30a9 100644 --- a/olm/dns_proxy.go +++ b/olm/dns_proxy.go @@ -42,8 +42,6 @@ type DNSProxy struct { ctx context.Context cancel context.CancelFunc wg sync.WaitGroup - - mutex sync.RWMutex } // NewDNSProxy creates a new DNS proxy @@ -264,6 +262,10 @@ func (p *DNSProxy) queryUpstream(server string, query []byte, timeout time.Durat func (p *DNSProxy) runPacketSender() { defer p.wg.Done() + // MessageTransportHeaderSize is the offset used by WireGuard device + // for reading/writing packets to the TUN interface + const offset = 16 + for { select { case <-p.ctx.Done(): @@ -279,20 +281,32 @@ func (p *DNSProxy) runPacketSender() { continue } - // Convert packet to bytes - view := pkt.ToView() - packetData := view.AsSlice() + // Extract packet data as slices + slices := pkt.AsSlices() + if len(slices) > 0 { + // Flatten all slices into a single packet buffer + var totalSize int + for _, slice := range slices { + totalSize += len(slice) + } - // Make a copy and write directly back to the TUN device - // This bypasses WireGuard - the packet goes straight back to the host - buf := make([]byte, len(packetData)) - copy(buf, packetData) + // Allocate buffer with offset space for WireGuard transport header + // The first 'offset' bytes are reserved for the transport header + buf := make([]byte, offset+totalSize) - // Write packet back to TUN device - bufs := [][]byte{buf} - _, err := p.tunDevice.Write(bufs, 0) - if err != nil { - logger.Error("Failed to write DNS response to TUN: %v", err) + // Copy packet data after the offset + pos := offset + for _, slice := range slices { + copy(buf[pos:], slice) + pos += len(slice) + } + + // Write packet to TUN device + // offset=16 indicates packet data starts at position 16 in the buffer + _, err := p.tunDevice.Write([][]byte{buf}, offset) + if err != nil { + logger.Error("Failed to write DNS response to TUN: %v", err) + } } pkt.DecRef()