- 将一台拥有公网ip的服务器和一台能访问公网的服务器通过tunnel连接到一个虚拟局域网中
- 直接make生成两个可执行文件 server 和 client
修改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...
在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
在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 | |
| +------------------+ | | +------------------+ |
+-------------------------------------+ +--------------------------------------------------+