2020年7月7日火曜日

WSL2にvpn接続してWindowsからDockerコンテナのIPでアクセスしてみた


WSL2で動いてるDockerコンテナに、WindowsからIPアドレス(172.17.0.3など)でアクセスしたい。
ポートを変えて localhost でアクセスするのが嫌だ。
コンテナたくさんあるので。

SoftetherVPN を使ってWindowsからWSL2にVPN接続すれば、WSL2のDockerコンテナにIPアドレスでアクセスできるのでは?

と思って、試してみました。

1. WSL2のDockerコンテナとしてSoftEtherVPNサーバを導入


1.1. コンテナ構築

Dockerfile を下記のように作成

SoftEtherVPNサーバをインストールして、起動します。

FROM centos:7
RUN yum -y install gcc make which net-tools
RUN cd /usr/local && \
    curl -L -o softether-vpnserver.tar.gz https://jp.softether-download.com/files/softether/v4.34-9745-rtm-2020.04.05-tree/Linux/SoftEther_VPN_Server/64bit_-_Intel_x64_or_AMD64/softether-vpnserver-v4.34-9745-rtm-2020.04.05-linux-x64-64bit.tar.gz && \
    tar xvfz softether-vpnserver.tar.gz
WORKDIR /usr/local/vpnserver
RUN make i_read_and_agree_the_license_agreement && \
    chmod 600 * && \
    chmod 700 vpncmd && \
    chmod 700 vpnserver
CMD ["/usr/local/vpnserver/vpnserver", "execsvc"]


Dockerfileと同じディレクトリにdocker-compose.yml を下記のように作成

コンテナは、host ネットワークを使用します。

version: '3'
services:
  server:
    build: .
    network_mode: host


"docker-compose build" でビルドして

"docker-compose up -d" でSoftetherVPNサーバを起動します。


1.2. SoftEtherVPNサーバの設定

"docker-compose exec server bash" を実行してコンテナに入ります。

check します.

[root@MyComputer vpnserver]# ./vpncmd localhost /tools
vpncmd command - SoftEther VPN Command Line Management Utility
SoftEther VPN Command Line Management Utility (vpncmd command)
Version 4.34 Build 9745   (English)
Compiled 2020/04/05 23:39:56 by buildsan at crosswin
Copyright (c) SoftEther VPN Project. All Rights Reserved.

VPN Tools has been launched. By inputting HELP, you can view a list of the commands that can be used.

VPN Tools>check
Check command - Check whether SoftEther VPN Operation is Possible
---------------------------------------------------
SoftEther VPN Operation Environment Check Tool

Copyright (c) SoftEther VPN Project.
All Rights Reserved.

If this operation environment check tool is run on a system and that system passes, it is most likely that SoftEther VPN software can operate on that system. This check may take a while. Please wait...

Checking 'Kernel System'...
              Pass
Checking 'Memory Operation System'...
              Pass
Checking 'ANSI / Unicode string processing system'...
              Pass
Checking 'File system'...
              Pass
Checking 'Thread processing system'...
              Pass
Checking 'Network system'...
              Pass

All checks passed. It is most likely that SoftEther VPN Server / Bridge can operate normally on this system.

The command completed successfully.

VPN Tools>exit
[root@MyComputer vpnserver]#


SoftEtherVPNサーバの設定をします。

[root@MyComputer vpnserver]# ./vpncmd localhost /server
vpncmd command - SoftEther VPN Command Line Management Utility
SoftEther VPN Command Line Management Utility (vpncmd command)
Version 4.34 Build 9745   (English)
Compiled 2020/04/05 23:39:56 by buildsan at crosswin
Copyright (c) SoftEther VPN Project. All Rights Reserved.

Connection has been established with VPN Server "localhost" (port 443).

You have administrator privileges for the entire VPN Server.

VPN Server>

HUBを指定してユーザを作成し、パスワードを設定します。

VPN Server>Hub DEFAULT
Hub command - Select Virtual Hub to Manage
The Virtual Hub "DEFAULT" has been selected.
The command completed successfully.

VPN Server/DEFAULT>UserCreate vpnuser /GROUP:none /REALNAME:none /NOTE:none
UserCreate command - Create User
The command completed successfully.

VPN Server/DEFAULT>UserPasswordSet vpnuser
UserPasswordSet command - Set Password Authentication for User Auth Type and Set Password
Please enter the password. To cancel press the Ctrl+D key.

Password: ********
Confirm input: ********


The command completed successfully.

