Skip to content

freelw/vpndemo

Repository files navigation

vpn demo

功能

  • 将一台拥有公网ip的服务器和一台能访问公网的服务器通过tunnel连接到一个虚拟局域网中

编译

  • 直接make生成两个可执行文件 server 和 client

配置ip

修改vpnrc文件中的环境变量

export VPN_CLIENT_IP=172.20.10.151
export VPN_SERVER_IP=172.20.10.150
export SERVER_REAL_IP=211.159.147.17
export VPN_MASK=24

含义:

  • VPN_CLIENT_IP 客户端局域网ip
  • VPN_SERVER_IP 服务端局域网ip
  • VPN_MASK 子网掩码
  • SERVER_REAL_IP 服务端的公网ip

运行方法

先启动服务端

[root@VM-0-11-centos vpndemo]# ./start_server.sh
sleeping 5s...

再启动客户端

[root@VM-0-15-centos vpndemo]# ./start_client.sh
sleeping 5s...

ping测试

在172.20.10.151上

[root@VM-0-15-centos vpndemo]# ping 172.20.10.150
PING 172.20.10.150 (172.20.10.150) 56(84) bytes of data.
64 bytes from 172.20.10.150: icmp_seq=1 ttl=64 time=2.52 ms
64 bytes from 172.20.10.150: icmp_seq=2 ttl=64 time=2.57 ms
64 bytes from 172.20.10.150: icmp_seq=3 ttl=64 time=2.54 ms
64 bytes from 172.20.10.150: icmp_seq=4 ttl=64 time=2.55 ms

tcp测试

在172.20.10.151上

[root@VM-0-15-centos vpndemo]# nc -lk 9001
hello
hi vpn demo!

在172.20.10.150上

[root@VM-0-11-centos vpndemo]# nc 172.20.10.151 9001
hello
hi vpn demo!

架构图

+-------------------------------------+                      +--------------------------------------------------+
|                                     |                      |                                                  |
|                                     |                      |                                                  |
|  +--------+                         |                      |                        +---------------------+   |
|  |        |	 port:1234            |       socket         |                        |                     |   |
|  | server |  <------------------------------------------------------------------->  |      client         |   |
|  |        |                         |                      |                        |                     |   |
|  | listen |  <---------+            |                      |           +--------->  |      connect        |   |
|  |        |            |            |                      |           |            |                     |   |
|  |  1234  |   +------------------+  |                      |  +------------------+  | 211.159.147.17:1234 |   |
|  |        |   |       tun0       |  |                      |  |       tun0       |  |                     |   |
|  +--------+   | 172.20.10.150/24 |  |                      |  | 172.20.10.151/24 |  +---------------------+   |
|               +------------------+  |                      |  +------------------+                            |
|                        ^            |                      |           ^                                      |
|                        |            |                      |           |                                      |
|                        |            |                      |           |                                      |
|               +------------------+  |                      |   +------------------+                           |
|               |    nc conncet    |  |                      |   |    nc listen     |                           |
|               |172.20.10.151 9001|  |                      |   |      9001        |                           |
|               +------------------+  |                      |   +------------------+                           |
+-------------------------------------+                      +--------------------------------------------------+

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published