前言

因为坛服务器放在学校机房里面。可能是网络中心的老师为了省事,在防火墙层面封掉了几乎所有端口。

学校机房——(只开放20,21,22,23,80,443端口)——校园网————(只开放80,443端口)————普通学生

就是这么丧心病狂。平常对服务器有什么改动,非常麻烦。
对于我来说,只有两个端口(80和443)是可用的,80端口是不能占用的,这个得留给http,443端口,由于我们用不到https,就征用了吧。FTP,远程桌面两个只能选一个。各种搜索,最后决定用VPN来解决。
VPN分很多种,大致看了下,满足条件只需要一个端口的,好像只有OpenVPN,于是开始了折腾。

安装OpenVPN

这是第一个让人蛋碎的位置。**在OpenVPN 官网无论是稳定版,还是开发版本。其附带的TAP-Win32 virtual ethernet adapter 都只是V8版本,在64位的Windows下根本无法使用。**最后,在中国科技大学找到了一个2.13版本。这个附带的TAP才是V9版本的,这个才是可用的。 按部就班的在服务器端装好OpenVPN,OpenVPN不分Server和Client版本,都是同样的程序,只是配置文件不同罢了。

开始菜单-所有程序-OpenVPN-Add a new TAP-Win32 virtual ethernet adapter

只有在“网络连接”中看到一个为TAP-Win32 Adapter V9的本地连结时,准备工作才算完成。

签发证书

这是一个我觉得蛋碎的地方。进入OpenVPN的安装目录下的“easy-rsc”文件夹。其实这一步网上有很多教程,但是都是互相抄袭。

  1. 列表项目 网上所有的人都只是让修改这一段
set KEY_COUNTRY=CN
set KEY_PROVINCE=Huibei
set KEY_CITY=Wuhan
set KEY_ORG=XingLin Studio
set KEY_EMAIL=xld@hbtcm.edu.cn

但是,都没有管开头的

set HOME=%ProgramFiles%\OpenVPN\easy-rsa
set KEY_CONFIG=openssl.cnf

后面证书的错误,源头都在这儿。现在基本都是64位的系统,而安装文件夹是在(X86)下面的。需要把%ProgramFiles%替换成真实的安装路径,这个能在下一步的clean-all中表现的很明显。 2.然后按部就班:

C:\Program Files (x86)\OpenVPN\easy-rsa>init-config
\>
C:\Program Files (x86)\OpenVPN\easy-rsa>copy vars.bat.sample vars.bat
\>已复制         1 个文件。
\>
C:\Program Files (x86)\OpenVPN\easy-rsa>copy openssl.cnf.sample openssl.cnf
\>已复制         1 个文件。
\>
C:\Program Files (x86)\OpenVPN\easy-rsa>vars
\>
C:\Program Files (x86)\OpenVPN\easy-rsa>clean-all
\>已复制         1 个文件。
\>已复制         1 个文件。</pre>  
如果上一步的%ProgramFiles%没有修改,这一步在clean-all时,会出现:
  <pre>
C:\Program Files (x86)\OpenVPN\easy-rsa>clean-all.bat
\>系统找不到指定的路径。
\>已复制         1 个文件。
\>已复制         1 个文件。

3.开始签名:

build-ca                      //证书管理机构
build-dh                      //加密之类的?
build-key-server server       //签发服务器证书
build-key client              //签发客户端证书

签发的时候,该怎么填就怎么填,需要注意的是,client证书如果需要多份,邮箱和名称不要重复。而且,如果之前的%ProgramFiles%没有改正的话。会出现诸如:unable to write 'random state'这样会导致签发的证书无法使用,很明显的就是,会产生0KB的证书。 签发的证书会放在key文件夹下面。

服务器配置

ca.crt
dh1024.pem
server.crt
server.key

四个文件放到config文件夹下。
再将sample-config文件夹下的server.ovpn复制一份到config文件夹下。 编辑server.ovpn。

#略去一些不必要的注释

# Which local IP address should OpenVPN
# listen on? (optional)
# 设置监听的IP,默认是所有IP
;local a.b.c.d

# 设置监听的端口
port 1194

# 选择TCP或者UDP服务
;proto tcp
proto udp

# "dev tun"和"dev tap"模式 
;dev tap
dev tun

# 如果Windows有多个虚拟的TAP网卡,需要指定一个
;dev-node MyTap