VPN Server/DEFAULT>


IPSecとSecureNATを有効にします。

"vpnvpn"が、事前共有キーです。

VPN Server/DEFAULT>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): vpnvpn

Default Virtual HUB in a case of omitting the HUB on the Username: DEFAULT

The command completed successfully.

VPN Server/DEFAULT>SecureNatEnable
SecureNatEnable command - Enable the Virtual NAT and DHCP Server Function (SecureNat Function)
The command completed successfully.

VPN Server/DEFAULT>


コンテナからでたあと、下記コマンドで上記設定を保存します。

$ docker-compose exec server cat ./vpn_server.config > ./vpn_server.config

Dockerfile を修正して、保存した設定を使用するようにします。

下から2行目のCOPYを追加しました。

FROM centos:7
RUN yum -y install gcc make which net-tools
RUN cd /usr/local && \
    curl -L -o softether-vpnserver.tar.gz https://jp.softether-download.com/files/softether/v4.34-9745-rtm-2020.04.05-tree/Linux/SoftEther_VPN_Server/64bit_-_Intel_x64_or_AMD64/softether-vpnserver-v4.34-9745-rtm-2020.04.05-linux-x64-64bit.tar.gz && \
    tar xvfz softether-vpnserver.tar.gz
WORKDIR /usr/local/vpnserver
RUN make i_read_and_agree_the_license_agreement && \
    chmod 600 * && \
    chmod 700 vpncmd && \
    chmod 700 vpnserver
COPY vpn_server.config .
CMD ["/usr/local/vpnserver/vpnserver", "execsvc"]

docker-compose build でビルドして、down/up します。


2.Windows10のVPNクライアント設定

事前にWSL2のIPアドレスをしらべておきます。

ubuntu@MyComputer:~$ ip a show dev eth0
4: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:15:5d:7c:48:74 brd ff:ff:ff:ff:ff:ff
    inet 192.168.158.243/20 brd 192.168.159.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::215:5dff:fe7c:4874/64 scope link
       valid_lft forever preferred_lft forever


2.1.Windows標準のVPNクライアントの作成


Windows標準のVPNクライアントを使用します。


下図だとサーバ名をlocalhostにしてますが、 つながらないので、WSL2のIPアドレスを指定します。




2.2. アダプターの設定変更

※これは必須ではないかも。

上記2.1で作成したVPNクライアントのアダプター設定を変更します。


「セキュリティ」タブで下図のプロトコルを許可します。

「ネットワーク」タブで、ipv6を無効にして、ipv4のプロパティを開きます。


DNSサーバを設定します。8.8.8.8はGoogleのDNSサーバです。
詳細設定をクリック


"リモートネットワークでデフォルトゲートウェイを使用"のチェックを外します。


2.3.ルーティングテーブルの設定


windowsのルーティングテーブルを変更します。
まず、VPNのIFを調べます。
下記の例だと 63 です。
PS C:\workspace> route print -4
===========================================================================
インターフェイス一覧
  7...80 fa 5b 66 ef 15 ......Realtek PCIe GbE Family Controller
 58...00 15 5d 5a e5 2e ......Hyper-V Virtual Ethernet Adapter
 11...0a 00 27 00 00 0b ......VirtualBox Host-Only Ethernet Adapter
  5...0a 00 27 00 00 05 ......VirtualBox Host-Only Ethernet Adapter #2
 63...........................wsl2
 10...d0 c6 37 09 a7 23 ......Microsoft Wi-Fi Direct Virtual Adapter
 13...d2 c6 37 09 a7 22 ......Microsoft Wi-Fi Direct Virtual Adapter #2
  9...d0 c6 37 09 a7 22 ......Intel(R) Wireless-AC 9462
  1...........................Software Loopback Interface 1
===========================================================================

今回はWSL2のDockerコンテナが使用しているIPが172.19で始まるIPなので、
172.19で始まるIPアドレスは、上記で調べたVPNのIFを使うように設定を追加します。
下記のようにコマンドを実行します。
route add 172.19.0.0 mask 255.255.0.0 192.168.30.1 if 63
これは、一時的な変更なので、VPNの接続を切ると、消えます。

3. 動作確認


WSL2 でコンテナのIPを調べます
docker ps でコンテナIDを確認して、
docker inspect <コンテナID>を実行すると、下図のようにIPアドレスを確認できます。

Windowsのターミナルから、調べたコンテナのIP(172.19.0.2)にPINGして下図のように疎通したらOKです。