Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/components/DynmapView.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
// map.torosaba.net の地図の画面。Dynmap の web/index.html の代わり
// 地図のデータは API (app/lib/api.ts。toro-webmap が TiDB から返す)、見た目は Dynmap の css/ と images/ (public/) をそのまま使う
// 地図のデータは API (app/lib/api.ts。地図サーバーが TiDB から返す)、見た目は Dynmap の css/ と images/ (public/) をそのまま使う
import L from 'leaflet'
import 'leaflet/dist/leaflet.css'
import { computed, markRaw, onMounted, ref } from 'vue'
Expand Down
2 changes: 1 addition & 1 deletion client/dynmap-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Dynmap のブラウザ側 (web/js/*.js) を、見た目を変えずに軽くする。
// toro-edge が index.html の standalone/config.js の前にこのスクリプトを差し込む。leaflet.js, custommarker.js, dynmaputils.js などは読み終わっている。
// 入り口のプロキシが index.html の standalone/config.js の前にこのスクリプトを差し込む。leaflet.js, custommarker.js, dynmaputils.js などは読み終わっている。
//
// 1. ズームのたびに Dynmap は全マーカーを外して付け直す (markers.js の updateMarker)。外してすぐ付け直すものは DOM に触らない
// 2. 画面の外 (と周りに半画面ずつ) のマーカーは DOM を作らず、近づいたら作る
Expand Down
2 changes: 1 addition & 1 deletion client/sw.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference lib="webworker" />
// map.torosaba.net の Service Worker。public/sw.js に変換して /sw.js で返す (app/app.vue が登録する)
// ホーム画面に追加してアプリのように開けるようにするためのもの。地図のデータ (tiles/, up/ など) はキャッシュしない
// (変わり続けるうえに量が多い。toro-edge と Cloudflare がまとめている)
// (変わり続けるうえに量が多い。入り口のプロキシと Cloudflare がまとめている)
// - ページを開くとき: いつもネットから。つながらないときはオフラインのページ (public/pwa/offline.html)
// - /_nuxt/ の js と css: 名前に版が入っていて中身が変わらないので、一度読んだものはキャッシュから
// モジュールにして、self を Service Worker の型で宣言し直す
Expand Down
14 changes: 7 additions & 7 deletions docs/map-deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
- プレイヤーの位置は、Minecraft サーバーの TORO-Core の `dynmap-live` が、TORO-API の内部専用の受け口 (`/live/plugin`) へ送ります。TORO-API は、これと Dynmap の更新を合わせて、`/map/v1/ws` (WebSocket) で画面へ流します。内部専用の受け口は、公開の受け口とは別の番号で待ち受け、公開の入り口 (Tunnel) の対象に入れません。公開側では `/live/*` は 404 です。
- 送信側 (`dynmap-live`) の設定は、`main/plugins/TORO-Core/dynmap-live/config.yml` の `endpoint` (送り先) と `token` (共有の合言葉。TORO-API の `MAP_LIVE_TOKEN` と同じ値) です。

## 入り口の振り分け (toro-edge)
## 入り口の振り分け

公開の入り口 (toro-edge。自前のリバースプロキシで、リクエストを中の各サービスへ振り分けます) は、API 用のホストの `/map/v1/` を、環境変数 `EDGE_API_BACKEND` で決めた送り先へ渡します。
公開の入り口 (自前のリバースプロキシで、リクエストを中の各サービスへ振り分けます) は、API 用のホストの `/map/v1/` を、環境変数 `EDGE_API_BACKEND` で決めた送り先へ渡します。

- `EDGE_API_BACKEND` を設定すると (`ホスト名:番号` の形)、`/map/v1/` の通常の通信 (設定、ワールド、タイル) と WebSocket が、その送り先へ行きます。本番では TORO-API に設定しています。
- **未設定のときの既定値は、従来の送り先 (toro-webmap、環境変数 `EDGE_MAPWEB` の値) です。** つまり、変数を取り除くだけで従来の動きに戻ります。
- **未設定のときの既定値は、従来の送り先 (地図サーバー、環境変数 `EDGE_MAPWEB` の値) です。** つまり、変数を取り除くだけで従来の動きに戻ります。
- 地図の画面のサイトと 3D 地図のサイトの送り先は、この変数では変わりません。
- toro-edge は 1.4.0 以降でこの変数に対応しています。
- 入り口のプロキシは 1.4.0 以降でこの変数に対応しています。

## API の公開するパス

Expand Down Expand Up @@ -60,13 +60,13 @@ API は `/map/v1/` の下で、次の 4 種類だけを返します。

切り替えた後に重大な食い違いが出たときは、次の順に戻します。Minecraft サーバー本体は再起動しません。

1. toro-edge の設定から `EDGE_API_BACKEND` を取り除き、toro-edge だけを作り直します (`docker compose up -d --no-deps toro-edge`)。ほかのサービスは作り直しません。
2. `dynmap-live` の `endpoint` を従来の送り先 (toro-webmap の `/live/plugin`) に戻し、`/torocore reload dynmap-live` で再読み込みします。
1. 入り口のプロキシの設定から `EDGE_API_BACKEND` を取り除き、そのサービスだけを作り直します (`docker compose up -d --no-deps <入り口のサービス名>`)。ほかのサービスは作り直しません。
2. `dynmap-live` の `endpoint` を従来の送り先 (地図サーバーの `/live/plugin`) に戻し、`/torocore reload dynmap-live` で再読み込みします。
3. 確認: 公開の `/map/v1/configuration` が従来の応答 (200) に戻ること。存在しないワールドの 404 の本文が、JSON から従来の HTML に戻ることでも、従来の送り先に戻ったと判別できます。位置情報 (WebSocket の `players`) が流れることも確かめます。

## 許容している違い

従来の送り先 (toro-webmap) と TORO-API の応答には、次の違いがあります。いずれも地図の画面の動作には影響しないため、許容しています。
従来の送り先 (地図サーバー) と TORO-API の応答には、次の違いがあります。いずれも地図の画面の動作には影響しないため、許容しています。

従来から許容しているもの:

Expand Down
14 changes: 7 additions & 7 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// app/: map.torosaba.net の地図の画面 (Dynmap の web/index.html の代わり)。サーバーで描かず、ブラウザで描く
// server/routes/: 地図のデータを TiDB から返す API (Dynmap と BlueMap)。toro-edge から呼ばれる。BlueMap (3D 地図) の画面は TORO-3D-WebMap
// server/routes/: 地図のデータを TiDB から返す API (Dynmap と BlueMap)。入り口のプロキシから呼ばれる。BlueMap (3D 地図) の画面は TORO-3D-WebMap
export default defineNuxtConfig({
compatibilityDate: '2026-10-01',
ssr: false,
Expand Down Expand Up @@ -28,7 +28,7 @@ export default defineNuxtConfig({
},
// NUXT_DB_HOST などの環境変数で上書きする
db: {
host: 'tidb',
host: 'localhost',
port: 4000,
user: 'root',
password: '',
Expand All @@ -43,7 +43,7 @@ export default defineNuxtConfig({
database: 'bluemap',
prefix: 'bluemap_',
// BlueMap 内蔵の Web サーバー (ライブ情報)
live: 'http://toro-main:8100',
live: 'http://localhost:8100',
// BlueMap の地図ごとに、マーカーの一覧へ地点を足す Dynmap のワールド
dynmapWorlds: { overworld: 'main' },
},
Expand All @@ -65,8 +65,8 @@ export default defineNuxtConfig({
marumasign: {
enabled: true,
// 看板データ (marumasign/<地図>.json) と、地図の一覧 (settings.json) の読み場所。
// http(s):// なら HTTP で (Docker では toro-edge。Host は signDataHost)、ほかは BlueMap の web のフォルダ
signData: 'http://toro-edge',
// http(s):// なら HTTP で (Host は signDataHost。空なら看板データは読まない。NUXT_MARUMASIGN_SIGN_DATA で指定する)、ほかは BlueMap の web のフォルダ
signData: '',
signDataHost: '3d-map.torosaba.net',
// 地図の id。空なら settings.json の maps
maps: [] as string[],
Expand Down Expand Up @@ -99,8 +99,8 @@ export default defineNuxtConfig({
experimental: { websocket: true },
},
// npm run dev: Dynmap の画面の API は既定の apiBase (本番の API) を使う。css/ images/ は public/ にある。
// 手元の API を使うなら NUXT_PUBLIC_API_BASE=/map/v1 で手元のサーバーを指す (NUXT_DB_HOST などで DB につなぐ)。TiDB はホストに公開しておらず toro-db の内側だけなので、
// サーバーで tidb の IP を調べて (docker network inspect toro-db)、ssh -L 4000:<tidb の IP>:4000 でつなぐ
// 手元の API を使うなら NUXT_PUBLIC_API_BASE=/map/v1 で手元のサーバーを指す (NUXT_DB_HOST などで DB につなぐ)。TiDB はホストに公開していないことが多いので、
// サーバーで DB の IP を調べて、ssh -L 4000:<DB の IP>:4000 でつなぐ
$development: {
// 看板データは本番のもの (中継は手元のサーバーが行う)
runtimeConfig: {
Expand Down
2 changes: 1 addition & 1 deletion server/routes/_assets/[file].ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Readable } from 'node:stream'
import type { H3Event } from 'h3'
import type { HostedFile } from '../../utils/assets'

// assets.torosaba.net/<id>.<拡張子> (toro-edge が /_assets/ に付け替えて送ってくる)。MarumaSign Companion がホストした看板の画像など
// assets.torosaba.net/<id>.<拡張子> (入り口のプロキシが /_assets/ に付け替えて送ってくる)。MarumaSign Companion がホストした看板の画像など
// 読めるのは、次のどちらかだけ (ほかのサイトに貼られたり、ただの置き場として使われたりしないように)
// - いま TORO で遊んでいるプレイヤーの IP から (MarumaSign の Mod が看板を読む)。IP は Cloudflare の CF-Connecting-IP、
// 遊んでいる人の IP は TORO-Proxy (Velocity) が知らせてくる (server/utils/clients.ts)
Expand Down
2 changes: 1 addition & 1 deletion server/routes/live/clients.post.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { tokenMatches } from '../../lib/token'

// TORO-Proxy (Velocity) が、いまつながっているプレイヤーの IP を全員分送ってくる ({"ips": [...]})。toro-edge は外に出さない
// TORO-Proxy (Velocity) が、いまつながっているプレイヤーの IP を全員分送ってくる ({"ips": [...]})。入り口のプロキシは外に出さない
// 合言葉は X-Toro-Token (NUXT_CLIENTS_TOKEN)。空のときは受け付けない

const MAX_IPS = 2000
Expand Down
2 changes: 1 addition & 1 deletion server/routes/live/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { tokenMatches } from '../../lib/token'

// TORO-Core の dynmap-live (Minecraft サーバーの中) からの WebSocket。全員の位置と体力が 0.25 秒ごとに届く
// toro-edge はここを外に出さない (Docker の中のネットワークからだけ)。さらに NUXT_LIVE_TOKEN で確かめる
// 入り口のプロキシはここを外に出さない (Docker の中のネットワークからだけ)。さらに NUXT_LIVE_TOKEN で確かめる
export default defineWebSocketHandler({
upgrade(request) {
if (!tokenMatches(request.headers.get('x-toro-token'), useRuntimeConfig().liveToken)) {
Expand Down
2 changes: 1 addition & 1 deletion server/routes/maps/[map]/live/markers.json.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function bluemapMarkers(mapId: string): Promise<MarkerSets> {
if (res.ok) return await res.json() as MarkerSets
}
catch {
// toro-main の再起動中など。DB の最後の版にする
// ゲームサーバーの再起動中など。DB の最後の版にする
}
const found = await bluemapItem(mapId, 'bluemap:markers')
if (!found) return {}
Expand Down
2 changes: 1 addition & 1 deletion server/routes/mcassets/skin.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default defineEventHandler(async (event) => {
if (!uuid) throw createError({ statusCode: 400, statusMessage: 'bad uuid' })
query = { uuid }
}
// Cloudflare が付けるクライアントの IP (toro-edge には Tunnel からしか届かない)
// Cloudflare が付けるクライアントの IP (入り口のプロキシには Tunnel からしか届かない)
const ip = getRequestHeader(event, 'cf-connecting-ip') || getRequestIP(event, { xForwardedFor: true }) || 'unknown'
const r = await resolver.resolve(query, limitKey(ip))
if (r === 'rate' || r === 'busy') {
Expand Down
12 changes: 6 additions & 6 deletions server/utils/bluemap-live.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import type { Peer } from 'crossws'

// BlueMap (3d-map.torosaba.net) のライブ情報 (プレイヤーの位置、描き直したタイル、マーカーの変更) を、ブラウザへ WebSocket (/up/ws) で届ける中継
//
// - BlueMap 内蔵の Web サーバー (toro-main:8100) の live/sse を、見ている地図ごとに 1 本だけつなぎ、届いたイベントをその地図を見ているブラウザ全員へ送る
// (今までは、見ている人の数だけ toro-main へつないでいた)
// - BlueMap 内蔵の Web サーバー (ゲームサーバー上) の live/sse を、見ている地図ごとに 1 本だけつなぎ、届いたイベントをその地図を見ているブラウザ全員へ送る
// (今までは、見ている人の数だけ ゲームサーバーへつないでいた)
// - 送るのは {"type":"bluemap","map":"<地図>","event":"player|tile|marker","data":"<BlueMap が送った JSON の文字列そのまま>"}。
// 画面 (app/bluemap/js/BlueMapApp.ts) は、SSE のときと同じ処理でこれを受ける
// - BlueMap 本体とつながっているかは {"type":"bluemap","map":"<地図>","state":"open|error"} で知らせる。
// 画面は open のあいだ 1 秒ごとの取りに行きをやめ、error (toro-main の再起動中など) のあいだは取りに行く
// - つなぎっぱなしのとき WebSocket が黙らないよう (toro-edge は 120 秒何も流れないつなぎを切る)、30 秒ごとに {"type":"ping"} を送る
// 画面は open のあいだ 1 秒ごとの取りに行きをやめ、error (ゲームサーバーの再起動中など) のあいだは取りに行く
// - つなぎっぱなしのとき WebSocket が黙らないよう (入り口のプロキシは 120 秒何も流れないつなぎを切る)、30 秒ごとに {"type":"ping"} を送る

interface Upstream {
open: boolean
Expand Down Expand Up @@ -50,7 +50,7 @@ function setOpen(mapId: string, up: Upstream, open: boolean): void {
broadcast(mapId, stateText(mapId, open))
}

/** toro-main の live/sse を読み続ける。切れたら間を置いてつなぎ直す。見る人がいなくなる (up.stopped) まで */
/** ゲームサーバーの live/sse を読み続ける。切れたら間を置いてつなぎ直す。見る人がいなくなる (up.stopped) まで */
async function run(mapId: string, up: Upstream): Promise<void> {
const { bluemap } = useRuntimeConfig()
const url = `${bluemap.live}/maps/${encodeURIComponent(mapId)}/live/sse`
Expand All @@ -73,7 +73,7 @@ async function run(mapId: string, up: Upstream): Promise<void> {
}
}
catch {
// toro-main の再起動中など。つなぎ直す
// ゲームサーバーの再起動中など。つなぎ直す
}
if (up.stopped) return
setOpen(mapId, up, false)
Expand Down
4 changes: 2 additions & 2 deletions server/utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import mysql from 'mysql2/promise'
import type { Pool, RowDataPacket } from 'mysql2/promise'

const pools = new Map<string, Pool>()
// 接続を待つ数の上限 (いっぱいなら待たせずに失敗にする。toro-edge は古いものを返す)。toro-edge が同時に聞く数 (256 + 256) より多く
// 接続を待つ数の上限 (いっぱいなら待たせずに失敗にする。入り口のプロキシは古いものを返す)。入り口のプロキシが同時に聞く数 (256 + 256) より多く
const QUEUE_LIMIT = 1024
// 1 つの問い合わせの時間 (toro-edge が返事を待つ長さと同じ)
// 1 つの問い合わせの時間 (入り口のプロキシが返事を待つ長さと同じ)
const QUERY_TIMEOUT_MILLIS = 30_000

/** DB ごとの接続プール。TiDB は 1 台なので、地図の配信には 16 本あれば足りる */
Expand Down
2 changes: 1 addition & 1 deletion server/utils/skin-lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class SkinResolver {
try {
const res = await this.fetcher(url, {
redirect: 'error', credentials: 'omit', signal: AbortSignal.timeout(TIMEOUT_MILLIS),
headers: { 'accept': 'application/json', 'user-agent': 'toro-webmap (3d-map.torosaba.net)' },
headers: { 'accept': 'application/json', 'user-agent': 'webmap (3d-map.torosaba.net)' },
})
if (res.status === 204 || res.status === 404) return null
if (!res.ok) throw new Error('HTTP ' + res.status)
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/self-signed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const utcTime = (d: Date): Buffer => der(0x17, Buffer.from(d.toISOString().repla
const pem = (label: string, data: Buffer): string =>
`-----BEGIN ${label}-----\n${data.toString('base64').replace(/(.{64})/g, '$1\n').replace(/\n$/, '')}\n-----END ${label}-----\n`

export function selfSignedPair(commonName = 'toro-webmap.test'): { cert: string, key: string } {
export function selfSignedPair(commonName = 'internal-host.test'): { cert: string, key: string } {
const { publicKey, privateKey } = generateKeyPairSync('rsa', { modulusLength: 2048 })
const name = der(0x30, der(0x31, der(0x30, oid(0x55, 0x04, 0x03), der(0x0c, Buffer.from(commonName)))))
const serial = randomBytes(8)
Expand Down
2 changes: 1 addition & 1 deletion tests/marumasign-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ test('読んでよい URL の形 (https だけ、怪しいホストとポート
assert.equal(checkUrl('https://example.com:8443/a'), 'https://example.com:8443/a')
for (const bad of [
'http://example.com/a', 'https://127.0.0.1/a', 'https://[::1]/a', 'https://0x7f.1/a', 'https://localhost/a', 'https://a.local/a',
'https://tidb./a', 'https://toro-edge/a', 'https://user:pw@example.com/a', 'https://example.com:22/a', 'https://example.com:3000/a',
'https://internal-db./a', 'https://internal-host/a', 'https://user:pw@example.com/a', 'https://example.com:22/a', 'https://example.com:3000/a',
'ftp://example.com/a', 'javascript:alert(1)', 'https://example.com/<x>', 'https://a..b.com/x',
]) {
assert.equal(checkUrl(bad), null, bad)
Expand Down
4 changes: 2 additions & 2 deletions tools/dynmap-zoomout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// 使い方 (このリポジトリで): node --experimental-strip-types tools/dynmap-zoomout.ts <world> <map の prefix> <いちばん縮小した段> [--check]
// 例: node --experimental-strip-types tools/dynmap-zoomout.ts main flat 10
// --check: 書き込まず、既にある段を作り直して Dynmap の作ったものと比べる (並べる向きの確認)
// 接続先は環境変数 DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_NAME (既定は tidb:4000 の root、dynmap)
// 接続先は環境変数 DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_NAME (既定は localhost:4000 の root、dynmap)

import mysql from 'mysql2/promise'
import type { RowDataPacket } from 'mysql2/promise'
Expand All @@ -23,7 +23,7 @@ if (!world || !prefix || !maxArg) {
const maxZoom = Number(maxArg)

const db = await mysql.createConnection({
host: process.env.DB_HOST ?? 'tidb',
host: process.env.DB_HOST ?? 'localhost',
port: Number(process.env.DB_PORT ?? 4000),
user: process.env.DB_USER ?? 'root',
password: process.env.DB_PASSWORD ?? '',
Expand Down
Loading