一、open***与pptp的抉择

PPTP

    点对点隧道协议(pptp)是一种实现虚拟专用网络的方法。pptp使用用于封装ppp数据包的tcp及GRE隧道控制通道。

Open×××

    Open×××是一免费开源软件,以路由器或桥接配置和远程访问设备方式实现虚拟专用网络(***)创建安全的点 对点或站对站连接的解决方案。它使用SSL / TLS安全加密,具有穿越网络地址转换(NATs)和防火墙的功能。

PPTP与Open×××之比较和抉择?

    在pptp和open***二者之间做出选择的一个重要考虑因素,也是我们无法控制的因素,就是有时互联网服务供应商会阻止 PPTP连接。这种情况下我们无计可施,只能选择使用open***。 pptp具有一些独 特优势,但此刻用open***会是不错的选择。

    PPTP的劣势在于他对防火墙的穿透力不是太强,有些网络接入商会屏蔽其1723端口,因此客户端会出现诸如619等错误的信息报告;同时由于其使用密码作为密钥,它的数据流载有可获取的混编密码,如果中间人拦截到了数据流破译了密码,那么他就可以破译你的信息了,当然,这是一项很难的技术。 pptp的劣势就是Open××× 的优势之处,open***以路由器或桥接配置和远程访问设备方式实现虚拟网专用连接,通过nat转换的方式实现通信,具有穿越防火墙的功能

二、部署open***

   如果你部署open***,是为了翻越GFW,那你就把此***部署在一台国外vps上,让所有的客户端通过代理***服务器来共享上网;所说,如果服务器在国内的话,貌似添加静态路由也可以。在部署翻越GFW的open***时,建议将代理什么的都关掉,我就是因为开了Proxy SwitchySharp,导致在访问国外网站时丢包特别严重,严重到根本打不开网

    本次部署open***服务器,因为使用了最新的open***2.3.4,而这个包里面没有包含最重要的证书制作部分:easy-rsa

    open***官网也给出明确说明:Starting with open***-2.3_alpha2 easy-rsa is no longer part of the Open××× source or binary packages

    所以,我们需要事先下载好easyrsa,可以到GitHub上进行下载,配置过程将在下面第3步进行,本次部署使用了easy-rsa3,与easy-rsa2.0的操作完全不同,网上其它关于easy-rsa2.0的教程不适合本次部署

    在部署open***之前,最好用ntpdate同步一下服务器的时间,否则生成证书的时间也不准确,会造成那个什么centificate error等的错误!

1、安装lzo(不安装也可以)

    lzo是致力于解压速度的一种数据压缩算法

[root@*** ~]# tar xf lzo-2.08.tar.gz
[root@*** ~]# cd lzo-2.08
[root@*** lzo-2.08]# ./configure && make && make install

2、安装open***

[root@*** ~]# tar xf open***-2.3.4.tar.gz
[root@*** ~]# cd open***-2.3.4
[root@*** open***-2.3.4]# ./configure --with-lzo-headers=/usr/local/include/ --with-lzo-lib=/usr/local/lib
[root@*** open***-2.3.4]# make && make install
[root@*** open***-2.3.4]# 
[root@*** open***-2.3.4]# which open***
/usr/local/sbin/open***      #看到这里,说明安装open***成功

3、配置easyrsa服务端

    open***-2.3.4软件包不包含证书(ca证书,服务端证书,客户端证书)制作工具,所以还需要单独下载easy-rsa,最新的为easy-rsa3

    Starting with open***-2.3_alpha2 easy-rsa is no longer part of the Open××× source or binary packages(来源open***官网)

[root@*** ~]# unzip easy-rsa-master.zip 
[root@*** ~]# mv easy-rsa-master easy-rsa
[root@*** ~]# cp -R easy-rsa/ open***-2.3.4/
[root@*** ~]# cd open***-2.3.4/easy-rsa/easyrsa3/
[root@*** easyrsa3]# cp vars.example vars
[root@*** easyrsa3]# vim vars
set_var EASYRSA_REQ_COUNTRY "CN"
set_var EASYRSA_REQ_PROVINCE "Beijing"
set_var EASYRSA_REQ_CITY "Beijing"
set_var EASYRSA_REQ_ORG "nmshuishui Certificate"
set_var EASYRSA_REQ_EMAIL "[email protected]"
set_var EASYRSA_REQ_OU "My Open×××"

4、创建服务端证书及key

(1)初始化

