# bastiangdg/virtual-private-network-p2 architecture

> Architecture diagram of the GitHub repository bastiangdg/virtual-private-network-p2, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bastiangdg/virtual-private-network-p2
- Repository: https://github.com/bastiangdg/virtual-private-network-p2

## Overview

#### 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.

Main entry points: [`src/server/server.py`](https://github.com/bastiangdg/virtual-private-network-p2/blob/main/src/server/server.py) (Server listener and router), [`src/client/client.py`](https://github.com/bastiangdg/virtual-private-network-p2/blob/main/src/client/client.py) (Client endpoint), [`src/crypto.py`](https://github.com/bastiangdg/virtual-private-network-p2/blob/main/src/crypto.py) (Crypto protection), [`src/comms.py`](https://github.com/bastiangdg/virtual-private-network-p2/blob/main/src/comms.py) (Packet transport)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client host"]
  node_client_os{{"Client OS network stack<br/>Linux networking"}}
  node_client_app["Client endpoint<br/>Python module<br/>[client.py]"]
  node_client_tun{{"Client TUN interface<br/>virtual interface"}}
  node_tunnel_config["Tunnel defaults<br/>deployment config<br/>[config.json]"]
end

subgraph group_transport["Protected tunnel"]
  node_comms["Packet transport<br/>shared communications<br/>[comms.py]"]
  node_crypto["Crypto protection<br/>shared cryptography<br/>[crypto.py]"]
  node_network(("Network connection<br/>TCP/transport link"))
end

subgraph group_server["Server host"]
  node_peer_config["Peer settings<br/>deployment config<br/>[peer.json]"]
  node_server_app["Server listener and router<br/>Python module<br/>[server.py]"]
  node_peer["Connected peer state<br/>per-client session<br/>[peer.py]"]
  node_server_tun{{"Server TUN/network path<br/>Linux networking"}}
end

node_config_module["Configuration loader<br/>shared module<br/>[config.py]"]

node_client_os -->|"IP packets"| node_client_tun
node_client_tun -->|"plaintext packets"| node_client_app
node_tunnel_config -->|"loads"| node_config_module
node_peer_config -->|"loads"| node_config_module
node_config_module -->|"mode and endpoint settings"| node_client_app
node_config_module -->|"tunnel and peer settings"| node_server_app
node_client_app -->|"encapsulate packets"| node_comms
node_comms -->|"protect frames"| node_crypto
node_crypto -->|"authenticated ciphertext"| node_network
node_network -->|"tunnel traffic"| node_server_app
node_server_app -->|"validate and decrypt"| node_crypto
node_server_app -->|"dispatch by peer"| node_peer
node_peer -->|"routed IP packets"| node_server_tun
node_server_tun -->|"return IP packets"| node_peer
node_peer -->|"return routing"| node_server_app
node_server_app -->|"protected return traffic"| node_network
node_network -->|"ciphertext"| node_crypto
node_crypto -->|"verified frames"| node_comms
node_comms -->|"decapsulated packets"| node_client_app
node_client_app -->|"inject IP packets"| node_client_tun

click node_client_app "https://github.com/bastiangdg/virtual-private-network-p2/blob/main/src/client/client.py"
click node_config_module "https://github.com/bastiangdg/virtual-private-network-p2/blob/main/src/config.py"
click node_tunnel_config "https://github.com/bastiangdg/virtual-private-network-p2/blob/main/config/config.json"
click node_peer_config "https://github.com/bastiangdg/virtual-private-network-p2/blob/main/config/peer.json"
click node_comms "https://github.com/bastiangdg/virtual-private-network-p2/blob/main/src/comms.py"
click node_crypto "https://github.com/bastiangdg/virtual-private-network-p2/blob/main/src/crypto.py"
click node_server_app "https://github.com/bastiangdg/virtual-private-network-p2/blob/main/src/server/server.py"
click node_peer "https://github.com/bastiangdg/virtual-private-network-p2/blob/main/src/server/peer.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_client_os,node_client_app,node_client_tun,node_tunnel_config toneBlue
class node_comms,node_crypto,node_network toneAmber
class node_peer_config,node_server_app,node_peer,node_server_tun toneMint
class node_config_module toneNeutral
```

## Components

### Client host

- **Client OS network stack**: Linux networking · Injects and receives IP packets through TUN.
- [Client endpoint](https://github.com/bastiangdg/virtual-private-network-p2/blob/main/src/client/client.py): Python module · Creates the outbound tunnel and applies local or global mode. · `src/client/client.py`
- **Client TUN interface**: virtual interface
- [Tunnel defaults](https://github.com/bastiangdg/virtual-private-network-p2/blob/main/config/config.json): deployment config · `config/config.json`

### Protected tunnel

- [Packet transport](https://github.com/bastiangdg/virtual-private-network-p2/blob/main/src/comms.py): shared communications · Bridges plaintext IP packets and protected transport frames. · `src/comms.py`
- [Crypto protection](https://github.com/bastiangdg/virtual-private-network-p2/blob/main/src/crypto.py): shared cryptography · Uses the shared password to protect and validate tunnel traffic. · `src/crypto.py`
- **Network connection**: TCP/transport link · Connects the client to the configured server address and port.

### Server host

- [Peer settings](https://github.com/bastiangdg/virtual-private-network-p2/blob/main/config/peer.json): deployment config · `config/peer.json`
- [Server listener and router](https://github.com/bastiangdg/virtual-private-network-p2/blob/main/src/server/server.py): Python module · Owns listener lifecycle, dispatch, and peer routing. · `src/server/server.py`
- [Connected peer state](https://github.com/bastiangdg/virtual-private-network-p2/blob/main/src/server/peer.py): per-client session · Owns an individual client connection and routing state. · `src/server/peer.py`
- **Server TUN/network path**: Linux networking · Delivers validated tunnel packets to the server-side network.

### Other components

- [Configuration loader](https://github.com/bastiangdg/virtual-private-network-p2/blob/main/src/config.py): shared module · Supplies shared tunnel and peer settings. · `src/config.py`

## Connections

- 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
