A lightweight, beginner-friendly VPN implementation written in C++ using:
- Linux TUN interfaces
- TCP sockets for communication
- OpenSSL AES-256-CBC for encryption
This project helps you understand the fundamentals behind VPNs β without diving into kernel modules or heavy protocols like OpenVPN/WireGuard.
- Create virtual network interfaces (
tun0,tun1) - Encrypted data transfer between client and server
- Tunnel ICMP/IPv4 packets (ping)
- Minimal, easy-to-read C++ code (<300 LOC)
- Works locally or across machines
cppVpn/ βββ server.cpp # VPN Server βββ client.cpp # VPN Client βββ encrypt.h # AES encryption helpers βββ utils.h # TUN + networking helpers βββ README.md # Project documentation
- Server creates TUN interface
tun0 - Client creates TUN interface
tun1 - Both interfaces behave like virtual network cards.
- Packets written into
tun0are encrypted & sent to client. - Client decrypts them and injects into
tun1, and vice-versa. - You can now:
ping 10.0.0.1from the clientping 10.0.0.2from the server
And traffic flows through your VPN tunnel.
Install dependencies:
sudo apt update
sudo apt install build-essential openssl libssl-dev
π¨ Build
Run this in the project directory:
Build server:
g++ server.cpp -o server -lcrypto
Build client:
g++ client.cpp -o client -lcrypto
π Run the VPN
π₯οΈ Terminal 1 β Start the server
sudo ./server secretpass tun0
Output:
TUN fd: 3 device tun0
Listening on port 5555
π₯οΈ Terminal 2 β Start the client
sudo ./client 127.0.0.1 secretpass tun1
Output:
TUN fd: 3 device tun1
Connected to server
π‘ Assign IPs to TUN Interfaces
On server:
sudo ip addr add 10.0.0.1/24 dev tun0
sudo ip link set tun0 up
On client:
sudo ip addr add 10.0.0.2/24 dev tun1
sudo ip link set tun1 up
π§ͺ Test the VPN Tunnel
Ping server from client:
ping 10.0.0.1
Ping client from server:
ping 10.0.0.2
If packets flow successfully β your VPN is working π
MIT License
Copyright (c) 2025 Shivesh
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.