[root@*** easyrsa3]# ls
easyrsa  openssl-1.0.cnf  vars  vars.example  x509-types
[root@*** easyrsa3]# 
[root@*** easyrsa3]# ./easyrsa init-pki

Note: using Easy-RSA configuration from: ./vars

init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /root/open***-2.3.4/easy-rsa/easyrsa3/pki

(2)创建根证书

[root@*** easyrsa3]# ./easyrsa build-ca

Note: using Easy-RSA configuration from: ./vars
Generating a 2048 bit RSA private key
.............................................+++
........+++
writing new private key to '/root/open***-2.3.4/easy-rsa/easyrsa3/pki/private/ca.key'
Enter PEM pass phrase:                      #输入密码,此密码用途证书签名
Verifying - Enter PEM pass phrase:          #确认密码
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [Easy-RSA CA]:nmshuishui  #输入一个Common Name

CA creation complete and you may now import and sign cert requests.
Your new CA certificate file for publishing is at:
/root/open***-2.3.4/easy-rsa/easyrsa3/pki/ca.crt

(3)创建服务器端证书

[root@*** easyrsa3]# ./easyrsa gen-req server nopass

Note: using Easy-RSA configuration from: ./vars
Generating a 2048 bit RSA private key
................................+++
......+++
writing new private key to '/root/open***-2.3.4/easy-rsa/easyrsa3/pki/private/server.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [server]:nmshuishui-BJ  #该Common Name一定不要与创建根证书时的
                                                                          #Common Name一样,这是血与泪的教训  
Keypair and certificate request completed. Your files are:
req: /root/open***-2.3.4/easy-rsa/easyrsa3/pki/reqs/server.req
key: /root/open***-2.3.4/easy-rsa/easyrsa3/pki/private/server.key

(4)签约服务器端证书

[root@*** easyrsa3]# ./easyrsa sign server server

Note: using Easy-RSA configuration from: ./vars


You are about to sign the following certificate.
Please check over the details shown below for accuracy. Note that this request
has not been cryptographically verified. Please be sure it came from a trusted
source or that you have verified the request checksum with the sender.

Request subject, to be signed as a server certificate for 3650 days:

subject=
    commonName                = nmshuishui


Type the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes        #输入yes继续
Using configuration from /root/open***-2.3.4/easy-rsa/easyrsa3/openssl-1.0.cnf
Enter pass phrase for /root/open***-2.3.4/easy-rsa/easyrsa3/pki/private/ca.key:    #输入刚才创建根证书时的密码
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :PRINTABLE:'nmshuishui'
Certificate is to be certified until Aug 21 14:18:49 2024 GMT (3650 days)

Write out database with 1 new entries
Data Base Updated

Certificate created at: /root/open***-2.3.4/easy-rsa/easyrsa3/pki/issued/server.crt

(5)创建Diffie-Hellman,确保key穿越不安全网络的命令:

[root@*** easyrsa3]# ./easyrsa gen-dh

Note: using Easy-RSA configuration from: ./vars
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
...................................................................................................................................................................................................................+..........................................................................................................................+..................................................+.....................................................+..................................................................................................................................+............+............................................................................................................+...+............+...............+..............................................+.........................+..................................+.................+............................................................+..................................+........................................................................................................................................+................................................................+.......................................+...................................................................................................................................................++*++*

DH parameters of size 2048 created at /root/open***-2.3.4/easy-rsa/easyrsa3/pki/dh.pem

5、创建客户端证书

(1)在根目录下建立client目录

[root@*** easyrsa3]# cd
[root@*** ~]# mkdir client
[root@*** ~]# cp -R easy-rsa/ client/

(2)初始化

[root@*** ~]# cd client/easy-rsa/easyrsa3/
[root@*** easyrsa3]# ls
easyrsa  openssl-1.0.cnf  vars  vars.example  x509-types
[root@*** easyrsa3]# ./easyrsa init-pki

Note: using Easy-RSA configuration from: ./vars

init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /root/client/easy-rsa/easyrsa3/pki

(3)创建客户端key及生成证书

[root@*** easyrsa3]# ./easyrsa gen-req nmshuishui

Note: using Easy-RSA configuration from: ./vars
Generating a 2048 bit RSA private key
....................................................+++
.................................................................................................................................................................................+++
writing new private key to '/root/client/easy-rsa/easyrsa3/pki/private/nmshuishui.key'
Enter PEM pass phrase:            #输入密码
Verifying - Enter PEM pass phrase:
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [nmshuishui]:nmshuishui   #输入nmshuishui                     

