How changlan/kytan works
Project type and purpose
• Kytan is a Rust-based, peer-to-peer VPN tool offering server and client modes. It ships as a single CLI binary that creates a virtual network interface (TUN/TAP), encapsulates/decapsulates IP packets, and sends them over UDP sockets with optional encryption and NAT traversal.
File structure
• Cargo.toml, .travis.yml, README.md, LICENSE at top level.
• src/main.rs: entry point, kicks off either server or client.
• src/cli.rs: command-line parsing (modes, flags, logging).
• src/device.rs: TUN/TAP interface setup and read/write.
• src/network.rs: UDP socket creation, peer management, NAT punch-through or hole-punching logic.
• src/packet.rs: packet framing, encryption/decryption, ordering, sequence numbers.
• src/utils.rs: logging, config loading, error handling, common helpers.
Read README for additional architecture clues
• Two operational modes: server (listens for peers, sets up NAT masquerade) and client (connects to server).
• Uses iptables externally for NAT on the server.
• Minimal config and multi-platform support.
a. Main components:
• CLI layer (cli.rs)
• Core engine
– Device Manager (device.rs)
– Packet Processor (packet.rs)
– Network Engine (network.rs)
• Utility services (utils.rs)
• OS Kernel/Network Stack (TUN/TAP driver, UDP stack, iptables)
b. Relationships & interactions:
• User invokes CLI → CLI parses flags/mode → instantiates Core engine
• Core engine starts Device Manager → creates a virtual interface (e.g., /dev/net/tun)
• Device Manager ↔ Packet Processor: reads raw IP packets from TUN, passes to Packet Processor for encapsulation
• Packet Processor ↔ Network Engine: SEND: encrypts and frames data, hands to Network Engine to send over UDP. RECEIVE: Network Engine receives UDP datagrams, passes to Packet Processor for decryption/decapsulation.
• Packet Processor → Device Manager: writes decapsulated IP packets back into the TUN device → OS Kernel routes them to the local network stack or other applications.
• Utilities provide logging, config, error handling to all modules.
• Server mode adds iptables/NAT setup step (external to the binary, shown as configuration on the OS).
c. Architectural patterns and principles:
• Modular separation of concerns: CLI, device abstraction, networking, and packet processing in distinct modules.
• Single binary with runtime mode selection (server vs. client).
• Synchronous main loop or async reactor pattern (depending on implementation) for multiplexing device I/O and socket I/O.
• Extensibility points: you could swap out the encryption algorithm in packet.rs or support additional transports in network.rs.
d. Key technologies/frameworks:
• Rust language and Cargo build system
• TUN/TAP virtual network interface (OS kernel driver)
• UDP sockets for P2P transport
• (Planned) iptables commands for NAT on Linux
• Logging via the standard Rust log facade (controlled by RUST_LOG)
Final reminders
• Be granular: break out device, packet, and network handling instead of lumping them together.
• Integration with the OS kernel (virtual interface, NAT) as a first-class element.