# m-ahmad3/pychat-lan-secure architecture

> Architecture diagram of the GitHub repository m-ahmad3/pychat-lan-secure, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/m-ahmad3/pychat-lan-secure
- Repository: https://github.com/m-ahmad3/pychat-lan-secure

## Overview

1. **Overall shape**
   - This repo is a small two-process system: `Server.py` hosts the chat service and `Client.py` is a terminal-based client.
   - The architecture is classic client-server over TCP, intended for LAN use rather than internet-scale deployment.

2. **Runtime and dependencies**
   - The project is pure Python and depends on `cryptography` for Fernet symmetric encryption.
   - There is no separate database, message broker, or web framework; all state lives in memory inside the server process.

3. **Server as the central authority**
   - The server is the hub for all group logic: creating groups, admitting members, tracking online users, and enforcing admin actions.
   - It also owns the encryption handshake and mediates all message/file traffic between clients.

4. **Client responsibilities**
   - The client is a command-line interface that handles user input, sends commands/messages, and displays incoming group traffic.
   - The README indicates dual threading on the client side: one path for sending input and another for receiving messages asynchronously.

5. **Connection and session flow**
   - A client connects to the server by IP and port, then supplies a username and group name.
   - If the group does not exist, the first joiner becomes the admin; otherwise, joining is gated by the admin approval workflow.

6. **Security boundary**
   - All messages, file payloads, and commands are encrypted in transit using Fernet symmetric encryption.
   - The server generates the key and shares it during handshake, so the trust boundary is between the local network transport and the encrypted application payloads.

7. **Messaging protocol**
   - Communication uses TCP with a custom length-prefixed message format, which is important for diagramming because it separates framing from content.
   - The same channel appears to carry both chat text and control commands, with command handling based on recognized inputs like `/1` through `/9`.

8. **Group management subsystem**
   - The server maintains in-memory membership lists, pending join requests, and admin assignment for each group.
   - Admin-only operations include approving requests, transferring admin, and kicking members; regular members can inspect membership and send messages/files.

9. **File-sharing path**
   - File sharing is not a separate service; it is part of the same encrypted chat channel and server coordination logic.
   - The server briefly stores file data temporarily and then cleans it up automatically, so there is no durable file store.

10. **Concurrency model**
   - The server is multi-threaded, with each client connection handled in its own thread to allow concurrent group activity.
   - This means the main architectural boundaries are: client UI, encrypted TCP transport, per-connection server threads, and shared in-memory group state.

11. **Operational characteristics**
   - The system is optimized for small LAN deployments and cross-platform terminal use on Windows, macOS, and Linux.
   - There is no persistence across restarts: chat history, memberships, and group state are lost when the server stops.

