How bastiangdg/virtual-private-network-p2 works
Purpose and Entry Points
This is a Python 3.12+ Linux VPN implementation using TUN interfaces and a password-authenticated client/server tunnel. The operational entry points are src/server/server.py for the VPN server and src/client/client.py for the client; both are launched as Python modules and require elevated privileges for TUN/network operations.
Runtime and Deployment Boundary
The system runs directly on Linux hosts with TUN support, managed through uv (pyproject.toml, uv.lock). Server and client are separate processes, potentially on different networks. They must share a password and communicate over the server address/port supplied to the client.
Server Control Plane
The server accepts client tunnel connections, authenticates or derives session protection from the shared password, and manages connected peers. Server-side coordination is centered in:
• src/server/server.py
• src/server/peer.py
• src/config.py
peer.py likely represents per-client connection/state ownership, while server.py owns listener lifecycle, packet dispatch, and peer routing.
Client Tunnel Endpoint
The client establishes the outbound connection to the server, configures its local TUN-facing behavior, and selects local versus global network operation:
• src/client/client.py
• src/config.py
• config/config.json
The --mode local|global argument is an important routing boundary: local mode targets a local-network deployment, while global mode supports a remote server path.
Packet Transport and Virtual Interface Flow
Traffic crosses two distinct planes: IP packets from the OS via TUN, and protected packets over the client/server transport. Shared communications code bridges these boundaries:
• src/comms.py
• src/client/client.py
• src/server/server.py
Expected primary flow is: OS/TUN packet → client encapsulation/protection → network transport → server validation/decapsulation → destination peer or server-side TUN/network path; return traffic follows the reverse direction.
Cryptographic Boundary
Encryption and packet protection are centralized rather than embedded solely in endpoint logic:
• src/crypto.py
• src/comms.py
• tests/encrypt.py
The supplied password is the trust boundary between client and server. Endpoint code should treat plaintext TUN packets as trusted only after successful transport authentication/decryption and should isolate crypto framing from routing decisions.
Configuration and Peer Identity
Static/default configuration and peer-oriented settings are kept outside runtime endpoint modules:
• config/config.json
• config/peer.json
• src/config.py
These files likely define tunnel/network defaults and peer parameters consumed by both sides. Configuration is therefore a key boundary between deployment-specific addressing and reusable tunnel/crypto/transport logic.
Components 12
Client host
- Client OS network stack · Injects and receives IP packets through TUN.
- Client endpoint · Creates the outbound tunnel and applies local or global mode.
src/client/client.py - Client TUN interface
- Tunnel defaults
config/config.json
Protected tunnel
- Packet transport · Bridges plaintext IP packets and protected transport frames.
src/comms.py - Crypto protection · Uses the shared password to protect and validate tunnel traffic.
src/crypto.py - Network connection · Connects the client to the configured server address and port.
Server host
- Peer settings
config/peer.json - Server listener and router · Owns listener lifecycle, dispatch, and peer routing.
src/server/server.py - Connected peer state · Owns an individual client connection and routing state.
src/server/peer.py - Server TUN/network path · Delivers validated tunnel packets to the server-side network.
Other components
- Configuration loader · Supplies shared tunnel and peer settings.
src/config.py
Connections 20
- Client OS network stack → Client TUN interface: IP packets
- Client TUN interface → Client endpoint: plaintext packets
- Tunnel defaults → Configuration loader: loads
- Peer settings → Configuration loader: loads
- Configuration loader → Client endpoint: mode and endpoint settings
- Configuration loader → Server listener and router: tunnel and peer settings
- Client endpoint → Packet transport: encapsulate packets
- Packet transport → Crypto protection: protect frames
- Crypto protection → Network connection: authenticated ciphertext
- Network connection → Server listener and router: tunnel traffic
- Server listener and router → Crypto protection: validate and decrypt
- Server listener and router → Connected peer state: dispatch by peer
- Connected peer state → Server TUN/network path: routed IP packets
- Server TUN/network path → Connected peer state: return IP packets
- Connected peer state → Server listener and router: return routing
- Server listener and router → Network connection: protected return traffic
- Network connection → Crypto protection: ciphertext
- Crypto protection → Packet transport: verified frames
- Packet transport → Client endpoint: decapsulated packets
- Client endpoint → Client TUN interface: inject IP packets