Virtual Private Network (VPN) Server 虚拟私人网络伺服器

前言

近来有读者查询,因为不明的网络原因而无法看到一些寄存在外国的网站,手机也无法使用一些通讯软件,希望笔者介绍如何建立 VPN Server 以解决网络连接问题。本文会介绍如何使用较为常用的云端 (Cloud) 方案去建立极低成本的私人 VPN Server。

⛑️  本文目的只为学术研究,解释网络原理及应用方法,并不鼓励任何犯法行为,敬请留意。⛑️

VPN Server 的原理

所谓 VPN (Virtual Private Network),虚拟私人网络,泛指一些网络管道,透过 VPN 加密 (Encryption),保护使用者所传送的讯息免受窃取或监听。由於 VPN Server 的架设不受地域限制,电脑或手机甚至可以隐藏自己的真正 IP Address,进一步保护私隐。

请看以下例子,一台位於香港的手机,透过互联络与一台位於美国的 VPN Server 建立 VPN 管道,并以此 VPN Server 作为闸道 (Gateway) 连接互联络上的其他伺服器,例如浏览网页或使用通讯软件,互联网络上所有的伺服器就会以为这台手机是来自美国,而不知道其真正地理位置。而且,管道中的讯息经过加密,就算是互联网供应商也无法看到其内容。

vpn server

事实上,世界各地有很多 VPN 服务供应商可以提供这种 VPN 服务,然而自设 VPN Server 有以下好处:

  1. 自设 Server 频宽高,数据使用量没有限制,但需按使用量付款。Cloud 的数据价格极为便宜,每 Gb 数据只需约 1 港元,由於 VPN 同时涉及数据输入及输出,因此费用将为 2 倍,即约 2 港元 1Gb。
  2. 一台 Server 可供多台设备及多位使用者同时使用。
  3. 最重要的是,国外的 VPN Server 可能会因为某些网络问题而突然无法连接,自设 Server 基本上不会出现这个问题。

第 1 步:启动 Cloud 上的 VPN Server

我们不会跑到外国去购买和安装一台 Server,取而代之的是使用网上的 Cloud 服务,例如 AWS (Amazon Web Service) 或者 Google Cloud。意思是租用这些公司设在国外的数据中心里面的伺服器里面的一丁点运算能力去跑我们的 VPN Server,本文会用 AWS 在作示范。不过读者需留意,Cloud 服务供应商一般会按 Server 的运算力 (Computing Power) 收取主机费用,但私人使用的 VPN Server 所需运算力极少,很多时候可以得到免费优惠,例如本文介绍的 AWS,可以整整 1 年不用付款。

首先到 https://aws.amazon.com/tw/console/ 申请帐号,只是填表格而已,不多说了。

vpn server

我们想要使用的 VPN Server 名叫 OpenVPN,可以在 AWS 里的一个叫 Marketplace 地方找到。Marketplace 就是各软体供应商放於 AWS 给大家使用的 Server。请在「寻找服务」中输入 Marketplace,便会找到 AWS Marketplace Subscriptions。

vpn server

然後搜寻 openvpn,点选 OpenVPN Access Server。

vpn server

然後按操作,再点选 Launch new instance。

vpn server

区域请选 us 字头,即选用位於美国的数据中心,其他不用更改,然後点透过 EC2 继续启动。

vpn server

然後是选择 Server 的运算能力,运算能力越强则收费越贵。由於现在要设置的 VPN Server 只供私人使用,理论上选最平价的便可以了。幸运地,AWS 正在推出 12 个月免费试用,选 t2.micro 便可得到此优惠,实乃穷人之选!🤑 完成後点选 Review and Launch。

vpn server

再送 30G 硬碟空间,照单全收便可。

vpn server

由於第一次使用 AWS,需要产生 Key Pair 在稍後登入 Server 时使用。然後按 Download Key Pair 把 Key 储存在电脑内。最後,按 Launch Instances 完成设定。

vpn server

回到服务版面选 EC2。

vpn server

再选 Running Instances。

vpn server

看到 Server 已经预备好了,留意 IPv4 Public IP 这一栏就是这台 VPN Server 的 IP 了。

vpn server

第 2 步:用 Key 登入 VPN Server

刚才所产生的 Key 是用来登入 Server 的,为了保安,Cloud 供应商的 Server 通常会使用 Key 来登入。假设读者使用 MacOS,先打开终端机,到刚才下载 Key 的 Folder 执行以下指令把 Key 的属性改为唯读。Windows 用家则请按这里查看连接方法。

MacBook:~ Jan$ cd Downloads/
MacBook:Downloads Jan$ chmod 400 key.pem 

然後便可使用以下指令利用 Key 登入 Server,IP 就是刚才看到在 Cloud 上的 Server 的 IP。

