How masterking32/masterdnsvpn works
Purpose and Executables
MasterDnsVPN is a Go DNS tunnel that carries TCP traffic in DNS queries/responses, exposing a local SOCKS4/5 proxy or fixed TCP forwarding client-side. It uses custom framing, encryption, ARQ, resolver multipath, and duplication to tolerate lossy/censored DNS paths.
Entry points:
• Client process: cmd/client/main.go
• Authoritative DNS tunnel server: cmd/server/main.go
• Runtime configuration: internal/config/client.go, internal/config/server.go
Client Ingress and Stream Lifecycle
The client accepts local application traffic through TCP/SOCKS listeners, creates multiplexed tunnel streams, and returns ordered tunnel data to the local connection. Optional local DNS can route DNS requests through the established tunnel.
Key boundaries:
• Local proxy protocol and connection handling: internal/client/socks_manager.go, internal/client/tcp_listener.go
• Logical session and stream state: internal/client/session.go, internal/client/stream_client.go
• Optional local DNS listener/cache-facing path: internal/client/dns_listener.go, internal/client/handlers/dns_handlers.go
Client Tunnel Transport Runtime
Client workers transform stream/control payloads into tunnel packets, dispatch DNS requests, and process responses asynchronously. The resolver balancer selects among configured public resolvers, duplicates critical packets, tracks health/latency/loss, and supports stream-level failover. MTU probing establishes practical payload limits per resolver.
Core runtime:
• Dispatch and asynchronous I/O orchestration: internal/client/dispatcher.go, internal/client/async_runtime.go, internal/client/tunnel_runtime.go
• Resolver balancing and probing: internal/client/balancer.go, internal/client/mtu.go, internal/client/ping_manager.go
• Packet-type handlers and packed controls: internal/client/handlers/registry.go, internal/client/handlers/packed_control_handler.go
Protocol, Reliability, and Security Boundary
Tunnel content is represented by a custom VPN protocol, then encoded into DNS-compatible labels. ARQ supplies sequencing, ACK/NACK, retransmission, reordering, TTL, and terminal-stream behavior; fragmentation stores support payloads exceeding one DNS message. Compression and encryption are negotiated/configured around this transport.
Protocol layers:
• Wire packet construction, parsing, and packing: internal/vpnproto/builder.go, internal/vpnproto/parser.go, internal/vpnproto/packing.go
• ARQ reliability state machine: internal/arq/arq.go
• Encryption and codecs: internal/security/codec.go, internal/security/encryption_key.go, internal/basecodec/codec.go
• Fragment assembly: internal/fragmentstore/store.go
Server DNS Front Door and Session Runtime
The server binds DNS UDP (and container deployment exposes TCP/UDP 53), parses incoming DNS tunnel queries, validates domain/session/cookie state, and routes ordering-sensitive work through deferred session processing. Session cleanup and invalid-cookie tracking protect lifecycle and resource boundaries.
Server stages:
• UDP socket, ingress queue, and runtime: internal/udpserver/server.go, internal/udpserver/server_ingress.go, internal/udpserver/server_runtime.go
• Session creation, lookup, and cleanup: internal/udpserver/session.go, internal/udpserver/server_session.go, internal/udpserver/session_cleanup_test.go
• Deferred/setup processing and DNS tunnel assembly: internal/udpserver/deferred_session.go, internal/udpserver/dns_tunnel.go
Egress and DNS Services
For SOCKS mode, the server establishes outbound target connections from client stream requests; it may instead chain through an external SOCKS5 upstream. Tunnelled DNS requests are separately forwarded to configured recursive DNS upstreams, deduplicated in-flight, cached, and returned through DNS responses.
Relevant paths:
• TCP stream egress and SOCKS upstream: internal/udpserver/stream_server.go, internal/udpserver/socks5_upstream.go
• DNS parse/response transport: internal/dnsparser/parser.go, internal/dnsparser/response.go
• DNS cache and request coalescing: internal/dnscache/store.go, internal/inflight/manager.go
Deployment and External Dependencies
The material runtime is Go 1.24+; networking is primarily local TCP plus DNS over UDP, with DNS TCP exposed in Docker deployments. External services are public DNS resolvers, delegated authoritative DNS for the tunnel domain, optional DNS upstream resolvers, and optional external SOCKS5 egress.
Deployment artifacts:
• Container image and startup behavior: docker/Dockerfile, docker/docker-entrypoint.sh, docker/docker-compose.yml
• Sample operational configs: client_config.toml.simple, server_config.toml.simple, client_resolvers.simple
Main entry points: deferred_session.go Server session processor · session.go Client sessions and streams · socks_manager.go SOCKS and TCP ingress · server_runtime.go DNS ingress runtime
Components 23
Client runtime
- Client process
cmd/client/main.go - Client configuration
internal/config/client.go - Local applications
- SOCKS and TCP ingress · Accepts local streams and maps them to tunnel streams.
internal/client/socks_manager.go - Client sessions and streams
internal/client/session.go - Local DNS tunnel service
internal/client/dns_listener.go - Client transport runtime · Dispatches tunnel work and handles asynchronous DNS responses.
internal/client/dispatcher.go - Resolver balancer · Selects, probes, duplicates, and fails over public resolvers.
internal/client/balancer.go
Tunnel protocol
- VPN packet codec · Builds, parses, packs, and DNS-label-encodes tunnel packets.
internal/vpnproto/builder.go - ARQ and fragments · Provides ordering, ACK/NACK, retries, TTL, and reassembly.
internal/arq/arq.go - Encryption and codecs
internal/security/codec.go
Server runtime
- Server process
cmd/server/main.go - Server configuration
internal/config/server.go - DNS ingress runtime · Receives DNS queries and queues tunnel ingress.
internal/udpserver/server_runtime.go - Server session processor · Validates cookies and routes ordering-sensitive deferred work.
internal/udpserver/deferred_session.go - TCP stream egress · Establishes requested target streams or uses SOCKS5 upstream.
internal/udpserver/stream_server.go - Tunnel DNS service · Coalesces in-flight requests, caches answers, and returns DNS responses.
internal/dnscache/store.go - Docker deployment · Exposes DNS over UDP and TCP port 53.
docker/Dockerfile
Networks and egress
- Public DNS resolvers
- Delegated tunnel domain
- Target TCP services
- Recursive DNS upstreams
- External SOCKS5
Connections 26
- Client configuration → Client process: configures
- Client process → SOCKS and TCP ingress: starts
- Client process → Client transport runtime: starts
- Local applications → SOCKS and TCP ingress: TCP/SOCKS
- SOCKS and TCP ingress → Client sessions and streams: opens streams
- Client sessions and streams → Client transport runtime: payloads and controls
- Local DNS tunnel service → Client sessions and streams: tunnelled DNS
- Client transport runtime → ARQ and fragments: reliable packets
- ARQ and fragments → Encryption and codecs: payload frames
- Encryption and codecs → VPN packet codec: protected content
- VPN packet codec → Resolver balancer: DNS queries
- Resolver balancer → Public DNS resolvers: multipath DNS
- Public DNS resolvers → Delegated tunnel domain: delegated queries
- Delegated tunnel domain → DNS ingress runtime: DNS UDP/TCP
- Docker deployment → DNS ingress runtime: runs and exposes
- Server configuration → Server process: configures
- Server process → DNS ingress runtime: starts
- DNS ingress runtime → Server session processor: tunnel queries
- Server session processor → VPN packet codec: decode/encode
- Server session processor → ARQ and fragments: ARQ state
- Server session processor → TCP stream egress: stream requests
- TCP stream egress → Target TCP services: outbound TCP
- TCP stream egress → External SOCKS5: optional proxy chain
- Server session processor → Tunnel DNS service: DNS requests
- Tunnel DNS service → Recursive DNS upstreams: recursive queries
- Client sessions and streams → SOCKS and TCP ingress: ordered return data