Secure WebSocket Messaging Application using Swoole PHP server and a simple HTML client. This application allows users to set their usernames, exchange public keys, and send encrypted messages securely.
- PHP 7.4 or higher
- Swoole PHP extension installed (
pecl install swoole) - OpenSSL for SSL/TLS support (optional)
- Web browser for the client interface
-
Clone the repository:
git clone https://github.com/ibizara/EasySecureMessaging.git cd EasySecureMessaging -
Install Swoole PHP extension:
pecl install swoole
-
Configure SSL Certificates (optional):
- Place your SSL certificates (
fullchain.pemandprivkey.pem) in the appropriate directory (/var/www/certs/by default).
- Place your SSL certificates (
-
Start the WebSocket Server:
php server.php
-
Open the Client Interface:
- Open
client.htmlin your web browser.
- Open
The server configuration is specified in server.php. The configurable options are:
// CONFIG START
$host_address = "0.0.0.0";
$host_port = 2644;
$protocol = 'wss'; // Change to 'ws' for non-SSL connection
$ssl_cert_file = '/var/www/certs/fullchain.pem';
$ssl_key_file = '/var/www/certs/privkey.pem';
$max_encrypted_length = 10000;
$debug = false;
// CONFIG END$host_address: The address the server will bind to (default:0.0.0.0).$host_port: The port the server will listen on (default:2644).$protocol: Protocol to use (wssfor SSL,wsfor non-SSL).$ssl_cert_file: Path to the SSL certificate file.$ssl_key_file: Path to the SSL key file.$max_encrypted_length: Maximum length for encrypted messages (default:10000).$debug: Enable or disable debug logging (default:false).
The client configuration is specified within the client.html file. The configurable options are:
// CONFIG START
const wsProtocol = 'wss'; // Change to 'ws' for non-SSL connection
const wsHost = 'your-server-address';
const wsPort = 2644;
const wsPath = 'chat';
const debug = false;
// CONFIG ENDwsProtocol: WebSocket protocol to use (wssfor SSL,wsfor non-SSL).wsHost: Host address of the WebSocket server.wsPort: Port of the WebSocket server (default:2644).wsPath: Path for the WebSocket endpoint (default:chat).debug: Enable or disable debug logging (default:false).
-
Edit the Server Configuration:
- Open
server.phpin a text editor. - Modify the configuration values under
// CONFIG STARTto suit your requirements. - Save the changes and restart the server.
- Open
-
Edit the Client Configuration:
- Open
client.htmlin a text editor. - Modify the configuration values under
// CONFIG STARTto match your server setup. - Save the changes and refresh the client interface in your web browser.
- Open
-
Set Username:
- Enter your username in the input field and click "Set Username".
-
Send Messages:
- Select a user from the user list, type your message, and click "Send".
-
Clear Chat:
- Click "Clear Chat" to clear the message area.
-
Remote Clear Chat:
- Click "Remote Clear" to clear the chat for both you and the selected user.
To run the WebSocket server as a systemd service, follow these steps:
-
Create a systemd service file:
sudo nano /etc/systemd/system/secure-websocket.service
-
Add the following content to the service file:
[Unit] Description=Secure WebSocket Messaging Server After=network.target [Service] Type=simple User=www-data Group=www-data WorkingDirectory=/path/to/EasySecureMessaging ExecStart=/usr/bin/php /path/to/EasySecureMessaging/server.php Restart=on-failure [Install] WantedBy=multi-user.target
Ensure the
UserandGrouphave permission to access the SSL certificates. -
Reload systemd and start the service:
sudo systemctl daemon-reload sudo systemctl start secure-websocket.service sudo systemctl enable secure-websocket.service
This project is licensed under the MIT License. See the LICENSE file for details.