在windows下编译openvpn

在windows下编译openvpn
译文原地址:https://community.openvpn.net/openvpn/wiki/BuildingOnWindows


壹、概要
警告:这片文章已经被废弃,且可能只能应用于部分历史版本的OpenVpn,当前的windows下的OpenVpn创建方法在这儿:Building Using Generic Build system
(https://community.openvpn.net/openvpn/wiki/BuildingUsingGenericBuildsystem)


***********************************分割线**************************************
传统windows下编译openvpn的方法是使用"domake-win"命令。这要使用gnu自动工具和gcc,以及MinGw环境或在Unix/Linux的交叉环境。更早的选项在这里列出,但是在2011年6月29日,这个方法是不完整的(译者:原文没给出链接)。你可以从这些脚本中获取更多的信息。你会发现 "domake-win"命令在一些新的软件版本中会运行出错,比如编译OpenSSl 1.0.0。(译者:openvpn需要依赖openssl,但要编译新版openssl时,会碰到不支持“domake-win"命令。可以查看我的相关文章)


因为”domake-win“脚本来创建openvpn被发现异常困难,一个新的相关的基于python的简单方法被写出。这个新的方法使得在windows可以更方便的创建。幸运的是这个新的创建方法只要使用visiual studio express工具,(nmake,compiler,linker,等)。所以不需要学习vs的图形用户接口。一体化的Vs也是可能的。

贰、安装要求 
1、通用
(1)、创建OpenVpn需要windows 软件开发包,这很重要(下载地址:https://www.microsoft.com/en-us/download/details.aspx?id=8279)

(2)、windows驱动包,这用于创建Tun/Tap驱动,下载地址:

https://www.microsoft.com/whdc/devtools/WDK/default.mspx


(3)、winrar或其他可以解压tar.gz/tar.bz2的工具,这用于解压LZO和OpenSSl。你可以忽略这些,假如你安装了gitExtensions且熟悉Linx/Unix的命令行。

(4)、GitExtensions是一个gui设置,可以使得在windows上使用git更方便。在很多情况下,这包括了git bash,这可以使你使用 Linux/Unix工具更方便。假如你想使用最新的开发源码,你就会需要Git了。

2、基于python的脚本系统
VS2008专业版被用于在windows上创建Openvpn。如果是express版本可能不起作用。同时也要确保安装时选择了典型安装而不是完全安装 。完全安装会安装x86 cross-tools,这可能会引起麻烦,难于调试。可以查看”常见故障“段落。

在新的创建方法中,需要python,所以你需要先安装python。

在创建OpenSSl中需要ActivePerl,所以创建OPenvpn也需要它。在这里看细节https://ehsanakhgari.org/blog/2008-05-04/compiling-openvpn-windows。(译者:err502,不知道为什么)。一个标准的Perl安装程序可以解决这个问题。(译者:到这个地址:http://www.perl.org/get.html下载最新的perl,安装后把安装路径放入环境变量中。更详细的安装请自行百度)

编译Openssl也需要Nasm(译者:Linux上常用到的汇编器)。所有的创建说明在他的install 文件中。在创建完后,你需要把nasm.exe添加到环境变量Path中


3、MinGw
MinGw是c的编译器和开发者工具包。两都都被包括在标准MinGw安装exe中。所以MinGw默认包括nasm和perl(译者:此句存疑)


使用MinGW创建pkcs11-helper需要man2html,确保man2html第一行是”#!/bin/perl and copy it to /bin“。

叁、编译OpenSSL
1、共同步骤
从https://www.openssl.org/source/下载Openssl,使用tar.exe或gitExtensions或MinGW。(译者:下载openssl后,能解压就行对解压工具没要求。)。使用最新的openssl源码包,确保没有安全漏洞。

按照下面步骤完成OpenSSL编译后,复制OpenSSL安装路径到openvpn解压路径中的openssl文件夹中(没有openssl文件夹就自己创建一个。)

2、基于python的创建方法
运行VS2008命令行工具,这个工具在开始菜单中(译者:找不到的自己百度)。与标准的命令行工具不行,vs2008命令行工具有所有的vc路径。

在这个工具行中,你先要通过perl编译OpenSSl。
C:\openssl-1.0.0> perl Configure VC-WIN32 --prefix=c:/<openssl-install-directory>
然后运行nasm命令
C:\openssl-1.0.0> ms\do_nasm
然后编译OpenSSl,通过已经生成的makfile
C:\openssl-1.0.0> nmake -f ms\ntdll.mak
C:\openssl-1.0.0> nmake -f ms\ntdll.mak test
C:\openssl-1.0.0> nmake -f ms\ntdll.mak install 


3、MinGW创建方法
运行MinGW命令行,并且进入openssl源码目录 ,执行命令:

$ ./config
$ make
$ make test

OpenSSL installation has to be done manually using something like this(译者:不知道怎么翻):

$ mkdir -p /c/openssl/include /c/openssl/bin /c/openssl/lib
$ cp -r include/openssl /c/openssl/include/
$ cp apps/openssl.exe /c/openssl/bin
$ cp libcrypto.a libssl.a /c/openssl/lib


肆、编译pkcs11-helper
1、共同步骤
下载最新的pkcs11-helper,地址:​http://www.opensc-project.org ,并且解压。


2、基于python的创建方法

pkcs11的为VS的安装步骤在install file中。

要编译pkcs11-helper,做以下事项:
a、进入pkcs11-helper-<version>/lib
b、复制<openssl-install-directory>/lib/libeay32.lib 到上面的路径中。这是链接器的要求
c、运行nmake -f makefile.w32-vc OPENSSL=1 OPENSSL_HOME=<path-to-openssl-sources>

理论上,如果你要重新编译,要先清理一下:
$ nmake -f Makefile.w32-vc clean
然后重复上面的步骤。除非你使用1.08或更新版本,否则你会碰上这样的错误信息:
pkcs11h-threading.c(477) : error C2036: 'void *' : unknown size

如果碰上了,按http://thread.gmane.org/gmane.comp.encryption.opensc.devel/8381的建议来编辑文本。只要libeay32.lib在创建目录中出现了,就说明编译成功了。

openvpn只对lib/libpkcs11-helper-1.dll感兴趣,并在用NSI生成安装文件时使用到。


3、MinGW创建方法
你需要连接pkcs11-helper 到 openssl 库。启动MinGW,运行
$ openssl_home=/c/openssl
$ ./configure --prefix=/ --disable-crypto-engine-gnutls --disable-crypto-engine-nss PKG_CONFIG=true OPENSSL_CFLAGS="-I${openssl_home}/include" OPENSSL_LIBS="-L${openssl_home}/lib -lcrypto"


伍、编译LZO
1、共同步骤
编译openVpn需要LZO库,一旦你已经解压了源码,打开 b/00readme.txt文件可看。

2、基于python的创建方法
假如一切准备好,你只需要运行bat文件:
C:\lzo-2.0.4> B\win32\vc_dll.bat
即可。
但这并不意味着安装上了lzo,事实上,你需要手动复制相关文件到openvpn的创建目录中

3、MinGW创建方法
The usual autotools process applies here:(译者:不会翻)
$ ./configure
$ make
$ make check
$ make test


陆、编译openvpn
一、安装依赖文件。
1、基于python创建
这篇文章中用的新的基于python创建方法已经贯穿到所有的修改中,在2010后,2011年前。强烈建议使用最新的git for window来获取最新的git 开发代码。检查这些项(https://community.openvpn.net/openvpn/wiki/TesterDocumentation)来查看哪个git uri来使用。要不然获取 OpenVpn2.2.0或之后版本,这些版本的大的修改都已经被提交应用了。

OpenVpn有几个编译和运行时的依赖项,这些需要被放到特定的目录中。

(译者:这一段解释目录结构,太多了,懒得翻)
<openvpn-build-root>: root build directory, e.g. C:\openvpn-build
<openvpn-sources>: a directory containing openvpn sources (from tarball/zip/git)
tapinstall
7600: copy of ​devcon.exe build directory. Can be found from C:\WINDDK\7600.16385.1\src\setup\devcon or similar, depending on the version of WINDDK. Note that in OpenVPN world devcon.exe is (confusingly) also known as tapinstall.exe. This directory is not needed if you use prebuilt TAP-drivers. NOTE: This directory gets copied during configuration phase (win/config_ti.py) to <openvpn-sources>/tapinstall.
pkcs11-helper: copy of pkcs11-helper build directory
lib: must also contain libpkcs11-helper-1.dll and libpkcs11-helper-1.dll.manifest files produced by pkcs11-helper build
openssl: copy of the openssl install directory
lzo
include: a copy of the include directory in the lzo build directory
lzo: the directory with actual lzo header files
bin: must also contain the lzo2.dll and lzo2.dll.manifest files generated by lzo build
lib: must also contain the lzo2.lib file generated by lzo build
Microsoft.VC90.CRT: a copy of C:\Program Files\MicrosofT Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT directory; the exact path may vary. Note that using the wrong version will cause issues on 32-bit or 64-bit Windows versions.
tap-prebuilt: this directory contain prebuilt, signed TAP-drivers and tapinstall.exe extracted from an OpenVPN installer. This is only needed if you don't build the TAP-drivers yourself.
i386: should contain 32-bit versions of the TAP-driver and tapinstall.exe
amd64: should contain 64-bit versions of the above
openvpn-gui: this directory should contain the openvpn-gui*.exe executable (see win/settings.in)
signtool: this directory should contain signtool.exe, which can be found from the Microsoft SDK directory (e.g. C:\Program Files\Microsoft SDKs\Windows\V6.0A\bin). However, it's Python wrapper (Sign class) is only available internally at OpenVPN Technologies, so you can skip this dependency.

3、MinGw 创建方法
参考旧的依赖包:https://openvpn.net/prebuilt/
二、设置参数
设置完目录后,你应该检查你的OpenVpn的编译配置文件是否需要修改。
1、基于python创建
<openvpn-sources>\win\settings.in: 基于python的编译配置。大部分编译配置都在这里
<openvpn-sources>\win\config.h.in:静态头文件,与config.h同,被自动工具生成,一般不修改。

<openvpn-sources\version.m4:包含 tap-driver 驱动版本信息,正常这里不需要修改。
注意:你需要手动添加vcvarsall.bat到path环境变量中,在64位平台中,这通常在C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC 目录中

2、MinGW创建方法
<openvpn-sources>/install-win32/settings.in:为minGw的编译配置文件,通过只有这个文件需要被编辑

<openvpn-sources>/version.m4:包含 tap-driver 动版本信息,正常这里不需要修改。


三、编译VPN
1、基于python的创建方法
打开VS2008命令行,并且进入 <openvpn-sources>\win目录下,你现在可以创建OpenVpn+tap驱动的使用了:
C:\openvpn-build\openvpn-testing\win> python build_all.py --unsigned

这会编译Openvpn.exe,OpenServ.exe和Tap驱动,并且复制结果到 <openvpn-sources>\dist中。
上文中的--unsigned参数是tap驱动签名的开关。
The --unsigned switch disables TAP driver signing, which would not work anyways because the Sign Python module is only available internally at OpenVPN Technologies. (译者:这句翻不来)

如果你想测试新的tap驱动版本,在windows 7/vista 64bit,你需要self-sign他们,并且jump through several hoops。如果货币于使用已存在的签名过的tap驱动,你可以从一个已存在的安装器中提取签名过的tap驱动并且忽略tap驱动(译者:这段翻译不太准确,请看原文):
C:\openvpn-build\openvpn-testing\win> python build_all.py --unsigned --notap

清理编译前后的项目:
C:\openvpn-build\openvpn-testing\win> python build.py clean

查看可行的编译选项,使用:
C:\openvpn-build\openvpn-testing\win> python build_all.py --help


2、MinGW创建方法
编译过程很简单:
$ cd <openvpn-sources>
$ ./domake-win

看下给出的错误,检查,重试,直至成功。

四、从Openvpn安装程序中提取已签名过的tap驱动
从OpenVpn安装程序中提取tap驱动(详情:https://community.openvpn.net/openvpn/wiki/ManagingWindowsTAPDrivers),然后做如下事项:
将32-bit签名的tap驱动和对应的tapInstall.exe文件复制到 <openvpn-build-root>\tap-prebuilt\i386
64-bit版本的复制到 <openvpn-build-root>\tap-prebuilt\amd64

五、打包OpenVpn。
1、基于python创建
………………(译者:头晕,有空再翻)…………





常见故障处理:
一、在Openssl编译中出现警告:
二、vs2008交叉编译OpenSSl出错:
三、输出tap驱动自签名证书
四、vcvarsall.bat文件未发现
五、代码调试
vs2008有一个实时调试工具(just in time debugger https://msdn.microsoft.com/en-us/library/5hs4b7a6.aspx),这工具会在程序崩溃时自动运行。这在调试openvpn.exe时很有用。调试器可以显示哪一行代码出了错,哪个文件是可行的等信息。

要使用这个调试器,你需要通过适当的调试标志来编译openvpn.exe。这通过 win/seetings.in中的PRODUCT_OPEVPN_DEBUG 来控制 :

# Build debugging version of openvpn.exe
!define PRODUCT_OPENVPN_DEBUG

…………(译者:还有一点,不翻了)……



























猜你喜欢

转载自blog.csdn.net/sspdfn/article/details/80626041