ngrokで自宅マイクラサーバーを公開してみる

f:id:tworks55:20220227160819p:plain

WSL2にDockerを入れたので、マイクラサーバーのDockerイメージを使い自宅サーバーを立てて外部に公開してみました。 ngrokを初めて使ったのは2016年でMicrosoftBot frameworkの検証をする為でしたが、あれから5年以上も経っているとは時が経つのも早いなと。。。

ということでまずはマイクラサーバーを立てる所からやっていきます。 使用するDocker imageはitzg/minecraft-serverです。

使い方はこちらに。

1. イメージをダウンロードして起動
> docker run -d -it -p 25565:25565 -e EULA=TRUE itzg/minecraft-server

f:id:tworks55:20220227162023p:plain

コンテナが起動しているか確認し、名前も確認します。以下でnervous_jangとなっている部分が名前です。

> docker ps
CONTAINER ID   IMAGE                   COMMAND    CREATED              STATUS                                 PORTS                                                      NAMES
30c39684bf80   itzg/minecraft-server   "/start"   About a minute ago   Up About a minute (health: starting)   0.0.0.0:25565->25565/tcp, :::25565->25565/tcp, 25575/tcp   nervous_jang
2. 起動ログを確認

上記で確認した名前nervous_jangを指定します。

> sudo docker logs nervous_jang

tworks55@LAPTOP-A1R0H57F:~$ sudo docker logs -f nervous_jang
[init] Running as uid=1000 gid=1000 with /data as 'drwxrwxr-x 2 1000 1000 4096 Feb 24 18:28 /data'
[init] Resolved version given LATEST into 1.18.1
[init] Resolving type given VANILLA
[init] Locating download for minecraft_server.1.18.1.jar ...
[init] Creating server.properties in /data/server.properties
[init] Disabling whitelist functionality
[init] Setting white-list to 'false' in /data/server.properties
[init] WARNING: whitelist enabled but not enforced. Set ENFORCE_WHITELIST=TRUE or update 'enforce-whitelist' in server.properties to enforce the whitelist.
[init] Setting enable-rcon to 'true' in /data/server.properties
[init] Setting rcon.password to 'minecraft' in /data/server.properties
[init] Setting rcon.port to '25575' in /data/server.properties
[init] Setting motd to 'A Vanilla Minecraft Server powered by Docker' in /data/server.properties
[init] Checking for JSON files.
[init] Setting initial memory to 1G and max to 1G
[init] Starting the Minecraft server...
Unpacking 1.18.1/server-1.18.1.jar (versions:1.18.1) to versions/1.18.1/server-1.18.1.jar
Unpacking com/github/oshi/oshi-core/5.8.2/oshi-core-5.8.2.jar (libraries:com.github.oshi:oshi-core:5.8.2) to libraries/com/github/oshi/oshi-core/5.8.2/oshi-core-5.8.2.jar
<中略>
Unpacking org/slf4j/slf4j-api/1.8.0-beta4/slf4j-api-1.8.0-beta4.jar (libraries:org.slf4j:slf4j-api:1.8.0-beta4) to libraries/org/slf4j/slf4j-api/1.8.0-beta4/slf4j-api-1.8.0-beta4.jar
Starting net.minecraft.server.Main
[07:19:55] [ServerMain/INFO]: Environment: authHost='https://authserver.mojang.com', accountsHost='https://api.mojang.com', sessionHost='https://sessionserver.mojang.com', servicesHost='https://api.minecraftservices.com', name='PROD'
[07:19:56] [ServerMain/WARN]: Ambiguity between arguments [teleport, location] and [teleport, destination] with inputs: [0.1 -0.5 .9, 0 0 0]
[07:19:56] [ServerMain/WARN]: Ambiguity between arguments [teleport, location] and [teleport, targets] with inputs: [0.1 -0.5 .9, 0 0 0]
[07:19:56] [ServerMain/WARN]: Ambiguity between arguments [teleport, destination] and [teleport, targets] with inputs: [Player, 0123, @e, dd12be42-52a9-4a91-a8a1-11c01849e498]
[07:19:56] [ServerMain/WARN]: Ambiguity between arguments [teleport, targets] and [teleport, destination] with inputs: [Player, 0123, dd12be42-52a9-4a91-a8a1-11c01849e498]
[07:19:56] [ServerMain/WARN]: Ambiguity between arguments [teleport, targets, location] and [teleport, targets, destination] with inputs: [0.1 -0.5 .9, 0 0 0]
[07:19:56] [ServerMain/INFO]: Reloading ResourceManager: Default
[07:19:58] [Worker-Main-13/INFO]: Loaded 7 recipes
[07:19:59] [Worker-Main-13/INFO]: Loaded 1141 advancements
[07:20:04] [Server thread/INFO]: Starting minecraft server version 1.18.1
[07:20:04] [Server thread/INFO]: Loading properties
[07:20:04] [Server thread/INFO]: Default game type: SURVIVAL
[07:20:04] [Server thread/INFO]: Generating keypair
[07:20:04] [Server thread/INFO]: Starting Minecraft server on *:25565
[07:20:04] [Server thread/INFO]: Using epoll channel type
[07:20:05] [Server thread/INFO]: Preparing level "world"
[07:20:26] [Server thread/INFO]: Preparing start region for dimension minecraft:overworld
[07:20:29] [Worker-Main-9/INFO]: Preparing spawn area: 0%
[07:20:29] [Worker-Main-9/INFO]: Preparing spawn area: 0%
[07:20:29] [Worker-Main-14/INFO]: Preparing spawn area: 0%
[07:20:29] [Worker-Main-12/INFO]: Preparing spawn area: 0%
<中略>
[07:21:46] [Worker-Main-13/INFO]: Preparing spawn area: 97%
[07:21:46] [Server thread/INFO]: Time elapsed: 80396 ms
[07:21:46] [Server thread/INFO]: Done (101.180s)! For help, type "help"
[07:21:46] [Server thread/INFO]: Starting remote control listener
[07:21:46] [Server thread/INFO]: Thread RCON Listener started
[07:21:46] [Server thread/INFO]: RCON running on 0.0.0.0:25575
[07:27:04] [Server thread/WARN]: handleDisconnection() called twice
3. ngrokをインストール