Main entry points: [`Server.py`](https://github.com/m-ahmad3/pychat-lan-secure/blob/main/Server.py) (Server.py), [`Client.py`](https://github.com/m-ahmad3/pychat-lan-secure/blob/main/Client.py) (Client.py)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client runtime"]
  node_client_py["Client.py<br/>terminal client<br/>[Client.py]"]
  node_client_ui["Input loop<br/>CLI input"]
  node_client_recv["Receive thread<br/>async receive"]
  node_client_send["Send path<br/>message sender"]
end

subgraph group_server["Server runtime"]
  node_server_py["Server.py<br/>chat server<br/>[Server.py]"]
  node_conn_threads["Conn threads<br/>per-client threads"]
  node_session_mgr["Session manager<br/>online users"]
  node_group_state[("Group state<br/>in-memory groups")]
  node_admin_flow["Admin workflow<br/>approval and control"]
  node_msg_router["Message router<br/>chat relay"]
  node_file_path["File relay<br/>ephemeral transfer"]
end

subgraph group_protocol["Protocol layer"]
  node_handshake{{"Handshake<br/>key exchange"}}
  node_transport(("TCP channel<br/>socket transport"))
  node_framing["Length prefix<br/>message framing"]
  node_crypto{{"Fernet crypto<br/>symmetric encryption"}}
  node_commands["Slash commands<br/>control protocol"]
end

node_client_py -->|"starts"| node_client_ui
node_client_ui -->|"submits"| node_client_send
node_client_recv -->|"displays"| node_client_ui
node_client_py -->|"connects"| node_transport
node_client_send -->|"encodes"| node_commands
node_commands -->|"protects"| node_crypto
node_client_send -->|"encrypts"| node_crypto
node_crypto -->|"wraps"| node_framing
node_framing -->|"sends over"| node_transport
node_transport -->|"delivers"| node_server_py
node_server_py -->|"creates key"| node_handshake
node_handshake -->|"enables"| node_crypto
node_server_py -->|"spawns"| node_conn_threads
node_conn_threads -->|"updates"| node_session_mgr
node_session_mgr -->|"tracks"| node_group_state
node_group_state -->|"governs"| node_admin_flow
node_conn_threads -->|"routes"| node_msg_router
node_msg_router -->|"reads state"| node_group_state
node_msg_router -->|"broadcasts"| node_client_recv
node_msg_router -->|"handles"| node_file_path
node_file_path -->|"cleans up"| node_group_state

click node_client_py "https://github.com/m-ahmad3/pychat-lan-secure/blob/main/Client.py"
click node_server_py "https://github.com/m-ahmad3/pychat-lan-secure/blob/main/Server.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_py,node_client_ui,node_client_recv,node_client_send toneBlue
class node_server_py,node_conn_threads,node_session_mgr,node_group_state,node_admin_flow,node_msg_router,node_file_path toneAmber
class node_handshake,node_transport,node_framing,node_crypto,node_commands toneMint
```

## Components

### Client runtime

Terminal client that sends commands and receives group traffic.

- [Client.py](https://github.com/m-ahmad3/pychat-lan-secure/blob/main/Client.py): terminal client · Command-line client entrypoint. · `Client.py`
- **Input loop**: CLI input · Reads user commands, usernames, and chat text.
- **Receive thread**: async receive · Background path that prints incoming messages and updates.
- **Send path**: message sender · Serializes commands, messages, and file-related actions to the server.

### Server runtime

Multi-threaded chat server that owns group state and enforces coordination.

- [Server.py](https://github.com/m-ahmad3/pychat-lan-secure/blob/main/Server.py): chat server · Server entrypoint and central coordinator. · `Server.py`
- **Conn threads**: per-client threads · Handles each connected client concurrently.
- **Session manager**: online users · Tracks connected clients, usernames, and group membership.
- **Group state**: in-memory groups · Maintains groups, members, pending requests, and admin assignment.
- **Admin workflow**: approval and control · Approves joins, transfers admin, and kicks members.
- **Message router**: chat relay · Routes chat text, commands, and file traffic between group members.
- **File relay**: ephemeral transfer · Coordinates encrypted file sharing and temporary storage cleanup.

### Protocol layer

Encrypted TCP messaging and framing used by both processes.

- **Handshake**: key exchange · Creates and shares the symmetric key used for encrypted sessions.
- **TCP channel**: socket transport · Length-prefixed TCP connection between client and server.
- **Length prefix**: message framing · Separates message boundaries from payload content.
- **Fernet crypto**: symmetric encryption · Encrypts chat messages, commands, and file payloads in transit.
- **Slash commands**: control protocol · Recognized inputs such as join, approve, admin actions, and membership operations.

## Connections

- Client.py → Input loop: starts · Launches the terminal interaction loop.
- Input loop → Send path: submits · User input becomes outbound commands or messages.
- Receive thread → Input loop: displays · Incoming traffic is rendered back to the terminal.
- Client.py → TCP channel: connects · Opens a TCP session to the server.
- Send path → Slash commands: encodes · Formats chat actions as recognized command tokens.
- Slash commands → Fernet crypto: protects · Commands are carried inside encrypted payloads.
- Send path → Fernet crypto: encrypts · Outward payloads are encrypted before transmission.
- Fernet crypto → Length prefix: wraps · Encrypted payloads are framed for transport.
- Length prefix → TCP channel: sends over · Frames are transmitted through TCP.
- TCP channel → Server.py: delivers · Server receives client connections and payloads.
- Server.py → Handshake: creates key · Server generates the session key during connect.
- Handshake → Fernet crypto: enables · Shared key powers Fernet encryption on both sides.
- Server.py → Conn threads: spawns · Each client connection is handled in its own thread.
- Conn threads → Session manager: updates · Threads register active users and group membership.
- Session manager → Group state: tracks · Online users and group membership live in memory.
- Group state → Admin workflow: governs · Admin-only approvals and moderation are enforced here.
- Conn threads → Message router: routes · Threads forward chat and control traffic to the relay logic.
- Message router → Group state: reads state · Routing depends on group membership and pending joins.
- Message router → Receive thread: broadcasts · Relayed messages are sent to group participants.
- Message router → File relay: handles · File payloads follow the same encrypted chat channel.
- File relay → Group state: cleans up · Temporary file data is discarded after transfer.