MacBook:Downloads Jan$ ssh -i "key.pem" openvpnas@18.209.111.198
The authenticity of host '18.209.111.198 (18.209.111.198)' can't be established.
ECDSA key fingerprint is SHA256:WbAO2tdMP+cbXpPJ5K1EEEcgSYk+m3WXtQSC6XfzUXs.
Are you sure you want to continue connecting (yes/no)? yes

如果成功的话,你会看到一大推 License 讯息,答 yes 便可以了。

Welcome to OpenVPN Access Server Appliance 2.7.5

  System information as of Wed Oct  9 11:33:47 UTC 2019

  System load:  0.0               Processes:           90
  Usage of /:   29.5% of 7.69GB   Users logged in:     0
  Memory usage: 22%               IP address for eth0: 172.31.40.237
  Swap usage:   0%


          OpenVPN Access Server
          Initial Configuration Tool
------------------------------------------------------
OpenVPN Access Server End User License Agreement (OpenVPN-AS EULA)

    1. Copyright Notice: OpenVPN Access Server License;
       Copyright (c) 2009-2019 OpenVPN Inc. All rights reserved.
       "OpenVPN" is a trademark of OpenVPN Inc.
    2. Redistribution of OpenVPN Access Server binary forms and related documents,
       are permitted provided that redistributions of OpenVPN Access Server binary
       forms and related documents reproduce the above copyright notice as well as
       a complete copy of this EULA.

<--Output Omitted-->

   11. Purchasing a license key does not entitle you to any special rights or
       privileges, except the ones explicitly outlined in this user agreement.
       Unless otherwise arranged prior to your purchase with OpenVPN,
       Inc., software maintenance costs and terms are subject to change after your
       initial purchase without notice. In case of price decreases or special
       promotions, OpenVPN Inc. will not retrospectively apply
       credits or price adjustments toward any licenses that have already been
       issued. Furthermore, no discounts will be given for license maintenance
       renewals unless this is specified in your contract with OpenVPN Inc.

Please enter 'yes' to indicate your agreement [no]: yes

第 3 步:设定 VPN Server

第一次登入 Server,需要进行初期设定,只要答几个问题便可完成设定,请看以下解说。

Once you provide a few initial configuration settings,
OpenVPN Access Server can be configured by accessing
its Admin Web UI using your Web browser.

Will this be the primary Access Server node?
(enter 'no' to configure as a backup or standby node)
> Press ENTER for default [yes]: yes
两台 OpenVPN 可以进行 HA (High Availability) 运作,第一台是 Primary (Active),第二台是 Secondary (Standby),如果这是读者第一台建立的 VPN Server 选 yes 便对了。

Please specify the network interface and IP address to be
used by the Admin Web UI:
(1) all interfaces: 0.0.0.0
(2) eth0: 172.31.40.237
Please enter the option number from the list above (1-2).
> Press Enter for default [1]: 1
用那组 Interface 作为 Admin Interface 的 IP,初心者选 (1) 便可。

Please specify the port number for the Admin Web UI.
> Press ENTER for default [943]: 943
如无特别考虑,用预设值便可。

Please specify the TCP port number for the OpenVPN Daemon
> Press ENTER for default [443]: 443
如无特别考虑,用预设值便可。

Should client traffic be routed by default through the VPN?
> Press ENTER for default [no]: yes
重要:用户的 Traffic 是否要路由至 VPN Tunnel 之中,当然要,这样才可利用 VPN Server 作为 Gatewy 连接互联网。

Should client DNS traffic be routed by default through the VPN?
> Press ENTER for default [no]: yes
重要:用户的 DNS Traffic 是否要路由至 VPN Tunnel 之中,也是需要的。

Use local authentication via internal DB?
> Press ENTER for default [yes]: yes
初心者选 yes 便可。

Private subnets detected: ['172.31.0.0/16']

Should private subnets be accessible to clients by default?
> Press ENTER for EC2 default [yes]: yes
初心者选 yes 便可。

Private subnets detected: ['172.31.0.0/16']

Should private subnets be accessible to clients by default?
> Press ENTER for EC2 default [yes]: yes
初心者选 yes 便可。

To initially login to the Admin Web UI, you must use a
username and password that successfully authenticates you
with the host UNIX system (you can later modify the settings
so that RADIUS or LDAP is used for authentication instead).

You can login to the Admin Web UI as "openvpn" or specify
a different user account to use for this purpose.

Do you wish to login to the Admin UI as "openvpn"?
> Press ENTER for default [yes]: yes
建立 openvpn 作为管理者帐号。

> Please specify your OpenVPN-AS license key (or leave blank to specify later): 
免费版本已支援 2 台装置连接 VPN,按 ENTER 跳过便可。