aptを使ってインストールしましたが、手順はこちらに記載されています。

> curl -s https://ngrok-agent.s3.amazonaws.com/ngrok.asc | sudo tee /etc/apt/trusted.gpg.d/ngrok.asc >/dev/null &&
              echo "deb https://ngrok-agent.s3.amazonaws.com buster main" | sudo tee /etc/apt/sources.list.d/ngrok.list &&
              sudo apt update && sudo apt install ngrok   
Hit:1 https://download.docker.com/linux/ubuntu focal InRelease
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:3 https://ngrok-agent.s3.amazonaws.com buster InRelease [7470 B]
Hit:4 http://archive.ubuntu.com/ubuntu focal InRelease
Get:5 https://ngrok-agent.s3.amazonaws.com buster/main amd64 Packages [661 B]
Get:6 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:7 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Fetched 344 kB in 3s (107 kB/s)
Reading package lists... Done
<中略>
Selecting previously unselected package ngrok.
(Reading database ... 64612 files and directories currently installed.)
Preparing to unpack .../ngrok_2.3.40_amd64.deb ...
Unpacking ngrok (2.3.40) ...
Setting up ngrok (2.3.40) ...
tworks55@LAPTOP-A1R0H57F:~$ ngrok -v
ngrok version 2.3.40     
4 ngrokを起動

イクラサーバーのポートを指定してngrokを起動しますがauthtokenがないとエラーになります。

tworks55@LAPTOP-A1R0H57F:~$ ngrok tcp 25565
TCP tunnels are only available after you sign up.
Sign up at: https://dashboard.ngrok.com/signup

If you have already signed up, make sure your authtoken is installed.
Your authtoken is available on your dashboard: https://dashboard.ngrok.com/get-started/your-authtoken

ERR_NGROK_302

https://dashboard.ngrok.com/signupにサインアップまたはログインし、authtokenを取得して設定します。

f:id:tworks55:20220227163730p:plain

> ngrok authtoken 25gSiQRFfEc****************************************
Authtoken saved to configuration file: /home/tworks55/.ngrok2/ngrok.yml

* authtokenは誰にも教えないようにする必要がありますのでマスキングしています。

再度ngrok を起動します。

> ngrok tcp 25565

f:id:tworks55:20220227164322p:plain

tcp://0.tcp.ngrok.io:19456が外部からアクセスできるURLとポート番号です。これをマイクラのクライアントに指定します。

5. マイクラのクライアントからログイン

イクラを立ち上げ、「マルチプレイ」を選択します。

f:id:tworks55:20220227164528p:plain

次に「サーバーを追加」を選択します。 f:id:tworks55:20220227164730p:plain

サーバーアドレス部分にngrokから発行された0.tcp.ngrok.io:19456を入力して「完了」を押します。

* 注意 tcp://は入力不要です f:id:tworks55:20220227165044p:plain

接続がうまくいくとpowered by DockerのVanillaサーバーが表示されます。 f:id:tworks55:20220227165628p:plain

「サーバーに接続」を選択します。

サーバー側のログを見るとログインに成功していることがわかります。

> docker logs -f nervous_jang
[07:57:27] [User Authenticator #3/INFO]: UUID of player mycrid is 6d2cdf21-15f5-47de-9523-a7dd145380f9
[07:57:27] [Server thread/INFO]: mycrid [/172.17.0.1:50352] logged in with entity id 513 at (-5.5, 75.0, -0.5)
[07:57:27] [Server thread/INFO]: mycrid joined the game

クライアント側もVanillaサーバーにログインして遊べるので、接続は成功です。 f:id:tworks55:20220227170059p:plain

6. Whitelist Playersの設定

以下2つの環境変数を設定して起動することでWhitelist されたユーザーだけがアクセスできるようになります。

ENFORCE_WHITELIST=TRUE
WHITELIST=xxxxx

以下のように起動

docker run -d -it -p 25565:25565 -e EULA=TRUE -e ENFORCE_WHITELIST=TRUE -e WHITELIST=myname itzg/minecraft-server

* myname は実際のユーザー名(プロフィール名)を設定します。

Whitelist されていないユーザーがアクセスすると以下のようなメッセージが表示されます。

f:id:tworks55:20220227175353p:plain

サーバー側もYou are not white-listed on this server!とログに記載されています。

[08:49:40] [User Authenticator #3/INFO]: UUID of player mycrid is 6d2cdf21-15f5-47de-9523-a7dd145380f9
[08:49:40] [Server thread/INFO]: Disconnecting com.mojang.authlib.GameProfile@6dda785e[id=6d2cdf21-15f5-47de-9523-a7dd145380f9,name=mycrid,properties={textures=[com.mojang.authlib.properties.Property@10b154db]},legacy=false] (/172.17.0.1:50646): You are not white-listed on this server!

このままだとDDOSアタック対策などのセキュリティ対策が不十分なため、本格運用には向いていません。 ちょっと身内で遊ぶくらいなら良いかもしれませんが自己責任でということになります。

ひとまず私の場合は実験用ということで全てShutdownして終了とします。