How m-ahmad3/pychat-lan-secure works
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 Server.py · Client.py Client.py
Components 16
Client runtime
Terminal client that sends commands and receives group traffic.
- Client.py · Command-line client entrypoint.
Client.py - Input loop · Reads user commands, usernames, and chat text.
- Receive thread · Background path that prints incoming messages and updates.
- Send path · 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 · Server entrypoint and central coordinator.
Server.py - Conn threads · Handles each connected client concurrently.
- Session manager · Tracks connected clients, usernames, and group membership.
- Group state · Maintains groups, members, pending requests, and admin assignment.
- Admin workflow · Approves joins, transfers admin, and kicks members.
- Message router · Routes chat text, commands, and file traffic between group members.
- File relay · Coordinates encrypted file sharing and temporary storage cleanup.
Protocol layer
Encrypted TCP messaging and framing used by both processes.
- Handshake · Creates and shares the symmetric key used for encrypted sessions.
- TCP channel · Length-prefixed TCP connection between client and server.
- Length prefix · Separates message boundaries from payload content.
- Fernet crypto · Encrypts chat messages, commands, and file payloads in transit.
- Slash commands · Recognized inputs such as join, approve, admin actions, and membership operations.
Connections 21
- 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.