kawabatas技術ブログ

試してみたことを書いていきます

GCE 上に VPN サーバーを立てる

概要

GCE インスタンスVPN サーバーを立ててみたのでメモ。

そして、iPhoneVPNを設定してみた。

背景

iPhonewifi 回線ではプロキシを経由できるが、4G 回線ではプロキシを経由する方法を見つけられなかった。

そして iPhoneVPNプロトコルによっては「設定」->「一般」->「VPN」で設定できることがわかった。

また、現在、Cloud VPN がサポートしているのは、ゲートウェイ間のみで、クライアント - ゲートウェイ間は満たせない。

なので、GCE インスタンスVPN サーバーを立てて、そこからプロキシを経由することに挑戦した。

VPN ソフトウェア

メジャーと思われるのはこちら。

また、VPN プロトコルにはいくつか種類があり、L2TP/IPSeciPhone / Android でネイティブでサポートされており、OpenVPNサードパーティのアプリを使用しなければならないらしい。

よって、L2TP/IPSecGithubの活発さより、SoftEther VPN を選択する。

SoftEther VPN

公式サイト

正式にサポートしている OS は「Windows 2000」以降と「Linux」らしい。https://ja.softether.org/4-docs/1-manual/7/7.1

一旦、正式にサポートしている RHEL とほぼ同じ CentOS7 で試してみる。GCE の OS image

GCE インスタンス作成

terraform で生成したので、そのコードを。gcloud コマンドで同様に作成すれば良い。

resource "google_compute_instance" "vpn" {
  name         = "vpn-test"
  machine_type = "f1-micro"
  zone         = "asia-northeast1-a"
   boot_disk {
    initialize_params {
      image = "centos-cloud/centos-7"
    }
  }
   network_interface {
    network = "default"
     access_config {
      // Ephemeral IP - leaving this block empty will generate a new external IP and assign it to the machine
    }
  }
   tags = ["vpn"]
   can_ip_forward = true
}

SoftEther インストール

公式サイト手順

参考:AWSで構築した手順

必要なソフトウェアおよびライブラリの確認

$ cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[kawabata@vpn-test ~]$ yum list installed | grep -E 'gcc|binutils|glibc|zlib|openssl|readline|ncurses|pthread'
binutils.x86_64                       2.27-28.base.el7_5.1            @updates
glibc.x86_64                          2.17-222.el7                    @anaconda
glibc-common.x86_64                   2.17-222.el7                    @anaconda
libgcc.x86_64                         4.8.5-28.el7_5.1                @updates
ncurses.x86_64                        5.9-14.20130511.el7_4           @anaconda
ncurses-base.noarch                   5.9-14.20130511.el7_4           @anaconda
ncurses-libs.x86_64                   5.9-14.20130511.el7_4           @anaconda
openssl.x86_64                        1:1.0.2k-12.el7                 @anaconda
openssl-libs.x86_64                   1:1.0.2k-12.el7                 @anaconda
readline.x86_64                       6.2-10.el7                      @anaconda
zlib.x86_64                           1.2.7-17.el7                    @anaconda
$ sudo yum -y update

gcc がないのでインストール

$ sudo yum -y install gcc

パッケージの解凍

ソースをダウンロード、解凍、ビルド。

curl -L -O https://github.com/SoftEtherVPN/SoftEtherVPN/releases/download/v4.24-9651-beta/softether-vpnserver-v4.24-9651-beta-2017.10.23-linux-x64-64bit.tar.gz
tar xzvf softether-vpnserver-v4.24-9651-beta-2017.10.23-linux-x64-64bit.tar.gz

実行可能ファイルの生成

cd vpnserver
yes 1 | make

VPN Server の配置

cd ~
sudo mv vpnserver /usr/local
cd /usr/local/vpnserver/
sudo chmod 600 *
sudo chmod 700 vpncmd
sudo chmod 700 vpnserver
sudo chown -R root:root *

vpncmd の check コマンドによる動作チェック

確認のみ。略

スタートアップスクリプトへの登録

sudo vi /etc/init.d/vpnserver

ドキュメントのまま

#!/bin/sh
# chkconfig: 2345 99 01
# description: SoftEther VPN Server
DAEMON=/usr/local/vpnserver/vpnserver
LOCK=/var/lock/subsys/vpnserver
test -x $DAEMON || exit 0
case "$1" in
start)
$DAEMON start
touch $LOCK
;;
stop)
$DAEMON stop
rm $LOCK
;;
restart)
$DAEMON stop
sleep 3
$DAEMON start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0
sudo chmod 755 /etc/init.d/vpnserver
sudo /sbin/chkconfig --add vpnserver

サービスの開始と停止

sudo /etc/init.d/vpnserver start

SoftEther 設定

参考になった記事

管理者パスワードの変更

sudo /usr/local/vpnserver/vpncmd

1を入力。まだ仮想HUBを作成していないので、2つとも空のままで Enter。

test という仮想HUB を作成し、ユーザを作成する

VPN Server> HubCreate test
VPN Server>HUB test
VPN Server/test>UserCreate

ユーザ認証の種類

パスワードにする。

VPN Server/test>UserPasswordSet

L2TP/IPsecVPNにする

VPN Server/test>IPsecEnable
IPsecEnable command - Enable or Disable IPsec VPN Server Function
Enable L2TP over IPsec Server Function (yes / no): yes

Enable Raw L2TP Server Function (yes / no): no

Enable EtherIP / L2TPv3 over IPsec Server Function (yes / no): no

Pre Shared Key for IPsec (Recommended: 9 letters at maximum): シークレット

Default Virtual HUB in a case of omitting the HUB on the Username: ユーザ名

The command completed successfully.

GCP ファイアウォールルールを追加

UDP 500, UDP 4500 を開ける。

terraform で用意したのでそのコードを。

resource "google_compute_firewall" "vpn" {
  name    = "allow-vpn"
  network = "default"
   allow {
    protocol = "udp"
    ports    = ["500", "4500"]
  }
   target_tags = ["vpn"]
}

仮想 NAT および DHCP サーバー機能 (SecureNAT 機能) の有効化

仮想 NAT および DHCP サーバー機能 (SecureNAT 機能) の有効化」をする必要があった

必要だった

VPN Server/test>SecureNatEnable

確認

iPhone からのコネクションのログは下記で確認できる

$ sudo tail -f /usr/local/vpnserver/server_log/vpn_20180803.log

iPhoneVPN 設定

「設定」->「一般」->「VPN

  • Type: L2TP
  • Server: GCE インスタンスのIP
  • Account: ユーザ名@仮想HUB名
  • Password: ユーザのパスワード
  • Secret: シークレット