This project is a messaging application that provides P2P (Peer-to-Peer) communication. It allows users to communicate directly with each other and securely transmit their messages by encrypting them. The application enables users to exchange keys among themselves to encrypt their messages.
- User Registration: When the application first opens, it asks the user to enter a username. This username is used to identify the user to others.
- User Broadcast: The username and IP address are broadcasted at regular intervals so that other users can discover this person.
- User List: Discovered users are displayed in a list and can be selected.
- Sending Messages: Messages can be sent to the selected user. Messages are transmitted in encrypted form.
- Chat Log: Sent and received messages are stored locally and can be viewed when desired.
- Download or copy the project files.
- Install the necessary Python libraries:
pip install cryptography requests asyncio
- Ensure your Python environment includes the following standard libraries (usually included by default):
- warnings
- json
- socket
- threading
- time
- random
- tkinter
- http.server
- os
- Run the application from the terminal with the following command:
python gui.py
- Enter a username in the opened window and click the "Save" button.
- The application only works for users on the same local network.
- The chat log is saved locally, so it cannot be accessed from other devices.
- While the application is running, it continuously searches for and discovers users. Therefore, network traffic may increase slightly.
- If a user goes offline, they are marked as "Away" in the list.
- Ensure the recipient is online before sending a message.
- Users need to have two computers running the application on the same local network to be able to communicate with each other.
- Due to its low latency and connectionless nature, UDP is ideal for operations requiring fast and frequent messaging, such as peer discovery.
- When used for registration announcements, UDP can broadcast messages widely across the network without guaranteeing their delivery.
- TCP is used for critical communications like sending and receiving messages because it provides reliable data transmission and data integrity.
- TCP ensures that data packets are delivered in order and without loss, guaranteeing that messages are received correctly.
In this application, the UDP protocol is used for peer discovery and registration announcements, while the TCP protocol is used for sending and receiving messages. This approach leverages the advantages of both protocols: the fast and low-latency nature of UDP, and the reliable and orderly data transmission of TCP, optimizing the application's performance and security.
- Purpose: Used for secure key exchange between peers.
- How It Works: When a connection is established, Diffie-Hellman is used to create a shared secret key. This key is used for encrypting communications between the peers.
- Purpose: Used for encrypting and decrypting messages.
- How It Works: Messages are encrypted using the shared secret key generated by Diffie-Hellman. Fernet provides symmetric encryption, ensuring that the message can only be decrypted by the intended recipient with the correct key.
- IP Address:
255.255.255.255
- Port:
6004
- Purpose: Used to discover other peers on the network. Each user broadcasts their presence using this IP and port. At the same time, they listen for broadcasts from other users to discover them.
- IP Address:
0.0.0.0
(Listens on all network interfaces) - Port: Defined by the user when starting the application.
- Purpose: Accepts incoming TCP connections and facilitates peer-to-peer communication.
- IP Address:
0.0.0.0
- Port: One more than the application's defined port (e.g., if the main server runs on port
x
, the HTTP server runs on portx+1
). - Purpose: Used to view chat logs via HTTP. This server serves log files over HTTP.
- IP Address: Dynamically assigned (IP addresses of both peers)
- Port: Ports defined by both peers when establishing a TCP connection, usually
self.port
orpeer_port
. - Purpose: Used to communicate directly with the selected peer. Ensures the transmission of encrypted messages.
- The user enters a username when the application starts and registers with the
register_user
method. - The
announce_registration
method broadcasts the user's IP address, port number, and username via UDP.
- The
peer_discovery
method listens for UDP broadcasts on port6004
. - It receives messages broadcasted by other users and adds new peers to the list.
- The
accept_connections
method accepts incoming TCP connections on the specified port. - The
handle_client
method processes incoming connections and decrypts encrypted messages.
- The
send_message
method sends messages to the selected peer via TCP. - Messages are encrypted before being sent to the peer.
- The
start_http_server
method starts an HTTP server on the port number one more than the defined port. - Used to view chat logs over HTTP.