Initializing OpenVPN...
Removing Cluster Admin user login...
userdel "admin_c"
Adding new user login...
useradd -s /sbin/nologin "openvpn"
Writing as configuration file...
Perform sa init...
Wiping any previous userdb...
Creating default profile...
Modifying default profile...
Adding new user to userdb...
Modifying new user as superuser in userdb...
Getting hostname...
Hostname: 18.209.111.198
Preparing web certificates...
Getting web user account...
Adding web group account...
Adding web group...
Adjusting license directory ownership...
Initializing confdb...
Generating init scripts...
Generating PAM config...
Generating init scripts auto command...
Starting openvpnas...

NOTE: Your system clock must be correct for OpenVPN Access Server
to perform correctly.  Please ensure that your time and date
are correct on this system.

Initial Configuration Complete!

You can now continue configuring OpenVPN Access Server by
directing your Web browser to this URL:

https://18.209.111.198:943/admin
Login as "openvpn" with the same password used to authenticate
to this UNIX host.

During normal operation, OpenVPN AS can be accessed via these URLs:
Admin  UI: https://18.209.111.198:943/admin
Client UI: https://18.209.111.198:943/

See the Release Notes for this release at:
Access Server Release Notes

最後还要帮 openvpn 这个帐号设定密码。

openvpnas@openvpnas2:~$ sudo passwd openvpn
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully

第 4 步:建立使用者帐号

现在可以在浏览器输入 https://<伺服器 IP>:<伺服器 Port>/admin 登入 VPN Server 的界面,以本文建立的 Server 为例,轮入 https://18.209.111.198:943/admin 。看到警告讯息是正常的,因 Server 没有正式的 SSL 签章, 按进阶然後点选「继续前往」便可以了。

vpn server

来到登入画面,输入刚才设定的管理员帐号 (openvpn) 和密码。看到 License 画面按 Agree 便可。

vpn server

看到 VPN Server 已经在运作中,基本上没有什麽设定要改,现在只需要为用户 (User) 创建帐号,用户帐号是给手机或电脑用作连接 VPN 之用,请在选单点选 User Management。

vpn server

再选 User Permissions,在 new username 输入帐号名称,然後按 More Setting 那个铅笔图案。

vpn server

设密码後拉到最底按 Save Setting 便完成了。

vpn server

最後按 Update Running Server 更新设定。

vpn server

第 5 步:在手机安装 OpenVPN App

现在可以在手机安装一个名为 OpenVPN 的 App,安装後打开 App,选 Access Server 进行设定。

vpn server

依次填入相关的设定,Access Server Hostname 填写 Server 的 IP Address,Title 会被自动填写,Port 漏空便可以了,最後填写用户的 Username 和 Password,填好後按右上角的 ADD 按钮。

vpn server

出现这个画面时按 Accept 便可。

vpn server

然後按允许,容许手机加入这个 VPN 设定。

vpn server

现在可以连接自己的 VPN server 了!

vpn server

第 6 步:测试

现在进行测试,到 查询我的 IP 工具 查看一下,发现手机正在使用 VPN Server 的 IP Address 连接互联网,位置在美国维珍尼亚州。

vpn server

利用通讯事务管理局 (OFCA) 的 APP 测试一下,速度算是十分不错了,只是网络延时慢一点,这是正常的结果,毕竟讯息在地球上跑了一圈再来到手机。

vpn server

由於启动 VPN 後所有手机的讯息都使用 VPN Tunnel 来传送,而刚才说过 AWS 会按数据使用量收费,如不需要使用 VPN 时可把 VPN 关掉,虽然收费极便宜,但小数怕长计。

进阶设定

PC 上安装 OpenVPN

除了手机之外,不同的操作系统 (如 Windows,MacOS,Linux) 都可安装 OpenVPN,让电脑也能透过 VPN 连接 Internet,可到 openvpn.net 网站下载安装档。

vpn server

购买 License

OpenVPN Server 免费版本只可同时容许 2 个装置连接,如要增加则需另外购买 License,但需留意多人使用的话 VPN Server 运算力可能需要提高。

vpn server

固定 IP

AWS 预设没有固定 IP,即 Server 关机後重开会获派不同的 IP,手机便要重新设定了。建议为 VPN Server 设固定 IP,详情可在 AWS 选单中按 Elastic IPs 了解一下。

vpn server

更改 Port

OpenVPN 预设使用 TCP 443 Port 及 UDP 1194 Port 作为连接埠,这也是 VPN Server 的预设连接埠。可是在地球上某些国度里,会特别注意这些连接埠,因此,如有需要的话,可把这预设设定稍作更改。要更改的地方有 2 个,首先在 OpenVPN Server 中点选 Configuration,然後是 Network Settings,更改 TCP Port nuber 和 UDP Port number。

vpn server

然後在 AWS 中的 Security Groups 更改 inbound rules。

vpn server

相關主題

发表回复

2021-07-22

Posted In: 其他技术 Others

Leave a Comment