Keypair and certificate request completed. Your files are:
req: /root/client/easy-rsa/easyrsa3/pki/reqs/nmshuishui.req
key: /root/client/easy-rsa/easyrsa3/pki/private/nmshuishui.key

(4)将得到的nmshuishui.req导入并签约证书

[root@*** ~]# cd open***-2.3.4/easy-rsa/easyrsa3/
[root@*** easyrsa3]#   #导入req
[root@*** easyrsa3]# ./easyrsa import-req /root/client/easy-rsa/easyrsa3/pki/reqs/nmshuishui.req nmshuishui

Note: using Easy-RSA configuration from: ./vars

The request has been successfully imported with a short name of: nmshuishui
You may now use this name to perform signing operations on this request.

[root@*** easyrsa3]#     #签约证书
[root@*** easyrsa3]# ./easyrsa sign client nmshuishui

Note: using Easy-RSA configuration from: ./vars


You are about to sign the following certificate.
Please check over the details shown below for accuracy. Note that this request
has not been cryptographically verified. Please be sure it came from a trusted
source or that you have verified the request checksum with the sender.

Request subject, to be signed as a client certificate for 3650 days:

subject=
    commonName                = nmshuishui


Type the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes       #输入yes
Using configuration from /root/open***-2.3.4/easy-rsa/easyrsa3/openssl-1.0.cnf
Enter pass phrase for /root/open***-2.3.4/easy-rsa/easyrsa3/pki/private/ca.key:    #输入创建根证书时的密码
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :PRINTABLE:'nmshuishui'
Certificate is to be certified until Aug 21 12:49:40 2024 GMT (3650 days)

Write out database with 1 new entries
Data Base Updated

Certificate created at: /root/open***-2.3.4/easy-rsa/easyrsa3/pki/issued/nmshuishui.crt   #签约成功

(5)服务端及客户端生成的文件

服务端:(/root/open***-2.3.4/easy-rsa/easyrsa3/pki)文件夹

/root/open***-2.3.4/easy-rsa/easyrsa3/pki/ca.crt
/root/open***-2.3.4/easy-rsa/easyrsa3/pki/reqs/server.req
/root/open***-2.3.4/easy-rsa/easyrsa3/pki/reqs/nmshuishui.req
/root/open***-2.3.4/easy-rsa/easyrsa3/pki/private/ca.key
/root/open***-2.3.4/easy-rsa/easyrsa3/pki/private/server.key
/root/open***-2.3.4/easy-rsa/easyrsa3/pki/issued/server.crt
/root/open***-2.3.4/easy-rsa/easyrsa3/pki/issued/nmshuishui.crt
/root/open***-2.3.4/easy-rsa/easyrsa3/pki/dh.pem

客户端:(/root/client/easy-rsa)

/root/client/easy-rsa/easyrsa3/pki/private/nmshuishui.key
/root/client/easy-rsa/easyrsa3/pki/reqs/nmshuishui.key   #这个文件被我们导入到了服务端文件,所以那里也有

(6)拷贝服务器密钥及证书等到open***目录

[root@*** ~]# cp open***-2.3.4/easy-rsa/easyrsa3/pki/ca.crt open***-2.3.4/
[root@*** ~]# cp open***-2.3.4/easy-rsa/easyrsa3/pki/private/server.key open***-2.3.4/
[root@*** ~]# cp open***-2.3.4/easy-rsa/easyrsa3/pki/issued/server.crt open***-2.3.4/
[root@*** ~]# cp open***-2.3.4/easy-rsa/easyrsa3/pki/dh.pem open***-2.3.4/

(7)拷贝客户端密钥及证书等到client目录

[root@*** ~]# cp open***-2.3.4/easy-rsa/easyrsa3/pki/ca.crt /root/client 
[root@*** ~]# cp open***-2.3.4/easy-rsa/easyrsa3/pki/issued/nmshuishui.crt /root/client
[root@*** ~]# cp /root/client/easy-rsa/easyrsa3/pki/private/nmshuishui.key /root/client

(8)为服务端编写配置文件

当安装好open***时候,它会提供一个server配置的文件例子

/root/open***-2.3.4/sample/sample-config-files/server.conf

将此例子拷贝open***目录,然后配置

