Skip to content

Commit 806d62e

Browse files
committed
Improve server README
1 parent fcd540d commit 806d62e

File tree

1 file changed

+60
-52
lines changed

1 file changed

+60
-52
lines changed

server/README.md

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,56 @@
11
# ToyVPN Server 🌐
22

33
## Overview 🛠️
4-
This is the VPN server implemented in C++. It utilizes various networking libraries to handle packet forwarding, routing, and tunneling. The server is designed to manage VPN connections, forward traffic, and provide essential network functionalities.
4+
ToyVPN is a lightweight VPN server implemented in C++. It leverages **PcapPlusPlus** and various networking tools to handle packet forwarding, routing, and tunneling. The server is designed to efficiently manage VPN connections, forward traffic, and provide essential network functionalities.
55

6-
It is heavily inspired by Android ToyVpn and provides a robust solution for handling VPN connections.
6+
Inspired by [Android ToyVpn](https://android.googlesource.com/platform/development/+/master/samples/ToyVpn), this project offers a robust solution for handling VPN connections.
77

88
## Features 🚀
9-
- Supports multiple clients
10-
- Single-threaded, utilizing epoll for efficient event handling
11-
- Automatic configuration: creates tun interface, configures iptables and IP routing, and handles automatic teardown
12-
- Records pcapng files with traffic per VPN client
9+
- **Multi-client support**: Handles multiple VPN clients simultaneously.
10+
- **Efficient event handling**: Uses **epoll** for scalable performance.
11+
- **Automatic setup & teardown**:
12+
- Creates a TUN interface dynamically.
13+
- Configures **iptables** and **IP routing**.
14+
- Cleans up configurations upon shutdown.
15+
- **Traffic logging**: Saves network traffic per client as **pcapng** files.
1316

1417
## Dependencies 🔗
1518
This project relies on the following libraries:
16-
- [PcapPlusPlus](https://github.com/seladb/PcapPlusPlus) for packet parsing and saving packets to pcapng files
17-
- [concurrentqueue](https://github.com/cameron314/concurrentqueue) for passing packets to save without interfering with the main processing thread
18-
- [argparse](https://github.com/p-ranav/argparse) for parsing CLI arguments
19-
- [AixLog](https://github.com/berkus/AixLog) for logging
19+
- **[PcapPlusPlus](https://github.com/seladb/PcapPlusPlus)** - Packet parsing & pcapng file generation.
20+
- **[concurrentqueue](https://github.com/cameron314/concurrentqueue)** - Non-blocking queue for efficient packet handling.
21+
- **[argparse](https://github.com/p-ranav/argparse)** - CLI argument parsing.
22+
- **[AixLog](https://github.com/berkus/AixLog)** - Logging framework.
2023

21-
## Building the Project 🛠
24+
## Building the Project 🏗
2225
### Prerequisites ✅
2326
Ensure you have the following installed:
24-
- A C++ compiler supporting C++17
25-
- CMake (version 3.20 or higher)
26-
- `libpcap-dev` (required for `pcapplusplus`)
27+
- A **C++17** compatible compiler.
28+
- **CMake** (version **3.20+**).
29+
- **libpcap-dev** (required for `pcapplusplus`).
2730

2831
### Installing PcapPlusPlus 📦
29-
- Go to the latest release page: https://github.com/seladb/PcapPlusPlus/releases
30-
- Download the relevant binary for your operating system
31-
- Extract the archive file under `libs/`
32-
- Rename the folder to `pcapplusplus`
32+
1. Download the latest release from: [PcapPlusPlus Releases](https://github.com/seladb/PcapPlusPlus/releases).
33+
2. Extract the archive into the `libs/` directory.
34+
3. Rename the extracted folder to `pcapplusplus`.
3335

34-
### Build Instructions 🏗
36+
### Build Instructions
3537
```sh
3638
mkdir build && cd build
3739
cmake ..
3840
make
3941
```
4042

4143
## Running the Server 🚀
44+
### Basic Usage
45+
The following command starts the VPN server:
4246
```sh
4347
sudo ./ToyVpnServer --port 5678 --public-network-iface eth0 --secret my_secret
4448
```
49+
- **Port:** `5678` (for incoming VPN connections).
50+
- **Public Network Interface:** `eth0` (provides Internet access).
51+
- **Secret Key:** `my_secret` (used for authentication).
4552

46-
## How to Use ⚙️
47-
The server supports various CLI flags for configuration:
48-
53+
### CLI Options ⚙️
4954
```sh
5055
Usage: ToyVpnServer [--help] [--version] [-t, --tun VAR] --port VAR [--private-network VAR] --public-network-iface VAR --secret VAR [--route VAR] [--mtu VAR] [--dns-server VAR] [--save-to-files VAR] [--verbose]
5156

@@ -66,40 +71,43 @@ Optional arguments:
6671

6772
## Architecture 🏛️
6873
### Platform Support 🐧
69-
This server is designed to work exclusively on Linux.
74+
This server is **Linux-only** due to its reliance on platform-specific networking tools.
7075

7176
### Main Components 🔩
72-
- `EpollWrapper.h`: Manages event-driven networking
73-
- `ServerSocketWrapper.h`: Manages the UDP socket that listens to incoming clients
74-
- `ClientHandler.h`: Handles the lifecycle of individual VPN clients - connecting to a new client, performing the handshake protocol, managing traffic from the client to the external network and vice versa, handling client disconnection
75-
- `TunInterfaceWrapper.h`: Creates and manages the TUN interface used for the VPN connection
76-
- `NatAndRoutingWrapper.h`: Configures IP forwarding and routing. Uses `iptables` and `ip route`
77-
- `PacketHandler.h`: Run in a separate thread, accepts packets from the main thread and saves them to pcapng files per VPN client
78-
- `ToyVpnServer.h`: Orchestrates all of the above and manages the server lifecycle
79-
80-
### Flow 🔄
81-
1. The server initializes and sets up a TUN interface.
82-
2. It configures IP forwarding and routing.
83-
3. Opens a UDP socket and starts listening to incoming clients.
84-
4. Clients connect, go through the handshake protocol and establish a VPN session.
85-
5. Packets are forwarded between clients and the external network (Internet).
86-
6. If requested by the user, packets are also passed to a different thread for saving them to pcapng files.
87-
7. When a client disconnects, the server cleans up the resources.
88-
8. When the server shuts down it removes the TUN interface and restores the IP forwarding and routing configurations.
77+
- **`EpollWrapper.h`** - Manages event-driven networking.
78+
- **`ServerSocketWrapper.h`** - Handles the UDP socket for client connections.
79+
- **`ClientHandler.h`** - Manages VPN client sessions, including:
80+
- Connection establishment.
81+
- Handshake protocol.
82+
- Traffic forwarding.
83+
- Disconnection handling.
84+
- **`TunInterfaceWrapper.h`** - Manages the TUN interface for VPN traffic.
85+
- **`NatAndRoutingWrapper.h`** - Configures NAT and routing using `iptables`.
86+
- **`PacketHandler.h`** - Runs in a separate thread to log VPN traffic.
87+
- **`ToyVpnServer.h`** - Orchestrates all components and manages the server lifecycle.
88+
89+
### Server Flow 🔄
90+
1. Initializes and configures a **TUN interface**.
91+
2. Sets up **IP forwarding and routing**.
92+
3. Opens a **UDP socket** to listen for clients.
93+
4. Handles client **authentication** and **session establishment**.
94+
5. Forwards packets between clients and the external network.
95+
6. Saves network traffic logs (if enabled).
96+
7. Cleans up resources upon client disconnection or server shutdown.
8997

9098
### Handshake Protocol 🤝
91-
The handshake process establishes a connection between the client and the server, exchanging configuration details and authentication data:
92-
1. The client connects to the UDP socket the server listens on
93-
2. It sends the secret to the server and the server verifies the secret
94-
3. The server allocates a new internal IP address for the client and send configuration parameters to the client that include:
95-
- Client internal IP address
96-
- The forwarding route (`0.0.0.0/0` by default)
97-
- MTU
98-
- DNS server to use if configured by the server
99-
4. The client sends control packets once in a while to indicate it's still connected
100-
5. When a client wants to disconnect, it sends a special control message
101-
6. If the server shuts down, it sends a special disconnect control message to all connected clients
99+
1. The client connects to the UDP port.
100+
2. It sends the **secret key** for authentication.
101+
3. If valid, the server:
102+
- Assigns an **internal IP** to the client.
103+
- Sends the **routing parameters**.
104+
- Provides **DNS settings** (if configured).
105+
- Sets **MTU**.
106+
4. The client sends **keep-alive** packets periodically.
107+
5. Disconnection occurs via:
108+
- Explicit **client request**.
109+
- **Server shutdown** (broadcasts a disconnect message).
102110

103111
## License 📜
104-
This project is licensed under the MIT License.
112+
This project is licensed under the **MIT License**.
105113

0 commit comments

Comments
 (0)