A lightweight and secure VPN implementation using Python. This project leverages TUN/TAP devices and AES encryption for secure communication in a client-server architecture.
- π Encryption: Uses AES (CBC mode) for secure communication.
- π TUN/TAP Support: Utilizes Linux TUN devices for packet forwarding.
- π₯οΈ Client-Server Architecture: Dynamically assigns IPs to clients.
- β€οΈ Heartbeat Mechanism: Keeps connections alive and monitors client health.
- βοΈ Cross-Platform Compatibility: Designed to work on Linux environments with Python 3.8+.
VPN-Server-Client/
βββ client/
β βββ client.py # Main client logic
β βββ tun_handler.py # Manages TUN device for the client
β βββ encryption.py # AES encryption/decryption module
β βββ config.json # Client configuration
β βββ vpn-client-setup.ps1 # PowerShell script to set up the client on Windows
β βββ vpn-client-setup.sh # Shell script to set up the client on Linux
βββ server/
β βββ server.py # Main server logic
β βββ tun_handler.py # Manages TUN device for the server
β βββ encryption.py # AES encryption/decryption module
β βββ config.json # Server configuration
β βββ setup-vpn-firewall.ps1 # PowerShell script to set up firewall rules on Windows
β βββ setup-vpn-firewall.sh # Shell script to set up firewall rules on Linux
βββ requirements.txt # Python dependencies
βββ README.md # Project overview and instructions
βββ INSTALL.md # Installation guide
βββ CONTRIBUTING.md # Contribution guidelines
βββ LICENSE # MIT License
βββ .gitignore # Files and directories to exclude from Git
- π₯οΈ Operating System: Linux or Windows with TUN/TAP support.
- π Python: Version 3.8 or higher.
- βοΈ Required Tools:
iproute2for managing TUN/TAP devices on Linux.- Build tools (
build-essential,libssl-dev,python3-dev) for compiling dependencies on Linux. - OpenVPN TAP driver for Windows.
Install the system dependencies:
sudo apt update
sudo apt install -y iproute2 build-essential libssl-dev python3-dev-
Clone the repository:
git clone https://github.com/bali-36/VPN-Server-Client.git cd VPN-Server-Client -
Install Python dependencies:
pip install -r requirements.txt
Update the config.json files in the server/ and client/ directories with your settings:
Example config.json for Server:
{
"server_ip": "0.0.0.0",
"server_port": 1194,
"encryption_key": "your-secure-32-byte-key-here1234"
}Example config.json for Client:
{
"server_ip": "192.168.1.100",
"server_port": 1194,
"encryption_key": "your-secure-32-byte-key-here1234"
}Make sure the encryption_key matches on both server and client.
Run the server with elevated privileges (to configure the TUN device):
sudo python3 server/server.pyRun the server with administrator privileges:
python server/server.pyMonitor the logs to ensure the server is running and waiting for connections.
-
Run Shell Scripts:
- Open a terminal.
- Run the client setup script:
sudo ./client/vpn-client-setup.sh
- Run the firewall setup script:
sudo ./server/setup-vpn-firewall.sh
-
Start the Client:
- Run the client with elevated privileges:
sudo python3 client/client.py
- Run the client with elevated privileges:
-
Install OpenVPN TAP Driver:
- Download the OpenVPN TAP driver from OpenVPN's official website.
- Run the installer and follow the instructions to install the TAP driver.
-
Run PowerShell Scripts:
- Open PowerShell as Administrator.
- Run the client setup script:
.\client\vpn-client-setup.ps1
- Run the firewall setup script:
.\server\setup-vpn-firewall.ps1
-
Start the Client:
- Run the client with administrator privileges:
python client/client.py
- Run the client with administrator privileges:
The client will connect to the server, receive an IP address, and establish the VPN connection.
-
Check the TUN device configuration:
ip addr show tun0 # Server-side on Linux ip addr show tun1 # Client-side on Linux
On Windows, use:
ipconfig /all
-
Use tools like
pingortcpdumpto verify traffic flow.
The project automatically configures IP routes after connecting:
- Server: Runs the
setup-vpn-firewall.shscript withsudoto configure routing. - Client: Runs the
vpn-client-setup.shwithoutsudofor client-specific routes.
Ensure the script is executable and located in the scripts/ directory.
-
π Encryption:
- All traffic is encrypted using AES with a shared secret key.
-
π TUN/TAP Devices:
- The server and client configure TUN devices for packet forwarding.
- Packets are sent over the encrypted connection.
-
β€οΈ Heartbeat Mechanism:
- The client periodically sends heartbeat messages to ensure the connection is alive.
- The server monitors heartbeats and disconnects inactive clients.
-
β TUN Device Not Found:
- Ensure
/dev/net/tunexists and has proper permissions on Linux:sudo chmod 666 /dev/net/tun
- Ensure the TAP driver is installed on Windows.
- Ensure
-
β Connection Timeout:
- Verify the
server_ipandserver_portin the client configuration.
- Verify the
-
β Decryption Errors:
- Ensure the
encryption_keyis identical on both server and client.
- Ensure the
- πͺ΅ Check logs on both server and client for detailed information.
- π§ Increase logging verbosity in
logging.basicConfig()if needed.
Contributions are welcome! See the CONTRIBUTING.md file for details on how to get involved.
This project is licensed under the MIT License. See the LICENSE file for more information.
- Developed using the Python programming language.
- Inspired by Linux TUN/TAP devices and secure networking principles.