[root@*** ~]# cp open***-2.3.4/sample/sample-config-files/server.conf open***-2.3.4/
[root@*** ~]# vim open***-2.3.4/server.conf
#local 192.168.200.16    #(自己***服务器 IP)
port 1194
proto udp          #使用的协议,默认使用UDP,如果使用HTTP proxy,必须使用TCP协议
dev tap            #设备可选tap和tun,tap是二层设备,支持链路层协议。#tun是ip层的点对点协议,限制稍微多一些
ca /root/open***-2.3.4/ca.crt
cert /root/open***-2.3.4/server.crt
key /root/open***-2.3.4/server.key # This file should be kept secret
dh /root/open***-2.3.4/dh.pem
keepalive 10 120
client-to-client
duplicate-cn              #如果Client使用的CA的Common Name有重复了,或者说客户都使用相同的CA #和keys连接×××,一定要打开这个选项,否则只允许一个人连接×××
comp-lzo                  #对数据进行压缩,注意Server和Client一致
max-clients 100           #定义最大连接数
persist-key               #通过keepalive检测超时后,重新启动×××,不重新读取keys,保留第一次使用的keys
persist-tun               #通过keepalive检测超时后,重新启动×××,一直保持tun或者tap设备是linkup的,否则网络连接会先linkdown然后linkup
status open***-status.log #open***状态log,定期把open***的一些状态信息写到文件中
log /var/log/open***.log
log-append /var/log/open***.log  #每次重新启动open***后保留原有的log信息,新信息追加到文件最后
verb 3                    #相当于debug level,可选0-9,具体查看manual


server 10.8.0.0 255.255.255.0            
push "dhcp-option DNS 8.8.8.8"            #向客户端发送DNS
push "dhcp-option DNS 8.8.4.4"

push "redirect-gateway"   #客户端连接成功后,默认网关改为***网关

#push "route 192.168.200.0 255.255.255.0"   #服务端push “服务器所在网络地址 子网掩码”


#网桥模式
#server 10.8.0.0 255.255.255.0
#server-bridge 192.168.200.1 255.255.255.0 192.168.200.202 192.168.200.205   #客户端拨入后用于分配的地址池,200.1将会是客户端的网关

(9)开启系统转发forward功能(网桥模式不需开启,路由模式开启)

[root@*** ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 0  改成 net.ipv4.ip_forward = 1
[root@*** ~]# sysctl -p
[root@*** ~]# sysctl -a | grep net.ipv4.ip_forward
net.ipv4.ip_forward = 1

(10)封装出去的数据包

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j MASQUERADE

 这样,所有客户端当和Open×××服务器连接后,就可以把该服务器作为对外的路由服务器使用了。(类似Proxy)

三、下载open***客户端,并进行配置

1、将客户端密钥及证书等拷出到windows备用

[root@*** ~]# cd client/
[root@*** client]# ls
ca.crt  easy-rsa  nmshuishui.crt  nmshuishui.key    #带后缀的这三个

2、安装open***-gui工具

(1)将D:\Program Files (x86)\Open×××\sample-config\client.o***复制到D:\Program Files (x86)\Open×××\config

(2)将从linux中拷贝出来的三个密钥及证书放到D:\Program Files (x86)\Open×××\config下

open***2.3.4服务器部署_第1张图片

3)编辑D:\Program Files (x86)\Open×××\config\client.o***,修改为

client
dev tap
proto udp
remote 192.168.200.16 1194 #如果在外网,就改成对应的公网IP
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert nmshuishui.crt
key nmshuishui.key
comp-lzo
verb 3

四、启动服务、测试

1、在***服务器上启动open***服务

[root@*** ~]# /usr/local/sbin/open*** --config open***-2.3.4/server.conf &
[root@*** ~]#
[root@*** ~]# echo "/usr/local/sbin/open*** --config /etc/open***/server.conf &" >>/etc/rc.local  #设为开机启动

2、在open***-gui上右键Connect输入密码连接

open***2.3.4服务器部署_第2张图片

3、查看***状态

(1)开启网桥模式前

open***2.3.4服务器部署_第3张图片

(2)开启网桥模式后

open***2.3.4服务器部署_第4张图片


五、关于MTU

open***中设置的MTU值不要比外网的大,否则要拆包,影响效率

以下以查看和修改eth0为例:

1、查看MTU值

# cat /sys/class/net/eth0/mtu

2、 修改MTU值

# echo "1460" > /sys/class/net/eth0/mtu

  顺便说一句,在/sys/class/net/eth0/目录中还有很多文件,对应了eth0的各个参数,可以手动的修改里面的值达到配置网卡参数的目的。

[root@cloud ~]# netstat  -i
Kernel Interface table
Iface       MTU Met    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
lo        16436   0        4      0      0      0        4      0      0      0 LRU
seth0      1460   0  1643368      0      0      0    65256      0      0      0 BMRU
tap0       1500   0    19515      0      0      0    20732      0      0      0 BMRU

  TPlink默认的MTU值为1480,在 网络参数 -- WAN口设置 -- 高级设置 里面

  你可以在客户端设置路由,访问国内网站时不走×××

