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_project/
├── 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
├── 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
├── 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 with TUN/TAP support.
- 🐍 Python: Version 3.8 or higher.
- ⚙️ Required Tools:
iproute2
for managing TUN/TAP devices.- Build tools (
build-essential
,libssl-dev
,python3-dev
) for compiling dependencies.
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/yourusername/vpn_project.git cd vpn_project
-
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-here"
}
Example config.json
for Client:
{
"server_ip": "192.168.1.100",
"server_port": 1194,
"encryption_key": "your-secure-32-byte-key-here"
}
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.py
Monitor the logs to ensure the server is running and waiting for connections.
Run the client with elevated privileges (to configure the TUN device):
sudo python3 client/client.py
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 ip addr show tun1 # Client-side
- Use tools like
ping
ortcpdump
to verify traffic flow.
The project automatically configures IP routes after connecting:
- Server: Runs the
setup-vpn-firewall.sh
script withsudo
to configure routing. - Client: Runs the
vpn-client-setup.sh
withoutsudo
for 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/tun
exists and has proper permissions:sudo chmod 666 /dev/net/tun
- Ensure
-
❌ Connection Timeout:
- Verify the
server_ip
andserver_port
in the client configuration.
- Verify the
-
❌ Decryption Errors:
- Ensure the
encryption_key
is 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.