# 签发的证书等等……
# 可以是相对路径,也可以是绝对路径
ca ca.crt
cert server.crt
key server.key  # This file should be kept secret
dh dh1024.pem

# 设置分配的虚拟网段
server 10.8.0.0 255.255.255.0

# 记录下地址分配情况
# 不用每次都分配
ifconfig-pool-persist ipp.txt

# 桥接模式?(这个没用,不熟)
;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100
;server-bridge

# 暴露VPN Server的内网环境给Client
;push "route 192.168.10.0 255.255.255.0"
;push "route 192.168.20.0 255.255.255.0"


# 这一段我也不懂
;client-config-dir ccd
;route 192.168.40.128 255.255.255.248
;client-config-dir ccd
;route 10.9.0.0 255.255.255.252
;learn-address ./script
#这一段我也不懂

# 使Client通过Server访问网络
;push "redirect-gateway def1 bypass-dhcp"

# 分配给Client的DNS
;push "dhcp-option DNS 208.67.222.222"
;push "dhcp-option DNS 208.67.220.220"

# 是否开启Client之间的互相访问
;client-to-client

# 一个证书不能被多个Client同时使用
;duplicate-cn

# 每10s连接一次,120s无响应为掉线
keepalive 10 120

# The server and each client must have
# a copy of this key.
# The second parameter should be '0'
# on the server and '1' on the clients.
;tls-auth ta.key 0 # This file is secret

# 加密方式
;cipher BF-CBC        # Blowfish (default)
;cipher AES-128-CBC   # AES
;cipher DES-EDE3-CBC  # Triple-DES

# 数据压缩
comp-lzo

# 最大同时链接数量
;max-clients 100

# 不知道是干嘛的
;user nobody
;group nobody
persist-key
persist-tun

# 输出服务状态日子
status openvpn-status.log
# 日志设置
;log         openvpn.log
;log-append  openvpn.log

# 日志详细程度,越大越详细
# 0 is silent, except for fatal errors
# 4 is reasonable for general usage
# 5 and 6 can help to debug connection problems
# 9 is extremely verbose
verb 3

# Silence repeating messages.  At most 20
# sequential messages of the same message
# category will be output to the log.
;mute 20

最后的配置文件为:

#基本信息
port 443
proto tcp
dev tun

#证书相关
ca ca.crt
cert server.crt
key server.key
dh dh1024.pem

# 设置虚拟网段
 server 10.8.1.0 255.255.255.0
ifconfig-pool-persist ipp.txt

# 本地IP
local 218.197.176.230
# 设置Client相关信息
push "route 218.197.176.230" 
push "redirect-gateway def1 bypass-dhcp bypass-dns"
push "dhcp-option DNS 218.197.176.8"
push "dhcp-option DNS 202.103.24.68"

# 连接信息
keepalive 10 120
comp-lzo
persist-key
persist-tun

# 日志信息
status openvpn-status.log
verb 4

现在只要以管理员身份运行OpenVPN即可。

客户端配置

ca.crt
client.crt
client.csr
client.key

和config-sample文件夹下的client.ovpn 复制到config文件夹下。 和Server端差不多

remote 218.197.176.230 443

写上服务器和端口即可。 依旧是以管理员身份启动即可。

一些备忘

这个时候可能顺利连接了,但是无法上网。原因也很简单:Server端在网络连接选择共享。具体看图:
配置图

大致就是这样了。 善用搜索,但不迷信。


更新:如何在client端选择部分网段走vpn。

在client端配置文件添加以下内容即可:

route-nopull
#内网
route 10.8.1.0 255.255.255.0 vpn_gateway
route 218.197.176.0 255.255.255.0 vpn_gateway
#结束

#cnki
route 211.151.247.0 255.255.255.0 vpn_gateway
route 116.211.96.0 255.255.255.0 vpn_gateway
route 119.167.209.0 255.255.255.0 vpn_gateway
route 121.194.4.0 255.255.255.0 vpn_gateway
route 211.151.93.0 255.255.255.0 vpn_gateway
#cnki

首先是用route-nopull无视掉sever端推送来的"redirect-gateway def1 bypass-dhcp bypass-dns", 然后是自建路由表,格式是

route ip段 子网掩码 经由方式

vpn_gateway表明是走vpn,net_gateway则是走本机网关。


部分资料:

超详细PDF