六、原理

1,虚拟网卡

在Linux2.4版本以上,操作系统支持一个名为tun的设备,tun设备的驱动程序中包含两个部分,一部分是字符设备驱动,一部分是网卡驱动。网卡的驱动把从TCP/IP协议栈收到的数据包结构skb放于tun设备的读取队列,用户进程通过调用字符设备接口read获得完整的IP数据包,字符驱动read函数的功能是从设备的读取队列读取数据,将核心态的skb传递给用户;反过来字符驱动write函数给用户提供了把用户态的数据写入核心态的接口,write函数把用户数据写入核心空间并穿入TCP/IP协议栈。该设备既能以字符设备的方式被读写,作为系统的虚拟网卡,也具有和物理网卡相同的特点:能够配置IP地址和路由。对虚拟网卡的使用是Open×××实现其SSL×××功能的关键。

2,地址池以及路由

Open×××服务器一般需要配置一个虚拟IP地址池和一个自用的静态虚拟IP地址(静态地址和地址池必须在同一个子网中),然后为每一个成功建立SSL连接的客户端动态分配一个虚拟IP地址池中未分配的地址。这样,物理网络中的客户端和Open×××服务器就连接成一个虚拟网络上的星型结构局域网,Open×××服务器成为每个客户端在虚拟网络上的网关。Open×××服务器同时提供对客户端虚拟网卡的路由管理。当客户端对Open×××服务器后端的应用服务器的任何访问时,数据包都会经过路由流经虚拟网卡,Open×××程序在虚拟网卡上截获数据IP报文,然后使用SSL协议将这些IP报文封装起来,再经过物理网卡发送出去。Open×××的服务器和客户端在虚拟网卡之上建立起一个虚拟的局域网络,这个虚拟的局域网对系统的用户来说是透明的。

3,客户端与服务端安全连接的建立

Open×××的服务器和客户端支持tcp和udp两种连接方式,只需在服务端和客户端预先定义好使用的连接方式(tcp或udp)和端口号,客户端和服务端在这个连接的基础上进行SSL握手。连接过程包括SSL的握手以及虚拟网络上的管理信息,Open×××将虚拟网上的网段、地址、路由发送给客户端。连接成功后,客户端和服务端建立起SSL安全连接,客户端和服务端的数据都流入虚拟网卡做SSL的处理,再在tcp或udp的连接上从物理网卡发送出去。

4,数据包的处理过程

4.1发送数据流程

应用层的外出数据,经过系统调用接口传入核心TCP/IP层做处理,在TCP/IP经过路由到虚拟网卡,虚拟网卡的网卡驱动发送处理程序hard_start_xmit()将数据包加入skb表并完成数据包从核心区到用户区的复制,Open×××调用虚拟网卡的字符处理程序tun_read(),读取到设备上的数据包,对读取的数据包使用SSL协议做封装处理后,通过socket系统调用发送出去。

4.2接收数据流程
物理网卡接收数据包,经过核心TCP/IP上传到Open×××,Open×××通过link_socket_read()接收数据包,使用SSL协议进行解包处理,经过处理的数据包Open×××调用虚拟网卡的字符处理程序tun_write()写入虚拟网卡的字符设备,设备驱动程序完成数据从用户区到核心区的复制,并将数据写入skb链表,然后调用网卡netif_rx()接收程序,数据包再次进入系统TCP/IP协议栈,传到上层应用程序。


5,数据包的封装

Open×××提供tun和tap两种工作模式。在tun模式下,从虚拟网卡上收到的是不含物理帧头IP数据包,SSL处理模块对IP包进行SSL封装;在tap模式下,从虚拟网卡上收到的是包含物理帧头的数据包,SSL处理模块对整个物理帧进行SSL封装。Tap模式称为网桥模式,整个虚拟的网络就像网桥方式连接的物理网络。这种模式可以传输以太网帧、IPX、NETBIOS等数据包,应用范围更广。

6,Open×××与Openssl

Open×××软件包需要和openssl软件一起安装,因为Open×××调用了Openssl函数库,Open×××的客户端和服务端建立SSL链接的过程是通过调用Openssl来实现的。通过bio_write()/函数把数据写入Openssl的状态机通道,bio_read()从Openssl读取结果。Open×××还调用Openssl的加解密函数处理转发的数据包。