A secure, feature-rich real-time chat application built with Python
Developed under the PacketVSocket SMP of Cipher SIG, IET NITK
- Overview
- Features
- Architecture
- Prerequisites
- Installation
- Configuration
- Usage
- Commands
- Security
- Database Schema
- Contributing
- License
TTYL is a lightweight, secure client-server chat application that enables real-time messaging between multiple users over a network. Built with Python's socket programming and enhanced with encryption capabilities, it provides both public chat rooms and private messaging functionality with comprehensive admin controls.
- End-to-end encryption for private messages using Fernet (AES 128)
- Public message encryption with shared keys
- SSL/TLS database connections for secure logging
- Public chat room for all connected users
- Private messaging between individual users
- Real-time message delivery with TCP reliability
- Username change functionality during chat sessions
- Automatic username conflict resolution with suffix generation
- Online user list retrieval
- User reporting system for moderation
- Graceful connection handling and cleanup
- Multi-admin support with promotion/demotion
- User kicking with temporary removal
- User banning with permanent restrictions
- Admin-only command restrictions
- Comprehensive audit logging
- MySQL database integration for persistent logging
- Event tracking (connections, admin actions, reports)
- Local file backup logging system
- Timestamp tracking for all activities
โโโโโโโโโโโโโโโโโโโ TCP/IP Socket โโโโโโโโโโโโโโโโโโโ
โ โโโโโโโโโโโโโโโโโโโโโบโ โ
โ Client (N) โ Encrypted Msgs โ Server โ
โ - client.py โ โ - server.py โ
โ - Commands โ โ - Threading โ
โ - Encryption โ โ - User Mgmt โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ
โ
โโโโโโโโโโโโโโโโโโโ
โ MySQL Database โ
โ - Chat Logs โ
โ - Audit Trail โ
โโโโโโโโโโโโโโโโโโโ
- Python 3.7+
- MySQL Server (or Aiven MySQL service)
- Required Python packages:
pip install cryptography mysql-connector-python
-
Clone the repository:
git clone <repository-url> cd ttyl-chat
-
Install dependencies:
pip install -r requirements.txt
-
Set up the database:
CREATE DATABASE ttyl; USE ttyl; CREATE TABLE chat_logs ( id INT AUTO_INCREMENT PRIMARY KEY, timestamp DATETIME NOT NULL, event_type VARCHAR(50) NOT NULL, username VARCHAR(100) NOT NULL, target_user VARCHAR(100), ip_address VARCHAR(45), details TEXT, is_admin_action BOOLEAN DEFAULT FALSE );
-
Configure database connection (see Configuration section)
Edit the database connection parameters in server.py:
def connect_db(self):
return mysql.connector.connect(
host="your-mysql-host",
port=3306, # or your custom port
user="your-username",
password="your-password",
database="ttyl",
ssl_ca="/path/to/ca.pem", # if using SSL
ssl_verify_cert=True
)Modify connection settings in both client.py and server.py:
HOST = '127.0.0.1' # Change to your server IP for LAN/WAN access
PORT = 7423 # Custom port (ensure it's open in firewall)# In client.py - Generate new keys using:
# from cryptography.fernet import Fernet
# key = Fernet.generate_key()
public_key = b'your-new-public-key-here'
key = b'your-new-private-key-here'python server.pyThe server will start listening on the configured HOST and PORT.
python client.pyEnter a username when prompted. If the username exists, a unique suffix will be automatically added.
- Connect to the server
- Enter username (automatic conflict resolution)
- Start chatting in the public room
- Use commands for additional features (see Commands section)
| Command | Syntax | Description |
|---|---|---|
/users |
/users |
List all online users |
/private |
/private username message |
Send encrypted private message |
/change_name |
/change_name new_username |
Change your username |
/report |
/report username |
Report a user to admins |
/exit |
/exit |
Leave the chat safely |
| Command | Syntax | Description |
|---|---|---|
/kick |
/kick username |
Temporarily remove user |
/ban |
/ban username |
Permanently ban user |
/unban |
/unban username |
Remove ban from user |
/admin |
/admin username |
Promote user to admin |
/demote |
/demote |
Demote yourself from admin |
# Public message
Hello everyone!
# Private message
/private john Hey, how are you?
# Administrative action
/kick spammer
# User management
/admin trusted_user- Private Messages: AES-128 encryption via Fernet
- Public Messages: Shared key encryption for basic obfuscation
- Database: SSL/TLS encrypted connections
- Change default encryption keys before deployment
- Use strong passwords for database connections
- Enable SSL certificates for production databases
- Regularly rotate encryption keys
- Monitor admin actions through database logs
- Shared public key visible in source code (obfuscation only)
- No user authentication system (username-based only)
- Admin privileges not persistent across restarts
CREATE TABLE chat_logs (
id INT AUTO_INCREMENT PRIMARY KEY,
timestamp DATETIME NOT NULL,
event_type VARCHAR(50) NOT NULL,
username VARCHAR(100) NOT NULL,
target_user VARCHAR(100),
ip_address VARCHAR(45),
details TEXT,
is_admin_action BOOLEAN DEFAULT FALSE
);CONNECT- User connectionsADMIN_ACTION- Administrative commandsDISCONNECT- User disconnectionsREPORT- User reports
ttyl-chat/
โโโ client.py # Client application
โโโ server.py # Server application
โโโ config.py # Configuration file
โโโ README.md # This file
โโโ requirements.txt # Python dependencies
โโโ chat_server.log # Local log file (generated)
Connection Refused:
- Check if server is running
- Verify HOST and PORT settings
- Check firewall settings
Database Connection Error:
- Verify database credentials
- Check SSL certificate path
- Ensure database exists
Encryption Errors:
- Verify matching keys between client/server
- Check for corrupted key formats
Username Conflicts:
- Automatic suffix resolution should handle this
- Check for special characters in usernames
We welcome contributions! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow PEP 8 style guidelines
- Add comments for complex logic
- Test with multiple clients
- Update documentation for new features
- User authentication system
- File transfer capabilities
- Chat history persistence
- Web-based client interface
- Voice message support
- Group chat rooms
- Message reactions and emojis
- Rate limiting and spam protection
This project is developed under the PacketVSocket SMP of Cipher SIG, IET NITK.
PacketVSocket SMP Team
Cipher SIG, IET NITK
For issues, questions, or contributions, please:
- Open an issue on GitHub
- Contact the development team
- Check the troubleshooting section above
Happy Chatting! ๐
TTYL - Where conversations come alive!