Skip to content

shivesh1606/Vpn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ› οΈ Simple C++ VPN (TUN-based Virtual Private Network)

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.


πŸš€ Features

  • 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

πŸ“‚ Project Structure

cppVpn/ │── server.cpp # VPN Server │── client.cpp # VPN Client │── encrypt.h # AES encryption helpers │── utils.h # TUN + networking helpers │── README.md # Project documentation


βš™οΈ How It Works (High Level)

  1. Server creates TUN interface tun0
  2. Client creates TUN interface tun1
  3. Both interfaces behave like virtual network cards.
  4. Packets written into tun0 are encrypted & sent to client.
  5. Client decrypts them and injects into tun1, and vice-versa.
  6. You can now:
    • ping 10.0.0.1 from the client
    • ping 10.0.0.2 from the server
      And traffic flows through your VPN tunnel.

🧰 Prerequisites

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages