mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-19 03:25:56 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fa759f862e |
No files matched your search
@@ -1,5 +0,0 @@
|
||||
target
|
||||
**/target
|
||||
node_modules
|
||||
dist
|
||||
.git
|
||||
@@ -6,7 +6,7 @@ on:
|
||||
pull_request:
|
||||
paths: [tools/sync_history.py, tools/test_sync_history.py, changelog/history.json, .github/workflows/changelog.yml]
|
||||
release:
|
||||
types: [published, prereleased, released]
|
||||
types: [published, released]
|
||||
workflow_run:
|
||||
workflows: [release]
|
||||
types: [completed]
|
||||
|
||||
@@ -6,114 +6,33 @@ on:
|
||||
push:
|
||||
tags: ['v*']
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
publish_release:
|
||||
description: Publish the GitHub release (leave off to prepare signed artifacts)
|
||||
type: boolean
|
||||
default: false
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
release-intent:
|
||||
runs-on: ubuntu-latest
|
||||
outputs:
|
||||
build: ${{ steps.intent.outputs.build }}
|
||||
steps:
|
||||
- id: intent
|
||||
name: Keep already published release assets immutable
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
REQUEST_PUBLISH: ${{ inputs.publish_release && 'true' || 'false' }}
|
||||
run: |
|
||||
python3 - <<'PY'
|
||||
import json, os, subprocess
|
||||
build = True
|
||||
if os.environ['GITHUB_EVENT_NAME'] == 'push' or os.environ.get('REQUEST_PUBLISH') == 'true':
|
||||
result = subprocess.run(['gh', 'api', 'repos/' + os.environ['GITHUB_REPOSITORY'] + '/releases/tags/' + os.environ['GITHUB_REF_NAME']], capture_output=True, text=True)
|
||||
if result.returncode == 0:
|
||||
build = json.loads(result.stdout)['draft']
|
||||
if not build:
|
||||
print('Published release already exists; preserve its assets and prerelease status.')
|
||||
elif 'HTTP 404' not in result.stderr:
|
||||
raise RuntimeError('Could not establish whether this release already exists: ' + result.stderr)
|
||||
with open(os.environ['GITHUB_OUTPUT'], 'a') as output:
|
||||
output.write('build=' + str(build).lower() + '\n')
|
||||
PY
|
||||
installer:
|
||||
needs: release-intent
|
||||
if: needs.release-intent.outputs.build == 'true'
|
||||
runs-on: windows-latest
|
||||
timeout-minutes: 60
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
- name: Validate release version
|
||||
shell: pwsh
|
||||
run: |
|
||||
$version = (Get-Content package.json -Raw | ConvertFrom-Json).version
|
||||
if ($env:GITHUB_EVENT_NAME -eq 'push' -and $env:GITHUB_REF_NAME -ne "v$version") {
|
||||
throw 'The release tag does not match the application version.'
|
||||
}
|
||||
- name: Cache Rust dependencies and compiled targets
|
||||
uses: actions/cache@v6
|
||||
continue-on-error: true
|
||||
with:
|
||||
path: |
|
||||
~/.cargo/registry
|
||||
~/.cargo/git
|
||||
target
|
||||
key: manager-installer-${{ runner.os }}-${{ hashFiles('Cargo.lock') }}-${{ github.run_id }}
|
||||
restore-keys: manager-installer-${{ runner.os }}-${{ hashFiles('Cargo.lock') }}-
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 22
|
||||
- run: npm ci
|
||||
- name: Tests
|
||||
run: cargo test --workspace -- --test-threads=1
|
||||
- name: Build and sign (publish only when requested)
|
||||
id: tauri
|
||||
- name: Build, sign and publish
|
||||
uses: tauri-apps/tauri-action@v0
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ (github.event_name == 'push' || inputs.publish_release) && github.token || '' }}
|
||||
GITHUB_TOKEN: ${{ github.token }}
|
||||
TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
|
||||
TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
|
||||
with:
|
||||
tagName: ${{ (github.event_name == 'push' || inputs.publish_release) && 'v__VERSION__' || '' }}
|
||||
releaseName: CoA Server Manager v__VERSION__
|
||||
tagName: ${{ github.ref_name }}
|
||||
releaseName: CoA Server Manager ${{ github.ref_name }}
|
||||
releaseBody: See the assets to download the installer. Existing installations update themselves.
|
||||
releaseDraft: false
|
||||
prerelease: false
|
||||
args: --bundles nsis
|
||||
includeUpdaterJson: true
|
||||
- name: Preserve the exact signed installer and updater metadata
|
||||
shell: pwsh
|
||||
env:
|
||||
MANAGER_ARTIFACTS: ${{ steps.tauri.outputs.artifactPaths }}
|
||||
run: |
|
||||
$paths = @($env:MANAGER_ARTIFACTS | ConvertFrom-Json)
|
||||
$installers = @($paths | Where-Object { $_ -like '*-setup.exe' })
|
||||
if ($installers.Count -ne 1) { throw 'Expected exactly one Windows NSIS installer.' }
|
||||
$installer = [IO.Path]::GetFullPath($installers[0])
|
||||
$workspace = [IO.Path]::GetFullPath((Get-Location).Path).TrimEnd('\') + '\'
|
||||
if (-not $installer.StartsWith($workspace, [StringComparison]::OrdinalIgnoreCase)) { throw 'Installer escaped the workspace.' }
|
||||
if (-not (Test-Path -LiteralPath "$installer.sig")) { throw 'The installer has no updater signature.' }
|
||||
$version = (Get-Content package.json -Raw | ConvertFrom-Json).version
|
||||
$name = [IO.Path]::GetFileName($installer).Replace(' ', '.')
|
||||
New-Item -ItemType Directory -Force _prepared-manager | Out-Null
|
||||
Copy-Item -LiteralPath $installer -Destination "_prepared-manager/$name"
|
||||
Copy-Item -LiteralPath "$installer.sig" -Destination "_prepared-manager/$name.sig"
|
||||
$platform = @{
|
||||
signature = (Get-Content -LiteralPath "$installer.sig" -Raw).Trim()
|
||||
url = "https://github.com/$env:GITHUB_REPOSITORY/releases/download/v$version/$([Uri]::EscapeDataString($name))"
|
||||
}
|
||||
@{ version = $version; notes = 'See the release notes.'; pub_date = [DateTime]::UtcNow.ToString('yyyy-MM-ddTHH:mm:ssZ'); platforms = @{ 'windows-x86_64' = $platform } } |
|
||||
ConvertTo-Json -Depth 8 | Set-Content _prepared-manager/latest.json -Encoding utf8NoBOM
|
||||
@{ schema = 1; version = $version; source = $env:GITHUB_SHA; run = $env:GITHUB_RUN_ID; installer = $name; sha256 = (Get-FileHash -LiteralPath $installer -Algorithm SHA256).Hash.ToLowerInvariant() } |
|
||||
ConvertTo-Json | Set-Content _prepared-manager/prepared-source.json -Encoding utf8NoBOM
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: prepared-manager-windows
|
||||
path: _prepared-manager/*
|
||||
if-no-files-found: error
|
||||
retention-days: 14
|
||||
@@ -4,8 +4,5 @@ dist
|
||||
*.log
|
||||
.env
|
||||
src-tauri/gen
|
||||
|
||||
# standalone service workspaces build into their own target folders
|
||||
services/*/target/
|
||||
# Disposable database fixtures and private recovery data
|
||||
.cache/
|
||||
@@ -31,10 +31,6 @@ Optional: one short paragraph in plain English for the reader (at most 300 chara
|
||||
|
||||
## Shared build and release workflow
|
||||
|
||||
Every published Manager version, including a clearly marked Pre-release, belongs in this repository's
|
||||
CHANGELOG.md and changelog/history.json. Its GitHub release notes use the same reviewed record.
|
||||
Promotion updates that entry without duplicating it; Manager and server histories remain separate.
|
||||
|
||||
Before preparing or publishing a Manager/server release, read the maintained
|
||||
[release runbook](https://github.com/Corfirean/coa-server-build/blob/main/docs/server-release.md)
|
||||
and [build agent instructions](https://github.com/Corfirean/coa-server-build/blob/main/AGENTS.md).
|
||||
|
||||
@@ -4,29 +4,8 @@ Manager releases only. Updating the Manager does not update the server.
|
||||
|
||||
[Server changelog](https://github.com/Corfirean/coa-server-build/blob/main/CHANGELOG.md)
|
||||
|
||||
Published releases and previews. Pre-release entries do not change the stable update channel.
|
||||
|
||||
Dates follow GitHub publication (UTC); legacy records retain their archived dates. Newest releases first.
|
||||
|
||||
## 0.7.0 — 2026-10-21
|
||||
|
||||
Portable transfers into Warcraft Reborn are not supported yet.
|
||||
|
||||
**Portable Characters are experimental and may contain bugs.**
|
||||
|
||||
- Keep portable characters and their saved versions in the Manager, and use them on compatible realms.
|
||||
- Make an existing character portable from a connected realm.
|
||||
- Browse public realms and see their connection details.
|
||||
- Link an existing realm account or create one on hosts that allow it.
|
||||
- Switch between CoA, Wildcard and Warcraft Reborn realm profiles on supported server builds.
|
||||
- Adjust creature health for different locations and enemy types in Content Scaling.
|
||||
- Switch Custom Races patches faster, with visible feedback while changes are applied.
|
||||
- Handle Windows requests for administrator permission when launching the game.
|
||||
|
||||
[Release](https://github.com/Corfirean/coa-server-manager/releases/tag/v0.7.0) · [Source changes](https://github.com/Corfirean/coa-server-manager/compare/v0.6.22...v0.7.0)
|
||||
|
||||
[Player guides and screenshots](https://github.com/Corfirean/coa-server-manager/tree/d22ff09ac6fd7628aac14d90ec23f547cd9b4bf1/docs)
|
||||
|
||||
## 0.6.22 — 2026-10-19
|
||||
|
||||
- Updated Custom Races client patches to 1.8.
|
||||
|
||||
Generated
+6
-376
@@ -8,41 +8,6 @@ version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
|
||||
|
||||
[[package]]
|
||||
name = "aead"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0"
|
||||
dependencies = [
|
||||
"crypto-common",
|
||||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aes"
|
||||
version = "0.8.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"cipher",
|
||||
"cpufeatures",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aes-gcm"
|
||||
version = "0.10.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "831010a0f742e1209b3bcea8fab6a8e149051ba6099432c8cb2cc117dec3ead1"
|
||||
dependencies = [
|
||||
"aead",
|
||||
"aes",
|
||||
"cipher",
|
||||
"ctr",
|
||||
"ghash",
|
||||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.5"
|
||||
@@ -311,15 +276,6 @@ dependencies = [
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "blake2"
|
||||
version = "0.10.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe"
|
||||
dependencies = [
|
||||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "block-buffer"
|
||||
version = "0.10.4"
|
||||
@@ -521,30 +477,6 @@ version = "1.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600"
|
||||
|
||||
[[package]]
|
||||
name = "chacha20"
|
||||
version = "0.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c3613f74bd2eac03dad61bd53dbe620703d4371614fe0bc3b9f04dd36fe4e818"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"cipher",
|
||||
"cpufeatures",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "chacha20poly1305"
|
||||
version = "0.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "10cd79432192d1c0f4e1a0fef9527696cc039165d729fb41b3f4f4f354c2dc35"
|
||||
dependencies = [
|
||||
"aead",
|
||||
"chacha20",
|
||||
"cipher",
|
||||
"poly1305",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.45"
|
||||
@@ -557,51 +489,20 @@ dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cipher"
|
||||
version = "0.4.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad"
|
||||
dependencies = [
|
||||
"crypto-common",
|
||||
"inout",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "coa-control-proto"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"coa-registry-proto",
|
||||
"ed25519-dalek",
|
||||
"hex",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha2",
|
||||
"snow",
|
||||
"thiserror 2.0.21",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "coa-core"
|
||||
version = "0.7.1"
|
||||
version = "0.6.21"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"chrono",
|
||||
"coa-control-proto",
|
||||
"coa-registry-proto",
|
||||
"dunce",
|
||||
"ed25519-dalek",
|
||||
"flate2",
|
||||
"fs4",
|
||||
"hex",
|
||||
"if-addrs",
|
||||
"num-bigint",
|
||||
"rand_core 0.6.4",
|
||||
"rand_core",
|
||||
"reqwest 0.12.28",
|
||||
"rusqlite",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha1",
|
||||
@@ -611,30 +512,15 @@ dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
"tungstenite",
|
||||
"uuid",
|
||||
"windows-sys 0.60.2",
|
||||
"zip 2.4.2",
|
||||
"zstd",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "coa-registry-proto"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"ed25519-dalek",
|
||||
"hex",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha2",
|
||||
"thiserror 2.0.21",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "coa-release"
|
||||
version = "0.7.1"
|
||||
version = "0.6.21"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"coa-core",
|
||||
@@ -643,11 +529,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "coa-server-manager"
|
||||
version = "0.7.1"
|
||||
version = "0.6.21"
|
||||
dependencies = [
|
||||
"coa-control-proto",
|
||||
"coa-core",
|
||||
"coa-registry-proto",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tauri",
|
||||
@@ -784,7 +668,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
"rand_core 0.6.4",
|
||||
"typenum",
|
||||
]
|
||||
|
||||
@@ -817,15 +700,6 @@ version = "1.0.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "914a755b7c2d4af2bdcff7ce1739e2db9a1b81a9b07123d8015786ae03c0980d"
|
||||
|
||||
[[package]]
|
||||
name = "ctr"
|
||||
version = "0.9.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835"
|
||||
dependencies = [
|
||||
"cipher",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "curve25519-dalek"
|
||||
version = "4.1.3"
|
||||
@@ -887,12 +761,6 @@ dependencies = [
|
||||
"syn 3.0.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "data-encoding"
|
||||
version = "2.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4583a4551df46e2792f82ceeac45e850d2e2d5debba0b91f102385cda5b11f06"
|
||||
|
||||
[[package]]
|
||||
name = "dbus"
|
||||
version = "0.9.12"
|
||||
@@ -994,7 +862,6 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
|
||||
dependencies = [
|
||||
"block-buffer",
|
||||
"crypto-common",
|
||||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1133,7 +1000,7 @@ checksum = "70e796c081cee67dc755e1a36a0a172b897fab85fc3f6bc48307991f64e4eca9"
|
||||
dependencies = [
|
||||
"curve25519-dalek",
|
||||
"ed25519",
|
||||
"rand_core 0.6.4",
|
||||
"rand_core",
|
||||
"serde",
|
||||
"sha2",
|
||||
"subtle",
|
||||
@@ -1234,18 +1101,6 @@ dependencies = [
|
||||
"pin-project-lite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fallible-iterator"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649"
|
||||
|
||||
[[package]]
|
||||
name = "fallible-streaming-iterator"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
|
||||
|
||||
[[package]]
|
||||
name = "fastrand"
|
||||
version = "2.5.0"
|
||||
@@ -1605,16 +1460,6 @@ dependencies = [
|
||||
"r-efi 6.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ghash"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0d8a4362ccb29cb0b265253fb0a2728f592895ee6854fd9bc13f2ffda266ff1"
|
||||
dependencies = [
|
||||
"opaque-debug",
|
||||
"polyval",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gio"
|
||||
version = "0.18.4"
|
||||
@@ -1769,32 +1614,11 @@ version = "0.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.16.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100"
|
||||
dependencies = [
|
||||
"foldhash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.17.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
|
||||
dependencies = [
|
||||
"foldhash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashlink"
|
||||
version = "0.12.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a596f1b20ed2cc5ecac41a164aaebc7258057060f06c0cf7a2ba3991ee7990fb"
|
||||
dependencies = [
|
||||
"hashbrown 0.17.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
@@ -2132,15 +1956,6 @@ dependencies = [
|
||||
"cfb",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "inout"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ipnet"
|
||||
version = "2.12.2"
|
||||
@@ -2440,17 +2255,6 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libsqlite3-sys"
|
||||
version = "0.38.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1d20bef17f513b9b3004532233187769cd072d790971f4e4da0e346eb6401e8"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"pkg-config",
|
||||
"vcpkg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.12.1"
|
||||
@@ -2892,12 +2696,6 @@ version = "1.21.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
||||
|
||||
[[package]]
|
||||
name = "opaque-debug"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381"
|
||||
|
||||
[[package]]
|
||||
name = "open"
|
||||
version = "5.4.4"
|
||||
@@ -3181,29 +2979,6 @@ dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "poly1305"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8159bd90725d2df49889a078b54f4f79e87f1f8a8444194cdca81d38f5393abf"
|
||||
dependencies = [
|
||||
"cpufeatures",
|
||||
"opaque-debug",
|
||||
"universal-hash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "polyval"
|
||||
version = "0.6.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9d1fe60d06143b2430aa532c94cfe9e29783047f06c0d7fd359a9a51b729fa25"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"cpufeatures",
|
||||
"opaque-debug",
|
||||
"universal-hash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "portable-atomic"
|
||||
version = "1.15.0"
|
||||
@@ -3234,15 +3009,6 @@ version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
|
||||
dependencies = [
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "precomputed-hash"
|
||||
version = "0.1.1"
|
||||
@@ -3341,26 +3107,6 @@ version = "6.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.9.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41"
|
||||
dependencies = [
|
||||
"rand_chacha",
|
||||
"rand_core 0.9.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core 0.9.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.6.4"
|
||||
@@ -3370,15 +3116,6 @@ dependencies = [
|
||||
"getrandom 0.2.17",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.9.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c"
|
||||
dependencies = [
|
||||
"getrandom 0.3.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "raw-window-handle"
|
||||
version = "0.6.2"
|
||||
@@ -3568,31 +3305,6 @@ dependencies = [
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rsqlite-vfs"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c51c9ae4df8a7fba42103df5c621fa3c37eccf3a3c650879e90fc48b11cc192c"
|
||||
dependencies = [
|
||||
"hashbrown 0.16.1",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rusqlite"
|
||||
version = "0.40.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "23f2a97da3e3873c73cb2a2e71b35c40ff95e0b1eefa8d72d8499a6928c3b5b3"
|
||||
dependencies = [
|
||||
"bitflags 2.13.2",
|
||||
"fallible-iterator",
|
||||
"fallible-streaming-iterator",
|
||||
"hashlink",
|
||||
"libsqlite3-sys",
|
||||
"smallvec",
|
||||
"sqlite-wasm-rs",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-hash"
|
||||
version = "2.1.3"
|
||||
@@ -4057,7 +3769,7 @@ version = "2.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de"
|
||||
dependencies = [
|
||||
"rand_core 0.6.4",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4100,22 +3812,6 @@ version = "1.16.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f9395f0f0eee849a9b707b2f06bb92a6a422090e2123bb2ef8e87a0e61892a8e"
|
||||
|
||||
[[package]]
|
||||
name = "snow"
|
||||
version = "0.9.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "850948bee068e713b8ab860fe1adc4d109676ab4c3b621fd8147f06b261f2f85"
|
||||
dependencies = [
|
||||
"aes-gcm",
|
||||
"blake2",
|
||||
"chacha20poly1305",
|
||||
"curve25519-dalek",
|
||||
"rand_core 0.6.4",
|
||||
"rustc_version",
|
||||
"sha2",
|
||||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "socket2"
|
||||
version = "0.6.5"
|
||||
@@ -4184,18 +3880,6 @@ dependencies = [
|
||||
"der",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sqlite-wasm-rs"
|
||||
version = "0.5.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc3efc0da82635d7e1ced0053bbbfa8c7ab9645d0bf36ceb4f7127bb85315d75"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"js-sys",
|
||||
"rsqlite-vfs",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "stable_deref_trait"
|
||||
version = "1.2.1"
|
||||
@@ -5114,24 +4798,6 @@ version = "0.2.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
|
||||
|
||||
[[package]]
|
||||
name = "tungstenite"
|
||||
version = "0.26.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4793cb5e56680ecbb1d843515b23b6de9a75eb04b66643e256a396d43be33c13"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"data-encoding",
|
||||
"http",
|
||||
"httparse",
|
||||
"log",
|
||||
"native-tls",
|
||||
"rand",
|
||||
"sha1",
|
||||
"thiserror 2.0.21",
|
||||
"utf-8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typeid"
|
||||
version = "1.0.3"
|
||||
@@ -5167,16 +4833,6 @@ version = "1.13.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8"
|
||||
|
||||
[[package]]
|
||||
name = "universal-hash"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea"
|
||||
dependencies = [
|
||||
"crypto-common",
|
||||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "untrusted"
|
||||
version = "0.9.0"
|
||||
@@ -5208,12 +4864,6 @@ dependencies = [
|
||||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "utf-8"
|
||||
version = "0.7.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
|
||||
|
||||
[[package]]
|
||||
name = "utf8_iter"
|
||||
version = "1.0.4"
|
||||
@@ -6108,26 +5758,6 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.8.61"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "879fb705ce98c32e41ebdb970fbe1204f8492423b314c6ab0354c3e7b5542866"
|
||||
dependencies = [
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.8.61"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "708882a28301d604fa039cc7727607a98d04c4b86dc76ec9cc683f805d709759"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.119",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerofrom"
|
||||
version = "0.1.8"
|
||||
|
||||
+2
-3
@@ -1,10 +1,9 @@
|
||||
[workspace]
|
||||
resolver = "2"
|
||||
members = ["crates/coa-core", "crates/coa-release", "crates/coa-registry-proto", "crates/coa-control-proto", "src-tauri"]
|
||||
exclude = ["services/coa-registry", "services/coa-registry-e2e", "services/coa-coordinator", "services/coa-relay"]
|
||||
members = ["crates/coa-core", "crates/coa-release", "src-tauri"]
|
||||
|
||||
[workspace.package]
|
||||
version = "0.7.1"
|
||||
version = "0.6.21"
|
||||
edition = "2021"
|
||||
license = "AGPL-3.0-only"
|
||||
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
# CoA Server Manager
|
||||
|
||||
**Portable characters are experimental.** This feature is in active development and may contain bugs.
|
||||
|
||||
A Windows app that installs and runs your own **Conquest of Azeroth** server (AzerothCore) with a few clicks:
|
||||
no console, no config files, no database tools. Tauri 2 + Rust + React.
|
||||
|
||||

|
||||

|
||||
|
||||
## What it does
|
||||
|
||||
* **Portable characters (Experimental).** Keep saved characters in the Manager, review their saved versions and make an existing character portable from a realm you can access. See the [short guide](docs/PORTABLE-CHARACTERS.md).
|
||||
* **Find a realm.** Browse public realms and compare their game modes, level caps, rates, modules and population.
|
||||
* **One-click install.** Downloads a signed, verified server package (resumable, checked file by file), sets up the
|
||||
database and creates your first account. Or import a server you already have.
|
||||
* **Start / Stop / PLAY.** One button for the whole server (database, auth, world) with live status, uptime and
|
||||
@@ -22,14 +18,14 @@ no console, no config files, no database tools. Tauri 2 + Rust + React.
|
||||
into **UPDATE**. Updates are authenticated (Ed25519), back up first, keep files you changed and can be rolled back.
|
||||
* **Backups.** Characters, accounts and settings on demand, automatically before every update; verify and restore
|
||||
from the app.
|
||||
* **CoA / Wildcard / Warcraft Reborn realms.** Choose the active realm on Overview. A server build supporting the selected realm is required;
|
||||
* **CoA / Wildcard realms.** Choose the active realm on Overview. A server build with Wildcard support is required;
|
||||
the first switch copies world data and creates an empty character database. Accounts are shared, characters and
|
||||
module settings are separate, and only one world runs. Switching a running server saves characters and restarts it.
|
||||
The closed client's saved realm follows this selection and is checked again before PLAY. If the client is open,
|
||||
the change is applied on the next launch through the Manager.
|
||||
Bots are unavailable on Wildcard and Warcraft Reborn; other optional modules are experimental. Backups and
|
||||
database updates include every created realm. Simultaneous startup supports CoA + Wildcard only.
|
||||
See [Wildcard guide](docs/WILDCARD.md) and [Warcraft Reborn](docs/WARCRAFT-REBORN.md).
|
||||
CoA companions are unavailable on Wildcard; other optional modules are experimental. Backups include both realms,
|
||||
and database updates apply to both.
|
||||
See [realm profiles and validation](docs/WILDCARD.md).
|
||||
* **Companions (bots).** Fill the world with level-appropriate bots, see progress, stop spawning, take them offline,
|
||||
despawn N to relieve the server, and choose how many log in at server start.
|
||||
* **Realmlist switcher.** Keep named realmlists (for example *Solo* and *PTR*) and switch the game client between them from a
|
||||
@@ -43,22 +39,17 @@ no console, no config files, no database tools. Tauri 2 + Rust + React.
|
||||
supports LAN, direct and private-network (Tailscale) play, with step-by-step guides and a package to send to friends.
|
||||
* **Updates to the Manager itself** download in the background and install when you close it. Your servers are never
|
||||
stopped or changed by a Manager update.
|
||||
* **Languages:** English, Russian, German, French, Spanish, Simplified Chinese.
|
||||
* **Languages:** English, Russian, German, French, Spanish.
|
||||
|
||||
## See the Manager
|
||||
| Install | Ready |
|
||||
|---------|-------|
|
||||
|  |  |
|
||||
|
||||
| Portable characters (Experimental) | Public realms |
|
||||
|---|---|
|
||||
|  |  |
|
||||
| Overview | Server updates |
|
||||
|----------|----------------|
|
||||
|  |  |
|
||||
|
||||
| Companions | Backups |
|
||||
|---|---|
|
||||
|  |  |
|
||||
|
||||
The full gallery also covers setup, modules, game clients, playing with friends and making an existing character portable.
|
||||
|
||||
|
||||
[Browse the screenshot gallery](docs/SCREENSHOTS.md) · [Player and server owner guides](docs/README.md).
|
||||

|
||||
|
||||
## Install
|
||||
|
||||
@@ -73,13 +64,9 @@ Manager and server updates have separate version histories.
|
||||
|
||||
## Status
|
||||
|
||||
**In active development.** Bugs may still occur. If you run into a problem, please [open an issue](https://github.com/Corfirean/coa-server-manager/issues/new/choose).
|
||||
|
||||
### Known Limitations
|
||||
* **Native client login prefill**: Ascension's 3.3.5 client does not accept command-line credentials; users type their password once or link an existing account.
|
||||
* **Non-Windows SecretStore**: Windows uses DPAPI (`CryptProtectData`) with domain separation entropy. On Linux/macOS, keys fall back to strict 0600 file permissions.
|
||||
* **Direct to Relay migration**: Established direct TCP sessions cannot migrate mid-stream to Relay; network breaks trigger automatic fallback on the next JOIN.
|
||||
* **WorkerThreads**: `CharacterDatabase.WorkerThreads` must be set to `1` (the default) for safe remote portable character imports.
|
||||
Phases 0–10 of the plan in [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) are done: foundation, import, start/stop,
|
||||
config engine, backups, clean install from signed packages, updates, companions, client launch, friends, polish.
|
||||
Remaining work (translation review by native speakers, optional extras) is listed in section 9 of that document. Translation notes: [docs/TRANSLATIONS.md](docs/TRANSLATIONS.md).
|
||||
|
||||
## Develop
|
||||
```
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
---
|
||||
area: client
|
||||
type: added
|
||||
audience: players
|
||||
title: New dragon races and updated racial abilities are available
|
||||
---
|
||||
The custom race client includes Drakkin, Drakonid, updated appearances and cursor addons. Existing client files are preserved when installing or removing the patches.
|
||||
@@ -1,7 +0,0 @@
|
||||
---
|
||||
area: manager
|
||||
type: fixed
|
||||
audience: admins
|
||||
title: Server stop errors remain readable while private connection codes stay hidden
|
||||
---
|
||||
The Manager keeps useful MySQL shutdown messages in diagnostics without exposing portable character connection codes.
|
||||
@@ -1,7 +0,0 @@
|
||||
---
|
||||
area: manager
|
||||
type: fixed
|
||||
audience: players
|
||||
title: Characters on other servers show their CoA class names
|
||||
---
|
||||
The character list shows Barbarian, Necromancer and the other CoA classes instead of a class number.
|
||||
@@ -1,7 +0,0 @@
|
||||
---
|
||||
area: manager
|
||||
type: fixed
|
||||
audience: players
|
||||
title: Play can launch Windows clients that require administrator approval
|
||||
---
|
||||
Windows now shows its normal permission prompt when the game requires elevation. Other launch errors retain their original details.
|
||||
@@ -1,7 +0,0 @@
|
||||
---
|
||||
area: manager
|
||||
type: fixed
|
||||
audience: players
|
||||
title: Switching Custom Races patches is faster and shows when changes are being applied
|
||||
---
|
||||
The Manager avoids copying large patches when switching them and prevents repeated clicks while the operation is running.
|
||||
@@ -1,7 +0,0 @@
|
||||
---
|
||||
area: manager
|
||||
type: changed
|
||||
audience: players
|
||||
title: Portable characters are now clearly marked as experimental
|
||||
---
|
||||
The experimental status is shown both in Characters and when making a character portable from another realm.
|
||||
@@ -1,7 +0,0 @@
|
||||
---
|
||||
area: manager
|
||||
type: added
|
||||
audience: admins
|
||||
title: Module settings for Content Scaling include creature health
|
||||
---
|
||||
Adjust creature health for the open world, dungeons, raids, ordinary enemies and bosses. Values from 0.1 to 10 multiply the existing balance; 1 keeps it unchanged. Requires a server build with the matching Content Scaling settings.
|
||||
@@ -1,7 +0,0 @@
|
||||
---
|
||||
area: manager
|
||||
type: added
|
||||
audience: admins
|
||||
title: Warcraft Reborn can now be created and selected as a separate realm
|
||||
---
|
||||
Warcraft Reborn has separate characters and settings, shares accounts, and is included in backups, updates and database checks. A server build with Warcraft Reborn support is required.
|
||||
@@ -1,7 +0,0 @@
|
||||
---
|
||||
area: manager
|
||||
type: changed
|
||||
audience: players
|
||||
title: Custom Races client patches now use the latest complete package
|
||||
---
|
||||
Install or repair Custom Races client patches from the Modules page to get the updated package, including its latest appearance fixes and login screen artwork.
|
||||
+1
-33
@@ -112,14 +112,7 @@
|
||||
"Corfirean/coa-server-manager:20261016-preserve-core-migration-history.md",
|
||||
"Corfirean/coa-server-manager:20261016-shared-auth-migration-history.md",
|
||||
"Corfirean/coa-server-manager:20261016-update-compatibility-wording.md",
|
||||
"Corfirean/coa-server-manager:20261018-custom-races-client.md",
|
||||
"Corfirean/coa-server-manager:20261019-portable-stop-diagnostics.md",
|
||||
"Corfirean/coa-server-manager:20261019-remote-character-class-names.md",
|
||||
"Corfirean/coa-server-manager:20261020-client-elevation.md",
|
||||
"Corfirean/coa-server-manager:20261021-custom-races-switching.md",
|
||||
"Corfirean/coa-server-manager:20261021-portable-experimental.md",
|
||||
"Corfirean/coa-server-manager:20261021-scaling-health-settings.md",
|
||||
"Corfirean/coa-server-manager:20261021-warcraft-reborn-realm.md"
|
||||
"Corfirean/coa-server-manager:20261018-custom-races-client.md"
|
||||
],
|
||||
"releases": [
|
||||
{
|
||||
@@ -578,31 +571,6 @@
|
||||
"Existing client files are preserved when installing or removing race patches"
|
||||
],
|
||||
"compare": "https://github.com/Corfirean/coa-server-manager/compare/v0.6.21...v0.6.22"
|
||||
},
|
||||
{
|
||||
"tag": "v0.7.0",
|
||||
"version": "0.7.0",
|
||||
"date": "2026-10-21",
|
||||
"url": "https://github.com/Corfirean/coa-server-manager/releases/tag/v0.7.0",
|
||||
"notes": [
|
||||
"Keep portable characters and their saved versions in the Manager, and use them on compatible realms",
|
||||
"Make an existing character portable from a connected realm",
|
||||
"Browse public realms and see their connection details",
|
||||
"Link an existing realm account or create one on hosts that allow it",
|
||||
"Switch between CoA, Wildcard and Warcraft Reborn realm profiles on supported server builds",
|
||||
"Adjust creature health for different locations and enemy types in Content Scaling",
|
||||
"Switch Custom Races patches faster, with visible feedback while changes are applied",
|
||||
"Handle Windows requests for administrator permission when launching the game"
|
||||
],
|
||||
"commit": "d22ff09ac6fd7628aac14d90ec23f547cd9b4bf1",
|
||||
"experimentalFeatures": [
|
||||
"portable-characters"
|
||||
],
|
||||
"compare": "https://github.com/Corfirean/coa-server-manager/compare/v0.6.22...v0.7.0",
|
||||
"notice": "Portable transfers into Warcraft Reborn are not supported yet.",
|
||||
"links": [
|
||||
"[Player guides and screenshots](https://github.com/Corfirean/coa-server-manager/tree/d22ff09ac6fd7628aac14d90ec23f547cd9b4bf1/docs)"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
[package]
|
||||
name = "coa-control-proto"
|
||||
description = "Player <-> Host control protocol (Phase 12): coordinator framing, the end-to-end Noise channel, identity binding and the application messages. No I/O."
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "AGPL-3.0-only"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
coa-registry-proto = { path = "../coa-registry-proto" }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
sha2 = "0.10"
|
||||
hex = "0.4"
|
||||
base64 = "0.22"
|
||||
ed25519-dalek = "2"
|
||||
snow = "0.9"
|
||||
thiserror = "2"
|
||||
uuid = { version = "1", features = ["serde", "v4", "v7"] }
|
||||
@@ -1,470 +0,0 @@
|
||||
//! The application messages inside the end-to-end channel. One request, one response, in order. Nothing here is ever visible to the Coordinator.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{invalid, ControlError, Result};
|
||||
|
||||
pub const MAX_CONTROL_REQUEST_BYTES: usize = 4096;
|
||||
pub const MAX_TRANSFER_REQUEST_BYTES: usize = 4 * 1024 * 1024; // 4 MiB
|
||||
pub const MAX_REQUEST_BYTES: usize = MAX_TRANSFER_REQUEST_BYTES;
|
||||
pub const MIN_USERNAME: usize = 3;
|
||||
pub const MAX_USERNAME: usize = 16;
|
||||
pub const MIN_PASSWORD: usize = 8;
|
||||
pub const MAX_PASSWORD: usize = 16;
|
||||
pub const MAX_CHARACTERS: usize = 200;
|
||||
|
||||
/// A game account name: 3 to 16 ASCII letters, digits or `_`, not beginning or ending with `_`.
|
||||
pub fn valid_username(s: &str) -> bool {
|
||||
(MIN_USERNAME..=MAX_USERNAME).contains(&s.len())
|
||||
&& s.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_')
|
||||
&& !s.starts_with('_')
|
||||
&& !s.ends_with('_')
|
||||
}
|
||||
|
||||
/// A game account password: 8 to 16 ASCII letters and digits (the game compares passwords case-insensitively, so letters carry no extra strength; digits and length do).
|
||||
pub fn valid_password(s: &str) -> bool {
|
||||
(MIN_PASSWORD..=MAX_PASSWORD).contains(&s.len()) && s.bytes().all(|b| b.is_ascii_alphanumeric())
|
||||
}
|
||||
|
||||
/// A password for a realm, 16 characters of `A-Z0-9` from the operating system's randomness (about 82 bits), unique per realm.
|
||||
pub fn generate_password() -> String {
|
||||
const ALPHABET: &[u8; 36] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
let mut out = String::with_capacity(16);
|
||||
while out.len() < 16 {
|
||||
for b in Uuid::new_v4().as_bytes() {
|
||||
// rejection sampling: 252 is the largest multiple of 36 below 256, so every symbol is equally likely
|
||||
if *b < 252 && out.len() < 16 {
|
||||
out.push(ALPHABET[(*b % 36) as usize] as char);
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The account name wanted, made fit for the game: letters and digits only, upper case, at most 16 characters.
|
||||
pub fn sanitize_username(wanted: &str) -> String {
|
||||
let base: String = wanted
|
||||
.chars()
|
||||
.filter(char::is_ascii_alphanumeric)
|
||||
.collect::<String>()
|
||||
.to_ascii_uppercase();
|
||||
let base: String = base.chars().take(MAX_USERNAME).collect();
|
||||
if base.len() >= MIN_USERNAME {
|
||||
base
|
||||
} else {
|
||||
format!("{base}PLAYER").chars().take(MAX_USERNAME).collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// The stable alternative of an occupied name: `DMITRY` becomes `DMITRY_7K4M`. The suffix is derived from the player's id, so the same player gets the same alternative.
|
||||
pub fn alternative_username(base: &str, player: &Uuid, attempt: u32) -> String {
|
||||
const ALPHABET: &[u8; 32] = b"ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
||||
let bytes = player.as_bytes();
|
||||
let mix = u32::from_be_bytes([bytes[12], bytes[13], bytes[14], bytes[15]])
|
||||
.wrapping_add(attempt.wrapping_mul(0x9E37_79B1));
|
||||
let suffix: String = (0..4)
|
||||
.map(|i| ALPHABET[((mix >> (i * 5)) & 31) as usize] as char)
|
||||
.collect();
|
||||
let keep = MAX_USERNAME - 5;
|
||||
let head: String = base.chars().take(keep).collect();
|
||||
format!("{head}_{suffix}")
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(tag = "op", rename_all = "snake_case", deny_unknown_fields)]
|
||||
pub enum Request {
|
||||
Hello {
|
||||
protocol: u32,
|
||||
client: String,
|
||||
},
|
||||
/// Make the player's account on this realm, or reuse it. `password` is the one the player's Manager keeps for this realm; it travels only inside the channel.
|
||||
Provision {
|
||||
desired: Option<String>,
|
||||
password: String,
|
||||
have_credentials: bool,
|
||||
},
|
||||
ListCharacters,
|
||||
Claim {
|
||||
token: u32,
|
||||
},
|
||||
ClaimAck {
|
||||
character_id: Uuid,
|
||||
},
|
||||
/// Link an account the realm already has (one time).
|
||||
Link {
|
||||
login: String,
|
||||
password: String,
|
||||
},
|
||||
Route,
|
||||
/// Phase 12.1: Start or resume a remote portable character transfer.
|
||||
TransferOffer {
|
||||
transfer_id: Uuid,
|
||||
character_id: Uuid,
|
||||
canonical_revision: u64,
|
||||
content_hash: String,
|
||||
total_size: usize,
|
||||
collections: BTreeMap<String, Vec<u32>>,
|
||||
},
|
||||
/// A chunk of the encoded portable character snapshot payload.
|
||||
TransferChunk {
|
||||
transfer_id: Uuid,
|
||||
offset: usize,
|
||||
data: String,
|
||||
},
|
||||
/// Query status or resume progress of a transfer.
|
||||
TransferStatus {
|
||||
transfer_id: Uuid,
|
||||
},
|
||||
/// Commit the transferred payload into the realm and arm a session.
|
||||
TransferCommit {
|
||||
transfer_id: Uuid,
|
||||
},
|
||||
/// Acknowledge successful transfer and session arming.
|
||||
TransferAck {
|
||||
transfer_id: Uuid,
|
||||
character_id: Uuid,
|
||||
},
|
||||
/// Phase 13: Request a short-lived relay allocation for a relayed JOIN.
|
||||
AllocateRelay {
|
||||
player_id: Uuid,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum AppError {
|
||||
UnsupportedVersion,
|
||||
Invalid,
|
||||
ProvisioningOff,
|
||||
NoAccount,
|
||||
NotAtCharacterSelect,
|
||||
NotYours,
|
||||
AlreadyClaimed,
|
||||
NotEligible,
|
||||
WrongCredentials,
|
||||
AccountTaken,
|
||||
RateLimited,
|
||||
Unavailable,
|
||||
Incompatible,
|
||||
Conflict,
|
||||
RelayUnavailable,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct CharacterEntry {
|
||||
/// Opaque to the player: the Host's handle for this character, valid for this realm.
|
||||
pub token: u32,
|
||||
pub name: String,
|
||||
pub class: u32,
|
||||
pub race: u32,
|
||||
pub level: u32,
|
||||
pub eligible: bool,
|
||||
pub reasons: Vec<String>,
|
||||
/// Already portable and claimed by this player.
|
||||
pub yours: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(tag = "op", rename_all = "snake_case")]
|
||||
pub enum Response {
|
||||
Welcome {
|
||||
realm_id: coa_registry_proto::RealmId,
|
||||
automatic: bool,
|
||||
existing_only: bool,
|
||||
route: Option<String>,
|
||||
},
|
||||
Provisioned {
|
||||
username: String,
|
||||
created: bool,
|
||||
reset: bool,
|
||||
},
|
||||
Characters {
|
||||
characters: Vec<CharacterEntry>,
|
||||
},
|
||||
/// The character as the Host exported it: `payload` is the base64 of the encoded portable snapshot, `sha256` the hash of its canonical JSON, `collections` the account's
|
||||
/// appearance and vanity ids the Host holds for the account (kind -> ids).
|
||||
Claimed {
|
||||
character_id: Uuid,
|
||||
sha256: String,
|
||||
payload: String,
|
||||
collections: BTreeMap<String, Vec<u32>>,
|
||||
},
|
||||
Linked {
|
||||
username: String,
|
||||
},
|
||||
Done,
|
||||
RouteInfo {
|
||||
address: Option<String>,
|
||||
},
|
||||
/// The Host is ready to receive transfer chunks (or indicates an offset to resume from).
|
||||
TransferReady {
|
||||
transfer_id: Uuid,
|
||||
received_offset: usize,
|
||||
},
|
||||
/// Acknowledgement of a transfer chunk.
|
||||
TransferChunkAck {
|
||||
transfer_id: Uuid,
|
||||
received_offset: usize,
|
||||
},
|
||||
/// Successful commit and session arm.
|
||||
TransferCommitted {
|
||||
transfer_id: Uuid,
|
||||
character_id: Uuid,
|
||||
local_guid: u32,
|
||||
session_id: Uuid,
|
||||
projected_level: Option<u32>,
|
||||
notes: Vec<String>,
|
||||
},
|
||||
/// Phase 13: Relay allocation details returned to the player.
|
||||
RelayAllocated {
|
||||
relay_host: String,
|
||||
auth_port: u16,
|
||||
world_port: u16,
|
||||
token: String,
|
||||
expires_at: i64,
|
||||
},
|
||||
Error {
|
||||
code: AppError,
|
||||
message: String,
|
||||
},
|
||||
}
|
||||
|
||||
pub fn encode_request(r: &Request) -> Vec<u8> {
|
||||
serde_json::to_vec(r).expect("a request serialises")
|
||||
}
|
||||
|
||||
pub fn decode_request(bytes: &[u8]) -> Result<Request> {
|
||||
if bytes.len() > MAX_TRANSFER_REQUEST_BYTES {
|
||||
return Err(ControlError::Limit("a request is too long".into()));
|
||||
}
|
||||
let r: Request = serde_json::from_slice(bytes)
|
||||
.map_err(|_| ControlError::Invalid("a request is not valid".into()))?;
|
||||
if !matches!(
|
||||
&r,
|
||||
Request::TransferOffer { .. } | Request::TransferChunk { .. }
|
||||
) && bytes.len() > MAX_CONTROL_REQUEST_BYTES
|
||||
{
|
||||
return Err(ControlError::Limit(
|
||||
"a standard control request exceeds 4096 bytes".into(),
|
||||
));
|
||||
}
|
||||
match &r {
|
||||
Request::Provision {
|
||||
desired, password, ..
|
||||
} => {
|
||||
if !valid_password(password)
|
||||
|| desired
|
||||
.as_deref()
|
||||
.is_some_and(|d| d.len() > 64 || d.chars().any(char::is_control))
|
||||
{
|
||||
return invalid("a provisioning request has an unusable password or name");
|
||||
}
|
||||
}
|
||||
Request::Link { login, password } => {
|
||||
if !valid_username(login) || !valid_password_for_link(password) {
|
||||
return invalid("a link request has an unusable login or password");
|
||||
}
|
||||
}
|
||||
Request::Hello { client, .. } if client.len() > 64 => {
|
||||
return invalid("the client name is too long")
|
||||
}
|
||||
Request::TransferOffer {
|
||||
content_hash,
|
||||
total_size,
|
||||
collections,
|
||||
..
|
||||
} => {
|
||||
if content_hash.len() != 64 || !content_hash.chars().all(|c| c.is_ascii_hexdigit()) {
|
||||
return invalid("transfer content hash must be 64 hex characters");
|
||||
}
|
||||
if *total_size > crate::noise::MAX_MESSAGE_BYTES {
|
||||
return Err(ControlError::Limit(
|
||||
"transfer total size is too large".into(),
|
||||
));
|
||||
}
|
||||
if collections.len() > 1000 {
|
||||
return Err(ControlError::Limit(
|
||||
"collections dictionary exceeds 1,000 keys".into(),
|
||||
));
|
||||
}
|
||||
let total_items: usize = collections.values().map(|v| v.len()).sum();
|
||||
if total_items > 100_000 {
|
||||
return Err(ControlError::Limit(
|
||||
"collection mapping exceeds 100,000 entries".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
Request::TransferChunk { offset, data, .. } => {
|
||||
if *offset > MAX_TRANSFER_REQUEST_BYTES {
|
||||
return Err(ControlError::Limit(
|
||||
"transfer chunk offset exceeds limit".into(),
|
||||
));
|
||||
}
|
||||
if data.len() > MAX_TRANSFER_REQUEST_BYTES {
|
||||
return Err(ControlError::Limit("transfer chunk is too large".into()));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(r)
|
||||
}
|
||||
|
||||
/// An existing account's password may be anything the game accepts (6 to 16 printable characters without spaces or quotes).
|
||||
pub fn valid_password_for_link(s: &str) -> bool {
|
||||
(6..=16).contains(&s.len())
|
||||
&& s.chars()
|
||||
.all(|c| c.is_ascii_graphic() && c != '"' && c != '\'' && c != '\\')
|
||||
}
|
||||
|
||||
pub fn encode_response(r: &Response) -> Vec<u8> {
|
||||
serde_json::to_vec(r).expect("a response serialises")
|
||||
}
|
||||
|
||||
pub fn decode_response(bytes: &[u8]) -> Result<Response> {
|
||||
if bytes.len() > MAX_TRANSFER_REQUEST_BYTES {
|
||||
return Err(ControlError::Limit("a response is too long".into()));
|
||||
}
|
||||
serde_json::from_slice(bytes)
|
||||
.map_err(|_| ControlError::Invalid("a response is not valid".into()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn passwords_are_random_unique_and_fit_the_game() {
|
||||
let a = generate_password();
|
||||
let b = generate_password();
|
||||
assert_ne!(a, b);
|
||||
assert!(
|
||||
valid_password(&a)
|
||||
&& a.len() == 16
|
||||
&& a.bytes()
|
||||
.all(|c| c.is_ascii_uppercase() || c.is_ascii_digit()),
|
||||
"{a}"
|
||||
);
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
for _ in 0..200 {
|
||||
assert!(seen.insert(generate_password()), "no repeats");
|
||||
}
|
||||
let symbols: std::collections::HashSet<char> = (0..200)
|
||||
.flat_map(|_| generate_password().chars().collect::<Vec<_>>())
|
||||
.collect();
|
||||
assert_eq!(symbols.len(), 36, "every symbol occurs");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn names_are_made_fit_and_the_alternative_is_stable() {
|
||||
assert_eq!(sanitize_username("Dmitry"), "DMITRY");
|
||||
assert_eq!(sanitize_username("d m-i_t.r y"), "DMITRY");
|
||||
assert_eq!(sanitize_username("ab"), "ABPLAYER");
|
||||
assert_eq!(
|
||||
sanitize_username("a very long player name indeed"),
|
||||
"AVERYLONGPLAYERN"
|
||||
);
|
||||
assert_eq!(sanitize_username("<script>"), "SCRIPT");
|
||||
assert!(
|
||||
valid_username(&sanitize_username("")) && valid_username(&sanitize_username("日本語"))
|
||||
);
|
||||
let p = Uuid::now_v7();
|
||||
let alt = alternative_username("DMITRY", &p, 0);
|
||||
assert!(
|
||||
valid_username(&alt) && alt.starts_with("DMITRY_") && alt.len() == 11,
|
||||
"{alt}"
|
||||
);
|
||||
assert_eq!(
|
||||
alt,
|
||||
alternative_username("DMITRY", &p, 0),
|
||||
"stable for the same player"
|
||||
);
|
||||
assert_ne!(
|
||||
alt,
|
||||
alternative_username("DMITRY", &p, 1),
|
||||
"another attempt, another suffix"
|
||||
);
|
||||
assert_ne!(alt, alternative_username("DMITRY", &Uuid::now_v7(), 0));
|
||||
assert_eq!(
|
||||
alternative_username("ABCDEFGHIJKLMNOP", &p, 0).len(),
|
||||
16,
|
||||
"a long name is shortened to make room"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn requests_are_strict() {
|
||||
let ok = encode_request(&Request::Provision {
|
||||
desired: Some("DMITRY".into()),
|
||||
password: generate_password(),
|
||||
have_credentials: false,
|
||||
});
|
||||
assert!(decode_request(&ok).is_ok());
|
||||
for bad in [
|
||||
br#"{"op":"provision","desired":null,"password":"short","have_credentials":false}"#.as_slice(),
|
||||
br#"{"op":"provision","desired":null,"password":"has space 123456","have_credentials":false}"#,
|
||||
br#"{"op":"claim","token":1,"extra":true}"#,
|
||||
br#"{"op":"link","login":"a b","password":"secret1"}"#,
|
||||
br#"{"op":"unknown"}"#,
|
||||
b"not json",
|
||||
] {
|
||||
assert!(decode_request(bad).is_err(), "{}", String::from_utf8_lossy(bad));
|
||||
}
|
||||
assert!(decode_request(&vec![b' '; MAX_REQUEST_BYTES + 1]).is_err());
|
||||
assert!(decode_request(br#"{"op":"list_characters"}"#).is_ok());
|
||||
assert!(
|
||||
valid_password_for_link("pa55-w0rd")
|
||||
&& !valid_password_for_link("has\"quote")
|
||||
&& !valid_password_for_link("short")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transfer_messages_roundtrip_and_validate() {
|
||||
let tid = Uuid::new_v4();
|
||||
let cid = Uuid::new_v4();
|
||||
let hash = "a".repeat(64);
|
||||
let offer = Request::TransferOffer {
|
||||
transfer_id: tid,
|
||||
character_id: cid,
|
||||
canonical_revision: 5,
|
||||
content_hash: hash.clone(),
|
||||
total_size: 1024,
|
||||
collections: BTreeMap::new(),
|
||||
};
|
||||
let encoded = encode_request(&offer);
|
||||
let decoded = decode_request(&encoded).unwrap();
|
||||
assert_eq!(offer, decoded);
|
||||
|
||||
let bad_hash = Request::TransferOffer {
|
||||
transfer_id: tid,
|
||||
character_id: cid,
|
||||
canonical_revision: 5,
|
||||
content_hash: "too_short".into(),
|
||||
total_size: 1024,
|
||||
collections: BTreeMap::new(),
|
||||
};
|
||||
assert!(decode_request(&encode_request(&bad_hash)).is_err());
|
||||
|
||||
let chunk = Request::TransferChunk {
|
||||
transfer_id: tid,
|
||||
offset: 0,
|
||||
data: "aGVsbG8=".into(),
|
||||
};
|
||||
assert_eq!(chunk, decode_request(&encode_request(&chunk)).unwrap());
|
||||
|
||||
let resp = Response::TransferCommitted {
|
||||
transfer_id: tid,
|
||||
character_id: cid,
|
||||
local_guid: 42,
|
||||
session_id: Uuid::new_v4(),
|
||||
projected_level: Some(60),
|
||||
notes: vec!["Projected".into()],
|
||||
};
|
||||
let resp_dec = decode_response(&encode_response(&resp)).unwrap();
|
||||
assert_eq!(resp, resp_dec);
|
||||
}
|
||||
}
|
||||
@@ -1,387 +0,0 @@
|
||||
//! The Coordinator's own protocol. Text frames are JSON control messages; binary frames carry one end-to-end frame for one connection, prefixed with the
|
||||
//! connection number. The Coordinator routes by connection number and never looks inside.
|
||||
|
||||
use base64::engine::general_purpose::URL_SAFE_NO_PAD as B64;
|
||||
use base64::Engine;
|
||||
use coa_registry_proto::RealmId;
|
||||
use ed25519_dalek::{Signature, Signer, SigningKey, VerifyingKey};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{invalid, ControlError, Result};
|
||||
|
||||
/// The largest end-to-end frame the Coordinator forwards (a Noise message with its tag fits in 64 KiB; chunks are much smaller).
|
||||
pub const MAX_FRAME_BYTES: usize = 20 * 1024;
|
||||
/// Bytes of the connection number in front of a binary frame.
|
||||
pub const CONN_PREFIX: usize = 4;
|
||||
pub const MAX_CONTROL_TEXT_BYTES: usize = 2048;
|
||||
pub const HELLO_TIMEOUT_SECS: u64 = 10;
|
||||
/// A Hello's timestamp must be this close to the Coordinator's clock.
|
||||
pub const MAX_HELLO_SKEW_SECS: i64 = 120;
|
||||
|
||||
/// Hard limits of the Coordinator (the defaults of its configuration).
|
||||
pub mod limits {
|
||||
/// Simultaneous Player connections routed to one Host.
|
||||
pub const PER_HOST_CONNECTIONS: usize = 16;
|
||||
/// Simultaneous connections from one address.
|
||||
pub const PER_IP_CONNECTIONS: usize = 8;
|
||||
/// New Player connections per minute per realm, and per address.
|
||||
pub const PER_REALM_PER_MINUTE: u32 = 60;
|
||||
pub const PER_IP_PER_MINUTE: u32 = 30;
|
||||
/// Bytes through one connection, in total.
|
||||
pub const BYTES_PER_CONNECTION: usize = 16 * 1024 * 1024;
|
||||
/// Seconds a connection may live, and may be silent.
|
||||
pub const CONNECTION_LIFETIME_SECS: u64 = 600;
|
||||
pub const IDLE_SECS: u64 = 60;
|
||||
/// Seconds the Host has to answer a new connection (the first frame back).
|
||||
pub const HOST_ANSWER_SECS: u64 = 10;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(tag = "t", rename_all = "snake_case", deny_unknown_fields)]
|
||||
pub enum Frame {
|
||||
/// Coordinator -> anyone who connected: prove who you are with this.
|
||||
Challenge {
|
||||
protocol: u32,
|
||||
nonce: String,
|
||||
},
|
||||
/// Host -> Coordinator.
|
||||
HostHello {
|
||||
realm_id: RealmId,
|
||||
ts: i64,
|
||||
sig: String,
|
||||
},
|
||||
/// Player -> Coordinator (the realm it wants is in the URL and is signed).
|
||||
PlayerHello {
|
||||
player_id: Uuid,
|
||||
public_key: String,
|
||||
realm_id: RealmId,
|
||||
ts: i64,
|
||||
sig: String,
|
||||
},
|
||||
/// Coordinator -> Host: the Host is registered and these are the limits.
|
||||
HostReady {
|
||||
max_connections: u32,
|
||||
max_frame: u32,
|
||||
},
|
||||
/// Coordinator -> Host: a Player wants to talk; its frames will carry this number.
|
||||
Open {
|
||||
conn: u32,
|
||||
#[serde(default)]
|
||||
client_ip: Option<String>,
|
||||
},
|
||||
/// Either side of a connection ends it.
|
||||
Close {
|
||||
conn: u32,
|
||||
reason: Option<String>,
|
||||
},
|
||||
/// Coordinator -> Player: the Host accepted the connection; binary frames flow now.
|
||||
PlayerReady {
|
||||
conn: u32,
|
||||
max_frame: u32,
|
||||
},
|
||||
Error {
|
||||
code: ErrorCode,
|
||||
message: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum ErrorCode {
|
||||
BadHello,
|
||||
UnknownRealm,
|
||||
HostOffline,
|
||||
HostBusy,
|
||||
RateLimited,
|
||||
TooLarge,
|
||||
Timeout,
|
||||
Internal,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct ProbePayload {
|
||||
pub ports: Vec<u16>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct ProbeResponse {
|
||||
pub client_ip: String,
|
||||
pub results: std::collections::HashMap<u16, bool>,
|
||||
pub all_reachable: bool,
|
||||
}
|
||||
|
||||
pub fn challenge_nonce() -> String {
|
||||
let mut n = [0u8; 32];
|
||||
n[..16].copy_from_slice(Uuid::new_v4().as_bytes());
|
||||
n[16..].copy_from_slice(Uuid::new_v4().as_bytes());
|
||||
B64.encode(n)
|
||||
}
|
||||
|
||||
fn host_input(realm: &RealmId, nonce: &str, ts: i64) -> Vec<u8> {
|
||||
format!(
|
||||
"coa-coord-host-v1\n{}\n{realm}\n{nonce}\n{ts}",
|
||||
crate::CONTROL_PROTOCOL_VERSION
|
||||
)
|
||||
.into_bytes()
|
||||
}
|
||||
|
||||
fn player_input(player: &Uuid, public_key: &str, realm: &RealmId, nonce: &str, ts: i64) -> Vec<u8> {
|
||||
format!(
|
||||
"coa-coord-player-v1\n{}\n{}\n{public_key}\n{realm}\n{nonce}\n{ts}",
|
||||
crate::CONTROL_PROTOCOL_VERSION,
|
||||
player.hyphenated()
|
||||
)
|
||||
.into_bytes()
|
||||
}
|
||||
|
||||
pub fn sign_host_hello(key: &SigningKey, realm: &RealmId, nonce: &str, ts: i64) -> Frame {
|
||||
Frame::HostHello {
|
||||
realm_id: *realm,
|
||||
ts,
|
||||
sig: B64.encode(key.sign(&host_input(realm, nonce, ts)).to_bytes()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sign_player_hello(
|
||||
key: &SigningKey,
|
||||
player: &Uuid,
|
||||
realm: &RealmId,
|
||||
nonce: &str,
|
||||
ts: i64,
|
||||
) -> Frame {
|
||||
let public_key = B64.encode(key.verifying_key().as_bytes());
|
||||
let sig = B64.encode(
|
||||
key.sign(&player_input(player, &public_key, realm, nonce, ts))
|
||||
.to_bytes(),
|
||||
);
|
||||
Frame::PlayerHello {
|
||||
player_id: *player,
|
||||
public_key,
|
||||
realm_id: *realm,
|
||||
ts,
|
||||
sig,
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_sig(sig: &str) -> Result<Signature> {
|
||||
if sig.len() != 86 {
|
||||
return invalid("the signature has the wrong length");
|
||||
}
|
||||
let bytes: [u8; 64] = B64
|
||||
.decode(sig)
|
||||
.map_err(|_| ControlError::Invalid("the signature is not base64url".into()))?
|
||||
.try_into()
|
||||
.map_err(|_| ControlError::Invalid("the signature is not 64 bytes".into()))?;
|
||||
Ok(Signature::from_bytes(&bytes))
|
||||
}
|
||||
|
||||
/// A Host's hello checked against the key the Registry holds for the realm.
|
||||
pub fn verify_host_hello(
|
||||
frame: &Frame,
|
||||
realm_key: &VerifyingKey,
|
||||
expect_realm: Option<&RealmId>,
|
||||
nonce: &str,
|
||||
now: i64,
|
||||
) -> Result<RealmId> {
|
||||
let Frame::HostHello { realm_id, ts, sig } = frame else {
|
||||
return Err(ControlError::Auth("not a host hello".into()));
|
||||
};
|
||||
if expect_realm.is_some_and(|r| r != realm_id) {
|
||||
return Err(ControlError::Auth("another realm".into()));
|
||||
}
|
||||
if (ts - now).abs() > MAX_HELLO_SKEW_SECS {
|
||||
return Err(ControlError::Auth(
|
||||
"the timestamp is outside the allowed skew".into(),
|
||||
));
|
||||
}
|
||||
realm_key
|
||||
.verify_strict(&host_input(realm_id, nonce, *ts), &decode_sig(sig)?)
|
||||
.map_err(|_| ControlError::Auth("the signature does not match".into()))?;
|
||||
Ok(*realm_id)
|
||||
}
|
||||
|
||||
/// A Player's hello checked against the key it presents (proof of possession; the Host decides whether it is a known player).
|
||||
pub fn verify_player_hello(
|
||||
frame: &Frame,
|
||||
nonce: &str,
|
||||
now: i64,
|
||||
) -> Result<(Uuid, RealmId, VerifyingKey)> {
|
||||
let Frame::PlayerHello {
|
||||
player_id,
|
||||
public_key,
|
||||
realm_id,
|
||||
ts,
|
||||
sig,
|
||||
} = frame
|
||||
else {
|
||||
return Err(ControlError::Auth("not a player hello".into()));
|
||||
};
|
||||
if (ts - now).abs() > MAX_HELLO_SKEW_SECS {
|
||||
return Err(ControlError::Auth(
|
||||
"the timestamp is outside the allowed skew".into(),
|
||||
));
|
||||
}
|
||||
let key = decode_public_key(public_key)?;
|
||||
key.verify_strict(
|
||||
&player_input(player_id, public_key, realm_id, nonce, *ts),
|
||||
&decode_sig(sig)?,
|
||||
)
|
||||
.map_err(|_| ControlError::Auth("the signature does not match".into()))?;
|
||||
Ok((*player_id, *realm_id, key))
|
||||
}
|
||||
|
||||
pub fn decode_public_key(text: &str) -> Result<VerifyingKey> {
|
||||
coa_registry_proto::sign::decode_public_key(text)
|
||||
.map_err(|e| ControlError::Invalid(e.to_string()))
|
||||
}
|
||||
|
||||
pub fn encode_public_key(key: &VerifyingKey) -> String {
|
||||
coa_registry_proto::sign::encode_public_key(key)
|
||||
}
|
||||
|
||||
/// `[connection number][end-to-end frame]`
|
||||
pub fn binary(conn: u32, frame: &[u8]) -> Vec<u8> {
|
||||
let mut out = Vec::with_capacity(CONN_PREFIX + frame.len());
|
||||
out.extend_from_slice(&conn.to_be_bytes());
|
||||
out.extend_from_slice(frame);
|
||||
out
|
||||
}
|
||||
|
||||
pub fn split_binary(data: &[u8]) -> Result<(u32, &[u8])> {
|
||||
if data.len() < CONN_PREFIX || data.len() > CONN_PREFIX + MAX_FRAME_BYTES {
|
||||
return Err(ControlError::Limit(
|
||||
"a frame is too short or too long".into(),
|
||||
));
|
||||
}
|
||||
Ok((
|
||||
u32::from_be_bytes(data[..CONN_PREFIX].try_into().expect("4 bytes")),
|
||||
&data[CONN_PREFIX..],
|
||||
))
|
||||
}
|
||||
|
||||
pub fn parse_text(text: &str) -> Result<Frame> {
|
||||
if text.len() > MAX_CONTROL_TEXT_BYTES {
|
||||
return Err(ControlError::Limit("a control message is too long".into()));
|
||||
}
|
||||
serde_json::from_str(text)
|
||||
.map_err(|_| ControlError::Invalid("a control message is not valid".into()))
|
||||
}
|
||||
|
||||
pub fn to_text(frame: &Frame) -> String {
|
||||
serde_json::to_string(frame).expect("a frame serialises")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn key(n: u8) -> SigningKey {
|
||||
SigningKey::from_bytes(&[n; 32])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_host_proves_its_realm_key_and_only_that() {
|
||||
let realm = RealmId::new();
|
||||
let nonce = challenge_nonce();
|
||||
let hello = sign_host_hello(&key(1), &realm, &nonce, 1000);
|
||||
assert_eq!(
|
||||
verify_host_hello(&hello, &key(1).verifying_key(), Some(&realm), &nonce, 1010).unwrap(),
|
||||
realm
|
||||
);
|
||||
assert!(
|
||||
verify_host_hello(&hello, &key(2).verifying_key(), Some(&realm), &nonce, 1010).is_err(),
|
||||
"another key"
|
||||
);
|
||||
assert!(
|
||||
verify_host_hello(
|
||||
&hello,
|
||||
&key(1).verifying_key(),
|
||||
Some(&RealmId::new()),
|
||||
&nonce,
|
||||
1010
|
||||
)
|
||||
.is_err(),
|
||||
"another realm"
|
||||
);
|
||||
assert!(
|
||||
verify_host_hello(
|
||||
&hello,
|
||||
&key(1).verifying_key(),
|
||||
None,
|
||||
&challenge_nonce(),
|
||||
1010
|
||||
)
|
||||
.is_err(),
|
||||
"another challenge: no replay"
|
||||
);
|
||||
assert!(
|
||||
verify_host_hello(&hello, &key(1).verifying_key(), None, &nonce, 5000).is_err(),
|
||||
"too old"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_player_proves_possession_of_its_key() {
|
||||
let (player, realm, nonce) = (Uuid::now_v7(), RealmId::new(), challenge_nonce());
|
||||
let hello = sign_player_hello(&key(3), &player, &realm, &nonce, 50);
|
||||
let (p, r, k) = verify_player_hello(&hello, &nonce, 55).unwrap();
|
||||
assert_eq!(
|
||||
(p, r, k.to_bytes()),
|
||||
(player, realm, key(3).verifying_key().to_bytes())
|
||||
);
|
||||
let Frame::PlayerHello {
|
||||
player_id,
|
||||
public_key,
|
||||
realm_id,
|
||||
ts,
|
||||
sig,
|
||||
} = hello.clone()
|
||||
else {
|
||||
unreachable!()
|
||||
};
|
||||
let other_key = encode_public_key(&key(4).verifying_key());
|
||||
assert!(
|
||||
verify_player_hello(
|
||||
&Frame::PlayerHello {
|
||||
player_id,
|
||||
public_key: other_key,
|
||||
realm_id,
|
||||
ts,
|
||||
sig: sig.clone()
|
||||
},
|
||||
&nonce,
|
||||
55
|
||||
)
|
||||
.is_err(),
|
||||
"a key that did not sign"
|
||||
);
|
||||
assert!(
|
||||
verify_player_hello(
|
||||
&Frame::PlayerHello {
|
||||
player_id: Uuid::now_v7(),
|
||||
public_key,
|
||||
realm_id,
|
||||
ts,
|
||||
sig
|
||||
},
|
||||
&nonce,
|
||||
55
|
||||
)
|
||||
.is_err(),
|
||||
"another player id"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frames_are_strict_and_bounded() {
|
||||
assert!(parse_text(r#"{"t":"close","conn":1,"reason":null,"extra":1}"#).is_err());
|
||||
assert!(parse_text(&"x".repeat(5000)).is_err());
|
||||
assert!(parse_text(r#"{"t":"open","conn":7}"#).is_ok());
|
||||
let b = binary(9, b"abc");
|
||||
assert_eq!(split_binary(&b).unwrap(), (9, b"abc".as_slice()));
|
||||
assert!(split_binary(&[0, 0]).is_err());
|
||||
assert!(split_binary(&vec![0u8; CONN_PREFIX + MAX_FRAME_BYTES + 1]).is_err());
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
//! The Player <-> Host control protocol (Phase 12), control plane only.
|
||||
//!
|
||||
//! ```text
|
||||
//! Player Manager --(WebSocket)--> Coordinator <--(WebSocket, opened by the Host)-- Host Manager
|
||||
//! \________ Noise_XX end-to-end channel, authenticated by Ed25519 signatures over its handshake hash ________/
|
||||
//! ```
|
||||
//!
|
||||
//! * [`coord`]: what the Coordinator sees and decides: who may connect (a Host proves its RealmId key, a Player proves a PlayerId key), how frames are routed, the limits.
|
||||
//! * [`noise`]: the end-to-end channel the Coordinator cannot read: the Noise handshake, the framing of long messages, and the binding of identities to it.
|
||||
//! * [`app`]: the application messages inside the channel (account provisioning, listing and claiming characters, linking an existing account, the route).
|
||||
//!
|
||||
//! The Coordinator never receives plaintext account passwords, character data or database/RA credentials: everything of that kind exists only inside the channel.
|
||||
|
||||
pub mod app;
|
||||
pub mod coord;
|
||||
pub mod noise;
|
||||
pub mod relay;
|
||||
|
||||
pub const CONTROL_PROTOCOL_VERSION: u32 = 1;
|
||||
|
||||
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
|
||||
pub enum ControlError {
|
||||
#[error("{0}")]
|
||||
Invalid(String),
|
||||
#[error("the channel failed: {0}")]
|
||||
Channel(String),
|
||||
#[error("the peer could not prove who it is: {0}")]
|
||||
Auth(String),
|
||||
#[error("a limit was exceeded: {0}")]
|
||||
Limit(String),
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, ControlError>;
|
||||
|
||||
pub(crate) fn invalid<T>(m: impl Into<String>) -> Result<T> {
|
||||
Err(ControlError::Invalid(m.into()))
|
||||
}
|
||||
@@ -1,458 +0,0 @@
|
||||
//! The end-to-end channel between a Player Manager and a Host Manager, over whatever carries frames between them (the Coordinator, later perhaps a direct socket).
|
||||
//!
|
||||
//! **Handshake: `Noise_XX_25519_ChaChaPoly_BLAKE2s`** (the Noise Protocol Framework, implemented by the `snow` crate; no primitive is invented here). Both sides use fresh
|
||||
//! random X25519 static keys for each channel, so the Noise layer gives confidentiality, integrity and forward secrecy but no identity. Identity is added the standard way,
|
||||
//! by **channel binding**: after the handshake each side signs the handshake hash `h` (unique to this channel) with its long-lived Ed25519 key.
|
||||
//!
|
||||
//! ```text
|
||||
//! Player -> Host msg1 (e)
|
||||
//! Host -> Player msg2 (e, ee, s, es)
|
||||
//! Player -> Host msg3 (s, se) channel is open; h = the handshake hash
|
||||
//! Host -> Player HostProof Ed25519_realm_key("coa-ctl-host-v1\0" || h || realm_id) the Player checks it against the key the Registry publishes
|
||||
//! Player -> Host PlayerProof Ed25519_player_key("coa-ctl-player-v1\0" || h || realm_id || player_id) sent only after the Host's proof was good
|
||||
//! ```
|
||||
//!
|
||||
//! A Coordinator that relays the frames can neither read nor alter them (Noise authenticates every message), and cannot impersonate either side: a signature over `h`
|
||||
//! for one channel is useless on another.
|
||||
|
||||
use base64::engine::general_purpose::URL_SAFE_NO_PAD as B64;
|
||||
use base64::Engine;
|
||||
use coa_registry_proto::RealmId;
|
||||
use ed25519_dalek::{Signature, Signer, SigningKey, VerifyingKey};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use snow::{Builder, HandshakeState, TransportState};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{ControlError, Result};
|
||||
|
||||
pub const NOISE_PARAMS: &str = "Noise_XX_25519_ChaChaPoly_BLAKE2s";
|
||||
/// Plaintext bytes per transport frame (a frame is this plus one flag byte plus the 16-byte tag).
|
||||
pub const CHUNK: usize = 16 * 1024;
|
||||
/// The largest application message (reassembled).
|
||||
pub const MAX_MESSAGE_BYTES: usize = 8 * 1024 * 1024;
|
||||
const MAX_HANDSHAKE_BYTES: usize = 1024;
|
||||
|
||||
fn channel_err(e: snow::Error) -> ControlError {
|
||||
ControlError::Channel(e.to_string())
|
||||
}
|
||||
|
||||
fn builder() -> Result<Builder<'static>> {
|
||||
Ok(Builder::new(NOISE_PARAMS.parse().map_err(channel_err)?))
|
||||
}
|
||||
|
||||
/// The Player's side of the handshake.
|
||||
pub struct Initiator {
|
||||
hs: HandshakeState,
|
||||
}
|
||||
|
||||
impl Initiator {
|
||||
/// The state and the first message to send.
|
||||
pub fn start() -> Result<(Self, Vec<u8>)> {
|
||||
let b = builder()?;
|
||||
let key = b.generate_keypair().map_err(channel_err)?;
|
||||
let mut hs = b
|
||||
.local_private_key(&key.private)
|
||||
.build_initiator()
|
||||
.map_err(channel_err)?;
|
||||
let mut buf = vec![0u8; MAX_HANDSHAKE_BYTES];
|
||||
let n = hs.write_message(&[], &mut buf).map_err(channel_err)?;
|
||||
buf.truncate(n);
|
||||
Ok((Self { hs }, buf))
|
||||
}
|
||||
|
||||
/// Read the Host's answer; returns the third message to send and the open channel.
|
||||
pub fn finish(mut self, msg2: &[u8]) -> Result<(Vec<u8>, Channel)> {
|
||||
if msg2.len() > MAX_HANDSHAKE_BYTES {
|
||||
return Err(ControlError::Limit(
|
||||
"a handshake message is too long".into(),
|
||||
));
|
||||
}
|
||||
let mut buf = vec![0u8; MAX_HANDSHAKE_BYTES];
|
||||
self.hs.read_message(msg2, &mut buf).map_err(channel_err)?;
|
||||
let n = self.hs.write_message(&[], &mut buf).map_err(channel_err)?;
|
||||
buf.truncate(n);
|
||||
if !self.hs.is_handshake_finished() {
|
||||
return Err(ControlError::Channel("the handshake did not finish".into()));
|
||||
}
|
||||
Ok((buf, Channel::from(self.hs)?))
|
||||
}
|
||||
}
|
||||
|
||||
/// The Host's side of the handshake.
|
||||
pub struct Responder {
|
||||
hs: HandshakeState,
|
||||
}
|
||||
|
||||
impl Responder {
|
||||
/// Read the first message; returns the state and the second message to send.
|
||||
pub fn start(msg1: &[u8]) -> Result<(Self, Vec<u8>)> {
|
||||
if msg1.len() > MAX_HANDSHAKE_BYTES {
|
||||
return Err(ControlError::Limit(
|
||||
"a handshake message is too long".into(),
|
||||
));
|
||||
}
|
||||
let b = builder()?;
|
||||
let key = b.generate_keypair().map_err(channel_err)?;
|
||||
let mut hs = b
|
||||
.local_private_key(&key.private)
|
||||
.build_responder()
|
||||
.map_err(channel_err)?;
|
||||
let mut buf = vec![0u8; MAX_HANDSHAKE_BYTES];
|
||||
hs.read_message(msg1, &mut buf).map_err(channel_err)?;
|
||||
let n = hs.write_message(&[], &mut buf).map_err(channel_err)?;
|
||||
buf.truncate(n);
|
||||
Ok((Self { hs }, buf))
|
||||
}
|
||||
|
||||
pub fn finish(mut self, msg3: &[u8]) -> Result<Channel> {
|
||||
if msg3.len() > MAX_HANDSHAKE_BYTES {
|
||||
return Err(ControlError::Limit(
|
||||
"a handshake message is too long".into(),
|
||||
));
|
||||
}
|
||||
let mut buf = vec![0u8; MAX_HANDSHAKE_BYTES];
|
||||
self.hs.read_message(msg3, &mut buf).map_err(channel_err)?;
|
||||
if !self.hs.is_handshake_finished() {
|
||||
return Err(ControlError::Channel("the handshake did not finish".into()));
|
||||
}
|
||||
Channel::from(self.hs)
|
||||
}
|
||||
}
|
||||
|
||||
/// An open channel: messages go out as one or more encrypted frames and come back reassembled.
|
||||
pub struct Channel {
|
||||
transport: TransportState,
|
||||
hash: Vec<u8>,
|
||||
partial: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Channel {
|
||||
fn from(hs: HandshakeState) -> Result<Self> {
|
||||
let hash = hs.get_handshake_hash().to_vec();
|
||||
Ok(Self {
|
||||
transport: hs.into_transport_mode().map_err(channel_err)?,
|
||||
hash,
|
||||
partial: Vec::new(),
|
||||
})
|
||||
}
|
||||
|
||||
/// The handshake hash: unique to this channel, the same on both ends.
|
||||
pub fn handshake_hash(&self) -> &[u8] {
|
||||
&self.hash
|
||||
}
|
||||
|
||||
/// Encrypt one application message into frames (in order).
|
||||
pub fn seal(&mut self, message: &[u8]) -> Result<Vec<Vec<u8>>> {
|
||||
if message.len() > MAX_MESSAGE_BYTES {
|
||||
return Err(ControlError::Limit("a message is too long".into()));
|
||||
}
|
||||
let chunks: Vec<&[u8]> = if message.is_empty() {
|
||||
vec![&[][..]]
|
||||
} else {
|
||||
message.chunks(CHUNK).collect()
|
||||
};
|
||||
let mut frames = Vec::with_capacity(chunks.len());
|
||||
for (i, chunk) in chunks.iter().enumerate() {
|
||||
let mut plain = Vec::with_capacity(chunk.len() + 1);
|
||||
plain.push(u8::from(i + 1 < chunks.len()));
|
||||
plain.extend_from_slice(chunk);
|
||||
let mut out = vec![0u8; plain.len() + 16];
|
||||
let n = self
|
||||
.transport
|
||||
.write_message(&plain, &mut out)
|
||||
.map_err(channel_err)?;
|
||||
out.truncate(n);
|
||||
frames.push(out);
|
||||
}
|
||||
Ok(frames)
|
||||
}
|
||||
|
||||
/// Decrypt one frame. `Some(message)` when it completed a message; `None` when more frames follow. A frame that does not authenticate ends the channel.
|
||||
pub fn open_frame(&mut self, frame: &[u8]) -> Result<Option<Vec<u8>>> {
|
||||
if frame.len() < 17 || frame.len() > CHUNK + 17 {
|
||||
return Err(ControlError::Limit(
|
||||
"a frame is too short or too long".into(),
|
||||
));
|
||||
}
|
||||
let mut plain = vec![0u8; frame.len()];
|
||||
let n = self
|
||||
.transport
|
||||
.read_message(frame, &mut plain)
|
||||
.map_err(channel_err)?;
|
||||
plain.truncate(n);
|
||||
let (&more, chunk) = plain
|
||||
.split_first()
|
||||
.ok_or_else(|| ControlError::Channel("an empty frame".into()))?;
|
||||
if self.partial.len() + chunk.len() > MAX_MESSAGE_BYTES {
|
||||
self.partial.clear();
|
||||
return Err(ControlError::Limit("a message is too long".into()));
|
||||
}
|
||||
self.partial.extend_from_slice(chunk);
|
||||
match more {
|
||||
0 => Ok(Some(std::mem::take(&mut self.partial))),
|
||||
1 => Ok(None),
|
||||
_ => {
|
||||
self.partial.clear();
|
||||
Err(ControlError::Channel("a frame has an unknown flag".into()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---- identity bound to the channel --------------------------------------------------------------------------------------------
|
||||
|
||||
fn host_input(h: &[u8], realm: &RealmId) -> Vec<u8> {
|
||||
let mut v = b"coa-ctl-host-v1\0".to_vec();
|
||||
v.extend_from_slice(h);
|
||||
v.extend_from_slice(realm.to_string().as_bytes());
|
||||
v
|
||||
}
|
||||
|
||||
fn player_input(h: &[u8], realm: &RealmId, player: &Uuid) -> Vec<u8> {
|
||||
let mut v = b"coa-ctl-player-v1\0".to_vec();
|
||||
v.extend_from_slice(h);
|
||||
v.extend_from_slice(realm.to_string().as_bytes());
|
||||
v.extend_from_slice(player.hyphenated().to_string().as_bytes());
|
||||
v
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct HostProof {
|
||||
pub realm_id: RealmId,
|
||||
pub sig: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct PlayerProof {
|
||||
pub player_id: Uuid,
|
||||
pub public_key: String,
|
||||
pub sig: String,
|
||||
}
|
||||
|
||||
fn decode_sig(sig: &str) -> Result<Signature> {
|
||||
let bytes: [u8; 64] = B64
|
||||
.decode(sig)
|
||||
.ok()
|
||||
.and_then(|b| b.try_into().ok())
|
||||
.ok_or_else(|| ControlError::Auth("a signature is not 64 bytes of base64url".into()))?;
|
||||
Ok(Signature::from_bytes(&bytes))
|
||||
}
|
||||
|
||||
pub fn host_proof(key: &SigningKey, realm: &RealmId, channel: &Channel) -> HostProof {
|
||||
HostProof {
|
||||
realm_id: *realm,
|
||||
sig: B64.encode(
|
||||
key.sign(&host_input(channel.handshake_hash(), realm))
|
||||
.to_bytes(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// The Player checks that the peer holds the key the Registry publishes for the realm it asked for, on this very channel.
|
||||
pub fn verify_host_proof(
|
||||
proof: &HostProof,
|
||||
realm_key: &VerifyingKey,
|
||||
expected_realm: &RealmId,
|
||||
channel: &Channel,
|
||||
) -> Result<()> {
|
||||
if proof.realm_id != *expected_realm {
|
||||
return Err(ControlError::Auth(
|
||||
"the peer answers for another realm".into(),
|
||||
));
|
||||
}
|
||||
realm_key
|
||||
.verify_strict(
|
||||
&host_input(channel.handshake_hash(), expected_realm),
|
||||
&decode_sig(&proof.sig)?,
|
||||
)
|
||||
.map_err(|_| ControlError::Auth("the peer is not the realm's Host".into()))
|
||||
}
|
||||
|
||||
pub fn player_proof(
|
||||
key: &SigningKey,
|
||||
realm: &RealmId,
|
||||
player: &Uuid,
|
||||
channel: &Channel,
|
||||
) -> PlayerProof {
|
||||
PlayerProof {
|
||||
player_id: *player,
|
||||
public_key: crate::coord::encode_public_key(&key.verifying_key()),
|
||||
sig: B64.encode(
|
||||
key.sign(&player_input(channel.handshake_hash(), realm, player))
|
||||
.to_bytes(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// The Host learns which player key signed this channel; whether that key is the one it knows for the player id is the Host's decision.
|
||||
pub fn verify_player_proof(
|
||||
proof: &PlayerProof,
|
||||
realm: &RealmId,
|
||||
channel: &Channel,
|
||||
) -> Result<VerifyingKey> {
|
||||
let key = crate::coord::decode_public_key(&proof.public_key)?;
|
||||
key.verify_strict(
|
||||
&player_input(channel.handshake_hash(), realm, &proof.player_id),
|
||||
&decode_sig(&proof.sig)?,
|
||||
)
|
||||
.map_err(|_| ControlError::Auth("the player's proof does not match this channel".into()))?;
|
||||
Ok(key)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn pair() -> (Channel, Channel) {
|
||||
let (init, m1) = Initiator::start().unwrap();
|
||||
let (resp, m2) = Responder::start(&m1).unwrap();
|
||||
let (m3, player) = init.finish(&m2).unwrap();
|
||||
let host = resp.finish(&m3).unwrap();
|
||||
(player, host)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_channel_carries_messages_of_any_size_in_both_directions() {
|
||||
let (mut player, mut host) = pair();
|
||||
assert_eq!(
|
||||
player.handshake_hash(),
|
||||
host.handshake_hash(),
|
||||
"both ends hold the same channel binding"
|
||||
);
|
||||
for len in [
|
||||
0usize,
|
||||
1,
|
||||
100,
|
||||
CHUNK - 1,
|
||||
CHUNK,
|
||||
CHUNK + 1,
|
||||
5 * CHUNK + 7,
|
||||
600_000,
|
||||
] {
|
||||
let msg: Vec<u8> = (0..len).map(|i| (i * 31 % 251) as u8).collect();
|
||||
let frames = player.seal(&msg).unwrap();
|
||||
assert!(
|
||||
frames.iter().all(|f| f.len() <= CHUNK + 17),
|
||||
"every frame fits the coordinator's limit"
|
||||
);
|
||||
let mut got = None;
|
||||
for f in &frames {
|
||||
got = host.open_frame(f).unwrap();
|
||||
}
|
||||
assert_eq!(got.as_deref(), Some(msg.as_slice()), "{len}");
|
||||
let back = host.seal(&msg).unwrap();
|
||||
let mut got = None;
|
||||
for f in &back {
|
||||
got = player.open_frame(f).unwrap();
|
||||
}
|
||||
assert_eq!(got.as_deref(), Some(msg.as_slice()));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn what_a_relay_sees_is_not_the_message_and_what_it_changes_is_refused() {
|
||||
let (mut player, mut host) = pair();
|
||||
let secret = b"the password of this account is HUNTER2HUNTER2";
|
||||
let frames = player.seal(secret).unwrap();
|
||||
assert!(
|
||||
!frames[0]
|
||||
.windows(7)
|
||||
.any(|w| w == b"HUNTER2" || w == b"account"),
|
||||
"ciphertext carries no plaintext"
|
||||
);
|
||||
let mut tampered = frames[0].clone();
|
||||
tampered[20] ^= 1;
|
||||
assert!(
|
||||
host.open_frame(&tampered).is_err(),
|
||||
"a changed frame does not authenticate"
|
||||
);
|
||||
let (mut p2, mut h2) = pair();
|
||||
let f = p2.seal(b"x").unwrap();
|
||||
assert!(h2.open_frame(&f[0]).is_ok());
|
||||
assert!(
|
||||
h2.open_frame(&f[0]).is_err(),
|
||||
"a replayed frame is refused (the nonce moved on)"
|
||||
);
|
||||
let (mut p3, mut h3) = pair();
|
||||
let (mut p4, _h4) = pair();
|
||||
let foreign = p4.seal(b"x").unwrap();
|
||||
assert!(
|
||||
h3.open_frame(&foreign[0]).is_err(),
|
||||
"a frame of another channel is refused"
|
||||
);
|
||||
let _ = &mut p3;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_message_that_is_too_long_or_a_frame_that_is_malformed_is_a_limit_error() {
|
||||
let (mut player, mut host) = pair();
|
||||
assert!(player.seal(&vec![0u8; MAX_MESSAGE_BYTES + 1]).is_err());
|
||||
assert!(host.open_frame(&[0u8; 5]).is_err());
|
||||
assert!(host.open_frame(&vec![0u8; CHUNK + 40]).is_err());
|
||||
assert!(Responder::start(&vec![0u8; 5000]).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn identities_are_bound_to_one_channel() {
|
||||
let (player_ch, host_ch) = pair();
|
||||
let (other_player_ch, _other_host_ch) = pair();
|
||||
let realm = RealmId::new();
|
||||
let (realm_key, player_key) = (
|
||||
SigningKey::from_bytes(&[5; 32]),
|
||||
SigningKey::from_bytes(&[6; 32]),
|
||||
);
|
||||
let player_id = Uuid::now_v7();
|
||||
|
||||
let proof = host_proof(&realm_key, &realm, &host_ch);
|
||||
verify_host_proof(&proof, &realm_key.verifying_key(), &realm, &player_ch).unwrap();
|
||||
assert!(
|
||||
verify_host_proof(
|
||||
&proof,
|
||||
&SigningKey::from_bytes(&[9; 32]).verifying_key(),
|
||||
&realm,
|
||||
&player_ch
|
||||
)
|
||||
.is_err(),
|
||||
"an impostor with another key"
|
||||
);
|
||||
assert!(
|
||||
verify_host_proof(
|
||||
&proof,
|
||||
&realm_key.verifying_key(),
|
||||
&RealmId::new(),
|
||||
&player_ch
|
||||
)
|
||||
.is_err(),
|
||||
"another realm"
|
||||
);
|
||||
assert!(
|
||||
verify_host_proof(&proof, &realm_key.verifying_key(), &realm, &other_player_ch)
|
||||
.is_err(),
|
||||
"a proof captured on another channel is useless (no relay can replay it)"
|
||||
);
|
||||
|
||||
let pp = player_proof(&player_key, &realm, &player_id, &player_ch);
|
||||
let key = verify_player_proof(&pp, &realm, &host_ch).unwrap();
|
||||
assert_eq!(key.to_bytes(), player_key.verifying_key().to_bytes());
|
||||
assert!(verify_player_proof(&pp, &RealmId::new(), &host_ch).is_err());
|
||||
assert!(
|
||||
verify_player_proof(
|
||||
&PlayerProof {
|
||||
player_id: Uuid::now_v7(),
|
||||
..pp.clone()
|
||||
},
|
||||
&realm,
|
||||
&host_ch
|
||||
)
|
||||
.is_err(),
|
||||
"another player id under the same signature"
|
||||
);
|
||||
let (_, third_host) = pair();
|
||||
assert!(
|
||||
verify_player_proof(&pp, &realm, &third_host).is_err(),
|
||||
"another channel"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,318 +0,0 @@
|
||||
//! Game Relay protocol (Phase 13):
|
||||
//! Host-to-Relay multiplexed tunnel protocol, allocation messaging,
|
||||
//! challenge-response authentication, and WoW 3.3.5 REALM_LIST address rewrite helper.
|
||||
|
||||
use coa_registry_proto::RealmId;
|
||||
use ed25519_dalek::{Signature, Signer, SigningKey, VerifyingKey};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use uuid::Uuid;
|
||||
|
||||
pub const RELAY_PROTOCOL_VERSION: u32 = 1;
|
||||
|
||||
/// Default local targets that the Host opens for game traffic.
|
||||
pub const LOCAL_AUTH_PORT: u16 = 3724;
|
||||
pub const LOCAL_WORLD_PORT: u16 = 8085;
|
||||
|
||||
/// Predefined targets that the Relay can request over the Host tunnel.
|
||||
/// Arbitrary destinations are strictly forbidden.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum RelayTarget {
|
||||
Auth,
|
||||
World,
|
||||
}
|
||||
|
||||
impl RelayTarget {
|
||||
pub fn local_port(&self) -> u16 {
|
||||
match self {
|
||||
RelayTarget::Auth => std::env::var("COA_OVERRIDE_LOCAL_AUTH_PORT")
|
||||
.ok()
|
||||
.and_then(|p| p.parse().ok())
|
||||
.unwrap_or(LOCAL_AUTH_PORT),
|
||||
RelayTarget::World => std::env::var("COA_OVERRIDE_LOCAL_WORLD_PORT")
|
||||
.ok()
|
||||
.and_then(|p| p.parse().ok())
|
||||
.unwrap_or(LOCAL_WORLD_PORT),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct RelayChallenge {
|
||||
pub nonce: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct HostHello {
|
||||
pub realm_id: RealmId,
|
||||
pub signature: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct RelayWelcome {
|
||||
pub ok: bool,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
/// Messages multiplexed across the Host <-> Relay tunnel.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(tag = "cmd", rename_all = "snake_case", deny_unknown_fields)]
|
||||
pub enum TunnelMsg {
|
||||
/// Host asks Relay to allocate a game session for a player.
|
||||
Allocate {
|
||||
request_id: u64,
|
||||
player_id: Uuid,
|
||||
#[serde(default)]
|
||||
expected_client_ip: Option<String>,
|
||||
},
|
||||
/// Relay responds to Host with allocation details.
|
||||
AllocateOk {
|
||||
request_id: u64,
|
||||
token: String,
|
||||
relay_host: String,
|
||||
auth_port: u16,
|
||||
world_port: u16,
|
||||
expires_at: i64,
|
||||
},
|
||||
AllocateErr {
|
||||
request_id: u64,
|
||||
error: String,
|
||||
},
|
||||
|
||||
/// Relay asks Host to open a local connection to `target` (Auth or World) for an incoming player connection.
|
||||
Connect {
|
||||
stream_id: u32,
|
||||
target: RelayTarget,
|
||||
token: String,
|
||||
},
|
||||
/// Host confirms local connection established.
|
||||
ConnectOk {
|
||||
stream_id: u32,
|
||||
},
|
||||
/// Connect failed.
|
||||
ConnectErr {
|
||||
stream_id: u32,
|
||||
error: String,
|
||||
},
|
||||
|
||||
/// Stream data chunk (base64 encoded).
|
||||
Data {
|
||||
stream_id: u32,
|
||||
chunk: String,
|
||||
},
|
||||
/// Stream clean half-close / EOF.
|
||||
Close {
|
||||
stream_id: u32,
|
||||
},
|
||||
/// Stream abrupt abort / error.
|
||||
Reset {
|
||||
stream_id: u32,
|
||||
},
|
||||
/// Keepalive ping.
|
||||
Ping,
|
||||
/// Keepalive pong.
|
||||
Pong,
|
||||
}
|
||||
|
||||
fn relay_input(realm: &RealmId, nonce: &str) -> Vec<u8> {
|
||||
format!(
|
||||
"coa-relay-host-v1\n{}\n{realm}\n{nonce}",
|
||||
RELAY_PROTOCOL_VERSION
|
||||
)
|
||||
.into_bytes()
|
||||
}
|
||||
|
||||
/// Create a signature over (realm_id, nonce) using the realm's signing key.
|
||||
pub fn sign_relay_challenge(key: &SigningKey, realm_id: &RealmId, nonce: &str) -> String {
|
||||
let sig: Signature = key.sign(&relay_input(realm_id, nonce));
|
||||
hex::encode(sig.to_bytes())
|
||||
}
|
||||
|
||||
/// Verify a signature over (realm_id, nonce) using the realm's verifying key.
|
||||
pub fn verify_relay_challenge(
|
||||
key: &VerifyingKey,
|
||||
realm_id: &RealmId,
|
||||
nonce: &str,
|
||||
sig_hex: &str,
|
||||
) -> bool {
|
||||
let Ok(sig_bytes) = hex::decode(sig_hex) else {
|
||||
return false;
|
||||
};
|
||||
let Ok(sig) = Signature::from_slice(&sig_bytes) else {
|
||||
return false;
|
||||
};
|
||||
key.verify_strict(&relay_input(realm_id, nonce), &sig)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
/// Rewrites the realm address in a WoW 3.3.5 REALM_LIST response packet (Opcode 0x10).
|
||||
///
|
||||
/// In WoW 3.3.5 client protocol:
|
||||
/// - Byte 0: 0x10 (REALM_LIST)
|
||||
/// - Bytes 1..2: uint16 LE payload length (total size after byte 2)
|
||||
/// - Bytes 3..6: uint32 LE unused (0)
|
||||
/// - Bytes 7..8: uint16 LE realm count
|
||||
/// - For each realm:
|
||||
/// - uint8 type (1 byte)
|
||||
/// - uint8 lock (1 byte)
|
||||
/// - uint8 flag (1 byte)
|
||||
/// - name: null-terminated C string
|
||||
/// - address: null-terminated C string ("host:port\0")
|
||||
/// - population: 4 bytes float
|
||||
/// - characters: 1 byte uint8
|
||||
/// - timezone: 1 byte uint8
|
||||
/// - realm_id: 1 byte uint8
|
||||
/// - if flag & 0x04 (SPECIFYBUILD): 5 bytes build info
|
||||
/// - Footer: 2 bytes (0x10, 0x00)
|
||||
///
|
||||
/// Replaces the address of the first realm (or matches) with `new_host_port` and adjusts
|
||||
/// the uint16 length at bytes 1..2 by the byte length delta.
|
||||
pub fn rewrite_realm_list_address(
|
||||
pkt: &[u8],
|
||||
new_host_port: &str,
|
||||
) -> Result<Option<Vec<u8>>, String> {
|
||||
if pkt.is_empty() || pkt[0] != 0x10 {
|
||||
return Ok(None);
|
||||
}
|
||||
if pkt.len() < 9 {
|
||||
return Err("packet too short for REALM_LIST header".into());
|
||||
}
|
||||
let body_len = u16::from_le_bytes([pkt[1], pkt[2]]) as usize;
|
||||
if pkt.len() < 3 + body_len {
|
||||
return Err("truncated REALM_LIST packet".into());
|
||||
}
|
||||
let realm_count = u16::from_le_bytes([pkt[7], pkt[8]]) as usize;
|
||||
if realm_count == 0 {
|
||||
return Ok(Some(pkt.to_vec()));
|
||||
}
|
||||
|
||||
let mut pos = 9;
|
||||
if pos + 3 > pkt.len() {
|
||||
return Err("malformed realm header".into());
|
||||
}
|
||||
pos += 3; // type, lock, flag
|
||||
|
||||
let name_end = pkt[pos..]
|
||||
.iter()
|
||||
.position(|&b| b == 0)
|
||||
.ok_or("unterminated realm name")?
|
||||
+ pos;
|
||||
pos = name_end + 1;
|
||||
|
||||
let addr_start = pos;
|
||||
let addr_end = pkt[pos..]
|
||||
.iter()
|
||||
.position(|&b| b == 0)
|
||||
.ok_or("unterminated realm address")?
|
||||
+ pos;
|
||||
let old_addr = &pkt[addr_start..addr_end];
|
||||
|
||||
if new_host_port.len() > 255 {
|
||||
return Err("new host:port exceeds maximum address length".into());
|
||||
}
|
||||
let diff = new_host_port.len() as isize - old_addr.len() as isize;
|
||||
let new_body_len_isize = body_len as isize + diff;
|
||||
if !(0..=u16::MAX as isize).contains(&new_body_len_isize) {
|
||||
return Err("adjusted body length out of bounds".into());
|
||||
}
|
||||
let new_body_len = new_body_len_isize as u16;
|
||||
|
||||
let mut out = Vec::with_capacity(pkt.len() + new_host_port.len() + 1);
|
||||
out.extend_from_slice(&pkt[..addr_start]);
|
||||
out.extend_from_slice(new_host_port.as_bytes());
|
||||
out.extend_from_slice(&pkt[addr_end..]);
|
||||
out[1..3].copy_from_slice(&new_body_len.to_le_bytes());
|
||||
|
||||
Ok(Some(out))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn challenge_signature_roundtrip() {
|
||||
let seed = [42u8; 32];
|
||||
let key = SigningKey::from_bytes(&seed);
|
||||
|
||||
let realm = RealmId::new();
|
||||
let nonce = "test-challenge-nonce-123";
|
||||
|
||||
let sig = sign_relay_challenge(&key, &realm, nonce);
|
||||
assert!(verify_relay_challenge(
|
||||
&key.verifying_key(),
|
||||
&realm,
|
||||
nonce,
|
||||
&sig
|
||||
));
|
||||
|
||||
// Wrong nonce
|
||||
assert!(!verify_relay_challenge(
|
||||
&key.verifying_key(),
|
||||
&realm,
|
||||
"other-nonce",
|
||||
&sig
|
||||
));
|
||||
// Wrong realm
|
||||
assert!(!verify_relay_challenge(
|
||||
&key.verifying_key(),
|
||||
&RealmId::new(),
|
||||
nonce,
|
||||
&sig
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rewrite_realm_list_address_works() {
|
||||
// Build a mock REALM_LIST packet for WoW 3.3.5
|
||||
let mut pkt = Vec::new();
|
||||
pkt.push(0x10); // Opcode
|
||||
pkt.extend_from_slice(&[0, 0]); // Placeholder for body_len
|
||||
pkt.extend_from_slice(&[0, 0, 0, 0]); // Unused uint32
|
||||
pkt.extend_from_slice(&1u16.to_le_bytes()); // Realm count = 1
|
||||
|
||||
// Realm 0
|
||||
pkt.push(1); // Type
|
||||
pkt.push(0); // Lock
|
||||
pkt.push(0); // Flags
|
||||
pkt.extend_from_slice(b"CoA Test Realm\0"); // Name
|
||||
pkt.extend_from_slice(b"127.0.0.1:8085\0"); // Old Address
|
||||
pkt.extend_from_slice(&0.5f32.to_le_bytes()); // Population
|
||||
pkt.push(1); // Characters
|
||||
pkt.push(1); // Timezone
|
||||
pkt.push(1); // Realm ID
|
||||
|
||||
// Footer
|
||||
pkt.push(0x10);
|
||||
pkt.push(0x00);
|
||||
|
||||
let body_len = (pkt.len() - 3) as u16;
|
||||
pkt[1..3].copy_from_slice(&body_len.to_le_bytes());
|
||||
|
||||
// Rewrite
|
||||
let new_target = "coa-manager.duckdns.org:40005";
|
||||
let res = rewrite_realm_list_address(&pkt, new_target)
|
||||
.unwrap()
|
||||
.expect("rewritten");
|
||||
|
||||
assert_eq!(res[0], 0x10);
|
||||
let new_len = u16::from_le_bytes([res[1], res[2]]) as usize;
|
||||
assert_eq!(res.len(), 3 + new_len);
|
||||
|
||||
// Address in res must be new_target
|
||||
let old_addr = b"127.0.0.1:8085";
|
||||
let new_addr = b"coa-manager.duckdns.org:40005";
|
||||
assert!(!res.windows(old_addr.len()).any(|w| w == old_addr));
|
||||
assert!(res.windows(new_addr.len()).any(|w| w == new_addr));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_realm_list_ignored() {
|
||||
let pkt = vec![0x00, 0x01, 0x02];
|
||||
assert_eq!(rewrite_realm_list_address(&pkt, "test:8085"), Ok(None));
|
||||
}
|
||||
}
|
||||
@@ -1,266 +0,0 @@
|
||||
//! Adversarial, property and hostile-input fuzz tests for Phase 15.
|
||||
//! Verifies that malformed or malicious inputs never panic, integer operations
|
||||
//! do not wrap, allocations remain bounded, and invalid states fail closed.
|
||||
|
||||
use coa_control_proto::app::{
|
||||
decode_request, decode_response, encode_request, Request, MAX_TRANSFER_REQUEST_BYTES,
|
||||
};
|
||||
use coa_control_proto::coord::{
|
||||
parse_text, split_binary, Frame, ProbePayload, CONN_PREFIX, MAX_FRAME_BYTES,
|
||||
};
|
||||
use coa_control_proto::noise::{Initiator, Responder, CHUNK};
|
||||
use coa_control_proto::relay::{
|
||||
rewrite_realm_list_address, sign_relay_challenge, verify_relay_challenge, TunnelMsg,
|
||||
};
|
||||
use coa_registry_proto::sign::RealmId;
|
||||
use ed25519_dalek::SigningKey;
|
||||
use std::collections::BTreeMap;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[test]
|
||||
fn test_cmd_realm_list_rewriter_adversarial_and_bounds() {
|
||||
let dummy_target = "127.0.0.1:8085";
|
||||
|
||||
// 1. Truncated inputs
|
||||
assert!(rewrite_realm_list_address(&[], dummy_target)
|
||||
.unwrap()
|
||||
.is_none());
|
||||
assert!(rewrite_realm_list_address(&[0x10], dummy_target).is_err());
|
||||
assert!(rewrite_realm_list_address(&[0x10, 0x05, 0x00], dummy_target).is_err());
|
||||
assert!(
|
||||
rewrite_realm_list_address(&[0x10, 0x10, 0x00, 0, 0, 0, 0, 1, 0], dummy_target).is_err()
|
||||
);
|
||||
|
||||
// 2. Non-0x10 opcodes ignored safely
|
||||
assert!(rewrite_realm_list_address(&[0x00, 0x01], dummy_target)
|
||||
.unwrap()
|
||||
.is_none());
|
||||
assert!(rewrite_realm_list_address(&[0xFF; 20], dummy_target)
|
||||
.unwrap()
|
||||
.is_none());
|
||||
|
||||
// 3. Zero realms handled safely
|
||||
let zero_realms = [
|
||||
0x10, // Opcode
|
||||
0x06, 0x00, // Length: 6 bytes follow
|
||||
0x00, 0x00, 0x00, 0x00, // Unused
|
||||
0x00, 0x00, // Realm count: 0
|
||||
];
|
||||
let res = rewrite_realm_list_address(&zero_realms, dummy_target).unwrap();
|
||||
assert_eq!(res.unwrap(), zero_realms);
|
||||
|
||||
// 4. Missing null terminators in realm string
|
||||
let unterminated_name = [
|
||||
0x10, 0x20, 0x00, 0, 0, 0, 0, 1, 0, // Header (count = 1)
|
||||
1, 0, 0, // type, lock, flag
|
||||
b'R', b'e', b'a', b'l', b'm', // no null byte!
|
||||
];
|
||||
assert!(rewrite_realm_list_address(&unterminated_name, dummy_target).is_err());
|
||||
|
||||
// 5. Excessive new address (> 255 bytes) rejected
|
||||
let giant_addr = "a".repeat(256);
|
||||
let valid_packet = make_valid_realm_list_pkt("1.2.3.4:1234");
|
||||
assert!(rewrite_realm_list_address(&valid_packet, &giant_addr).is_err());
|
||||
|
||||
// 6. Valid rewrite preserves packet structure
|
||||
let rewritten = rewrite_realm_list_address(&valid_packet, "99.88.77.66:30000")
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(rewritten[0], 0x10);
|
||||
let s = String::from_utf8_lossy(&rewritten);
|
||||
assert!(s.contains("99.88.77.66:30000"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_app_request_hostile_limits_and_validation() {
|
||||
// 1. Oversized control request (> 4096 bytes)
|
||||
let big_client = "A".repeat(5000);
|
||||
let big_hello = format!(r#"{{"op":"hello","protocol":1,"client":"{big_client}"}}"#);
|
||||
assert!(decode_request(big_hello.as_bytes()).is_err());
|
||||
|
||||
// 2. Unknown fields rejected
|
||||
let extra_field = r#"{"op":"claim","token":1,"unknown_admin_flag":true}"#;
|
||||
assert!(decode_request(extra_field.as_bytes()).is_err());
|
||||
|
||||
// 3. Giant collection count in TransferOffer rejected
|
||||
let mut collections = BTreeMap::new();
|
||||
for i in 0..1500 {
|
||||
collections.insert(format!("cat_{i}"), vec![1, 2, 3]);
|
||||
}
|
||||
let offer = Request::TransferOffer {
|
||||
transfer_id: Uuid::now_v7(),
|
||||
character_id: Uuid::now_v7(),
|
||||
canonical_revision: 1,
|
||||
content_hash: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef".into(),
|
||||
total_size: 1024,
|
||||
collections,
|
||||
};
|
||||
let offer_bytes = encode_request(&offer);
|
||||
assert!(
|
||||
decode_request(&offer_bytes).is_err(),
|
||||
"offers with > 1000 categories must be rejected"
|
||||
);
|
||||
|
||||
// 4. Invalid content hash (not 64 hex chars)
|
||||
let bad_hash_offer = Request::TransferOffer {
|
||||
transfer_id: Uuid::now_v7(),
|
||||
character_id: Uuid::now_v7(),
|
||||
canonical_revision: 1,
|
||||
content_hash: "not_a_valid_hex_hash".into(),
|
||||
total_size: 1024,
|
||||
collections: BTreeMap::new(),
|
||||
};
|
||||
assert!(decode_request(&encode_request(&bad_hash_offer)).is_err());
|
||||
|
||||
// 5. TransferChunk offset out of bounds
|
||||
let bad_offset_chunk = Request::TransferChunk {
|
||||
transfer_id: Uuid::now_v7(),
|
||||
offset: MAX_TRANSFER_REQUEST_BYTES + 1,
|
||||
data: "abc".into(),
|
||||
};
|
||||
assert!(decode_request(&encode_request(&bad_offset_chunk)).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_coordinator_text_and_binary_adversarial() {
|
||||
// 1. Oversized text frame (> 2048 bytes)
|
||||
let giant_text = format!(
|
||||
r#"{{"t":"close","conn":1,"reason":"{}"}}"#,
|
||||
"x".repeat(3000)
|
||||
);
|
||||
assert!(parse_text(&giant_text).is_err());
|
||||
|
||||
// 2. Binary frame bounds
|
||||
assert!(split_binary(&[]).is_err());
|
||||
assert!(split_binary(&[0, 1, 2]).is_err()); // less than 4 bytes
|
||||
let max_allowed = vec![0u8; CONN_PREFIX + MAX_FRAME_BYTES];
|
||||
assert!(split_binary(&max_allowed).is_ok());
|
||||
let too_large = vec![0u8; CONN_PREFIX + MAX_FRAME_BYTES + 1];
|
||||
assert!(split_binary(&too_large).is_err());
|
||||
|
||||
// 3. ProbePayload unknown fields rejected
|
||||
let bad_probe = r#"{"ports":[8085],"admin":true}"#;
|
||||
assert!(serde_json::from_str::<ProbePayload>(bad_probe).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_noise_frame_reassembly_adversarial() {
|
||||
let (init, msg1) = Initiator::start().unwrap();
|
||||
let (resp, msg2) = Responder::start(&msg1).unwrap();
|
||||
let (msg3, mut client_chan) = init.finish(&msg2).unwrap();
|
||||
let mut server_chan = resp.finish(&msg3).unwrap();
|
||||
|
||||
// 1. Normal message seals and opens
|
||||
let frames = client_chan.seal(b"hello world").unwrap();
|
||||
assert_eq!(frames.len(), 1);
|
||||
let opened = server_chan.open_frame(&frames[0]).unwrap().unwrap();
|
||||
assert_eq!(opened, b"hello world");
|
||||
|
||||
// 2. Corrupted ciphertext ends/fails without panicking
|
||||
let mut corrupted = frames[0].clone();
|
||||
corrupted[10] ^= 0xFF;
|
||||
assert!(server_chan.open_frame(&corrupted).is_err());
|
||||
|
||||
// 3. Truncated frame (< 17 bytes)
|
||||
assert!(server_chan.open_frame(&[0u8; 16]).is_err());
|
||||
|
||||
// 4. Oversized frame (> CHUNK + 17)
|
||||
let giant_frame = vec![0u8; CHUNK + 18];
|
||||
assert!(server_chan.open_frame(&giant_frame).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_relay_protocol_adversarial() {
|
||||
let key = SigningKey::from_bytes(&[42u8; 32]);
|
||||
let realm_id = RealmId::new();
|
||||
let nonce = "challenge_nonce_12345";
|
||||
|
||||
let sig = sign_relay_challenge(&key, &realm_id, nonce);
|
||||
assert!(verify_relay_challenge(
|
||||
&key.verifying_key(),
|
||||
&realm_id,
|
||||
nonce,
|
||||
&sig
|
||||
));
|
||||
|
||||
// Tampered nonce
|
||||
assert!(!verify_relay_challenge(
|
||||
&key.verifying_key(),
|
||||
&realm_id,
|
||||
"different_nonce",
|
||||
&sig
|
||||
));
|
||||
// Tampered realm_id
|
||||
assert!(!verify_relay_challenge(
|
||||
&key.verifying_key(),
|
||||
&RealmId::new(),
|
||||
nonce,
|
||||
&sig
|
||||
));
|
||||
// Corrupted signature
|
||||
assert!(!verify_relay_challenge(
|
||||
&key.verifying_key(),
|
||||
&realm_id,
|
||||
nonce,
|
||||
"not_valid_hex"
|
||||
));
|
||||
|
||||
// TunnelMsg unknown fields rejected
|
||||
let bad_tunnel = r#"{"cmd":"close","stream_id":1,"extra":true}"#;
|
||||
assert!(serde_json::from_str::<TunnelMsg>(bad_tunnel).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fuzz_smoke_never_panics_on_arbitrary_bytes() {
|
||||
// Deterministic pseudo-random bytes
|
||||
let mut seed: u64 = 0xDEADBEEFCAFEBABE;
|
||||
let mut next_bytes = |len: usize| -> Vec<u8> {
|
||||
let mut v = Vec::with_capacity(len);
|
||||
for _ in 0..len {
|
||||
seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1);
|
||||
v.push((seed >> 33) as u8);
|
||||
}
|
||||
v
|
||||
};
|
||||
|
||||
for len in [0, 1, 2, 3, 4, 8, 16, 32, 64, 128, 256, 1024, 4096] {
|
||||
for _ in 0..50 {
|
||||
let sample = next_bytes(len);
|
||||
// Must not panic on any random input:
|
||||
let _ = decode_request(&sample);
|
||||
let _ = decode_response(&sample);
|
||||
let _ = split_binary(&sample);
|
||||
let _ = rewrite_realm_list_address(&sample, "127.0.0.1:8085");
|
||||
if let Ok(s) = std::str::from_utf8(&sample) {
|
||||
let _ = parse_text(s);
|
||||
let _ = serde_json::from_str::<TunnelMsg>(s);
|
||||
let _ = serde_json::from_str::<Frame>(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn make_valid_realm_list_pkt(addr: &str) -> Vec<u8> {
|
||||
let name = b"CoA Realm\0";
|
||||
let addr_bytes = format!("{addr}\0").into_bytes();
|
||||
let body_len = 4 + 2 + 3 + name.len() + addr_bytes.len() + 4 + 1 + 1 + 1 + 2;
|
||||
|
||||
let mut pkt = Vec::new();
|
||||
pkt.push(0x10); // Opcode
|
||||
pkt.extend_from_slice(&(body_len as u16).to_le_bytes()); // Length
|
||||
pkt.extend_from_slice(&0u32.to_le_bytes()); // Unused
|
||||
pkt.extend_from_slice(&1u16.to_le_bytes()); // Realm count = 1
|
||||
|
||||
// Realm info:
|
||||
pkt.extend_from_slice(&[1, 0, 0]); // type, lock, flag
|
||||
pkt.extend_from_slice(name);
|
||||
pkt.extend_from_slice(&addr_bytes);
|
||||
pkt.extend_from_slice(&[0, 0, 0, 0]); // pop
|
||||
pkt.extend_from_slice(&[0]); // chars
|
||||
pkt.extend_from_slice(&[1]); // timezone
|
||||
pkt.extend_from_slice(&[1]); // realm id
|
||||
|
||||
// Footer:
|
||||
pkt.extend_from_slice(&[0x10, 0x00]);
|
||||
pkt
|
||||
}
|
||||
@@ -10,7 +10,7 @@ serde_json = "1"
|
||||
sha2 = "0.10"
|
||||
hex = "0.4"
|
||||
thiserror = "2"
|
||||
uuid = { version = "1", features = ["v4", "v7", "serde"] }
|
||||
uuid = { version = "1", features = ["v4", "serde"] }
|
||||
chrono = { version = "0.4", default-features = false, features = ["clock", "serde", "std"] }
|
||||
dunce = "1"
|
||||
fs4 = "0.13"
|
||||
@@ -22,22 +22,17 @@ reqwest = { version = "0.12", default-features = false, features = ["blocking",
|
||||
tar = "0.4"
|
||||
num-bigint = "0.4"
|
||||
zip = { version = "2", default-features = false, features = ["deflate"] }
|
||||
flate2 = "1.0"
|
||||
sha1 = "0.10"
|
||||
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt", "std"] }
|
||||
tempfile = "3"
|
||||
rusqlite = { version = "0.40.2", features = ["bundled"] }
|
||||
if-addrs = "0.15"
|
||||
coa-registry-proto = { path = "../coa-registry-proto" }
|
||||
coa-control-proto = { path = "../coa-control-proto" }
|
||||
tungstenite = { version = "0.26", default-features = false, features = ["handshake", "native-tls"] }
|
||||
|
||||
[dev-dependencies]
|
||||
rand_core = { version = "0.6", features = ["getrandom"] }
|
||||
ed25519-dalek = { version = "2", features = ["rand_core"] }
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
windows-sys = { version = "0.60", features = ["Win32_Foundation", "Win32_Storage_FileSystem", "Win32_System_Threading", "Win32_System_SystemInformation", "Win32_System_Diagnostics_ToolHelp", "Win32_NetworkManagement_IpHelper", "Win32_Networking_WinSock", "Win32_Security_Cryptography", "Win32_UI_Shell", "Win32_System_Registry", "Win32_UI_WindowsAndMessaging"] }
|
||||
windows-sys = { version = "0.60", features = ["Win32_Foundation", "Win32_Storage_FileSystem", "Win32_System_Threading", "Win32_System_SystemInformation", "Win32_System_Diagnostics_ToolHelp", "Win32_NetworkManagement_IpHelper", "Win32_Networking_WinSock"] }
|
||||
|
||||
[features]
|
||||
testkit = []
|
||||
@@ -3,37 +3,15 @@ use coa_core::backup::{self, Kind, Trigger};
|
||||
use std::path::Path;
|
||||
|
||||
fn main() {
|
||||
let root = std::env::args()
|
||||
.nth(1)
|
||||
.expect("usage: backup_e2e <fixture folder>");
|
||||
assert!(
|
||||
root.to_lowercase().contains("fixture"),
|
||||
"refusing: not a fixture folder"
|
||||
);
|
||||
let root = std::env::args().nth(1).expect("usage: backup_e2e <fixture folder>");
|
||||
assert!(root.to_lowercase().contains("fixture"), "refusing: not a fixture folder");
|
||||
let root = Path::new(&root);
|
||||
let meta = coa_core::registry::metadata_dir_for(root).unwrap();
|
||||
std::fs::create_dir_all(&meta).unwrap();
|
||||
|
||||
let t = std::time::Instant::now();
|
||||
let point = backup::create(
|
||||
root,
|
||||
&meta,
|
||||
Kind::Quick,
|
||||
Trigger::Manual,
|
||||
Some("e2e".into()),
|
||||
&|s| println!(" .. {s}"),
|
||||
)
|
||||
.expect("backup");
|
||||
println!(
|
||||
"quick backup {} in {:?}: {:?}",
|
||||
point.id,
|
||||
t.elapsed(),
|
||||
point
|
||||
.components
|
||||
.iter()
|
||||
.map(|c| (&c.name, c.bytes, c.tables))
|
||||
.collect::<Vec<_>>()
|
||||
);
|
||||
let point = backup::create(root, &meta, Kind::Quick, Trigger::Manual, Some("e2e".into()), &|s| println!(" .. {s}")).expect("backup");
|
||||
println!("quick backup {} in {:?}: {:?}", point.id, t.elapsed(), point.components.iter().map(|c| (&c.name, c.bytes, c.tables)).collect::<Vec<_>>());
|
||||
assert!(backup::verify(&meta, &point.id).unwrap().ok);
|
||||
|
||||
// Change live state after the backup.
|
||||
@@ -45,44 +23,22 @@ fn main() {
|
||||
.unwrap();
|
||||
|
||||
let r = backup::restore_database(root, &meta, &point.id, "characters").expect("restore");
|
||||
println!(
|
||||
"restored {} tables; previous database kept as {}; safety backup {}",
|
||||
r.tables_restored, r.previous_schema, r.safety_backup
|
||||
);
|
||||
println!("restored {} tables; previous database kept as {}; safety backup {}", r.tables_restored, r.previous_schema, r.safety_backup);
|
||||
|
||||
backup::with_database(root, |db| {
|
||||
let live = db.tables("acore_characters")?;
|
||||
assert!(
|
||||
!live.contains(&"manager_marker".to_string()),
|
||||
"marker must be gone from the restored database"
|
||||
);
|
||||
assert!(!live.contains(&"manager_marker".to_string()), "marker must be gone from the restored database");
|
||||
let old = db.tables(&r.previous_schema)?;
|
||||
assert!(
|
||||
old.contains(&"manager_marker".to_string()),
|
||||
"the replaced database keeps the marker"
|
||||
);
|
||||
assert_eq!(
|
||||
db.query(&format!(
|
||||
"SELECT id FROM {}.manager_marker;",
|
||||
r.previous_schema
|
||||
))?,
|
||||
"42"
|
||||
);
|
||||
assert!(old.contains(&"manager_marker".to_string()), "the replaced database keeps the marker");
|
||||
assert_eq!(db.query(&format!("SELECT id FROM {}.manager_marker;", r.previous_schema))?, "42");
|
||||
assert_eq!(live.len(), r.tables_restored);
|
||||
println!(
|
||||
"OK: live tables {}, old schema tables {}",
|
||||
live.len(),
|
||||
old.len()
|
||||
);
|
||||
println!("OK: live tables {}, old schema tables {}", live.len(), old.len());
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
// A rejected restore (server "running" is simulated by a damaged backup) must change nothing.
|
||||
let victim = meta
|
||||
.join("backups")
|
||||
.join(&point.id)
|
||||
.join("characters.sql.zst");
|
||||
let victim = meta.join("backups").join(&point.id).join("characters.sql.zst");
|
||||
let mut bytes = std::fs::read(&victim).unwrap();
|
||||
bytes[10] ^= 0xFF;
|
||||
std::fs::write(&victim, bytes).unwrap();
|
||||
|
||||
@@ -7,69 +7,25 @@ use coa_core::download::{Cancel, HttpTransport};
|
||||
|
||||
fn main() {
|
||||
let full = clientdl::fetch_latest().expect("manifest");
|
||||
println!(
|
||||
"version {} · {} files · {:.1} GB",
|
||||
full.version,
|
||||
full.files.len(),
|
||||
full.total_bytes() as f64 / 1e9
|
||||
);
|
||||
println!("version {} · {} files · {:.1} GB", full.version, full.files.len(), full.total_bytes() as f64 / 1e9);
|
||||
let mut small = full.files.clone();
|
||||
small.sort_by_key(|f| f.size);
|
||||
let skip: usize = std::env::var("PROBE_SKIP")
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.unwrap_or(0);
|
||||
let skip: usize = std::env::var("PROBE_SKIP").ok().and_then(|v| v.parse().ok()).unwrap_or(0);
|
||||
let small: Vec<_> = small.into_iter().skip(skip).take(6).collect();
|
||||
let manifest = Manifest {
|
||||
version: full.version.clone(),
|
||||
published_at: None,
|
||||
files: small,
|
||||
};
|
||||
let manifest = Manifest { version: full.version.clone(), published_at: None, files: small };
|
||||
|
||||
let dir = std::env::temp_dir().join(format!("coa-client-probe-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let cancel = Cancel::default();
|
||||
let plan = clientdl::plan(&dir, &manifest, &Default::default(), &cancel, &|_| {}).unwrap();
|
||||
println!(
|
||||
"plan: {} files, {} bytes",
|
||||
plan.items.len(),
|
||||
plan.download_bytes
|
||||
);
|
||||
println!("plan: {} files, {} bytes", plan.items.len(), plan.download_bytes);
|
||||
let transport = HttpTransport::new().unwrap();
|
||||
clientdl::apply(
|
||||
&Apply {
|
||||
client: &dir,
|
||||
manifest: &manifest,
|
||||
plan: &plan,
|
||||
keep_modified: false,
|
||||
transport: &transport,
|
||||
objects_url: clientdl::OBJECTS_URL,
|
||||
cancel: &cancel,
|
||||
},
|
||||
&|s| {
|
||||
println!(
|
||||
"{} {}/{} {}",
|
||||
s.phase,
|
||||
s.done,
|
||||
s.total,
|
||||
s.file.unwrap_or_default()
|
||||
)
|
||||
},
|
||||
&Apply { client: &dir, manifest: &manifest, plan: &plan, keep_modified: false, transport: &transport, objects_url: clientdl::OBJECTS_URL, cancel: &cancel },
|
||||
&|s| println!("{} {}/{} {}", s.phase, s.done, s.total, s.file.unwrap_or_default()),
|
||||
)
|
||||
.expect("apply");
|
||||
let again = clientdl::plan(
|
||||
&dir,
|
||||
&manifest,
|
||||
&clientdl::load_state(&dir).unwrap(),
|
||||
&cancel,
|
||||
&|_| {},
|
||||
)
|
||||
.unwrap();
|
||||
println!(
|
||||
"second plan: {} to do, {} up to date, version {:?}",
|
||||
again.items.len(),
|
||||
again.up_to_date_files,
|
||||
clientdl::local(&dir).version
|
||||
);
|
||||
let again = clientdl::plan(&dir, &manifest, &clientdl::load_state(&dir).unwrap(), &cancel, &|_| {}).unwrap();
|
||||
println!("second plan: {} to do, {} up to date, version {:?}", again.items.len(), again.up_to_date_files, clientdl::local(&dir).version);
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
@@ -1,8 +1,4 @@
|
||||
fn main() {
|
||||
let p = std::env::args().nth(1).expect("client folder");
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&coa_core::client::detect(std::path::Path::new(&p), None))
|
||||
.unwrap()
|
||||
);
|
||||
println!("{}", serde_json::to_string_pretty(&coa_core::client::detect(std::path::Path::new(&p), None)).unwrap());
|
||||
}
|
||||
@@ -5,24 +5,12 @@ use coa_core::db::{Account, Db};
|
||||
fn main() {
|
||||
let a: Vec<String> = std::env::args().skip(1).collect();
|
||||
let root = std::path::PathBuf::from(&a[0]);
|
||||
assert!(
|
||||
a[0].to_lowercase().contains("installtest"),
|
||||
"refusing to touch a server that is not a disposable test install"
|
||||
);
|
||||
assert!(a[0].to_lowercase().contains("installtest"), "refusing to touch a server that is not a disposable test install");
|
||||
let count: u32 = a[1].parse().unwrap();
|
||||
let db = Db::from_repack(&root, Account::Admin).expect("db");
|
||||
println!(
|
||||
"templates before: {}",
|
||||
coa_core::companions::template_count(&db).unwrap()
|
||||
);
|
||||
println!(
|
||||
"added: {}",
|
||||
coa_core::companions::ensure_templates(&db).unwrap()
|
||||
);
|
||||
println!(
|
||||
"templates after: {}",
|
||||
coa_core::companions::template_count(&db).unwrap()
|
||||
);
|
||||
println!("templates before: {}", coa_core::companions::template_count(&db).unwrap());
|
||||
println!("added: {}", coa_core::companions::ensure_templates(&db).unwrap());
|
||||
println!("templates after: {}", coa_core::companions::template_count(&db).unwrap());
|
||||
let t = std::time::Instant::now();
|
||||
match coa_core::companions::offline_create(&root, &root.join("companions-e2e.log"), count) {
|
||||
Ok(tail) => println!("offline create ok in {:?}:\n{tail}", t.elapsed()),
|
||||
|
||||
@@ -3,16 +3,12 @@ use coa_core::config::{self, parser::ConfFile, Scope};
|
||||
use std::path::Path;
|
||||
|
||||
fn main() {
|
||||
let root = std::env::args()
|
||||
.nth(1)
|
||||
.expect("usage: config_check <server folder>");
|
||||
let root = std::env::args().nth(1).expect("usage: config_check <server folder>");
|
||||
let root = Path::new(&root);
|
||||
|
||||
let mut files = 0;
|
||||
for dir in ["Core/configs", "Core/configs/modules", "Settings"] {
|
||||
let Ok(rd) = std::fs::read_dir(root.join(dir)) else {
|
||||
continue;
|
||||
};
|
||||
let Ok(rd) = std::fs::read_dir(root.join(dir)) else { continue };
|
||||
for e in rd.flatten() {
|
||||
let p = e.path();
|
||||
let name = p.file_name().unwrap().to_string_lossy().to_lowercase();
|
||||
@@ -22,12 +18,7 @@ fn main() {
|
||||
let bytes = std::fs::read(&p).unwrap();
|
||||
match ConfFile::parse_bytes(&bytes) {
|
||||
Ok(c) => {
|
||||
assert_eq!(
|
||||
c.to_text().as_bytes(),
|
||||
&bytes[..],
|
||||
"round-trip mismatch: {}",
|
||||
p.display()
|
||||
);
|
||||
assert_eq!(c.to_text().as_bytes(), &bytes[..], "round-trip mismatch: {}", p.display());
|
||||
files += 1;
|
||||
}
|
||||
Err(e) => println!("skipped {}: {e}", p.display()),
|
||||
@@ -40,34 +31,16 @@ fn main() {
|
||||
match config::load(root, scope) {
|
||||
Ok(v) => {
|
||||
let present = v.settings.iter().filter(|s| s.present).count();
|
||||
let problems: Vec<_> = v
|
||||
.settings
|
||||
.iter()
|
||||
.filter(|s| s.problem.is_some())
|
||||
.map(|s| (&s.meta.key, s.problem.clone().unwrap()))
|
||||
.collect();
|
||||
let missing: Vec<_> = v
|
||||
.settings
|
||||
.iter()
|
||||
.filter(|s| !s.present)
|
||||
.map(|s| s.meta.key.as_str())
|
||||
.collect();
|
||||
let non_default = v
|
||||
.settings
|
||||
.iter()
|
||||
.filter(|s| s.present && !s.is_default)
|
||||
.count();
|
||||
let problems: Vec<_> = v.settings.iter().filter(|s| s.problem.is_some()).map(|s| (&s.meta.key, s.problem.clone().unwrap())).collect();
|
||||
let missing: Vec<_> = v.settings.iter().filter(|s| !s.present).map(|s| s.meta.key.as_str()).collect();
|
||||
let non_default = v.settings.iter().filter(|s| s.present && !s.is_default).count();
|
||||
println!(
|
||||
"{scope:?}: {} settings, {present} present, {non_default} non-default, {} unknown keys, drift {:?}\n problems {:?}\n missing {:?}",
|
||||
v.settings.len(), v.unknown_keys, v.drift_keys, problems, missing
|
||||
);
|
||||
for p in scope.presets() {
|
||||
let pv = config::preview_preset(root, scope, &p.id).unwrap();
|
||||
println!(
|
||||
" preset {:<14} would change {} settings",
|
||||
p.id,
|
||||
pv.changes.len()
|
||||
);
|
||||
println!(" preset {:<14} would change {} settings", p.id, pv.changes.len());
|
||||
}
|
||||
}
|
||||
Err(e) => println!("{scope:?}: {e}"),
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
use coa_core::{
|
||||
manifest::{Kind, Manifest},
|
||||
package::{self, BuildOptions},
|
||||
pkgsource::{self, Source},
|
||||
release, signing, Result,
|
||||
};
|
||||
use std::{fs, path::PathBuf};
|
||||
use coa_core::{manifest::{Kind, Manifest}, package::{self, BuildOptions}, pkgsource::{self, Source}, release, signing, Result};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
@@ -14,61 +9,30 @@ fn main() -> Result<()> {
|
||||
if args[0] == "extract" {
|
||||
assert!(!tree.exists(), "Use a new release tree");
|
||||
package::extract(&PathBuf::from(&args[1]), &previous, &tree, &|_, _| {})?;
|
||||
println!(
|
||||
"Verified and extracted {}: {} historical migrations",
|
||||
previous.version,
|
||||
previous.migrations.len()
|
||||
);
|
||||
println!("Verified and extracted {}: {} historical migrations", previous.version, previous.migrations.len());
|
||||
return Ok(());
|
||||
}
|
||||
assert_eq!(args[0], "pack");
|
||||
let out = PathBuf::from(&args[3]);
|
||||
assert!(!out.exists(), "Release addresses must be immutable");
|
||||
for migration in &previous.migrations {
|
||||
let entry = previous
|
||||
.files
|
||||
.iter()
|
||||
.find(|file| file.path.starts_with("_migrations/") && file.sha256 == migration.sha256)
|
||||
.expect("Historical SQL entry");
|
||||
assert_eq!(
|
||||
coa_core::fsx::sha256_file(&tree.join(&entry.path))?,
|
||||
entry.sha256,
|
||||
"Historical SQL changed: {}",
|
||||
migration.id
|
||||
);
|
||||
let entry = previous.files.iter().find(|file| file.path.starts_with("_migrations/") && file.sha256 == migration.sha256).expect("Historical SQL entry");
|
||||
assert_eq!(coa_core::fsx::sha256_file(&tree.join(&entry.path))?, entry.sha256, "Historical SQL changed: {}", migration.id);
|
||||
}
|
||||
let mut manifest = package::build(
|
||||
&tree,
|
||||
&out,
|
||||
&BuildOptions {
|
||||
kind: Kind::Update,
|
||||
version: args[4].clone(),
|
||||
core_commit: Some(args[5].clone()),
|
||||
bots_commit: previous.bots.and_then(|revision| revision.commit),
|
||||
built_at: chrono::Utc::now().to_rfc3339(),
|
||||
part_size: package::DEFAULT_PART_SIZE,
|
||||
migrations: previous.migrations.clone(),
|
||||
},
|
||||
&|line| println!("{line}"),
|
||||
)?;
|
||||
let mut manifest = package::build(&tree, &out, &BuildOptions {
|
||||
kind: Kind::Update, version: args[4].clone(), core_commit: Some(args[5].clone()),
|
||||
bots_commit: previous.bots.and_then(|revision| revision.commit),
|
||||
built_at: chrono::Utc::now().to_rfc3339(), part_size: package::DEFAULT_PART_SIZE,
|
||||
migrations: previous.migrations.clone(),
|
||||
}, &|line| println!("{line}"))?;
|
||||
manifest.min_manager_version = "0.6.5".into();
|
||||
manifest.validate()?;
|
||||
coa_core::fsx::atomic_write(
|
||||
&out.join("manifest.json"),
|
||||
&serde_json::to_vec_pretty(&manifest)?,
|
||||
)?;
|
||||
let key_path = PathBuf::from(std::env::var("USERPROFILE").unwrap())
|
||||
.join(".coa-manager/signing/manifest-signing.key");
|
||||
coa_core::fsx::atomic_write(&out.join("manifest.json"), &serde_json::to_vec_pretty(&manifest)?)?;
|
||||
let key_path = PathBuf::from(std::env::var("USERPROFILE").unwrap()).join(".coa-manager/signing/manifest-signing.key");
|
||||
release::sign_manifest(&out, &fs::read_to_string(key_path)?)?;
|
||||
coa_core::release_schema::verify_package(&out, signing::EMBEDDED_PUBLIC_KEY)?;
|
||||
let reread: Manifest = serde_json::from_slice(&fs::read(out.join("manifest.json"))?)?;
|
||||
assert_eq!(
|
||||
serde_json::to_value(reread.migrations)?,
|
||||
serde_json::to_value(previous.migrations)?
|
||||
);
|
||||
println!(
|
||||
"PASS: signed {}; all historical migration bytes, checksums, aliases and order preserved",
|
||||
manifest.version
|
||||
);
|
||||
assert_eq!(serde_json::to_value(reread.migrations)?, serde_json::to_value(previous.migrations)?);
|
||||
println!("PASS: signed {}; all historical migration bytes, checksums, aliases and order preserved", manifest.version);
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,131 +1,64 @@
|
||||
use coa_core::{backup, driver, fsx, Result};
|
||||
use std::path::{Path, PathBuf};
|
||||
use coa_core::{backup, driver, fsx, Result};
|
||||
|
||||
fn set(root: &Path, filename: &str, values: &[(&str, &str)]) -> Result<()> {
|
||||
let path = root.join("Core/configs/modules").join(filename);
|
||||
let mut config = coa_core::config::parser::ConfFile::parse_bytes(&std::fs::read(&path)?)?;
|
||||
for (key, value) in values {
|
||||
config.set(key, value, &[]);
|
||||
}
|
||||
for (key, value) in values { config.set(key, value, &[]); }
|
||||
fsx::atomic_write(&path, config.to_text().as_bytes())
|
||||
}
|
||||
|
||||
fn ledgers(root: &Path) -> Result<Vec<String>> {
|
||||
backup::with_database(root, |db| {
|
||||
["acore_playerbots", "acore_world", "acore_characters"]
|
||||
.into_iter()
|
||||
.map(|schema| {
|
||||
db.query(&format!(
|
||||
"SELECT path,sha256 FROM `{schema}`.coa_squid_migrations ORDER BY path;"
|
||||
))
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
backup::with_database(root, |db| ["acore_playerbots", "acore_world", "acore_characters"].into_iter()
|
||||
.map(|schema| db.query(&format!("SELECT path,sha256 FROM `{schema}`.coa_squid_migrations ORDER BY path;"))).collect())
|
||||
}
|
||||
|
||||
fn exercise(root: &Path) -> Result<()> {
|
||||
let meta = coa_core::registry::metadata_dir_for(root)?;
|
||||
let point = backup::create(
|
||||
root,
|
||||
&meta,
|
||||
backup::Kind::Full,
|
||||
backup::Trigger::Manual,
|
||||
Some("Disposable SQUID acceptance".into()),
|
||||
&|stage| println!("{stage}"),
|
||||
)?;
|
||||
let point = backup::create(root, &meta, backup::Kind::Full, backup::Trigger::Manual, Some("Disposable SQUID acceptance".into()), &|stage| println!("{stage}"))?;
|
||||
assert!(backup::verify(&meta, &point.id)?.ok);
|
||||
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> Result<()> {
|
||||
backup::with_database(root, |db| {
|
||||
db.query("DROP DATABASE IF EXISTS acore_playerbots;")
|
||||
.map(|_| ())
|
||||
})?;
|
||||
assert!(
|
||||
driver::run(root, driver::Verb::StartAll)?.ok,
|
||||
"Disabled SQUID must start without its database"
|
||||
);
|
||||
backup::with_database(root, |db| db.query("DROP DATABASE IF EXISTS acore_playerbots;").map(|_| ()))?;
|
||||
assert!(driver::run(root, driver::Verb::StartAll)?.ok, "Disabled SQUID must start without its database");
|
||||
assert!(driver::run(root, driver::Verb::StopAll)?.ok);
|
||||
coa_core::multiworld::set_enabled(root, false)?;
|
||||
coa_core::realms::select(root, coa_core::realms::Mode::Coa)?;
|
||||
set(root, "mod_coa_playerbots.conf", &[("CoaBots.Enable", "0")])?;
|
||||
set(
|
||||
root,
|
||||
"playerbots.conf",
|
||||
&[
|
||||
("AiPlayerbot.Enabled", "1"),
|
||||
("AiPlayerbot.RandomBotAutologin", "0"),
|
||||
("AiPlayerbot.MinRandomBots", "0"),
|
||||
("AiPlayerbot.MaxRandomBots", "0"),
|
||||
("AiPlayerbot.RandomBotGuildCount", "0"),
|
||||
("AiPlayerbot.RandomBotAccountCount", "0"),
|
||||
],
|
||||
)?;
|
||||
set(root, "playerbots.conf", &[("AiPlayerbot.Enabled", "1"), ("AiPlayerbot.RandomBotAutologin", "0"), ("AiPlayerbot.MinRandomBots", "0"), ("AiPlayerbot.MaxRandomBots", "0"), ("AiPlayerbot.RandomBotGuildCount", "0"), ("AiPlayerbot.RandomBotAccountCount", "0")])?;
|
||||
let started = driver::run(root, driver::Verb::StartAll)?;
|
||||
assert!(
|
||||
started.ok,
|
||||
"Fresh SQUID startup: {}",
|
||||
coa_core::diag::redact(&started.output)
|
||||
);
|
||||
assert!(started.ok, "Fresh SQUID startup: {}", coa_core::diag::redact(&started.output));
|
||||
let before = ledgers(root)?;
|
||||
assert!(!before[0].is_empty(), "Fresh SQUID history missing");
|
||||
assert!(driver::run(root, driver::Verb::StopAll)?.ok);
|
||||
let started = driver::run(root, driver::Verb::StartAll)?;
|
||||
assert!(
|
||||
started.ok,
|
||||
"Repeated SQUID startup: {}",
|
||||
coa_core::diag::redact(&started.output)
|
||||
);
|
||||
assert_eq!(
|
||||
ledgers(root)?,
|
||||
before,
|
||||
"SQUID replay changed its migration history"
|
||||
);
|
||||
assert!(started.ok, "Repeated SQUID startup: {}", coa_core::diag::redact(&started.output));
|
||||
assert_eq!(ledgers(root)?, before, "SQUID replay changed its migration history");
|
||||
println!("PASS: SQUID-disabled dual startup without its database; fresh enabled SQUID import and startup; repeated startup preserves all three histories");
|
||||
Ok(())
|
||||
}));
|
||||
assert!(driver::run(root, driver::Verb::StopAll)?.ok);
|
||||
restore(root, &meta, &point)?;
|
||||
result.map_err(|_| {
|
||||
coa_core::Error::Invalid(
|
||||
"SQUID acceptance failed; disposable databases and configurations restored".into(),
|
||||
)
|
||||
})?
|
||||
result.map_err(|_| coa_core::Error::Invalid("SQUID acceptance failed; disposable databases and configurations restored".into()))?
|
||||
}
|
||||
|
||||
fn restore(root: &Path, meta: &Path, point: &backup::RecoveryPoint) -> Result<()> {
|
||||
assert!(backup::verify(meta, &point.id)?.ok);
|
||||
assert!(driver::run(root, driver::Verb::StopAll)?.ok);
|
||||
coa_core::realms::select(root, point.realm)?;
|
||||
for component in point
|
||||
.components
|
||||
.iter()
|
||||
.filter(|component| component.name != "configs")
|
||||
{
|
||||
for component in point.components.iter().filter(|component| component.name != "configs") {
|
||||
backup::restore_database(root, meta, &point.id, &component.name)?;
|
||||
}
|
||||
backup::restore_configs(root, meta, &point.id)?;
|
||||
assert!(driver::run(root, driver::Verb::StopAll)?.ok);
|
||||
println!(
|
||||
"PASS: disposable SQUID acceptance databases and original realm configurations restored"
|
||||
);
|
||||
println!("PASS: disposable SQUID acceptance databases and original realm configurations restored");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let root = PathBuf::from(std::env::args().nth(1).unwrap());
|
||||
assert!(root
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.starts_with("coa-schema-fixture-chaos-064-transition-"));
|
||||
assert_eq!(
|
||||
std::fs::read(root.join(".transition-fixture"))?,
|
||||
b"disposable published-064 transition fixture"
|
||||
);
|
||||
assert!(!root
|
||||
.join("Data")
|
||||
.symlink_metadata()?
|
||||
.file_type()
|
||||
.is_symlink());
|
||||
assert!(root.file_name().unwrap().to_string_lossy().starts_with("coa-schema-fixture-chaos-064-transition-"));
|
||||
assert_eq!(std::fs::read(root.join(".transition-fixture"))?, b"disposable published-064 transition fixture");
|
||||
assert!(!root.join("Data").symlink_metadata()?.file_type().is_symlink());
|
||||
assert!(driver::run(&root, driver::Verb::StopAll)?.ok);
|
||||
if let Some(id) = std::env::args().nth(2) {
|
||||
let meta = coa_core::registry::metadata_dir_for(&root)?;
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
use coa_core::{
|
||||
backup, driver,
|
||||
registry::MetaDir,
|
||||
update::{self, RepackEnv},
|
||||
Result,
|
||||
};
|
||||
use std::{collections::BTreeMap, path::PathBuf};
|
||||
use coa_core::{backup, driver, registry::MetaDir, update::{self, RepackEnv}, Result};
|
||||
|
||||
fn players(db: &coa_core::db::Db) -> Result<String> {
|
||||
db.query("SELECT CONCAT(id,':',username,':',HEX(salt),':',HEX(verifier)) FROM acore_auth.account WHERE id=900001; SELECT CONCAT(guid,':',account,':',name,':',level,':',money) FROM acore_characters.characters WHERE guid=900001; SELECT CONCAT(guid,':',itemEntry,':',owner_guid,':',enchantments) FROM acore_characters.item_instance WHERE guid=900001; SELECT CONCAT(guid,':',bag,':',slot,':',item) FROM acore_characters.character_inventory WHERE guid=900001;")
|
||||
@@ -13,73 +8,22 @@ fn players(db: &coa_core::db::Db) -> Result<String> {
|
||||
fn exercise(root: &std::path::Path, package: &str) -> Result<()> {
|
||||
let meta_dir = coa_core::registry::metadata_dir_for(root)?;
|
||||
let (_, meta) = MetaDir::open(&meta_dir)?;
|
||||
let expected = backup::with_database(root, |db| {
|
||||
[
|
||||
coa_core::realms::Mode::Coa,
|
||||
coa_core::realms::Mode::Wildcard,
|
||||
]
|
||||
.into_iter()
|
||||
.map(|mode| players(&db.clone().for_realm(mode)))
|
||||
.collect::<Result<Vec<_>>>()
|
||||
})?;
|
||||
let expected = backup::with_database(root, |db| [coa_core::realms::Mode::Coa, coa_core::realms::Mode::Wildcard].into_iter().map(|mode| players(&db.clone().for_realm(mode))).collect::<Result<Vec<_>>>())?;
|
||||
assert!(driver::run(root, driver::Verb::StopAll)?.ok);
|
||||
let source = coa_core::pkgsource::Source::Dir(package.into());
|
||||
let preview = update::preview(
|
||||
root,
|
||||
&meta,
|
||||
&source,
|
||||
coa_core::signing::EMBEDDED_PUBLIC_KEY,
|
||||
&BTreeMap::new(),
|
||||
)?;
|
||||
assert_eq!(
|
||||
preview.pending_migrations, 0,
|
||||
"Historical SQL must not replay"
|
||||
);
|
||||
assert!(
|
||||
!preview
|
||||
.items
|
||||
.iter()
|
||||
.any(|item| item.action == update::Action::Conflict),
|
||||
"Unresolved candidate files: {:?}",
|
||||
preview.items
|
||||
);
|
||||
let environment = RepackEnv {
|
||||
root,
|
||||
meta_dir: &meta_dir,
|
||||
};
|
||||
let outcome = update::apply(
|
||||
&update::Params {
|
||||
root,
|
||||
meta_dir: &meta_dir,
|
||||
source,
|
||||
trusted_key: coa_core::signing::EMBEDDED_PUBLIC_KEY,
|
||||
resolutions: BTreeMap::new(),
|
||||
cancel: Default::default(),
|
||||
env: &environment,
|
||||
fail_after_ops: None,
|
||||
},
|
||||
&|stage, progress| println!("{progress}% {stage}"),
|
||||
)?;
|
||||
assert_eq!(
|
||||
outcome.txn.state,
|
||||
update::State::Committed,
|
||||
"{:?}",
|
||||
outcome.txn
|
||||
);
|
||||
let preview = update::preview(root, &meta, &source, coa_core::signing::EMBEDDED_PUBLIC_KEY, &BTreeMap::new())?;
|
||||
assert_eq!(preview.pending_migrations, 0, "Historical SQL must not replay");
|
||||
assert!(!preview.items.iter().any(|item| item.action == update::Action::Conflict), "Unresolved candidate files: {:?}", preview.items);
|
||||
let environment = RepackEnv { root, meta_dir: &meta_dir };
|
||||
let outcome = update::apply(&update::Params {
|
||||
root, meta_dir: &meta_dir, source, trusted_key: coa_core::signing::EMBEDDED_PUBLIC_KEY,
|
||||
resolutions: BTreeMap::new(), cancel: Default::default(), env: &environment, fail_after_ops: None,
|
||||
}, &|stage, progress| println!("{progress}% {stage}"))?;
|
||||
assert_eq!(outcome.txn.state, update::State::Committed, "{:?}", outcome.txn);
|
||||
backup::with_database(root, |db| {
|
||||
for (index, mode) in [
|
||||
coa_core::realms::Mode::Coa,
|
||||
coa_core::realms::Mode::Wildcard,
|
||||
]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
for (index, mode) in [coa_core::realms::Mode::Coa, coa_core::realms::Mode::Wildcard].into_iter().enumerate() {
|
||||
let realm = db.clone().for_realm(mode);
|
||||
assert_eq!(
|
||||
players(&realm)?,
|
||||
expected[index],
|
||||
"Player data changed in {mode:?}"
|
||||
);
|
||||
assert_eq!(players(&realm)?, expected[index], "Player data changed in {mode:?}");
|
||||
let problems = coa_core::schema_check::check(&realm, root)?;
|
||||
assert!(problems.is_empty(), "{problems:?}");
|
||||
}
|
||||
@@ -89,22 +33,9 @@ fn exercise(root: &std::path::Path, package: &str) -> Result<()> {
|
||||
println!("SQUID metadata: {release:?}");
|
||||
assert_eq!(coa_core::squid::fields(root)?.len(), 40);
|
||||
let (_, installed) = MetaDir::open(&meta_dir)?;
|
||||
let repeated = update::preview(
|
||||
root,
|
||||
&installed,
|
||||
&coa_core::pkgsource::Source::Dir(package.into()),
|
||||
coa_core::signing::EMBEDDED_PUBLIC_KEY,
|
||||
&BTreeMap::new(),
|
||||
)?;
|
||||
let repeated = update::preview(root, &installed, &coa_core::pkgsource::Source::Dir(package.into()), coa_core::signing::EMBEDDED_PUBLIC_KEY, &BTreeMap::new())?;
|
||||
assert_eq!(repeated.pending_migrations, 0);
|
||||
assert!(
|
||||
repeated
|
||||
.items
|
||||
.iter()
|
||||
.all(|item| item.action == update::Action::Skip),
|
||||
"{:?}",
|
||||
repeated.items
|
||||
);
|
||||
assert!(repeated.items.iter().all(|item| item.action == update::Action::Skip), "{:?}", repeated.items);
|
||||
println!("PASS: coordinated update committed, both realms started, player fields preserved, historical SQL skipped, repeated preview clean");
|
||||
Ok(())
|
||||
}
|
||||
@@ -112,30 +43,12 @@ fn exercise(root: &std::path::Path, package: &str) -> Result<()> {
|
||||
fn main() -> Result<()> {
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
let root = PathBuf::from(&args[0]);
|
||||
assert!(root
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.starts_with("coa-schema-fixture-chaos-064-transition-"));
|
||||
assert_eq!(
|
||||
std::fs::read(root.join(".transition-fixture"))?,
|
||||
b"disposable published-064 transition fixture"
|
||||
);
|
||||
assert_eq!(
|
||||
std::fs::read(root.join(".release-schema-fixture"))?,
|
||||
b"disposable release-schema fixture"
|
||||
);
|
||||
assert!(!root
|
||||
.join("Data")
|
||||
.symlink_metadata()?
|
||||
.file_type()
|
||||
.is_symlink());
|
||||
assert!(root.file_name().unwrap().to_string_lossy().starts_with("coa-schema-fixture-chaos-064-transition-"));
|
||||
assert_eq!(std::fs::read(root.join(".transition-fixture"))?, b"disposable published-064 transition fixture");
|
||||
assert_eq!(std::fs::read(root.join(".release-schema-fixture"))?, b"disposable release-schema fixture");
|
||||
assert!(!root.join("Data").symlink_metadata()?.file_type().is_symlink());
|
||||
assert!(driver::run(&root, driver::Verb::StopAll)?.ok);
|
||||
let outcome =
|
||||
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| exercise(&root, &args[1])));
|
||||
assert!(
|
||||
driver::run(&root, driver::Verb::StopAll)?.ok,
|
||||
"Fixture shutdown failed"
|
||||
);
|
||||
let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| exercise(&root, &args[1])));
|
||||
assert!(driver::run(&root, driver::Verb::StopAll)?.ok, "Fixture shutdown failed");
|
||||
outcome.map_err(|_| coa_core::Error::Invalid("Disposable coordinated acceptance failed; stopped services and retained recovery copies".into()))?
|
||||
}
|
||||
@@ -1,14 +1,7 @@
|
||||
fn main() {
|
||||
let a: Vec<String> = std::env::args().skip(1).collect();
|
||||
let job = coa_core::download::Job {
|
||||
url: a[0].clone(),
|
||||
dest: a[1].clone().into(),
|
||||
sha256: a[2].clone(),
|
||||
size: a[3].parse().unwrap(),
|
||||
};
|
||||
let job = coa_core::download::Job { url: a[0].clone(), dest: a[1].clone().into(), sha256: a[2].clone(), size: a[3].parse().unwrap() };
|
||||
let t = std::time::Instant::now();
|
||||
let r = coa_core::download::fetch(&job, &Default::default(), &|p| {
|
||||
eprintln!("{}/{}", p.downloaded, p.total)
|
||||
});
|
||||
let r = coa_core::download::fetch(&job, &Default::default(), &|p| eprintln!("{}/{}", p.downloaded, p.total));
|
||||
println!("{r:?} in {:?}", t.elapsed());
|
||||
}
|
||||
@@ -9,35 +9,13 @@ use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
let a: Vec<String> = std::env::args().skip(1).collect();
|
||||
assert!(
|
||||
a.len() == 4,
|
||||
"usage: docker_install <package folder> <destination> <game data folder> <public key>"
|
||||
);
|
||||
assert!(a.len() == 4, "usage: docker_install <package folder> <destination> <game data folder> <public key>");
|
||||
let registry = Registry::at(std::env::temp_dir().join("coa-docker-install/installs.json"));
|
||||
let t = std::time::Instant::now();
|
||||
let done = install(
|
||||
&Params {
|
||||
source: Source::Dir(PathBuf::from(&a[0])),
|
||||
dest: PathBuf::from(&a[1]),
|
||||
data_dir: PathBuf::from(&a[2]),
|
||||
trusted_key: &a[3],
|
||||
registry: ®istry,
|
||||
cancel: Cancel::default(),
|
||||
},
|
||||
&|s| {
|
||||
eprintln!(
|
||||
" {:>3}% {} {}",
|
||||
s.percent,
|
||||
s.step,
|
||||
s.detail.unwrap_or_default()
|
||||
)
|
||||
},
|
||||
&Params { source: Source::Dir(PathBuf::from(&a[0])), dest: PathBuf::from(&a[1]), data_dir: PathBuf::from(&a[2]), trusted_key: &a[3], registry: ®istry, cancel: Cancel::default() },
|
||||
&|s| eprintln!(" {:>3}% {} {}", s.percent, s.step, s.detail.unwrap_or_default()),
|
||||
)
|
||||
.unwrap_or_else(|e| panic!("install failed: {e}"));
|
||||
println!(
|
||||
"installed {} at {} in {:?}",
|
||||
done.version,
|
||||
done.path,
|
||||
t.elapsed()
|
||||
);
|
||||
println!("installed {} at {} in {:?}", done.version, done.path, t.elapsed());
|
||||
}
|
||||
@@ -2,9 +2,7 @@ use std::path::Path;
|
||||
|
||||
fn main() {
|
||||
let mut args = std::env::args().skip(1);
|
||||
let root = args
|
||||
.next()
|
||||
.expect("usage: drive <server folder> <start|stop>");
|
||||
let root = args.next().expect("usage: drive <server folder> <start|stop>");
|
||||
let verb = match args.next().as_deref() {
|
||||
Some("start") => coa_core::driver::Verb::StartAll,
|
||||
Some("mysql") => coa_core::driver::Verb::StartMysql,
|
||||
|
||||
@@ -1,27 +1,11 @@
|
||||
//! Verify published checksum compatibility on a disposable real database, without running SQL migrations.
|
||||
use coa_core::{
|
||||
backup,
|
||||
db::Db,
|
||||
migrations::{self, LedgerRow, Status, Store},
|
||||
pkgsource::{self, Source},
|
||||
Result,
|
||||
};
|
||||
use std::{
|
||||
path::{Path, PathBuf},
|
||||
sync::atomic::{AtomicUsize, Ordering},
|
||||
};
|
||||
use std::{path::{Path, PathBuf}, sync::atomic::{AtomicUsize, Ordering}};
|
||||
use coa_core::{backup, db::Db, migrations::{self, LedgerRow, Status, Store}, pkgsource::{self, Source}, Result};
|
||||
|
||||
struct Observed<'a> {
|
||||
db: &'a Db,
|
||||
executions: AtomicUsize,
|
||||
}
|
||||
struct Observed<'a> { db: &'a Db, executions: AtomicUsize }
|
||||
impl Store for Observed<'_> {
|
||||
fn load(&self) -> Result<Vec<LedgerRow>> {
|
||||
self.db.load()
|
||||
}
|
||||
fn put(&self, row: &LedgerRow) -> Result<()> {
|
||||
self.db.put(row)
|
||||
}
|
||||
fn load(&self) -> Result<Vec<LedgerRow>> { self.db.load() }
|
||||
fn put(&self, row: &LedgerRow) -> Result<()> { self.db.put(row) }
|
||||
fn run_file(&self, _: &str, _: &Path) -> Result<()> {
|
||||
self.executions.fetch_add(1, Ordering::SeqCst);
|
||||
panic!("Historical SQL must never be replayed");
|
||||
@@ -31,149 +15,62 @@ impl Store for Observed<'_> {
|
||||
fn main() -> Result<()> {
|
||||
let args: Vec<_> = std::env::args().skip(1).collect();
|
||||
let root = PathBuf::from(&args[0]);
|
||||
assert!(root
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.starts_with("coa-schema-fixture-chaos-"));
|
||||
assert_eq!(
|
||||
std::fs::read(root.join(".coa-chaos-fixture"))?,
|
||||
b"disposable update chaos fixture"
|
||||
);
|
||||
assert_eq!(
|
||||
std::fs::read(root.join(".release-schema-fixture"))?,
|
||||
b"disposable release-schema fixture"
|
||||
);
|
||||
assert!(root.file_name().unwrap().to_string_lossy().starts_with("coa-schema-fixture-chaos-"));
|
||||
assert_eq!(std::fs::read(root.join(".coa-chaos-fixture"))?, b"disposable update chaos fixture");
|
||||
assert_eq!(std::fs::read(root.join(".release-schema-fixture"))?, b"disposable release-schema fixture");
|
||||
let source = Source::Dir(PathBuf::from(&args[1]));
|
||||
let package = pkgsource::fetch_manifest(&source, coa_core::signing::EMBEDDED_PUBLIC_KEY)?.0;
|
||||
let staging = tempfile::tempdir()?;
|
||||
let parts = pkgsource::fetch_parts(
|
||||
&source,
|
||||
&package,
|
||||
&staging.path().join("download"),
|
||||
&Default::default(),
|
||||
&|_, _| {},
|
||||
)?;
|
||||
let parts = pkgsource::fetch_parts(&source, &package, &staging.path().join("download"), &Default::default(), &|_, _| {})?;
|
||||
coa_core::package::extract(&parts, &package, &staging.path().join("tree"), &|_, _| {})?;
|
||||
migrations::verify_files(
|
||||
&package.migrations,
|
||||
&staging.path().join("tree/_migrations"),
|
||||
)?;
|
||||
println!(
|
||||
"PASS: all {} signed-package SQL artifacts verified before any database mutation",
|
||||
package.migrations.len()
|
||||
);
|
||||
let migration = package
|
||||
.migrations
|
||||
.iter()
|
||||
.find(|m| m.id == "manager_repair__20261005_missing_wildcard_tables")
|
||||
.unwrap();
|
||||
let hashes = [
|
||||
"c1d8dbf2271234283a48a39e4b4aea1106b83b70581f1e3c4aa9fff438e8e4d2",
|
||||
"9e4a36245c3f83255415c122c01f08ceb3e3b83898d3d427ac4e2fbcf61e9638",
|
||||
];
|
||||
for hash in hashes {
|
||||
assert!(migration.sha256 == hash || migration.compatible_sha256.iter().any(|h| h == hash));
|
||||
}
|
||||
migrations::verify_files(&package.migrations, &staging.path().join("tree/_migrations"))?;
|
||||
println!("PASS: all {} signed-package SQL artifacts verified before any database mutation", package.migrations.len());
|
||||
let migration = package.migrations.iter().find(|m| m.id == "manager_repair__20261005_missing_wildcard_tables").unwrap();
|
||||
let hashes = ["c1d8dbf2271234283a48a39e4b4aea1106b83b70581f1e3c4aa9fff438e8e4d2", "9e4a36245c3f83255415c122c01f08ceb3e3b83898d3d427ac4e2fbcf61e9638"];
|
||||
for hash in hashes { assert!(migration.sha256 == hash || migration.compatible_sha256.iter().any(|h| h == hash)); }
|
||||
let fixture_meta = coa_core::registry::metadata_dir_for(&root)?;
|
||||
let test_meta = tempfile::tempdir()?;
|
||||
coa_core::fsx::atomic_write(
|
||||
&test_meta.path().join("install.json"),
|
||||
&std::fs::read(fixture_meta.join("install.json"))?,
|
||||
)?;
|
||||
coa_core::fsx::atomic_write(&test_meta.path().join("install.json"), &std::fs::read(fixture_meta.join("install.json"))?)?;
|
||||
coa_core::registry::MetaDir::open(test_meta.path())?;
|
||||
std::fs::create_dir_all(test_meta.path().join("updates"))?;
|
||||
let result = backup::with_database(&root, |db| {
|
||||
let mut modes = vec![coa_core::realms::Mode::Coa];
|
||||
if coa_core::realms::state(&root)?.wildcard_created {
|
||||
modes.push(coa_core::realms::Mode::Wildcard);
|
||||
}
|
||||
if coa_core::realms::state(&root)?.wildcard_created { modes.push(coa_core::realms::Mode::Wildcard); }
|
||||
for mode in modes {
|
||||
let db = db.clone().for_realm(mode);
|
||||
let saved = db
|
||||
.load()?
|
||||
.into_iter()
|
||||
.find(|row| row.id == migration.id && row.db == migration.db)
|
||||
.expect("Published repair must already be applied");
|
||||
let saved = db.load()?.into_iter().find(|row| row.id == migration.id && row.db == migration.db).expect("Published repair must already be applied");
|
||||
assert_eq!(saved.status, Status::Applied);
|
||||
let counts = db.query("SELECT CONCAT((SELECT COUNT(*) FROM acore_characters.characters),':',(SELECT COUNT(*) FROM acore_characters.item_instance),':',(SELECT COUNT(*) FROM acore_auth.account));")?;
|
||||
let check = || -> Result<()> {
|
||||
for hash in hashes {
|
||||
db.put(&LedgerRow {
|
||||
sha256: hash.into(),
|
||||
..saved.clone()
|
||||
})?;
|
||||
let observed = Observed {
|
||||
db: &db,
|
||||
executions: AtomicUsize::new(0),
|
||||
};
|
||||
db.put(&LedgerRow { sha256: hash.into(), ..saved.clone() })?;
|
||||
let observed = Observed { db: &db, executions: AtomicUsize::new(0) };
|
||||
migrations::preflight(&db, std::slice::from_ref(migration))?;
|
||||
let report = migrations::apply_pending(
|
||||
&observed,
|
||||
std::slice::from_ref(migration),
|
||||
&root.join(".unused-hotfix-sql"),
|
||||
&|| panic!("Already applied SQL needs no snapshot"),
|
||||
)?;
|
||||
let report = migrations::apply_pending(&observed, std::slice::from_ref(migration), &root.join(".unused-hotfix-sql"), &|| panic!("Already applied SQL needs no snapshot"))?;
|
||||
assert!(report.applied.is_empty());
|
||||
assert_eq!(observed.executions.load(Ordering::SeqCst), 0);
|
||||
assert_eq!(
|
||||
db.load()?
|
||||
.into_iter()
|
||||
.find(|r| r.id == migration.id && r.db == migration.db)
|
||||
.unwrap()
|
||||
.sha256,
|
||||
hash
|
||||
);
|
||||
assert_eq!(db.load()?.into_iter().find(|r| r.id == migration.id && r.db == migration.db).unwrap().sha256, hash);
|
||||
}
|
||||
let meta = test_meta.path();
|
||||
let file_hash = coa_core::fsx::sha256_file(&root.join("Core/worldserver.exe"))?;
|
||||
let install_hash = coa_core::fsx::sha256_file(&meta.join("install.json"))?;
|
||||
let transaction_count = std::fs::read_dir(meta.join("updates"))?.count();
|
||||
for status in [Status::Running, Status::Failed] {
|
||||
db.put(&LedgerRow {
|
||||
status,
|
||||
..saved.clone()
|
||||
})?;
|
||||
let env = coa_core::update::RepackEnv {
|
||||
root: &root,
|
||||
meta_dir: meta,
|
||||
};
|
||||
let rejected = coa_core::update::apply(
|
||||
&coa_core::update::Params {
|
||||
root: &root,
|
||||
meta_dir: meta,
|
||||
source: Source::Dir(PathBuf::from(&args[1])),
|
||||
trusted_key: coa_core::signing::EMBEDDED_PUBLIC_KEY,
|
||||
cancel: Default::default(),
|
||||
resolutions: Default::default(),
|
||||
env: &env,
|
||||
fail_after_ops: None,
|
||||
},
|
||||
&|_, _| {},
|
||||
);
|
||||
db.put(&LedgerRow { status, ..saved.clone() })?;
|
||||
let env = coa_core::update::RepackEnv { root: &root, meta_dir: meta };
|
||||
let rejected = coa_core::update::apply(&coa_core::update::Params {
|
||||
root: &root, meta_dir: meta, source: Source::Dir(PathBuf::from(&args[1])),
|
||||
trusted_key: coa_core::signing::EMBEDDED_PUBLIC_KEY, cancel: Default::default(),
|
||||
resolutions: Default::default(), env: &env, fail_after_ops: None,
|
||||
}, &|_, _| {});
|
||||
let message = rejected.unwrap_err().to_string();
|
||||
assert!(
|
||||
message.contains("partially applied"),
|
||||
"Unexpected rejection: {message}"
|
||||
);
|
||||
assert_eq!(
|
||||
std::fs::read_dir(meta.join("updates"))?.count(),
|
||||
transaction_count
|
||||
);
|
||||
assert_eq!(
|
||||
coa_core::fsx::sha256_file(&root.join("Core/worldserver.exe"))?,
|
||||
file_hash
|
||||
);
|
||||
assert_eq!(
|
||||
coa_core::fsx::sha256_file(&meta.join("install.json"))?,
|
||||
install_hash
|
||||
);
|
||||
assert!(message.contains("partially applied"), "Unexpected rejection: {message}");
|
||||
assert_eq!(std::fs::read_dir(meta.join("updates"))?.count(), transaction_count);
|
||||
assert_eq!(coa_core::fsx::sha256_file(&root.join("Core/worldserver.exe"))?, file_hash);
|
||||
assert_eq!(coa_core::fsx::sha256_file(&meta.join("install.json"))?, install_hash);
|
||||
}
|
||||
db.put(&saved)?;
|
||||
let env = coa_core::update::RepackEnv {
|
||||
root: &root,
|
||||
meta_dir: meta,
|
||||
};
|
||||
let env = coa_core::update::RepackEnv { root: &root, meta_dir: meta };
|
||||
let pending_before = env.pending_migrations(&package)?;
|
||||
db.query("DELETE FROM acore_world.coa_manager_migrations WHERE db='characters' AND id='manager_repair__20261005_missing_wildcard_tables';")?;
|
||||
assert_eq!(env.pending_migrations(&package)?, pending_before + 1,
|
||||
@@ -184,12 +81,7 @@ fn main() -> Result<()> {
|
||||
};
|
||||
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(check));
|
||||
db.put(&saved)?;
|
||||
result.map_err(|_| {
|
||||
coa_core::Error::Invalid(
|
||||
"History regression assertion failed; the injected ledger row was restored."
|
||||
.into(),
|
||||
)
|
||||
})??;
|
||||
result.map_err(|_| coa_core::Error::Invalid("History regression assertion failed; the injected ledger row was restored.".into()))??;
|
||||
assert_eq!(db.query("SELECT CONCAT((SELECT COUNT(*) FROM acore_characters.characters),':',(SELECT COUNT(*) FROM acore_characters.item_instance),':',(SELECT COUNT(*) FROM acore_auth.account));")?, counts);
|
||||
println!("PASS: {} published checksums; Running/Failed rejected before staging; missing migration counted; zero SQL replays; history and player counts preserved", mode.name());
|
||||
}
|
||||
|
||||
@@ -1,43 +1,16 @@
|
||||
//! Read-only update preview against the marked disposable acceptance fixture.
|
||||
use std::{collections::BTreeMap, path::PathBuf};
|
||||
fn main() -> coa_core::Result<()> {
|
||||
let args: Vec<_> = std::env::args().skip(1).collect();
|
||||
let root = PathBuf::from(&args[0]);
|
||||
assert!(root
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.starts_with("coa-schema-fixture-chaos-064-transition-"));
|
||||
assert_eq!(
|
||||
std::fs::read(root.join(".transition-fixture"))?,
|
||||
b"disposable published-064 transition fixture"
|
||||
);
|
||||
let meta_dir = coa_core::registry::metadata_dir_for(&root)?;
|
||||
let (_, meta) = coa_core::registry::MetaDir::open(&meta_dir)?;
|
||||
let preview = coa_core::update::preview(
|
||||
&root,
|
||||
&meta,
|
||||
&coa_core::pkgsource::Source::Dir(args[1].clone().into()),
|
||||
coa_core::signing::EMBEDDED_PUBLIC_KEY,
|
||||
&BTreeMap::new(),
|
||||
)?;
|
||||
assert_eq!(
|
||||
preview.from_version.as_deref(),
|
||||
Some(preview.to_version.as_str())
|
||||
);
|
||||
assert_eq!(preview.pending_migrations, 0);
|
||||
assert!(
|
||||
preview
|
||||
.items
|
||||
.iter()
|
||||
.all(|item| item.action == coa_core::update::Action::Skip),
|
||||
"{:?}",
|
||||
preview
|
||||
.items
|
||||
.iter()
|
||||
.filter(|item| item.action != coa_core::update::Action::Skip)
|
||||
.collect::<Vec<_>>()
|
||||
);
|
||||
use std::{collections::BTreeMap,path::PathBuf};
|
||||
fn main()->coa_core::Result<()> {
|
||||
let args:Vec<_>=std::env::args().skip(1).collect();
|
||||
let root=PathBuf::from(&args[0]);
|
||||
assert!(root.file_name().unwrap().to_string_lossy().starts_with("coa-schema-fixture-chaos-064-transition-"));
|
||||
assert_eq!(std::fs::read(root.join(".transition-fixture"))?,b"disposable published-064 transition fixture");
|
||||
let meta_dir=coa_core::registry::metadata_dir_for(&root)?;
|
||||
let (_,meta)=coa_core::registry::MetaDir::open(&meta_dir)?;
|
||||
let preview=coa_core::update::preview(&root,&meta,&coa_core::pkgsource::Source::Dir(args[1].clone().into()),coa_core::signing::EMBEDDED_PUBLIC_KEY,&BTreeMap::new())?;
|
||||
assert_eq!(preview.from_version.as_deref(),Some(preview.to_version.as_str()));
|
||||
assert_eq!(preview.pending_migrations,0);
|
||||
assert!(preview.items.iter().all(|item|item.action==coa_core::update::Action::Skip),"{:?}",preview.items.iter().filter(|item|item.action!=coa_core::update::Action::Skip).collect::<Vec<_>>());
|
||||
println!("PASS: real fixture repeated preview: {} files unchanged, zero pending SQL, no update offered",preview.items.len());
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,48 +1,23 @@
|
||||
//! Real signed-package transition on an independent, explicitly marked disposable copy.
|
||||
//! The Env wrapper instruments only launcher isolation; migration/backup/update code is production code.
|
||||
use coa_core::{
|
||||
backup, driver, fsx, migrations,
|
||||
pkgsource::{self, Source},
|
||||
registry::{InstallMeta, MetaDir},
|
||||
update::{self, Env, RepackEnv},
|
||||
Result,
|
||||
};
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use std::{collections::BTreeMap, path::{Path, PathBuf}};
|
||||
use coa_core::{backup, driver, fsx, migrations, pkgsource::{self, Source}, registry::{InstallMeta, MetaDir}, update::{self, Env, RepackEnv}, Result};
|
||||
|
||||
struct Offline<'a>(RepackEnv<'a>);
|
||||
impl Env for Offline<'_> {
|
||||
fn ensure_stopped(&self) -> Result<()> {
|
||||
self.0.ensure_stopped()
|
||||
}
|
||||
fn preflight(&self, m: &coa_core::manifest::Manifest) -> Result<()> {
|
||||
self.0.preflight(m)
|
||||
}
|
||||
fn verify_snapshot(&self, id: &str) -> Result<()> {
|
||||
self.0.verify_snapshot(id)
|
||||
}
|
||||
fn snapshot(&self) -> Result<String> {
|
||||
self.0.snapshot()
|
||||
}
|
||||
fn restore_snapshot(&self, id: &str) -> Result<()> {
|
||||
self.0.restore_snapshot(id)
|
||||
}
|
||||
fn migrate(
|
||||
&self,
|
||||
m: &coa_core::manifest::Manifest,
|
||||
p: &Path,
|
||||
) -> Result<migrations::ApplyReport> {
|
||||
self.0.migrate(m, p)
|
||||
}
|
||||
fn ensure_stopped(&self) -> Result<()> { self.0.ensure_stopped() }
|
||||
fn preflight(&self, m: &coa_core::manifest::Manifest) -> Result<()> { self.0.preflight(m) }
|
||||
fn verify_snapshot(&self, id: &str) -> Result<()> { self.0.verify_snapshot(id) }
|
||||
fn snapshot(&self) -> Result<String> { self.0.snapshot() }
|
||||
fn restore_snapshot(&self, id: &str) -> Result<()> { self.0.restore_snapshot(id) }
|
||||
fn migrate(&self, m: &coa_core::manifest::Manifest, p: &Path) -> Result<migrations::ApplyReport> { self.0.migrate(m,p) }
|
||||
fn validate(&self) -> Result<()> {
|
||||
let path = self.0.root.join("Scripts/manage.py");
|
||||
let source = std::fs::read_to_string(&path)?.replace("\r\n", "\n");
|
||||
let path=self.0.root.join("Scripts/manage.py");
|
||||
let source=std::fs::read_to_string(&path)?.replace("\r\n", "\n");
|
||||
// Explicit test instrumentation prevents relay transmissions and binds services locally.
|
||||
let guarded=source.replace("def spawn(name, command, cwd, log):\n", "def spawn(name, command, cwd, log):\n for filename in ('worldserver.conf', 'authserver.conf'):\n path = ROOT / 'Core/configs' / filename\n if path.exists():\n text = path.read_text(encoding='utf-8-sig')\n for key, value in (('BindIP', '\"127.0.0.1\"'), ('Ra.IP', '\"127.0.0.1\"'), ('SOAP.Enabled', '0')):\n text = re.sub(r'(?m)^\\s*' + re.escape(key) + r'\\s*=.*$', '', text)\n text += '\\n' + key + ' = ' + value + '\\n'\n path.write_text(text, encoding='utf-8')\n").replace("def relay_worker():\n", "def relay_worker():\n save_process('relay-ready', os.getpid())\n while not (STATE / 'stop-relay').exists():\n time.sleep(0.5)\n return\n");
|
||||
assert_ne!(source, guarded, "Fixture launcher isolation failed");
|
||||
fsx::atomic_write(&path, guarded.as_bytes())?;
|
||||
assert_ne!(source,guarded,"Fixture launcher isolation failed");
|
||||
fsx::atomic_write(&path,guarded.as_bytes())?;
|
||||
self.0.validate()
|
||||
}
|
||||
}
|
||||
@@ -50,261 +25,138 @@ fn players(db: &coa_core::db::Db) -> Result<String> {
|
||||
db.query("SELECT CONCAT(id,':',username,':',HEX(salt),':',HEX(verifier)) FROM acore_auth.account WHERE id=900001; SELECT CONCAT(guid,':',account,':',name,':',level,':',money) FROM acore_characters.characters WHERE guid=900001; SELECT CONCAT(guid,':',itemEntry,':',owner_guid,':',enchantments) FROM acore_characters.item_instance WHERE guid=900001; SELECT CONCAT(guid,':',bag,':',slot,':',item) FROM acore_characters.character_inventory WHERE guid=900001;")
|
||||
}
|
||||
fn main() -> Result<()> {
|
||||
let args: Vec<_> = std::env::args().skip(1).collect();
|
||||
let root = PathBuf::from(&args[0]);
|
||||
assert!(root
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.starts_with("coa-schema-fixture-chaos-064-transition-"));
|
||||
assert_eq!(
|
||||
std::fs::read(root.join(".transition-fixture"))?,
|
||||
b"disposable published-064 transition fixture"
|
||||
);
|
||||
if args.get(3).is_some_and(|mode| mode == "legacy-recovery") {
|
||||
return run_fixture(&root, || legacy_recovery(&root));
|
||||
let args:Vec<_>=std::env::args().skip(1).collect();
|
||||
let root=PathBuf::from(&args[0]);
|
||||
assert!(root.file_name().unwrap().to_string_lossy().starts_with("coa-schema-fixture-chaos-064-transition-"));
|
||||
assert_eq!(std::fs::read(root.join(".transition-fixture"))?,b"disposable published-064 transition fixture");
|
||||
if args.get(3).is_some_and(|mode| mode=="legacy-recovery") {
|
||||
return run_fixture(&root,||legacy_recovery(&root));
|
||||
}
|
||||
assert!(
|
||||
!root.join(".transition-started").exists(),
|
||||
"Use a new independent fixture"
|
||||
);
|
||||
fsx::atomic_write(&root.join(".transition-started"), b"started")?;
|
||||
let meta = coa_core::registry::metadata_dir_for(&root)?;
|
||||
assert!(!meta.exists(), "Do not reuse experimental or user metadata");
|
||||
run_fixture(&root, || exercise(&root, &meta, &args[1], &args[2]))
|
||||
assert!(!root.join(".transition-started").exists(),"Use a new independent fixture");
|
||||
fsx::atomic_write(&root.join(".transition-started"),b"started")?;
|
||||
let meta=coa_core::registry::metadata_dir_for(&root)?;
|
||||
assert!(!meta.exists(),"Do not reuse experimental or user metadata");
|
||||
run_fixture(&root,||exercise(&root,&meta,&args[1],&args[2]))
|
||||
}
|
||||
fn run_fixture(root: &Path, operation: impl FnOnce() -> Result<()>) -> Result<()> {
|
||||
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(operation));
|
||||
assert!(
|
||||
driver::run(root, driver::Verb::StopAll)?.ok,
|
||||
"Fixture shutdown failed"
|
||||
);
|
||||
fn run_fixture(root:&Path,operation:impl FnOnce()->Result<()>)->Result<()> {
|
||||
let result=std::panic::catch_unwind(std::panic::AssertUnwindSafe(operation));
|
||||
assert!(driver::run(root,driver::Verb::StopAll)?.ok,"Fixture shutdown failed");
|
||||
result.map_err(|_|coa_core::Error::Invalid("Disposable acceptance assertion failed; services were stopped and recovery copies retained.".into()))?
|
||||
}
|
||||
fn legacy_recovery(root: &Path) -> Result<()> {
|
||||
let meta = coa_core::registry::metadata_dir_for(root)?;
|
||||
assert!(driver::run(root, driver::Verb::StopAll)?.ok);
|
||||
let expected = backup::with_database(root, |db| {
|
||||
[
|
||||
coa_core::realms::Mode::Coa,
|
||||
coa_core::realms::Mode::Wildcard,
|
||||
]
|
||||
.into_iter()
|
||||
.map(|mode| players(&db.clone().for_realm(mode)))
|
||||
.collect::<Result<Vec<_>>>()
|
||||
})?;
|
||||
assert!(driver::run(root, driver::Verb::StopAll)?.ok);
|
||||
let dirs = std::fs::read_dir(meta.join("updates"))?.collect::<std::io::Result<Vec<_>>>()?;
|
||||
assert_eq!(
|
||||
dirs.len(),
|
||||
1,
|
||||
"Requires exactly the preceding successful disposable transition"
|
||||
);
|
||||
let txn_dir = dirs[0].path();
|
||||
let journal = txn_dir.join("txn.json");
|
||||
let mut legacy: serde_json::Value = fsx::read_json(&journal)?;
|
||||
assert_eq!(legacy["state"], "committed");
|
||||
let point = legacy["recovery_point"].as_str().unwrap().to_string();
|
||||
let point_path = meta.join("backups").join(&point).join("backup.json");
|
||||
let mut point_json: serde_json::Value = fsx::read_json(&point_path)?;
|
||||
point_json["manager_version"] = "0.6.4".into();
|
||||
for component in point_json["components"].as_array_mut().unwrap() {
|
||||
component.as_object_mut().unwrap().remove("file_sha256");
|
||||
}
|
||||
fsx::atomic_write_json(&point_path, &point_json)?;
|
||||
fn legacy_recovery(root:&Path)->Result<()> {
|
||||
let meta=coa_core::registry::metadata_dir_for(root)?;
|
||||
assert!(driver::run(root,driver::Verb::StopAll)?.ok);
|
||||
let expected=backup::with_database(root,|db| [coa_core::realms::Mode::Coa,coa_core::realms::Mode::Wildcard].into_iter().map(|mode|players(&db.clone().for_realm(mode))).collect::<Result<Vec<_>>>())?;
|
||||
assert!(driver::run(root,driver::Verb::StopAll)?.ok);
|
||||
let dirs=std::fs::read_dir(meta.join("updates"))?.collect::<std::io::Result<Vec<_>>>()?;
|
||||
assert_eq!(dirs.len(),1,"Requires exactly the preceding successful disposable transition");
|
||||
let txn_dir=dirs[0].path();
|
||||
let journal=txn_dir.join("txn.json");
|
||||
let mut legacy:serde_json::Value=fsx::read_json(&journal)?;
|
||||
assert_eq!(legacy["state"],"committed");
|
||||
let point=legacy["recovery_point"].as_str().unwrap().to_string();
|
||||
let point_path=meta.join("backups").join(&point).join("backup.json");
|
||||
let mut point_json:serde_json::Value=fsx::read_json(&point_path)?;
|
||||
point_json["manager_version"]="0.6.4".into();
|
||||
for component in point_json["components"].as_array_mut().unwrap() { component.as_object_mut().unwrap().remove("file_sha256"); }
|
||||
fsx::atomic_write_json(&point_path,&point_json)?;
|
||||
legacy.as_object_mut().unwrap().remove("recovery_hashes");
|
||||
legacy.as_object_mut().unwrap().remove("databases_started");
|
||||
legacy["state"] = "applied".into();
|
||||
fsx::atomic_write_json(&journal, &legacy)?;
|
||||
assert!(
|
||||
update::ensure_recovered(&meta).is_err(),
|
||||
"Legacy unfinished update must block startup"
|
||||
);
|
||||
let id = legacy["id"].as_str().unwrap();
|
||||
let env = RepackEnv {
|
||||
root,
|
||||
meta_dir: &meta,
|
||||
};
|
||||
legacy["state"]="applied".into();
|
||||
fsx::atomic_write_json(&journal,&legacy)?;
|
||||
assert!(update::ensure_recovered(&meta).is_err(),"Legacy unfinished update must block startup");
|
||||
let id=legacy["id"].as_str().unwrap();
|
||||
let env=RepackEnv {root,meta_dir:&meta};
|
||||
println!("Recovering legacy-format transaction and recovery point on disposable installation");
|
||||
assert_eq!(
|
||||
update::rollback(root, &meta, id, &env)?.state,
|
||||
update::State::RolledBack
|
||||
);
|
||||
assert_eq!(update::rollback(root,&meta,id,&env)?.state,update::State::RolledBack);
|
||||
for op in legacy["ops"].as_array().unwrap() {
|
||||
let relative = op["path"].as_str().unwrap();
|
||||
if op["had_previous"] == true {
|
||||
assert_eq!(
|
||||
fsx::sha256_file(&fsx::safe_join(root, relative)?)?,
|
||||
fsx::sha256_file(&fsx::safe_join(&txn_dir.join("before"), relative)?)?,
|
||||
"Legacy file restoration mismatch: {relative}"
|
||||
);
|
||||
} else {
|
||||
assert!(!fsx::safe_join(root, relative)?.exists());
|
||||
}
|
||||
let relative=op["path"].as_str().unwrap();
|
||||
if op["had_previous"]==true {
|
||||
assert_eq!(fsx::sha256_file(&fsx::safe_join(root,relative)?)?,fsx::sha256_file(&fsx::safe_join(&txn_dir.join("before"),relative)?)?,"Legacy file restoration mismatch: {relative}");
|
||||
} else { assert!(!fsx::safe_join(root,relative)?.exists()); }
|
||||
}
|
||||
assert_eq!(
|
||||
std::fs::read(meta.join("install.json"))?,
|
||||
std::fs::read(txn_dir.join("before/manager-install.json"))?
|
||||
);
|
||||
backup::with_database(root, |db| {
|
||||
for (index, mode) in [
|
||||
coa_core::realms::Mode::Coa,
|
||||
coa_core::realms::Mode::Wildcard,
|
||||
]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
let db = db.clone().for_realm(mode);
|
||||
assert_eq!(players(&db)?, expected[index]);
|
||||
assert_eq!(std::fs::read(meta.join("install.json"))?,std::fs::read(txn_dir.join("before/manager-install.json"))?);
|
||||
backup::with_database(root,|db| {
|
||||
for (index,mode) in [coa_core::realms::Mode::Coa,coa_core::realms::Mode::Wildcard].into_iter().enumerate() {
|
||||
let db=db.clone().for_realm(mode);
|
||||
assert_eq!(players(&db)?,expected[index]);
|
||||
assert_eq!(db.query("SELECT COUNT(*) FROM acore_world.coa_manager_migrations WHERE id IN ('manager_repair__20261005_reconcile_wildcard_tables','manager_repair__20261005_reconcile_missing_class_starts');")?,"0");
|
||||
assert_eq!(db.query("SELECT sha256 FROM acore_world.coa_manager_migrations WHERE id='manager_repair__20261005_missing_wildcard_tables';")?,"9e4a36245c3f83255415c122c01f08ceb3e3b83898d3d427ac4e2fbcf61e9638");
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
assert!(driver::run(root, driver::Verb::StopAll)?.ok);
|
||||
assert!(driver::run(root,driver::Verb::StopAll)?.ok);
|
||||
assert!(update::pending_checked(&meta)?.is_none());
|
||||
assert!(
|
||||
update::rollback(root, &meta, id, &env).is_err(),
|
||||
"Completed recovery must not run again"
|
||||
);
|
||||
assert!(update::rollback(root,&meta,id,&env).is_err(),"Completed recovery must not run again");
|
||||
println!("PASS: legacy journal blocked startup; old files, metadata, migration histories and player fields restored; completed recovery rejects replay. Original baseline had a missing table, so healthy startup is not claimed after rollback.");
|
||||
Ok(())
|
||||
}
|
||||
fn exercise(root: &Path, meta: &Path, baseline: &str, candidate: &str) -> Result<()> {
|
||||
let mut install: InstallMeta = fsx::read_json(&PathBuf::from(
|
||||
"C:/games/coa-schema-fixture-chaos-20261005-v1.manager/install.json",
|
||||
))?;
|
||||
install.server_path = root.to_string_lossy().into_owned();
|
||||
install.id = uuid::Uuid::new_v4().to_string();
|
||||
install.manager_version = "0.6.4".into();
|
||||
MetaDir::create(root, &install)?;
|
||||
fn exercise(root:&Path,meta:&Path,baseline:&str,candidate:&str)->Result<()> {
|
||||
let mut install:InstallMeta=fsx::read_json(&PathBuf::from("C:/games/coa-schema-fixture-chaos-20261005-v1.manager/install.json"))?;
|
||||
install.server_path=root.to_string_lossy().into_owned();
|
||||
install.id=uuid::Uuid::new_v4().to_string();
|
||||
install.manager_version="0.6.4".into();
|
||||
MetaDir::create(root,&install)?;
|
||||
// Private ports, and no inherited PID records or paths to the source database.
|
||||
let mut cfg: serde_json::Value = fsx::read_json(&root.join("Settings/repack.json"))?;
|
||||
let mut listeners = Vec::new();
|
||||
for key in ["mysqlPort", "authPort", "worldPort", "raPort"] {
|
||||
let listener = std::net::TcpListener::bind("127.0.0.1:0")?;
|
||||
cfg[key] = listener.local_addr()?.port().into();
|
||||
listeners.push(listener);
|
||||
let mut cfg:serde_json::Value=fsx::read_json(&root.join("Settings/repack.json"))?;
|
||||
let mut listeners=Vec::new();
|
||||
for key in ["mysqlPort","authPort","worldPort","raPort"] {
|
||||
let listener=std::net::TcpListener::bind("127.0.0.1:0")?;
|
||||
cfg[key]=listener.local_addr()?.port().into(); listeners.push(listener);
|
||||
}
|
||||
fsx::atomic_write_json(&root.join("Settings/repack.json"), &cfg)?;
|
||||
let mut profile: serde_json::Value = fsx::read_json(&root.join("Settings/realm-profile.json"))?;
|
||||
for key in ["secondary_world_port", "secondary_ra_port"] {
|
||||
let listener = std::net::TcpListener::bind("127.0.0.1:0")?;
|
||||
profile[key] = listener.local_addr()?.port().into();
|
||||
listeners.push(listener);
|
||||
fsx::atomic_write_json(&root.join("Settings/repack.json"),&cfg)?;
|
||||
let mut profile:serde_json::Value=fsx::read_json(&root.join("Settings/realm-profile.json"))?;
|
||||
for key in ["secondary_world_port","secondary_ra_port"] {
|
||||
let listener=std::net::TcpListener::bind("127.0.0.1:0")?;
|
||||
profile[key]=listener.local_addr()?.port().into(); listeners.push(listener);
|
||||
}
|
||||
fsx::atomic_write_json(&root.join("Settings/realm-profile.json"), &profile)?;
|
||||
fsx::atomic_write_json(&root.join("Settings/realm-profile.json"),&profile)?;
|
||||
drop(listeners);
|
||||
let state = root.join(".state");
|
||||
if state.exists() {
|
||||
std::fs::remove_dir_all(&state)?;
|
||||
}
|
||||
let secondary_state = root.join(".realms/secondary/.state");
|
||||
if secondary_state.exists() {
|
||||
std::fs::remove_dir_all(&secondary_state)?;
|
||||
}
|
||||
let source = Source::Dir(baseline.into());
|
||||
let (manifest, _) = pkgsource::fetch_manifest(&source, coa_core::signing::EMBEDDED_PUBLIC_KEY)?;
|
||||
let temp = tempfile::tempdir()?;
|
||||
let parts = pkgsource::fetch_parts(
|
||||
&source,
|
||||
&manifest,
|
||||
&temp.path().join("download"),
|
||||
&Default::default(),
|
||||
&|_, _| {},
|
||||
)?;
|
||||
let tree = temp.path().join("tree");
|
||||
coa_core::package::extract(&parts, &manifest, &tree, &|_, _| {})?;
|
||||
let state=root.join(".state");
|
||||
if state.exists() { std::fs::remove_dir_all(&state)?; }
|
||||
let secondary_state=root.join(".realms/secondary/.state");
|
||||
if secondary_state.exists() { std::fs::remove_dir_all(&secondary_state)?; }
|
||||
let source=Source::Dir(baseline.into());
|
||||
let (manifest,_)=pkgsource::fetch_manifest(&source,coa_core::signing::EMBEDDED_PUBLIC_KEY)?;
|
||||
let temp=tempfile::tempdir()?;
|
||||
let parts=pkgsource::fetch_parts(&source,&manifest,&temp.path().join("download"),&Default::default(),&|_,_|{})?;
|
||||
let tree=temp.path().join("tree");
|
||||
coa_core::package::extract(&parts,&manifest,&tree,&|_,_|{})?;
|
||||
for file in &manifest.files {
|
||||
if file.path.starts_with("_migrations/") {
|
||||
continue;
|
||||
}
|
||||
fsx::atomic_write(
|
||||
&fsx::safe_join(root, &file.path)?,
|
||||
&std::fs::read(fsx::safe_join(&tree, &file.path)?)?,
|
||||
)?;
|
||||
install
|
||||
.original_hashes
|
||||
.insert(file.path.clone(), file.sha256.clone());
|
||||
if file.path.starts_with("_migrations/") {continue;}
|
||||
fsx::atomic_write(&fsx::safe_join(root,&file.path)?,&std::fs::read(fsx::safe_join(&tree,&file.path)?)?)?;
|
||||
install.original_hashes.insert(file.path.clone(),file.sha256.clone());
|
||||
}
|
||||
install.core.version = Some(manifest.version);
|
||||
fsx::atomic_write_json(&meta.join("install.json"), &install)?;
|
||||
install.core.version=Some(manifest.version);
|
||||
fsx::atomic_write_json(&meta.join("install.json"),&install)?;
|
||||
// Both worlds must remain local, including generated secondary configuration.
|
||||
for relative in [
|
||||
"Settings/worldserver.conf.template",
|
||||
"Settings/authserver.conf.template",
|
||||
] {
|
||||
let p = root.join(relative);
|
||||
let mut conf = coa_core::config::parser::ConfFile::parse_bytes(&std::fs::read(&p)?)?;
|
||||
conf.set("BindIP", "\"127.0.0.1\"", &[]);
|
||||
conf.set("Ra.IP", "\"127.0.0.1\"", &[]);
|
||||
conf.set("SOAP.Enabled", "0", &[]);
|
||||
fsx::atomic_write(&p, conf.to_text().as_bytes())?;
|
||||
for relative in ["Settings/worldserver.conf.template","Settings/authserver.conf.template"] {
|
||||
let p=root.join(relative);
|
||||
let mut conf=coa_core::config::parser::ConfFile::parse_bytes(&std::fs::read(&p)?)?;
|
||||
conf.set("BindIP","\"127.0.0.1\"",&[]); conf.set("Ra.IP","\"127.0.0.1\"",&[]); conf.set("SOAP.Enabled","0",&[]);
|
||||
fsx::atomic_write(&p,conf.to_text().as_bytes())?;
|
||||
}
|
||||
let expected = backup::with_database(root, |db| {
|
||||
let mut saved = Vec::new();
|
||||
for mode in [
|
||||
coa_core::realms::Mode::Coa,
|
||||
coa_core::realms::Mode::Wildcard,
|
||||
] {
|
||||
let db = db.clone().for_realm(mode);
|
||||
let expected=backup::with_database(root,|db| {
|
||||
let mut saved=Vec::new();
|
||||
for mode in [coa_core::realms::Mode::Coa,coa_core::realms::Mode::Wildcard] {
|
||||
let db=db.clone().for_realm(mode);
|
||||
saved.push(players(&db)?);
|
||||
db.query("DROP TABLE IF EXISTS acore_characters.coa_wildcard_skill_card; DELETE FROM acore_world.coa_manager_migrations WHERE id IN ('manager_repair__20261005_reconcile_wildcard_tables','manager_repair__20261005_reconcile_missing_class_starts'); UPDATE acore_world.coa_manager_migrations SET sha256='9e4a36245c3f83255415c122c01f08ceb3e3b83898d3d427ac4e2fbcf61e9638' WHERE id='manager_repair__20261005_missing_wildcard_tables' AND status='applied';")?;
|
||||
}
|
||||
Ok(saved)
|
||||
})?;
|
||||
let source = Source::Dir(candidate.into());
|
||||
let candidate_manifest =
|
||||
pkgsource::fetch_manifest(&source, coa_core::signing::EMBEDDED_PUBLIC_KEY)?.0;
|
||||
let choices: BTreeMap<_, _> =
|
||||
update::plan(root, &install, &candidate_manifest, &BTreeMap::new(), None)?
|
||||
.into_iter()
|
||||
.filter(|p| p.action == update::Action::Conflict)
|
||||
.map(|p| (p.path, update::Resolution::Replace))
|
||||
.collect();
|
||||
let env = Offline(RepackEnv {
|
||||
root,
|
||||
meta_dir: meta,
|
||||
});
|
||||
let outcome = update::apply(
|
||||
&update::Params {
|
||||
root,
|
||||
meta_dir: meta,
|
||||
source,
|
||||
trusted_key: coa_core::signing::EMBEDDED_PUBLIC_KEY,
|
||||
cancel: Default::default(),
|
||||
resolutions: choices,
|
||||
env: &env,
|
||||
fail_after_ops: None,
|
||||
},
|
||||
&|stage, pct| println!("{pct}% {stage}"),
|
||||
)?;
|
||||
assert_eq!(
|
||||
outcome.txn.state,
|
||||
update::State::Committed,
|
||||
"{:?}",
|
||||
outcome.txn.message
|
||||
);
|
||||
backup::with_database(root, |db| {
|
||||
for (index, mode) in [
|
||||
coa_core::realms::Mode::Coa,
|
||||
coa_core::realms::Mode::Wildcard,
|
||||
]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
let db = db.clone().for_realm(mode);
|
||||
assert_eq!(
|
||||
players(&db)?,
|
||||
expected[index],
|
||||
"Player data changed in {}",
|
||||
mode.name()
|
||||
);
|
||||
assert!(coa_core::schema_check::check(&db, root)?.is_empty());
|
||||
assert_eq!(
|
||||
migrations::pending_count(&db, &candidate_manifest.migrations)?,
|
||||
0
|
||||
);
|
||||
let source=Source::Dir(candidate.into());
|
||||
let candidate_manifest=pkgsource::fetch_manifest(&source,coa_core::signing::EMBEDDED_PUBLIC_KEY)?.0;
|
||||
let choices:BTreeMap<_,_>=update::plan(root,&install,&candidate_manifest,&BTreeMap::new(),None)?.into_iter().filter(|p|p.action==update::Action::Conflict).map(|p|(p.path,update::Resolution::Replace)).collect();
|
||||
let env=Offline(RepackEnv{root,meta_dir:meta});
|
||||
let outcome=update::apply(&update::Params {root,meta_dir:meta,source,trusted_key:coa_core::signing::EMBEDDED_PUBLIC_KEY,cancel:Default::default(),resolutions:choices,env:&env,fail_after_ops:None},&|stage,pct|println!("{pct}% {stage}"))?;
|
||||
assert_eq!(outcome.txn.state,update::State::Committed,"{:?}",outcome.txn.message);
|
||||
backup::with_database(root,|db| {
|
||||
for (index,mode) in [coa_core::realms::Mode::Coa,coa_core::realms::Mode::Wildcard].into_iter().enumerate() {
|
||||
let db=db.clone().for_realm(mode);
|
||||
assert_eq!(players(&db)?,expected[index],"Player data changed in {}",mode.name());
|
||||
assert!(coa_core::schema_check::check(&db,root)?.is_empty());
|
||||
assert_eq!(migrations::pending_count(&db,&candidate_manifest.migrations)?,0);
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
@@ -11,123 +11,47 @@ use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
let a: Vec<String> = std::env::args().skip(1).collect();
|
||||
let (fixture, out, dest) = (
|
||||
PathBuf::from(&a[0]),
|
||||
PathBuf::from(&a[1]),
|
||||
PathBuf::from(&a[2]),
|
||||
);
|
||||
assert!(
|
||||
a[0].to_lowercase().contains("fixture"),
|
||||
"refusing: source is not a fixture"
|
||||
);
|
||||
assert!(
|
||||
std::fs::symlink_metadata(fixture.join("Data"))
|
||||
.unwrap()
|
||||
.file_type()
|
||||
.is_symlink(),
|
||||
"Data junction must be skipped by the packager"
|
||||
);
|
||||
let (fixture, out, dest) = (PathBuf::from(&a[0]), PathBuf::from(&a[1]), PathBuf::from(&a[2]));
|
||||
assert!(a[0].to_lowercase().contains("fixture"), "refusing: source is not a fixture");
|
||||
assert!(std::fs::symlink_metadata(fixture.join("Data")).unwrap().file_type().is_symlink(), "Data junction must be skipped by the packager");
|
||||
|
||||
let t = std::time::Instant::now();
|
||||
let m = build(
|
||||
&fixture,
|
||||
&out,
|
||||
&BuildOptions {
|
||||
kind: coa_core::manifest::Kind::Base,
|
||||
version: "0.1.0".into(),
|
||||
core_commit: None,
|
||||
built_at: chrono::Utc::now().to_rfc3339(),
|
||||
part_size: 256 * 1024 * 1024,
|
||||
bots_commit: None,
|
||||
migrations: vec![],
|
||||
},
|
||||
&|s| eprintln!(" pack: {s}"),
|
||||
)
|
||||
.expect("build");
|
||||
let m = build(&fixture, &out, &BuildOptions { kind: coa_core::manifest::Kind::Base, version: "0.1.0".into(), core_commit: None, built_at: chrono::Utc::now().to_rfc3339(), part_size: 256 * 1024 * 1024, bots_commit: None, migrations: vec![] }, &|s| eprintln!(" pack: {s}")).expect("build");
|
||||
let arch = m.archive.as_ref().unwrap();
|
||||
println!(
|
||||
"packaged {} files, {:.2} GB -> {} parts, {:.2} GB in {:?}",
|
||||
m.files.len(),
|
||||
arch.unpacked_size as f64 / 1e9,
|
||||
arch.parts.len(),
|
||||
arch.parts.iter().map(|p| p.size).sum::<u64>() as f64 / 1e9,
|
||||
t.elapsed()
|
||||
);
|
||||
println!("packaged {} files, {:.2} GB -> {} parts, {:.2} GB in {:?}", m.files.len(), arch.unpacked_size as f64 / 1e9, arch.parts.len(), arch.parts.iter().map(|p| p.size).sum::<u64>() as f64 / 1e9, t.elapsed());
|
||||
|
||||
let key_path = format!(
|
||||
"{}/.coa-manager/signing/manifest-signing.key",
|
||||
std::env::var("USERPROFILE").unwrap()
|
||||
);
|
||||
let seed: [u8; 32] = STANDARD
|
||||
.decode(std::fs::read_to_string(key_path).unwrap().trim())
|
||||
.unwrap()
|
||||
.try_into()
|
||||
.unwrap();
|
||||
let sig =
|
||||
SigningKey::from_bytes(&seed).sign(&std::fs::read(out.join("manifest.json")).unwrap());
|
||||
std::fs::write(
|
||||
out.join("manifest.json.sig"),
|
||||
STANDARD.encode(sig.to_bytes()),
|
||||
)
|
||||
.unwrap();
|
||||
let key_path = format!("{}/.coa-manager/signing/manifest-signing.key", std::env::var("USERPROFILE").unwrap());
|
||||
let seed: [u8; 32] = STANDARD.decode(std::fs::read_to_string(key_path).unwrap().trim()).unwrap().try_into().unwrap();
|
||||
let sig = SigningKey::from_bytes(&seed).sign(&std::fs::read(out.join("manifest.json")).unwrap());
|
||||
std::fs::write(out.join("manifest.json.sig"), STANDARD.encode(sig.to_bytes())).unwrap();
|
||||
|
||||
let reg_dir = std::env::temp_dir().join("coa-install-e2e");
|
||||
let _ = std::fs::remove_dir_all(®_dir);
|
||||
let registry = Registry::at(reg_dir.join("installs.json"));
|
||||
let t = std::time::Instant::now();
|
||||
let installed = install_base(
|
||||
&Params {
|
||||
source: Source::Dir(out.clone()),
|
||||
dest: dest.clone(),
|
||||
trusted_key: coa_core::signing::EMBEDDED_PUBLIC_KEY,
|
||||
registry: ®istry,
|
||||
cancel: Cancel::default(),
|
||||
},
|
||||
&|s| {
|
||||
eprintln!(
|
||||
" install: {:>3}% {} {}",
|
||||
s.percent,
|
||||
s.step,
|
||||
s.detail.unwrap_or_default()
|
||||
)
|
||||
},
|
||||
&Params { source: Source::Dir(out.clone()), dest: dest.clone(), trusted_key: coa_core::signing::EMBEDDED_PUBLIC_KEY, registry: ®istry, cancel: Cancel::default() },
|
||||
&|s| eprintln!(" install: {:>3}% {} {}", s.percent, s.step, s.detail.unwrap_or_default()),
|
||||
)
|
||||
.unwrap_or_else(|e| panic!("install failed: {e:?}"));
|
||||
println!(
|
||||
"installed {} at {} in {:?}",
|
||||
installed.version,
|
||||
installed.path,
|
||||
t.elapsed()
|
||||
);
|
||||
println!("installed {} at {} in {:?}", installed.version, installed.path, t.elapsed());
|
||||
|
||||
assert!(!dest.with_file_name("coa-installtest.installing").exists());
|
||||
assert!(
|
||||
!dest.join("Settings/database.bootstrap.json").exists(),
|
||||
"bootstrap credentials are removed"
|
||||
);
|
||||
assert!(!dest.join("Settings/database.bootstrap.json").exists(), "bootstrap credentials are removed");
|
||||
assert!(dest.join("Core/worldserver.exe").is_file());
|
||||
assert!(dest
|
||||
.with_file_name("coa-installtest.manager/install.json")
|
||||
.is_file());
|
||||
assert!(dest.with_file_name("coa-installtest.manager/install.json").is_file());
|
||||
assert_eq!(registry.list().unwrap().len(), 1);
|
||||
|
||||
// The rotated credentials must differ from the packaged ones and work.
|
||||
let packaged: serde_json::Value =
|
||||
serde_json::from_slice(&std::fs::read(fixture.join("Settings/database.json")).unwrap())
|
||||
.unwrap();
|
||||
let now: serde_json::Value =
|
||||
serde_json::from_slice(&std::fs::read(dest.join("Settings/database.json")).unwrap())
|
||||
.unwrap();
|
||||
let packaged: serde_json::Value = serde_json::from_slice(&std::fs::read(fixture.join("Settings/database.json")).unwrap()).unwrap();
|
||||
let now: serde_json::Value = serde_json::from_slice(&std::fs::read(dest.join("Settings/database.json")).unwrap()).unwrap();
|
||||
assert_ne!(packaged["rootPassword"], now["rootPassword"]);
|
||||
assert_ne!(packaged["appPassword"], now["appPassword"]);
|
||||
coa_core::backup::with_database(&dest, |db: &Db| {
|
||||
assert!(db.ping());
|
||||
assert!(db.tables("acore_auth")?.len() > 10);
|
||||
assert_eq!(db.tables("acore_auth")?.len() > 10, true);
|
||||
let app = Db::from_repack(&dest, Account::App)?;
|
||||
assert!(
|
||||
app.query("SELECT 1;")? == "1",
|
||||
"the game account works with the new password"
|
||||
);
|
||||
assert!(app.query("SELECT 1;")? == "1", "the game account works with the new password");
|
||||
Ok(())
|
||||
})
|
||||
.expect("database after install");
|
||||
|
||||
@@ -5,23 +5,9 @@ use coa_core::registry::Registry;
|
||||
|
||||
fn main() {
|
||||
let a: Vec<String> = std::env::args().skip(1).collect();
|
||||
let reg = Registry::at(
|
||||
std::env::temp_dir()
|
||||
.join("coa-install-pkg")
|
||||
.join("installs.json"),
|
||||
);
|
||||
let reg = Registry::at(std::env::temp_dir().join("coa-install-pkg").join("installs.json"));
|
||||
let _ = std::fs::remove_dir_all(std::env::temp_dir().join("coa-install-pkg"));
|
||||
let t = std::time::Instant::now();
|
||||
let r = install_base(
|
||||
&Params {
|
||||
source: Source::Dir(a[0].clone().into()),
|
||||
dest: a[1].clone().into(),
|
||||
trusted_key: coa_core::signing::EMBEDDED_PUBLIC_KEY,
|
||||
registry: ®,
|
||||
cancel: Cancel::default(),
|
||||
},
|
||||
&|s| eprintln!(" {:>3}% {}", s.percent, s.step),
|
||||
)
|
||||
.unwrap_or_else(|e| panic!("install failed: {e:?}"));
|
||||
let r = install_base(&Params { source: Source::Dir(a[0].clone().into()), dest: a[1].clone().into(), trusted_key: coa_core::signing::EMBEDDED_PUBLIC_KEY, registry: ®, cancel: Cancel::default() }, &|s| eprintln!(" {:>3}% {}", s.percent, s.step)).unwrap_or_else(|e| panic!("install failed: {e:?}"));
|
||||
println!("installed {} at {} in {:?}", r.version, r.path, t.elapsed());
|
||||
}
|
||||
@@ -5,23 +5,9 @@ use coa_core::registry::Registry;
|
||||
|
||||
fn main() {
|
||||
let a: Vec<String> = std::env::args().skip(1).collect();
|
||||
let reg = Registry::at(
|
||||
std::env::temp_dir()
|
||||
.join("coa-install-url")
|
||||
.join("installs.json"),
|
||||
);
|
||||
let reg = Registry::at(std::env::temp_dir().join("coa-install-url").join("installs.json"));
|
||||
let _ = std::fs::remove_dir_all(std::env::temp_dir().join("coa-install-url"));
|
||||
let t = std::time::Instant::now();
|
||||
let r = install_base(
|
||||
&Params {
|
||||
source: Source::Url(a[0].clone()),
|
||||
dest: a[1].clone().into(),
|
||||
trusted_key: coa_core::signing::EMBEDDED_PUBLIC_KEY,
|
||||
registry: ®,
|
||||
cancel: Cancel::default(),
|
||||
},
|
||||
&|s| eprintln!(" {:>3}% {}", s.percent, s.step),
|
||||
)
|
||||
.unwrap_or_else(|e| panic!("install failed: {e:?}"));
|
||||
let r = install_base(&Params { source: Source::Url(a[0].clone()), dest: a[1].clone().into(), trusted_key: coa_core::signing::EMBEDDED_PUBLIC_KEY, registry: ®, cancel: Cancel::default() }, &|s| eprintln!(" {:>3}% {}", s.percent, s.step)).unwrap_or_else(|e| panic!("install failed: {e:?}"));
|
||||
println!("installed {} at {} in {:?}", r.version, r.path, t.elapsed());
|
||||
}
|
||||
@@ -5,27 +5,14 @@ use ed25519_dalek::SigningKey;
|
||||
use std::{fs, path::PathBuf};
|
||||
|
||||
fn main() {
|
||||
let private = PathBuf::from(std::env::var("USERPROFILE").expect("USERPROFILE"))
|
||||
.join(".coa-manager/signing/manifest-signing.key");
|
||||
let public = PathBuf::from(
|
||||
std::env::args()
|
||||
.nth(1)
|
||||
.unwrap_or_else(|| "keys/manifest-signing.pub".into()),
|
||||
);
|
||||
let private = PathBuf::from(std::env::var("USERPROFILE").expect("USERPROFILE")).join(".coa-manager/signing/manifest-signing.key");
|
||||
let public = PathBuf::from(std::env::args().nth(1).unwrap_or_else(|| "keys/manifest-signing.pub".into()));
|
||||
if private.exists() || public.exists() {
|
||||
panic!("a key already exists; refusing to overwrite");
|
||||
}
|
||||
fs::create_dir_all(private.parent().unwrap()).unwrap();
|
||||
let sk = SigningKey::generate(&mut rand_core::OsRng);
|
||||
fs::write(&private, STANDARD.encode(sk.to_bytes())).unwrap();
|
||||
fs::write(
|
||||
&public,
|
||||
format!("{}\n", STANDARD.encode(sk.verifying_key().to_bytes())),
|
||||
)
|
||||
.unwrap();
|
||||
println!(
|
||||
"private key: {}\npublic key: {}",
|
||||
private.display(),
|
||||
public.display()
|
||||
);
|
||||
fs::write(&public, format!("{}\n", STANDARD.encode(sk.verifying_key().to_bytes()))).unwrap();
|
||||
println!("private key: {}\npublic key: {}", private.display(), public.display());
|
||||
}
|
||||
@@ -8,24 +8,14 @@ use coa_core::migrations::{apply_pending, status, Status};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
fn sql_files(dir: &Path) -> Vec<PathBuf> {
|
||||
let mut v: Vec<PathBuf> = std::fs::read_dir(dir)
|
||||
.map(|rd| {
|
||||
rd.flatten()
|
||||
.map(|e| e.path())
|
||||
.filter(|p| p.extension().map(|e| e == "sql").unwrap_or(false))
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
let mut v: Vec<PathBuf> = std::fs::read_dir(dir).map(|rd| rd.flatten().map(|e| e.path()).filter(|p| p.extension().map(|e| e == "sql").unwrap_or(false)).collect()).unwrap_or_default();
|
||||
v.sort();
|
||||
v
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a: Vec<String> = std::env::args().skip(1).collect();
|
||||
assert!(
|
||||
a[0].to_lowercase().contains("fixture"),
|
||||
"refusing: not a fixture folder"
|
||||
);
|
||||
assert!(a[0].to_lowercase().contains("fixture"), "refusing: not a fixture folder");
|
||||
let (root, core) = (PathBuf::from(&a[0]), PathBuf::from(&a[1]));
|
||||
let staging = std::env::temp_dir().join("coa-migrate-e2e");
|
||||
let _ = std::fs::remove_dir_all(&staging);
|
||||
@@ -34,37 +24,17 @@ fn main() {
|
||||
let mut list: Vec<Migration> = Vec::new();
|
||||
let mut origin: std::collections::HashMap<(String, String), String> = Default::default();
|
||||
let mut add = |db: &str, id: String, src: &Path| {
|
||||
origin.insert(
|
||||
(db.to_string(), id.clone()),
|
||||
src.strip_prefix(&core)
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.replace('\\', "/"),
|
||||
);
|
||||
origin.insert((db.to_string(), id.clone()), src.strip_prefix(&core).unwrap().to_string_lossy().replace('\\', "/"));
|
||||
let bytes = std::fs::read(src).unwrap();
|
||||
let dst = staging.join(db).join(format!("{id}.sql"));
|
||||
std::fs::create_dir_all(dst.parent().unwrap()).unwrap();
|
||||
std::fs::write(&dst, &bytes).unwrap();
|
||||
list.push(Migration {
|
||||
compatible_sha256: vec![],
|
||||
id,
|
||||
db: db.into(),
|
||||
sha256: fsx::sha256_bytes(&bytes),
|
||||
destructive: false,
|
||||
});
|
||||
list.push(Migration { compatible_sha256: vec![], id, db: db.into(), sha256: fsx::sha256_bytes(&bytes), destructive: false });
|
||||
};
|
||||
for (kind, dirs) in [
|
||||
("auth", ["db_auth", "pending_db_auth"]),
|
||||
("characters", ["db_characters", "pending_db_characters"]),
|
||||
("world", ["db_world", "pending_db_world"]),
|
||||
] {
|
||||
for (kind, dirs) in [("auth", ["db_auth", "pending_db_auth"]), ("characters", ["db_characters", "pending_db_characters"]), ("world", ["db_world", "pending_db_world"])] {
|
||||
for d in dirs {
|
||||
for f in sql_files(&core.join("data/sql/updates").join(d)) {
|
||||
add(
|
||||
kind,
|
||||
f.file_stem().unwrap().to_string_lossy().into_owned(),
|
||||
&f,
|
||||
);
|
||||
add(kind, f.file_stem().unwrap().to_string_lossy().into_owned(), &f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,21 +43,10 @@ fn main() {
|
||||
for kind in ["auth", "characters", "world"] {
|
||||
let base = m.path().join(format!("data/sql/db-{kind}"));
|
||||
for f in sql_files(&base.join("base")) {
|
||||
add(
|
||||
kind,
|
||||
format!(
|
||||
"mod_{mname}__base__{}",
|
||||
f.file_stem().unwrap().to_string_lossy()
|
||||
),
|
||||
&f,
|
||||
);
|
||||
add(kind, format!("mod_{mname}__base__{}", f.file_stem().unwrap().to_string_lossy()), &f);
|
||||
}
|
||||
for f in sql_files(&base) {
|
||||
add(
|
||||
kind,
|
||||
format!("mod_{mname}__{}", f.file_stem().unwrap().to_string_lossy()),
|
||||
&f,
|
||||
);
|
||||
add(kind, format!("mod_{mname}__{}", f.file_stem().unwrap().to_string_lossy()), &f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -95,68 +54,29 @@ fn main() {
|
||||
// table created by a module script that arrived earlier). The release pipeline computes exactly this.
|
||||
let out = std::process::Command::new("git")
|
||||
.current_dir(&core)
|
||||
.args([
|
||||
"log",
|
||||
"--reverse",
|
||||
"--diff-filter=A",
|
||||
"--name-only",
|
||||
"--pretty=format:",
|
||||
"--",
|
||||
"data/sql/updates",
|
||||
"modules",
|
||||
])
|
||||
.args(["log", "--reverse", "--diff-filter=A", "--name-only", "--pretty=format:", "--", "data/sql/updates", "modules"])
|
||||
.output()
|
||||
.unwrap();
|
||||
let mut rank = std::collections::HashMap::new();
|
||||
for (i, path) in String::from_utf8_lossy(&out.stdout)
|
||||
.lines()
|
||||
.filter(|l| l.ends_with(".sql"))
|
||||
.enumerate()
|
||||
{
|
||||
for (i, path) in String::from_utf8_lossy(&out.stdout).lines().filter(|l| l.ends_with(".sql")).enumerate() {
|
||||
rank.entry(path.to_string()).or_insert(i);
|
||||
}
|
||||
let key = |m: &Migration| -> usize {
|
||||
origin
|
||||
.get(&(m.db.clone(), m.id.clone()))
|
||||
.and_then(|p| rank.get(p))
|
||||
.copied()
|
||||
.unwrap_or(usize::MAX)
|
||||
origin.get(&(m.db.clone(), m.id.clone())).and_then(|p| rank.get(p)).copied().unwrap_or(usize::MAX)
|
||||
};
|
||||
list.sort_by_key(|m| key(m));
|
||||
println!(
|
||||
"candidate migrations: {} ({} not found in history)",
|
||||
list.len(),
|
||||
list.iter().filter(|m| key(m) == usize::MAX).count()
|
||||
);
|
||||
println!("candidate migrations: {} ({} not found in history)", list.len(), list.iter().filter(|m| key(m) == usize::MAX).count());
|
||||
|
||||
let admin = Db::from_repack(&root, Account::Admin).unwrap();
|
||||
let _ = &admin;
|
||||
let t = std::time::Instant::now();
|
||||
let report = backup::with_database(&root, |db| {
|
||||
let before = status(db, &list)?;
|
||||
let pending = before
|
||||
.iter()
|
||||
.filter(|i| i.status == Status::Pending)
|
||||
.count();
|
||||
println!(
|
||||
"already recorded by the database: {}; pending: {pending}",
|
||||
list.len() - pending
|
||||
);
|
||||
let pending_list: Vec<Migration> = list
|
||||
.iter()
|
||||
.zip(&before)
|
||||
.filter(|(_, i)| i.status == Status::Pending)
|
||||
.map(|(m, _)| m.clone())
|
||||
.collect();
|
||||
apply_pending(db, &pending_list, &staging, &|| {
|
||||
Ok("(fixture: no backup)".into())
|
||||
})
|
||||
let pending = before.iter().filter(|i| i.status == Status::Pending).count();
|
||||
println!("already recorded by the database: {}; pending: {pending}", list.len() - pending);
|
||||
let pending_list: Vec<Migration> = list.iter().zip(&before).filter(|(_, i)| i.status == Status::Pending).map(|(m, _)| m.clone()).collect();
|
||||
apply_pending(db, &pending_list, &staging, &|| Ok("(fixture: no backup)".into()))
|
||||
})
|
||||
.expect("apply");
|
||||
println!(
|
||||
"applied {} in {:?}; failed: {:?}",
|
||||
report.applied.len(),
|
||||
t.elapsed(),
|
||||
report.failed
|
||||
);
|
||||
println!("applied {} in {:?}; failed: {:?}", report.applied.len(), t.elapsed(), report.failed);
|
||||
}
|
||||
@@ -7,48 +7,19 @@ use ed25519_dalek::{Signer, SigningKey};
|
||||
fn main() {
|
||||
let a: Vec<String> = std::env::args().skip(1).collect();
|
||||
let out = std::path::PathBuf::from(&a[0]);
|
||||
let src = out.with_file_name(format!(
|
||||
"{}-src",
|
||||
out.file_name().unwrap().to_string_lossy()
|
||||
));
|
||||
let src = out.with_file_name(format!("{}-src", out.file_name().unwrap().to_string_lossy()));
|
||||
let _ = std::fs::remove_dir_all(&src);
|
||||
let _ = std::fs::remove_dir_all(&out);
|
||||
for spec in &a[2..] {
|
||||
let (p, c) = spec.split_once('=').expect("path=content");
|
||||
let f = src.join(p);
|
||||
std::fs::create_dir_all(f.parent().unwrap()).unwrap();
|
||||
std::fs::write(f, c.replace("\\n", "\n")).unwrap();
|
||||
std::fs::write(f, c.replace("\n", "\n")).unwrap();
|
||||
}
|
||||
build(
|
||||
&src,
|
||||
&out,
|
||||
&BuildOptions {
|
||||
kind: Kind::Update,
|
||||
version: a[1].clone(),
|
||||
core_commit: None,
|
||||
built_at: chrono::Utc::now().to_rfc3339(),
|
||||
part_size: 1 << 20,
|
||||
bots_commit: None,
|
||||
migrations: vec![],
|
||||
},
|
||||
&|_| {},
|
||||
)
|
||||
.unwrap();
|
||||
let key = format!(
|
||||
"{}/.coa-manager/signing/manifest-signing.key",
|
||||
std::env::var("USERPROFILE").unwrap()
|
||||
);
|
||||
let seed: [u8; 32] = STANDARD
|
||||
.decode(std::fs::read_to_string(key).unwrap().trim())
|
||||
.unwrap()
|
||||
.try_into()
|
||||
.unwrap();
|
||||
let sig =
|
||||
SigningKey::from_bytes(&seed).sign(&std::fs::read(out.join("manifest.json")).unwrap());
|
||||
std::fs::write(
|
||||
out.join("manifest.json.sig"),
|
||||
STANDARD.encode(sig.to_bytes()),
|
||||
)
|
||||
.unwrap();
|
||||
build(&src, &out, &BuildOptions { kind: Kind::Update, version: a[1].clone(), core_commit: None, built_at: chrono::Utc::now().to_rfc3339(), part_size: 1 << 20, bots_commit: None, migrations: vec![] }, &|_| {}).unwrap();
|
||||
let key = format!("{}/.coa-manager/signing/manifest-signing.key", std::env::var("USERPROFILE").unwrap());
|
||||
let seed: [u8; 32] = STANDARD.decode(std::fs::read_to_string(key).unwrap().trim()).unwrap().try_into().unwrap();
|
||||
let sig = SigningKey::from_bytes(&seed).sign(&std::fs::read(out.join("manifest.json")).unwrap());
|
||||
std::fs::write(out.join("manifest.json.sig"), STANDARD.encode(sig.to_bytes())).unwrap();
|
||||
println!("package {} at {}", a[1], out.display());
|
||||
}
|
||||
@@ -1,40 +1,18 @@
|
||||
//! Probe only a disposable fixture: enable both worlds, verify isolation, gracefully stop both.
|
||||
use coa_core::{
|
||||
driver::{self, Verb},
|
||||
multiworld,
|
||||
process::{self, ServiceState},
|
||||
realms::{self, Mode},
|
||||
Error, Result,
|
||||
};
|
||||
use coa_core::{Error, Result, driver::{self, Verb}, realms::{self, Mode}, multiworld, process::{self, ServiceState}};
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let root = PathBuf::from(
|
||||
std::env::args()
|
||||
.nth(1)
|
||||
.ok_or_else(|| Error::Invalid("fixture path required".into()))?,
|
||||
);
|
||||
if !root
|
||||
.file_name()
|
||||
.is_some_and(|n| n.to_string_lossy().contains("fixture"))
|
||||
{
|
||||
return Err(Error::Invalid("Refusing a non-fixture server.".into()));
|
||||
}
|
||||
let root = PathBuf::from(std::env::args().nth(1).ok_or_else(|| Error::Invalid("fixture path required".into()))?);
|
||||
if !root.file_name().is_some_and(|n| n.to_string_lossy().contains("fixture")) { return Err(Error::Invalid("Refusing a non-fixture server.".into())); }
|
||||
let action = std::env::args().nth(2).unwrap_or_else(|| "smoke".into());
|
||||
if action == "stop" {
|
||||
driver::run(&root, Verb::StopAll)?;
|
||||
return Ok(());
|
||||
}
|
||||
if action == "stop" { driver::run(&root, Verb::StopAll)?; return Ok(()); }
|
||||
driver::run(&root, Verb::StopAll)?;
|
||||
if action == "wildcard" {
|
||||
realms::select(&root, Mode::Wildcard)?;
|
||||
}
|
||||
if action == "wildcard" { realms::select(&root, Mode::Wildcard)?; }
|
||||
multiworld::set_enabled(&root, true)?;
|
||||
let result = (|| {
|
||||
let out = driver::run(&root, Verb::StartAll)?;
|
||||
if !out.ok {
|
||||
return Err(Error::Invalid(out.output));
|
||||
}
|
||||
if !out.ok { return Err(Error::Invalid(out.output)); }
|
||||
let first = process::observe(&root, &coa_core::layout::read_ports(&root));
|
||||
let second_root = multiworld::root(&root);
|
||||
let second = process::observe(&second_root, &coa_core::layout::read_ports(&second_root));
|
||||
@@ -44,32 +22,21 @@ fn main() -> Result<()> {
|
||||
assert_ne!(first.world.port, second.world.port);
|
||||
assert!(second.mysql.pid.is_none() && second.auth.pid.is_none());
|
||||
let db = coa_core::db::Db::from_repack(&root, coa_core::db::Account::Admin)?;
|
||||
let realms = db.query(
|
||||
"SELECT id,port,flag FROM acore_auth.realmlist WHERE id IN (1,2) ORDER BY id;",
|
||||
)?;
|
||||
let realms = db.query("SELECT id,port,flag FROM acore_auth.realmlist WHERE id IN (1,2) ORDER BY id;")?;
|
||||
println!("Two distinct world PIDs; shared auth/MySQL; realm rows:\n{realms}");
|
||||
for mode in [Mode::Coa, Mode::Wildcard] {
|
||||
let problems = coa_core::schema_check::check(&db.clone().for_realm(mode), &root)?;
|
||||
println!("{}: {} database problems", mode.name(), problems.len());
|
||||
for p in problems.iter().take(5) {
|
||||
println!("{}.{}.{}: {}", p.database, p.table, p.column, p.detail);
|
||||
}
|
||||
for p in problems.iter().take(5) { println!("{}.{}.{}: {}", p.database, p.table, p.column, p.detail); }
|
||||
}
|
||||
assert!(multiworld::set_enabled(&root, false).is_err());
|
||||
assert!(realms::select(&root, Mode::Wildcard).is_err());
|
||||
Ok(())
|
||||
})();
|
||||
let stopped = driver::run(&root, Verb::StopAll)?;
|
||||
if !stopped.ok {
|
||||
return Err(Error::Invalid(stopped.output));
|
||||
}
|
||||
if !stopped.ok { return Err(Error::Invalid(stopped.output)); }
|
||||
assert!(!multiworld::is_running(&root));
|
||||
assert_eq!(
|
||||
process::observe(&root, &coa_core::layout::read_ports(&root))
|
||||
.mysql
|
||||
.state,
|
||||
ServiceState::Stopped
|
||||
);
|
||||
assert_eq!(process::observe(&root, &coa_core::layout::read_ports(&root)).mysql.state, ServiceState::Stopped);
|
||||
result?;
|
||||
println!("Both worlds shut down before the shared database.");
|
||||
Ok(())
|
||||
|
||||
@@ -3,19 +3,12 @@ fn main() {
|
||||
let ports = coa_core::layout::read_ports(std::path::Path::new(&root));
|
||||
println!("LAN address: {:?}", coa_core::net::lan_ip());
|
||||
for e in coa_core::net::exposure(&ports) {
|
||||
println!(
|
||||
"{:<14} port {:<5} listening={} reachable_from_network={}",
|
||||
e.what, e.port, e.listening, e.reachable_from_network
|
||||
);
|
||||
println!("{:<14} port {:<5} listening={} reachable_from_network={}", e.what, e.port, e.listening, e.reachable_from_network);
|
||||
}
|
||||
println!("{:?}", coa_core::net::tailscale());
|
||||
println!("firewall rules: {:?}", coa_core::firewall::status());
|
||||
match coa_core::upnp::discover() {
|
||||
Some(gw) => println!(
|
||||
"UPnP router: {} -> outside address {:?}",
|
||||
gw.control_url,
|
||||
coa_core::upnp::external_ip(&gw)
|
||||
),
|
||||
Some(gw) => println!("UPnP router: {} -> outside address {:?}", gw.control_url, coa_core::upnp::external_ip(&gw)),
|
||||
None => println!("UPnP router: none answered"),
|
||||
}
|
||||
}
|
||||
@@ -1,680 +0,0 @@
|
||||
//! Command-line harness for the portable-characters round trip, for manual smoke tests against a **disposable** realm database.
|
||||
//! It performs exactly the operations the Manager performs (`coa_core::portable::realm`), one per invocation, with a Manager
|
||||
//! store kept in a directory of your choice. It is not a user interface and not part of the shipped Manager.
|
||||
//!
|
||||
//! ```text
|
||||
//! portable_smoke --tools <mysql bin dir> --port <port> --store <dir> [--user root] [--realm coa] <command> [args]
|
||||
//! the database password is read from the environment variable COA_DB_PASSWORD
|
||||
//!
|
||||
//! commands
|
||||
//! list characters of the realm and why they cannot be exported
|
||||
//! make-portable <server-id> <guid> export an offline character into the store (revision 1)
|
||||
//! import <character-id> <server-id> <account> create the character on a STOPPED realm
|
||||
//! begin-session <character-id> <server-id> freeze B0: run it after the realm's first load/save, before play
|
||||
//! reconcile <character-id> <server-id> [--close] bring what was played back into the store
|
||||
//! update <character-id> <server-id> bring the realm's own character to the newest revision, in place
|
||||
//! recover <server-id> finish or abort interrupted imports and updates
|
||||
//! show <character-id> revisions and realm bindings
|
||||
//! session-offer <character-id> <server-id> <offer.json> (owner store) offer a character and open its runtime session
|
||||
//! session-import <offer.json> <account> <server-id> (host store: --host-store) keep the copy, import it with the session
|
||||
//! armed (online through the core's import service when --ra-port and --job-dir are given, else offline)
|
||||
//! host-run <owner-store-dir> <server-id> (host store) run the Host: automatic baseline, a checkpoint every
|
||||
//! --interval seconds (default 60), the final checkpoint at logout; messages go straight to the owner store
|
||||
//! dump <character-id> the canonical JSON of the current revision (what a core import job carries)
|
||||
//! collection-states <out.json> (owner store) the canonical account collections as messages
|
||||
//! collection-apply <states.json> <account> <server-id> write the canonical collections to the realm account (known ids only, INSERT IGNORE)
|
||||
//! capabilities <server-id> the realm's content profile (from its core when --ra-port is given), remembered in the store
|
||||
//! preflight <character-id> <server-id> [online|session] what would happen to the character on that realm, before anything is written
|
||||
//! reevaluate <character-id> <server-id> look again at what was held back when the realm's content profile changed (realm stopped)
|
||||
//! collection-add <kind> <id>... (owner store) add ids to a profile collection by hand (a test aid)
|
||||
//! collection-show [<account>] the Owner's collections (revision, count, hash) and, with an account, the realm's
|
||||
//! project <character-id> <server-id> ask the realm's running core what a level-cap projection of the character holds (needs --ra-port
|
||||
//! and --job-dir); --out <file> writes the decision, which a later `import`/`update` of a STOPPED realm can be given with
|
||||
//! --projection <file> (it answers only for the exact state of the character it was made for)
|
||||
//!
|
||||
//! --collection-interval <seconds> how often host-run looks at the account collections (default 300; a session start and the final
|
||||
//! checkpoint always look)
|
||||
//! --data-dir <realm Data dir> the destination's client data (Appearances.dbc, VanityCollection.dbc): without it no selected
|
||||
//! appearance and no collection is written to the realm (everything stays canonical)
|
||||
//! ```
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use coa_core::db::Db;
|
||||
use coa_core::portable::realm::{self, ImportOptions};
|
||||
use coa_core::portable::session::live::LiveBridge;
|
||||
use coa_core::portable::session::protocol::SessionOffer;
|
||||
use coa_core::portable::session::{HostConfig, HostService, OwnerService};
|
||||
use coa_core::portable::{CharacterId, Store};
|
||||
use coa_core::realms::Mode;
|
||||
|
||||
/// The options of a command, with the realm's content profile: assembled from its schema, its client data and (given an RA port) its
|
||||
/// core, and remembered in the store. Without `--data-dir` there is no profile and nothing is evaluated.
|
||||
fn with_profile(
|
||||
db: &Db,
|
||||
store: &mut Store,
|
||||
base: &ImportOptions,
|
||||
data_dir: &str,
|
||||
ra_port: Option<u16>,
|
||||
ra_user: &str,
|
||||
server: &str,
|
||||
) -> Result<ImportOptions, String> {
|
||||
if data_dir.is_empty() {
|
||||
return Ok(base.clone());
|
||||
}
|
||||
let registry = coa_core::portable::extension::ExtensionRegistry::new();
|
||||
let mut ra = ra_port.and_then(|port| {
|
||||
std::env::var("COA_RA_PASSWORD")
|
||||
.ok()
|
||||
.and_then(|pw| coa_core::ra::Ra::connect_to(port, ra_user, &pw).ok())
|
||||
});
|
||||
let mut caps = realm::profile::probe_capabilities(
|
||||
db,
|
||||
Some(std::path::Path::new(data_dir)),
|
||||
ra.as_mut(),
|
||||
®istry,
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
if ra.is_none() {
|
||||
let remembered = store.realm_profile(server).map_err(|e| e.to_string())?;
|
||||
caps = realm::profile::with_remembered_progression(caps, remembered.as_ref())
|
||||
.map_err(|e| e.to_string())?;
|
||||
if caps.progression.is_none() {
|
||||
eprintln!("realm {server}: its progression (level cap) has never been read from a running core: start it once and run `capabilities` with --ra-port");
|
||||
}
|
||||
}
|
||||
let change = store
|
||||
.set_realm_profile(server, &caps, if ra.is_some() { "live" } else { "offline" })
|
||||
.map_err(|e| e.to_string())?;
|
||||
eprintln!(
|
||||
"realm {server}: content profile {}{}",
|
||||
&caps.content_profile_hash[..16],
|
||||
if change.changed() {
|
||||
" (new or changed)"
|
||||
} else {
|
||||
""
|
||||
}
|
||||
);
|
||||
Ok(ImportOptions {
|
||||
capabilities: Some(std::sync::Arc::new(caps)),
|
||||
extensions: Some(std::sync::Arc::new(registry)),
|
||||
..base.clone()
|
||||
})
|
||||
}
|
||||
|
||||
fn usage() -> ! {
|
||||
eprintln!(
|
||||
"{}",
|
||||
include_str!("portable_smoke.rs")
|
||||
.lines()
|
||||
.take_while(|l| l.starts_with("//!"))
|
||||
.map(|l| l.trim_start_matches("//!").trim_start_matches(' '))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
);
|
||||
std::process::exit(2)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
if let Err(e) = run() {
|
||||
eprintln!("error: {e}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fn run() -> Result<(), String> {
|
||||
let mut args: Vec<String> = std::env::args().skip(1).collect();
|
||||
let mut take = |name: &str, default: Option<&str>| -> Result<String, String> {
|
||||
if let Some(i) = args.iter().position(|a| a == name) {
|
||||
if i + 1 >= args.len() {
|
||||
return Err(format!("{name} needs a value"));
|
||||
}
|
||||
let v = args.remove(i + 1);
|
||||
args.remove(i);
|
||||
return Ok(v);
|
||||
}
|
||||
default
|
||||
.map(str::to_string)
|
||||
.ok_or_else(|| format!("{name} is required"))
|
||||
};
|
||||
let tools = PathBuf::from(take("--tools", None)?);
|
||||
let port: u16 = take("--port", None)?
|
||||
.parse()
|
||||
.map_err(|_| "--port is not a number".to_string())?;
|
||||
let store_dir = PathBuf::from(take("--store", None)?);
|
||||
let user = take("--user", Some("root"))?;
|
||||
let realm_mode = match take("--realm", Some("coa"))?.as_str() {
|
||||
"coa" => Mode::Coa,
|
||||
other => {
|
||||
return Err(format!(
|
||||
"--realm {other}: only coa is supported (Wildcard portable transfer is not supported)"
|
||||
))
|
||||
}
|
||||
};
|
||||
let password =
|
||||
std::env::var("COA_DB_PASSWORD").map_err(|_| "set COA_DB_PASSWORD".to_string())?;
|
||||
// `with_tools` takes a 'static user name; this is a short-lived process
|
||||
let user: &'static str = Box::leak(user.into_boxed_str());
|
||||
let db = Db::with_tools(tools, port, user, &password, realm_mode);
|
||||
let mut store = Store::open(&store_dir).map_err(|e| e.to_string())?;
|
||||
let data_dir = take("--data-dir", Some(""))?;
|
||||
let knowledge = match data_dir.as_str() {
|
||||
"" => None,
|
||||
dir => Some(std::sync::Arc::new(
|
||||
coa_core::portable::realm::knowledge::RealmKnowledge::from_data_dir(
|
||||
std::path::Path::new(dir),
|
||||
)
|
||||
.map_err(|e| e.to_string())?,
|
||||
)),
|
||||
};
|
||||
let projection_file = take("--projection", Some(""))?;
|
||||
let out_file = take("--out", Some(""))?;
|
||||
let supplied = match projection_file.as_str() {
|
||||
"" => None,
|
||||
file => {
|
||||
let hold: coa_core::portable::projection::ProjectionHold =
|
||||
serde_json::from_slice(&std::fs::read(file).map_err(|e| e.to_string())?)
|
||||
.map_err(|e| format!("{file}: {e}"))?;
|
||||
Some(coa_core::portable::projection::Oracle(std::sync::Arc::new(
|
||||
coa_core::portable::projection::SuppliedDecision(hold),
|
||||
)))
|
||||
}
|
||||
};
|
||||
let opts = ImportOptions {
|
||||
game_server_users: vec![user.to_string(), "acore".to_string()],
|
||||
knowledge: knowledge.clone(),
|
||||
projection: supplied,
|
||||
..ImportOptions::default()
|
||||
};
|
||||
let host_dir = PathBuf::from(take(
|
||||
"--host-store",
|
||||
Some(&format!("{}-host", store_dir.display())),
|
||||
)?);
|
||||
let ra_port: Option<u16> = take("--ra-port", Some("0"))?
|
||||
.parse()
|
||||
.ok()
|
||||
.filter(|p| *p != 0);
|
||||
let ra_user = take("--ra-user", Some("local"))?;
|
||||
let job_dir = take("--job-dir", Some(""))?;
|
||||
let profile_for = |db: &Db, store: &mut Store, server: &str| -> Result<ImportOptions, String> {
|
||||
with_profile(db, store, &opts, &data_dir, ra_port, &ra_user, server)
|
||||
};
|
||||
let interval: u64 = take("--interval", Some("60"))?
|
||||
.parse()
|
||||
.map_err(|_| "--interval is not a number".to_string())?;
|
||||
let collection_interval: u64 = take("--collection-interval", Some("300"))?
|
||||
.parse()
|
||||
.map_err(|_| "--collection-interval is not a number".to_string())?;
|
||||
let id = |s: &str| -> Result<CharacterId, String> {
|
||||
s.parse()
|
||||
.map_err(|_| format!("{s:?} is not a character id"))
|
||||
};
|
||||
|
||||
let command = if args.is_empty() {
|
||||
usage()
|
||||
} else {
|
||||
args.remove(0)
|
||||
};
|
||||
match (command.as_str(), args.as_slice()) {
|
||||
("list", []) => {
|
||||
for c in realm::inspect_characters(&db).map_err(|e| e.to_string())? {
|
||||
println!(
|
||||
"{}\t{}\tlevel {}\trace {} class {}\t{}",
|
||||
c.local_guid,
|
||||
c.name,
|
||||
c.level,
|
||||
c.race,
|
||||
c.class,
|
||||
if c.eligible() {
|
||||
"ok".to_string()
|
||||
} else {
|
||||
c.blockers
|
||||
.iter()
|
||||
.map(|b| b.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join("; ")
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
("make-portable", [server, guid]) => {
|
||||
let profile = store.default_profile().map_err(|e| e.to_string())?;
|
||||
let made = realm::make_portable(
|
||||
&db,
|
||||
&mut store,
|
||||
profile,
|
||||
server,
|
||||
guid.parse().map_err(|_| "guid")?,
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
println!(
|
||||
"portable character {} (revision {}) {}",
|
||||
made.character_id,
|
||||
made.revision,
|
||||
made.warnings.join("; ")
|
||||
);
|
||||
}
|
||||
("import", [character, server, account]) => {
|
||||
let opts = profile_for(&db, &mut store, server)?;
|
||||
let o = realm::import_character(
|
||||
&db,
|
||||
&mut store,
|
||||
id(character)?,
|
||||
server,
|
||||
account.parse().map_err(|_| "account")?,
|
||||
&opts,
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
for line in &o.not_applied {
|
||||
println!(" not applied: {line}");
|
||||
}
|
||||
println!(
|
||||
"imported as local character {} \"{}\" ({} items, {} pets, renamed: {})",
|
||||
o.local_guid, o.final_name, o.items, o.pets, o.renamed
|
||||
);
|
||||
}
|
||||
("begin-session", [character, server]) => {
|
||||
let s = realm::begin_session(&db, &mut store, id(character)?, server)
|
||||
.map_err(|e| e.to_string())?;
|
||||
println!("baseline captured at canonical revision {}: {} item(s) held back by the realm, {} of its own", s.c0_revision, s.items_filtered, s.items_realm_local);
|
||||
}
|
||||
("reconcile", [character, server, rest @ ..]) => {
|
||||
let close = rest.iter().any(|a| a == "--close");
|
||||
let o = realm::reconcile_session(
|
||||
&db,
|
||||
&mut store,
|
||||
id(character)?,
|
||||
server,
|
||||
close,
|
||||
Some("portable_smoke"),
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
println!(
|
||||
"canonical revision {}{}",
|
||||
o.revision,
|
||||
if o.new_revision {
|
||||
" (new)"
|
||||
} else {
|
||||
" (unchanged)"
|
||||
}
|
||||
);
|
||||
for c in o.changes {
|
||||
println!(" {c}");
|
||||
}
|
||||
}
|
||||
("update", [character, server]) => {
|
||||
let opts = profile_for(&db, &mut store, server)?;
|
||||
let o = realm::update_realm_character(&db, &mut store, id(character)?, server, &opts)
|
||||
.map_err(|e| e.to_string())?;
|
||||
for line in &o.warnings {
|
||||
println!(" {line}");
|
||||
}
|
||||
println!(
|
||||
"{} (revision {} -> {}): {:?}",
|
||||
if o.updated {
|
||||
"updated in place"
|
||||
} else {
|
||||
"already up to date"
|
||||
},
|
||||
o.from_revision,
|
||||
o.to_revision,
|
||||
o.counts
|
||||
);
|
||||
}
|
||||
("recover", [server]) => {
|
||||
for r in
|
||||
realm::recover_imports(&db, &mut store, server, &opts).map_err(|e| e.to_string())?
|
||||
{
|
||||
println!("{}: {:?}", r.import_id, r.resolution);
|
||||
}
|
||||
}
|
||||
("session-offer", [character, server, out]) => {
|
||||
let offer = OwnerService::new(&mut store)
|
||||
.offer(id(character)?, server)
|
||||
.map_err(|e| e.to_string())?;
|
||||
std::fs::write(
|
||||
out,
|
||||
serde_json::to_vec_pretty(&offer).map_err(|e| e.to_string())?,
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
println!(
|
||||
"session {} offered at canonical revision {} -> {out}",
|
||||
offer.session_id, offer.canonical_revision
|
||||
);
|
||||
}
|
||||
("session-import", [file, account, server]) => {
|
||||
let offer: SessionOffer =
|
||||
serde_json::from_slice(&std::fs::read(file).map_err(|e| e.to_string())?)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let mut host = Store::open(&host_dir).map_err(|e| e.to_string())?;
|
||||
let profile = host.default_profile().map_err(|e| e.to_string())?;
|
||||
HostService::new(&mut host, server, HostConfig::default())
|
||||
.accept_offer(profile, &offer)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let account: u32 = account.parse().map_err(|_| "account")?;
|
||||
let opts = with_profile(&db, &mut host, &opts, &data_dir, ra_port, &ra_user, server)?;
|
||||
let outcome = match (ra_port, job_dir.is_empty()) {
|
||||
(Some(port), false) => {
|
||||
let mut ra = coa_core::ra::Ra::connect_to(
|
||||
port,
|
||||
&ra_user,
|
||||
&std::env::var("COA_RA_PASSWORD")
|
||||
.map_err(|_| "set COA_RA_PASSWORD".to_string())?,
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
realm::online::import_character_online(
|
||||
&mut ra,
|
||||
&mut host,
|
||||
offer.character_id,
|
||||
server,
|
||||
account,
|
||||
&opts,
|
||||
std::path::Path::new(&job_dir),
|
||||
Some(offer.session_id),
|
||||
)
|
||||
.map_err(|e| e.to_string())?
|
||||
}
|
||||
_ => realm::import_character_in_session(
|
||||
&db,
|
||||
&mut host,
|
||||
offer.character_id,
|
||||
server,
|
||||
account,
|
||||
&opts,
|
||||
Some(offer.session_id),
|
||||
)
|
||||
.map_err(|e| e.to_string())?,
|
||||
};
|
||||
HostService::new(&mut host, server, HostConfig::default())
|
||||
.bind(offer.session_id, outcome.local_guid)
|
||||
.map_err(|e| e.to_string())?;
|
||||
println!("imported as local character {} \"{}\"; session {} is armed: the realm takes the baseline at its first load", outcome.local_guid, outcome.final_name, offer.session_id);
|
||||
}
|
||||
("host-run", [owner_dir, server]) => {
|
||||
let mut owner = Store::open(&PathBuf::from(owner_dir)).map_err(|e| e.to_string())?;
|
||||
let mut host = Store::open(&host_dir).map_err(|e| e.to_string())?;
|
||||
let mut service = HostService::new(
|
||||
&mut host,
|
||||
server,
|
||||
HostConfig {
|
||||
checkpoint_interval_secs: interval,
|
||||
collection_interval_secs: collection_interval,
|
||||
},
|
||||
);
|
||||
let start = std::time::Instant::now();
|
||||
println!("host running for {server}; Ctrl-C to stop");
|
||||
loop {
|
||||
let ra = ra_port.and_then(|port| {
|
||||
std::env::var("COA_RA_PASSWORD")
|
||||
.ok()
|
||||
.and_then(|pw| coa_core::ra::Ra::connect_to(port, &ra_user, &pw).ok())
|
||||
});
|
||||
let mut bridge = match ra {
|
||||
Some(ra) => LiveBridge::new(&db, ra),
|
||||
None => LiveBridge::without_console(&db),
|
||||
}
|
||||
.with_knowledge(knowledge.clone());
|
||||
match service.tick(&mut bridge, start.elapsed().as_secs()) {
|
||||
Ok(events) => events.iter().for_each(|e| println!("{e:?}")),
|
||||
Err(e) => eprintln!("tick failed: {e}"),
|
||||
}
|
||||
for m in service.outbox().map_err(|e| e.to_string())? {
|
||||
let ack = if m.started {
|
||||
OwnerService::new(&mut owner).handle_started(&m.bytes)
|
||||
} else {
|
||||
OwnerService::new(&mut owner).handle_checkpoint(&m.bytes)
|
||||
};
|
||||
match ack {
|
||||
Ok(ack) => {
|
||||
println!(
|
||||
"owner: session {} #{}: {:?} (canonical revision {})",
|
||||
ack.session_id, ack.sequence, ack.outcome, ack.canonical_revision
|
||||
);
|
||||
if let Err(e) = service.receive_ack(&mut bridge, &ack) {
|
||||
eprintln!("acknowledgement failed: {e}");
|
||||
}
|
||||
}
|
||||
Err(e) => eprintln!("owner refused a message: {e}"),
|
||||
}
|
||||
}
|
||||
for m in service.collection_outbox().map_err(|e| e.to_string())? {
|
||||
match OwnerService::new(&mut owner).handle_collection(&m.bytes) {
|
||||
Ok(ack) => {
|
||||
println!("owner: {} of account {}: {:?} (collection revision {}, canonical sent back: {})", ack.kind, m.account, ack.outcome, ack.collection_revision, ack.canonical.is_some());
|
||||
match service.receive_collection_ack(&mut bridge, m.account, &ack) {
|
||||
Ok(Some(applied)) => {
|
||||
println!("host: applied to the realm: {applied:?}")
|
||||
}
|
||||
Ok(None) => {}
|
||||
Err(e) => eprintln!("collection acknowledgement failed: {e}"),
|
||||
}
|
||||
}
|
||||
Err(e) => eprintln!("owner refused a collection message: {e}"),
|
||||
}
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_secs(1));
|
||||
}
|
||||
}
|
||||
("collection-states", [out]) => {
|
||||
let states = OwnerService::new(&mut store)
|
||||
.collection_states()
|
||||
.map_err(|e| e.to_string())?;
|
||||
for s in &states {
|
||||
println!(
|
||||
"{}: revision {}, {} ids",
|
||||
s.kind, s.collection_revision, s.set.count
|
||||
);
|
||||
}
|
||||
std::fs::write(out, serde_json::to_vec(&states).map_err(|e| e.to_string())?)
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
("collection-apply", [file, account, server]) => {
|
||||
let states: Vec<coa_core::portable::session::protocol::CollectionState> =
|
||||
serde_json::from_slice(&std::fs::read(file).map_err(|e| e.to_string())?)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let account: u32 = account.parse().map_err(|_| "account")?;
|
||||
let mut host = Store::open(&host_dir).map_err(|e| e.to_string())?;
|
||||
let mut bridge = LiveBridge::without_console(&db).with_knowledge(knowledge.clone());
|
||||
let mut service = HostService::new(&mut host, server, HostConfig::default());
|
||||
for state in &states {
|
||||
let applied = service
|
||||
.receive_collection_state(&mut bridge, account, state)
|
||||
.map_err(|e| e.to_string())?;
|
||||
println!("{}: {:?}", state.kind, applied);
|
||||
}
|
||||
}
|
||||
("capabilities", [server]) => {
|
||||
let caps = profile_for(&db, &mut store, server)?
|
||||
.capabilities
|
||||
.ok_or("give --data-dir to read the realm's client data")?;
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&*caps).map_err(|e| e.to_string())?
|
||||
);
|
||||
}
|
||||
("preflight", [character, server, rest @ ..]) => {
|
||||
let opts = profile_for(&db, &mut store, server)?;
|
||||
let caps = opts
|
||||
.capabilities
|
||||
.clone()
|
||||
.ok_or("give --data-dir to read the realm's client data")?;
|
||||
let model = store
|
||||
.load_current(id(character)?)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let operation = match rest.first().map(String::as_str) {
|
||||
Some("online") => coa_core::portable::compat::Operation::OnlineImport,
|
||||
Some("session") => coa_core::portable::compat::Operation::RuntimeSession,
|
||||
_ if store
|
||||
.server_mappings(model.character_id)
|
||||
.map_err(|e| e.to_string())?
|
||||
.iter()
|
||||
.any(|m| &m.server_id == server) =>
|
||||
{
|
||||
coa_core::portable::compat::Operation::Update
|
||||
}
|
||||
_ => coa_core::portable::compat::Operation::OfflineImport,
|
||||
};
|
||||
let report =
|
||||
coa_core::portable::compat::evaluate(&coa_core::portable::compat::Inputs {
|
||||
operation,
|
||||
model: &model,
|
||||
capabilities: &caps,
|
||||
knowledge: opts.knowledge.as_deref(),
|
||||
collections: &[],
|
||||
extensions: opts.extensions.as_deref(),
|
||||
projection_decider: opts.projection.is_some()
|
||||
|| matches!(
|
||||
operation,
|
||||
coa_core::portable::compat::Operation::OnlineImport
|
||||
| coa_core::portable::compat::Operation::RuntimeSession
|
||||
) && ra_port.is_some(),
|
||||
});
|
||||
println!(
|
||||
"{} on {server} (content profile {}): {:?}",
|
||||
report.operation,
|
||||
&caps.content_profile_hash[..16],
|
||||
report.verdict()
|
||||
);
|
||||
for o in &report.outcomes {
|
||||
println!(" {o}");
|
||||
}
|
||||
}
|
||||
("project", [character, server]) => {
|
||||
let port = ra_port
|
||||
.ok_or("give --ra-port: only a running core can say what a projection holds")?;
|
||||
if job_dir.is_empty() {
|
||||
return Err("give --job-dir (the core's PortableImport.JobDir)".into());
|
||||
}
|
||||
let opts = profile_for(&db, &mut store, server)?;
|
||||
let canonical = store
|
||||
.load_current(id(character)?)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let mut ra = coa_core::ra::Ra::connect_to(
|
||||
port,
|
||||
&ra_user,
|
||||
&std::env::var("COA_RA_PASSWORD").map_err(|_| "set COA_RA_PASSWORD".to_string())?,
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let progression = opts
|
||||
.capabilities
|
||||
.as_ref()
|
||||
.and_then(|c| c.progression.clone());
|
||||
match realm::project::decide_with_core(
|
||||
&mut ra,
|
||||
std::path::Path::new(&job_dir),
|
||||
&canonical,
|
||||
)
|
||||
.map_err(|e| e.to_string())?
|
||||
{
|
||||
coa_core::portable::projection::Decision::Native => println!(
|
||||
"not projected: level {} is within the realm's cap {}",
|
||||
canonical.progression.level,
|
||||
progression.map_or("?".to_string(), |p| p.max_player_level.to_string())
|
||||
),
|
||||
coa_core::portable::projection::Decision::Projected(hold) => {
|
||||
println!(
|
||||
"projected level {} -> {} (signature {})",
|
||||
hold.canonical_level,
|
||||
hold.projected_level,
|
||||
&hold.progression_signature[..16]
|
||||
);
|
||||
println!(" held: {} item(s), {} ability(ies), {} button(s), {} build record(s) edited, {} blocked", hold.held_items.len(), hold.held_spells.len(), hold.held_actions.len(), hold.settings.iter().filter(|s| !s.entries.is_empty() || !s.buttons.is_empty()).count(), hold.blocked_settings.len());
|
||||
if !out_file.is_empty() {
|
||||
std::fs::write(
|
||||
&out_file,
|
||||
serde_json::to_vec_pretty(&hold).map_err(|e| e.to_string())?,
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
println!(" decision written to {out_file}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
("reevaluate", [character, server]) => {
|
||||
let opts = profile_for(&db, &mut store, server)?;
|
||||
let r =
|
||||
realm::reevaluate_realm_character(&db, &mut store, id(character)?, server, &opts)
|
||||
.map_err(|e| e.to_string())?;
|
||||
if r.profile_unchanged {
|
||||
println!("the realm's content profile is the one this character was synchronised under: nothing to look at");
|
||||
} else {
|
||||
println!(
|
||||
"{}",
|
||||
r.update
|
||||
.map(|u| format!("restored: {:?}", u.changes))
|
||||
.unwrap_or_else(|| "nothing new to show on this realm".into())
|
||||
);
|
||||
for e in r.extensions {
|
||||
println!(" {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
("collection-add", [kind, ids @ ..]) => {
|
||||
let profile = store.default_profile().map_err(|e| e.to_string())?;
|
||||
let ids = ids
|
||||
.iter()
|
||||
.map(|i| i.parse::<u32>().map_err(|_| format!("{i:?} is not an id")))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let set =
|
||||
coa_core::portable::collection::IdSet::from_ids(ids).map_err(|e| e.to_string())?;
|
||||
let merge = store
|
||||
.merge_collection(profile, kind, &set)
|
||||
.map_err(|e| e.to_string())?;
|
||||
println!(
|
||||
"{kind}: {} id(s) added{}",
|
||||
merge.added,
|
||||
merge
|
||||
.info
|
||||
.map(|i| format!(", revision {}, {} ids", i.revision, i.count))
|
||||
.unwrap_or_default()
|
||||
);
|
||||
}
|
||||
("collection-show", rest) => {
|
||||
let profile = store.default_profile().map_err(|e| e.to_string())?;
|
||||
for kind in coa_core::portable::session::protocol::COLLECTION_KINDS {
|
||||
match store
|
||||
.collection_info(profile, kind)
|
||||
.map_err(|e| e.to_string())?
|
||||
{
|
||||
Some(i) => println!(
|
||||
"owner {kind}: revision {}, {} ids, hash {}",
|
||||
i.revision,
|
||||
i.count,
|
||||
hex::encode(&i.hash[..8])
|
||||
),
|
||||
None => println!("owner {kind}: none"),
|
||||
}
|
||||
if let [account] = rest {
|
||||
let account: u32 = account.parse().map_err(|_| "account")?;
|
||||
let set = coa_core::portable::realm::collections::read_set(&db, account, kind)
|
||||
.map_err(|e| e.to_string())?;
|
||||
println!("realm {kind} of account {account}: {} ids", set.len());
|
||||
}
|
||||
}
|
||||
}
|
||||
("dump", [character]) => {
|
||||
let model = store
|
||||
.load_current(id(character)?)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let json =
|
||||
coa_core::portable::snapshot::canonical_json(&model).map_err(|e| e.to_string())?;
|
||||
println!("{}", String::from_utf8_lossy(&json));
|
||||
}
|
||||
("show", [character]) => {
|
||||
let c = id(character)?;
|
||||
let r = store.character(c).map_err(|e| e.to_string())?;
|
||||
println!(
|
||||
"{} {} level {} revision {}",
|
||||
r.character_id, r.name, r.level, r.revision
|
||||
);
|
||||
for m in store.server_mappings(c).map_err(|e| e.to_string())? {
|
||||
println!(
|
||||
" on {}: local guid {} at revision {} ({:?})",
|
||||
m.server_id, m.local_guid, m.last_revision, m.state
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => usage(),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,12 +1,7 @@
|
||||
//! Create an account through the Manager's RA client on a RUNNING disposable fixture.
|
||||
fn main() {
|
||||
let a: Vec<String> = std::env::args().skip(1).collect();
|
||||
assert!(
|
||||
a[0].to_lowercase().contains("fixture")
|
||||
|| a[0].to_lowercase().contains("installtest")
|
||||
|| a[0].to_lowercase().contains("cleaninstall"),
|
||||
"refusing: not a disposable folder"
|
||||
);
|
||||
assert!(a[0].to_lowercase().contains("fixture") || a[0].to_lowercase().contains("installtest") || a[0].to_lowercase().contains("cleaninstall"), "refusing: not a disposable folder");
|
||||
let root = std::path::Path::new(&a[0]);
|
||||
let mut ra = coa_core::ra::Ra::connect(root).expect("connect");
|
||||
ra.create_account(&a[1], &a[2]).expect("create");
|
||||
|
||||
@@ -13,69 +13,30 @@ fn main() -> Result<()> {
|
||||
let action = args.get(1).map(String::as_str).unwrap_or("view");
|
||||
match action {
|
||||
"select" => {
|
||||
let mode = if args.get(2).map(String::as_str) == Some("wildcard") {
|
||||
Mode::Wildcard
|
||||
} else {
|
||||
Mode::Coa
|
||||
};
|
||||
let mode = if args.get(2).map(String::as_str) == Some("wildcard") { Mode::Wildcard } else { Mode::Coa };
|
||||
println!("{}", serde_json::to_string(&realms::select(root, mode)?)?);
|
||||
}
|
||||
"start" | "stop" => {
|
||||
let out = driver::run(
|
||||
root,
|
||||
if action == "start" {
|
||||
Verb::StartAll
|
||||
} else {
|
||||
Verb::StopAll
|
||||
},
|
||||
)?;
|
||||
let out = driver::run(root, if action == "start" { Verb::StartAll } else { Verb::StopAll })?;
|
||||
println!("{}", serde_json::to_string(&out)?);
|
||||
if !out.ok {
|
||||
return Err(Error::Invalid("Fixture operation failed".into()));
|
||||
}
|
||||
if !out.ok { return Err(Error::Invalid("Fixture operation failed".into())); }
|
||||
}
|
||||
"probe" => {
|
||||
let db = Db::from_repack(root, Account::Admin)?;
|
||||
let s = realms::state(root)?;
|
||||
println!(
|
||||
"{}",
|
||||
db.query("SELECT * FROM acore_characters.manager_profile_probe;")?
|
||||
);
|
||||
println!(
|
||||
"active={} chars={} world={}",
|
||||
s.active.name(),
|
||||
s.active.schema("characters")?,
|
||||
s.active.schema("world")?
|
||||
);
|
||||
println!("{}", db.query("SELECT * FROM acore_characters.manager_profile_probe;")?);
|
||||
println!("active={} chars={} world={}", s.active.name(), s.active.schema("characters")?, s.active.schema("world")?);
|
||||
}
|
||||
"mark" => {
|
||||
Db::from_repack(root, Account::Admin)?
|
||||
.query("INSERT INTO acore_characters.manager_profile_probe VALUES (22);")?;
|
||||
Db::from_repack(root, Account::Admin)?.query("INSERT INTO acore_characters.manager_profile_probe VALUES (22);")?;
|
||||
}
|
||||
"backup" => {
|
||||
use coa_core::backup::{self, Kind, Trigger};
|
||||
let meta = root.join("fixture-manager");
|
||||
let point = backup::create(
|
||||
root,
|
||||
&meta,
|
||||
Kind::Full,
|
||||
Trigger::Manual,
|
||||
Some("realm isolation check".into()),
|
||||
&|step| println!("{step}"),
|
||||
)?;
|
||||
let point = backup::create(root, &meta, Kind::Full, Trigger::Manual, Some("realm isolation check".into()), &|step| println!("{step}"))?;
|
||||
assert_eq!(point.realm, Mode::Wildcard);
|
||||
for name in [
|
||||
"characters",
|
||||
"world",
|
||||
"auth",
|
||||
"coa-characters",
|
||||
"coa-world",
|
||||
"configs",
|
||||
] {
|
||||
assert!(
|
||||
point.components.iter().any(|c| c.name == name),
|
||||
"Missing {name}"
|
||||
);
|
||||
for name in ["characters", "world", "auth", "coa-characters", "coa-world", "configs"] {
|
||||
assert!(point.components.iter().any(|c| c.name == name), "Missing {name}");
|
||||
}
|
||||
assert!(backup::verify(&meta, &point.id)?.ok);
|
||||
println!("Verified both realm backups: {}", point.id);
|
||||
@@ -85,10 +46,7 @@ fn main() -> Result<()> {
|
||||
use coa_core::manifest::{Manifest, Migration};
|
||||
use coa_core::update::{Env, RepackEnv};
|
||||
let meta = root.join("fixture-manager");
|
||||
let env = RepackEnv {
|
||||
root,
|
||||
meta_dir: &meta,
|
||||
};
|
||||
let env = RepackEnv { root, meta_dir: &meta };
|
||||
env.ensure_stopped()?;
|
||||
let staged = root.join("fixture-migrations");
|
||||
let mut manifest: Manifest = serde_json::from_value(serde_json::json!({
|
||||
@@ -100,13 +58,7 @@ fn main() -> Result<()> {
|
||||
let path = staged.join(format!("{kind}/manager_realm_fixture.sql"));
|
||||
std::fs::create_dir_all(path.parent().unwrap())?;
|
||||
std::fs::write(&path, sql)?;
|
||||
manifest.migrations.push(Migration {
|
||||
compatible_sha256: vec![],
|
||||
id: "manager_realm_fixture".into(),
|
||||
db: kind.into(),
|
||||
sha256: coa_core::fsx::sha256_file(&path)?,
|
||||
destructive: false,
|
||||
});
|
||||
manifest.migrations.push(Migration { compatible_sha256: vec![], id: "manager_realm_fixture".into(), db: kind.into(), sha256: coa_core::fsx::sha256_file(&path)?, destructive: false });
|
||||
}
|
||||
let result = env.migrate(&manifest, &staged)?;
|
||||
assert!(result.failed.is_none());
|
||||
@@ -114,12 +66,7 @@ fn main() -> Result<()> {
|
||||
for mode in [Mode::Coa, Mode::Wildcard] {
|
||||
let realm = db.clone().for_realm(mode);
|
||||
for kind in ["characters", "world", "auth"] {
|
||||
assert_eq!(
|
||||
realm.query(&format!(
|
||||
"SELECT COUNT(*) FROM acore_{kind}.manager_migration_probe;"
|
||||
))?,
|
||||
"1"
|
||||
);
|
||||
assert_eq!(realm.query(&format!("SELECT COUNT(*) FROM acore_{kind}.manager_migration_probe;"))?, "1");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -132,16 +79,8 @@ fn main() -> Result<()> {
|
||||
})?;
|
||||
backup::restore_database(root, &meta, &point.id, "characters")?;
|
||||
backup::with_database(root, |db| {
|
||||
assert_eq!(
|
||||
db.query("SELECT COUNT(*) FROM acore_characters.manager_profile_probe;")?,
|
||||
"0"
|
||||
);
|
||||
assert_eq!(
|
||||
db.clone()
|
||||
.for_realm(Mode::Coa)
|
||||
.query("SELECT value FROM acore_characters.manager_profile_probe;")?,
|
||||
"11"
|
||||
);
|
||||
assert_eq!(db.query("SELECT COUNT(*) FROM acore_characters.manager_profile_probe;")?, "0");
|
||||
assert_eq!(db.clone().for_realm(Mode::Coa).query("SELECT value FROM acore_characters.manager_profile_probe;")?, "11");
|
||||
Ok(())
|
||||
})?;
|
||||
println!("Both realms migrated once; shared auth migrated once; Wildcard restore left CoA intact.");
|
||||
|
||||
@@ -1,29 +1,12 @@
|
||||
//! Signed-package repair smoke test. Only a disposable fixture may be used.
|
||||
use base64::{engine::general_purpose::STANDARD, Engine};
|
||||
use coa_core::{
|
||||
fsx,
|
||||
manifest::{Kind, Migration},
|
||||
package::{self, BuildOptions},
|
||||
pkgsource::Source,
|
||||
realms::Mode,
|
||||
registry::{metadata_dir_for, InstallKind, InstallMeta, MetaDir},
|
||||
Error, Result,
|
||||
};
|
||||
use ed25519_dalek::{Signer, SigningKey};
|
||||
use std::path::PathBuf;
|
||||
use base64::{engine::general_purpose::STANDARD, Engine};
|
||||
use ed25519_dalek::{Signer, SigningKey};
|
||||
use coa_core::{Result, Error, fsx, package::{self, BuildOptions}, manifest::{Kind, Migration}, registry::{InstallKind, InstallMeta, MetaDir, metadata_dir_for}, pkgsource::Source, realms::Mode};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let root = PathBuf::from(
|
||||
std::env::args()
|
||||
.nth(1)
|
||||
.ok_or_else(|| Error::Invalid("fixture path required".into()))?,
|
||||
);
|
||||
if !root
|
||||
.file_name()
|
||||
.is_some_and(|n| n.to_string_lossy().contains("fixture"))
|
||||
{
|
||||
return Err(Error::Invalid("Refusing a non-fixture server.".into()));
|
||||
}
|
||||
let root = PathBuf::from(std::env::args().nth(1).ok_or_else(|| Error::Invalid("fixture path required".into()))?);
|
||||
if !root.file_name().is_some_and(|n| n.to_string_lossy().contains("fixture")) { return Err(Error::Invalid("Refusing a non-fixture server.".into())); }
|
||||
let work = tempfile::tempdir()?;
|
||||
let base_tree = work.path().join("base-tree");
|
||||
let update_tree = work.path().join("update-tree");
|
||||
@@ -34,54 +17,15 @@ fn main() -> Result<()> {
|
||||
fsx::atomic_write(&update_tree.join(probe), b"correct official file")?;
|
||||
let sql = "CREATE TABLE IF NOT EXISTS acore_world.manager_repair_probe (id INT PRIMARY KEY); INSERT IGNORE INTO acore_world.manager_repair_probe VALUES (42);";
|
||||
let id = "20261004_repair_e2e";
|
||||
fsx::atomic_write(
|
||||
&update_tree.join(format!("_migrations/world/{id}.sql")),
|
||||
sql.as_bytes(),
|
||||
)?;
|
||||
let options = |kind, version: &str, migrations| BuildOptions {
|
||||
kind,
|
||||
version: version.into(),
|
||||
core_commit: None,
|
||||
built_at: "fixture".into(),
|
||||
part_size: 1 << 20,
|
||||
bots_commit: None,
|
||||
migrations,
|
||||
};
|
||||
let base = package::build(
|
||||
&base_tree,
|
||||
&base_pkg,
|
||||
&options(Kind::Base, "1.0.0", vec![]),
|
||||
&|_| {},
|
||||
)?;
|
||||
let update = package::build(
|
||||
&update_tree,
|
||||
&update_pkg,
|
||||
&options(
|
||||
Kind::Update,
|
||||
"1.0.1",
|
||||
vec![Migration {
|
||||
compatible_sha256: vec![],
|
||||
id: id.into(),
|
||||
db: "world".into(),
|
||||
sha256: fsx::sha256_bytes(sql.as_bytes()),
|
||||
destructive: false,
|
||||
}],
|
||||
),
|
||||
&|_| {},
|
||||
)?;
|
||||
fsx::atomic_write(&update_tree.join(format!("_migrations/world/{id}.sql")), sql.as_bytes())?;
|
||||
let options = |kind, version: &str, migrations| BuildOptions { kind, version: version.into(), core_commit: None, built_at: "fixture".into(), part_size: 1 << 20, bots_commit: None, migrations };
|
||||
let base = package::build(&base_tree, &base_pkg, &options(Kind::Base, "1.0.0", vec![]), &|_| {})?;
|
||||
let update = package::build(&update_tree, &update_pkg, &options(Kind::Update, "1.0.1", vec![Migration { compatible_sha256: vec![], id: id.into(), db: "world".into(), sha256: fsx::sha256_bytes(sql.as_bytes()), destructive: false }]), &|_| {})?;
|
||||
// A test-only trust key, unrelated to production signing material.
|
||||
let key = SigningKey::from_bytes(&[23; 32]);
|
||||
let public = STANDARD.encode(key.verifying_key().as_bytes());
|
||||
for pkg in [&base_pkg, &update_pkg] {
|
||||
fsx::atomic_write(
|
||||
&pkg.join("manifest.json.sig"),
|
||||
STANDARD
|
||||
.encode(
|
||||
key.sign(&std::fs::read(pkg.join("manifest.json"))?)
|
||||
.to_bytes(),
|
||||
)
|
||||
.as_bytes(),
|
||||
)?;
|
||||
fsx::atomic_write(&pkg.join("manifest.json.sig"), STANDARD.encode(key.sign(&std::fs::read(pkg.join("manifest.json"))?).to_bytes()).as_bytes())?;
|
||||
}
|
||||
let dir = metadata_dir_for(&root)?;
|
||||
let mut meta = InstallMeta::new(InstallKind::New, &root);
|
||||
@@ -91,43 +35,24 @@ fn main() -> Result<()> {
|
||||
fsx::atomic_write_json(&dir.join("manifests/update-1.0.1.json"), &update)?;
|
||||
fsx::atomic_write(&root.join(probe), b"corrupted")?;
|
||||
let settings = std::fs::read(root.join("Settings/repack.json"))?;
|
||||
let counts = coa_core::backup::with_database(&root, |db| {
|
||||
db.query("SELECT COUNT(*) FROM acore_auth.account; SELECT COUNT(*) FROM acore_characters.characters; SELECT COUNT(*) FROM acore_characters_wildcard.characters;")
|
||||
})?;
|
||||
let report = coa_core::repair::run(
|
||||
&root,
|
||||
&dir,
|
||||
&Source::Dir(base_pkg),
|
||||
&Source::Dir(update_pkg),
|
||||
&public,
|
||||
&|s, p| println!("{p}% {s}"),
|
||||
)?;
|
||||
let counts = coa_core::backup::with_database(&root, |db| db.query("SELECT COUNT(*) FROM acore_auth.account; SELECT COUNT(*) FROM acore_characters.characters; SELECT COUNT(*) FROM acore_characters_wildcard.characters;"))?;
|
||||
let report = coa_core::repair::run(&root, &dir, &Source::Dir(base_pkg), &Source::Dir(update_pkg), &public, &|s,p| println!("{p}% {s}"))?;
|
||||
assert_eq!(std::fs::read(root.join(probe))?, b"correct official file");
|
||||
assert!(report.error.is_none(), "{:?}", report.error);
|
||||
assert_eq!(report.applied.len(), 2, "Migration must run in both worlds");
|
||||
assert!(report.database.iter().all(|d| d.problems.is_empty()
|
||||
&& d.migrations
|
||||
.iter()
|
||||
.all(|m| m.status == coa_core::migrations::Status::Applied)));
|
||||
assert!(report.database.iter().all(|d| d.problems.is_empty() && d.migrations.iter().all(|m| m.status == coa_core::migrations::Status::Applied)));
|
||||
assert_eq!(std::fs::read(root.join("Settings/repack.json"))?, settings);
|
||||
assert!(coa_core::backup::verify(&dir, &report.backup)?.ok);
|
||||
coa_core::backup::with_database(&root, |db| {
|
||||
assert_eq!(db.query("SELECT COUNT(*) FROM acore_auth.account; SELECT COUNT(*) FROM acore_characters.characters; SELECT COUNT(*) FROM acore_characters_wildcard.characters;")?, counts);
|
||||
for mode in [Mode::Coa, Mode::Wildcard] {
|
||||
assert_eq!(
|
||||
db.clone()
|
||||
.for_realm(mode)
|
||||
.query("SELECT id FROM acore_world.manager_repair_probe;")?,
|
||||
"42"
|
||||
);
|
||||
assert_eq!(db.clone().for_realm(mode).query("SELECT id FROM acore_world.manager_repair_probe;")?, "42");
|
||||
}
|
||||
// Corrupt only the second realm, proving that checks read its actual schema.
|
||||
let wildcard = db.clone().for_realm(Mode::Wildcard);
|
||||
wildcard.query("ALTER TABLE acore_characters.character_action DROP COLUMN type;")?;
|
||||
let problems = coa_core::schema_check::check(&wildcard, &root)?;
|
||||
assert!(problems
|
||||
.iter()
|
||||
.any(|p| p.table == "character_action" && p.column == "type"));
|
||||
assert!(problems.iter().any(|p| p.table == "character_action" && p.column == "type"));
|
||||
assert!(coa_core::schema_check::check(&db.clone().for_realm(Mode::Coa), &root)?.is_empty());
|
||||
wildcard.query("ALTER TABLE acore_characters.character_action ADD COLUMN type TINYINT UNSIGNED NOT NULL DEFAULT 0;")?;
|
||||
Ok(())
|
||||
|
||||
@@ -3,10 +3,7 @@ fn main() {
|
||||
let url = std::env::args().nth(1).unwrap();
|
||||
let mode = std::env::args().nth(2).unwrap();
|
||||
let t = std::time::Instant::now();
|
||||
let client = reqwest::blocking::Client::builder()
|
||||
.timeout(None::<std::time::Duration>)
|
||||
.build()
|
||||
.unwrap();
|
||||
let client = reqwest::blocking::Client::builder().timeout(None::<std::time::Duration>).build().unwrap();
|
||||
let mut resp = client.get(&url).send().unwrap();
|
||||
if mode == "bytes" {
|
||||
let b = resp.bytes().unwrap();
|
||||
@@ -16,9 +13,7 @@ fn main() {
|
||||
let mut total = 0;
|
||||
loop {
|
||||
let n = resp.read(&mut buf).unwrap();
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
if n == 0 { break; }
|
||||
total += n;
|
||||
}
|
||||
println!("read loop {} in {:?}", total, t.elapsed());
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
use std::path::Path;
|
||||
|
||||
fn main() {
|
||||
let path = std::env::args()
|
||||
.nth(1)
|
||||
.expect("usage: scan <server folder>");
|
||||
let path = std::env::args().nth(1).expect("usage: scan <server folder>");
|
||||
let root = Path::new(&path);
|
||||
let report = coa_core::layout::scan(root).expect("scan failed");
|
||||
println!("{}", serde_json::to_string_pretty(&report).unwrap());
|
||||
|
||||
@@ -1,18 +1,9 @@
|
||||
//! Check full schema contracts and blocking custom columns, only against a disposable fixture.
|
||||
use coa_core::{backup, realms::Mode, schema_check, Error, Result};
|
||||
use coa_core::{Result, Error, backup, schema_check, realms::Mode};
|
||||
use std::path::PathBuf;
|
||||
fn main() -> Result<()> {
|
||||
let root = PathBuf::from(
|
||||
std::env::args()
|
||||
.nth(1)
|
||||
.ok_or_else(|| Error::Invalid("fixture path required".into()))?,
|
||||
);
|
||||
if !root
|
||||
.file_name()
|
||||
.is_some_and(|n| n.to_string_lossy().contains("fixture"))
|
||||
{
|
||||
return Err(Error::Invalid("Refusing a non-fixture server.".into()));
|
||||
}
|
||||
let root = PathBuf::from(std::env::args().nth(1).ok_or_else(|| Error::Invalid("fixture path required".into()))?);
|
||||
if !root.file_name().is_some_and(|n| n.to_string_lossy().contains("fixture")) { return Err(Error::Invalid("Refusing a non-fixture server.".into())); }
|
||||
backup::with_database(&root, |db| {
|
||||
let coa = db.clone().for_realm(Mode::Coa);
|
||||
let wildcard = db.clone().for_realm(Mode::Wildcard);
|
||||
@@ -23,14 +14,11 @@ fn main() -> Result<()> {
|
||||
assert!(schema_check::check(&coa, reference.path())?.is_empty());
|
||||
wildcard.query("ALTER TABLE acore_characters.character_action ADD COLUMN manager_required INT NOT NULL;")?;
|
||||
let result = (|| {
|
||||
assert!(schema_check::check(&wildcard, &root)?
|
||||
.iter()
|
||||
.any(|p| p.table == "character_action" && p.column == "manager_required"));
|
||||
assert!(schema_check::check(&wildcard, &root)?.iter().any(|p| p.table == "character_action" && p.column == "manager_required"));
|
||||
assert!(schema_check::check(&coa, &root)?.is_empty());
|
||||
Ok(())
|
||||
})();
|
||||
wildcard
|
||||
.query("ALTER TABLE acore_characters.character_action DROP COLUMN manager_required;")?;
|
||||
wildcard.query("ALTER TABLE acore_characters.character_action DROP COLUMN manager_required;")?;
|
||||
result
|
||||
})?;
|
||||
println!("Full schema contract and a blocking NOT NULL column verified; second-realm corruption does not affect CoA checks.");
|
||||
|
||||
@@ -9,26 +9,13 @@ fn main() {
|
||||
let seed_b64 = match std::env::var("COA_SIGNING_KEY") {
|
||||
Ok(v) => v,
|
||||
Err(_) => {
|
||||
let path = a.next().unwrap_or_else(|| {
|
||||
format!(
|
||||
"{}/.coa-manager/signing/manifest-signing.key",
|
||||
std::env::var("USERPROFILE").unwrap()
|
||||
)
|
||||
});
|
||||
let path = a.next().unwrap_or_else(|| format!("{}/.coa-manager/signing/manifest-signing.key", std::env::var("USERPROFILE").unwrap()));
|
||||
fs::read_to_string(path).expect("cannot read signing key")
|
||||
}
|
||||
};
|
||||
let seed: [u8; 32] = STANDARD
|
||||
.decode(seed_b64.trim())
|
||||
.unwrap()
|
||||
.try_into()
|
||||
.expect("key length");
|
||||
let seed: [u8; 32] = STANDARD.decode(seed_b64.trim()).unwrap().try_into().expect("key length");
|
||||
let bytes = fs::read(&manifest).unwrap();
|
||||
let sig = SigningKey::from_bytes(&seed).sign(&bytes);
|
||||
fs::write(
|
||||
format!("{manifest}.sig"),
|
||||
format!("{}\n", STANDARD.encode(sig.to_bytes())),
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(format!("{manifest}.sig"), format!("{}\n", STANDARD.encode(sig.to_bytes()))).unwrap();
|
||||
println!("signed {manifest}");
|
||||
}
|
||||
@@ -1,10 +1,7 @@
|
||||
//! Read-only helper for a DISPOSABLE fixture: run one query. usage: sqlq <fixture> <sql>
|
||||
fn main() {
|
||||
let a: Vec<String> = std::env::args().skip(1).collect();
|
||||
assert!(
|
||||
a[0].to_lowercase().contains("fixture"),
|
||||
"refusing: not a fixture folder"
|
||||
);
|
||||
assert!(a[0].to_lowercase().contains("fixture"), "refusing: not a fixture folder");
|
||||
let root = std::path::Path::new(&a[0]);
|
||||
let out = coa_core::backup::with_database(root, |db| db.query(&a[1])).expect("query");
|
||||
println!("{out}");
|
||||
|
||||
@@ -19,10 +19,7 @@ fn put(root: &std::path::Path, rel: &str, body: &str) {
|
||||
|
||||
fn main() {
|
||||
let root = PathBuf::from(std::env::args().nth(1).expect("fixture folder"));
|
||||
assert!(
|
||||
root.to_string_lossy().to_lowercase().contains("fixture"),
|
||||
"refusing: not a fixture folder"
|
||||
);
|
||||
assert!(root.to_string_lossy().to_lowercase().contains("fixture"), "refusing: not a fixture folder");
|
||||
let meta_dir = metadata_dir_for(&root).unwrap();
|
||||
if !meta_dir.join("install.json").is_file() {
|
||||
MetaDir::create(&root, &InstallMeta::new(InstallKind::Imported, &root)).unwrap();
|
||||
@@ -31,105 +28,33 @@ fn main() {
|
||||
let work = std::env::temp_dir().join("coa-update-e2e");
|
||||
let _ = std::fs::remove_dir_all(&work);
|
||||
let (src, pkg) = (work.join("src"), work.join("pkg"));
|
||||
put(
|
||||
&src,
|
||||
"Core/reference/update-e2e.txt",
|
||||
"shipped by the update\n",
|
||||
);
|
||||
put(
|
||||
&src,
|
||||
"Settings/worldserver.conf.template",
|
||||
"[worldserver]\nUpdateE2E.NewSetting = 7\n",
|
||||
);
|
||||
put(&src, "Core/reference/update-e2e.txt", "shipped by the update\n");
|
||||
put(&src, "Settings/worldserver.conf.template", "[worldserver]\nUpdateE2E.NewSetting = 7\n");
|
||||
let sql = "CREATE TABLE IF NOT EXISTS coa_manager_update_test (id INT PRIMARY KEY); INSERT IGNORE INTO coa_manager_update_test VALUES (1);";
|
||||
put(&src, "_migrations/world/2026_09_30_00_update_e2e.sql", sql);
|
||||
let mut m = build(
|
||||
&src,
|
||||
&pkg,
|
||||
&BuildOptions {
|
||||
kind: Kind::Update,
|
||||
version: "0.2.0".into(),
|
||||
core_commit: None,
|
||||
built_at: chrono::Utc::now().to_rfc3339(),
|
||||
part_size: 1 << 20,
|
||||
bots_commit: None,
|
||||
migrations: vec![],
|
||||
},
|
||||
&|_| {},
|
||||
)
|
||||
.unwrap();
|
||||
m.migrations.push(Migration {
|
||||
compatible_sha256: vec![],
|
||||
id: "2026_09_30_00_update_e2e".into(),
|
||||
db: "world".into(),
|
||||
sha256: fsx::sha256_bytes(sql.as_bytes()),
|
||||
destructive: false,
|
||||
});
|
||||
std::fs::write(
|
||||
pkg.join("manifest.json"),
|
||||
serde_json::to_vec_pretty(&m).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
let key = format!(
|
||||
"{}/.coa-manager/signing/manifest-signing.key",
|
||||
std::env::var("USERPROFILE").unwrap()
|
||||
);
|
||||
let seed: [u8; 32] = STANDARD
|
||||
.decode(std::fs::read_to_string(key).unwrap().trim())
|
||||
.unwrap()
|
||||
.try_into()
|
||||
.unwrap();
|
||||
let sig =
|
||||
SigningKey::from_bytes(&seed).sign(&std::fs::read(pkg.join("manifest.json")).unwrap());
|
||||
std::fs::write(
|
||||
pkg.join("manifest.json.sig"),
|
||||
STANDARD.encode(sig.to_bytes()),
|
||||
)
|
||||
.unwrap();
|
||||
let mut m = build(&src, &pkg, &BuildOptions { kind: Kind::Update, version: "0.2.0".into(), core_commit: None, built_at: chrono::Utc::now().to_rfc3339(), part_size: 1 << 20, bots_commit: None, migrations: vec![] }, &|_| {}).unwrap();
|
||||
m.migrations.push(Migration { compatible_sha256: vec![], id: "2026_09_30_00_update_e2e".into(), db: "world".into(), sha256: fsx::sha256_bytes(sql.as_bytes()), destructive: false });
|
||||
std::fs::write(pkg.join("manifest.json"), serde_json::to_vec_pretty(&m).unwrap()).unwrap();
|
||||
let key = format!("{}/.coa-manager/signing/manifest-signing.key", std::env::var("USERPROFILE").unwrap());
|
||||
let seed: [u8; 32] = STANDARD.decode(std::fs::read_to_string(key).unwrap().trim()).unwrap().try_into().unwrap();
|
||||
let sig = SigningKey::from_bytes(&seed).sign(&std::fs::read(pkg.join("manifest.json")).unwrap());
|
||||
std::fs::write(pkg.join("manifest.json.sig"), STANDARD.encode(sig.to_bytes())).unwrap();
|
||||
|
||||
let template_before = std::fs::read(root.join("Settings/worldserver.conf.template")).unwrap();
|
||||
let env = RepackEnv {
|
||||
root: &root,
|
||||
meta_dir: &meta_dir,
|
||||
};
|
||||
let env = RepackEnv { root: &root, meta_dir: &meta_dir };
|
||||
let t = std::time::Instant::now();
|
||||
let out = apply(
|
||||
&Params {
|
||||
root: &root,
|
||||
meta_dir: &meta_dir,
|
||||
source: Source::Dir(pkg),
|
||||
trusted_key: coa_core::signing::EMBEDDED_PUBLIC_KEY,
|
||||
cancel: Cancel::default(),
|
||||
resolutions: Default::default(),
|
||||
env: &env,
|
||||
fail_after_ops: None,
|
||||
},
|
||||
&Params { root: &root, meta_dir: &meta_dir, source: Source::Dir(pkg), trusted_key: coa_core::signing::EMBEDDED_PUBLIC_KEY, cancel: Cancel::default(), resolutions: Default::default(), env: &env, fail_after_ops: None },
|
||||
&|s, p| eprintln!(" {p:>3}% {s}"),
|
||||
)
|
||||
.expect("update");
|
||||
println!(
|
||||
"update {:?} in {:?}; recovery point {:?}; migrations {:?}",
|
||||
out.txn.state,
|
||||
t.elapsed(),
|
||||
out.txn.recovery_point,
|
||||
out.migrations.as_ref().map(|r| &r.applied)
|
||||
);
|
||||
println!("update {:?} in {:?}; recovery point {:?}; migrations {:?}", out.txn.state, t.elapsed(), out.txn.recovery_point, out.migrations.as_ref().map(|r| &r.applied));
|
||||
|
||||
let tpl = std::fs::read_to_string(root.join("Settings/worldserver.conf.template")).unwrap();
|
||||
assert!(tpl.contains("UpdateE2E.NewSetting = 7"), "new key merged");
|
||||
assert!(
|
||||
tpl.len() > template_before.len()
|
||||
&& tpl.starts_with(&String::from_utf8_lossy(&template_before).into_owned()),
|
||||
"existing template content untouched, key appended"
|
||||
);
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(root.join("Core/reference/update-e2e.txt")).unwrap(),
|
||||
"shipped by the update\n"
|
||||
);
|
||||
let n = backup::with_database(&root, |db| {
|
||||
db.query("SELECT COUNT(*) FROM acore_world.coa_manager_update_test;")
|
||||
})
|
||||
.unwrap();
|
||||
assert!(tpl.len() > template_before.len() && tpl.starts_with(&String::from_utf8_lossy(&template_before).into_owned()), "existing template content untouched, key appended");
|
||||
assert_eq!(std::fs::read_to_string(root.join("Core/reference/update-e2e.txt")).unwrap(), "shipped by the update\n");
|
||||
let n = backup::with_database(&root, |db| db.query("SELECT COUNT(*) FROM acore_world.coa_manager_update_test;")).unwrap();
|
||||
assert_eq!(n, "1", "the migration ran");
|
||||
println!("OK: files, merge, migration and health check verified");
|
||||
}
|
||||
@@ -1,39 +1,18 @@
|
||||
//! Failure injection on a NEW disposable fixture created by tools/make-fixture.ps1.
|
||||
//! Uses real MySQL dumps, SQL execution, signed packages and update rollback.
|
||||
use base64::Engine;
|
||||
use coa_core::{
|
||||
backup,
|
||||
db::{Account, Db},
|
||||
download::Cancel,
|
||||
driver::{self, Verb},
|
||||
fsx,
|
||||
manifest::{Kind, Migration},
|
||||
package::{self, BuildOptions},
|
||||
pkgsource::Source,
|
||||
use std::{collections::BTreeMap, path::{Path, PathBuf}};
|
||||
use coa_core::{backup, db::{Account, Db}, download::Cancel, driver::{self, Verb}, fsx,
|
||||
manifest::{Kind, Migration}, package::{self, BuildOptions}, pkgsource::Source,
|
||||
registry::{InstallKind, InstallMeta, MetaDir},
|
||||
update::{self, Env, RepackEnv},
|
||||
Error, Result,
|
||||
};
|
||||
update::{self, Env, RepackEnv}, Error, Result};
|
||||
use base64::Engine;
|
||||
use ed25519_dalek::{Signer, SigningKey};
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let arg = std::env::args()
|
||||
.nth(1)
|
||||
.expect("usage: update_recovery_e2e <new fixture>");
|
||||
let arg = std::env::args().nth(1).expect("usage: update_recovery_e2e <new fixture>");
|
||||
let root = PathBuf::from(arg);
|
||||
assert!(root
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.starts_with("coa-update-recovery-fixture-"));
|
||||
assert!(
|
||||
!root.join("recovery-e2e-started").exists(),
|
||||
"Use a new fixture; never reuse an owned server"
|
||||
);
|
||||
assert!(root.file_name().unwrap().to_string_lossy().starts_with("coa-update-recovery-fixture-"));
|
||||
assert!(!root.join("recovery-e2e-started").exists(), "Use a new fixture; never reuse an owned server");
|
||||
fsx::atomic_write(&root.join("recovery-e2e-started"), b"disposable fixture")?;
|
||||
let result = exercise(&root);
|
||||
let stopped = driver::run(&root, Verb::StopAll)?;
|
||||
@@ -55,119 +34,43 @@ fn exercise(root: &Path) -> Result<()> {
|
||||
fsx::atomic_write(&source.join("Core/recovery-probe.txt"), b"updated")?;
|
||||
let mut migrations = Vec::new();
|
||||
for (db, id, sql) in [
|
||||
(
|
||||
"characters",
|
||||
"probe_chars",
|
||||
"UPDATE manager_recovery_probe SET id=999;",
|
||||
),
|
||||
(
|
||||
"world",
|
||||
"probe_world",
|
||||
"DROP TABLE manager_recovery_probe; THIS IS INTENTIONALLY INVALID SQL;",
|
||||
),
|
||||
("characters", "probe_chars", "UPDATE manager_recovery_probe SET id=999;"),
|
||||
("world", "probe_world", "DROP TABLE manager_recovery_probe; THIS IS INTENTIONALLY INVALID SQL;"),
|
||||
] {
|
||||
fsx::atomic_write(
|
||||
&source.join(format!("_migrations/{db}/{id}.sql")),
|
||||
sql.as_bytes(),
|
||||
)?;
|
||||
migrations.push(Migration {
|
||||
compatible_sha256: vec![],
|
||||
db: db.into(),
|
||||
id: id.into(),
|
||||
sha256: fsx::sha256_bytes(sql.as_bytes()),
|
||||
destructive: false,
|
||||
});
|
||||
fsx::atomic_write(&source.join(format!("_migrations/{db}/{id}.sql")), sql.as_bytes())?;
|
||||
migrations.push(Migration { compatible_sha256: vec![], db: db.into(), id: id.into(), sha256: fsx::sha256_bytes(sql.as_bytes()), destructive: false });
|
||||
}
|
||||
let pkg = root.join("fixture-package");
|
||||
let manifest = package::build(
|
||||
&source,
|
||||
&pkg,
|
||||
&BuildOptions {
|
||||
kind: Kind::Update,
|
||||
version: "2.0.0".into(),
|
||||
core_commit: None,
|
||||
bots_commit: None,
|
||||
built_at: "fixture".into(),
|
||||
part_size: 1 << 20,
|
||||
migrations,
|
||||
},
|
||||
&|_| {},
|
||||
)?;
|
||||
let manifest = package::build(&source, &pkg, &BuildOptions {
|
||||
kind: Kind::Update, version: "2.0.0".into(), core_commit: None, bots_commit: None,
|
||||
built_at: "fixture".into(), part_size: 1 << 20, migrations,
|
||||
}, &|_| {})?;
|
||||
let key = SigningKey::generate(&mut rand_core::OsRng);
|
||||
let signature = base64::engine::general_purpose::STANDARD.encode(
|
||||
key.sign(&std::fs::read(pkg.join("manifest.json"))?)
|
||||
.to_bytes(),
|
||||
);
|
||||
let signature = base64::engine::general_purpose::STANDARD.encode(key.sign(&std::fs::read(pkg.join("manifest.json"))?).to_bytes());
|
||||
fsx::atomic_write(&pkg.join("manifest.json.sig"), signature.as_bytes())?;
|
||||
let trusted = base64::engine::general_purpose::STANDARD.encode(key.verifying_key().to_bytes());
|
||||
let env = RepackEnv {
|
||||
root,
|
||||
meta_dir: &meta,
|
||||
};
|
||||
let error = update::apply(
|
||||
&update::Params {
|
||||
root,
|
||||
meta_dir: &meta,
|
||||
source: Source::Dir(pkg),
|
||||
trusted_key: &trusted,
|
||||
cancel: Cancel::default(),
|
||||
resolutions: BTreeMap::new(),
|
||||
env: &env,
|
||||
fail_after_ops: None,
|
||||
},
|
||||
&|step, pct| println!("{pct}% {step}"),
|
||||
)
|
||||
.unwrap_err();
|
||||
let env = RepackEnv { root, meta_dir: &meta };
|
||||
let error = update::apply(&update::Params {
|
||||
root, meta_dir: &meta, source: Source::Dir(pkg), trusted_key: &trusted,
|
||||
cancel: Cancel::default(), resolutions: BTreeMap::new(), env: &env, fail_after_ops: None,
|
||||
}, &|step, pct| println!("{pct}% {step}" )).unwrap_err();
|
||||
assert!(error.to_string().contains("probe_world"), "{error}");
|
||||
assert!(
|
||||
error
|
||||
.to_string()
|
||||
.contains("private update rehearsal failed"),
|
||||
"{error}"
|
||||
);
|
||||
assert!(error.to_string().contains("private update rehearsal failed"), "{error}");
|
||||
assert!(!root.join("Core/recovery-probe.txt").exists());
|
||||
assert!(update::unfinished(&meta).is_none());
|
||||
assert_eq!(
|
||||
MetaDir::open(&meta)?.1.core.version.as_deref(),
|
||||
Some("1.0.0")
|
||||
);
|
||||
assert_eq!(MetaDir::open(&meta)?.1.core.version.as_deref(), Some("1.0.0"));
|
||||
backup::with_database(root, |db| {
|
||||
assert_eq!(
|
||||
db.query("SELECT id FROM acore_characters.manager_recovery_probe;")?,
|
||||
"42"
|
||||
);
|
||||
assert_eq!(
|
||||
db.query("SELECT id FROM acore_world.manager_recovery_probe;")?,
|
||||
"43"
|
||||
);
|
||||
assert_eq!(
|
||||
db.query("SELECT id FROM acore_characters.manager_recovery_view;")?,
|
||||
"42"
|
||||
);
|
||||
assert_eq!(
|
||||
db.recovery_objects("acore_characters")?
|
||||
.iter()
|
||||
.filter(|object| object["name"]
|
||||
.as_str()
|
||||
.is_some_and(|name| name.starts_with("manager_recovery_")))
|
||||
.count(),
|
||||
3
|
||||
);
|
||||
assert!(!coa_core::migrations::status(db, &manifest.migrations)?
|
||||
.iter()
|
||||
.any(|m| m.status == coa_core::migrations::Status::Applied));
|
||||
assert_eq!(db.query("SELECT id FROM acore_characters.manager_recovery_probe;")?, "42");
|
||||
assert_eq!(db.query("SELECT id FROM acore_world.manager_recovery_probe;")?, "43");
|
||||
assert_eq!(db.query("SELECT id FROM acore_characters.manager_recovery_view;")?, "42");
|
||||
assert_eq!(db.recovery_objects("acore_characters")?.iter().filter(|object| object["name"].as_str().is_some_and(|name| name.starts_with("manager_recovery_"))).count(), 3);
|
||||
assert!(!coa_core::migrations::status(db, &manifest.migrations)?.iter().any(|m| m.status == coa_core::migrations::Status::Applied));
|
||||
Ok(())
|
||||
})?;
|
||||
// A recovery point can be reused if the process died midway through restoring multiple schemas.
|
||||
let point = backup::list(&meta)
|
||||
.into_iter()
|
||||
.find(|p| p.trigger == backup::Trigger::BeforeUpdate)
|
||||
.ok_or_else(|| Error::Invalid("missing backup".into()))?;
|
||||
let point = backup::list(&meta).into_iter().find(|p| p.trigger == backup::Trigger::BeforeUpdate).ok_or_else(|| Error::Invalid("missing backup".into()))?;
|
||||
env.restore_snapshot(&point.id)?;
|
||||
assert_eq!(
|
||||
Db::from_repack(root, Account::Admin)?.realm(),
|
||||
coa_core::realms::Mode::Coa
|
||||
);
|
||||
assert_eq!(Db::from_repack(root, Account::Admin)?.realm(), coa_core::realms::Mode::Coa);
|
||||
println!("PASS: private rehearsal caught failed SQL before changing the installed world, characters, ledger, objects, files or version; complete recovery succeeded");
|
||||
Ok(())
|
||||
}
|
||||
@@ -3,28 +3,15 @@ use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
let root = PathBuf::from(std::env::args().nth(1).expect("disposable fixture path"));
|
||||
assert_eq!(
|
||||
std::fs::read(root.join(".release-schema-fixture")).unwrap(),
|
||||
b"disposable release-schema fixture"
|
||||
);
|
||||
assert_eq!(std::fs::read(root.join(".release-schema-fixture")).unwrap(), b"disposable release-schema fixture");
|
||||
let meta = coa_core::registry::metadata_dir_for(&root).unwrap();
|
||||
let env = RepackEnv {
|
||||
root: &root,
|
||||
meta_dir: &meta,
|
||||
};
|
||||
let env = RepackEnv { root: &root, meta_dir: &meta };
|
||||
let result = env.validate();
|
||||
let _ = coa_core::driver::run(&root, coa_core::driver::Verb::StopAll);
|
||||
if let Err(error) = result {
|
||||
eprintln!("{}", error);
|
||||
for log in [
|
||||
"Core/Logs/auth-console.log",
|
||||
"Core/Logs/world-console.log",
|
||||
"Core/Logs/supervisor.log",
|
||||
] {
|
||||
eprintln!(
|
||||
"{log}:\n{}",
|
||||
coa_core::diag::redact(&coa_core::health::tail(&root.join(log), 8192))
|
||||
);
|
||||
for log in ["Core/Logs/auth-console.log", "Core/Logs/world-console.log", "Core/Logs/supervisor.log"] {
|
||||
eprintln!("{log}:\n{}", coa_core::diag::redact(&coa_core::health::tail(&root.join(log), 8192)));
|
||||
}
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
@@ -8,56 +8,18 @@ use coa_core::update::{apply, preview, Params, RepackEnv};
|
||||
fn main() {
|
||||
let a: Vec<String> = std::env::args().skip(1).collect();
|
||||
let root = std::path::PathBuf::from(&a[0]);
|
||||
assert!(
|
||||
a[0].to_lowercase().contains("installtest"),
|
||||
"refusing: not a disposable test install"
|
||||
);
|
||||
assert!(a[0].to_lowercase().contains("installtest"), "refusing: not a disposable test install");
|
||||
let dir = metadata_dir_for(&root).unwrap();
|
||||
let (_, meta) = MetaDir::open(&dir).unwrap();
|
||||
let src = Source::Url(a[1].clone());
|
||||
let p = preview(
|
||||
&root,
|
||||
&meta,
|
||||
&src,
|
||||
coa_core::signing::EMBEDDED_PUBLIC_KEY,
|
||||
&Default::default(),
|
||||
)
|
||||
.unwrap_or_else(|e| panic!("preview failed: {e:?}"));
|
||||
println!(
|
||||
"preview: {} -> {} | download {} bytes | {} files change | {} migrations | conflicts {:?}",
|
||||
p.from_version.clone().unwrap_or_default(),
|
||||
p.to_version,
|
||||
p.download_bytes,
|
||||
p.items
|
||||
.iter()
|
||||
.filter(|i| format!("{:?}", i.action) != "Skip")
|
||||
.count(),
|
||||
p.migrations,
|
||||
p.conflicts
|
||||
);
|
||||
let env = RepackEnv {
|
||||
root: &root,
|
||||
meta_dir: &dir,
|
||||
};
|
||||
let p = preview(&root, &meta, &src, coa_core::signing::EMBEDDED_PUBLIC_KEY, &Default::default()).unwrap_or_else(|e| panic!("preview failed: {e:?}"));
|
||||
println!("preview: {} -> {} | download {} bytes | {} files change | {} migrations | conflicts {:?}", p.from_version.clone().unwrap_or_default(), p.to_version, p.download_bytes, p.items.iter().filter(|i| format!("{:?}", i.action) != "Skip").count(), p.migrations, p.conflicts);
|
||||
let env = RepackEnv { root: &root, meta_dir: &dir };
|
||||
let t = std::time::Instant::now();
|
||||
let out = apply(
|
||||
&Params {
|
||||
root: &root,
|
||||
meta_dir: &dir,
|
||||
source: src,
|
||||
trusted_key: coa_core::signing::EMBEDDED_PUBLIC_KEY,
|
||||
cancel: Cancel::default(),
|
||||
resolutions: Default::default(),
|
||||
env: &env,
|
||||
fail_after_ops: None,
|
||||
},
|
||||
&Params { root: &root, meta_dir: &dir, source: src, trusted_key: coa_core::signing::EMBEDDED_PUBLIC_KEY, cancel: Cancel::default(), resolutions: Default::default(), env: &env, fail_after_ops: None },
|
||||
&|s, pct| eprintln!(" {pct:>3}% {s}"),
|
||||
)
|
||||
.unwrap_or_else(|e| panic!("apply failed: {e:?}"));
|
||||
println!(
|
||||
"apply finished in {:?}: state {:?}, to {:?}",
|
||||
t.elapsed(),
|
||||
out.txn.state,
|
||||
out.txn.to_version
|
||||
);
|
||||
println!("apply finished in {:?}: state {:?}, to {:?}", t.elapsed(), out.txn.state, out.txn.to_version);
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
fn main() {
|
||||
let m = std::env::args()
|
||||
.nth(1)
|
||||
.expect("usage: verify <manifest.json>");
|
||||
let m = std::env::args().nth(1).expect("usage: verify <manifest.json>");
|
||||
let bytes = std::fs::read(&m).unwrap();
|
||||
let sig = std::fs::read_to_string(format!("{m}.sig")).unwrap();
|
||||
match coa_core::signing::verify_embedded(&bytes, &sig) {
|
||||
|
||||
@@ -1,34 +1,15 @@
|
||||
//! Reproduce applied migration history with five missing tables on a disposable MySQL fixture.
|
||||
use coa_core::{
|
||||
backup, fsx,
|
||||
manifest::Migration,
|
||||
migrations::{self, LedgerRow, Status, Store},
|
||||
release_schema, schema_check, Error, Result,
|
||||
};
|
||||
use std::path::PathBuf;
|
||||
use coa_core::{backup, fsx, manifest::Migration, migrations::{self, LedgerRow, Status, Store}, release_schema, schema_check, Error, Result};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let mut args = std::env::args().skip(1);
|
||||
let root = PathBuf::from(
|
||||
args.next()
|
||||
.ok_or_else(|| Error::Invalid("fixture path required".into()))?,
|
||||
);
|
||||
let old_sql = PathBuf::from(
|
||||
args.next()
|
||||
.ok_or_else(|| Error::Invalid("core SQL directory required".into()))?,
|
||||
);
|
||||
let repairs = PathBuf::from(
|
||||
args.next()
|
||||
.ok_or_else(|| Error::Invalid("repair SQL directory required".into()))?,
|
||||
);
|
||||
if !root.file_name().is_some_and(|n| {
|
||||
n.to_string_lossy()
|
||||
.starts_with("coa-update-recovery-fixture-")
|
||||
}) || !root.join("recovery-e2e-started").exists()
|
||||
{
|
||||
return Err(Error::Invalid(
|
||||
"Refusing a non-disposable recovery fixture.".into(),
|
||||
));
|
||||
let root = PathBuf::from(args.next().ok_or_else(|| Error::Invalid("fixture path required".into()))?);
|
||||
let old_sql = PathBuf::from(args.next().ok_or_else(|| Error::Invalid("core SQL directory required".into()))?);
|
||||
let repairs = PathBuf::from(args.next().ok_or_else(|| Error::Invalid("repair SQL directory required".into()))?);
|
||||
if !root.file_name().is_some_and(|n| n.to_string_lossy().starts_with("coa-update-recovery-fixture-"))
|
||||
|| !root.join("recovery-e2e-started").exists() {
|
||||
return Err(Error::Invalid("Refusing a non-disposable recovery fixture.".into()));
|
||||
}
|
||||
let tree = tempfile::tempdir()?;
|
||||
backup::with_database(&root, |db| {
|
||||
@@ -37,90 +18,38 @@ fn main() -> Result<()> {
|
||||
db.load()?;
|
||||
// Record exactly the old file hashes, as in the user's all-applied report.
|
||||
let mut migrations = Vec::new();
|
||||
for name in [
|
||||
"rev_20260929_92_coa_wildcard_skill_cards",
|
||||
"rev_20260929_93_coa_wildcard_skill_card_duplicates",
|
||||
"rev_20260929_94_coa_wildcard_specialization_cache",
|
||||
] {
|
||||
for name in ["rev_20260929_92_coa_wildcard_skill_cards", "rev_20260929_93_coa_wildcard_skill_card_duplicates", "rev_20260929_94_coa_wildcard_specialization_cache"] {
|
||||
let path = old_sql.join(format!("{name}.sql"));
|
||||
let hash = fsx::sha256_file(&path)?;
|
||||
db.put(&LedgerRow {
|
||||
db: "characters".into(),
|
||||
id: name.into(),
|
||||
sha256: hash.clone(),
|
||||
status: Status::Applied,
|
||||
error: None,
|
||||
baseline: false,
|
||||
})?;
|
||||
migrations.push(Migration {
|
||||
compatible_sha256: vec![],
|
||||
db: "characters".into(),
|
||||
id: name.into(),
|
||||
sha256: hash,
|
||||
destructive: false,
|
||||
});
|
||||
db.put(&LedgerRow { db: "characters".into(), id: name.into(), sha256: hash.clone(), status: Status::Applied, error: None, baseline: false })?;
|
||||
migrations.push(Migration { compatible_sha256: vec![], db: "characters".into(), id: name.into(), sha256: hash, destructive: false });
|
||||
}
|
||||
for (_, tables) in schema_check::WILDCARD_TABLES
|
||||
.iter()
|
||||
.filter(|(kind, _)| *kind == "characters")
|
||||
{
|
||||
for table in *tables {
|
||||
db.query(&format!("DROP TABLE IF EXISTS acore_characters.`{table}`;"))?;
|
||||
}
|
||||
for (_, tables) in schema_check::WILDCARD_TABLES.iter().filter(|(kind,_)| *kind == "characters") {
|
||||
for table in *tables { db.query(&format!("DROP TABLE IF EXISTS acore_characters.`{table}`;"))?; }
|
||||
}
|
||||
assert!(migrations::status(&db, &migrations)?
|
||||
.iter()
|
||||
.all(|m| m.status == Status::Applied));
|
||||
assert!(!db
|
||||
.tables("acore_characters")?
|
||||
.iter()
|
||||
.any(|t| t == "coa_wildcard_skill_card"));
|
||||
assert!(migrations::status(&db, &migrations)?.iter().all(|m| m.status == Status::Applied));
|
||||
assert!(!db.tables("acore_characters")?.iter().any(|t| t == "coa_wildcard_skill_card"));
|
||||
let empty_core = tempfile::tempdir()?;
|
||||
let files = release_schema::collect(empty_core.path(), None, Some(&repairs))?;
|
||||
assert_eq!(files.len(), 1);
|
||||
let repair = &files[0];
|
||||
migrations.push(Migration {
|
||||
compatible_sha256: vec![],
|
||||
db: repair.db.clone(),
|
||||
id: repair.id.clone(),
|
||||
sha256: repair.sha256.clone(),
|
||||
destructive: false,
|
||||
});
|
||||
migrations.push(Migration { compatible_sha256: vec![], db: repair.db.clone(), id: repair.id.clone(), sha256: repair.sha256.clone(), destructive: false });
|
||||
let staged = tree.path().join("sql/characters");
|
||||
fsx::atomic_write(
|
||||
&staged.join(format!("{}.sql", repair.id)),
|
||||
&std::fs::read(&repair.abs)?,
|
||||
)?;
|
||||
let report =
|
||||
migrations::apply_pending(&db, &migrations, &tree.path().join("sql"), &|| {
|
||||
Ok("disposable fixture".into())
|
||||
})?;
|
||||
fsx::atomic_write(&staged.join(format!("{}.sql", repair.id)), &std::fs::read(&repair.abs)?)?;
|
||||
let report = migrations::apply_pending(&db, &migrations, &tree.path().join("sql"), &|| Ok("disposable fixture".into()))?;
|
||||
assert!(report.failed.is_none());
|
||||
assert_eq!(&report.applied, std::slice::from_ref(&repair.id));
|
||||
assert_eq!(
|
||||
db.query("SELECT COUNT(*) FROM acore_characters.characters;")?,
|
||||
characters
|
||||
);
|
||||
assert_eq!(report.applied, [repair.id.clone()]);
|
||||
assert_eq!(db.query("SELECT COUNT(*) FROM acore_characters.characters;")?, characters);
|
||||
db.query("INSERT INTO acore_characters.coa_wildcard_skill_card VALUES (77,123,456);")?;
|
||||
db.run_sql_file("acore_characters", &repair.abs)?;
|
||||
assert_eq!(db.query("SELECT progress FROM acore_characters.coa_wildcard_skill_card WHERE account=77 AND card=123;")?, "456");
|
||||
assert!(
|
||||
migrations::apply_pending(&db, &migrations, &tree.path().join("sql"), &|| Ok(
|
||||
"disposable fixture".into()
|
||||
))?
|
||||
.applied
|
||||
.is_empty()
|
||||
);
|
||||
assert!(migrations::apply_pending(&db, &migrations, &tree.path().join("sql"), &|| Ok("disposable fixture".into()))?.applied.is_empty());
|
||||
schema_check::capture(&db, tree.path())?;
|
||||
db.query("DROP TABLE acore_characters.coa_wildcard_skill_card_purchase;")?;
|
||||
assert!(schema_check::check(&db, tree.path())?
|
||||
.iter()
|
||||
.any(|p| p.table == "coa_wildcard_skill_card_purchase" && p.detail == "Missing table"));
|
||||
assert!(schema_check::check(&db, tree.path())?.iter().any(|p| p.table == "coa_wildcard_skill_card_purchase" && p.detail == "Missing table"));
|
||||
db.run_sql_file("acore_characters", &repair.abs)?;
|
||||
db.query("ALTER TABLE acore_characters.coa_wildcard_skill_card DROP COLUMN progress;")?;
|
||||
assert!(schema_check::check(&db, tree.path())?
|
||||
.iter()
|
||||
.any(|p| p.table == "coa_wildcard_skill_card" && p.column == "progress"));
|
||||
assert!(schema_check::check(&db, tree.path())?.iter().any(|p| p.table == "coa_wildcard_skill_card" && p.column == "progress"));
|
||||
db.query("ALTER TABLE acore_characters.coa_wildcard_skill_card ADD COLUMN progress INT UNSIGNED NOT NULL DEFAULT 0;")?;
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
@@ -35,15 +35,8 @@ pub fn squid_bot_prefix(root: &Path) -> String {
|
||||
std::fs::read(root.join("Core/configs/modules/playerbots.conf"))
|
||||
.ok()
|
||||
.and_then(|bytes| crate::config::parser::ConfFile::parse_bytes(&bytes).ok())
|
||||
.and_then(|conf| {
|
||||
conf.get("AiPlayerbot.RandomBotAccountPrefix")
|
||||
.map(|v| crate::config::parser::unquote(v).to_string())
|
||||
})
|
||||
.filter(|prefix| {
|
||||
!prefix.is_empty()
|
||||
&& prefix.len() <= 32
|
||||
&& prefix.chars().all(|c| c.is_ascii_alphanumeric())
|
||||
})
|
||||
.and_then(|conf| conf.get("AiPlayerbot.RandomBotAccountPrefix").map(|v| crate::config::parser::unquote(v).to_string()))
|
||||
.filter(|prefix| !prefix.is_empty() && prefix.len() <= 32 && prefix.chars().all(|c| c.is_ascii_alphanumeric()))
|
||||
.unwrap_or_else(|| "rndbot".into())
|
||||
}
|
||||
|
||||
@@ -69,10 +62,7 @@ pub fn parse_list(out: &str) -> Vec<AccountInfo> {
|
||||
|
||||
pub fn list(root: &Path) -> Result<Vec<AccountInfo>> {
|
||||
let db = Db::from_repack(root, Account::Admin)?;
|
||||
Ok(parse_list(&db.query(&LIST_SQL.replace(
|
||||
"{SQUID}",
|
||||
&squid_bot_prefix(root).to_ascii_uppercase(),
|
||||
))?))
|
||||
Ok(parse_list(&db.query(&LIST_SQL.replace("{SQUID}", &squid_bot_prefix(root).to_ascii_uppercase()))?))
|
||||
}
|
||||
|
||||
fn upper(name: &str) -> Result<String> {
|
||||
@@ -86,32 +76,22 @@ pub fn rename(root: &Path, ra: &mut Ra, old: &str, new: &str, password: &str) ->
|
||||
let (old, new) = (upper(old)?, upper(new)?);
|
||||
validate_account(&new, password)?;
|
||||
if is_reserved(&old) || old.starts_with(&squid_bot_prefix(root).to_ascii_uppercase()) {
|
||||
return Err(Error::Invalid(
|
||||
"This account belongs to the Manager or the bots and cannot be renamed.".into(),
|
||||
));
|
||||
return Err(Error::Invalid("This account belongs to the Manager or the bots and cannot be renamed.".into()));
|
||||
}
|
||||
if old == new {
|
||||
return Err(Error::Invalid(
|
||||
"That is already the name of this account.".into(),
|
||||
));
|
||||
return Err(Error::Invalid("That is already the name of this account.".into()));
|
||||
}
|
||||
let db = Db::from_repack(root, Account::Admin)?;
|
||||
let taken = db.query(&format!(
|
||||
"SELECT COUNT(*) FROM acore_auth.account WHERE username = '{new}';"
|
||||
))?;
|
||||
let taken = db.query(&format!("SELECT COUNT(*) FROM acore_auth.account WHERE username = '{new}';"))?;
|
||||
if taken.trim() != "0" {
|
||||
return Err(Error::Invalid("That account name is already taken.".into()));
|
||||
}
|
||||
let changed = db.query(&format!("UPDATE acore_auth.account SET username = '{new}' WHERE username = '{old}' AND online = 0; SELECT ROW_COUNT();"))?;
|
||||
if changed.trim() != "1" {
|
||||
return Err(Error::Invalid(
|
||||
"The account was not renamed: it does not exist or is logged in right now.".into(),
|
||||
));
|
||||
return Err(Error::Invalid("The account was not renamed: it does not exist or is logged in right now.".into()));
|
||||
}
|
||||
if let Err(e) = ra.set_account_password(&new, password) {
|
||||
let _ = db.query(&format!(
|
||||
"UPDATE acore_auth.account SET username = '{old}' WHERE username = '{new}';"
|
||||
));
|
||||
let _ = db.query(&format!("UPDATE acore_auth.account SET username = '{old}' WHERE username = '{new}';"));
|
||||
return Err(e);
|
||||
}
|
||||
tracing::info!(%old, %new, "account renamed");
|
||||
@@ -128,33 +108,16 @@ fn is_reserved(upper_name: &str) -> bool {
|
||||
pub fn delete(root: &Path, ra: &mut Ra, name: &str) -> Result<()> {
|
||||
let name = upper(name)?;
|
||||
if is_reserved(&name) || name.starts_with(&squid_bot_prefix(root).to_ascii_uppercase()) {
|
||||
return Err(Error::Invalid(
|
||||
"This account belongs to the Manager or the bots and cannot be deleted.".into(),
|
||||
));
|
||||
return Err(Error::Invalid("This account belongs to the Manager or the bots and cannot be deleted.".into()));
|
||||
}
|
||||
let db = Db::from_repack(root, Account::Admin)?;
|
||||
match db
|
||||
.query(&format!(
|
||||
"SELECT online FROM acore_auth.account WHERE username = '{name}';"
|
||||
))?
|
||||
.trim()
|
||||
{
|
||||
match db.query(&format!("SELECT online FROM acore_auth.account WHERE username = '{name}';"))?.trim() {
|
||||
"" => return Err(Error::Invalid("That account does not exist.".into())),
|
||||
"0" => {}
|
||||
_ => {
|
||||
return Err(Error::Invalid(
|
||||
"The account is logged in right now; log it out first.".into(),
|
||||
))
|
||||
}
|
||||
_ => return Err(Error::Invalid("The account is logged in right now; log it out first.".into())),
|
||||
}
|
||||
ra.delete_account(&name)?;
|
||||
if db
|
||||
.query(&format!(
|
||||
"SELECT COUNT(*) FROM acore_auth.account WHERE username = '{name}';"
|
||||
))?
|
||||
.trim()
|
||||
!= "0"
|
||||
{
|
||||
if db.query(&format!("SELECT COUNT(*) FROM acore_auth.account WHERE username = '{name}';"))?.trim() != "0" {
|
||||
return Err(Error::Invalid("The account was not deleted.".into()));
|
||||
}
|
||||
tracing::info!(%name, "account deleted");
|
||||
@@ -170,27 +133,13 @@ mod tests {
|
||||
let out = "2\tALICE\t3\t0\t2026-09-30 21:04:11\t4\n5\tBOB\t0\t1\t\t0\n7\tBROKEN\n";
|
||||
let a = parse_list(out);
|
||||
assert_eq!(a.len(), 2, "a short row is skipped");
|
||||
assert_eq!(
|
||||
a[0],
|
||||
AccountInfo {
|
||||
id: 2,
|
||||
name: "ALICE".into(),
|
||||
access: 3,
|
||||
online: false,
|
||||
last_login: Some("2026-09-30 21:04:11".into()),
|
||||
characters: 4
|
||||
}
|
||||
);
|
||||
assert_eq!(a[0], AccountInfo { id: 2, name: "ALICE".into(), access: 3, online: false, last_login: Some("2026-09-30 21:04:11".into()), characters: 4 });
|
||||
assert!(a[1].online && a[1].last_login.is_none() && a[1].access == 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_internal_accounts_are_filtered_out_by_the_query() {
|
||||
assert!(
|
||||
LIST_SQL.contains("COABOTHOST%")
|
||||
&& LIST_SQL.contains("COAMANAGER")
|
||||
&& LIST_SQL.contains("{SQUID}%")
|
||||
);
|
||||
assert!(LIST_SQL.contains("COABOTHOST%") && LIST_SQL.contains("COAMANAGER") && LIST_SQL.contains("{SQUID}%"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -201,16 +150,8 @@ mod tests {
|
||||
std::fs::create_dir_all(conf.parent().unwrap()).unwrap();
|
||||
std::fs::write(&conf, "AiPlayerbot.RandomBotAccountPrefix = \"MyBots\"\n").unwrap();
|
||||
assert_eq!(squid_bot_prefix(root.path()), "MyBots");
|
||||
std::fs::write(
|
||||
&conf,
|
||||
"AiPlayerbot.RandomBotAccountPrefix = \"x' OR 1=1 --\"\n",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
squid_bot_prefix(root.path()),
|
||||
"rndbot",
|
||||
"an unsafe prefix is ignored"
|
||||
);
|
||||
std::fs::write(&conf, "AiPlayerbot.RandomBotAccountPrefix = \"x' OR 1=1 --\"\n").unwrap();
|
||||
assert_eq!(squid_bot_prefix(root.path()), "rndbot", "an unsafe prefix is ignored");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -226,10 +167,6 @@ mod tests {
|
||||
#[test]
|
||||
fn names_are_validated_and_stored_in_capitals() {
|
||||
assert_eq!(upper("Alice1").unwrap(), "ALICE1");
|
||||
assert!(
|
||||
upper("a").is_err()
|
||||
&& upper("x; DROP TABLE account").is_err()
|
||||
&& upper("bad name").is_err()
|
||||
);
|
||||
assert!(upper("a").is_err() && upper("x; DROP TABLE account").is_err() && upper("bad name").is_err());
|
||||
}
|
||||
}
|
||||
@@ -39,21 +39,7 @@ fn hidden(key: &str) -> bool {
|
||||
|| k.starts_with("logindatabase.")
|
||||
|| k.starts_with("worlddatabase.")
|
||||
|| k.starts_with("characterdatabase.")
|
||||
|| [
|
||||
"bindip",
|
||||
"realmid",
|
||||
"datadir",
|
||||
"logsdir",
|
||||
"tempdir",
|
||||
"sourcedirectory",
|
||||
"mysqlexecutable",
|
||||
"cmakecommand",
|
||||
"builddirectory",
|
||||
"pidfile",
|
||||
"vmapdir",
|
||||
"mmapdir",
|
||||
]
|
||||
.contains(&k.as_str())
|
||||
|| ["bindip", "realmid", "datadir", "logsdir", "tempdir", "sourcedirectory", "mysqlexecutable", "cmakecommand", "builddirectory", "pidfile", "vmapdir", "mmapdir"].contains(&k.as_str())
|
||||
}
|
||||
|
||||
fn doc_of(block: &[String]) -> String {
|
||||
@@ -64,11 +50,7 @@ fn doc_of(block: &[String]) -> String {
|
||||
continue;
|
||||
}
|
||||
// the lines that only name the settings the block describes
|
||||
let named = l.starts_with("# ")
|
||||
&& !l.starts_with("# ")
|
||||
&& body
|
||||
.chars()
|
||||
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'));
|
||||
let named = l.starts_with("# ") && !l.starts_with("# ") && body.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'));
|
||||
if named {
|
||||
continue;
|
||||
}
|
||||
@@ -106,10 +88,7 @@ fn parse_dist(text: &str) -> Vec<(String, String, String)> {
|
||||
}
|
||||
if let Some((k, v)) = t.split_once('=') {
|
||||
let k = k.trim();
|
||||
if !k.is_empty()
|
||||
&& k.chars()
|
||||
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'))
|
||||
{
|
||||
if !k.is_empty() && k.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-')) {
|
||||
out.push((k.to_string(), v.trim().to_string(), doc.clone()));
|
||||
}
|
||||
}
|
||||
@@ -122,30 +101,16 @@ fn dist_path(root: &Path) -> std::path::PathBuf {
|
||||
}
|
||||
|
||||
pub fn list(root: &Path) -> Result<Vec<Item>> {
|
||||
let dist = fs::read_to_string(dist_path(root)).map_err(|_| {
|
||||
Error::Invalid(
|
||||
"The documented settings file (worldserver.conf.dist) was not found on this server."
|
||||
.into(),
|
||||
)
|
||||
})?;
|
||||
let dist = fs::read_to_string(dist_path(root)).map_err(|_| Error::Invalid("The documented settings file (worldserver.conf.dist) was not found on this server.".into()))?;
|
||||
let t = targets(root, Scope::Server)?;
|
||||
let active = ConfFile::parse_bytes(&fs::read(&t.read)?)?;
|
||||
Ok(parse_dist(&dist)
|
||||
.into_iter()
|
||||
.filter(|(k, _, _)| !hidden(k))
|
||||
.map(|(key, default, doc)| {
|
||||
let value = active
|
||||
.get(&key)
|
||||
.map(|v| v.trim().to_string())
|
||||
.unwrap_or_else(|| default.clone());
|
||||
let value = active.get(&key).map(|v| v.trim().to_string()).unwrap_or_else(|| default.clone());
|
||||
let changed = value != default;
|
||||
Item {
|
||||
key,
|
||||
value,
|
||||
default,
|
||||
doc,
|
||||
changed,
|
||||
}
|
||||
Item { key, value, default, doc, changed }
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
@@ -153,10 +118,7 @@ pub fn list(root: &Path) -> Result<Vec<Item>> {
|
||||
/// Change settings that exist in the documented file. A value is one line of at most 500 characters. Returns the keys
|
||||
/// that actually changed; the previous files are snapshotted first (and can be restored from Settings).
|
||||
pub fn save(root: &Path, meta: &Path, changes: &BTreeMap<String, String>) -> Result<Vec<String>> {
|
||||
let known: BTreeMap<String, String> =
|
||||
parse_dist(&fs::read_to_string(dist_path(root)).map_err(|_| {
|
||||
Error::Invalid("worldserver.conf.dist was not found on this server.".into())
|
||||
})?)
|
||||
let known: BTreeMap<String, String> = parse_dist(&fs::read_to_string(dist_path(root)).map_err(|_| Error::Invalid("worldserver.conf.dist was not found on this server.".into()))?)
|
||||
.into_iter()
|
||||
.map(|(k, d, _)| (k, d))
|
||||
.collect();
|
||||
@@ -172,14 +134,10 @@ pub fn save(root: &Path, meta: &Path, changes: &BTreeMap<String, String>) -> Res
|
||||
for (key, value) in changes {
|
||||
let value = value.trim();
|
||||
if hidden(key) || !known.contains_key(key) {
|
||||
return Err(Error::Invalid(format!(
|
||||
"{key} is not a setting that can be changed here."
|
||||
)));
|
||||
return Err(Error::Invalid(format!("{key} is not a setting that can be changed here.")));
|
||||
}
|
||||
if value.is_empty() || value.len() > 500 || value.chars().any(char::is_control) {
|
||||
return Err(Error::Invalid(format!(
|
||||
"The value of {key} must be one line of text."
|
||||
)));
|
||||
return Err(Error::Invalid(format!("The value of {key} must be one line of text.")));
|
||||
}
|
||||
let same = confs.iter().all(|c| match c.get(key) {
|
||||
Some(v) => v.trim() == value,
|
||||
@@ -206,15 +164,7 @@ pub fn save(root: &Path, meta: &Path, changes: &BTreeMap<String, String>) -> Res
|
||||
}
|
||||
}
|
||||
}
|
||||
take_snapshot(
|
||||
meta,
|
||||
Scope::Server,
|
||||
&format!(
|
||||
"before changing {} setting(s) in the full list",
|
||||
changed.len()
|
||||
),
|
||||
&originals,
|
||||
)?;
|
||||
take_snapshot(meta, Scope::Server, &format!("before changing {} setting(s) in the full list", changed.len()), &originals)?;
|
||||
let mut written = 0;
|
||||
for (i, (path, _)) in originals.iter().enumerate() {
|
||||
if let Err(e) = fsx::atomic_write(path, texts[i].as_bytes()) {
|
||||
@@ -252,28 +202,11 @@ mod tests {
|
||||
let (root, _) = server(d.path());
|
||||
let items = list(&root).unwrap();
|
||||
let keys: Vec<&str> = items.iter().map(|i| i.key.as_str()).collect();
|
||||
assert_eq!(
|
||||
keys,
|
||||
[
|
||||
"Rate.Drop.Item.Rare",
|
||||
"Rate.Drop.Item.Epic",
|
||||
"Instance.ResetTimeHour"
|
||||
],
|
||||
"the database connection is hidden"
|
||||
);
|
||||
assert_eq!(
|
||||
items[0].doc,
|
||||
"Description: Drop rates by quality. Default: 1"
|
||||
);
|
||||
assert_eq!(keys, ["Rate.Drop.Item.Rare", "Rate.Drop.Item.Epic", "Instance.ResetTimeHour"], "the database connection is hidden");
|
||||
assert_eq!(items[0].doc, "Description: Drop rates by quality. Default: 1");
|
||||
assert_eq!(items[0].doc, items[1].doc);
|
||||
assert_eq!(
|
||||
items[2].doc,
|
||||
"Description: Hour of the reset. Important: Use 0-23. Default: 4"
|
||||
);
|
||||
assert!(
|
||||
!items[0].changed && items[0].value == "1",
|
||||
"absent from the file: documented default"
|
||||
);
|
||||
assert_eq!(items[2].doc, "Description: Hour of the reset. Important: Use 0-23. Default: 4");
|
||||
assert!(!items[0].changed && items[0].value == "1", "absent from the file: documented default");
|
||||
assert!(items[1].changed && items[1].value == "3");
|
||||
}
|
||||
|
||||
@@ -283,71 +216,24 @@ mod tests {
|
||||
let (root, meta) = server(d.path());
|
||||
let ok = BTreeMap::from([("Instance.ResetTimeHour".to_string(), "6".to_string())]);
|
||||
assert_eq!(save(&root, &meta, &ok).unwrap(), ["Instance.ResetTimeHour"]);
|
||||
for f in [
|
||||
"Settings/worldserver.conf.template",
|
||||
"Core/configs/worldserver.conf",
|
||||
] {
|
||||
for f in ["Settings/worldserver.conf.template", "Core/configs/worldserver.conf"] {
|
||||
let t = fs::read_to_string(root.join(f)).unwrap();
|
||||
assert!(
|
||||
t.contains("Instance.ResetTimeHour = 6") && t.contains("Rate.Drop.Item.Epic = 3"),
|
||||
"{f}: {t}"
|
||||
);
|
||||
assert!(t.contains("Instance.ResetTimeHour = 6") && t.contains("Rate.Drop.Item.Epic = 3"), "{f}: {t}");
|
||||
}
|
||||
assert!(
|
||||
save(&root, &meta, &ok).unwrap().is_empty(),
|
||||
"same value: nothing to do"
|
||||
);
|
||||
let same_as_default =
|
||||
BTreeMap::from([("Rate.Drop.Item.Rare".to_string(), "1".to_string())]);
|
||||
assert!(
|
||||
save(&root, &meta, &same_as_default).unwrap().is_empty(),
|
||||
"the default is already in effect"
|
||||
);
|
||||
for bad in [
|
||||
("LoginDatabaseInfo", "\"evil\""),
|
||||
("Nope.Key", "1"),
|
||||
("Rate.Drop.Item.Rare", ""),
|
||||
("Rate.Drop.Item.Rare", "1\n2"),
|
||||
] {
|
||||
assert!(
|
||||
save(
|
||||
&root,
|
||||
&meta,
|
||||
&BTreeMap::from([(bad.0.to_string(), bad.1.to_string())])
|
||||
)
|
||||
.is_err(),
|
||||
"{bad:?}"
|
||||
);
|
||||
assert!(save(&root, &meta, &ok).unwrap().is_empty(), "same value: nothing to do");
|
||||
let same_as_default = BTreeMap::from([("Rate.Drop.Item.Rare".to_string(), "1".to_string())]);
|
||||
assert!(save(&root, &meta, &same_as_default).unwrap().is_empty(), "the default is already in effect");
|
||||
for bad in [("LoginDatabaseInfo", "\"evil\""), ("Nope.Key", "1"), ("Rate.Drop.Item.Rare", ""), ("Rate.Drop.Item.Rare", "1\n2")] {
|
||||
assert!(save(&root, &meta, &BTreeMap::from([(bad.0.to_string(), bad.1.to_string())])).is_err(), "{bad:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn what_belongs_to_the_manager_is_hidden() {
|
||||
for k in [
|
||||
"LoginDatabaseInfo",
|
||||
"WorldServerPort",
|
||||
"BindIP",
|
||||
"Ra.Enable",
|
||||
"Ra.Password",
|
||||
"Console.Enable",
|
||||
"console.enable",
|
||||
"Logger.root",
|
||||
"Appender.Console",
|
||||
"DataDir",
|
||||
"Updates.EnableDatabases",
|
||||
"LoginDatabase.WorkerThreads",
|
||||
"InstanceServerPort",
|
||||
] {
|
||||
for k in ["LoginDatabaseInfo", "WorldServerPort", "BindIP", "Ra.Enable", "Ra.Password", "Console.Enable", "console.enable", "Logger.root", "Appender.Console", "DataDir", "Updates.EnableDatabases", "LoginDatabase.WorkerThreads", "InstanceServerPort"] {
|
||||
assert!(hidden(k), "{k}");
|
||||
}
|
||||
for k in [
|
||||
"Rate.XP.Kill",
|
||||
"PlayerLimit",
|
||||
"SupportEnabled",
|
||||
"Instance.ResetTimeHour",
|
||||
"Warden.Enabled",
|
||||
"CharacterCreating.Disabled",
|
||||
] {
|
||||
for k in ["Rate.XP.Kill", "PlayerLimit", "SupportEnabled", "Instance.ResetTimeHour", "Warden.Enabled", "CharacterCreating.Disabled"] {
|
||||
assert!(!hidden(k), "{k}");
|
||||
}
|
||||
}
|
||||
|
||||
+84
-332
@@ -99,9 +99,7 @@ fn backups_dir(meta: &Path) -> PathBuf {
|
||||
fn backup_roots(meta: &Path) -> Vec<PathBuf> {
|
||||
let mut roots: Vec<PathBuf> = custom_dir(meta).into_iter().collect();
|
||||
let default = default_dir(meta);
|
||||
if !roots.contains(&default) {
|
||||
roots.push(default);
|
||||
}
|
||||
if !roots.contains(&default) { roots.push(default); }
|
||||
roots
|
||||
}
|
||||
|
||||
@@ -115,11 +113,7 @@ pub struct BackupLocation {
|
||||
pub fn location(meta: &Path) -> BackupLocation {
|
||||
let default = default_dir(meta);
|
||||
let current = backups_dir(meta);
|
||||
BackupLocation {
|
||||
is_default: current == default,
|
||||
path: current.to_string_lossy().into_owned(),
|
||||
default_path: default.to_string_lossy().into_owned(),
|
||||
}
|
||||
BackupLocation { is_default: current == default, path: current.to_string_lossy().into_owned(), default_path: default.to_string_lossy().into_owned() }
|
||||
}
|
||||
|
||||
/// Choose where new backups are stored (`None` = the default folder next to the server). Existing backups stay where
|
||||
@@ -130,32 +124,19 @@ pub fn set_location(root: &Path, meta: &Path, path: Option<&str>) -> Result<Back
|
||||
match path.map(str::trim).filter(|p| !p.is_empty()) {
|
||||
None => {
|
||||
let file = meta.join(LOCATION_FILE);
|
||||
if file.exists() {
|
||||
fs::remove_file(file)?;
|
||||
}
|
||||
if file.exists() { fs::remove_file(file)?; }
|
||||
}
|
||||
Some(chosen) => {
|
||||
let dir = PathBuf::from(chosen);
|
||||
if !dir.is_absolute() {
|
||||
return Err(Error::Invalid(
|
||||
"Choose a full folder path, for example D:\\CoA backups.".into(),
|
||||
));
|
||||
}
|
||||
if !dir.is_absolute() { return Err(Error::Invalid("Choose a full folder path, for example D:\\CoA backups.".into())); }
|
||||
if fsx::ensure_within(root, &dir).is_ok() {
|
||||
return Err(Error::Invalid("Backups cannot be stored inside the server folder: they would be part of what is being backed up.".into()));
|
||||
}
|
||||
fs::create_dir_all(&dir)
|
||||
.map_err(|e| Error::Invalid(format!("The folder cannot be created: {e}")))?;
|
||||
fs::create_dir_all(&dir).map_err(|e| Error::Invalid(format!("The folder cannot be created: {e}")))?;
|
||||
let probe = dir.join(format!(".coa-write-test-{}", uuid::Uuid::new_v4().simple()));
|
||||
fs::write(&probe, b"x")
|
||||
.map_err(|e| Error::Invalid(format!("The folder is not writable: {e}")))?;
|
||||
fs::write(&probe, b"x").map_err(|e| Error::Invalid(format!("The folder is not writable: {e}")))?;
|
||||
let _ = fs::remove_file(&probe);
|
||||
fsx::atomic_write_json(
|
||||
&meta.join(LOCATION_FILE),
|
||||
&StoredLocation {
|
||||
path: dir.to_string_lossy().into_owned(),
|
||||
},
|
||||
)?;
|
||||
fsx::atomic_write_json(&meta.join(LOCATION_FILE), &StoredLocation { path: dir.to_string_lossy().into_owned() })?;
|
||||
}
|
||||
}
|
||||
Ok(location(meta))
|
||||
@@ -167,23 +148,14 @@ pub(crate) fn point_json(meta: &Path, id: &str) -> Result<PathBuf> {
|
||||
}
|
||||
|
||||
fn id_ok(id: &str) -> bool {
|
||||
!id.is_empty()
|
||||
&& id.len() < 100
|
||||
&& id
|
||||
.chars()
|
||||
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.'))
|
||||
&& !id.contains("..")
|
||||
!id.is_empty() && id.len() < 100 && id.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.')) && !id.contains("..")
|
||||
}
|
||||
|
||||
fn point_dir(meta: &Path, id: &str) -> Result<PathBuf> {
|
||||
if !id_ok(id) {
|
||||
return Err(Error::PathRejected(format!("bad backup id {id:?}")));
|
||||
}
|
||||
if let Some(found) = backup_roots(meta)
|
||||
.into_iter()
|
||||
.map(|root| root.join(id))
|
||||
.find(|dir| dir.is_dir())
|
||||
{
|
||||
if let Some(found) = backup_roots(meta).into_iter().map(|root| root.join(id)).find(|dir| dir.is_dir()) {
|
||||
return Ok(found);
|
||||
}
|
||||
Ok(backups_dir(meta).join(id))
|
||||
@@ -193,16 +165,10 @@ pub fn list(meta: &Path) -> Vec<RecoveryPoint> {
|
||||
let mut out: Vec<RecoveryPoint> = Vec::new();
|
||||
for root in backup_roots(meta) {
|
||||
let found: Vec<RecoveryPoint> = fs::read_dir(&root)
|
||||
.map(|rd| {
|
||||
rd.filter_map(|e| e.ok())
|
||||
.filter_map(|e| fsx::read_json(&e.path().join("backup.json")).ok())
|
||||
.collect()
|
||||
})
|
||||
.map(|rd| rd.filter_map(|e| e.ok()).filter_map(|e| fsx::read_json(&e.path().join("backup.json")).ok()).collect())
|
||||
.unwrap_or_default();
|
||||
for point in found {
|
||||
if !out.iter().any(|p| p.id == point.id) {
|
||||
out.push(point);
|
||||
}
|
||||
if !out.iter().any(|p| p.id == point.id) { out.push(point); }
|
||||
}
|
||||
}
|
||||
out.sort_by(|a, b| b.id.cmp(&a.id));
|
||||
@@ -213,20 +179,9 @@ pub fn get(meta: &Path, id: &str) -> Result<RecoveryPoint> {
|
||||
let dir = point_dir(meta, id)?;
|
||||
// The folder holding the backups may itself be a junction or symlink to another drive (the owner moved it there);
|
||||
// what must hold is that the recovery point stays inside that folder.
|
||||
let holder = dir
|
||||
.parent()
|
||||
.ok_or_else(|| Error::PathRejected(format!("bad backup folder for {id}")))?;
|
||||
let point: RecoveryPoint =
|
||||
fsx::read_json(&fsx::ensure_within(holder, &dir.join("backup.json"))?).map_err(|_| {
|
||||
Error::Invalid(format!(
|
||||
"backup {id} was not found or its metadata is damaged"
|
||||
))
|
||||
})?;
|
||||
if point.id != id || point.schema != 1 {
|
||||
return Err(Error::Invalid(format!(
|
||||
"Backup {id} has inconsistent identity or an unsupported schema."
|
||||
)));
|
||||
}
|
||||
let holder = dir.parent().ok_or_else(|| Error::PathRejected(format!("bad backup folder for {id}")))?;
|
||||
let point: RecoveryPoint = fsx::read_json(&fsx::ensure_within(holder, &dir.join("backup.json"))?).map_err(|_| Error::Invalid(format!("backup {id} was not found or its metadata is damaged")))?;
|
||||
if point.id != id || point.schema != 1 { return Err(Error::Invalid(format!("Backup {id} has inconsistent identity or an unsupported schema."))); }
|
||||
Ok(point)
|
||||
}
|
||||
|
||||
@@ -243,11 +198,7 @@ pub fn config_files(root: &Path) -> Vec<String> {
|
||||
walk(root, &p, out);
|
||||
} else if md.len() <= MAX {
|
||||
let name = p.file_name().unwrap().to_string_lossy().to_lowercase();
|
||||
if name.ends_with(".bak")
|
||||
|| name.ends_with(".orig")
|
||||
|| name.ends_with(".old")
|
||||
|| name.contains(".bak-")
|
||||
{
|
||||
if name.ends_with(".bak") || name.ends_with(".orig") || name.ends_with(".old") || name.contains(".bak-") {
|
||||
continue;
|
||||
}
|
||||
if let Ok(rel) = p.strip_prefix(root) {
|
||||
@@ -270,9 +221,7 @@ pub fn config_files(root: &Path) -> Vec<String> {
|
||||
out.push("RELEASE.json".into());
|
||||
}
|
||||
walk(root, &root.join("Settings/realm-profiles"), &mut out);
|
||||
if root.join("Settings/realm-profile.json").is_file() {
|
||||
out.push("Settings/realm-profile.json".into());
|
||||
}
|
||||
if root.join("Settings/realm-profile.json").is_file() { out.push("Settings/realm-profile.json".into()); }
|
||||
out.sort();
|
||||
out
|
||||
}
|
||||
@@ -290,15 +239,7 @@ fn copy_configs(root: &Path, dir: &Path) -> Result<Component> {
|
||||
bytes += content.len() as u64;
|
||||
file_sha256.insert(rel.clone(), fsx::sha256_bytes(&content));
|
||||
}
|
||||
Ok(Component {
|
||||
name: "configs".into(),
|
||||
path: "files".into(),
|
||||
bytes,
|
||||
sha256: None,
|
||||
tables: None,
|
||||
files: Some(files),
|
||||
file_sha256,
|
||||
})
|
||||
Ok(Component { name: "configs".into(), path: "files".into(), bytes, sha256: None, tables: None, files: Some(files), file_sha256 })
|
||||
}
|
||||
|
||||
/// Make sure the database is up for `f`. If we had to start it (and nothing else is running), stop it again.
|
||||
@@ -310,19 +251,13 @@ pub fn with_database<T>(root: &Path, f: impl FnOnce(&Db) -> Result<T>) -> Result
|
||||
let out = driver::run(root, Verb::StartMysql)?;
|
||||
if !out.ok {
|
||||
// Keep the launcher's own last lines: the title alone ("Something went wrong") hides the cause.
|
||||
return Err(Error::Invalid(format!(
|
||||
"The database could not be started. {}",
|
||||
driver::startup_failure(root, &out)
|
||||
)));
|
||||
return Err(Error::Invalid(format!("The database could not be started. {}", driver::startup_failure(root, &out))));
|
||||
}
|
||||
}
|
||||
let result = f(&db);
|
||||
if !mysql_was_up {
|
||||
let now = process::observe(root, &read_ports(root));
|
||||
if now.world.state == ServiceState::Stopped
|
||||
&& now.auth.state == ServiceState::Stopped
|
||||
&& !crate::multiworld::is_running(root)
|
||||
{
|
||||
if now.world.state == ServiceState::Stopped && now.auth.state == ServiceState::Stopped && !crate::multiworld::is_running(root) {
|
||||
let _ = driver::run(root, Verb::StopAll);
|
||||
}
|
||||
}
|
||||
@@ -330,15 +265,10 @@ pub fn with_database<T>(root: &Path, f: impl FnOnce(&Db) -> Result<T>) -> Result
|
||||
}
|
||||
|
||||
/// Run `f` only if the database is already up; `None` (and nothing started) otherwise.
|
||||
pub fn with_running_database<T>(
|
||||
root: &Path,
|
||||
f: impl FnOnce(&Db) -> Result<T>,
|
||||
) -> Result<Option<T>> {
|
||||
pub fn with_running_database<T>(root: &Path, f: impl FnOnce(&Db) -> Result<T>) -> Result<Option<T>> {
|
||||
let db = Db::from_repack(root, Account::Admin)?;
|
||||
let observed = process::observe(root, &read_ports(root));
|
||||
if observed.mysql.state != ServiceState::Running || !db.ping() {
|
||||
return Ok(None);
|
||||
}
|
||||
if observed.mysql.state != ServiceState::Running || !db.ping() { return Ok(None); }
|
||||
f(&db).map(Some)
|
||||
}
|
||||
|
||||
@@ -351,29 +281,17 @@ fn wanted_databases(kind: Kind) -> &'static [&'static str] {
|
||||
}
|
||||
|
||||
/// Create a recovery point. The server may be running: dumps are consistent snapshots (`--single-transaction`).
|
||||
pub fn create(
|
||||
root: &Path,
|
||||
meta: &Path,
|
||||
kind: Kind,
|
||||
trigger: Trigger,
|
||||
label: Option<String>,
|
||||
progress: &dyn Fn(&str),
|
||||
) -> Result<RecoveryPoint> {
|
||||
let id = format!(
|
||||
"{}-{}-{}",
|
||||
chrono::Utc::now().format("%Y%m%d-%H%M%S"),
|
||||
match trigger {
|
||||
Trigger::Manual => "manual",
|
||||
Trigger::Automatic => "auto",
|
||||
Trigger::BeforeUpdate => "before-update",
|
||||
Trigger::BeforeRestore => "before-restore",
|
||||
Trigger::BeforeMigration => "before-migration",
|
||||
Trigger::BeforeBots => "before-bots",
|
||||
Trigger::BeforeRepair => "before-repair",
|
||||
Trigger::BeforeDangerousChange => "before-change",
|
||||
},
|
||||
uuid::Uuid::new_v4().simple()
|
||||
);
|
||||
pub fn create(root: &Path, meta: &Path, kind: Kind, trigger: Trigger, label: Option<String>, progress: &dyn Fn(&str)) -> Result<RecoveryPoint> {
|
||||
let id = format!("{}-{}-{}", chrono::Utc::now().format("%Y%m%d-%H%M%S"), match trigger {
|
||||
Trigger::Manual => "manual",
|
||||
Trigger::Automatic => "auto",
|
||||
Trigger::BeforeUpdate => "before-update",
|
||||
Trigger::BeforeRestore => "before-restore",
|
||||
Trigger::BeforeMigration => "before-migration",
|
||||
Trigger::BeforeBots => "before-bots",
|
||||
Trigger::BeforeRepair => "before-repair",
|
||||
Trigger::BeforeDangerousChange => "before-change",
|
||||
}, uuid::Uuid::new_v4().simple());
|
||||
let final_dir = point_dir(meta, &id)?;
|
||||
let partial = backups_dir(meta).join(format!("{id}.partial"));
|
||||
fs::create_dir_all(&partial)?;
|
||||
@@ -383,11 +301,10 @@ pub fn create(
|
||||
let realm = crate::realms::state(root)?;
|
||||
let mut databases: Vec<(String, &str)> = wanted_databases(kind).iter()
|
||||
.map(|name| Ok((name.to_string(), realm.active.schema(name)?))).collect::<Result<_>>()?;
|
||||
if kind != Kind::Config {
|
||||
for other in realm.modes().into_iter().filter(|m| *m != realm.active) {
|
||||
databases.push((format!("{}-characters", other.name()), other.schema("characters")?));
|
||||
if kind == Kind::Full { databases.push((format!("{}-world", other.name()), other.schema("world")?)); }
|
||||
}
|
||||
if realm.wildcard_created && kind != Kind::Config {
|
||||
let other = if realm.active == crate::realms::Mode::Coa { crate::realms::Mode::Wildcard } else { crate::realms::Mode::Coa };
|
||||
databases.push((format!("{}-characters", other.name()), other.schema("characters")?));
|
||||
if kind == Kind::Full { databases.push((format!("{}-world", other.name()), other.schema("world")?)); }
|
||||
}
|
||||
if kind != Kind::Config && with_database(root, |db| db.schema_exists("acore_playerbots"))? {
|
||||
databases.push(("playerbots".into(), "acore_playerbots"));
|
||||
@@ -401,15 +318,7 @@ pub fn create(
|
||||
let tables = db.tables(schema)?.len();
|
||||
let file = format!("{name}.sql.zst");
|
||||
let (bytes, sha) = db.dump_to(schema, &partial.join(&file))?;
|
||||
Ok(Component {
|
||||
name,
|
||||
path: file,
|
||||
bytes,
|
||||
sha256: Some(sha),
|
||||
tables: Some(tables),
|
||||
files: None,
|
||||
file_sha256: Default::default(),
|
||||
})
|
||||
Ok(Component { name, path: file, bytes, sha256: Some(sha), tables: Some(tables), files: None, file_sha256: Default::default() })
|
||||
})?;
|
||||
components.push(component);
|
||||
}
|
||||
@@ -460,13 +369,8 @@ pub fn create(
|
||||
|
||||
/// Keep the newest `keep` automatic recovery points; manual and safety ones are never pruned.
|
||||
pub fn prune_automatic(meta: &Path, keep: usize) {
|
||||
if crate::update::ensure_recovered(meta).is_err() {
|
||||
return;
|
||||
}
|
||||
let autos: Vec<_> = list(meta)
|
||||
.into_iter()
|
||||
.filter(|p| p.trigger == Trigger::Automatic)
|
||||
.collect();
|
||||
if crate::update::ensure_recovered(meta).is_err() { return; }
|
||||
let autos: Vec<_> = list(meta).into_iter().filter(|p| p.trigger == Trigger::Automatic).collect();
|
||||
for p in autos.into_iter().skip(keep) {
|
||||
if let Ok(dir) = point_dir(meta, &p.id) {
|
||||
let _ = fs::remove_dir_all(dir);
|
||||
@@ -490,29 +394,16 @@ pub fn verify(meta: &Path, id: &str) -> Result<VerifyReport> {
|
||||
let mut names = std::collections::BTreeSet::new();
|
||||
let mut schemas = std::collections::BTreeSet::new();
|
||||
for c in &point.components {
|
||||
if !names.insert(&c.name) {
|
||||
problems.push(format!("{} appears more than once", c.name));
|
||||
}
|
||||
if !names.insert(&c.name) { problems.push(format!("{} appears more than once", c.name)); }
|
||||
let path = fsx::ensure_within(&dir, &fsx::safe_join(&dir, &c.path)?)?;
|
||||
if c.name != "configs" && (c.sha256.is_none() || c.tables.is_none()) {
|
||||
problems.push(format!("{} lacks database integrity metadata", c.name));
|
||||
}
|
||||
if c.name != "configs" && (c.sha256.is_none() || c.tables.is_none()) { problems.push(format!("{} lacks database integrity metadata", c.name)); }
|
||||
if c.name == "configs" {
|
||||
if c.sha256.is_some() || c.files.is_none() {
|
||||
problems.push("configuration integrity metadata is inconsistent".into());
|
||||
}
|
||||
if c.sha256.is_some() || c.files.is_none() { problems.push("configuration integrity metadata is inconsistent".into()); }
|
||||
} else {
|
||||
let schema = if c.name.contains('-') || c.name == "playerbots" {
|
||||
db::schema_of(&c.name)
|
||||
} else {
|
||||
point.realm.schema(&c.name)
|
||||
};
|
||||
let schema = if c.name.contains('-') || c.name == "playerbots" { db::schema_of(&c.name) } else { point.realm.schema(&c.name) };
|
||||
match schema {
|
||||
Ok(schema) if schemas.insert(schema) => {}
|
||||
_ => problems.push(format!(
|
||||
"{} has an unknown or duplicate database target",
|
||||
c.name
|
||||
)),
|
||||
_ => problems.push(format!("{} has an unknown or duplicate database target", c.name)),
|
||||
}
|
||||
}
|
||||
match (&c.sha256, &c.files) {
|
||||
@@ -523,29 +414,19 @@ pub fn verify(meta: &Path, id: &str) -> Result<VerifyReport> {
|
||||
},
|
||||
(None, Some(files)) => {
|
||||
for f in files {
|
||||
if !c.file_sha256.is_empty() && !c.file_sha256.contains_key(f) {
|
||||
problems.push(format!("configuration file {f} lacks its checksum"));
|
||||
}
|
||||
if !fsx::safe_join(&path, f)
|
||||
.map(|p| p.is_file())
|
||||
.unwrap_or(false)
|
||||
{
|
||||
if !c.file_sha256.is_empty() && !c.file_sha256.contains_key(f) { problems.push(format!("configuration file {f} lacks its checksum")); }
|
||||
if !fsx::safe_join(&path, f).map(|p| p.is_file()).unwrap_or(false) {
|
||||
problems.push(format!("configuration file {f} is missing"));
|
||||
} else if let Some(expected) = c.file_sha256.get(f) {
|
||||
let file = fsx::ensure_within(&dir, &fsx::safe_join(&path, f)?)?;
|
||||
if fsx::sha256_file(&file)?.as_str() != expected {
|
||||
problems.push(format!("configuration file {f} is damaged"));
|
||||
}
|
||||
if fsx::sha256_file(&file)?.as_str() != expected { problems.push(format!("configuration file {f} is damaged")); }
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => problems.push(format!("{} lacks an integrity inventory", c.name)),
|
||||
}
|
||||
}
|
||||
Ok(VerifyReport {
|
||||
ok: problems.is_empty(),
|
||||
problems,
|
||||
})
|
||||
Ok(VerifyReport { ok: problems.is_empty(), problems })
|
||||
}
|
||||
|
||||
/// Delete one recovery point (its own folder only).
|
||||
@@ -553,9 +434,7 @@ pub fn delete(meta: &Path, id: &str) -> Result<()> {
|
||||
let _lock = crate::update::operation_lock(meta)?;
|
||||
crate::update::ensure_recovered(meta)?;
|
||||
if crate::update::unfinished(meta).is_some_and(|t| t.recovery_point.as_deref() == Some(id)) {
|
||||
return Err(Error::Invalid(
|
||||
"This recovery point is required by an unfinished update and cannot be deleted.".into(),
|
||||
));
|
||||
return Err(Error::Invalid("This recovery point is required by an unfinished update and cannot be deleted.".into()));
|
||||
}
|
||||
let dir = point_dir(meta, id)?;
|
||||
get(meta, id)?; // must be a real recovery point
|
||||
@@ -567,28 +446,13 @@ pub fn delete(meta: &Path, id: &str) -> Result<()> {
|
||||
pub fn restore_configs(root: &Path, meta: &Path, id: &str) -> Result<RecoveryPoint> {
|
||||
let point = get(meta, id)?;
|
||||
if point.realm != crate::realms::state(root)?.active {
|
||||
return Err(Error::Invalid(
|
||||
"Select the realm this backup belongs to before restoring it.".into(),
|
||||
));
|
||||
return Err(Error::Invalid("Select the realm this backup belongs to before restoring it.".into()));
|
||||
}
|
||||
let comp = point
|
||||
.components
|
||||
.iter()
|
||||
.find(|c| c.name == "configs")
|
||||
.ok_or_else(|| Error::Invalid("this backup has no configuration".into()))?;
|
||||
let comp = point.components.iter().find(|c| c.name == "configs").ok_or_else(|| Error::Invalid("this backup has no configuration".into()))?;
|
||||
if !verify(meta, id)?.ok {
|
||||
return Err(Error::Invalid(
|
||||
"This backup is damaged and cannot be restored.".into(),
|
||||
));
|
||||
return Err(Error::Invalid("This backup is damaged and cannot be restored.".into()));
|
||||
}
|
||||
let safety = create(
|
||||
root,
|
||||
meta,
|
||||
Kind::Config,
|
||||
Trigger::BeforeRestore,
|
||||
Some(format!("before restoring {id}")),
|
||||
&|_| {},
|
||||
)?;
|
||||
let safety = create(root, meta, Kind::Config, Trigger::BeforeRestore, Some(format!("before restoring {id}")), &|_| {})?;
|
||||
let src_root = point_dir(meta, id)?.join(&comp.path);
|
||||
for rel in comp.files.as_deref().unwrap_or_default() {
|
||||
let dst = fsx::ensure_within(root, &fsx::safe_join(root, rel)?)?;
|
||||
@@ -610,67 +474,37 @@ pub struct DbRestore {
|
||||
pub fn restore_database(root: &Path, meta: &Path, id: &str, name: &str) -> Result<DbRestore> {
|
||||
let point = get(meta, id)?;
|
||||
if point.realm != crate::realms::state(root)?.active {
|
||||
return Err(Error::Invalid(
|
||||
"Select the realm this backup belongs to before restoring it.".into(),
|
||||
));
|
||||
return Err(Error::Invalid("Select the realm this backup belongs to before restoring it.".into()));
|
||||
}
|
||||
let comp = point
|
||||
.components
|
||||
.iter()
|
||||
.find(|c| c.name == name && c.sha256.is_some())
|
||||
.ok_or_else(|| Error::Invalid(format!("this backup has no {name} database")))?;
|
||||
let comp = point.components.iter().find(|c| c.name == name && c.sha256.is_some()).ok_or_else(|| Error::Invalid(format!("this backup has no {name} database")))?;
|
||||
if !verify(meta, id)?.ok {
|
||||
return Err(Error::Invalid(
|
||||
"This backup is damaged and cannot be restored.".into(),
|
||||
));
|
||||
return Err(Error::Invalid("This backup is damaged and cannot be restored.".into()));
|
||||
}
|
||||
let now = process::observe(root, &read_ports(root));
|
||||
if now.world.state != ServiceState::Stopped
|
||||
|| now.auth.state != ServiceState::Stopped
|
||||
|| crate::multiworld::is_running(root)
|
||||
{
|
||||
return Err(Error::Invalid(
|
||||
"Stop the server before restoring a database.".into(),
|
||||
));
|
||||
if now.world.state != ServiceState::Stopped || now.auth.state != ServiceState::Stopped || crate::multiworld::is_running(root) {
|
||||
return Err(Error::Invalid("Stop the server before restoring a database.".into()));
|
||||
}
|
||||
let live = if name.contains('-') || name == "playerbots" {
|
||||
db::schema_of(name)?
|
||||
} else {
|
||||
point.realm.schema(name)?
|
||||
};
|
||||
let live = if name.contains('-') || name == "playerbots" { db::schema_of(name)? } else { point.realm.schema(name)? };
|
||||
let expected_tables = comp.tables.unwrap_or(0);
|
||||
let dump = point_dir(meta, id)?.join(&comp.path);
|
||||
let stamp = uuid::Uuid::new_v4().simple().to_string()[..16].to_string();
|
||||
|
||||
// 1. Safety copy of the current state, so even a wrong restore is reversible.
|
||||
let safety = create(
|
||||
root,
|
||||
meta,
|
||||
Kind::Database,
|
||||
Trigger::BeforeRestore,
|
||||
Some(format!("before restoring {name} from {id}")),
|
||||
&|_| {},
|
||||
)?;
|
||||
let safety = create(root, meta, Kind::Database, Trigger::BeforeRestore, Some(format!("before restoring {name} from {id}")), &|_| {})?;
|
||||
|
||||
with_database(root, |db| {
|
||||
let db = db.clone().for_realm(crate::realms::Mode::Coa);
|
||||
let staging = format!("{live}_restore_{stamp}");
|
||||
let old = format!("{live}_before_restore_{stamp}");
|
||||
if db.schema_exists(&staging)? || db.schema_exists(&old)? {
|
||||
return Err(Error::Invalid(
|
||||
"A previous restore left its work schemas behind; try again in a moment.".into(),
|
||||
));
|
||||
return Err(Error::Invalid("A previous restore left its work schemas behind; try again in a moment.".into()));
|
||||
}
|
||||
// 2. Import into staging.
|
||||
db.query(&format!(
|
||||
"CREATE DATABASE `{staging}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
|
||||
))?;
|
||||
db.query(&format!("CREATE DATABASE `{staging}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"))?;
|
||||
db.import_from(&staging, &dump)?;
|
||||
// 3. Sanity checks.
|
||||
let staged = db.tables(&staging)?;
|
||||
if comp.tables.is_some_and(|expected| staged.len() != expected)
|
||||
|| (comp.tables.is_none() && staged.is_empty())
|
||||
{
|
||||
if comp.tables.is_some_and(|expected| staged.len() != expected) || (comp.tables.is_none() && staged.is_empty()) {
|
||||
return Err(Error::Invalid(format!("The restored copy looks incomplete ({} of {expected_tables} tables); nothing was changed.", staged.len())));
|
||||
}
|
||||
if db.extra_objects(&staging)? > 0 || db.extra_objects(live)? > 0 {
|
||||
@@ -678,9 +512,7 @@ pub fn restore_database(root: &Path, meta: &Path, id: &str, name: &str) -> Resul
|
||||
}
|
||||
// 4. Atomic swap: live tables move to the `before_restore` schema, staged tables move into place.
|
||||
let current = db.tables(live)?;
|
||||
db.query(&format!(
|
||||
"CREATE DATABASE `{old}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
|
||||
))?;
|
||||
db.query(&format!("CREATE DATABASE `{old}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"))?;
|
||||
let mut renames = Vec::new();
|
||||
for t in ¤t {
|
||||
renames.push(format!("`{live}`.`{t}` TO `{old}`.`{t}`"));
|
||||
@@ -688,14 +520,8 @@ pub fn restore_database(root: &Path, meta: &Path, id: &str, name: &str) -> Resul
|
||||
for t in &staged {
|
||||
renames.push(format!("`{staging}`.`{t}` TO `{live}`.`{t}`"));
|
||||
}
|
||||
if !renames.is_empty() {
|
||||
db.query(&format!("RENAME TABLE {};", renames.join(", ")))?;
|
||||
}
|
||||
Ok(DbRestore {
|
||||
previous_schema: old,
|
||||
safety_backup: safety.id.clone(),
|
||||
tables_restored: staged.len(),
|
||||
})
|
||||
if !renames.is_empty() { db.query(&format!("RENAME TABLE {};", renames.join(", ")))?; }
|
||||
Ok(DbRestore { previous_schema: old, safety_backup: safety.id.clone(), tables_restored: staged.len() })
|
||||
})
|
||||
}
|
||||
|
||||
@@ -708,11 +534,7 @@ mod tests {
|
||||
let root = dir.path().join("srv");
|
||||
let meta = dir.path().join("srv.manager");
|
||||
crate::layout::testkit::fake_repack(&root);
|
||||
fs::write(
|
||||
root.join("Settings/database.json"),
|
||||
r#"{"rootPassword":"secret-root","appPassword":"secret-app"}"#,
|
||||
)
|
||||
.unwrap_or_else(|_| {
|
||||
fs::write(root.join("Settings/database.json"), r#"{"rootPassword":"secret-root","appPassword":"secret-app"}"#).unwrap_or_else(|_| {
|
||||
fs::create_dir_all(root.join("Settings")).unwrap();
|
||||
fs::write(root.join("Settings/database.json"), "{}").unwrap();
|
||||
});
|
||||
@@ -735,26 +557,13 @@ mod tests {
|
||||
#[test]
|
||||
fn damaged_configuration_is_detected_before_restore_and_copies_are_retained() {
|
||||
let (_d, root, meta) = setup();
|
||||
let point = create(
|
||||
&root,
|
||||
&meta,
|
||||
Kind::Config,
|
||||
Trigger::Automatic,
|
||||
None,
|
||||
&|_| {},
|
||||
)
|
||||
.unwrap();
|
||||
let file = point_dir(&meta, &point.id)
|
||||
.unwrap()
|
||||
.join("files/Core/configs/worldserver.conf");
|
||||
let point = create(&root, &meta, Kind::Config, Trigger::Automatic, None, &|_| {}).unwrap();
|
||||
let file = point_dir(&meta, &point.id).unwrap().join("files/Core/configs/worldserver.conf");
|
||||
fs::write(&file, b"corrupted").unwrap();
|
||||
assert!(!verify(&meta, &point.id).unwrap().ok);
|
||||
let original = fs::read(root.join("Core/configs/worldserver.conf")).unwrap();
|
||||
assert!(restore_configs(&root, &meta, &point.id).is_err());
|
||||
assert_eq!(
|
||||
fs::read(root.join("Core/configs/worldserver.conf")).unwrap(),
|
||||
original
|
||||
);
|
||||
assert_eq!(fs::read(root.join("Core/configs/worldserver.conf")).unwrap(), original);
|
||||
assert_eq!(list(&meta).len(), 1);
|
||||
fs::create_dir_all(meta.join("updates/broken")).unwrap();
|
||||
fs::write(meta.join("updates/broken/txn.json"), b"{damaged").unwrap();
|
||||
@@ -767,53 +576,26 @@ mod tests {
|
||||
fn config_backup_excludes_secrets_and_clutter_and_restores_byte_exact() {
|
||||
let (_d, root, meta) = setup();
|
||||
fs::create_dir_all(root.join("Settings")).unwrap();
|
||||
fs::write(
|
||||
root.join("Settings/database.json"),
|
||||
r#"{"rootPassword":"x"}"#,
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(root.join("Settings/database.json"), r#"{"rootPassword":"x"}"#).unwrap();
|
||||
fs::write(root.join("Settings/worldserver.conf.template"), "A = 1\n").unwrap();
|
||||
fs::write(root.join("Core/configs/modules/old.conf.bak"), "junk").unwrap();
|
||||
let files = config_files(&root);
|
||||
assert!(files.contains(&"Core/configs/worldserver.conf".to_string()));
|
||||
assert!(files.contains(&"Settings/worldserver.conf.template".to_string()));
|
||||
assert!(
|
||||
!files.iter().any(|f| f.contains("database.json")),
|
||||
"secrets excluded"
|
||||
);
|
||||
assert!(!files.iter().any(|f| f.contains("database.json")), "secrets excluded");
|
||||
assert!(!files.iter().any(|f| f.ends_with(".bak")));
|
||||
|
||||
let p = create(
|
||||
&root,
|
||||
&meta,
|
||||
Kind::Config,
|
||||
Trigger::Manual,
|
||||
Some("test".into()),
|
||||
&|_| {},
|
||||
)
|
||||
.unwrap();
|
||||
let p = create(&root, &meta, Kind::Config, Trigger::Manual, Some("test".into()), &|_| {}).unwrap();
|
||||
assert!(verify(&meta, &p.id).unwrap().ok);
|
||||
assert_eq!(list(&meta).len(), 1);
|
||||
assert!(!backups_dir(&meta)
|
||||
.join(format!("{}.partial", p.id))
|
||||
.exists());
|
||||
assert!(!backups_dir(&meta).join(format!("{}.partial", p.id)).exists());
|
||||
|
||||
let original = fs::read(root.join("Core/configs/worldserver.conf")).unwrap();
|
||||
fs::write(
|
||||
root.join("Core/configs/worldserver.conf"),
|
||||
"[worldserver]\nRealmID = 99\n",
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(root.join("Core/configs/worldserver.conf"), "[worldserver]\nRealmID = 99\n").unwrap();
|
||||
fs::write(root.join("Core/configs/unrelated_new.conf"), "keep").unwrap();
|
||||
let safety = restore_configs(&root, &meta, &p.id).unwrap();
|
||||
assert_eq!(
|
||||
fs::read(root.join("Core/configs/worldserver.conf")).unwrap(),
|
||||
original
|
||||
);
|
||||
assert!(
|
||||
root.join("Core/configs/unrelated_new.conf").is_file(),
|
||||
"restore never deletes other files"
|
||||
);
|
||||
assert_eq!(fs::read(root.join("Core/configs/worldserver.conf")).unwrap(), original);
|
||||
assert!(root.join("Core/configs/unrelated_new.conf").is_file(), "restore never deletes other files");
|
||||
assert_eq!(safety.trigger, Trigger::BeforeRestore);
|
||||
assert!(list(&meta).len() >= 2);
|
||||
}
|
||||
@@ -821,15 +603,7 @@ mod tests {
|
||||
#[test]
|
||||
fn backups_can_live_in_a_chosen_folder_and_old_ones_stay_visible() {
|
||||
let (d, root, meta) = setup();
|
||||
let before = create(
|
||||
&root,
|
||||
&meta,
|
||||
Kind::Config,
|
||||
Trigger::Manual,
|
||||
Some("default folder".into()),
|
||||
&|_| {},
|
||||
)
|
||||
.unwrap();
|
||||
let before = create(&root, &meta, Kind::Config, Trigger::Manual, Some("default folder".into()), &|_| {}).unwrap();
|
||||
assert!(location(&meta).is_default);
|
||||
|
||||
// inside the server folder is refused, a relative path is refused
|
||||
@@ -839,30 +613,13 @@ mod tests {
|
||||
let elsewhere = d.path().join("other drive").join("CoA backups");
|
||||
let loc = set_location(&root, &meta, Some(elsewhere.to_str().unwrap())).unwrap();
|
||||
assert!(!loc.is_default && elsewhere.is_dir());
|
||||
let after = create(
|
||||
&root,
|
||||
&meta,
|
||||
Kind::Config,
|
||||
Trigger::Manual,
|
||||
Some("chosen folder".into()),
|
||||
&|_| {},
|
||||
)
|
||||
.unwrap();
|
||||
assert!(
|
||||
elsewhere.join(&after.id).join("backup.json").is_file(),
|
||||
"new backups go to the chosen folder"
|
||||
);
|
||||
let after = create(&root, &meta, Kind::Config, Trigger::Manual, Some("chosen folder".into()), &|_| {}).unwrap();
|
||||
assert!(elsewhere.join(&after.id).join("backup.json").is_file(), "new backups go to the chosen folder");
|
||||
assert!(!default_dir(&meta).join(&after.id).exists());
|
||||
let ids: Vec<_> = list(&meta).into_iter().map(|p| p.id).collect();
|
||||
assert!(
|
||||
ids.contains(&before.id) && ids.contains(&after.id),
|
||||
"the old backup is still listed"
|
||||
);
|
||||
assert!(ids.contains(&before.id) && ids.contains(&after.id), "the old backup is still listed");
|
||||
assert!(verify(&meta, &before.id).unwrap().ok && verify(&meta, &after.id).unwrap().ok);
|
||||
assert_eq!(
|
||||
point_json(&meta, &after.id).unwrap(),
|
||||
elsewhere.join(&after.id).join("backup.json")
|
||||
);
|
||||
assert_eq!(point_json(&meta, &after.id).unwrap(), elsewhere.join(&after.id).join("backup.json"));
|
||||
restore_configs(&root, &meta, &after.id).unwrap();
|
||||
delete(&meta, &after.id).unwrap();
|
||||
assert!(!elsewhere.join(&after.id).exists());
|
||||
@@ -875,9 +632,7 @@ mod tests {
|
||||
fn verify_detects_tampering_and_restore_refuses_damaged_backup() {
|
||||
let (_d, root, meta) = setup();
|
||||
let p = create(&root, &meta, Kind::Config, Trigger::Manual, None, &|_| {}).unwrap();
|
||||
let victim = point_dir(&meta, &p.id)
|
||||
.unwrap()
|
||||
.join("files/Core/configs/worldserver.conf");
|
||||
let victim = point_dir(&meta, &p.id).unwrap().join("files/Core/configs/worldserver.conf");
|
||||
fs::remove_file(&victim).unwrap();
|
||||
let v = verify(&meta, &p.id).unwrap();
|
||||
assert!(!v.ok && v.problems[0].contains("worldserver.conf"));
|
||||
@@ -920,9 +675,6 @@ mod tests {
|
||||
assert_eq!(left.iter().filter(|i| i.ends_with("-auto")).count(), 2);
|
||||
assert!(left.contains(&"20260101-000000-manual".to_string()));
|
||||
assert!(left.contains(&"20260102-000000-before-restore".to_string()));
|
||||
assert!(
|
||||
left.contains(&"20260204-000000-auto".to_string()),
|
||||
"newest automatic kept"
|
||||
);
|
||||
assert!(left.contains(&"20260204-000000-auto".to_string()), "newest automatic kept");
|
||||
}
|
||||
}
|
||||
@@ -24,30 +24,15 @@ pub struct Params<'a> {
|
||||
}
|
||||
|
||||
/// Auth tables that hold player data. Everything else (permissions, build info, realmlist) is configuration.
|
||||
pub(crate) const AUTH_PLAYER_TABLES: [&str; 11] = [
|
||||
"account",
|
||||
"account_access",
|
||||
"account_banned",
|
||||
"account_muted",
|
||||
"account_password_history",
|
||||
"ip_banned",
|
||||
"logs",
|
||||
"logs_ip_actions",
|
||||
"realmcharacters",
|
||||
"uptime",
|
||||
"rbac_account_permissions",
|
||||
];
|
||||
pub(crate) const AUTH_PLAYER_TABLES: [&str; 11] =
|
||||
["account", "account_access", "account_banned", "account_muted", "account_password_history", "ip_banned", "logs", "logs_ip_actions", "realmcharacters", "uptime", "rbac_account_permissions"];
|
||||
|
||||
fn copy_dir(src: &Path, dst: &Path, skip: &dyn Fn(&str) -> bool, base: &Path) -> Result<u64> {
|
||||
let mut bytes = 0;
|
||||
for e in fs::read_dir(src)? {
|
||||
let e = e?;
|
||||
let p = e.path();
|
||||
let rel = p
|
||||
.strip_prefix(base)
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.replace('\\', "/");
|
||||
let rel = p.strip_prefix(base).unwrap().to_string_lossy().replace('\\', "/");
|
||||
if skip(&rel) {
|
||||
continue;
|
||||
}
|
||||
@@ -67,44 +52,27 @@ fn copy_dir(src: &Path, dst: &Path, skip: &dyn Fn(&str) -> bool, base: &Path) ->
|
||||
}
|
||||
|
||||
fn seven_zip() -> Result<PathBuf> {
|
||||
for c in [
|
||||
"C:/Program Files/7-Zip/7z.exe",
|
||||
"C:/Program Files (x86)/7-Zip/7z.exe",
|
||||
] {
|
||||
for c in ["C:/Program Files/7-Zip/7z.exe", "C:/Program Files (x86)/7-Zip/7z.exe"] {
|
||||
if Path::new(c).is_file() {
|
||||
return Ok(PathBuf::from(c));
|
||||
}
|
||||
}
|
||||
Err(Error::Invalid(
|
||||
"7-Zip is needed to read the repack's database archive (install 7-Zip).".into(),
|
||||
))
|
||||
Err(Error::Invalid("7-Zip is needed to read the repack's database archive (install 7-Zip).".into()))
|
||||
}
|
||||
|
||||
/// Which repack files belong to a base package's scaffolding.
|
||||
pub fn scaffolding(rel: &str) -> bool {
|
||||
let l = rel.to_lowercase();
|
||||
if l.starts_with(".state")
|
||||
|| l.contains("__pycache__")
|
||||
|| l.starts_with("core/") && !l.ends_with(".dll")
|
||||
|| l.starts_with("source/")
|
||||
|| l.starts_with("testing/")
|
||||
{
|
||||
if l.starts_with(".state") || l.contains("__pycache__") || l.starts_with("core/") && !l.ends_with(".dll") || l.starts_with("source/") || l.starts_with("testing/") {
|
||||
return false;
|
||||
}
|
||||
if l.starts_with("bugreport/") {
|
||||
return !(l.starts_with("bugreport/logs") || l.starts_with("bugreport/reports"));
|
||||
}
|
||||
if l.starts_with("mysql/") {
|
||||
return l.starts_with("mysql/bin")
|
||||
|| l.starts_with("mysql/lib")
|
||||
|| l.starts_with("mysql/share");
|
||||
return l.starts_with("mysql/bin") || l.starts_with("mysql/lib") || l.starts_with("mysql/share");
|
||||
}
|
||||
l.starts_with("runtime/")
|
||||
|| l.starts_with("scripts/")
|
||||
|| l.starts_with("settings/")
|
||||
|| l.starts_with("licenses/")
|
||||
|| l == "readme.txt"
|
||||
|| (l.starts_with("core/") && l.ends_with(".dll") && !l[5..].contains('/'))
|
||||
l.starts_with("runtime/") || l.starts_with("scripts/") || l.starts_with("settings/") || l.starts_with("licenses/") || l == "readme.txt" || (l.starts_with("core/") && l.ends_with(".dll") && !l[5..].contains('/'))
|
||||
}
|
||||
|
||||
/// A maintainer's repack may have "detailed logging" switched on: the launcher then rewrites the config templates to Trace
|
||||
@@ -134,9 +102,7 @@ pub fn restore_quiet_logging(settings: &Path) -> Result<()> {
|
||||
pub fn quiet_world_logging(text: &str) -> String {
|
||||
let nl = if text.contains("\r\n") { "\r\n" } else { "\n" };
|
||||
let mut out: Vec<String> = Vec::new();
|
||||
let has_coa = text
|
||||
.lines()
|
||||
.any(|l| l.trim_start().starts_with("Logger.coa="));
|
||||
let has_coa = text.lines().any(|l| l.trim_start().starts_with("Logger.coa="));
|
||||
for line in text.lines() {
|
||||
let l = line.trim();
|
||||
if l.starts_with("Logger.network=") {
|
||||
@@ -157,10 +123,7 @@ pub fn quiet_world_logging(text: &str) -> String {
|
||||
|
||||
pub fn build(p: &Params, say: &dyn Fn(&str)) -> Result<()> {
|
||||
if p.out.exists() {
|
||||
return Err(Error::Invalid(format!(
|
||||
"{} already exists; choose a new folder.",
|
||||
p.out.display()
|
||||
)));
|
||||
return Err(Error::Invalid(format!("{} already exists; choose a new folder.", p.out.display())));
|
||||
}
|
||||
fsx::require_space(p.out, 12 * 1024 * 1024 * 1024)?;
|
||||
fs::create_dir_all(p.out)?;
|
||||
@@ -178,16 +141,7 @@ pub fn build(p: &Params, say: &dyn Fn(&str)) -> Result<()> {
|
||||
}
|
||||
for d in top {
|
||||
// Only these folders can contain scaffolding; the per-file rule decides what inside them is kept.
|
||||
if ![
|
||||
"runtime",
|
||||
"scripts",
|
||||
"settings",
|
||||
"bugreport",
|
||||
"licenses",
|
||||
"mysql",
|
||||
]
|
||||
.contains(&d.to_lowercase().as_str())
|
||||
{
|
||||
if !["runtime", "scripts", "settings", "bugreport", "licenses", "mysql"].contains(&d.to_lowercase().as_str()) {
|
||||
continue;
|
||||
}
|
||||
let dst = p.out.join(&d);
|
||||
@@ -206,20 +160,11 @@ pub fn build(p: &Params, say: &dyn Fn(&str)) -> Result<()> {
|
||||
say("Extracting the pristine database archive");
|
||||
let archive = p.repack.join("mysql/data.7z");
|
||||
if !archive.is_file() {
|
||||
return Err(Error::Invalid(
|
||||
"mysql/data.7z was not found in the repack.".into(),
|
||||
));
|
||||
return Err(Error::Invalid("mysql/data.7z was not found in the repack.".into()));
|
||||
}
|
||||
let status7 = Command::new(seven_zip()?)
|
||||
.arg("x")
|
||||
.arg(&archive)
|
||||
.arg(format!("-o{}", p.out.join("mysql").display()))
|
||||
.arg("-y")
|
||||
.output()?;
|
||||
let status7 = Command::new(seven_zip()?).arg("x").arg(&archive).arg(format!("-o{}", p.out.join("mysql").display())).arg("-y").output()?;
|
||||
if !status7.status.success() || !p.out.join("mysql/data/acore_world").is_dir() {
|
||||
return Err(Error::Invalid(
|
||||
"The database archive could not be extracted.".into(),
|
||||
));
|
||||
return Err(Error::Invalid("The database archive could not be extracted.".into()));
|
||||
}
|
||||
let _ = fs::remove_file(p.out.join("mysql/data/auto.cnf")); // a new server UUID is generated on first start
|
||||
|
||||
@@ -227,12 +172,7 @@ pub fn build(p: &Params, say: &dyn Fn(&str)) -> Result<()> {
|
||||
let repack_json = p.out.join("Settings/repack.json");
|
||||
let mut cfg: serde_json::Value = fsx::read_json(&repack_json)?;
|
||||
let real_ports = cfg.clone();
|
||||
for (k, v) in [
|
||||
("mysqlPort", 14307u16),
|
||||
("authPort", 14724),
|
||||
("worldPort", 14085),
|
||||
("raPort", 14443),
|
||||
] {
|
||||
for (k, v) in [("mysqlPort", 14307u16), ("authPort", 14724), ("worldPort", 14085), ("raPort", 14443)] {
|
||||
cfg[k] = v.into();
|
||||
}
|
||||
fsx::atomic_write_json(&repack_json, &cfg)?;
|
||||
@@ -241,16 +181,7 @@ pub fn build(p: &Params, say: &dyn Fn(&str)) -> Result<()> {
|
||||
if let Some(b) = p.bots {
|
||||
sql.extend(collect_bots_sql(b)?);
|
||||
}
|
||||
let migrations: Vec<_> = sql
|
||||
.iter()
|
||||
.map(|f| crate::manifest::Migration {
|
||||
compatible_sha256: vec![],
|
||||
id: f.id.clone(),
|
||||
db: f.db.clone(),
|
||||
sha256: f.sha256.clone(),
|
||||
destructive: false,
|
||||
})
|
||||
.collect();
|
||||
let migrations: Vec<_> = sql.iter().map(|f| crate::manifest::Migration { compatible_sha256: vec![], id: f.id.clone(), db: f.db.clone(), sha256: f.sha256.clone(), destructive: false }).collect();
|
||||
let staging = std::env::temp_dir().join("coa-cleanbase-sql");
|
||||
let _ = fs::remove_dir_all(&staging);
|
||||
for f in &sql {
|
||||
@@ -260,17 +191,10 @@ pub fn build(p: &Params, say: &dyn Fn(&str)) -> Result<()> {
|
||||
}
|
||||
// The maintainer's database records `rev_20260906_03_any_race_class.sql` as applied although its 86 race/class starts
|
||||
// are absent (the client patch offers those pairs, so creating such a character failed). The file is idempotent.
|
||||
let any_race_class = sql
|
||||
.iter()
|
||||
.find(|f| f.id.contains("any_race_class"))
|
||||
.map(|f| f.abs.clone());
|
||||
let any_race_class = sql.iter().find(|f| f.id.contains("any_race_class")).map(|f| f.abs.clone());
|
||||
let base_chars: Vec<PathBuf> = {
|
||||
let mut v: Vec<PathBuf> = fs::read_dir(p.core.join("data/sql/base/db_characters"))
|
||||
.map_err(|_| {
|
||||
Error::Invalid(
|
||||
"data/sql/base/db_characters was not found in the core checkout.".into(),
|
||||
)
|
||||
})?
|
||||
.map_err(|_| Error::Invalid("data/sql/base/db_characters was not found in the core checkout.".into()))?
|
||||
.flatten()
|
||||
.map(|e| e.path())
|
||||
.filter(|x| x.extension().map(|e| e == "sql").unwrap_or(false))
|
||||
@@ -284,18 +208,11 @@ pub fn build(p: &Params, say: &dyn Fn(&str)) -> Result<()> {
|
||||
backup::with_database(out, |db| {
|
||||
// 1. accounts and session data out of auth
|
||||
let existing = db.tables("acore_auth")?;
|
||||
let mut sql = String::from(
|
||||
"SET FOREIGN_KEY_CHECKS=0;
|
||||
",
|
||||
);
|
||||
for t in AUTH_PLAYER_TABLES
|
||||
.iter()
|
||||
.filter(|t| existing.iter().any(|e| e == *t))
|
||||
{
|
||||
sql.push_str(&format!(
|
||||
"TRUNCATE TABLE `acore_auth`.`{t}`;
|
||||
"
|
||||
));
|
||||
let mut sql = String::from("SET FOREIGN_KEY_CHECKS=0;
|
||||
");
|
||||
for t in AUTH_PLAYER_TABLES.iter().filter(|t| existing.iter().any(|e| e == *t)) {
|
||||
sql.push_str(&format!("TRUNCATE TABLE `acore_auth`.`{t}`;
|
||||
"));
|
||||
}
|
||||
sql.push_str("SET FOREIGN_KEY_CHECKS=1;");
|
||||
db.query(&sql)?;
|
||||
@@ -303,11 +220,7 @@ pub fn build(p: &Params, say: &dyn Fn(&str)) -> Result<()> {
|
||||
db.query("DROP DATABASE IF EXISTS `acore_characters`; CREATE DATABASE `acore_characters` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;")?;
|
||||
// The ledger lives in world, so dropping characters does not clear its migration history.
|
||||
// Never carry that history into a freshly rebuilt character database.
|
||||
if db
|
||||
.tables("acore_world")?
|
||||
.iter()
|
||||
.any(|t| t == "coa_manager_migrations")
|
||||
{
|
||||
if db.tables("acore_world")?.iter().any(|t| t == "coa_manager_migrations") {
|
||||
db.query("DELETE FROM acore_world.coa_manager_migrations WHERE `db`='characters';")?;
|
||||
}
|
||||
for f in &base_chars {
|
||||
@@ -315,18 +228,11 @@ pub fn build(p: &Params, say: &dyn Fn(&str)) -> Result<()> {
|
||||
}
|
||||
// 3. every migration up to the core commit (already-recorded ones are skipped)
|
||||
let before = status(db, &migrations)?;
|
||||
let pending: Vec<_> = migrations
|
||||
.iter()
|
||||
.zip(&before)
|
||||
.filter(|(_, i)| i.status != Status::Applied)
|
||||
.map(|(m, _)| m.clone())
|
||||
.collect();
|
||||
let pending: Vec<_> = migrations.iter().zip(&before).filter(|(_, i)| i.status != Status::Applied).map(|(m, _)| m.clone()).collect();
|
||||
say(&format!("Applying {} database updates", pending.len()));
|
||||
let report = apply_pending(db, &pending, &staging, &|| Ok("(scratch database)".into()))?;
|
||||
if let Some((id, why)) = report.failed {
|
||||
return Err(Error::Invalid(format!(
|
||||
"Database update {id} failed: {why}"
|
||||
)));
|
||||
return Err(Error::Invalid(format!("Database update {id} failed: {why}")));
|
||||
}
|
||||
// 3b. A module script older than a table rename re-creates the old table next to the renamed one (the core then
|
||||
// logs an error at every start). Fold its rows into the new table and drop it.
|
||||
@@ -336,13 +242,7 @@ pub fn build(p: &Params, say: &dyn Fn(&str)) -> Result<()> {
|
||||
}
|
||||
// 3c. re-run the race/class starts when their effect is missing
|
||||
if let Some(file) = &any_race_class {
|
||||
if db
|
||||
.query(
|
||||
"SELECT COUNT(*) FROM acore_world.playercreateinfo WHERE race=7 AND class=27;",
|
||||
)?
|
||||
.trim()
|
||||
== "0"
|
||||
{
|
||||
if db.query("SELECT COUNT(*) FROM acore_world.playercreateinfo WHERE race=7 AND class=27;")?.trim() == "0" {
|
||||
db.run_sql_file("acore_world", file)?;
|
||||
}
|
||||
}
|
||||
@@ -351,33 +251,20 @@ pub fn build(p: &Params, say: &dyn Fn(&str)) -> Result<()> {
|
||||
// 4. prove it is clean
|
||||
let accounts = db.query("SELECT COUNT(*) FROM acore_auth.account;")?;
|
||||
// nothing but the template characters may exist
|
||||
let chars = db.query(&format!(
|
||||
"SELECT COUNT(*) FROM acore_characters.characters WHERE account<>{};",
|
||||
crate::companions::TEMPLATE_ACCOUNT
|
||||
))?;
|
||||
let chars = db.query(&format!("SELECT COUNT(*) FROM acore_characters.characters WHERE account<>{};", crate::companions::TEMPLATE_ACCOUNT))?;
|
||||
let templates = crate::companions::template_count(db)?;
|
||||
if accounts != "0" || chars != "0" || templates != 21 {
|
||||
return Err(Error::Invalid(format!("The database is not clean ({accounts} accounts, {chars} player characters, {templates} templates).")));
|
||||
}
|
||||
if let Some(p) = crate::schema_check::check(db, p.tree)?.first() {
|
||||
return Err(Error::Invalid(format!(
|
||||
"The rebuilt base database is incomplete: {}.{}.{}: {}",
|
||||
p.database, p.table, p.column, p.detail
|
||||
)));
|
||||
return Err(Error::Invalid(format!("The rebuilt base database is incomplete: {}.{}.{}: {}", p.database, p.table, p.column, p.detail)));
|
||||
}
|
||||
crate::schema_check::capture(db, out)?;
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
say("Removing run-time leftovers");
|
||||
for leftover in [
|
||||
".state",
|
||||
"mysql/logs",
|
||||
"mysql/my.ini",
|
||||
"mysql/admin-client.ini",
|
||||
"mysql/mysql.pid",
|
||||
"Core/Logs",
|
||||
] {
|
||||
for leftover in [".state", "mysql/logs", "mysql/my.ini", "mysql/admin-client.ini", "mysql/mysql.pid", "Core/Logs"] {
|
||||
let path = p.out.join(leftover);
|
||||
if path.is_dir() {
|
||||
fs::remove_dir_all(&path)?;
|
||||
@@ -392,12 +279,7 @@ pub fn build(p: &Params, say: &dyn Fn(&str)) -> Result<()> {
|
||||
fsx::atomic_write_json(&repack_json, &shipped)?;
|
||||
|
||||
say("Adding the release files");
|
||||
copy_dir(
|
||||
p.tree,
|
||||
p.out,
|
||||
&|rel| rel == crate::schema_check::CONTRACT,
|
||||
p.tree,
|
||||
)?;
|
||||
copy_dir(p.tree, p.out, &|rel| rel == crate::schema_check::CONTRACT, p.tree)?;
|
||||
if let Some(data) = p.data {
|
||||
say("Copying game data");
|
||||
fs::create_dir_all(p.out.join("Data"))?;
|
||||
@@ -412,31 +294,10 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn scaffolding_keeps_the_launcher_and_drops_runtime_state_and_live_data() {
|
||||
for keep in [
|
||||
"Runtime/python/python.exe",
|
||||
"Scripts/manage.py",
|
||||
"Settings/worldserver.conf.template",
|
||||
"mysql/bin/mysqld.exe",
|
||||
"mysql/share/english/errmsg.sys",
|
||||
"mysql/lib/private/icudt.dat",
|
||||
"BugReport/relay.py",
|
||||
"Core/libssl-3-x64.dll",
|
||||
"README.txt",
|
||||
] {
|
||||
for keep in ["Runtime/python/python.exe", "Scripts/manage.py", "Settings/worldserver.conf.template", "mysql/bin/mysqld.exe", "mysql/share/english/errmsg.sys", "mysql/lib/private/icudt.dat", "BugReport/relay.py", "Core/libssl-3-x64.dll", "README.txt"] {
|
||||
assert!(scaffolding(keep), "{keep}");
|
||||
}
|
||||
for drop in [
|
||||
".state/world.json",
|
||||
"mysql/data/acore_world/x.ibd",
|
||||
"mysql/logs/error.log",
|
||||
"Core/worldserver.exe",
|
||||
"Core/Logs/Server.log",
|
||||
"BugReport/reports/a.json",
|
||||
"Source/server-source.zip",
|
||||
"Scripts/__pycache__/x.pyc",
|
||||
"Testing/notes.md",
|
||||
"mysql/data.7z",
|
||||
] {
|
||||
for drop in [".state/world.json", "mysql/data/acore_world/x.ibd", "mysql/logs/error.log", "Core/worldserver.exe", "Core/Logs/Server.log", "BugReport/reports/a.json", "Source/server-source.zip", "Scripts/__pycache__/x.pyc", "Testing/notes.md", "mysql/data.7z"] {
|
||||
assert!(!scaffolding(drop), "{drop}");
|
||||
}
|
||||
}
|
||||
@@ -444,29 +305,14 @@ mod tests {
|
||||
#[test]
|
||||
fn detailed_logging_templates_are_replaced_by_their_originals() {
|
||||
let d = tempfile::tempdir().unwrap();
|
||||
fs::write(
|
||||
d.path().join("worldserver.conf.template"),
|
||||
"Logger.root=6,Console Server",
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(
|
||||
d.path().join("worldserver.conf.template.original"),
|
||||
"Logger.root=2,Console Server",
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(d.path().join("worldserver.conf.template"), "Logger.root=6,Console Server").unwrap();
|
||||
fs::write(d.path().join("worldserver.conf.template.original"), "Logger.root=2,Console Server").unwrap();
|
||||
fs::write(d.path().join("authserver.conf.template"), "unchanged").unwrap();
|
||||
restore_quiet_logging(d.path()).unwrap();
|
||||
let restored = fs::read_to_string(d.path().join("worldserver.conf.template")).unwrap();
|
||||
assert!(
|
||||
restored.starts_with("Logger.root=2,Console Server")
|
||||
&& !restored.contains("Logger.root=6"),
|
||||
"{restored}"
|
||||
);
|
||||
assert!(restored.starts_with("Logger.root=2,Console Server") && !restored.contains("Logger.root=6"), "{restored}");
|
||||
assert!(!d.path().join("worldserver.conf.template.original").exists());
|
||||
assert_eq!(
|
||||
fs::read_to_string(d.path().join("authserver.conf.template")).unwrap(),
|
||||
"unchanged"
|
||||
);
|
||||
assert_eq!(fs::read_to_string(d.path().join("authserver.conf.template")).unwrap(), "unchanged");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -480,14 +326,7 @@ mod tests {
|
||||
#[test]
|
||||
fn refuses_to_overwrite_an_existing_output_folder() {
|
||||
let d = tempfile::tempdir().unwrap();
|
||||
let p = Params {
|
||||
repack: d.path(),
|
||||
core: d.path(),
|
||||
bots: None,
|
||||
tree: d.path(),
|
||||
data: None,
|
||||
out: d.path(),
|
||||
};
|
||||
let p = Params { repack: d.path(), core: d.path(), bots: None, tree: d.path(), data: None, out: d.path() };
|
||||
assert!(build(&p, &|_| {}).is_err());
|
||||
}
|
||||
}
|
||||
+57
-483
@@ -8,11 +8,10 @@ use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Command;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::fsx;
|
||||
pub use crate::glue_config;
|
||||
|
||||
pub const ADDON_NAME: &str = "CoABotUI";
|
||||
const EXECUTABLES: [&str; 3] = ["Ascension.exe", "Wow.exe", "WoW.exe"];
|
||||
@@ -29,41 +28,26 @@ pub fn is_running(client: &Path) -> bool {
|
||||
}
|
||||
|
||||
fn realm_config(old: &[u8], realm: &str) -> Vec<u8> {
|
||||
let eol: &[u8] = if old.windows(2).any(|w| w == b"\r\n") {
|
||||
b"\r\n"
|
||||
} else {
|
||||
b"\n"
|
||||
};
|
||||
let eol: &[u8] = if old.windows(2).any(|w| w == b"\r\n") { b"\r\n" } else { b"\n" };
|
||||
let mut out = Vec::new();
|
||||
let mut replaced = false;
|
||||
for line in old.split_inclusive(|b| *b == b'\n') {
|
||||
let body = line.strip_prefix(b"\xef\xbb\xbf").unwrap_or(line);
|
||||
let mut words = body
|
||||
.split(|b| b.is_ascii_whitespace())
|
||||
.filter(|w| !w.is_empty());
|
||||
let mut words = body.split(|b| b.is_ascii_whitespace()).filter(|w| !w.is_empty());
|
||||
let matches = words.next().is_some_and(|w| w.eq_ignore_ascii_case(b"SET"))
|
||||
&& words
|
||||
.next()
|
||||
.is_some_and(|w| w.eq_ignore_ascii_case(b"realmName"));
|
||||
&& words.next().is_some_and(|w| w.eq_ignore_ascii_case(b"realmName"));
|
||||
if matches {
|
||||
if line.starts_with(b"\xef\xbb\xbf") {
|
||||
out.extend_from_slice(b"\xef\xbb\xbf");
|
||||
}
|
||||
if line.starts_with(b"\xef\xbb\xbf") { out.extend_from_slice(b"\xef\xbb\xbf"); }
|
||||
out.extend_from_slice(format!("SET realmName \"{realm}\"").as_bytes());
|
||||
if line.ends_with(b"\r\n") {
|
||||
out.extend_from_slice(b"\r\n");
|
||||
} else if line.ends_with(b"\n") {
|
||||
out.push(b'\n');
|
||||
}
|
||||
if line.ends_with(b"\r\n") { out.extend_from_slice(b"\r\n"); }
|
||||
else if line.ends_with(b"\n") { out.push(b'\n'); }
|
||||
replaced = true;
|
||||
} else {
|
||||
out.extend_from_slice(line);
|
||||
}
|
||||
}
|
||||
if !replaced {
|
||||
if !out.is_empty() && !out.ends_with(b"\n") {
|
||||
out.extend_from_slice(eol);
|
||||
}
|
||||
if !out.is_empty() && !out.ends_with(b"\n") { out.extend_from_slice(eol); }
|
||||
out.extend_from_slice(format!("SET realmName \"{realm}\"").as_bytes());
|
||||
out.extend_from_slice(eol);
|
||||
}
|
||||
@@ -74,15 +58,8 @@ pub fn sync_realm(client: &Path, meta: &Path, mode: crate::realms::Mode) -> Resu
|
||||
sync_realm_when_closed(client, meta, mode, is_running(client))
|
||||
}
|
||||
|
||||
fn sync_realm_when_closed(
|
||||
client: &Path,
|
||||
meta: &Path,
|
||||
mode: crate::realms::Mode,
|
||||
running: bool,
|
||||
) -> Result<bool> {
|
||||
if running {
|
||||
return Ok(false);
|
||||
}
|
||||
fn sync_realm_when_closed(client: &Path, meta: &Path, mode: crate::realms::Mode, running: bool) -> Result<bool> {
|
||||
if running { return Ok(false); }
|
||||
let file = client.join("WTF/Config.wtf");
|
||||
let old = match fs::read(&file) {
|
||||
Ok(bytes) => bytes,
|
||||
@@ -92,12 +69,9 @@ fn sync_realm_when_closed(
|
||||
let realm = match mode {
|
||||
crate::realms::Mode::Coa => "Conquest of Azeroth",
|
||||
crate::realms::Mode::Wildcard => "Wildcard",
|
||||
crate::realms::Mode::Reborn => "Warcraft Reborn",
|
||||
};
|
||||
let new = realm_config(&old, realm);
|
||||
if new == old {
|
||||
return Ok(true);
|
||||
}
|
||||
if new == old { return Ok(true); }
|
||||
if file.exists() {
|
||||
let saved = backup_dir(meta).join(format!("config-realm-{}.wtf", uuid::Uuid::new_v4()));
|
||||
fsx::atomic_write(&saved, &old)?;
|
||||
@@ -136,12 +110,7 @@ fn find_exe(dir: &Path) -> Option<&'static str> {
|
||||
/// `Data/<locale>/realmlist.wtf` files (a client may have several locales).
|
||||
pub fn realmlist_files(client: &Path) -> Vec<PathBuf> {
|
||||
let mut v: Vec<PathBuf> = fs::read_dir(client.join("Data"))
|
||||
.map(|rd| {
|
||||
rd.flatten()
|
||||
.map(|e| e.path().join("realmlist.wtf"))
|
||||
.filter(|p| p.is_file())
|
||||
.collect()
|
||||
})
|
||||
.map(|rd| rd.flatten().map(|e| e.path().join("realmlist.wtf")).filter(|p| p.is_file()).collect())
|
||||
.unwrap_or_default();
|
||||
v.sort();
|
||||
v
|
||||
@@ -150,25 +119,13 @@ pub fn realmlist_files(client: &Path) -> Vec<PathBuf> {
|
||||
fn parse_host(text: &str) -> Option<String> {
|
||||
text.lines().find_map(|l| {
|
||||
let l = l.trim();
|
||||
let rest = l
|
||||
.get(..3)
|
||||
.filter(|p| p.eq_ignore_ascii_case("set"))
|
||||
.and_then(|_| {
|
||||
l[3..]
|
||||
.trim_start()
|
||||
.get(..9)
|
||||
.filter(|w| w.eq_ignore_ascii_case("realmlist"))
|
||||
.map(|_| l[3..].trim_start()[9..].trim())
|
||||
})?;
|
||||
let rest = l.get(..3).filter(|p| p.eq_ignore_ascii_case("set")).and_then(|_| l[3..].trim_start().get(..9).filter(|w| w.eq_ignore_ascii_case("realmlist")).map(|_| l[3..].trim_start()[9..].trim()))?;
|
||||
Some(rest.trim_matches('"').to_string())
|
||||
})
|
||||
}
|
||||
|
||||
fn toc_version(toc: &Path) -> Option<String> {
|
||||
fs::read_to_string(toc)
|
||||
.ok()?
|
||||
.lines()
|
||||
.find_map(|l| l.strip_prefix("## Version:").map(|v| v.trim().to_string()))
|
||||
fs::read_to_string(toc).ok()?.lines().find_map(|l| l.strip_prefix("## Version:").map(|v| v.trim().to_string()))
|
||||
}
|
||||
|
||||
fn tree_hashes(dir: &Path) -> BTreeMap<String, String> {
|
||||
@@ -216,25 +173,10 @@ pub fn detect(path: &Path, addon_package: Option<&Path>) -> Option<ClientInfo> {
|
||||
let exe = find_exe(path)?;
|
||||
let realmlists = realmlist_files(path)
|
||||
.into_iter()
|
||||
.map(|p| Realmlist {
|
||||
host: fs::read_to_string(&p).ok().and_then(|t| parse_host(&t)),
|
||||
path: p.to_string_lossy().into_owned(),
|
||||
})
|
||||
.map(|p| Realmlist { host: fs::read_to_string(&p).ok().and_then(|t| parse_host(&t)), path: p.to_string_lossy().into_owned() })
|
||||
.collect();
|
||||
let other = fs::read_dir(path.join("Interface/AddOns"))
|
||||
.map(|rd| {
|
||||
rd.flatten()
|
||||
.filter(|e| e.path().is_dir() && e.file_name() != ADDON_NAME)
|
||||
.count()
|
||||
})
|
||||
.unwrap_or(0);
|
||||
Some(ClientInfo {
|
||||
path: path.to_string_lossy().into_owned(),
|
||||
executable: exe.into(),
|
||||
realmlists,
|
||||
addon: addon_state(path, addon_package),
|
||||
other_addons: other,
|
||||
})
|
||||
let other = fs::read_dir(path.join("Interface/AddOns")).map(|rd| rd.flatten().filter(|e| e.path().is_dir() && e.file_name() != ADDON_NAME).count()).unwrap_or(0);
|
||||
Some(ClientInfo { path: path.to_string_lossy().into_owned(), executable: exe.into(), realmlists, addon: addon_state(path, addon_package), other_addons: other })
|
||||
}
|
||||
|
||||
fn backup_dir(meta: &Path) -> PathBuf {
|
||||
@@ -242,11 +184,7 @@ fn backup_dir(meta: &Path) -> PathBuf {
|
||||
}
|
||||
|
||||
pub fn host_ok(h: &str) -> bool {
|
||||
!h.is_empty()
|
||||
&& h.len() <= 253
|
||||
&& h.chars()
|
||||
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | ':'))
|
||||
&& !h.starts_with('-')
|
||||
!h.is_empty() && h.len() <= 253 && h.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | ':')) && !h.starts_with('-')
|
||||
}
|
||||
|
||||
/// Locales a client has game data for (`Data/<locale>/locale-<locale>.MPQ`), e.g. `enUS`.
|
||||
@@ -256,18 +194,8 @@ fn installed_locales(client: &Path) -> Vec<String> {
|
||||
rd.flatten()
|
||||
.filter(|e| e.path().is_dir())
|
||||
.filter_map(|e| e.file_name().into_string().ok())
|
||||
.filter(|n| {
|
||||
n.len() == 4
|
||||
&& n[..2].bytes().all(|b| b.is_ascii_lowercase())
|
||||
&& n[2..].bytes().all(|b| b.is_ascii_uppercase())
|
||||
})
|
||||
.filter(|n| {
|
||||
client
|
||||
.join("Data")
|
||||
.join(n)
|
||||
.join(format!("locale-{n}.MPQ"))
|
||||
.is_file()
|
||||
})
|
||||
.filter(|n| n.len() == 4 && n[..2].bytes().all(|b| b.is_ascii_lowercase()) && n[2..].bytes().all(|b| b.is_ascii_uppercase()))
|
||||
.filter(|n| client.join("Data").join(n).join(format!("locale-{n}.MPQ")).is_file())
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
@@ -285,14 +213,8 @@ pub fn set_realmlist(client: &Path, meta: &Path, host: &str) -> Result<Vec<Strin
|
||||
if realmlist_files(client).is_empty() {
|
||||
for loc in installed_locales(client) {
|
||||
let file = client.join("Data").join(&loc).join("realmlist.wtf");
|
||||
fsx::atomic_write(
|
||||
&file,
|
||||
format!(
|
||||
"set realmlist {host}
|
||||
"
|
||||
)
|
||||
.as_bytes(),
|
||||
)?;
|
||||
fsx::atomic_write(&file, format!("set realmlist {host}
|
||||
").as_bytes())?;
|
||||
changed.push(file.to_string_lossy().into_owned());
|
||||
}
|
||||
if !changed.is_empty() {
|
||||
@@ -302,8 +224,7 @@ pub fn set_realmlist(client: &Path, meta: &Path, host: &str) -> Result<Vec<Strin
|
||||
let stamp = chrono::Utc::now().format("%Y%m%d-%H%M%S").to_string();
|
||||
for file in realmlist_files(client) {
|
||||
let old = fs::read(&file)?;
|
||||
let text = String::from_utf8(old.clone())
|
||||
.map_err(|_| Error::Invalid("realmlist.wtf is not text".into()))?;
|
||||
let text = String::from_utf8(old.clone()).map_err(|_| Error::Invalid("realmlist.wtf is not text".into()))?;
|
||||
if parse_host(&text).as_deref() == Some(host) {
|
||||
continue;
|
||||
}
|
||||
@@ -324,11 +245,7 @@ pub fn set_realmlist(client: &Path, meta: &Path, host: &str) -> Result<Vec<Strin
|
||||
lines.push(format!("set realmlist {host}"));
|
||||
}
|
||||
let new = lines.join(eol) + eol;
|
||||
let locale = file
|
||||
.parent()
|
||||
.and_then(|p| p.file_name())
|
||||
.map(|n| n.to_string_lossy().into_owned())
|
||||
.unwrap_or_default();
|
||||
let locale = file.parent().and_then(|p| p.file_name()).map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
let saved = backup_dir(meta).join(format!("realmlist-{locale}-{stamp}.wtf"));
|
||||
fsx::atomic_write(&saved, &old)?;
|
||||
fsx::atomic_write(&file, new.as_bytes())?;
|
||||
@@ -341,19 +258,10 @@ pub fn set_realmlist(client: &Path, meta: &Path, host: &str) -> Result<Vec<Strin
|
||||
/// is saved to the backup folder first; files that already hold the same lines are left alone. A client without one gets a
|
||||
/// file for each installed locale. Returns the files that changed.
|
||||
pub fn write_realmlist(client: &Path, meta: &Path, content: &str) -> Result<Vec<String>> {
|
||||
let compact = |t: &str| {
|
||||
t.lines()
|
||||
.map(|l| l.trim().to_ascii_lowercase())
|
||||
.filter(|l| !l.is_empty())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
};
|
||||
let compact = |t: &str| t.lines().map(|l| l.trim().to_ascii_lowercase()).filter(|l| !l.is_empty()).collect::<Vec<_>>().join("\n");
|
||||
let mut files = realmlist_files(client);
|
||||
if files.is_empty() {
|
||||
files = installed_locales(client)
|
||||
.into_iter()
|
||||
.map(|loc| client.join("Data").join(loc).join("realmlist.wtf"))
|
||||
.collect();
|
||||
files = installed_locales(client).into_iter().map(|loc| client.join("Data").join(loc).join("realmlist.wtf")).collect();
|
||||
}
|
||||
let stamp = chrono::Utc::now().format("%Y%m%d-%H%M%S").to_string();
|
||||
let mut changed = Vec::new();
|
||||
@@ -363,15 +271,8 @@ pub fn write_realmlist(client: &Path, meta: &Path, content: &str) -> Result<Vec<
|
||||
if compact(&String::from_utf8_lossy(old)) == compact(content) {
|
||||
continue;
|
||||
}
|
||||
let locale = file
|
||||
.parent()
|
||||
.and_then(|p| p.file_name())
|
||||
.map(|n| n.to_string_lossy().into_owned())
|
||||
.unwrap_or_default();
|
||||
fsx::atomic_write(
|
||||
&backup_dir(meta).join(format!("realmlist-{locale}-{stamp}.wtf")),
|
||||
old,
|
||||
)?;
|
||||
let locale = file.parent().and_then(|p| p.file_name()).map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
fsx::atomic_write(&backup_dir(meta).join(format!("realmlist-{locale}-{stamp}.wtf")), old)?;
|
||||
}
|
||||
fsx::atomic_write(&file, content.as_bytes())?;
|
||||
changed.push(file.to_string_lossy().into_owned());
|
||||
@@ -392,11 +293,8 @@ pub fn install_addon(client: &Path, meta: &Path, source: &Path) -> Result<()> {
|
||||
if want.iter().all(|(k, v)| have.get(k) == Some(v)) {
|
||||
return Ok(());
|
||||
}
|
||||
let keep = backup_dir(meta).join(format!(
|
||||
"{ADDON_NAME}-{}",
|
||||
chrono::Utc::now().format("%Y%m%d-%H%M%S")
|
||||
));
|
||||
for rel in have.keys() {
|
||||
let keep = backup_dir(meta).join(format!("{ADDON_NAME}-{}", chrono::Utc::now().format("%Y%m%d-%H%M%S")));
|
||||
for (rel, _) in &have {
|
||||
let to = fsx::safe_join(&keep, rel)?;
|
||||
fs::create_dir_all(to.parent().unwrap())?;
|
||||
fs::copy(fsx::safe_join(&dest, rel)?, to)?;
|
||||
@@ -411,197 +309,24 @@ pub fn install_addon(client: &Path, meta: &Path, source: &Path) -> Result<()> {
|
||||
|
||||
/// Start the game client. The caller is responsible for making sure the server is ready first.
|
||||
pub fn launch(client: &Path) -> Result<u32> {
|
||||
let exe =
|
||||
find_exe(client).ok_or_else(|| Error::Invalid("The game client was not found.".into()))?;
|
||||
let exe = find_exe(client).ok_or_else(|| Error::Invalid("The game client was not found.".into()))?;
|
||||
if crate::platform::flavor() == crate::platform::Flavor::Docker {
|
||||
return crate::wine::launch(client, exe);
|
||||
}
|
||||
let executable = client.join(exe);
|
||||
let spawn = || Command::new(&executable).current_dir(client).spawn().map(|child| child.id());
|
||||
#[cfg(windows)]
|
||||
{ launch_with_elevation_fallback(spawn, || launch_elevated(&executable, client)).map_err(Into::into) }
|
||||
#[cfg(not(windows))]
|
||||
{ spawn().map_err(Into::into) }
|
||||
}
|
||||
|
||||
/// Start normally first; ask Windows for elevation only when the client requires it (ERROR_ELEVATION_REQUIRED, 740).
|
||||
/// Any other error, including the player declining the prompt (1223), is returned unchanged.
|
||||
#[cfg(windows)]
|
||||
fn launch_with_elevation_fallback(
|
||||
spawn: impl FnOnce() -> std::io::Result<u32>,
|
||||
elevate: impl FnOnce() -> std::io::Result<u32>,
|
||||
) -> std::io::Result<u32> {
|
||||
match spawn() {
|
||||
Err(error) if error.raw_os_error() == Some(740) => elevate(),
|
||||
result => result,
|
||||
}
|
||||
}
|
||||
|
||||
/// Start only the game with the normal Windows permission prompt; the Manager itself stays unelevated.
|
||||
#[cfg(windows)]
|
||||
fn launch_elevated(executable: &Path, directory: &Path) -> std::io::Result<u32> {
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
use windows_sys::Win32::Foundation::CloseHandle;
|
||||
use windows_sys::Win32::System::Threading::GetProcessId;
|
||||
use windows_sys::Win32::UI::Shell::{ShellExecuteExW, SHELLEXECUTEINFOW, SEE_MASK_NOCLOSEPROCESS, SEE_MASK_NOASYNC};
|
||||
use windows_sys::Win32::UI::WindowsAndMessaging::SW_SHOWNORMAL;
|
||||
let file: Vec<u16> = executable.as_os_str().encode_wide().chain(Some(0)).collect();
|
||||
let cwd: Vec<u16> = directory.as_os_str().encode_wide().chain(Some(0)).collect();
|
||||
let verb: Vec<u16> = "runas".encode_utf16().chain(Some(0)).collect();
|
||||
let mut info = SHELLEXECUTEINFOW {
|
||||
cbSize: std::mem::size_of::<SHELLEXECUTEINFOW>() as u32,
|
||||
fMask: SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NOASYNC,
|
||||
lpVerb: verb.as_ptr(),
|
||||
lpFile: file.as_ptr(),
|
||||
lpDirectory: cwd.as_ptr(),
|
||||
nShow: SW_SHOWNORMAL,
|
||||
..Default::default()
|
||||
};
|
||||
if unsafe { ShellExecuteExW(&mut info) } == 0 { return Err(std::io::Error::last_os_error()); }
|
||||
if info.hProcess.is_null() { return Err(std::io::Error::other("Windows did not return the game process handle.")); }
|
||||
let pid = unsafe { GetProcessId(info.hProcess) };
|
||||
let error = std::io::Error::last_os_error();
|
||||
unsafe { CloseHandle(info.hProcess); }
|
||||
if pid == 0 { return Err(error); }
|
||||
Ok(pid)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct RealmlistOverrideEntry {
|
||||
pub file_path: PathBuf,
|
||||
pub original_bytes: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct RealmlistOverrideJournal {
|
||||
pub client_path: PathBuf,
|
||||
pub target_host: String,
|
||||
pub entries: Vec<RealmlistOverrideEntry>,
|
||||
pub created_at: i64,
|
||||
}
|
||||
|
||||
pub fn journal_path(meta: &Path) -> PathBuf {
|
||||
meta.join("realmlist_override_journal.json")
|
||||
}
|
||||
|
||||
/// Begins a crash-safe temporary realmlist redirection for PLAY.
|
||||
/// The exact original bytes (or absence) of every realmlist file are journaled atomically before any file is touched.
|
||||
pub fn begin_realmlist_override(
|
||||
client: &Path,
|
||||
meta: &Path,
|
||||
host: &str,
|
||||
) -> Result<RealmlistOverrideJournal> {
|
||||
if !host_ok(host) {
|
||||
return Err(Error::Invalid("That address is not valid.".into()));
|
||||
}
|
||||
let jpath = journal_path(meta);
|
||||
let mut files = realmlist_files(client);
|
||||
if files.is_empty() {
|
||||
files = installed_locales(client)
|
||||
.into_iter()
|
||||
.map(|loc| client.join("Data").join(loc).join("realmlist.wtf"))
|
||||
.collect();
|
||||
}
|
||||
let mut entries = Vec::new();
|
||||
for file in &files {
|
||||
let original_bytes = fs::read(file).ok();
|
||||
entries.push(RealmlistOverrideEntry {
|
||||
file_path: file.clone(),
|
||||
original_bytes,
|
||||
});
|
||||
}
|
||||
|
||||
let journal = RealmlistOverrideJournal {
|
||||
client_path: client.to_path_buf(),
|
||||
target_host: host.to_string(),
|
||||
entries,
|
||||
created_at: chrono::Utc::now().timestamp(),
|
||||
};
|
||||
|
||||
if !jpath.exists() {
|
||||
let json = serde_json::to_vec_pretty(&journal)
|
||||
.map_err(|e| Error::Invalid(format!("Failed to serialize override journal: {e}")))?;
|
||||
fsx::atomic_write(&jpath, &json)?;
|
||||
}
|
||||
|
||||
for file in &files {
|
||||
let dir = file
|
||||
.parent()
|
||||
.ok_or_else(|| Error::Invalid("file has no parent".into()))?;
|
||||
fs::create_dir_all(dir)?;
|
||||
let content = format!("set realmlist {host}\r\n");
|
||||
fsx::atomic_write(file, content.as_bytes())?;
|
||||
}
|
||||
|
||||
Ok(journal)
|
||||
}
|
||||
|
||||
/// Restores the client's original realmlist bytes or absence from the journal.
|
||||
/// Removes the journal upon clean completion. Returns true if an override was recovered/reverted.
|
||||
pub fn revert_realmlist_override(meta: &Path) -> Result<bool> {
|
||||
let jpath = journal_path(meta);
|
||||
if !jpath.exists() {
|
||||
return Ok(false);
|
||||
}
|
||||
let bytes = match fs::read(&jpath) {
|
||||
Ok(b) => b,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(false),
|
||||
Err(e) => return Err(e.into()),
|
||||
};
|
||||
let journal: RealmlistOverrideJournal = serde_json::from_slice(&bytes)
|
||||
.map_err(|e| Error::Invalid(format!("Corrupt override journal: {e}")))?;
|
||||
|
||||
for entry in &journal.entries {
|
||||
match &entry.original_bytes {
|
||||
Some(orig) => {
|
||||
let _ = fsx::atomic_write(&entry.file_path, orig);
|
||||
}
|
||||
None => {
|
||||
if entry.file_path.exists() {
|
||||
let _ = fs::remove_file(&entry.file_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let _ = fs::remove_file(&jpath);
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// Recovers any unfinished realmlist overrides across known client metadata folders.
|
||||
pub fn recover_all_realmlist_overrides(meta_roots: &[PathBuf]) -> usize {
|
||||
let mut recovered = 0;
|
||||
for meta in meta_roots {
|
||||
if revert_realmlist_override(meta).unwrap_or(false) {
|
||||
recovered += 1;
|
||||
}
|
||||
}
|
||||
recovered
|
||||
let child = Command::new(client.join(exe)).current_dir(client).spawn()?;
|
||||
Ok(child.id())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn elevation_is_requested_only_when_windows_requires_it() {
|
||||
assert_eq!(launch_with_elevation_fallback(|| Ok(17), || panic!("unneeded elevation")).unwrap(), 17);
|
||||
assert_eq!(launch_with_elevation_fallback(|| Err(std::io::Error::from_raw_os_error(740)), || Ok(23)).unwrap(), 23);
|
||||
let error = launch_with_elevation_fallback(|| Err(std::io::Error::from_raw_os_error(2)), || panic!("wrong error elevated")).unwrap_err();
|
||||
assert_eq!(error.raw_os_error(), Some(2));
|
||||
let cancelled = launch_with_elevation_fallback(|| Err(std::io::Error::from_raw_os_error(740)), || Err(std::io::Error::from_raw_os_error(1223))).unwrap_err();
|
||||
assert_eq!(cancelled.raw_os_error(), Some(1223));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn realm_selection_preserves_other_settings_bytes_and_line_endings() {
|
||||
let old = b"\xef\xbb\xbfSET realmName \"Wildcard\"\r\nSET accountName \"\xff\"\r\nset REALMNAME \"old\"\r\nSET realmList \"127.0.0.1\"";
|
||||
let expected = b"\xef\xbb\xbfSET realmName \"Conquest of Azeroth\"\r\nSET accountName \"\xff\"\r\nSET realmName \"Conquest of Azeroth\"\r\nSET realmList \"127.0.0.1\"";
|
||||
assert_eq!(realm_config(old, "Conquest of Azeroth"), expected);
|
||||
assert_eq!(
|
||||
realm_config(b"SET sound 1", "Wildcard"),
|
||||
b"SET sound 1\nSET realmName \"Wildcard\"\n"
|
||||
);
|
||||
assert_eq!(realm_config(b"SET sound 1", "Wildcard"), b"SET sound 1\nSET realmName \"Wildcard\"\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -619,8 +344,6 @@ mod tests {
|
||||
assert_eq!(fs::read_dir(backup_dir(&meta)).unwrap().count(), 1);
|
||||
assert!(sync_realm(&c, &meta, crate::realms::Mode::Wildcard).unwrap());
|
||||
assert_eq!(fs::read(c.join("WTF/Config.wtf")).unwrap(), original);
|
||||
assert!(sync_realm(&c, &meta, crate::realms::Mode::Reborn).unwrap());
|
||||
assert_eq!(fs::read(c.join("WTF/Config.wtf")).unwrap(), b"SET sound 1\r\nSET realmName \"Warcraft Reborn\"\r\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -634,10 +357,7 @@ mod tests {
|
||||
assert_eq!(fs::read(c.join("WTF/Config.wtf")).unwrap(), original);
|
||||
assert!(!backup_dir(&meta).exists());
|
||||
assert!(sync_realm_when_closed(&c, &meta, crate::realms::Mode::Coa, false).unwrap());
|
||||
assert_eq!(
|
||||
fs::read(c.join("WTF/Config.wtf")).unwrap(),
|
||||
b"SET realmName \"Conquest of Azeroth\"\n"
|
||||
);
|
||||
assert_eq!(fs::read(c.join("WTF/Config.wtf")).unwrap(), b"SET realmName \"Conquest of Azeroth\"\n");
|
||||
}
|
||||
|
||||
fn fake_client(d: &Path) -> PathBuf {
|
||||
@@ -647,16 +367,8 @@ mod tests {
|
||||
fs::create_dir_all(c.join("Interface/AddOns/ElvUI")).unwrap();
|
||||
fs::create_dir_all(c.join("WTF")).unwrap();
|
||||
fs::write(c.join("Ascension.exe"), b"exe").unwrap();
|
||||
fs::write(
|
||||
c.join("Data/enUS/realmlist.wtf"),
|
||||
"set realmlist old.example.com\r\nset patchlist x\r\n",
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(
|
||||
c.join("Data/ruRU/realmlist.wtf"),
|
||||
"set realmlist 127.0.0.1\n",
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(c.join("Data/enUS/realmlist.wtf"), "set realmlist old.example.com\r\nset patchlist x\r\n").unwrap();
|
||||
fs::write(c.join("Data/ruRU/realmlist.wtf"), "set realmlist 127.0.0.1\n").unwrap();
|
||||
fs::write(c.join("WTF/Config.wtf"), "SET x 1").unwrap();
|
||||
fs::write(c.join("Interface/AddOns/ElvUI/ElvUI.toc"), "## Title: Elv").unwrap();
|
||||
c
|
||||
@@ -665,11 +377,7 @@ mod tests {
|
||||
fn package(d: &Path, version: &str) -> PathBuf {
|
||||
let p = d.join("pkg").join(ADDON_NAME);
|
||||
fs::create_dir_all(&p).unwrap();
|
||||
fs::write(
|
||||
p.join("CoABotUI.toc"),
|
||||
format!("## Version: {version}\nCoABotUI.lua\n"),
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(p.join("CoABotUI.toc"), format!("## Version: {version}\nCoABotUI.lua\n")).unwrap();
|
||||
fs::write(p.join("CoABotUI.lua"), format!("-- v{version}")).unwrap();
|
||||
p
|
||||
}
|
||||
@@ -688,15 +396,9 @@ mod tests {
|
||||
assert_eq!(info.realmlists[0].host.as_deref(), Some("old.example.com"));
|
||||
assert_eq!(info.other_addons, 1);
|
||||
assert!(!info.addon.installed);
|
||||
assert!(
|
||||
detect(d.path(), None).is_none(),
|
||||
"a plain folder is not a client"
|
||||
);
|
||||
assert!(detect(d.path(), None).is_none(), "a plain folder is not a client");
|
||||
fs::create_dir_all(d.path().join("srv/Data")).unwrap();
|
||||
assert!(
|
||||
detect(&d.path().join("srv"), None).is_none(),
|
||||
"Data without an executable is not a client"
|
||||
);
|
||||
assert!(detect(&d.path().join("srv"), None).is_none(), "Data without an executable is not a client");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -707,51 +409,16 @@ mod tests {
|
||||
let before = snapshot(&c);
|
||||
let changed = set_realmlist(&c, &meta, "192.168.0.5").unwrap();
|
||||
assert_eq!(changed.len(), 2);
|
||||
assert_eq!(
|
||||
fs::read_to_string(c.join("Data/enUS/realmlist.wtf")).unwrap(),
|
||||
"set realmlist 192.168.0.5\r\nset patchlist x\r\n",
|
||||
"other lines and line endings preserved"
|
||||
);
|
||||
assert_eq!(
|
||||
fs::read_to_string(c.join("Data/ruRU/realmlist.wtf")).unwrap(),
|
||||
"set realmlist 192.168.0.5\n"
|
||||
);
|
||||
let saved: Vec<_> = fs::read_dir(meta.join("backups/client"))
|
||||
.unwrap()
|
||||
.flatten()
|
||||
.collect();
|
||||
assert_eq!(fs::read_to_string(c.join("Data/enUS/realmlist.wtf")).unwrap(), "set realmlist 192.168.0.5\r\nset patchlist x\r\n", "other lines and line endings preserved");
|
||||
assert_eq!(fs::read_to_string(c.join("Data/ruRU/realmlist.wtf")).unwrap(), "set realmlist 192.168.0.5\n");
|
||||
let saved: Vec<_> = fs::read_dir(meta.join("backups/client")).unwrap().flatten().collect();
|
||||
assert_eq!(saved.len(), 2, "each changed file was backed up");
|
||||
assert_eq!(
|
||||
fs::read_to_string(
|
||||
saved
|
||||
.iter()
|
||||
.find(|e| e.file_name().to_string_lossy().contains("enUS"))
|
||||
.unwrap()
|
||||
.path()
|
||||
)
|
||||
.unwrap(),
|
||||
"set realmlist old.example.com\r\nset patchlist x\r\n"
|
||||
);
|
||||
assert_eq!(fs::read_to_string(saved.iter().find(|e| e.file_name().to_string_lossy().contains("enUS")).unwrap().path()).unwrap(), "set realmlist old.example.com\r\nset patchlist x\r\n");
|
||||
// nothing else in the client changed
|
||||
let after = snapshot(&c);
|
||||
let diff: Vec<_> = after
|
||||
.iter()
|
||||
.filter(|(k, v)| before.get(*k) != Some(*v))
|
||||
.map(|(k, _)| k.clone())
|
||||
.collect();
|
||||
assert_eq!(
|
||||
diff,
|
||||
["Data/ruRU/realmlist.wtf", "Data/enUS/realmlist.wtf"]
|
||||
.iter()
|
||||
.map(|s| s.to_string())
|
||||
.collect::<std::collections::BTreeSet<_>>()
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>()
|
||||
);
|
||||
assert!(
|
||||
set_realmlist(&c, &meta, "192.168.0.5").unwrap().is_empty(),
|
||||
"already set: no write, no new backup"
|
||||
);
|
||||
let diff: Vec<_> = after.iter().filter(|(k, v)| before.get(*k) != Some(*v)).map(|(k, _)| k.clone()).collect();
|
||||
assert_eq!(diff, ["Data/ruRU/realmlist.wtf", "Data/enUS/realmlist.wtf"].iter().map(|s| s.to_string()).collect::<std::collections::BTreeSet<_>>().into_iter().collect::<Vec<_>>());
|
||||
assert!(set_realmlist(&c, &meta, "192.168.0.5").unwrap().is_empty(), "already set: no write, no new backup");
|
||||
assert!(set_realmlist(&c, &meta, "bad host; rm -rf").is_err());
|
||||
}
|
||||
|
||||
@@ -765,16 +432,10 @@ mod tests {
|
||||
fs::write(c.join("Ascension.exe"), b"exe").unwrap();
|
||||
let changed = set_realmlist(&c, &d.path().join("meta"), "127.0.0.1").unwrap();
|
||||
assert_eq!(changed.len(), 1);
|
||||
assert_eq!(
|
||||
fs::read_to_string(c.join("Data/enUS/realmlist.wtf")).unwrap(),
|
||||
"set realmlist 127.0.0.1
|
||||
"
|
||||
);
|
||||
assert_eq!(fs::read_to_string(c.join("Data/enUS/realmlist.wtf")).unwrap(), "set realmlist 127.0.0.1
|
||||
");
|
||||
assert!(!c.join("Data/Content/realmlist.wtf").exists());
|
||||
assert_eq!(
|
||||
detect(&c, None).unwrap().realmlists[0].host.as_deref(),
|
||||
Some("127.0.0.1")
|
||||
);
|
||||
assert_eq!(detect(&c, None).unwrap().realmlists[0].host.as_deref(), Some("127.0.0.1"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -785,14 +446,7 @@ mod tests {
|
||||
let v1 = package(d.path(), "1");
|
||||
install_addon(&c, &meta, &v1).unwrap();
|
||||
assert_eq!(addon_state(&c, Some(&v1)).up_to_date, Some(true));
|
||||
assert!(
|
||||
!meta.join("backups/client").exists()
|
||||
|| fs::read_dir(meta.join("backups/client"))
|
||||
.unwrap()
|
||||
.next()
|
||||
.is_none(),
|
||||
"first install has nothing to back up"
|
||||
);
|
||||
assert!(!meta.join("backups/client").exists() || fs::read_dir(meta.join("backups/client")).unwrap().next().is_none(), "first install has nothing to back up");
|
||||
|
||||
let before = snapshot(&c);
|
||||
install_addon(&c, &meta, &v1).unwrap();
|
||||
@@ -801,36 +455,14 @@ mod tests {
|
||||
// user tweaks their copy, then a newer package arrives
|
||||
fs::write(c.join("Interface/AddOns/CoABotUI/notes.txt"), "mine").unwrap();
|
||||
let v2 = package(d.path(), "2");
|
||||
let outside_before: BTreeMap<_, _> = snapshot(&c)
|
||||
.into_iter()
|
||||
.filter(|(k, _)| !k.starts_with("Interface/AddOns/CoABotUI"))
|
||||
.collect();
|
||||
let outside_before: BTreeMap<_, _> = snapshot(&c).into_iter().filter(|(k, _)| !k.starts_with("Interface/AddOns/CoABotUI")).collect();
|
||||
install_addon(&c, &meta, &v2).unwrap();
|
||||
assert_eq!(addon_state(&c, Some(&v2)).version.as_deref(), Some("2"));
|
||||
assert_eq!(
|
||||
fs::read_to_string(c.join("Interface/AddOns/CoABotUI/notes.txt")).unwrap(),
|
||||
"mine",
|
||||
"the user's extra file survives"
|
||||
);
|
||||
let outside_after: BTreeMap<_, _> = snapshot(&c)
|
||||
.into_iter()
|
||||
.filter(|(k, _)| !k.starts_with("Interface/AddOns/CoABotUI"))
|
||||
.collect();
|
||||
assert_eq!(
|
||||
outside_before, outside_after,
|
||||
"other addons, WTF, Data untouched"
|
||||
);
|
||||
let kept = fs::read_dir(meta.join("backups/client"))
|
||||
.unwrap()
|
||||
.flatten()
|
||||
.find(|e| e.file_name().to_string_lossy().starts_with(ADDON_NAME))
|
||||
.unwrap()
|
||||
.path();
|
||||
assert_eq!(
|
||||
fs::read_to_string(kept.join("CoABotUI.lua")).unwrap(),
|
||||
"-- v1",
|
||||
"the previous version and the user's copy are backed up"
|
||||
);
|
||||
assert_eq!(fs::read_to_string(c.join("Interface/AddOns/CoABotUI/notes.txt")).unwrap(), "mine", "the user's extra file survives");
|
||||
let outside_after: BTreeMap<_, _> = snapshot(&c).into_iter().filter(|(k, _)| !k.starts_with("Interface/AddOns/CoABotUI")).collect();
|
||||
assert_eq!(outside_before, outside_after, "other addons, WTF, Data untouched");
|
||||
let kept = fs::read_dir(meta.join("backups/client")).unwrap().flatten().find(|e| e.file_name().to_string_lossy().starts_with(ADDON_NAME)).unwrap().path();
|
||||
assert_eq!(fs::read_to_string(kept.join("CoABotUI.lua")).unwrap(), "-- v1", "the previous version and the user's copy are backed up");
|
||||
assert!(kept.join("notes.txt").is_file());
|
||||
}
|
||||
|
||||
@@ -843,62 +475,4 @@ mod tests {
|
||||
assert!(install_addon(&c, &d.path().join("meta"), &empty).is_err());
|
||||
assert!(launch(d.path()).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn realmlist_override_crash_safety_and_byte_for_byte_recovery() {
|
||||
let d = tempfile::tempdir().unwrap();
|
||||
let c = fake_client(d.path());
|
||||
let meta = d.path().join("meta");
|
||||
fs::create_dir_all(&meta).unwrap();
|
||||
|
||||
let orig_bytes = b"set realmlist custom-auth.game.org\nset patchlist patch.game.org\r\n";
|
||||
let target_file = c.join("Data/enUS/realmlist.wtf");
|
||||
fs::write(&target_file, orig_bytes).unwrap();
|
||||
|
||||
let journal = begin_realmlist_override(&c, &meta, "198.51.100.1").unwrap();
|
||||
assert_eq!(journal.target_host, "198.51.100.1");
|
||||
assert_eq!(
|
||||
fs::read_to_string(&target_file).unwrap(),
|
||||
"set realmlist 198.51.100.1\r\n"
|
||||
);
|
||||
assert!(
|
||||
journal_path(&meta).exists(),
|
||||
"journal file must exist while playing"
|
||||
);
|
||||
|
||||
let recovered = revert_realmlist_override(&meta).unwrap();
|
||||
assert!(recovered, "must report successful recovery");
|
||||
assert!(
|
||||
!journal_path(&meta).exists(),
|
||||
"journal must be deleted after recovery"
|
||||
);
|
||||
|
||||
let restored_bytes = fs::read(&target_file).unwrap();
|
||||
assert_eq!(
|
||||
restored_bytes, orig_bytes,
|
||||
"Must restore exact original bytes"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn realmlist_override_restores_absence_if_file_did_not_exist() {
|
||||
let d = tempfile::tempdir().unwrap();
|
||||
let c = d.path().join("wow_fresh");
|
||||
fs::create_dir_all(c.join("Data/enUS")).unwrap();
|
||||
fs::write(c.join("Data/enUS/locale-enUS.MPQ"), b"x").unwrap();
|
||||
let meta = d.path().join("meta");
|
||||
fs::create_dir_all(&meta).unwrap();
|
||||
|
||||
let target_file = c.join("Data/enUS/realmlist.wtf");
|
||||
assert!(!target_file.exists());
|
||||
|
||||
begin_realmlist_override(&c, &meta, "198.51.100.1").unwrap();
|
||||
assert!(target_file.exists());
|
||||
|
||||
revert_realmlist_override(&meta).unwrap();
|
||||
assert!(
|
||||
!target_file.exists(),
|
||||
"Originally absent file must be deleted upon reversion"
|
||||
);
|
||||
}
|
||||
}
|
||||
+63
-354
@@ -19,8 +19,7 @@ use crate::download::{self, Cancel, HttpTransport, Job, Transport};
|
||||
use crate::error::{Error, Result};
|
||||
use crate::fsx;
|
||||
|
||||
pub const MANIFEST_URL: &str =
|
||||
"https://launcher-api.coa-development.org/downloads/client/latest.json";
|
||||
pub const MANIFEST_URL: &str = "https://launcher-api.coa-development.org/downloads/client/latest.json";
|
||||
pub const OBJECTS_URL: &str = "https://launcher-api.coa-development.org/downloads/client/objects";
|
||||
const STATE_DIR: &str = ".coa-manager";
|
||||
const MAX_MANIFEST_BYTES: u64 = 16 * 1024 * 1024;
|
||||
@@ -73,23 +72,14 @@ fn is_excluded(path: &str) -> bool {
|
||||
/// file it is left exactly as it is, whatever the "keep my files" choice; it is only created when missing.
|
||||
fn is_player_owned(path: &str) -> bool {
|
||||
let name = path.rsplit('/').next().unwrap_or(path).to_ascii_lowercase();
|
||||
matches!(
|
||||
name.as_str(),
|
||||
"d3d8.dll" | "d3d9.dll" | "d3d10core.dll" | "d3d11.dll" | "dxgi.dll" | "dxvk.conf"
|
||||
) || matches!(
|
||||
name.rsplit('.').next(),
|
||||
Some("ini" | "conf" | "cfg" | "wtf")
|
||||
)
|
||||
matches!(name.as_str(), "d3d8.dll" | "d3d9.dll" | "d3d10core.dll" | "d3d11.dll" | "dxgi.dll" | "dxvk.conf")
|
||||
|| matches!(name.rsplit('.').next(), Some("ini" | "conf" | "cfg" | "wtf"))
|
||||
}
|
||||
|
||||
pub fn parse_manifest(text: &str) -> Result<Manifest> {
|
||||
let raw: RawManifest = serde_json::from_str(text)
|
||||
.map_err(|e| Error::Invalid(format!("The client list could not be read: {e}")))?;
|
||||
let raw: RawManifest = serde_json::from_str(text).map_err(|e| Error::Invalid(format!("The client list could not be read: {e}")))?;
|
||||
if raw.schema != 1 {
|
||||
return Err(Error::Invalid(format!(
|
||||
"The client list uses a newer format ({}); update the Manager.",
|
||||
raw.schema
|
||||
)));
|
||||
return Err(Error::Invalid(format!("The client list uses a newer format ({}); update the Manager.", raw.schema)));
|
||||
}
|
||||
if raw.version.trim().is_empty() || raw.version.len() > 64 {
|
||||
return Err(Error::Invalid("The client list has no version.".into()));
|
||||
@@ -100,47 +90,26 @@ pub fn parse_manifest(text: &str) -> Result<Manifest> {
|
||||
fsx::safe_join(root, &f.path)?;
|
||||
let hash_ok = f.sha256.len() == 64 && f.sha256.bytes().all(|b| b.is_ascii_hexdigit());
|
||||
if !hash_ok || f.size > MAX_FILE_BYTES {
|
||||
return Err(Error::Invalid(format!(
|
||||
"The client list has a bad entry for {}.",
|
||||
f.path
|
||||
)));
|
||||
return Err(Error::Invalid(format!("The client list has a bad entry for {}.", f.path)));
|
||||
}
|
||||
if is_excluded(&f.path) {
|
||||
continue;
|
||||
}
|
||||
files.push(ManifestFile {
|
||||
path: f.path.replace('\\', "/"),
|
||||
size: f.size,
|
||||
sha256: f.sha256.to_ascii_lowercase(),
|
||||
});
|
||||
files.push(ManifestFile { path: f.path.replace('\\', "/"), size: f.size, sha256: f.sha256.to_ascii_lowercase() });
|
||||
}
|
||||
if files.is_empty() {
|
||||
return Err(Error::Invalid("The client list is empty.".into()));
|
||||
}
|
||||
Ok(Manifest {
|
||||
version: raw.version,
|
||||
published_at: raw.published_at,
|
||||
files,
|
||||
})
|
||||
Ok(Manifest { version: raw.version, published_at: raw.published_at, files })
|
||||
}
|
||||
|
||||
pub fn fetch_manifest(transport: &dyn Transport, url: &str) -> Result<Manifest> {
|
||||
let mut reply = transport
|
||||
.get(url, 0)
|
||||
.map_err(|e| Error::Invalid(format!("Could not reach the client download server: {e}")))?;
|
||||
let mut reply = transport.get(url, 0).map_err(|e| Error::Invalid(format!("Could not reach the client download server: {e}")))?;
|
||||
if reply.status != 200 {
|
||||
return Err(Error::Invalid(format!(
|
||||
"The client download server answered {}.",
|
||||
reply.status
|
||||
)));
|
||||
return Err(Error::Invalid(format!("The client download server answered {}.", reply.status)));
|
||||
}
|
||||
let mut text = String::new();
|
||||
reply
|
||||
.body
|
||||
.by_ref()
|
||||
.take(MAX_MANIFEST_BYTES)
|
||||
.read_to_string(&mut text)
|
||||
.map_err(|e| Error::Invalid(format!("Could not read the client list: {e}")))?;
|
||||
reply.body.by_ref().take(MAX_MANIFEST_BYTES).read_to_string(&mut text).map_err(|e| Error::Invalid(format!("Could not read the client list: {e}")))?;
|
||||
parse_manifest(&text)
|
||||
}
|
||||
|
||||
@@ -188,10 +157,7 @@ pub struct Local {
|
||||
|
||||
pub fn local(client: &Path) -> Local {
|
||||
let s = load_state(client);
|
||||
Local {
|
||||
managed: s.is_some(),
|
||||
version: s.and_then(|s| s.version),
|
||||
}
|
||||
Local { managed: s.is_some(), version: s.and_then(|s| s.version) }
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
|
||||
@@ -228,10 +194,7 @@ pub struct Plan {
|
||||
|
||||
impl Plan {
|
||||
pub fn modified(&self) -> Vec<&Item> {
|
||||
self.items
|
||||
.iter()
|
||||
.filter(|i| i.kind == Kind::Modified)
|
||||
.collect()
|
||||
self.items.iter().filter(|i| i.kind == Kind::Modified).collect()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,11 +213,7 @@ fn cancelled() -> Error {
|
||||
}
|
||||
|
||||
fn mtime_secs(m: &fs::Metadata) -> u64 {
|
||||
m.modified()
|
||||
.ok()
|
||||
.and_then(|t| t.duration_since(UNIX_EPOCH).ok())
|
||||
.map(|d| d.as_secs())
|
||||
.unwrap_or(0)
|
||||
m.modified().ok().and_then(|t| t.duration_since(UNIX_EPOCH).ok()).map(|d| d.as_secs()).unwrap_or(0)
|
||||
}
|
||||
|
||||
fn hash_file(path: &Path, cancel: &Cancel, on_bytes: &mut dyn FnMut(u64)) -> Result<String> {
|
||||
@@ -276,13 +235,7 @@ fn hash_file(path: &Path, cancel: &Cancel, on_bytes: &mut dyn FnMut(u64)) -> Res
|
||||
|
||||
/// Compare the folder with the manifest. Files are hashed only when their size or modification time no longer
|
||||
/// matches what the state file recorded, so a routine check of a managed folder is quick.
|
||||
pub fn plan(
|
||||
client: &Path,
|
||||
m: &Manifest,
|
||||
state: &State,
|
||||
cancel: &Cancel,
|
||||
on_step: &dyn Fn(Step),
|
||||
) -> Result<Plan> {
|
||||
pub fn plan(client: &Path, m: &Manifest, state: &State, cancel: &Cancel, on_step: &dyn Fn(Step)) -> Result<Plan> {
|
||||
let total = m.total_bytes();
|
||||
let mut done = 0u64;
|
||||
let mut items = Vec::new();
|
||||
@@ -292,23 +245,12 @@ pub fn plan(
|
||||
if cancel.is_set() {
|
||||
return Err(cancelled());
|
||||
}
|
||||
on_step(Step {
|
||||
phase: "scan",
|
||||
done,
|
||||
total,
|
||||
bytes_per_sec: 0,
|
||||
file: Some(f.path.clone()),
|
||||
});
|
||||
on_step(Step { phase: "scan", done, total, bytes_per_sec: 0, file: Some(f.path.clone()) });
|
||||
let path = fsx::safe_join(client, &f.path)?;
|
||||
let meta = match fs::metadata(&path) {
|
||||
Ok(meta) if meta.is_file() => meta,
|
||||
_ => {
|
||||
items.push(Item {
|
||||
path: f.path.clone(),
|
||||
size: f.size,
|
||||
kind: Kind::Missing,
|
||||
sha256: f.sha256.clone(),
|
||||
});
|
||||
items.push(Item { path: f.path.clone(), size: f.size, kind: Kind::Missing, sha256: f.sha256.clone() });
|
||||
done += f.size;
|
||||
continue;
|
||||
}
|
||||
@@ -323,13 +265,7 @@ pub fn plan(
|
||||
let mut seen = 0u64;
|
||||
let h = hash_file(&path, cancel, &mut |n| {
|
||||
seen += n;
|
||||
on_step(Step {
|
||||
phase: "scan",
|
||||
done: base + seen.min(f.size),
|
||||
total,
|
||||
bytes_per_sec: 0,
|
||||
file: Some(f.path.clone()),
|
||||
});
|
||||
on_step(Step { phase: "scan", done: base + seen.min(f.size), total, bytes_per_sec: 0, file: Some(f.path.clone()) });
|
||||
})?;
|
||||
Some(h)
|
||||
} else {
|
||||
@@ -342,77 +278,29 @@ pub fn plan(
|
||||
None => hash_file(&path, cancel, &mut |_| {})?,
|
||||
};
|
||||
kept += 1;
|
||||
confirmed.insert(
|
||||
f.path.clone(),
|
||||
Entry {
|
||||
size,
|
||||
mtime,
|
||||
sha256: sha,
|
||||
kept_against: Some(f.sha256.clone()),
|
||||
},
|
||||
);
|
||||
confirmed.insert(f.path.clone(), Entry { size, mtime, sha256: sha, kept_against: Some(f.sha256.clone()) });
|
||||
continue;
|
||||
}
|
||||
if local.as_deref() == Some(f.sha256.as_str()) {
|
||||
up_to_date += 1;
|
||||
confirmed.insert(
|
||||
f.path.clone(),
|
||||
Entry {
|
||||
size,
|
||||
mtime,
|
||||
sha256: f.sha256.clone(),
|
||||
kept_against: None,
|
||||
},
|
||||
);
|
||||
confirmed.insert(f.path.clone(), Entry { size, mtime, sha256: f.sha256.clone(), kept_against: None });
|
||||
continue;
|
||||
}
|
||||
let as_left = entry.filter(|e| local.as_deref() == Some(e.sha256.as_str()));
|
||||
match as_left {
|
||||
Some(e) if e.kept_against.as_deref() == Some(f.sha256.as_str()) => {
|
||||
kept += 1;
|
||||
confirmed.insert(
|
||||
f.path.clone(),
|
||||
Entry {
|
||||
size,
|
||||
mtime,
|
||||
sha256: e.sha256.clone(),
|
||||
kept_against: e.kept_against.clone(),
|
||||
},
|
||||
);
|
||||
confirmed.insert(f.path.clone(), Entry { size, mtime, sha256: e.sha256.clone(), kept_against: e.kept_against.clone() });
|
||||
}
|
||||
Some(e) if e.kept_against.is_none() => {
|
||||
items.push(Item {
|
||||
path: f.path.clone(),
|
||||
size: f.size,
|
||||
kind: Kind::Changed,
|
||||
sha256: f.sha256.clone(),
|
||||
});
|
||||
items.push(Item { path: f.path.clone(), size: f.size, kind: Kind::Changed, sha256: f.sha256.clone() });
|
||||
}
|
||||
_ => items.push(Item {
|
||||
path: f.path.clone(),
|
||||
size: f.size,
|
||||
kind: Kind::Modified,
|
||||
sha256: f.sha256.clone(),
|
||||
}),
|
||||
_ => items.push(Item { path: f.path.clone(), size: f.size, kind: Kind::Modified, sha256: f.sha256.clone() }),
|
||||
}
|
||||
}
|
||||
on_step(Step {
|
||||
phase: "scan",
|
||||
done: total,
|
||||
total,
|
||||
bytes_per_sec: 0,
|
||||
file: None,
|
||||
});
|
||||
on_step(Step { phase: "scan", done: total, total, bytes_per_sec: 0, file: None });
|
||||
let download_bytes = items.iter().map(|i| i.size).sum();
|
||||
Ok(Plan {
|
||||
version: m.version.clone(),
|
||||
items,
|
||||
download_bytes,
|
||||
total_files: m.files.len(),
|
||||
up_to_date_files: up_to_date,
|
||||
kept_files: kept,
|
||||
confirmed,
|
||||
})
|
||||
Ok(Plan { version: m.version.clone(), items, download_bytes, total_files: m.files.len(), up_to_date_files: up_to_date, kept_files: kept, confirmed })
|
||||
}
|
||||
|
||||
pub struct Apply<'a> {
|
||||
@@ -432,10 +320,7 @@ fn blocked(path: &str, e: std::io::Error) -> Error {
|
||||
|
||||
/// Download what the plan lists. Progress is saved after every file, so an interrupted run resumes where it stopped.
|
||||
pub fn apply(a: &Apply, on_step: &dyn Fn(Step)) -> Result<()> {
|
||||
let mut state = State {
|
||||
version: None,
|
||||
files: a.plan.confirmed.clone(),
|
||||
};
|
||||
let mut state = State { version: None, files: a.plan.confirmed.clone() };
|
||||
save_state(a.client, &state)?;
|
||||
|
||||
let mut todo = Vec::new();
|
||||
@@ -444,15 +329,7 @@ pub fn apply(a: &Apply, on_step: &dyn Fn(Step)) -> Result<()> {
|
||||
let path = fsx::safe_join(a.client, &item.path)?;
|
||||
let meta = fs::metadata(&path)?;
|
||||
let sha = hash_file(&path, a.cancel, &mut |_| {})?;
|
||||
state.files.insert(
|
||||
item.path.clone(),
|
||||
Entry {
|
||||
size: meta.len(),
|
||||
mtime: mtime_secs(&meta),
|
||||
sha256: sha,
|
||||
kept_against: Some(item.sha256.clone()),
|
||||
},
|
||||
);
|
||||
state.files.insert(item.path.clone(), Entry { size: meta.len(), mtime: mtime_secs(&meta), sha256: sha, kept_against: Some(item.sha256.clone()) });
|
||||
} else {
|
||||
todo.push(item);
|
||||
}
|
||||
@@ -461,11 +338,7 @@ pub fn apply(a: &Apply, on_step: &dyn Fn(Step)) -> Result<()> {
|
||||
fsx::require_space(a.client, total)?;
|
||||
|
||||
let staging = a.client.join(STATE_DIR).join("staging");
|
||||
let replaced = a
|
||||
.client
|
||||
.join(STATE_DIR)
|
||||
.join("replaced")
|
||||
.join(chrono::Utc::now().format("%Y%m%d-%H%M%S").to_string());
|
||||
let replaced = a.client.join(STATE_DIR).join("replaced").join(chrono::Utc::now().format("%Y%m%d-%H%M%S").to_string());
|
||||
let mut done = 0u64;
|
||||
let mut placed: HashMap<&str, PathBuf> = HashMap::new();
|
||||
for item in todo {
|
||||
@@ -478,21 +351,10 @@ pub fn apply(a: &Apply, on_step: &dyn Fn(Step)) -> Result<()> {
|
||||
let staged = staging.join(&item.sha256);
|
||||
let copy_from = placed.get(item.sha256.as_str()).cloned();
|
||||
if copy_from.is_none() {
|
||||
let job = Job {
|
||||
url: format!("{}/{}", a.objects_url, item.sha256),
|
||||
dest: staged.clone(),
|
||||
sha256: item.sha256.clone(),
|
||||
size: item.size,
|
||||
};
|
||||
let job = Job { url: format!("{}/{}", a.objects_url, item.sha256), dest: staged.clone(), sha256: item.sha256.clone(), size: item.size };
|
||||
let base = done;
|
||||
let result = download::fetch_with(a.transport, &job, a.cancel, &|p| {
|
||||
on_step(Step {
|
||||
phase: "download",
|
||||
done: base + p.downloaded,
|
||||
total,
|
||||
bytes_per_sec: p.bytes_per_sec,
|
||||
file: Some(item.path.clone()),
|
||||
});
|
||||
on_step(Step { phase: "download", done: base + p.downloaded, total, bytes_per_sec: p.bytes_per_sec, file: Some(item.path.clone()) });
|
||||
});
|
||||
if let Err(e) = result {
|
||||
let _ = save_state(a.client, &state);
|
||||
@@ -521,36 +383,16 @@ pub fn apply(a: &Apply, on_step: &dyn Fn(Step)) -> Result<()> {
|
||||
}
|
||||
placed.insert(&item.sha256, target.clone());
|
||||
let meta = fs::metadata(&target)?;
|
||||
state.files.insert(
|
||||
item.path.clone(),
|
||||
Entry {
|
||||
size: meta.len(),
|
||||
mtime: mtime_secs(&meta),
|
||||
sha256: item.sha256.clone(),
|
||||
kept_against: None,
|
||||
},
|
||||
);
|
||||
state.files.insert(item.path.clone(), Entry { size: meta.len(), mtime: mtime_secs(&meta), sha256: item.sha256.clone(), kept_against: None });
|
||||
save_state(a.client, &state)?;
|
||||
done += item.size;
|
||||
let secs = started.elapsed().as_secs_f64().max(0.001);
|
||||
on_step(Step {
|
||||
phase: "download",
|
||||
done,
|
||||
total,
|
||||
bytes_per_sec: (item.size as f64 / secs) as u64,
|
||||
file: Some(item.path.clone()),
|
||||
});
|
||||
on_step(Step { phase: "download", done, total, bytes_per_sec: (item.size as f64 / secs) as u64, file: Some(item.path.clone()) });
|
||||
}
|
||||
state.version = Some(a.manifest.version.clone());
|
||||
save_state(a.client, &state)?;
|
||||
let _ = fs::remove_dir_all(&staging);
|
||||
on_step(Step {
|
||||
phase: "finish",
|
||||
done: total,
|
||||
total,
|
||||
bytes_per_sec: 0,
|
||||
file: None,
|
||||
});
|
||||
on_step(Step { phase: "finish", done: total, total, bytes_per_sec: 0, file: None });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -571,16 +413,9 @@ mod tests {
|
||||
|
||||
impl Server {
|
||||
fn new(files: &[(&str, &[u8])]) -> Self {
|
||||
let s = Server {
|
||||
objects: Default::default(),
|
||||
requested: Default::default(),
|
||||
cancel_after: None,
|
||||
};
|
||||
let s = Server { objects: Default::default(), requested: Default::default(), cancel_after: None };
|
||||
for (_, body) in files {
|
||||
s.objects
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(fsx::sha256_bytes(body), body.to_vec());
|
||||
s.objects.lock().unwrap().insert(fsx::sha256_bytes(body), body.to_vec());
|
||||
}
|
||||
s
|
||||
}
|
||||
@@ -595,24 +430,10 @@ mod tests {
|
||||
cancel.cancel();
|
||||
}
|
||||
}
|
||||
let body = self
|
||||
.objects
|
||||
.lock()
|
||||
.unwrap()
|
||||
.get(&sha)
|
||||
.cloned()
|
||||
.ok_or("404")?;
|
||||
let (status, start) = if from > 0 {
|
||||
(206, from as usize)
|
||||
} else {
|
||||
(200, 0)
|
||||
};
|
||||
let body = self.objects.lock().unwrap().get(&sha).cloned().ok_or("404")?;
|
||||
let (status, start) = if from > 0 { (206, from as usize) } else { (200, 0) };
|
||||
let cr = (from > 0).then(|| format!("bytes {from}-{}/{}", body.len() - 1, body.len()));
|
||||
Ok(Reply {
|
||||
status,
|
||||
content_range: cr,
|
||||
body: Box::new(Cursor::new(body[start..].to_vec())),
|
||||
})
|
||||
Ok(Reply { status, content_range: cr, body: Box::new(Cursor::new(body[start..].to_vec())) })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -620,40 +441,17 @@ mod tests {
|
||||
Manifest {
|
||||
version: version.into(),
|
||||
published_at: None,
|
||||
files: files
|
||||
.iter()
|
||||
.map(|(p, b)| ManifestFile {
|
||||
path: p.to_string(),
|
||||
size: b.len() as u64,
|
||||
sha256: fsx::sha256_bytes(b),
|
||||
})
|
||||
.collect(),
|
||||
files: files.iter().map(|(p, b)| ManifestFile { path: p.to_string(), size: b.len() as u64, sha256: fsx::sha256_bytes(b) }).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
const V1: [(&str, &[u8]); 4] = [
|
||||
("Wow.exe", b"exe-1"),
|
||||
("Data/common.MPQ", b"common-mpq-data"),
|
||||
("Data/patch-A.MPQ", b"patch-a-1"),
|
||||
("Data/enUS/locale.MPQ", b"locale"),
|
||||
];
|
||||
const V1: [(&str, &[u8]); 4] = [("Wow.exe", b"exe-1"), ("Data/common.MPQ", b"common-mpq-data"), ("Data/patch-A.MPQ", b"patch-a-1"), ("Data/enUS/locale.MPQ", b"locale")];
|
||||
|
||||
fn sync(dir: &Path, m: &Manifest, server: &Server, keep: bool) -> Result<Plan> {
|
||||
let state = load_state(dir).unwrap_or_default();
|
||||
let cancel = Cancel::default();
|
||||
let p = plan(dir, m, &state, &cancel, &|_| {})?;
|
||||
apply(
|
||||
&Apply {
|
||||
client: dir,
|
||||
manifest: m,
|
||||
plan: &p,
|
||||
keep_modified: keep,
|
||||
transport: server,
|
||||
objects_url: "https://x.invalid/objects",
|
||||
cancel: &cancel,
|
||||
},
|
||||
&|_| {},
|
||||
)?;
|
||||
apply(&Apply { client: dir, manifest: m, plan: &p, keep_modified: keep, transport: server, objects_url: "https://x.invalid/objects", cancel: &cancel }, &|_| {})?;
|
||||
Ok(p)
|
||||
}
|
||||
|
||||
@@ -666,10 +464,7 @@ mod tests {
|
||||
{"path":"Interface/AddOns/CoABotUI/x.lua","size":3,"sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
|
||||
{"path":"MemoryBridge.log","size":3,"sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}]}"#;
|
||||
let m = parse_manifest(ok).unwrap();
|
||||
assert_eq!(
|
||||
m.files.iter().map(|f| f.path.as_str()).collect::<Vec<_>>(),
|
||||
["Wow.exe"]
|
||||
);
|
||||
assert_eq!(m.files.iter().map(|f| f.path.as_str()).collect::<Vec<_>>(), ["Wow.exe"]);
|
||||
for bad in [
|
||||
r#"{"schema":1,"version":"v","files":[{"path":"../evil.dll","size":1,"sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}]}"#,
|
||||
r#"{"schema":1,"version":"v","files":[{"path":"C:/evil.dll","size":1,"sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}]}"#,
|
||||
@@ -707,26 +502,10 @@ mod tests {
|
||||
let (m2, s2) = (manifest("v2", &v2), Server::new(&v2));
|
||||
let p = sync(d.path(), &m2, &s2, false).unwrap();
|
||||
let kinds: Vec<_> = p.items.iter().map(|i| (i.path.as_str(), i.kind)).collect();
|
||||
assert_eq!(
|
||||
kinds,
|
||||
[
|
||||
("Data/patch-A.MPQ", Kind::Changed),
|
||||
("Data/new.MPQ", Kind::Missing)
|
||||
]
|
||||
);
|
||||
assert_eq!(
|
||||
s2.requested.lock().unwrap().len(),
|
||||
2,
|
||||
"unchanged files were not downloaded again"
|
||||
);
|
||||
assert_eq!(
|
||||
fs::read(d.path().join("Data/patch-A.MPQ")).unwrap(),
|
||||
b"patch-a-2-longer"
|
||||
);
|
||||
assert!(
|
||||
!d.path().join(".coa-manager/replaced").exists(),
|
||||
"a file the Manager itself wrote needs no backup"
|
||||
);
|
||||
assert_eq!(kinds, [("Data/patch-A.MPQ", Kind::Changed), ("Data/new.MPQ", Kind::Missing)]);
|
||||
assert_eq!(s2.requested.lock().unwrap().len(), 2, "unchanged files were not downloaded again");
|
||||
assert_eq!(fs::read(d.path().join("Data/patch-A.MPQ")).unwrap(), b"patch-a-2-longer");
|
||||
assert!(!d.path().join(".coa-manager/replaced").exists(), "a file the Manager itself wrote needs no backup");
|
||||
let again = sync(d.path(), &m2, &s2, false).unwrap();
|
||||
assert!(again.items.is_empty() && again.up_to_date_files == 5);
|
||||
assert_eq!(load_state(d.path()).unwrap().version.as_deref(), Some("v2"));
|
||||
@@ -743,20 +522,10 @@ mod tests {
|
||||
|
||||
let p = sync(d.path(), &m2, &s2, true).unwrap();
|
||||
assert_eq!(p.modified().len(), 1);
|
||||
assert_eq!(
|
||||
fs::read(d.path().join("Wow.exe")).unwrap(),
|
||||
b"my patched exe",
|
||||
"kept"
|
||||
);
|
||||
assert!(
|
||||
s2.requested.lock().unwrap().is_empty(),
|
||||
"nothing needed downloading"
|
||||
);
|
||||
assert_eq!(fs::read(d.path().join("Wow.exe")).unwrap(), b"my patched exe", "kept");
|
||||
assert!(s2.requested.lock().unwrap().is_empty(), "nothing needed downloading");
|
||||
let quiet = sync(d.path(), &m2, &s2, true).unwrap();
|
||||
assert!(
|
||||
quiet.items.is_empty() && quiet.kept_files == 1,
|
||||
"the decision is remembered"
|
||||
);
|
||||
assert!(quiet.items.is_empty() && quiet.kept_files == 1, "the decision is remembered");
|
||||
|
||||
let mut v3 = V1.to_vec();
|
||||
v3[0] = ("Wow.exe", b"exe-3");
|
||||
@@ -764,28 +533,14 @@ mod tests {
|
||||
let again = sync(d.path(), &m3, &s3, false).unwrap();
|
||||
assert_eq!(again.modified().len(), 1, "a newer client asks again");
|
||||
assert_eq!(fs::read(d.path().join("Wow.exe")).unwrap(), b"exe-3");
|
||||
let saved = fs::read_dir(d.path().join(".coa-manager/replaced"))
|
||||
.unwrap()
|
||||
.next()
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.path();
|
||||
assert_eq!(
|
||||
fs::read(saved.join("Wow.exe")).unwrap(),
|
||||
b"my patched exe",
|
||||
"the player's version was moved aside, not lost"
|
||||
);
|
||||
let saved = fs::read_dir(d.path().join(".coa-manager/replaced")).unwrap().next().unwrap().unwrap().path();
|
||||
assert_eq!(fs::read(saved.join("Wow.exe")).unwrap(), b"my patched exe", "the player's version was moved aside, not lost");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn renderer_files_configs_and_everything_not_in_the_manifest_survive_an_update_untouched() {
|
||||
let d = tempfile::tempdir().unwrap();
|
||||
let v1: Vec<(&str, &[u8])> = vec![
|
||||
("Wow.exe", b"exe-1"),
|
||||
("d3d9.dll", b"official d3d9"),
|
||||
("dxvk.conf", b"official conf"),
|
||||
("Data/common.MPQ", b"common-1"),
|
||||
];
|
||||
let v1: Vec<(&str, &[u8])> = vec![("Wow.exe", b"exe-1"), ("d3d9.dll", b"official d3d9"), ("dxvk.conf", b"official conf"), ("Data/common.MPQ", b"common-1")];
|
||||
sync(d.path(), &manifest("v1", &v1), &Server::new(&v1), false).unwrap();
|
||||
// the player's own setup: custom renderer + configs, addons, WTF, realmlist, extra archives, backups
|
||||
let mine: Vec<(&str, &[u8])> = vec![
|
||||
@@ -793,11 +548,8 @@ mod tests {
|
||||
("dxvk.conf", b"my conf"),
|
||||
("ModernWoWRenderer.ini", b"cfg"),
|
||||
("GraphicsEffects.ini.bak", b"bak"),
|
||||
(
|
||||
"Data/enUS/realmlist.wtf",
|
||||
b"set realmlist 127.0.0.1
|
||||
",
|
||||
),
|
||||
("Data/enUS/realmlist.wtf", b"set realmlist 127.0.0.1
|
||||
"),
|
||||
("Data/patch-5.MPQ", b"extra archive"),
|
||||
("Interface/AddOns/ElvUI/ElvUI.toc", b"## Title: Elv"),
|
||||
("WTF/Account/x/SavedVariables.lua", b"vars"),
|
||||
@@ -808,36 +560,18 @@ mod tests {
|
||||
fs::create_dir_all(path.parent().unwrap()).unwrap();
|
||||
fs::write(path, b).unwrap();
|
||||
}
|
||||
let before: BTreeMap<String, String> = mine
|
||||
.iter()
|
||||
.map(|(p, b)| (p.to_string(), fsx::sha256_bytes(b)))
|
||||
.collect();
|
||||
let v2: Vec<(&str, &[u8])> = vec![
|
||||
("Wow.exe", b"exe-2"),
|
||||
("d3d9.dll", b"official d3d9 v2"),
|
||||
("dxvk.conf", b"official conf v2"),
|
||||
("Data/common.MPQ", b"common-2"),
|
||||
];
|
||||
let before: BTreeMap<String, String> = mine.iter().map(|(p, b)| (p.to_string(), fsx::sha256_bytes(b))).collect();
|
||||
let v2: Vec<(&str, &[u8])> = vec![("Wow.exe", b"exe-2"), ("d3d9.dll", b"official d3d9 v2"), ("dxvk.conf", b"official conf v2"), ("Data/common.MPQ", b"common-2")];
|
||||
let (m2, s2) = (manifest("v2", &v2), Server::new(&v2));
|
||||
for keep in [false, true] {
|
||||
sync(d.path(), &m2, &s2, keep).unwrap();
|
||||
for (p, h) in &before {
|
||||
assert_eq!(
|
||||
&fsx::sha256_file(&d.path().join(p)).unwrap(),
|
||||
h,
|
||||
"{p} must not change (keep_modified={keep})"
|
||||
);
|
||||
assert_eq!(&fsx::sha256_file(&d.path().join(p)).unwrap(), h, "{p} must not change (keep_modified={keep})");
|
||||
}
|
||||
assert_eq!(fs::read(d.path().join("Wow.exe")).unwrap(), b"exe-2");
|
||||
assert_eq!(
|
||||
fs::read(d.path().join("Data/common.MPQ")).unwrap(),
|
||||
b"common-2"
|
||||
);
|
||||
assert_eq!(fs::read(d.path().join("Data/common.MPQ")).unwrap(), b"common-2");
|
||||
}
|
||||
assert!(
|
||||
!d.path().join(".coa-manager/replaced").exists(),
|
||||
"nothing of the player's was displaced"
|
||||
);
|
||||
assert!(!d.path().join(".coa-manager/replaced").exists(), "nothing of the player's was displaced");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -855,13 +589,7 @@ mod tests {
|
||||
let m = manifest("v1", &V1);
|
||||
let p = plan(d.path(), &m, &State::default(), &Cancel::default(), &|_| {}).unwrap();
|
||||
let kinds: Vec<_> = p.items.iter().map(|i| (i.path.as_str(), i.kind)).collect();
|
||||
assert_eq!(
|
||||
kinds,
|
||||
[
|
||||
("Data/patch-A.MPQ", Kind::Modified),
|
||||
("Data/enUS/locale.MPQ", Kind::Missing)
|
||||
]
|
||||
);
|
||||
assert_eq!(kinds, [("Data/patch-A.MPQ", Kind::Modified), ("Data/enUS/locale.MPQ", Kind::Missing)]);
|
||||
assert_eq!(p.up_to_date_files, 2);
|
||||
assert_eq!(fs::read(d.path().join("WTF/Config.wtf")).unwrap(), b"mine");
|
||||
}
|
||||
@@ -875,34 +603,15 @@ mod tests {
|
||||
s.cancel_after = Some((AtomicUsize::new(3), cancel.clone()));
|
||||
let state = State::default();
|
||||
let p = plan(d.path(), &m, &state, &cancel, &|_| {}).unwrap();
|
||||
let r = apply(
|
||||
&Apply {
|
||||
client: d.path(),
|
||||
manifest: &m,
|
||||
plan: &p,
|
||||
keep_modified: false,
|
||||
transport: &s,
|
||||
objects_url: "https://x.invalid/o",
|
||||
cancel: &cancel,
|
||||
},
|
||||
&|_| {},
|
||||
);
|
||||
let r = apply(&Apply { client: d.path(), manifest: &m, plan: &p, keep_modified: false, transport: &s, objects_url: "https://x.invalid/o", cancel: &cancel }, &|_| {});
|
||||
assert!(r.is_err());
|
||||
let st = load_state(d.path()).unwrap();
|
||||
assert!(
|
||||
st.version.is_none() && !st.files.is_empty() && st.files.len() < 4,
|
||||
"unfinished but remembered: {}",
|
||||
st.files.len()
|
||||
);
|
||||
assert!(st.version.is_none() && !st.files.is_empty() && st.files.len() < 4, "unfinished but remembered: {}", st.files.len());
|
||||
assert!(local(d.path()).managed && local(d.path()).version.is_none());
|
||||
|
||||
let s2 = Server::new(&V1);
|
||||
sync(d.path(), &m, &s2, false).unwrap();
|
||||
assert_eq!(
|
||||
s2.requested.lock().unwrap().len(),
|
||||
4 - st.files.len(),
|
||||
"only the missing files were requested"
|
||||
);
|
||||
assert_eq!(s2.requested.lock().unwrap().len(), 4 - st.files.len(), "only the missing files were requested");
|
||||
assert_eq!(load_state(d.path()).unwrap().version.as_deref(), Some("v1"));
|
||||
}
|
||||
|
||||
|
||||
@@ -26,10 +26,7 @@ pub fn template_count(db: &Db) -> Result<u32> {
|
||||
}
|
||||
|
||||
fn run_seed(db: &Db, base_guid: &str) -> Result<()> {
|
||||
let file = std::env::temp_dir().join(format!(
|
||||
"coa-companion-templates-{}.sql",
|
||||
std::process::id()
|
||||
));
|
||||
let file = std::env::temp_dir().join(format!("coa-companion-templates-{}.sql", std::process::id()));
|
||||
fs::write(&file, seed_sql(base_guid))?;
|
||||
let r = db.run_sql_file("acore_characters", &file);
|
||||
let _ = fs::remove_file(&file);
|
||||
@@ -47,10 +44,7 @@ pub fn ensure_templates(db: &Db) -> Result<bool> {
|
||||
if template_count(db)? > 0 {
|
||||
return Ok(false);
|
||||
}
|
||||
run_seed(
|
||||
db,
|
||||
"(SELECT IFNULL(MAX(guid),0) FROM acore_characters.characters) + 100000",
|
||||
)?;
|
||||
run_seed(db, "(SELECT IFNULL(MAX(guid),0) FROM acore_characters.characters) + 100000")?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
@@ -63,20 +57,14 @@ pub const OFFLINE_MIN: u32 = 100;
|
||||
|
||||
pub fn offline_factory(root: &Path) -> Option<std::path::PathBuf> {
|
||||
let p = root.join(OFFLINE_FACTORY);
|
||||
(p.is_file()
|
||||
&& root.join("Runtime/python/python.exe").is_file()
|
||||
&& root.join("mysql/bin/mysql.exe").is_file()
|
||||
&& root.join("mysql/admin-client.ini").is_file())
|
||||
.then_some(p)
|
||||
(p.is_file() && root.join("Runtime/python/python.exe").is_file() && root.join("mysql/bin/mysql.exe").is_file() && root.join("mysql/admin-client.ini").is_file()).then_some(p)
|
||||
}
|
||||
|
||||
/// Run the offline factory for `count` leveled bots. The database must be running and the world/auth servers stopped.
|
||||
/// Returns the tail of its report. Credentials are passed as a file path only, never on the command line.
|
||||
pub fn offline_create(root: &Path, log: &Path, count: u32) -> Result<String> {
|
||||
use std::process::{Command, Stdio};
|
||||
let script = offline_factory(root).ok_or_else(|| {
|
||||
crate::error::Error::Invalid("The offline bot factory is not part of this server.".into())
|
||||
})?;
|
||||
let script = offline_factory(root).ok_or_else(|| crate::error::Error::Invalid("The offline bot factory is not part of this server.".into()))?;
|
||||
if let Some(dir) = log.parent() {
|
||||
fs::create_dir_all(dir)?;
|
||||
}
|
||||
@@ -100,28 +88,16 @@ pub fn offline_create(root: &Path, log: &Path, count: u32) -> Result<String> {
|
||||
}
|
||||
if started.elapsed() > std::time::Duration::from_secs(45 * 60) {
|
||||
let _ = child.kill();
|
||||
return Err(crate::error::Error::Invalid(
|
||||
"Creating the companions took too long and was stopped.".into(),
|
||||
));
|
||||
return Err(crate::error::Error::Invalid("Creating the companions took too long and was stopped.".into()));
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(500));
|
||||
};
|
||||
let text = fs::read_to_string(log).unwrap_or_default();
|
||||
let tail: Vec<&str> = text
|
||||
.lines()
|
||||
.rev()
|
||||
.take(6)
|
||||
.collect::<Vec<_>>()
|
||||
.into_iter()
|
||||
.rev()
|
||||
.collect();
|
||||
let tail: Vec<&str> = text.lines().rev().take(6).collect::<Vec<_>>().into_iter().rev().collect();
|
||||
if status.success() {
|
||||
Ok(tail.join("\n"))
|
||||
} else {
|
||||
Err(crate::error::Error::Invalid(format!(
|
||||
"The bot factory reported an error: {}",
|
||||
tail.join(" | ")
|
||||
)))
|
||||
Err(crate::error::Error::Invalid(format!("The bot factory reported an error: {}", tail.join(" | "))))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,11 +111,7 @@ mod tests {
|
||||
assert!(s.starts_with("SET @G = 0; SET @ACC = 900000;"));
|
||||
let chars = s.matches("INSERT IGNORE INTO `characters`").count();
|
||||
assert_eq!(chars, 21);
|
||||
assert_eq!(
|
||||
s.matches("VALUES (@G+").count(),
|
||||
s.matches("INSERT IGNORE").count(),
|
||||
"every row takes its guid from @G"
|
||||
);
|
||||
assert_eq!(s.matches("VALUES (@G+").count(), s.matches("INSERT IGNORE").count(), "every row takes its guid from @G");
|
||||
assert!(s.contains("@ACC"));
|
||||
assert!(!s.contains("INSERT INTO "), "rows that exist are kept");
|
||||
}
|
||||
|
||||
@@ -13,22 +13,9 @@ pub struct MergePlan {
|
||||
}
|
||||
|
||||
pub fn plan(conf: &ConfFile, dist: &ConfFile) -> MergePlan {
|
||||
let added = dist
|
||||
.entries()
|
||||
.map(|(k, _)| k)
|
||||
.filter(|k| !conf.contains(k))
|
||||
.map(str::to_string)
|
||||
.collect::<Vec<_>>();
|
||||
let not_in_dist = conf
|
||||
.entries()
|
||||
.map(|(k, _)| k)
|
||||
.filter(|k| !dist.contains(k))
|
||||
.map(str::to_string)
|
||||
.collect();
|
||||
MergePlan {
|
||||
added: dedup(added),
|
||||
not_in_dist: dedup(not_in_dist),
|
||||
}
|
||||
let added = dist.entries().map(|(k, _)| k).filter(|k| !conf.contains(k)).map(str::to_string).collect::<Vec<_>>();
|
||||
let not_in_dist = conf.entries().map(|(k, _)| k).filter(|k| !dist.contains(k)).map(str::to_string).collect();
|
||||
MergePlan { added: dedup(added), not_in_dist: dedup(not_in_dist) }
|
||||
}
|
||||
|
||||
fn dedup(mut v: Vec<String>) -> Vec<String> {
|
||||
@@ -57,9 +44,7 @@ mod tests {
|
||||
#[test]
|
||||
fn adds_new_keys_with_docs_and_preserves_user_values_and_unknowns() {
|
||||
let mut conf = ConfFile::parse("[worldserver]\nA = 42\n# mine\nCustom.Key = 1\n");
|
||||
let dist = ConfFile::parse(
|
||||
"[worldserver]\n# About A\nA = 1\n# About B\n# second line\nB = \"x\"\n",
|
||||
);
|
||||
let dist = ConfFile::parse("[worldserver]\n# About A\nA = 1\n# About B\n# second line\nB = \"x\"\n");
|
||||
let before = conf.to_text();
|
||||
let p = apply(&mut conf, &dist);
|
||||
assert_eq!(p.added, ["B"]);
|
||||
|
||||
+138
-593
File diff suppressed because it is too large.
Load diff
@@ -36,9 +36,7 @@ fn classify(text: &str) -> Kind {
|
||||
if t.is_empty() || t.starts_with('#') || t.starts_with(';') || t.starts_with('[') {
|
||||
return Kind::Other;
|
||||
}
|
||||
let Some(eq) = text.find('=') else {
|
||||
return Kind::Other;
|
||||
};
|
||||
let Some(eq) = text.find('=') else { return Kind::Other };
|
||||
let key = text[..eq].trim();
|
||||
if key.is_empty() || !key.chars().all(is_key_char) {
|
||||
return Kind::Other;
|
||||
@@ -46,10 +44,7 @@ fn classify(text: &str) -> Kind {
|
||||
// prefix = everything up to and including '=' and the spaces after it
|
||||
let after = &text[eq + 1..];
|
||||
let spaces = after.len() - after.trim_start_matches([' ', '\t']).len();
|
||||
Kind::Entry {
|
||||
key: key.to_string(),
|
||||
prefix_len: eq + 1 + spaces,
|
||||
}
|
||||
Kind::Entry { key: key.to_string(), prefix_len: eq + 1 + spaces }
|
||||
}
|
||||
|
||||
impl ConfFile {
|
||||
@@ -63,9 +58,7 @@ impl ConfFile {
|
||||
let mut rest = body;
|
||||
while !rest.is_empty() {
|
||||
let (raw, eol, next) = match rest.find('\n') {
|
||||
Some(i) if i > 0 && rest.as_bytes()[i - 1] == b'\r' => {
|
||||
(&rest[..i - 1], "\r\n", &rest[i + 1..])
|
||||
}
|
||||
Some(i) if i > 0 && rest.as_bytes()[i - 1] == b'\r' => (&rest[..i - 1], "\r\n", &rest[i + 1..]),
|
||||
Some(i) => (&rest[..i], "\n", &rest[i + 1..]),
|
||||
None => (rest, "", ""),
|
||||
};
|
||||
@@ -74,23 +67,14 @@ impl ConfFile {
|
||||
"\n" => lf += 1,
|
||||
_ => {}
|
||||
}
|
||||
lines.push(Line {
|
||||
text: raw.to_string(),
|
||||
eol: eol.to_string(),
|
||||
kind: classify(raw),
|
||||
});
|
||||
lines.push(Line { text: raw.to_string(), eol: eol.to_string(), kind: classify(raw) });
|
||||
rest = next;
|
||||
}
|
||||
ConfFile {
|
||||
lines,
|
||||
bom,
|
||||
default_eol: if crlf > lf { "\r\n" } else { "\n" },
|
||||
}
|
||||
ConfFile { lines, bom, default_eol: if crlf > lf { "\r\n" } else { "\n" } }
|
||||
}
|
||||
|
||||
pub fn parse_bytes(bytes: &[u8]) -> Result<ConfFile> {
|
||||
let text = std::str::from_utf8(bytes)
|
||||
.map_err(|_| Error::Invalid("configuration file is not valid UTF-8".into()))?;
|
||||
let text = std::str::from_utf8(bytes).map_err(|_| Error::Invalid("configuration file is not valid UTF-8".into()))?;
|
||||
Ok(Self::parse(text))
|
||||
}
|
||||
|
||||
@@ -109,19 +93,14 @@ impl ConfFile {
|
||||
/// Active (uncommented) entries in file order.
|
||||
pub fn entries(&self) -> impl Iterator<Item = (&str, &str)> {
|
||||
self.lines.iter().filter_map(|l| match &l.kind {
|
||||
Kind::Entry { key, prefix_len } => {
|
||||
Some((key.as_str(), l.text[*prefix_len..].trim_end()))
|
||||
}
|
||||
Kind::Entry { key, prefix_len } => Some((key.as_str(), l.text[*prefix_len..].trim_end())),
|
||||
Kind::Other => None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Raw value (quotes included) of the last active occurrence of `key` (the one the server ends up using).
|
||||
pub fn get(&self, key: &str) -> Option<&str> {
|
||||
self.entries()
|
||||
.filter(|(k, _)| *k == key)
|
||||
.map(|(_, v)| v)
|
||||
.last()
|
||||
self.entries().filter(|(k, _)| *k == key).map(|(_, v)| v).last()
|
||||
}
|
||||
|
||||
pub fn contains(&self, key: &str) -> bool {
|
||||
@@ -131,12 +110,7 @@ impl ConfFile {
|
||||
/// Set `raw` (already formatted, quotes included) as the value of `key`. Returns true if the key was appended.
|
||||
/// `doc` lines (without `#`) are written above a newly appended key.
|
||||
pub fn set(&mut self, key: &str, raw: &str, doc: &[&str]) -> bool {
|
||||
if let Some(l) = self
|
||||
.lines
|
||||
.iter_mut()
|
||||
.rev()
|
||||
.find(|l| matches!(&l.kind, Kind::Entry { key: k, .. } if k == key))
|
||||
{
|
||||
if let Some(l) = self.lines.iter_mut().rev().find(|l| matches!(&l.kind, Kind::Entry { key: k, .. } if k == key)) {
|
||||
if let Kind::Entry { prefix_len, .. } = l.kind {
|
||||
let prefix = l.text[..prefix_len].to_string();
|
||||
l.text = format!("{prefix}{raw}");
|
||||
@@ -151,28 +125,16 @@ impl ConfFile {
|
||||
}
|
||||
}
|
||||
for d in doc {
|
||||
self.lines.push(Line {
|
||||
text: format!("# {d}"),
|
||||
eol: eol.to_string(),
|
||||
kind: Kind::Other,
|
||||
});
|
||||
self.lines.push(Line { text: format!("# {d}"), eol: eol.to_string(), kind: Kind::Other });
|
||||
}
|
||||
let text = format!("{key} = {raw}");
|
||||
self.lines.push(Line {
|
||||
kind: classify(&text),
|
||||
text,
|
||||
eol: eol.to_string(),
|
||||
});
|
||||
self.lines.push(Line { kind: classify(&text), text, eol: eol.to_string() });
|
||||
true
|
||||
}
|
||||
|
||||
/// The comment block directly above the last active occurrence of `key` (without the leading `#`).
|
||||
pub fn doc_for(&self, key: &str) -> Vec<String> {
|
||||
let Some(idx) = self
|
||||
.lines
|
||||
.iter()
|
||||
.rposition(|l| matches!(&l.kind, Kind::Entry { key: k, .. } if k == key))
|
||||
else {
|
||||
let Some(idx) = self.lines.iter().rposition(|l| matches!(&l.kind, Kind::Entry { key: k, .. } if k == key)) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let mut doc = Vec::new();
|
||||
@@ -195,11 +157,7 @@ impl ConfFile {
|
||||
}
|
||||
}
|
||||
for d in doc {
|
||||
self.lines.push(Line {
|
||||
text: d.to_string(),
|
||||
eol: eol.to_string(),
|
||||
kind: Kind::Other,
|
||||
});
|
||||
self.lines.push(Line { text: d.to_string(), eol: eol.to_string(), kind: Kind::Other });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -207,9 +165,7 @@ impl ConfFile {
|
||||
/// Strip one pair of surrounding double quotes.
|
||||
pub fn unquote(raw: &str) -> &str {
|
||||
let t = raw.trim();
|
||||
t.strip_prefix('"')
|
||||
.and_then(|s| s.strip_suffix('"'))
|
||||
.unwrap_or(t)
|
||||
t.strip_prefix('"').and_then(|s| s.strip_suffix('"')).unwrap_or(t)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -220,16 +176,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn roundtrip_is_byte_identical_including_odd_input() {
|
||||
for s in [
|
||||
SAMPLE,
|
||||
"",
|
||||
"\n",
|
||||
"no newline",
|
||||
"a=1\n\n\nb = 2\n",
|
||||
"\u{feff}k = v\r\n",
|
||||
"mixed=1\nendings=2\r\nhere=3\n",
|
||||
"=novalue\nweird key = 1\n",
|
||||
] {
|
||||
for s in [SAMPLE, "", "\n", "no newline", "a=1\n\n\nb = 2\n", "\u{feff}k = v\r\n", "mixed=1\nendings=2\r\nhere=3\n", "=novalue\nweird key = 1\n"] {
|
||||
assert_eq!(ConfFile::parse(s).to_text(), s, "input: {s:?}");
|
||||
}
|
||||
}
|
||||
@@ -254,21 +201,14 @@ mod tests {
|
||||
// last duplicate changed, first untouched
|
||||
assert!(out.contains("A.Key = 5\r\n") && out.contains("A.Key = 10\r\n"));
|
||||
// everything else byte-identical
|
||||
assert_eq!(
|
||||
out.replace("C = 9", "C = 7")
|
||||
.replace("A.Key = 10", "A.Key = 6"),
|
||||
SAMPLE
|
||||
);
|
||||
assert_eq!(out.replace("C = 9", "C = 7").replace("A.Key = 10", "A.Key = 6"), SAMPLE);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_appends_missing_key_with_doc_using_file_eol() {
|
||||
let mut f = ConfFile::parse("[worldserver]\r\nA = 1");
|
||||
assert!(f.set("New.Key", "\"x\"", &["Added by CoA Server Manager"]));
|
||||
assert_eq!(
|
||||
f.to_text(),
|
||||
"[worldserver]\r\nA = 1\r\n# Added by CoA Server Manager\r\nNew.Key = \"x\"\r\n"
|
||||
);
|
||||
assert_eq!(f.to_text(), "[worldserver]\r\nA = 1\r\n# Added by CoA Server Manager\r\nNew.Key = \"x\"\r\n");
|
||||
assert_eq!(f.get("New.Key"), Some("\"x\""));
|
||||
}
|
||||
|
||||
|
||||
@@ -166,18 +166,12 @@ impl Setting {
|
||||
_ => Err("must be a number".into()),
|
||||
},
|
||||
SettingType::String => match value.as_str() {
|
||||
Some(s) if s.contains(['"', '\n', '\r']) => {
|
||||
Err("must not contain quotes or line breaks".into())
|
||||
}
|
||||
Some(s) if s.contains(['"', '\n', '\r']) => Err("must not contain quotes or line breaks".into()),
|
||||
Some(s) => Ok(format!("\"{s}\"")),
|
||||
None => Err("must be text".into()),
|
||||
},
|
||||
SettingType::Enum => {
|
||||
let opt = self
|
||||
.options
|
||||
.iter()
|
||||
.find(|o| o.value == *value)
|
||||
.ok_or("is not one of the allowed choices")?;
|
||||
let opt = self.options.iter().find(|o| o.value == *value).ok_or("is not one of the allowed choices")?;
|
||||
Ok(match &opt.value {
|
||||
Value::String(s) => format!("\"{s}\""),
|
||||
other => other.to_string(),
|
||||
@@ -196,10 +190,7 @@ impl Setting {
|
||||
"0" | "false" | "no" | "off" => Ok(Value::Bool(false)),
|
||||
_ => Err(format!("{t:?} is not on/off")),
|
||||
},
|
||||
SettingType::Int => unq
|
||||
.parse::<i64>()
|
||||
.map(Value::from)
|
||||
.map_err(|_| format!("{t:?} is not a whole number")),
|
||||
SettingType::Int => unq.parse::<i64>().map(Value::from).map_err(|_| format!("{t:?} is not a whole number")),
|
||||
SettingType::Float => unq
|
||||
.parse::<f64>()
|
||||
.ok()
|
||||
@@ -207,7 +198,6 @@ impl Setting {
|
||||
.map(Value::Number)
|
||||
.ok_or_else(|| format!("{t:?} is not a number")),
|
||||
SettingType::String => Ok(Value::String(unq.to_string())),
|
||||
#[allow(clippy::cmp_owned)]
|
||||
SettingType::Enum => {
|
||||
for o in &self.options {
|
||||
let matches = match &o.value {
|
||||
@@ -263,12 +253,7 @@ mod tests {
|
||||
assert_eq!(f.to_raw(&json!(1.5)).unwrap(), "1.5");
|
||||
assert_eq!(f.to_raw(&json!(2.0)).unwrap(), "2");
|
||||
assert!(f.to_raw(&json!(-0.1)).is_err());
|
||||
assert_eq!(
|
||||
s(SettingType::Bool, None, None)
|
||||
.to_raw(&json!(true))
|
||||
.unwrap(),
|
||||
"1"
|
||||
);
|
||||
assert_eq!(s(SettingType::Bool, None, None).to_raw(&json!(true)).unwrap(), "1");
|
||||
assert!(s(SettingType::Bool, None, None).to_raw(&json!(1)).is_err());
|
||||
let st = s(SettingType::String, None, None);
|
||||
assert_eq!(st.to_raw(&json!("a b")).unwrap(), "\"a b\"");
|
||||
@@ -279,34 +264,14 @@ mod tests {
|
||||
#[test]
|
||||
fn enum_and_decode() {
|
||||
let mut e = s(SettingType::Enum, None, None);
|
||||
e.options = vec![
|
||||
EnumOption {
|
||||
value: json!(0),
|
||||
label: "A".into(),
|
||||
},
|
||||
EnumOption {
|
||||
value: json!(2),
|
||||
label: "B".into(),
|
||||
},
|
||||
];
|
||||
e.options = vec![EnumOption { value: json!(0), label: "A".into() }, EnumOption { value: json!(2), label: "B".into() }];
|
||||
assert_eq!(e.to_raw(&json!(2)).unwrap(), "2");
|
||||
assert!(e.to_raw(&json!(1)).is_err());
|
||||
assert_eq!(e.from_raw("2").unwrap(), json!(2));
|
||||
assert!(e.from_raw("9").is_err());
|
||||
assert_eq!(
|
||||
s(SettingType::Bool, None, None).from_raw(" 1 ").unwrap(),
|
||||
json!(true)
|
||||
);
|
||||
assert_eq!(
|
||||
s(SettingType::String, None, None)
|
||||
.from_raw("\"x y\"")
|
||||
.unwrap(),
|
||||
json!("x y")
|
||||
);
|
||||
assert_eq!(
|
||||
s(SettingType::Float, None, None).from_raw("1.5").unwrap(),
|
||||
json!(1.5)
|
||||
);
|
||||
assert_eq!(s(SettingType::Bool, None, None).from_raw(" 1 ").unwrap(), json!(true));
|
||||
assert_eq!(s(SettingType::String, None, None).from_raw("\"x y\"").unwrap(), json!("x y"));
|
||||
assert_eq!(s(SettingType::Float, None, None).from_raw("1.5").unwrap(), json!(1.5));
|
||||
assert!(s(SettingType::Int, None, None).from_raw("abc").is_err());
|
||||
}
|
||||
}
|
||||
@@ -42,11 +42,7 @@ pub struct Line {
|
||||
|
||||
fn classify(l: &str) -> Level {
|
||||
let u = l.to_ascii_uppercase();
|
||||
if u.contains(" FATAL ")
|
||||
|| u.contains(" ERROR ")
|
||||
|| u.contains("[ERROR]")
|
||||
|| u.contains("ERROR:")
|
||||
{
|
||||
if u.contains(" FATAL ") || u.contains(" ERROR ") || u.contains("[ERROR]") || u.contains("ERROR:") {
|
||||
Level::Error
|
||||
} else if u.contains(" WARN ") || u.contains("[WARNING]") || u.contains(" WARNING ") {
|
||||
Level::Warn
|
||||
@@ -71,8 +67,7 @@ pub fn lines_from_text(text: &str, filter: Option<&str>, max_lines: usize) -> Ve
|
||||
/// The last `max_lines` lines (after filtering) from the last ~512 KB of a log. Works on multi-gigabyte files.
|
||||
pub fn tail(path: &Path, filter: Option<&str>, max_lines: usize) -> Result<Vec<Line>> {
|
||||
const WINDOW: u64 = 512 * 1024;
|
||||
let mut f =
|
||||
File::open(path).map_err(|_| Error::Invalid("This log does not exist yet.".into()))?;
|
||||
let mut f = File::open(path).map_err(|_| Error::Invalid("This log does not exist yet.".into()))?;
|
||||
let len = f.metadata()?.len();
|
||||
let start = len.saturating_sub(WINDOW);
|
||||
f.seek(SeekFrom::Start(start))?;
|
||||
@@ -86,16 +81,8 @@ pub fn tail(path: &Path, filter: Option<&str>, max_lines: usize) -> Result<Vec<L
|
||||
let needle = filter.map(str::to_lowercase).filter(|n| !n.is_empty());
|
||||
let out: Vec<Line> = lines
|
||||
.into_iter()
|
||||
.filter(|l| {
|
||||
needle
|
||||
.as_ref()
|
||||
.map(|n| l.to_lowercase().contains(n))
|
||||
.unwrap_or(true)
|
||||
})
|
||||
.map(|l| Line {
|
||||
level: classify(l),
|
||||
text: crate::diag::redact(l),
|
||||
})
|
||||
.filter(|l| needle.as_ref().map(|n| l.to_lowercase().contains(n)).unwrap_or(true))
|
||||
.map(|l| Line { level: classify(l), text: crate::diag::redact(l) })
|
||||
.collect();
|
||||
let skip = out.len().saturating_sub(max_lines);
|
||||
Ok(out.into_iter().skip(skip).collect())
|
||||
@@ -113,20 +100,7 @@ pub enum Risk {
|
||||
pub fn risk(command: &str) -> Risk {
|
||||
let c = command.trim().trim_start_matches('.').to_lowercase();
|
||||
const DANGEROUS: [&str; 14] = [
|
||||
"server shutdown",
|
||||
"server exit",
|
||||
"server restart",
|
||||
"account delete",
|
||||
"character delete",
|
||||
"character erase",
|
||||
"reset ",
|
||||
"ban ",
|
||||
"unban ",
|
||||
"deleted ",
|
||||
"server idlerestart",
|
||||
"server idleshutdown",
|
||||
"account set password",
|
||||
"reload ",
|
||||
"server shutdown", "server exit", "server restart", "account delete", "character delete", "character erase", "reset ", "ban ", "unban ", "deleted ", "server idlerestart", "server idleshutdown", "account set password", "reload ",
|
||||
];
|
||||
if DANGEROUS.iter().any(|d| c.starts_with(d)) {
|
||||
Risk::Dangerous
|
||||
@@ -161,10 +135,7 @@ mod tests {
|
||||
let all = tail(&p, None, 5).unwrap();
|
||||
assert_eq!(all.len(), 5);
|
||||
assert_eq!(all[3].level, Level::Error);
|
||||
assert!(
|
||||
!all[3].text.contains("abc"),
|
||||
"secret-looking lines are redacted"
|
||||
);
|
||||
assert!(!all[3].text.contains("abc"), "secret-looking lines are redacted");
|
||||
assert_eq!(all[4].level, Level::Warn);
|
||||
let f = tail(&p, Some("CAREFUL"), 100).unwrap();
|
||||
assert_eq!(f.len(), 1);
|
||||
@@ -173,14 +144,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn dangerous_commands_are_recognised_and_input_is_validated() {
|
||||
for c in [
|
||||
"server shutdown 10",
|
||||
".account delete bob",
|
||||
"character delete x",
|
||||
"reset talents",
|
||||
"ban account x 1d y",
|
||||
"reload all",
|
||||
] {
|
||||
for c in ["server shutdown 10", ".account delete bob", "character delete x", "reset talents", "ban account x 1d y", "reload all"] {
|
||||
assert_eq!(risk(c), Risk::Dangerous, "{c}");
|
||||
}
|
||||
for c in ["server info", ".account onlinelist", "lookup item sword"] {
|
||||
|
||||
@@ -1,246 +0,0 @@
|
||||
//! The real [`RealmBackend`]: the realm's login and character databases (read through the same `mysql` tools the rest of the Manager uses), its world console (for
|
||||
//! creating accounts and setting passwords, the supported way) and this Manager's portable engine (for exporting characters).
|
||||
//!
|
||||
//! Account names are validated before they reach any statement. Passwords go to the console command and, for a link, into the computation of the realm's own SRP6
|
||||
//! verifier to compare with the one stored; they are never written anywhere by this module.
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
use coa_control_proto::app;
|
||||
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::service::{
|
||||
AccountRow, CharInfo, ClaimExport, PreflightVerdict, Presence, RealmBackend, RealmInfo,
|
||||
RemoteImportOutcome, RemoteImportParams,
|
||||
};
|
||||
use crate::portable::realm;
|
||||
use crate::portable::service::access::RealmAccess;
|
||||
use crate::portable::service::runtime::PortableRuntime;
|
||||
use crate::realm_registry::advert::find_access;
|
||||
use crate::realm_registry::settings;
|
||||
use crate::{Error, Result};
|
||||
|
||||
pub type Installs = Arc<dyn Fn() -> Vec<(String, PathBuf)> + Send + Sync>;
|
||||
|
||||
pub struct LocalBackend {
|
||||
portable: Arc<PortableRuntime>,
|
||||
/// Folder of `registry.json` (the owner's choices per realm: existing accounts only, a stated route).
|
||||
settings_dir: PathBuf,
|
||||
descriptors: PathBuf,
|
||||
installs: Installs,
|
||||
}
|
||||
|
||||
fn unavailable(what: &str, e: impl std::fmt::Display) -> Error {
|
||||
Error::Invalid(format!("{what}: {e}"))
|
||||
}
|
||||
|
||||
fn name(username: &str) -> Result<String> {
|
||||
let upper = username.to_ascii_uppercase();
|
||||
if !app::valid_username(&upper) {
|
||||
return Err(Error::Invalid("That is not a game account name.".into()));
|
||||
}
|
||||
Ok(upper)
|
||||
}
|
||||
|
||||
fn is_internal(upper: &str) -> bool {
|
||||
upper == "COAMANAGER" || upper.starts_with("COABOT")
|
||||
}
|
||||
|
||||
impl LocalBackend {
|
||||
pub fn new(
|
||||
portable: Arc<PortableRuntime>,
|
||||
settings_dir: impl Into<PathBuf>,
|
||||
descriptors: impl Into<PathBuf>,
|
||||
installs: Installs,
|
||||
) -> Self {
|
||||
Self {
|
||||
portable,
|
||||
settings_dir: settings_dir.into(),
|
||||
descriptors: descriptors.into(),
|
||||
installs,
|
||||
}
|
||||
}
|
||||
|
||||
fn access(&self, local_id: &str) -> Result<RealmAccess> {
|
||||
find_access(&self.descriptors, &(self.installs)(), local_id)
|
||||
.ok_or_else(|| Error::Invalid("That realm is not known to this Manager.".into()))
|
||||
}
|
||||
|
||||
fn account(&self, local_id: &str, filter: &str) -> Result<Option<AccountRow>> {
|
||||
let db = self.access(local_id)?.db()?;
|
||||
let out = db.query(&format!(
|
||||
"SELECT id, username FROM acore_auth.account WHERE {filter} LIMIT 1;"
|
||||
))?;
|
||||
Ok(out.lines().find(|l| !l.trim().is_empty()).and_then(|l| {
|
||||
let mut c = l.split('\t');
|
||||
Some(AccountRow {
|
||||
id: c.next()?.trim().parse().ok()?,
|
||||
username: c.next()?.trim().to_string(),
|
||||
})
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl RealmBackend for LocalBackend {
|
||||
fn info(&self, local_id: &str) -> Result<RealmInfo> {
|
||||
let cfg = settings::load(&self.settings_dir)?.realms.remove(local_id);
|
||||
Ok(RealmInfo {
|
||||
automatic: cfg.as_ref().is_none_or(|c| !c.existing_only),
|
||||
route: cfg.and_then(|c| c.route),
|
||||
})
|
||||
}
|
||||
|
||||
fn account_by_name(&self, local_id: &str, username: &str) -> Result<Option<AccountRow>> {
|
||||
let n = name(username)?;
|
||||
self.account(local_id, &format!("username = '{n}'"))
|
||||
}
|
||||
|
||||
fn account_by_id(&self, local_id: &str, id: u32) -> Result<Option<AccountRow>> {
|
||||
self.account(local_id, &format!("id = {id}"))
|
||||
}
|
||||
|
||||
fn create_account(&self, local_id: &str, username: &str, password: &str) -> Result<()> {
|
||||
let n = name(username)?;
|
||||
if is_internal(&n) {
|
||||
return Err(Error::Invalid("That name is reserved.".into()));
|
||||
}
|
||||
self.access(local_id)?.ra()?.create_account(&n, password)
|
||||
}
|
||||
|
||||
fn set_password(&self, local_id: &str, username: &str, password: &str) -> Result<()> {
|
||||
let n = name(username)?;
|
||||
if is_internal(&n) {
|
||||
return Err(Error::Invalid("That name is reserved.".into()));
|
||||
}
|
||||
self.access(local_id)?
|
||||
.ra()?
|
||||
.set_account_password(&n, password)
|
||||
}
|
||||
|
||||
fn check_login(
|
||||
&self,
|
||||
local_id: &str,
|
||||
username: &str,
|
||||
password: &str,
|
||||
) -> Result<Option<AccountRow>> {
|
||||
let n = name(username)?;
|
||||
if is_internal(&n) || !app::valid_password_for_link(password) {
|
||||
return Ok(None);
|
||||
}
|
||||
let db = self.access(local_id)?.db()?;
|
||||
let out = db.query(&format!("SELECT id, username, HEX(salt), HEX(verifier) FROM acore_auth.account WHERE username = '{n}' LIMIT 1;"))?;
|
||||
let Some(line) = out.lines().find(|l| !l.trim().is_empty()) else {
|
||||
return Ok(None);
|
||||
};
|
||||
let c: Vec<&str> = line.split('\t').map(str::trim).collect();
|
||||
if c.len() < 4 {
|
||||
return Ok(None);
|
||||
}
|
||||
let (Ok(salt), Ok(stored)) = (hex::decode(c[2]), hex::decode(c[3])) else {
|
||||
return Ok(None);
|
||||
};
|
||||
let Ok(salt): std::result::Result<[u8; 32], _> = salt.try_into() else {
|
||||
return Ok(None);
|
||||
};
|
||||
let computed = crate::srp6::verifier(&n, password, &salt);
|
||||
// both are fixed-size secrets of equal length; compare without an early exit
|
||||
let equal = stored.len() == 32
|
||||
&& computed
|
||||
.iter()
|
||||
.zip(&stored)
|
||||
.fold(0u8, |acc, (a, b)| acc | (a ^ b))
|
||||
== 0;
|
||||
Ok(equal
|
||||
.then(|| AccountRow {
|
||||
id: c[0].parse().unwrap_or(0),
|
||||
username: c[1].to_string(),
|
||||
})
|
||||
.filter(|r| r.id != 0))
|
||||
}
|
||||
|
||||
fn presence(&self, local_id: &str, account_id: u32) -> Result<Presence> {
|
||||
let db = self.access(local_id)?.db()?;
|
||||
let out = db.query(&format!("SELECT (SELECT online FROM acore_auth.account WHERE id = {account_id}), (SELECT COUNT(*) FROM acore_characters.characters WHERE account = {account_id} AND online = 1);"))?;
|
||||
let line = out.lines().find(|l| !l.trim().is_empty()).unwrap_or("");
|
||||
let mut c = line.split('\t').map(str::trim);
|
||||
let account_online = c
|
||||
.next()
|
||||
.is_some_and(|v| !v.is_empty() && v != "NULL" && v != "0");
|
||||
let online_characters = c.next().and_then(|v| v.parse().ok()).unwrap_or(0);
|
||||
Ok(Presence {
|
||||
account_online,
|
||||
online_characters,
|
||||
})
|
||||
}
|
||||
|
||||
fn characters(&self, local_id: &str, account_id: u32) -> Result<Vec<CharInfo>> {
|
||||
let db = self.access(local_id)?.db()?;
|
||||
let list =
|
||||
realm::inspect_characters(&db).map_err(|e| unavailable("reading characters", e))?;
|
||||
Ok(list
|
||||
.into_iter()
|
||||
.filter(|c| c.account == account_id)
|
||||
.map(|c| CharInfo {
|
||||
guid: c.local_guid,
|
||||
name: c.name.clone(),
|
||||
class: c.class,
|
||||
race: c.race,
|
||||
level: c.level,
|
||||
eligible: c.eligible(),
|
||||
reasons: c.blockers.iter().map(|b| b.code().to_string()).collect(),
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn is_portable(&self, local_id: &str, guid: u32) -> Result<bool> {
|
||||
let id = local_id.to_string();
|
||||
self.portable
|
||||
.call(move |s| s.is_portable_here(&id, guid))
|
||||
.map_err(|e| unavailable("portable engine", e))?
|
||||
.map_err(|e| unavailable("portable engine", e))
|
||||
}
|
||||
|
||||
fn export(&self, local_id: &str, _account_id: u32, guid: u32) -> Result<ClaimExport> {
|
||||
let id = local_id.to_string();
|
||||
let bundle = self
|
||||
.portable
|
||||
.call(move |s| s.export_for_claim(&id, guid))
|
||||
.map_err(|e| unavailable("portable engine", e))?
|
||||
.map_err(|e| unavailable("export", e))?;
|
||||
Ok(ClaimExport {
|
||||
character_id: bundle.character_id.as_uuid(),
|
||||
payload: bundle.payload,
|
||||
sha256: bundle.content_hash,
|
||||
collections: bundle.collections,
|
||||
})
|
||||
}
|
||||
|
||||
fn check_transfer(
|
||||
&self,
|
||||
local_id: &str,
|
||||
character_id: Uuid,
|
||||
revision: u64,
|
||||
) -> Result<PreflightVerdict> {
|
||||
let id = local_id.to_string();
|
||||
let cid = crate::portable::CharacterId::from_uuid(character_id)
|
||||
.map_err(|e| unavailable("character id", e))?;
|
||||
self.portable
|
||||
.call(move |s| s.check_remote_transfer(&id, cid, revision))
|
||||
.map_err(|e| unavailable("portable engine", e))?
|
||||
.map_err(|e| unavailable("check transfer", e))
|
||||
}
|
||||
|
||||
fn import_remote(
|
||||
&self,
|
||||
local_id: &str,
|
||||
params: RemoteImportParams,
|
||||
) -> Result<RemoteImportOutcome> {
|
||||
let id = local_id.to_string();
|
||||
self.portable
|
||||
.call(move |s| s.import_remote_character(&id, params))
|
||||
.map_err(|e| unavailable("portable engine", e))?
|
||||
.map_err(|e| unavailable("import remote", e))
|
||||
}
|
||||
}
|
||||
@@ -1,466 +0,0 @@
|
||||
//! Direct Ingress Proxy for Phase 14:
|
||||
//! Client connects to direct public TCP -> Host direct ingress/proxy
|
||||
//! ├─ AUTH -> 127.0.0.1:3724 (with narrow CMD_REALM_LIST address rewrite)
|
||||
//! └─ WORLD -> 127.0.0.1:8085 (dumb byte tunnel)
|
||||
//!
|
||||
//! Reuses `coa_control_proto::relay::rewrite_realm_list_address` so direct Internet players,
|
||||
//! Relay players, and LAN players coexist without mutating AzerothCore's MySQL realmlist.
|
||||
|
||||
use std::io::{Read, Write};
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::thread::JoinHandle;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
|
||||
pub struct DirectIngress {
|
||||
auth_port: u16,
|
||||
world_port: u16,
|
||||
world_target: Arc<std::sync::RwLock<String>>,
|
||||
stop: Arc<AtomicBool>,
|
||||
#[allow(dead_code)]
|
||||
threads: Vec<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl DirectIngress {
|
||||
pub fn start(
|
||||
auth_bind_port: u16,
|
||||
world_bind_port: u16,
|
||||
public_ip: String,
|
||||
ext_world_port: u16,
|
||||
local_auth_port: u16,
|
||||
local_world_port: u16,
|
||||
) -> Result<Self> {
|
||||
let auth_listener =
|
||||
TcpListener::bind(format!("0.0.0.0:{auth_bind_port}")).map_err(|e| {
|
||||
Error::Invalid(format!(
|
||||
"Cannot bind direct auth listener on port {auth_bind_port}: {e}"
|
||||
))
|
||||
})?;
|
||||
auth_listener
|
||||
.set_nonblocking(true)
|
||||
.map_err(|e| Error::Invalid(e.to_string()))?;
|
||||
let auth_actual_port = auth_listener
|
||||
.local_addr()
|
||||
.map_err(|e| Error::Invalid(e.to_string()))?
|
||||
.port();
|
||||
|
||||
let world_listener =
|
||||
TcpListener::bind(format!("0.0.0.0:{world_bind_port}")).map_err(|e| {
|
||||
Error::Invalid(format!(
|
||||
"Cannot bind direct world listener on port {world_bind_port}: {e}"
|
||||
))
|
||||
})?;
|
||||
world_listener
|
||||
.set_nonblocking(true)
|
||||
.map_err(|e| Error::Invalid(e.to_string()))?;
|
||||
let world_actual_port = world_listener
|
||||
.local_addr()
|
||||
.map_err(|e| Error::Invalid(e.to_string()))?
|
||||
.port();
|
||||
|
||||
let stop = Arc::new(AtomicBool::new(false));
|
||||
let mut threads = Vec::new();
|
||||
|
||||
let effective_world_port = if ext_world_port == 0 {
|
||||
world_actual_port
|
||||
} else {
|
||||
ext_world_port
|
||||
};
|
||||
let world_target = Arc::new(std::sync::RwLock::new(format!(
|
||||
"{public_ip}:{effective_world_port}"
|
||||
)));
|
||||
|
||||
// Spawn Auth listener thread
|
||||
let stop_auth = stop.clone();
|
||||
let target_clone = world_target.clone();
|
||||
let auth_handle = std::thread::Builder::new()
|
||||
.name("direct-ingress-auth".into())
|
||||
.spawn(move || {
|
||||
while !stop_auth.load(Ordering::Relaxed) {
|
||||
match auth_listener.accept() {
|
||||
Ok((client_stream, _)) => {
|
||||
let _ = client_stream.set_nonblocking(false);
|
||||
let target = target_clone.clone();
|
||||
std::thread::Builder::new()
|
||||
.name("direct-auth-worker".into())
|
||||
.spawn(move || {
|
||||
let _ =
|
||||
handle_auth_client(client_stream, local_auth_port, target);
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
|
||||
std::thread::sleep(Duration::from_millis(20));
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
})
|
||||
.map_err(|e| Error::Invalid(e.to_string()))?;
|
||||
threads.push(auth_handle);
|
||||
|
||||
// Spawn World listener thread
|
||||
let stop_world = stop.clone();
|
||||
let world_handle = std::thread::Builder::new()
|
||||
.name("direct-ingress-world".into())
|
||||
.spawn(move || {
|
||||
while !stop_world.load(Ordering::Relaxed) {
|
||||
match world_listener.accept() {
|
||||
Ok((client_stream, _)) => {
|
||||
let _ = client_stream.set_nonblocking(false);
|
||||
std::thread::Builder::new()
|
||||
.name("direct-world-worker".into())
|
||||
.spawn(move || {
|
||||
let _ = handle_world_client(client_stream, local_world_port);
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
|
||||
std::thread::sleep(Duration::from_millis(20));
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
})
|
||||
.map_err(|e| Error::Invalid(e.to_string()))?;
|
||||
threads.push(world_handle);
|
||||
|
||||
Ok(Self {
|
||||
auth_port: auth_actual_port,
|
||||
world_port: world_actual_port,
|
||||
world_target,
|
||||
stop,
|
||||
threads,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn auth_port(&self) -> u16 {
|
||||
self.auth_port
|
||||
}
|
||||
|
||||
pub fn world_port(&self) -> u16 {
|
||||
self.world_port
|
||||
}
|
||||
|
||||
pub fn update_external_world_endpoint(&self, public_ip: &str, mapped_world_port: u16) {
|
||||
if let Ok(mut g) = self.world_target.write() {
|
||||
*g = format!("{public_ip}:{mapped_world_port}");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn world_target(&self) -> String {
|
||||
self.world_target
|
||||
.read()
|
||||
.map(|s| s.clone())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn stop(&mut self) {
|
||||
self.stop.store(true, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for DirectIngress {
|
||||
fn drop(&mut self) {
|
||||
self.stop();
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_auth_client(
|
||||
mut client: TcpStream,
|
||||
local_auth_port: u16,
|
||||
world_target: Arc<std::sync::RwLock<String>>,
|
||||
) -> Result<()> {
|
||||
let mut server = TcpStream::connect(format!("127.0.0.1:{local_auth_port}"))
|
||||
.map_err(|e| Error::Invalid(format!("Cannot connect to local authserver: {e}")))?;
|
||||
|
||||
let _ = client.set_read_timeout(Some(Duration::from_millis(500)));
|
||||
let _ = server.set_read_timeout(Some(Duration::from_millis(500)));
|
||||
|
||||
let mut client_read = client
|
||||
.try_clone()
|
||||
.map_err(|e| Error::Invalid(e.to_string()))?;
|
||||
let mut server_write = server
|
||||
.try_clone()
|
||||
.map_err(|e| Error::Invalid(e.to_string()))?;
|
||||
|
||||
let done = Arc::new(AtomicBool::new(false));
|
||||
let done_c = done.clone();
|
||||
let server_shutdown = server.try_clone().ok();
|
||||
|
||||
// Forward Client -> Auth Server
|
||||
let t_c2s = std::thread::spawn(move || {
|
||||
let mut buf = [0u8; 4096];
|
||||
while !done_c.load(Ordering::Relaxed) {
|
||||
match client_read.read(&mut buf) {
|
||||
Ok(0) => break,
|
||||
Ok(n) => {
|
||||
if server_write.write_all(&buf[..n]).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(ref e)
|
||||
if e.kind() == std::io::ErrorKind::WouldBlock
|
||||
|| e.kind() == std::io::ErrorKind::TimedOut =>
|
||||
{
|
||||
continue;
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
done_c.store(true, Ordering::Relaxed);
|
||||
if let Some(s) = server_shutdown {
|
||||
let _ = s.shutdown(std::net::Shutdown::Both);
|
||||
}
|
||||
});
|
||||
|
||||
// Forward Auth Server -> Client with narrow CMD_REALM_LIST rewrite
|
||||
let client_shutdown = client.try_clone().ok();
|
||||
let mut buf = [0u8; 8192];
|
||||
while !done.load(Ordering::Relaxed) {
|
||||
match server.read(&mut buf) {
|
||||
Ok(0) => break,
|
||||
Ok(n) => {
|
||||
let chunk = &buf[..n];
|
||||
let current_target = world_target
|
||||
.read()
|
||||
.map(|s| s.clone())
|
||||
.unwrap_or_else(|_| "127.0.0.1:8085".to_string());
|
||||
match coa_control_proto::relay::rewrite_realm_list_address(chunk, ¤t_target) {
|
||||
Ok(Some(rewritten)) => {
|
||||
if client.write_all(&rewritten).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
if client.write_all(chunk).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(ref e)
|
||||
if e.kind() == std::io::ErrorKind::WouldBlock
|
||||
|| e.kind() == std::io::ErrorKind::TimedOut =>
|
||||
{
|
||||
continue;
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
done.store(true, Ordering::Relaxed);
|
||||
if let Some(c) = client_shutdown {
|
||||
let _ = c.shutdown(std::net::Shutdown::Write);
|
||||
}
|
||||
|
||||
let _ = t_c2s.join();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_world_client(mut client: TcpStream, local_world_port: u16) -> Result<()> {
|
||||
let mut server = TcpStream::connect(format!("127.0.0.1:{local_world_port}"))
|
||||
.map_err(|e| Error::Invalid(format!("Cannot connect to local worldserver: {e}")))?;
|
||||
|
||||
let _ = client.set_nonblocking(false);
|
||||
let _ = server.set_nonblocking(false);
|
||||
let _ = client.set_read_timeout(None);
|
||||
let _ = server.set_read_timeout(None);
|
||||
|
||||
let mut client_read = client
|
||||
.try_clone()
|
||||
.map_err(|e| Error::Invalid(e.to_string()))?;
|
||||
let mut server_write = server
|
||||
.try_clone()
|
||||
.map_err(|e| Error::Invalid(e.to_string()))?;
|
||||
|
||||
let t_c2s = std::thread::spawn(move || {
|
||||
let _ = std::io::copy(&mut client_read, &mut server_write);
|
||||
let _ = server_write.shutdown(std::net::Shutdown::Both);
|
||||
});
|
||||
|
||||
let _ = std::io::copy(&mut server, &mut client);
|
||||
let _ = client.shutdown(std::net::Shutdown::Both);
|
||||
|
||||
let _ = t_c2s.join();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_direct_ingress_auth_and_world() {
|
||||
let mock_auth = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let mock_auth_port = mock_auth.local_addr().unwrap().port();
|
||||
|
||||
let mock_world = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let mock_world_port = mock_world.local_addr().unwrap().port();
|
||||
|
||||
let mut ingress = DirectIngress::start(
|
||||
0,
|
||||
0,
|
||||
"198.51.100.1".to_string(),
|
||||
8085,
|
||||
mock_auth_port,
|
||||
mock_world_port,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let ingress_auth = ingress.auth_port();
|
||||
let ingress_world = ingress.world_port();
|
||||
|
||||
// 1. Verify World dumb byte tunnel
|
||||
let t_world = std::thread::spawn(move || {
|
||||
let (mut stream, _) = mock_world.accept().unwrap();
|
||||
let mut buf = [0u8; 16];
|
||||
let n = stream.read(&mut buf).unwrap();
|
||||
assert_eq!(&buf[..n], b"WORLD_PING");
|
||||
stream.write_all(b"WORLD_PONG").unwrap();
|
||||
let _ = stream.shutdown(std::net::Shutdown::Write);
|
||||
});
|
||||
|
||||
let mut client_world = TcpStream::connect(format!("127.0.0.1:{ingress_world}")).unwrap();
|
||||
client_world.write_all(b"WORLD_PING").unwrap();
|
||||
let mut resp = [0u8; 10];
|
||||
client_world.read_exact(&mut resp).unwrap();
|
||||
assert_eq!(&resp, b"WORLD_PONG");
|
||||
drop(client_world);
|
||||
t_world.join().unwrap();
|
||||
|
||||
// 2. Verify Auth rewrite
|
||||
let t_auth = std::thread::spawn(move || {
|
||||
let (mut stream, _) = mock_auth.accept().unwrap();
|
||||
let mut buf = [0u8; 16];
|
||||
let _ = stream.read(&mut buf).unwrap();
|
||||
|
||||
let mut fake_realm_list = vec![
|
||||
0x10, // Opcode CMD_REALM_LIST
|
||||
0x00, 0x00, // Size placeholder
|
||||
0x00, 0x00, 0x00, 0x00, // Unused
|
||||
0x01, 0x00, // Realm count: 1
|
||||
0x01, 0x00, 0x00, 0x00, // Icon
|
||||
0x00, // Lock
|
||||
0x00, // Flags
|
||||
];
|
||||
fake_realm_list.extend_from_slice(b"RealmOne\0");
|
||||
fake_realm_list.extend_from_slice(b"127.0.0.1:8085\0"); // original target
|
||||
fake_realm_list.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]); // Population
|
||||
fake_realm_list.extend_from_slice(&[0x01]); // Num characters
|
||||
fake_realm_list.extend_from_slice(&[0x01]); // Timezone
|
||||
fake_realm_list.extend_from_slice(&[0x00]); // Realm ID
|
||||
fake_realm_list.extend_from_slice(&[0x02, 0x00]); // Footer
|
||||
|
||||
let body_len = (fake_realm_list.len() - 3) as u16;
|
||||
fake_realm_list[1..3].copy_from_slice(&body_len.to_le_bytes());
|
||||
|
||||
stream.write_all(&fake_realm_list).unwrap();
|
||||
let _ = stream.shutdown(std::net::Shutdown::Write);
|
||||
});
|
||||
|
||||
let mut client_auth = TcpStream::connect(format!("127.0.0.1:{ingress_auth}")).unwrap();
|
||||
client_auth.write_all(b"AUTH_HELLO").unwrap();
|
||||
let mut auth_buf = [0u8; 512];
|
||||
let an = client_auth.read(&mut auth_buf).unwrap();
|
||||
let payload = String::from_utf8_lossy(&auth_buf[..an]);
|
||||
assert!(
|
||||
payload.contains("198.51.100.1:8085"),
|
||||
"Expected rewritten world address, got: {payload}"
|
||||
);
|
||||
drop(client_auth);
|
||||
t_auth.join().unwrap();
|
||||
|
||||
ingress.stop();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_direct_ingress_uses_final_mapped_world_port_not_desired_or_bound() {
|
||||
let desired_world_port = 8085;
|
||||
let mock_auth = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let mock_auth_port = mock_auth.local_addr().unwrap().port();
|
||||
|
||||
let mock_world = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let mock_world_port = mock_world.local_addr().unwrap().port();
|
||||
|
||||
// DirectIngress binds to ephemeral ports (0, 0), so actual ingress port != 8085
|
||||
let ingress = DirectIngress::start(
|
||||
0,
|
||||
0,
|
||||
"198.51.100.1".to_string(),
|
||||
desired_world_port,
|
||||
mock_auth_port,
|
||||
mock_world_port,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let actual_ingress_world = ingress.world_port();
|
||||
assert_ne!(
|
||||
actual_ingress_world, desired_world_port,
|
||||
"Actual ingress port must not be 8085"
|
||||
);
|
||||
|
||||
// The router / NAT-PMP assigns an external mapped world port != 8085 and != actual ingress port
|
||||
let external_mapped_world_port = 48085;
|
||||
assert_ne!(external_mapped_world_port, desired_world_port);
|
||||
assert_ne!(external_mapped_world_port, actual_ingress_world);
|
||||
|
||||
// Update with final verified mapped external endpoint: public_ip:mapped_world_port
|
||||
ingress.update_external_world_endpoint("198.51.100.1", external_mapped_world_port);
|
||||
|
||||
let t_auth = std::thread::spawn(move || {
|
||||
let (mut stream, _) = mock_auth.accept().unwrap();
|
||||
let mut buf = [0u8; 16];
|
||||
let _ = stream.read(&mut buf).unwrap();
|
||||
|
||||
let mut fake_realm_list = vec![
|
||||
0x10, // Opcode CMD_REALM_LIST
|
||||
0x00, 0x00, // Size placeholder
|
||||
0x00, 0x00, 0x00, 0x00, // Unused
|
||||
0x01, 0x00, // Realm count: 1
|
||||
0x01, 0x00, 0x00, 0x00, // Icon
|
||||
0x00, // Lock
|
||||
0x00, // Flags
|
||||
];
|
||||
fake_realm_list.extend_from_slice(b"RealmOne\0");
|
||||
fake_realm_list.extend_from_slice(b"127.0.0.1:8085\0");
|
||||
fake_realm_list.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]);
|
||||
fake_realm_list.extend_from_slice(&[0x01]);
|
||||
fake_realm_list.extend_from_slice(&[0x01]);
|
||||
fake_realm_list.extend_from_slice(&[0x00]);
|
||||
fake_realm_list.extend_from_slice(&[0x02, 0x00]);
|
||||
|
||||
let body_len = (fake_realm_list.len() - 3) as u16;
|
||||
fake_realm_list[1..3].copy_from_slice(&body_len.to_le_bytes());
|
||||
|
||||
stream.write_all(&fake_realm_list).unwrap();
|
||||
let _ = stream.shutdown(std::net::Shutdown::Write);
|
||||
});
|
||||
|
||||
let mut client_auth =
|
||||
TcpStream::connect(format!("127.0.0.1:{}", ingress.auth_port())).unwrap();
|
||||
client_auth.write_all(b"AUTH_HELLO").unwrap();
|
||||
let mut auth_buf = [0u8; 512];
|
||||
let an = client_auth.read(&mut auth_buf).unwrap();
|
||||
let payload = String::from_utf8_lossy(&auth_buf[..an]);
|
||||
|
||||
// Assert that the rewritten REALM_LIST contains EXACTLY the mapped external port
|
||||
assert!(
|
||||
payload.contains("198.51.100.1:48085"),
|
||||
"Expected rewritten realm list to contain mapped external port 48085, got: {payload}"
|
||||
);
|
||||
assert!(
|
||||
!payload.contains("198.51.100.1:8085"),
|
||||
"Must NOT contain desired world port 8085"
|
||||
);
|
||||
assert!(
|
||||
!payload.contains(&format!("198.51.100.1:{actual_ingress_world}")),
|
||||
"Must NOT contain actual internal ingress port"
|
||||
);
|
||||
|
||||
drop(client_auth);
|
||||
t_auth.join().unwrap();
|
||||
}
|
||||
}
|
||||
@@ -1,583 +0,0 @@
|
||||
//! Direct Route Discovery and State Machine (Phase 14):
|
||||
//! State machine:
|
||||
//! Probe interfaces
|
||||
//! -> discover public address
|
||||
//! -> check existing reachability
|
||||
//! -> try automatic mapping (UPnP IGD, NAT-PMP, PCP) if needed
|
||||
//! -> start DirectIngress proxy (Auth rewrite + World byte tunnel)
|
||||
//! -> verify externally via Coordinator probe helper
|
||||
//! -> DIRECT_READY
|
||||
//! otherwise
|
||||
//! RELAY_ONLY / DIRECT_UNAVAILABLE
|
||||
//!
|
||||
//! Important: STUN is discovery only (never hole punching). External verification prevents
|
||||
//! false positives from broken router hairpinning. Raw public IP is never stored in public Registry.
|
||||
|
||||
use std::net::Ipv4Addr;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, RwLock};
|
||||
use std::time::Duration;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::direct_ingress::DirectIngress;
|
||||
use super::nat_pmp;
|
||||
use super::service::HostService;
|
||||
use crate::net;
|
||||
use crate::upnp;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(tag = "status", rename_all = "snake_case")]
|
||||
pub enum RouteStatus {
|
||||
Probing,
|
||||
DirectReady {
|
||||
public_ip: String,
|
||||
auth_port: u16,
|
||||
world_port: u16,
|
||||
method: String,
|
||||
},
|
||||
RelayOnly {
|
||||
reason: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum ActiveMapping {
|
||||
None,
|
||||
Upnp {
|
||||
gateway: upnp::Gateway,
|
||||
auth_port: u16,
|
||||
world_port: u16,
|
||||
},
|
||||
NatPmp {
|
||||
gateway: Ipv4Addr,
|
||||
auth_internal: u16,
|
||||
auth_external: u16,
|
||||
world_internal: u16,
|
||||
world_external: u16,
|
||||
lifetime_secs: u32,
|
||||
renew_at: std::time::Instant,
|
||||
},
|
||||
}
|
||||
|
||||
impl ActiveMapping {
|
||||
pub fn cleanup(&mut self) {
|
||||
match self {
|
||||
ActiveMapping::None => {}
|
||||
ActiveMapping::Upnp {
|
||||
gateway,
|
||||
auth_port,
|
||||
world_port,
|
||||
} => {
|
||||
let _ = upnp::remove_mapping(gateway, *auth_port);
|
||||
let _ = upnp::remove_mapping(gateway, *world_port);
|
||||
}
|
||||
ActiveMapping::NatPmp {
|
||||
gateway,
|
||||
auth_internal,
|
||||
world_internal,
|
||||
..
|
||||
} => {
|
||||
let _ = nat_pmp::delete_mapping(*gateway, *auth_internal);
|
||||
let _ = nat_pmp::delete_mapping(*gateway, *world_internal);
|
||||
}
|
||||
}
|
||||
*self = ActiveMapping::None;
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DirectRouteManager {
|
||||
status: Arc<RwLock<RouteStatus>>,
|
||||
stop: Arc<AtomicBool>,
|
||||
handle: Option<std::thread::JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl DirectRouteManager {
|
||||
pub fn start(registry_base_url: Option<String>, service: Arc<HostService>) -> Self {
|
||||
let status = Arc::new(RwLock::new(RouteStatus::Probing));
|
||||
let stop = Arc::new(AtomicBool::new(false));
|
||||
|
||||
let st_clone = status.clone();
|
||||
let stop_clone = stop.clone();
|
||||
|
||||
let handle = std::thread::Builder::new()
|
||||
.name("direct-route-manager".into())
|
||||
.spawn(move || {
|
||||
run_route_manager(registry_base_url, service, st_clone, stop_clone);
|
||||
})
|
||||
.ok();
|
||||
|
||||
Self {
|
||||
status,
|
||||
stop,
|
||||
handle,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn status(&self) -> RouteStatus {
|
||||
self.status
|
||||
.read()
|
||||
.map(|s| s.clone())
|
||||
.unwrap_or(RouteStatus::Probing)
|
||||
}
|
||||
|
||||
pub fn is_direct_ready(&self) -> bool {
|
||||
matches!(self.status(), RouteStatus::DirectReady { .. })
|
||||
}
|
||||
|
||||
pub fn shutdown(&mut self) {
|
||||
self.stop.store(true, Ordering::SeqCst);
|
||||
if let Some(h) = self.handle.take() {
|
||||
let _ = h.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for DirectRouteManager {
|
||||
fn drop(&mut self) {
|
||||
self.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
fn set_status(status: &Arc<RwLock<RouteStatus>>, new_status: RouteStatus) {
|
||||
if let Ok(mut g) = status.write() {
|
||||
*g = new_status;
|
||||
}
|
||||
}
|
||||
|
||||
fn probe_endpoints(base_url: Option<&str>, ports: &[u16]) -> bool {
|
||||
if let Some(base) = base_url {
|
||||
let probe_url = format!("{}/coord/v1/probe", base.trim_end_matches('/'));
|
||||
let client = reqwest::blocking::Client::builder()
|
||||
.timeout(Duration::from_secs(4))
|
||||
.build()
|
||||
.ok();
|
||||
if let Some(c) = client {
|
||||
let probe_body = coa_control_proto::coord::ProbePayload {
|
||||
ports: ports.to_vec(),
|
||||
};
|
||||
let body_str = serde_json::to_string(&probe_body).unwrap_or_default();
|
||||
if let Ok(resp) = c
|
||||
.post(&probe_url)
|
||||
.header("Content-Type", "application/json")
|
||||
.body(body_str)
|
||||
.send()
|
||||
{
|
||||
if let Ok(text) = resp.text() {
|
||||
if let Ok(p_resp) =
|
||||
serde_json::from_str::<coa_control_proto::coord::ProbeResponse>(&text)
|
||||
{
|
||||
return p_resp.all_reachable;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
} else {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
std::env::var("COA_TEST_FORCE_DIRECT_VERIFIED").is_ok()
|
||||
}
|
||||
#[cfg(not(debug_assertions))]
|
||||
{
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run_route_manager(
|
||||
registry_base_url: Option<String>,
|
||||
service: Arc<HostService>,
|
||||
status: Arc<RwLock<RouteStatus>>,
|
||||
stop: Arc<AtomicBool>,
|
||||
) {
|
||||
while !stop.load(Ordering::Relaxed) {
|
||||
attempt_route_discovery(®istry_base_url, &service, &status, &stop);
|
||||
if stop.load(Ordering::Relaxed) {
|
||||
break;
|
||||
}
|
||||
for _ in 0..20 {
|
||||
if stop.load(Ordering::Relaxed) {
|
||||
break;
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(500));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn attempt_route_discovery(
|
||||
registry_base_url: &Option<String>,
|
||||
service: &Arc<HostService>,
|
||||
status: &Arc<RwLock<RouteStatus>>,
|
||||
stop: &Arc<AtomicBool>,
|
||||
) {
|
||||
// 1. Discover local LAN address
|
||||
let Some(lan_ip) = net::lan_ip() else {
|
||||
set_status(
|
||||
status,
|
||||
RouteStatus::RelayOnly {
|
||||
reason: "No usable local network address found.".into(),
|
||||
},
|
||||
);
|
||||
service.set_direct_route(None);
|
||||
return;
|
||||
};
|
||||
|
||||
// 2. Discover default gateway using OS routing table API
|
||||
let default_gw = net::default_gateway();
|
||||
|
||||
// 3. Discover UPnP Gateway and/or NAT-PMP Gateway
|
||||
let upnp_gw = upnp::discover();
|
||||
let mut discovered_public_ip: Option<Ipv4Addr> = None;
|
||||
let mut method = "direct_public";
|
||||
|
||||
if let Some(ref gw) = upnp_gw {
|
||||
if let Ok(ext) = upnp::external_ip(gw) {
|
||||
discovered_public_ip = Some(ext);
|
||||
method = "upnp";
|
||||
}
|
||||
}
|
||||
|
||||
if discovered_public_ip.is_none() {
|
||||
if let Some(gw) = default_gw {
|
||||
if let Some(ext) = nat_pmp::query_external_ip(gw) {
|
||||
discovered_public_ip = Some(ext);
|
||||
method = "nat_pmp";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if discovered_public_ip.is_none() && !net::is_private(lan_ip) && !net::is_cgnat_range(lan_ip) {
|
||||
discovered_public_ip = Some(lan_ip);
|
||||
method = "public_ip";
|
||||
}
|
||||
|
||||
let mut probe_ip: Option<Ipv4Addr> = None;
|
||||
if let Some(ref base) = registry_base_url {
|
||||
let probe_url = format!("{}/coord/v1/probe?ports=3724", base.trim_end_matches('/'));
|
||||
let client = reqwest::blocking::Client::builder()
|
||||
.timeout(Duration::from_secs(3))
|
||||
.build()
|
||||
.ok();
|
||||
if let Some(c) = client {
|
||||
if let Ok(resp) = c.get(&probe_url).send() {
|
||||
if let Ok(text) = resp.text() {
|
||||
if let Ok(p_resp) =
|
||||
serde_json::from_str::<coa_control_proto::coord::ProbeResponse>(&text)
|
||||
{
|
||||
probe_ip = p_resp.client_ip.parse::<Ipv4Addr>().ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let public_ip = discovered_public_ip.or(probe_ip);
|
||||
|
||||
let Some(public_ip) = public_ip else {
|
||||
set_status(
|
||||
status,
|
||||
RouteStatus::RelayOnly {
|
||||
reason: "Could not discover public IPv4 address.".into(),
|
||||
},
|
||||
);
|
||||
service.set_direct_route(None);
|
||||
return;
|
||||
};
|
||||
|
||||
// 4. CGNAT check
|
||||
if net::is_cgnat_range(public_ip) {
|
||||
set_status(
|
||||
status,
|
||||
RouteStatus::RelayOnly {
|
||||
reason: format!(
|
||||
"Carrier-grade NAT detected ({public_ip}); direct routing unavailable."
|
||||
),
|
||||
},
|
||||
);
|
||||
service.set_direct_route(None);
|
||||
return;
|
||||
};
|
||||
|
||||
// 5. Start local direct ingress proxy
|
||||
let local_auth = std::env::var("COA_OVERRIDE_LOCAL_AUTH_PORT")
|
||||
.ok()
|
||||
.and_then(|p| p.parse().ok())
|
||||
.unwrap_or(coa_control_proto::relay::LOCAL_AUTH_PORT);
|
||||
let local_world = std::env::var("COA_OVERRIDE_LOCAL_WORLD_PORT")
|
||||
.ok()
|
||||
.and_then(|p| p.parse().ok())
|
||||
.unwrap_or(coa_control_proto::relay::LOCAL_WORLD_PORT);
|
||||
|
||||
let desired_auth_port: u16 = std::env::var("COA_DIRECT_AUTH_PORT")
|
||||
.ok()
|
||||
.and_then(|p| p.parse().ok())
|
||||
.unwrap_or(3724);
|
||||
let desired_world_port: u16 = std::env::var("COA_DIRECT_WORLD_PORT")
|
||||
.ok()
|
||||
.and_then(|p| p.parse().ok())
|
||||
.unwrap_or(8085);
|
||||
|
||||
let ingress = match DirectIngress::start(
|
||||
desired_auth_port,
|
||||
desired_world_port,
|
||||
public_ip.to_string(),
|
||||
desired_world_port,
|
||||
local_auth,
|
||||
local_world,
|
||||
) {
|
||||
Ok(ing) => Ok(ing),
|
||||
Err(_) => DirectIngress::start(
|
||||
0,
|
||||
0,
|
||||
public_ip.to_string(),
|
||||
desired_world_port,
|
||||
local_auth,
|
||||
local_world,
|
||||
),
|
||||
};
|
||||
|
||||
let Ok(mut ingress) = ingress else {
|
||||
set_status(
|
||||
status,
|
||||
RouteStatus::RelayOnly {
|
||||
reason: "Cannot bind direct ingress listeners.".into(),
|
||||
},
|
||||
);
|
||||
service.set_direct_route(None);
|
||||
return;
|
||||
};
|
||||
|
||||
let bound_auth_port = ingress.auth_port();
|
||||
let bound_world_port = ingress.world_port();
|
||||
|
||||
// 6. Transactional Port Mapping (if behind NAT)
|
||||
let is_behind_nat = net::is_private(lan_ip);
|
||||
let mut mapped_auth_port = bound_auth_port;
|
||||
let mut mapped_world_port = bound_world_port;
|
||||
let mut active_mapping = ActiveMapping::None;
|
||||
|
||||
if is_behind_nat {
|
||||
// Try UPnP IGD transactionally
|
||||
if let Some(ref gw) = upnp_gw {
|
||||
if upnp::add_mapping(gw, bound_auth_port, lan_ip, "Auth Direct").is_ok() {
|
||||
if upnp::add_mapping(gw, bound_world_port, lan_ip, "World Direct").is_ok() {
|
||||
mapped_auth_port = bound_auth_port;
|
||||
mapped_world_port = bound_world_port;
|
||||
active_mapping = ActiveMapping::Upnp {
|
||||
gateway: gw.clone(),
|
||||
auth_port: bound_auth_port,
|
||||
world_port: bound_world_port,
|
||||
};
|
||||
method = "upnp";
|
||||
} else {
|
||||
// Transactional rollback: remove auth mapping immediately before fallback!
|
||||
let _ = upnp::remove_mapping(gw, bound_auth_port);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Try NAT-PMP transactionally if UPnP failed
|
||||
if matches!(active_mapping, ActiveMapping::None) {
|
||||
if let Some(gw) = default_gw {
|
||||
let lifetime = 3600;
|
||||
if let Some(a_ext) =
|
||||
nat_pmp::request_mapping(gw, bound_auth_port, bound_auth_port, lifetime)
|
||||
{
|
||||
if let Some(w_ext) =
|
||||
nat_pmp::request_mapping(gw, bound_world_port, bound_world_port, lifetime)
|
||||
{
|
||||
mapped_auth_port = a_ext;
|
||||
mapped_world_port = w_ext;
|
||||
let renew_at =
|
||||
std::time::Instant::now() + Duration::from_secs(lifetime as u64 / 2);
|
||||
active_mapping = ActiveMapping::NatPmp {
|
||||
gateway: gw,
|
||||
auth_internal: bound_auth_port,
|
||||
auth_external: a_ext,
|
||||
world_internal: bound_world_port,
|
||||
world_external: w_ext,
|
||||
lifetime_secs: lifetime,
|
||||
renew_at,
|
||||
};
|
||||
method = "nat_pmp";
|
||||
} else {
|
||||
// Transactional rollback: remove auth mapping immediately before fallback!
|
||||
let _ = nat_pmp::delete_mapping(gw, bound_auth_port);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if matches!(active_mapping, ActiveMapping::None) {
|
||||
set_status(
|
||||
status,
|
||||
RouteStatus::RelayOnly {
|
||||
reason: "Router port mapping (UPnP/NAT-PMP) failed or unsupported.".into(),
|
||||
},
|
||||
);
|
||||
service.set_direct_route(None);
|
||||
ingress.stop();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Update DirectIngress with the final verified mapped external world endpoint
|
||||
ingress.update_external_world_endpoint(&public_ip.to_string(), mapped_world_port);
|
||||
|
||||
// 7. External Reachability Verification Probe
|
||||
let verified = probe_endpoints(
|
||||
registry_base_url.as_deref(),
|
||||
&[mapped_auth_port, mapped_world_port],
|
||||
);
|
||||
|
||||
if !verified {
|
||||
set_status(status, RouteStatus::RelayOnly {
|
||||
reason: format!("External port reachability check failed on {public_ip}:{mapped_auth_port},{mapped_world_port}."),
|
||||
});
|
||||
service.set_direct_route(None);
|
||||
active_mapping.cleanup();
|
||||
ingress.stop();
|
||||
return;
|
||||
}
|
||||
|
||||
// 8. DIRECT_READY: update status and register direct route with HostService
|
||||
let direct_endpoint = format!("{public_ip}:{mapped_auth_port}");
|
||||
set_status(
|
||||
status,
|
||||
RouteStatus::DirectReady {
|
||||
public_ip: public_ip.to_string(),
|
||||
auth_port: mapped_auth_port,
|
||||
world_port: mapped_world_port,
|
||||
method: method.to_string(),
|
||||
},
|
||||
);
|
||||
service.set_direct_route(Some(direct_endpoint));
|
||||
|
||||
// 9. Route Health, Renewal, and Network Change Monitoring Loop
|
||||
let mut last_health_check = std::time::Instant::now();
|
||||
let last_lan_ip = lan_ip;
|
||||
|
||||
while !stop.load(Ordering::Relaxed) {
|
||||
std::thread::sleep(Duration::from_millis(500));
|
||||
|
||||
// Network change detection
|
||||
let current_lan_ip = net::lan_ip();
|
||||
if current_lan_ip != Some(last_lan_ip) {
|
||||
set_status(
|
||||
status,
|
||||
RouteStatus::RelayOnly {
|
||||
reason: "Local network interface address changed.".into(),
|
||||
},
|
||||
);
|
||||
service.set_direct_route(None);
|
||||
active_mapping.cleanup();
|
||||
ingress.stop();
|
||||
return;
|
||||
}
|
||||
|
||||
// NAT-PMP mapping renewal before expiry
|
||||
let mut need_probe = false;
|
||||
if let ActiveMapping::NatPmp {
|
||||
gateway,
|
||||
auth_internal,
|
||||
auth_external,
|
||||
world_internal,
|
||||
world_external,
|
||||
lifetime_secs,
|
||||
ref mut renew_at,
|
||||
} = active_mapping
|
||||
{
|
||||
if std::time::Instant::now() >= *renew_at {
|
||||
let ra =
|
||||
nat_pmp::request_mapping(gateway, auth_internal, auth_external, lifetime_secs);
|
||||
let rw = nat_pmp::request_mapping(
|
||||
gateway,
|
||||
world_internal,
|
||||
world_external,
|
||||
lifetime_secs,
|
||||
);
|
||||
if ra == Some(auth_external) && rw == Some(world_external) {
|
||||
*renew_at =
|
||||
std::time::Instant::now() + Duration::from_secs(lifetime_secs as u64 / 2);
|
||||
need_probe = true;
|
||||
} else {
|
||||
set_status(
|
||||
status,
|
||||
RouteStatus::RelayOnly {
|
||||
reason: "NAT-PMP port mapping renewal failed.".into(),
|
||||
},
|
||||
);
|
||||
service.set_direct_route(None);
|
||||
active_mapping.cleanup();
|
||||
ingress.stop();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Periodic external reachability check (every 30s) or immediately after renewal
|
||||
if need_probe || last_health_check.elapsed() >= Duration::from_secs(30) {
|
||||
last_health_check = std::time::Instant::now();
|
||||
if registry_base_url.is_some() {
|
||||
let still_reachable = probe_endpoints(
|
||||
registry_base_url.as_deref(),
|
||||
&[mapped_auth_port, mapped_world_port],
|
||||
);
|
||||
if !still_reachable {
|
||||
set_status(status, RouteStatus::RelayOnly {
|
||||
reason: format!("Periodic external reachability probe failed on {public_ip}:{mapped_auth_port},{mapped_world_port}."),
|
||||
});
|
||||
service.set_direct_route(None);
|
||||
active_mapping.cleanup();
|
||||
ingress.stop();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Graceful shutdown
|
||||
service.set_direct_route(None);
|
||||
active_mapping.cleanup();
|
||||
ingress.stop();
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_active_mapping_cleanup() {
|
||||
let mut mapping = ActiveMapping::NatPmp {
|
||||
gateway: Ipv4Addr::new(127, 0, 0, 1),
|
||||
auth_internal: 3724,
|
||||
auth_external: 43724,
|
||||
world_internal: 8085,
|
||||
world_external: 48085,
|
||||
lifetime_secs: 3600,
|
||||
renew_at: std::time::Instant::now() + Duration::from_secs(1800),
|
||||
};
|
||||
assert!(matches!(mapping, ActiveMapping::NatPmp { .. }));
|
||||
mapping.cleanup();
|
||||
assert!(matches!(mapping, ActiveMapping::None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_route_manager_health_invalidation() {
|
||||
let status = Arc::new(RwLock::new(RouteStatus::DirectReady {
|
||||
public_ip: "198.51.100.1".into(),
|
||||
auth_port: 3724,
|
||||
world_port: 8085,
|
||||
method: "nat_pmp".into(),
|
||||
}));
|
||||
set_status(
|
||||
&status,
|
||||
RouteStatus::RelayOnly {
|
||||
reason: "Renewal failed".into(),
|
||||
},
|
||||
);
|
||||
let cur = status.read().unwrap().clone();
|
||||
assert!(matches!(cur, RouteStatus::RelayOnly { .. }));
|
||||
}
|
||||
}
|
||||
@@ -1,360 +0,0 @@
|
||||
//! The Host Manager's side of the control plane: one outbound connection per published realm to the Coordinator, over which Player Managers reach the
|
||||
//! realm's [`HostService`] through an end-to-end channel the Coordinator cannot read.
|
||||
//!
|
||||
//! The Host connects out, so it needs no open port. A player's channel goes through these steps (see `docs/CONTROL_PROTOCOL.md`):
|
||||
//! `Open{conn}` -> Noise XX handshake (3 messages) -> the Host sends its proof (realm key over the handshake hash) -> the Player sends its proof (player key over the
|
||||
//! same hash) -> requests and responses. A channel that breaks a step, speaks too much or stays too long is closed.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread::JoinHandle;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use coa_control_proto::app::{self, AppError, Response};
|
||||
use coa_control_proto::coord::{self, Frame};
|
||||
use coa_control_proto::noise::{self, Channel, PlayerProof, Responder};
|
||||
use coa_registry_proto::RealmId;
|
||||
use ed25519_dalek::SigningKey;
|
||||
use serde::Serialize;
|
||||
use tungstenite::Message;
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::service::{HostService, Session};
|
||||
use super::transport::{self, LinkError};
|
||||
|
||||
const MAX_CHANNELS: usize = 16;
|
||||
const CHANNEL_LIFETIME: Duration = Duration::from_secs(300);
|
||||
const CHANNEL_IDLE: Duration = Duration::from_secs(45);
|
||||
const POLL: Duration = Duration::from_millis(500);
|
||||
|
||||
enum Stage {
|
||||
AwaitFirst,
|
||||
AwaitThird(Responder),
|
||||
AwaitPlayerProof(Channel),
|
||||
Ready {
|
||||
channel: Channel,
|
||||
player: Uuid,
|
||||
public_key: String,
|
||||
session: Session,
|
||||
},
|
||||
}
|
||||
|
||||
struct Conn {
|
||||
stage: Option<Stage>,
|
||||
opened: Instant,
|
||||
last: Instant,
|
||||
client_ip: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Serialize)]
|
||||
pub struct LinkStatus {
|
||||
pub connected: bool,
|
||||
pub last_error: Option<String>,
|
||||
pub channels: usize,
|
||||
pub requests: u64,
|
||||
}
|
||||
|
||||
pub struct HostLink {
|
||||
stop: Arc<AtomicBool>,
|
||||
status: Arc<Mutex<LinkStatus>>,
|
||||
join: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
pub type Clock = Arc<dyn Fn() -> i64 + Send + Sync>;
|
||||
|
||||
impl HostLink {
|
||||
/// `url` is the Coordinator's host endpoint, e.g. `wss://registry.example/coord/v1/host`.
|
||||
pub fn start(
|
||||
url: String,
|
||||
realm_id: RealmId,
|
||||
key: SigningKey,
|
||||
service: Arc<HostService>,
|
||||
clock: Clock,
|
||||
) -> HostLink {
|
||||
let stop = Arc::new(AtomicBool::new(false));
|
||||
let status = Arc::new(Mutex::new(LinkStatus::default()));
|
||||
let (s, st) = (stop.clone(), status.clone());
|
||||
let join = std::thread::Builder::new()
|
||||
.name("control-host".into())
|
||||
.spawn(move || run(&url, realm_id, &key, &service, &clock, &s, &st))
|
||||
.ok();
|
||||
HostLink { stop, status, join }
|
||||
}
|
||||
|
||||
pub fn status(&self) -> LinkStatus {
|
||||
self.status.lock().map(|s| s.clone()).unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn stop(&mut self) {
|
||||
self.stop.store(true, Ordering::SeqCst);
|
||||
if let Some(j) = self.join.take() {
|
||||
let _ = j.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for HostLink {
|
||||
fn drop(&mut self) {
|
||||
self.stop();
|
||||
}
|
||||
}
|
||||
|
||||
fn set(status: &Mutex<LinkStatus>, f: impl FnOnce(&mut LinkStatus)) {
|
||||
if let Ok(mut s) = status.lock() {
|
||||
f(&mut s);
|
||||
}
|
||||
}
|
||||
|
||||
fn run(
|
||||
url: &str,
|
||||
realm: RealmId,
|
||||
key: &SigningKey,
|
||||
service: &HostService,
|
||||
clock: &Clock,
|
||||
stop: &AtomicBool,
|
||||
status: &Mutex<LinkStatus>,
|
||||
) {
|
||||
let mut failures = 0u32;
|
||||
while !stop.load(Ordering::SeqCst) {
|
||||
let began = Instant::now();
|
||||
let result = session(url, realm, key, service, clock, stop, status);
|
||||
set(status, |s| {
|
||||
s.connected = false;
|
||||
s.channels = 0;
|
||||
if let Err(e) = &result {
|
||||
s.last_error = Some(e.to_string());
|
||||
}
|
||||
});
|
||||
if stop.load(Ordering::SeqCst) {
|
||||
return;
|
||||
}
|
||||
// a link that held for a while starts the backoff over
|
||||
failures = if began.elapsed() > Duration::from_secs(60) {
|
||||
1
|
||||
} else {
|
||||
failures.saturating_add(1)
|
||||
};
|
||||
let wait = super::super::realm_registry::host::backoff(
|
||||
failures,
|
||||
Duration::from_secs(2),
|
||||
Duration::from_secs(60),
|
||||
0.5,
|
||||
);
|
||||
let end = Instant::now() + wait;
|
||||
while Instant::now() < end && !stop.load(Ordering::SeqCst) {
|
||||
std::thread::sleep(Duration::from_millis(200));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn session(
|
||||
url: &str,
|
||||
realm: RealmId,
|
||||
key: &SigningKey,
|
||||
service: &HostService,
|
||||
clock: &Clock,
|
||||
stop: &AtomicBool,
|
||||
status: &Mutex<LinkStatus>,
|
||||
) -> Result<(), LinkError> {
|
||||
let mut ws = transport::connect(url, POLL)?;
|
||||
let nonce = match transport::next_frame(&mut ws, Duration::from_secs(15))? {
|
||||
Frame::Challenge { nonce, .. } => nonce,
|
||||
_ => return Err(LinkError::Protocol("a challenge was expected".into())),
|
||||
};
|
||||
transport::send_text(
|
||||
&mut ws,
|
||||
&coord::sign_host_hello(key, &realm, &nonce, clock()),
|
||||
)?;
|
||||
match transport::next_frame(&mut ws, Duration::from_secs(15))? {
|
||||
Frame::HostReady { .. } => {}
|
||||
_ => {
|
||||
return Err(LinkError::Protocol(
|
||||
"the Coordinator did not accept the hello".into(),
|
||||
))
|
||||
}
|
||||
}
|
||||
set(status, |s| {
|
||||
s.connected = true;
|
||||
s.last_error = None;
|
||||
});
|
||||
tracing::info!(%realm, "control link up");
|
||||
let mut conns: HashMap<u32, Conn> = HashMap::new();
|
||||
let mut last_ping = Instant::now();
|
||||
while !stop.load(Ordering::SeqCst) {
|
||||
match transport::poll(&mut ws)? {
|
||||
None => {}
|
||||
Some(Message::Text(t)) => {
|
||||
match coord::parse_text(t.as_str()).map_err(LinkError::from)? {
|
||||
Frame::Open { conn, client_ip } => {
|
||||
if conns.len() >= MAX_CHANNELS || conns.contains_key(&conn) {
|
||||
transport::send_text(
|
||||
&mut ws,
|
||||
&Frame::Close {
|
||||
conn,
|
||||
reason: Some("busy".into()),
|
||||
},
|
||||
)?;
|
||||
} else {
|
||||
conns.insert(
|
||||
conn,
|
||||
Conn {
|
||||
stage: Some(Stage::AwaitFirst),
|
||||
opened: Instant::now(),
|
||||
last: Instant::now(),
|
||||
client_ip,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Frame::Close { conn, .. } => {
|
||||
conns.remove(&conn);
|
||||
}
|
||||
Frame::Error { code, message } => {
|
||||
return Err(transport::from_error_frame(code, &message))
|
||||
}
|
||||
_ => return Err(LinkError::Protocol("an unexpected control frame".into())),
|
||||
}
|
||||
}
|
||||
Some(Message::Binary(data)) => {
|
||||
let (conn, frame) = coord::split_binary(&data).map_err(LinkError::from)?;
|
||||
let Some(c) = conns.get_mut(&conn) else {
|
||||
continue;
|
||||
};
|
||||
c.last = Instant::now();
|
||||
match step(c, frame, realm, key, service, status) {
|
||||
Ok(out) => {
|
||||
for f in out {
|
||||
transport::send_binary(&mut ws, coord::binary(conn, &f))?;
|
||||
}
|
||||
}
|
||||
Err(why) => {
|
||||
tracing::debug!(%realm, conn, reason = %why, "channel closed");
|
||||
conns.remove(&conn);
|
||||
transport::send_text(
|
||||
&mut ws,
|
||||
&Frame::Close {
|
||||
conn,
|
||||
reason: Some("closed".into()),
|
||||
},
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(Message::Close(_)) => {
|
||||
return Err(LinkError::Io(
|
||||
"the Coordinator closed the connection".into(),
|
||||
))
|
||||
}
|
||||
Some(_) => {}
|
||||
}
|
||||
let now = Instant::now();
|
||||
let expired: Vec<u32> = conns
|
||||
.iter()
|
||||
.filter(|(_, c)| {
|
||||
now.duration_since(c.opened) > CHANNEL_LIFETIME
|
||||
|| now.duration_since(c.last) > CHANNEL_IDLE
|
||||
})
|
||||
.map(|(k, _)| *k)
|
||||
.collect();
|
||||
for conn in expired {
|
||||
conns.remove(&conn);
|
||||
transport::send_text(
|
||||
&mut ws,
|
||||
&Frame::Close {
|
||||
conn,
|
||||
reason: Some("expired".into()),
|
||||
},
|
||||
)?;
|
||||
}
|
||||
if last_ping.elapsed() > Duration::from_secs(20) {
|
||||
ws.send(Message::Ping(Vec::new().into()))
|
||||
.map_err(|e| LinkError::Io(e.to_string()))?;
|
||||
last_ping = Instant::now();
|
||||
}
|
||||
set(status, |s| s.channels = conns.len());
|
||||
}
|
||||
let _ = ws.close(None);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn seal_all(channel: &mut Channel, message: &[u8]) -> Result<Vec<Vec<u8>>, String> {
|
||||
channel.seal(message).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Advance one channel by one frame; the frames to send back, or the reason to close it.
|
||||
fn step(
|
||||
c: &mut Conn,
|
||||
frame: &[u8],
|
||||
realm: RealmId,
|
||||
key: &SigningKey,
|
||||
service: &HostService,
|
||||
status: &Mutex<LinkStatus>,
|
||||
) -> Result<Vec<Vec<u8>>, String> {
|
||||
let stage = c.stage.take().ok_or("a channel in no state")?;
|
||||
match stage {
|
||||
Stage::AwaitFirst => {
|
||||
let (responder, msg2) = Responder::start(frame).map_err(|e| e.to_string())?;
|
||||
c.stage = Some(Stage::AwaitThird(responder));
|
||||
Ok(vec![msg2])
|
||||
}
|
||||
Stage::AwaitThird(responder) => {
|
||||
let mut channel = responder.finish(frame).map_err(|e| e.to_string())?;
|
||||
let proof = noise::host_proof(key, &realm, &channel);
|
||||
let frames = seal_all(
|
||||
&mut channel,
|
||||
&serde_json::to_vec(&proof).map_err(|e| e.to_string())?,
|
||||
)?;
|
||||
c.stage = Some(Stage::AwaitPlayerProof(channel));
|
||||
Ok(frames)
|
||||
}
|
||||
Stage::AwaitPlayerProof(mut channel) => {
|
||||
let Some(message) = channel.open_frame(frame).map_err(|e| e.to_string())? else {
|
||||
c.stage = Some(Stage::AwaitPlayerProof(channel));
|
||||
return Ok(vec![]);
|
||||
};
|
||||
let proof: PlayerProof = serde_json::from_slice(&message)
|
||||
.map_err(|_| "a player proof is not valid".to_string())?;
|
||||
noise::verify_player_proof(&proof, &realm, &channel).map_err(|e| e.to_string())?;
|
||||
if !service.key_matches(&proof.player_id, &proof.public_key) {
|
||||
return Err("the player id belongs to another key".into());
|
||||
}
|
||||
c.stage = Some(Stage::Ready {
|
||||
channel,
|
||||
player: proof.player_id,
|
||||
public_key: proof.public_key,
|
||||
session: Session::with_client_ip(c.client_ip.clone()),
|
||||
});
|
||||
Ok(vec![])
|
||||
}
|
||||
Stage::Ready {
|
||||
mut channel,
|
||||
player,
|
||||
public_key,
|
||||
mut session,
|
||||
} => {
|
||||
let message = channel.open_frame(frame).map_err(|e| e.to_string())?;
|
||||
let out = match message {
|
||||
None => vec![],
|
||||
Some(bytes) => {
|
||||
let response = match app::decode_request(&bytes) {
|
||||
Ok(request) => service.handle(&mut session, &player, &public_key, request),
|
||||
Err(_) => Response::Error {
|
||||
code: AppError::Invalid,
|
||||
message: "That request is not valid.".into(),
|
||||
},
|
||||
};
|
||||
set(status, |s| s.requests += 1);
|
||||
seal_all(&mut channel, &app::encode_response(&response))?
|
||||
}
|
||||
};
|
||||
c.stage = Some(Stage::Ready {
|
||||
channel,
|
||||
player,
|
||||
public_key,
|
||||
session,
|
||||
});
|
||||
Ok(out)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,201 +0,0 @@
|
||||
//! Keeps one [`HostLink`] per published realm: it follows the publishing settings (`registry.json`), so a realm that is published has its control link and a realm that is
|
||||
//! not does not. The link needs the realm's key, which exists once the realm has been published at least once.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread::JoinHandle;
|
||||
use std::time::Duration;
|
||||
|
||||
use coa_registry_proto::RealmId;
|
||||
use serde::Serialize;
|
||||
|
||||
use super::host_link::{Clock, HostLink, LinkStatus};
|
||||
use super::service::{HostService, RealmBackend};
|
||||
use super::store::ControlStore;
|
||||
use super::transport::coordinator_url;
|
||||
use crate::realm_registry::host::system_clock;
|
||||
use crate::realm_registry::keys::KeyStore;
|
||||
use crate::realm_registry::settings;
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct HostControlRealm {
|
||||
pub local_id: String,
|
||||
pub realm_id: String,
|
||||
pub link: LinkStatus,
|
||||
}
|
||||
|
||||
pub struct HostControl {
|
||||
stop: Arc<AtomicBool>,
|
||||
status: Arc<Mutex<BTreeMap<String, HostControlRealm>>>,
|
||||
join: Mutex<Option<JoinHandle<()>>>,
|
||||
}
|
||||
|
||||
struct Running {
|
||||
realm_id: RealmId,
|
||||
url: String,
|
||||
link: HostLink,
|
||||
#[allow(dead_code)]
|
||||
relay_link: Option<Arc<crate::control::relay_link::RelayLink>>,
|
||||
#[allow(dead_code)]
|
||||
direct_route: Option<crate::control::direct_route::DirectRouteManager>,
|
||||
}
|
||||
|
||||
impl HostControl {
|
||||
/// `url_override` replaces the Registry address from the settings (`COA_REGISTRY_URL` in tests and deployments).
|
||||
pub fn start(
|
||||
settings_dir: PathBuf,
|
||||
keys: Arc<dyn KeyStore>,
|
||||
store: Arc<Mutex<ControlStore>>,
|
||||
backend: Arc<dyn RealmBackend>,
|
||||
url_override: Option<String>,
|
||||
) -> HostControl {
|
||||
Self::start_with(
|
||||
settings_dir,
|
||||
keys,
|
||||
store,
|
||||
backend,
|
||||
url_override,
|
||||
system_clock(),
|
||||
Duration::from_secs(2),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn start_with(
|
||||
settings_dir: PathBuf,
|
||||
keys: Arc<dyn KeyStore>,
|
||||
store: Arc<Mutex<ControlStore>>,
|
||||
backend: Arc<dyn RealmBackend>,
|
||||
url_override: Option<String>,
|
||||
clock: Clock,
|
||||
every: Duration,
|
||||
) -> HostControl {
|
||||
let stop = Arc::new(AtomicBool::new(false));
|
||||
let status = Arc::new(Mutex::new(BTreeMap::new()));
|
||||
let (s, st) = (stop.clone(), status.clone());
|
||||
let join = std::thread::Builder::new()
|
||||
.name("control-host-manager".into())
|
||||
.spawn(move || {
|
||||
let mut running: BTreeMap<String, Running> = BTreeMap::new();
|
||||
while !s.load(Ordering::SeqCst) {
|
||||
let wanted = wanted(&settings_dir, keys.as_ref(), url_override.as_deref());
|
||||
running.retain(|id, r| {
|
||||
wanted
|
||||
.get(id)
|
||||
.is_some_and(|(realm, url, _, _)| *realm == r.realm_id && *url == r.url)
|
||||
});
|
||||
for (local_id, (realm_id, url, base, key)) in wanted {
|
||||
if running.contains_key(&local_id) {
|
||||
continue;
|
||||
}
|
||||
let service = Arc::new(HostService::new(
|
||||
&local_id,
|
||||
realm_id,
|
||||
store.clone(),
|
||||
backend.clone(),
|
||||
));
|
||||
let relay_link =
|
||||
crate::control::transport::relay_url(&base, "/relay/v1/host").map(
|
||||
|r_url| {
|
||||
let rl = crate::control::relay_link::RelayLink::start(
|
||||
r_url,
|
||||
realm_id,
|
||||
key.clone(),
|
||||
);
|
||||
service.set_relay(rl.clone());
|
||||
rl
|
||||
},
|
||||
);
|
||||
let direct_route =
|
||||
Some(crate::control::direct_route::DirectRouteManager::start(
|
||||
Some(base.clone()),
|
||||
service.clone(),
|
||||
));
|
||||
let link =
|
||||
HostLink::start(url.clone(), realm_id, key, service, clock.clone());
|
||||
running.insert(
|
||||
local_id,
|
||||
Running {
|
||||
realm_id,
|
||||
url,
|
||||
link,
|
||||
relay_link,
|
||||
direct_route,
|
||||
},
|
||||
);
|
||||
}
|
||||
if let Ok(mut st) = st.lock() {
|
||||
*st = running
|
||||
.iter()
|
||||
.map(|(id, r)| {
|
||||
(
|
||||
id.clone(),
|
||||
HostControlRealm {
|
||||
local_id: id.clone(),
|
||||
realm_id: r.realm_id.to_string(),
|
||||
link: r.link.status(),
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
let end = std::time::Instant::now() + every;
|
||||
while std::time::Instant::now() < end && !s.load(Ordering::SeqCst) {
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
}
|
||||
running.clear();
|
||||
})
|
||||
.ok();
|
||||
HostControl {
|
||||
stop,
|
||||
status,
|
||||
join: Mutex::new(join),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn status(&self) -> Vec<HostControlRealm> {
|
||||
self.status
|
||||
.lock()
|
||||
.map(|s| s.values().cloned().collect())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn shutdown(&self) {
|
||||
self.stop.store(true, Ordering::SeqCst);
|
||||
if let Some(j) = self.join.lock().ok().and_then(|mut g| g.take()) {
|
||||
let _ = j.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for HostControl {
|
||||
fn drop(&mut self) {
|
||||
self.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
type Wanted = BTreeMap<String, (RealmId, String, String, ed25519_dalek::SigningKey)>;
|
||||
|
||||
fn wanted(dir: &std::path::Path, keys: &dyn KeyStore, url_override: Option<&str>) -> Wanted {
|
||||
let mut out = Wanted::new();
|
||||
let Ok(s) = settings::load(dir) else {
|
||||
return out;
|
||||
};
|
||||
let base = url_override
|
||||
.map(str::to_string)
|
||||
.unwrap_or_else(|| s.effective_url().to_string());
|
||||
let Some(url) = coordinator_url(&base, "/coord/v1/host") else {
|
||||
return out;
|
||||
};
|
||||
for (local_id, cfg) in s.realms {
|
||||
let (true, Some(realm)) = (cfg.enabled, cfg.realm_id) else {
|
||||
continue;
|
||||
};
|
||||
if let Ok(Some(key)) = keys.load(&realm) {
|
||||
out.insert(local_id, (realm, url.clone(), base.clone(), key));
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
//! The player's identity: a random `PlayerId` and an Ed25519 key, created once and kept in the [`SecretStore`](super::secrets::SecretStore).
|
||||
//! The id is public (a Host records it); the key never leaves the machine. A key that cannot be read is an error, never silently replaced by a new identity,
|
||||
//! because the new identity would own none of the player's accounts and characters.
|
||||
|
||||
use base64::engine::general_purpose::URL_SAFE_NO_PAD as B64;
|
||||
use base64::Engine;
|
||||
use ed25519_dalek::SigningKey;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::secrets::SecretStore;
|
||||
use crate::{Error, Result};
|
||||
|
||||
const NAME: &str = "player-identity";
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Stored {
|
||||
player_id: Uuid,
|
||||
seed: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct PlayerIdentity {
|
||||
pub player_id: Uuid,
|
||||
pub key: SigningKey,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for PlayerIdentity {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "PlayerIdentity({})", self.player_id)
|
||||
}
|
||||
}
|
||||
|
||||
impl PlayerIdentity {
|
||||
/// The stored identity, or a new one when the player has none yet.
|
||||
pub fn load_or_create(secrets: &dyn SecretStore) -> Result<PlayerIdentity> {
|
||||
if let Some(bytes) = secrets.get(NAME)? {
|
||||
let stored: Stored = serde_json::from_slice(&bytes)
|
||||
.map_err(|_| Error::Invalid("the player identity is damaged".into()))?;
|
||||
let seed: [u8; 32] = B64
|
||||
.decode(&stored.seed)
|
||||
.ok()
|
||||
.and_then(|b| b.try_into().ok())
|
||||
.ok_or_else(|| Error::Invalid("the player identity is damaged".into()))?;
|
||||
return Ok(PlayerIdentity {
|
||||
player_id: stored.player_id,
|
||||
key: SigningKey::from_bytes(&seed),
|
||||
});
|
||||
}
|
||||
let mut seed = [0u8; 32];
|
||||
seed[..16].copy_from_slice(Uuid::new_v4().as_bytes());
|
||||
seed[16..].copy_from_slice(Uuid::new_v4().as_bytes());
|
||||
let id = PlayerIdentity {
|
||||
player_id: Uuid::now_v7(),
|
||||
key: SigningKey::from_bytes(&seed),
|
||||
};
|
||||
let stored = Stored {
|
||||
player_id: id.player_id,
|
||||
seed: B64.encode(seed),
|
||||
};
|
||||
secrets.put(NAME, &serde_json::to_vec(&stored)?)?;
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub fn public_key(&self) -> String {
|
||||
coa_control_proto::coord::encode_public_key(&self.key.verifying_key())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::control::secrets::MemoryStore;
|
||||
|
||||
#[test]
|
||||
fn the_identity_is_created_once_and_survives() {
|
||||
let secrets = MemoryStore::default();
|
||||
let a = PlayerIdentity::load_or_create(&secrets).unwrap();
|
||||
let b = PlayerIdentity::load_or_create(&secrets).unwrap();
|
||||
assert_eq!(
|
||||
(a.player_id, a.key.to_bytes()),
|
||||
(b.player_id, b.key.to_bytes())
|
||||
);
|
||||
assert_ne!(
|
||||
PlayerIdentity::load_or_create(&MemoryStore::default())
|
||||
.unwrap()
|
||||
.player_id,
|
||||
a.player_id
|
||||
);
|
||||
assert!(
|
||||
!format!("{a:?}").contains(&B64.encode(a.key.to_bytes())),
|
||||
"the key is not printed"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_damaged_identity_is_an_error_not_a_new_player() {
|
||||
let secrets = MemoryStore::default();
|
||||
secrets.put(NAME, b"garbage").unwrap();
|
||||
assert!(PlayerIdentity::load_or_create(&secrets).is_err());
|
||||
assert_eq!(
|
||||
secrets.get(NAME).unwrap().unwrap(),
|
||||
b"garbage",
|
||||
"nothing was overwritten"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,549 +0,0 @@
|
||||
//! The player's operations on a remote realm, over a verified [`PlayerChannel`]: get or reuse an account, link an existing one, list and claim characters.
|
||||
//! The player's realm credentials are kept in the protected store and nowhere else; the control database remembers only account names.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use base64::Engine;
|
||||
use coa_control_proto::app::{self, AppError, CharacterEntry, Request, Response};
|
||||
use coa_control_proto::coord;
|
||||
use coa_registry_proto::{RealmDetail, RealmId};
|
||||
use ed25519_dalek::VerifyingKey;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::identity::PlayerIdentity;
|
||||
use super::player::PlayerChannel;
|
||||
use super::secrets::SecretStore;
|
||||
use super::store::{AccountKind, ControlStore};
|
||||
use super::transport::{self, LinkError};
|
||||
use crate::{Error, Result};
|
||||
|
||||
/// Where and as whom to reach a realm.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RealmTarget {
|
||||
pub realm_id: RealmId,
|
||||
pub key: VerifyingKey,
|
||||
/// The Coordinator's player endpoint (no query).
|
||||
pub coordinator: String,
|
||||
}
|
||||
|
||||
impl RealmTarget {
|
||||
/// From the Registry's public record of the realm; the key published there is what the Host must prove it holds.
|
||||
pub fn from_detail(registry_url: &str, detail: &RealmDetail) -> Result<RealmTarget> {
|
||||
let key = coord::decode_public_key(&detail.public_key)
|
||||
.map_err(|e| Error::Invalid(e.to_string()))?;
|
||||
let coordinator =
|
||||
transport::coordinator_url(registry_url, "/coord/v1/player").ok_or_else(|| {
|
||||
Error::Invalid("the Registry address is not usable for a connection".into())
|
||||
})?;
|
||||
Ok(RealmTarget {
|
||||
realm_id: detail.realm_id,
|
||||
key,
|
||||
coordinator,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ControlFail {
|
||||
Link(LinkError),
|
||||
App { code: AppError, message: String },
|
||||
Local(String),
|
||||
}
|
||||
|
||||
impl ControlFail {
|
||||
pub fn code(&self) -> String {
|
||||
match self {
|
||||
ControlFail::Link(e) => e.code().to_string(),
|
||||
ControlFail::App { code, .. } => serde_json::to_value(code)
|
||||
.ok()
|
||||
.and_then(|v| v.as_str().map(str::to_string))
|
||||
.unwrap_or_else(|| "error".into()),
|
||||
ControlFail::Local(_) => "local".into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ControlFail {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
ControlFail::Link(e) => write!(f, "{e}"),
|
||||
ControlFail::App { message, .. } => write!(f, "{message}"),
|
||||
ControlFail::Local(m) => write!(f, "{m}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for ControlFail {}
|
||||
|
||||
impl From<LinkError> for ControlFail {
|
||||
fn from(e: LinkError) -> Self {
|
||||
ControlFail::Link(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Error> for ControlFail {
|
||||
fn from(e: Error) -> Self {
|
||||
ControlFail::Local(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
type R<T> = std::result::Result<T, ControlFail>;
|
||||
|
||||
/// What the player types into the game's login window. The password is shown or copied on request and never logged.
|
||||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Credentials {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Credentials {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Credentials({}, ****)", self.username)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
|
||||
pub struct AccountOutcome {
|
||||
pub username: String,
|
||||
pub created: bool,
|
||||
pub password_reset: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Claimed {
|
||||
pub character_id: Uuid,
|
||||
pub payload: Vec<u8>,
|
||||
pub sha256: [u8; 32],
|
||||
pub collections: BTreeMap<String, Vec<u32>>,
|
||||
}
|
||||
|
||||
pub struct PlayerControl {
|
||||
pub identity: PlayerIdentity,
|
||||
secrets: Arc<dyn SecretStore>,
|
||||
store: Mutex<ControlStore>,
|
||||
}
|
||||
|
||||
fn secret_name(realm: &RealmId) -> String {
|
||||
format!("realm-{}", realm.to_string().replace('-', ""))
|
||||
}
|
||||
|
||||
fn clock() -> i64 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_secs() as i64)
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
fn expect<T>(
|
||||
response: Response,
|
||||
ok: impl FnOnce(Response) -> std::result::Result<T, Response>,
|
||||
) -> R<T> {
|
||||
match ok(response) {
|
||||
Ok(v) => Ok(v),
|
||||
Err(Response::Error { code, message }) => Err(ControlFail::App { code, message }),
|
||||
Err(_) => Err(ControlFail::Link(LinkError::Protocol(
|
||||
"the answer does not match the question".into(),
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
impl PlayerControl {
|
||||
pub fn open(dir: &Path, secrets: Arc<dyn SecretStore>) -> Result<Self> {
|
||||
let identity = PlayerIdentity::load_or_create(secrets.as_ref())?;
|
||||
Ok(Self {
|
||||
identity,
|
||||
secrets,
|
||||
store: Mutex::new(ControlStore::open(dir)?),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn secret_store_kind(&self) -> &'static str {
|
||||
self.secrets.kind()
|
||||
}
|
||||
|
||||
fn store(&self) -> R<std::sync::MutexGuard<'_, ControlStore>> {
|
||||
self.store
|
||||
.lock()
|
||||
.map_err(|_| ControlFail::Local("the control database is busy".into()))
|
||||
}
|
||||
|
||||
/// The credentials the realm has confirmed (a password still waiting for the realm's confirmation is not one).
|
||||
pub fn credentials(&self, realm: &RealmId) -> Result<Option<Credentials>> {
|
||||
Ok(self
|
||||
.raw_credentials(realm)?
|
||||
.filter(|c| !c.username.is_empty()))
|
||||
}
|
||||
|
||||
fn raw_credentials(&self, realm: &RealmId) -> Result<Option<Credentials>> {
|
||||
let Some(bytes) = self.secrets.get(&secret_name(realm))? else {
|
||||
return Ok(None);
|
||||
};
|
||||
serde_json::from_slice(&bytes)
|
||||
.map(Some)
|
||||
.map_err(|_| Error::Invalid("the saved credentials for this server are damaged".into()))
|
||||
}
|
||||
|
||||
fn save_credentials(&self, realm: &RealmId, c: &Credentials) -> Result<()> {
|
||||
self.secrets
|
||||
.put(&secret_name(realm), &serde_json::to_vec(c)?)
|
||||
}
|
||||
|
||||
/// Forget the account on a realm (the saved password and the name). The account itself stays on the realm.
|
||||
pub fn forget(&self, realm: &RealmId) -> R<()> {
|
||||
self.secrets.delete(&secret_name(realm))?;
|
||||
self.store()?.player_realm_remove(&realm.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn known_account(&self, realm: &RealmId) -> R<Option<(String, AccountKind)>> {
|
||||
Ok(self
|
||||
.store()?
|
||||
.player_realm(&realm.to_string())?
|
||||
.map(|r| (r.username, r.kind)))
|
||||
}
|
||||
|
||||
/// Open a verified channel to the realm's Host. The realm's key is pinned the first time: the Registry says which key a realm id has, and a Registry (or anything between) that later
|
||||
/// names another key for the same id is refused here, before anything is sent.
|
||||
pub fn connect(&self, target: &RealmTarget) -> R<PlayerChannel> {
|
||||
let id = target.realm_id.to_string();
|
||||
let key = coord::encode_public_key(&target.key);
|
||||
match self.store()?.realm_pin(&id)? {
|
||||
Some(pinned) if pinned != key => {
|
||||
return Err(ControlFail::Link(LinkError::Auth(
|
||||
"this realm's key is not the one it had when you first joined it".into(),
|
||||
)))
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
let channel = PlayerChannel::connect(
|
||||
&target.coordinator,
|
||||
&target.realm_id,
|
||||
&target.key,
|
||||
&self.identity,
|
||||
&clock,
|
||||
)?;
|
||||
self.store()?.realm_pin_set(&id, &key)?;
|
||||
Ok(channel)
|
||||
}
|
||||
|
||||
pub fn welcome(&self, ch: &mut PlayerChannel) -> R<(bool, bool, Option<String>)> {
|
||||
let r = ch.request(&Request::Hello {
|
||||
protocol: coa_control_proto::CONTROL_PROTOCOL_VERSION,
|
||||
client: "coa-manager".into(),
|
||||
})?;
|
||||
expect(r, |r| match r {
|
||||
Response::Welcome {
|
||||
automatic,
|
||||
existing_only,
|
||||
route,
|
||||
..
|
||||
} => Ok((automatic, existing_only, route)),
|
||||
other => Err(other),
|
||||
})
|
||||
}
|
||||
|
||||
/// The account on this realm: reused when the player has one, created otherwise (on realms that create accounts). The generated password is saved *before* it is
|
||||
/// sent, so a crash between the Host creating the account and the player learning of it leaves a password that the next call sets again.
|
||||
pub fn ensure_account(
|
||||
&self,
|
||||
ch: &mut PlayerChannel,
|
||||
realm: &RealmId,
|
||||
preferred: Option<&str>,
|
||||
) -> R<AccountOutcome> {
|
||||
let saved = self.raw_credentials(realm)?;
|
||||
// saved credentials with no account name are a password that was made but never confirmed by the realm: it is sent again, as a new one
|
||||
let have = saved.as_ref().is_some_and(|c| !c.username.is_empty());
|
||||
let password = saved
|
||||
.as_ref()
|
||||
.map(|c| c.password.clone())
|
||||
.unwrap_or_else(app::generate_password);
|
||||
if saved.is_none() {
|
||||
self.save_credentials(
|
||||
realm,
|
||||
&Credentials {
|
||||
username: String::new(),
|
||||
password: password.clone(),
|
||||
},
|
||||
)?;
|
||||
}
|
||||
let r = ch.request(&Request::Provision {
|
||||
desired: preferred.map(str::to_string),
|
||||
password: password.clone(),
|
||||
have_credentials: have,
|
||||
})?;
|
||||
let (username, created, reset) = expect(r, |r| match r {
|
||||
Response::Provisioned {
|
||||
username,
|
||||
created,
|
||||
reset,
|
||||
} => Ok((username, created, reset)),
|
||||
other => Err(other),
|
||||
})
|
||||
.inspect_err(|e| {
|
||||
// a refusal means the pending password will never be used; a lost connection keeps it for the next try
|
||||
if !have && matches!(e, ControlFail::App { .. }) {
|
||||
let _ = self.secrets.delete(&secret_name(realm));
|
||||
}
|
||||
})?;
|
||||
self.save_credentials(
|
||||
realm,
|
||||
&Credentials {
|
||||
username: username.clone(),
|
||||
password,
|
||||
},
|
||||
)?;
|
||||
// an account the player linked stays a linked one when the realm confirms it again
|
||||
let kind = match self.store()?.player_realm(&realm.to_string())? {
|
||||
Some(known) if known.username == username => known.kind,
|
||||
_ => AccountKind::Generated,
|
||||
};
|
||||
self.store()?
|
||||
.player_realm_set(&realm.to_string(), &username, kind)?;
|
||||
Ok(AccountOutcome {
|
||||
username,
|
||||
created,
|
||||
password_reset: reset,
|
||||
})
|
||||
}
|
||||
|
||||
/// One-time link of an account the realm already has.
|
||||
pub fn link_existing(
|
||||
&self,
|
||||
ch: &mut PlayerChannel,
|
||||
realm: &RealmId,
|
||||
login: &str,
|
||||
password: &str,
|
||||
) -> R<String> {
|
||||
let r = ch.request(&Request::Link {
|
||||
login: login.to_string(),
|
||||
password: password.to_string(),
|
||||
})?;
|
||||
let username = expect(r, |r| match r {
|
||||
Response::Linked { username } => Ok(username),
|
||||
other => Err(other),
|
||||
})?;
|
||||
self.save_credentials(
|
||||
realm,
|
||||
&Credentials {
|
||||
username: username.clone(),
|
||||
password: password.to_string(),
|
||||
},
|
||||
)?;
|
||||
self.store()?
|
||||
.player_realm_set(&realm.to_string(), &username, AccountKind::Linked)?;
|
||||
Ok(username)
|
||||
}
|
||||
|
||||
pub fn list_characters(&self, ch: &mut PlayerChannel) -> R<Vec<CharacterEntry>> {
|
||||
let r = ch.request(&Request::ListCharacters)?;
|
||||
expect(r, |r| match r {
|
||||
Response::Characters { characters } => Ok(characters),
|
||||
other => Err(other),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn claim(&self, ch: &mut PlayerChannel, token: u32) -> R<Claimed> {
|
||||
let r = ch.request(&Request::Claim { token })?;
|
||||
let (character_id, sha256, payload, collections) = expect(r, |r| match r {
|
||||
Response::Claimed {
|
||||
character_id,
|
||||
sha256,
|
||||
payload,
|
||||
collections,
|
||||
} => Ok((character_id, sha256, payload, collections)),
|
||||
other => Err(other),
|
||||
})?;
|
||||
let payload = base64::engine::general_purpose::STANDARD
|
||||
.decode(payload)
|
||||
.map_err(|_| {
|
||||
ControlFail::Link(LinkError::Protocol(
|
||||
"the character data is not valid".into(),
|
||||
))
|
||||
})?;
|
||||
let hash: [u8; 32] = hex::decode(&sha256)
|
||||
.ok()
|
||||
.and_then(|b| b.try_into().ok())
|
||||
.ok_or_else(|| {
|
||||
ControlFail::Link(LinkError::Protocol(
|
||||
"the character hash is not valid".into(),
|
||||
))
|
||||
})?;
|
||||
Ok(Claimed {
|
||||
character_id,
|
||||
payload,
|
||||
sha256: hash,
|
||||
collections,
|
||||
})
|
||||
}
|
||||
|
||||
/// Tell the Host the character is safely stored here; only then is the claim final.
|
||||
pub fn acknowledge(
|
||||
&self,
|
||||
ch: &mut PlayerChannel,
|
||||
character_id: Uuid,
|
||||
realm: &RealmId,
|
||||
name: &str,
|
||||
) -> R<()> {
|
||||
self.store()?
|
||||
.player_claim_add(&realm.to_string(), &character_id, name)?;
|
||||
let r = ch.request(&Request::ClaimAck { character_id })?;
|
||||
expect(r, |r| match r {
|
||||
Response::Done => Ok(()),
|
||||
other => Err(other),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn route(&self, ch: &mut PlayerChannel) -> R<Option<String>> {
|
||||
let r = ch.request(&Request::Route)?;
|
||||
expect(r, |r| match r {
|
||||
Response::RouteInfo { address } => Ok(address),
|
||||
other => Err(other),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn claimed_here(&self, realm: &RealmId) -> R<Vec<(Uuid, String)>> {
|
||||
Ok(self.store()?.player_claims(&realm.to_string())?)
|
||||
}
|
||||
|
||||
/// Transfer a portable character to a remote realm over the verified control channel (Phase 12.1).
|
||||
pub fn transfer_character(
|
||||
&self,
|
||||
ch: &mut PlayerChannel,
|
||||
character_id: Uuid,
|
||||
canonical_revision: u64,
|
||||
payload: &[u8],
|
||||
content_hash: [u8; 32],
|
||||
collections: BTreeMap<String, Vec<u32>>,
|
||||
) -> R<RemoteTransferOutcome> {
|
||||
let transfer_id = Uuid::now_v7();
|
||||
let content_hash_str = hex::encode(content_hash);
|
||||
let offer = Request::TransferOffer {
|
||||
transfer_id,
|
||||
character_id,
|
||||
canonical_revision,
|
||||
content_hash: content_hash_str.clone(),
|
||||
total_size: payload.len(),
|
||||
collections,
|
||||
};
|
||||
let r = ch.request(&offer)?;
|
||||
let mut offset = match r {
|
||||
Response::TransferReady {
|
||||
received_offset, ..
|
||||
} => received_offset,
|
||||
Response::TransferCommitted {
|
||||
local_guid,
|
||||
session_id,
|
||||
projected_level,
|
||||
notes,
|
||||
..
|
||||
} => {
|
||||
let _ = ch.request(&Request::TransferAck {
|
||||
transfer_id,
|
||||
character_id,
|
||||
});
|
||||
return Ok(RemoteTransferOutcome {
|
||||
local_guid,
|
||||
session_id,
|
||||
projected_level,
|
||||
notes,
|
||||
});
|
||||
}
|
||||
Response::Error { code, message } => return Err(ControlFail::App { code, message }),
|
||||
_ => {
|
||||
return Err(ControlFail::Link(LinkError::Protocol(
|
||||
"unexpected response to transfer offer".into(),
|
||||
)))
|
||||
}
|
||||
};
|
||||
|
||||
const CHUNK_SIZE: usize = 128 * 1024;
|
||||
while offset < payload.len() {
|
||||
let end = (offset + CHUNK_SIZE).min(payload.len());
|
||||
let chunk_data =
|
||||
base64::engine::general_purpose::STANDARD.encode(&payload[offset..end]);
|
||||
let chunk_req = Request::TransferChunk {
|
||||
transfer_id,
|
||||
offset,
|
||||
data: chunk_data,
|
||||
};
|
||||
let cr = ch.request(&chunk_req)?;
|
||||
match cr {
|
||||
Response::TransferChunkAck {
|
||||
received_offset, ..
|
||||
} => {
|
||||
offset = received_offset;
|
||||
}
|
||||
Response::Error { code, message } => {
|
||||
return Err(ControlFail::App { code, message })
|
||||
}
|
||||
_ => {
|
||||
return Err(ControlFail::Link(LinkError::Protocol(
|
||||
"unexpected response to transfer chunk".into(),
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let commit_req = Request::TransferCommit { transfer_id };
|
||||
let commit_resp = ch.request(&commit_req)?;
|
||||
let outcome = expect(commit_resp, |r| match r {
|
||||
Response::TransferCommitted {
|
||||
local_guid,
|
||||
session_id,
|
||||
projected_level,
|
||||
notes,
|
||||
..
|
||||
} => Ok(RemoteTransferOutcome {
|
||||
local_guid,
|
||||
session_id,
|
||||
projected_level,
|
||||
notes,
|
||||
}),
|
||||
other => Err(other),
|
||||
})?;
|
||||
|
||||
let _ = ch.request(&Request::TransferAck {
|
||||
transfer_id,
|
||||
character_id,
|
||||
});
|
||||
Ok(outcome)
|
||||
}
|
||||
|
||||
/// Request a short-lived Game Relay allocation for a relayed JOIN (Phase 13).
|
||||
pub fn allocate_relay(
|
||||
&self,
|
||||
ch: &mut PlayerChannel,
|
||||
) -> R<crate::control::relay_link::RelayAllocationInfo> {
|
||||
let req = Request::AllocateRelay {
|
||||
player_id: self.identity.player_id,
|
||||
};
|
||||
let resp = ch.request(&req)?;
|
||||
expect(resp, |r| match r {
|
||||
Response::RelayAllocated {
|
||||
relay_host,
|
||||
auth_port,
|
||||
world_port,
|
||||
token,
|
||||
expires_at,
|
||||
} => Ok(crate::control::relay_link::RelayAllocationInfo {
|
||||
relay_host,
|
||||
auth_port,
|
||||
world_port,
|
||||
token,
|
||||
expires_at,
|
||||
}),
|
||||
other => Err(other),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct RemoteTransferOutcome {
|
||||
pub local_guid: u32,
|
||||
pub session_id: Uuid,
|
||||
pub projected_level: Option<u32>,
|
||||
pub notes: Vec<String>,
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
//! The control plane between a Player Manager and a Host Manager (Phase 12). Account provisioning, character claiming and the route are decided here; nothing
|
||||
//! here carries game traffic. See `docs/CONTROL_PROTOCOL.md`.
|
||||
|
||||
pub mod backend;
|
||||
pub mod direct_ingress;
|
||||
pub mod direct_route;
|
||||
pub mod host_link;
|
||||
pub mod host_manager;
|
||||
pub mod identity;
|
||||
pub mod join;
|
||||
pub mod nat_pmp;
|
||||
pub mod player;
|
||||
pub mod relay_link;
|
||||
pub mod secrets;
|
||||
pub mod service;
|
||||
pub mod store;
|
||||
pub mod transport;
|
||||
@@ -1,125 +0,0 @@
|
||||
//! NAT-PMP (RFC 6886) client for automatic port mapping on routers supporting NAT-PMP.
|
||||
//! Discovery and port mapping change only login and world ports, and only when the router answers.
|
||||
|
||||
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket};
|
||||
use std::time::Duration;
|
||||
|
||||
pub const NAT_PMP_PORT: u16 = 5351;
|
||||
|
||||
/// Request the external (public) IPv4 address from the NAT-PMP gateway.
|
||||
pub fn query_external_ip(gateway: Ipv4Addr) -> Option<Ipv4Addr> {
|
||||
let sock = UdpSocket::bind("0.0.0.0:0").ok()?;
|
||||
sock.set_read_timeout(Some(Duration::from_millis(800)))
|
||||
.ok()?;
|
||||
let target = SocketAddr::V4(SocketAddrV4::new(gateway, NAT_PMP_PORT));
|
||||
|
||||
// NAT-PMP External Address Request: Version 0 (1 byte), Opcode 0 (1 byte)
|
||||
let req = [0u8, 0u8];
|
||||
sock.send_to(&req, target).ok()?;
|
||||
|
||||
let mut buf = [0u8; 16];
|
||||
let (n, from) = sock.recv_from(&mut buf).ok()?;
|
||||
if from != target || n < 12 {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Response format:
|
||||
// Byte 0: Version (0)
|
||||
// Byte 1: Opcode (128 = 0x80)
|
||||
// Bytes 2-3: Result Code (0 = Success)
|
||||
// Bytes 4-7: Seconds since start of epoch
|
||||
// Bytes 8-11: External IPv4 address
|
||||
if buf[0] == 0 && buf[1] == 128 && buf[2] == 0 && buf[3] == 0 {
|
||||
Some(Ipv4Addr::new(buf[8], buf[9], buf[10], buf[11]))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Request a TCP port mapping from the NAT-PMP gateway.
|
||||
/// If `lifetime_secs` is 0, this requests deletion of the mapping.
|
||||
pub fn request_mapping(
|
||||
gateway: Ipv4Addr,
|
||||
internal_port: u16,
|
||||
external_port: u16,
|
||||
lifetime_secs: u32,
|
||||
) -> Option<u16> {
|
||||
let sock = UdpSocket::bind("0.0.0.0:0").ok()?;
|
||||
sock.set_read_timeout(Some(Duration::from_millis(1000)))
|
||||
.ok()?;
|
||||
let target = SocketAddr::V4(SocketAddrV4::new(gateway, NAT_PMP_PORT));
|
||||
|
||||
// NAT-PMP TCP Mapping Request:
|
||||
// Byte 0: Version (0)
|
||||
// Byte 1: Opcode (2 = TCP)
|
||||
// Bytes 2-3: Reserved (0)
|
||||
// Bytes 4-5: Internal Port (big-endian)
|
||||
// Bytes 6-7: Requested External Port (big-endian)
|
||||
// Bytes 8-11: Lifetime in seconds (big-endian)
|
||||
let mut req = [0u8; 12];
|
||||
req[0] = 0;
|
||||
req[1] = 2; // TCP mapping
|
||||
req[4..6].copy_from_slice(&internal_port.to_be_bytes());
|
||||
req[6..8].copy_from_slice(&external_port.to_be_bytes());
|
||||
req[8..12].copy_from_slice(&lifetime_secs.to_be_bytes());
|
||||
|
||||
sock.send_to(&req, target).ok()?;
|
||||
|
||||
let mut buf = [0u8; 16];
|
||||
let (n, from) = sock.recv_from(&mut buf).ok()?;
|
||||
if from != target || n < 16 {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Response format:
|
||||
// Byte 0: Version (0)
|
||||
// Byte 1: Opcode (130 = 0x82)
|
||||
// Bytes 2-3: Result Code (0 = Success)
|
||||
// Bytes 4-7: Epoch seconds
|
||||
// Bytes 8-9: Internal Port
|
||||
// Bytes 10-11: Assigned External Port
|
||||
// Bytes 12-15: Port Mapping Lifetime
|
||||
if buf[0] == 0 && buf[1] == 130 && buf[2] == 0 && buf[3] == 0 {
|
||||
let assigned = u16::from_be_bytes([buf[10], buf[11]]);
|
||||
Some(assigned)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete a TCP port mapping from the NAT-PMP gateway.
|
||||
pub fn delete_mapping(gateway: Ipv4Addr, internal_port: u16) -> bool {
|
||||
request_mapping(gateway, internal_port, 0, 0).is_some()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_nat_pmp_packet_formats() {
|
||||
let req_ip = [0u8, 0u8];
|
||||
assert_eq!(req_ip.len(), 2);
|
||||
|
||||
let mut req_map = [0u8; 12];
|
||||
req_map[0] = 0;
|
||||
req_map[1] = 2;
|
||||
req_map[4..6].copy_from_slice(&3724u16.to_be_bytes());
|
||||
req_map[6..8].copy_from_slice(&3724u16.to_be_bytes());
|
||||
req_map[8..12].copy_from_slice(&3600u32.to_be_bytes());
|
||||
assert_eq!(req_map[1], 2);
|
||||
assert_eq!(u16::from_be_bytes([req_map[4], req_map[5]]), 3724);
|
||||
assert_eq!(
|
||||
u32::from_be_bytes([req_map[8], req_map[9], req_map[10], req_map[11]]),
|
||||
3600
|
||||
);
|
||||
|
||||
// Test timeout on unanswering gateway
|
||||
assert_eq!(query_external_ip(Ipv4Addr::new(127, 0, 0, 1)), None);
|
||||
assert_eq!(
|
||||
request_mapping(Ipv4Addr::new(127, 0, 0, 1), 3724, 3724, 3600),
|
||||
None
|
||||
);
|
||||
assert!(!delete_mapping(Ipv4Addr::new(127, 0, 0, 1), 3724));
|
||||
}
|
||||
}
|
||||
@@ -1,133 +0,0 @@
|
||||
//! The Player Manager's side: connect to a realm through the Coordinator, open the end-to-end channel, check that the other end is the realm's Host (the
|
||||
//! realm key the Registry publishes), prove the player's own key and exchange requests. One [`PlayerChannel`] is one short conversation.
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use coa_control_proto::app::{self, Request, Response};
|
||||
use coa_control_proto::coord::{self, Frame};
|
||||
use coa_control_proto::noise::{self, Channel, HostProof, Initiator};
|
||||
use coa_registry_proto::RealmId;
|
||||
use ed25519_dalek::VerifyingKey;
|
||||
|
||||
use super::identity::PlayerIdentity;
|
||||
use super::transport::{self, LinkError, Ws};
|
||||
|
||||
pub struct PlayerChannel {
|
||||
ws: Ws,
|
||||
channel: Channel,
|
||||
conn: u32,
|
||||
}
|
||||
|
||||
const STEP: Duration = Duration::from_secs(20);
|
||||
const ANSWER: Duration = Duration::from_secs(60);
|
||||
|
||||
impl PlayerChannel {
|
||||
/// `coordinator` is the Coordinator's player endpoint without the query, e.g. `wss://registry.example/coord/v1/player`.
|
||||
pub fn connect(
|
||||
coordinator: &str,
|
||||
realm: &RealmId,
|
||||
realm_key: &VerifyingKey,
|
||||
identity: &PlayerIdentity,
|
||||
clock: &dyn Fn() -> i64,
|
||||
) -> Result<PlayerChannel, LinkError> {
|
||||
let mut ws = transport::connect(
|
||||
&format!("{coordinator}?realm={realm}"),
|
||||
Duration::from_millis(500),
|
||||
)?;
|
||||
let nonce = match transport::next_frame(&mut ws, STEP)? {
|
||||
Frame::Challenge { nonce, .. } => nonce,
|
||||
_ => return Err(LinkError::Protocol("a challenge was expected".into())),
|
||||
};
|
||||
transport::send_text(
|
||||
&mut ws,
|
||||
&coord::sign_player_hello(&identity.key, &identity.player_id, realm, &nonce, clock()),
|
||||
)?;
|
||||
let conn = match transport::next_frame(&mut ws, STEP)? {
|
||||
Frame::PlayerReady { conn, .. } => conn,
|
||||
_ => {
|
||||
return Err(LinkError::Protocol(
|
||||
"the Coordinator did not accept the hello".into(),
|
||||
))
|
||||
}
|
||||
};
|
||||
let (initiator, msg1) = Initiator::start().map_err(LinkError::from)?;
|
||||
transport::send_binary(&mut ws, msg1)?;
|
||||
let msg2 = next_binary(&mut ws, STEP)?;
|
||||
let (msg3, mut channel) = initiator.finish(&msg2).map_err(LinkError::from)?;
|
||||
transport::send_binary(&mut ws, msg3)?;
|
||||
// the Host proves it holds the realm's key on this very channel before anything private is said
|
||||
let proof_bytes = next_message(&mut ws, &mut channel, STEP)?;
|
||||
let proof: HostProof = serde_json::from_slice(&proof_bytes)
|
||||
.map_err(|_| LinkError::Auth("the host's proof is not valid".into()))?;
|
||||
noise::verify_host_proof(&proof, realm_key, realm, &channel).map_err(LinkError::from)?;
|
||||
let mine = noise::player_proof(&identity.key, realm, &identity.player_id, &channel);
|
||||
for f in channel
|
||||
.seal(&serde_json::to_vec(&mine).map_err(|e| LinkError::Protocol(e.to_string()))?)
|
||||
.map_err(LinkError::from)?
|
||||
{
|
||||
transport::send_binary(&mut ws, f)?;
|
||||
}
|
||||
Ok(PlayerChannel { ws, channel, conn })
|
||||
}
|
||||
|
||||
pub fn connection(&self) -> u32 {
|
||||
self.conn
|
||||
}
|
||||
|
||||
pub fn request(&mut self, request: &Request) -> Result<Response, LinkError> {
|
||||
for f in self
|
||||
.channel
|
||||
.seal(&app::encode_request(request))
|
||||
.map_err(LinkError::from)?
|
||||
{
|
||||
transport::send_binary(&mut self.ws, f)?;
|
||||
}
|
||||
let bytes = next_message(&mut self.ws, &mut self.channel, ANSWER)?;
|
||||
app::decode_response(&bytes).map_err(LinkError::from)
|
||||
}
|
||||
|
||||
pub fn close(mut self) {
|
||||
let _ = self.ws.close(None);
|
||||
}
|
||||
}
|
||||
|
||||
fn next_binary(ws: &mut Ws, total: Duration) -> Result<Vec<u8>, LinkError> {
|
||||
let end = std::time::Instant::now() + total;
|
||||
loop {
|
||||
if std::time::Instant::now() >= end {
|
||||
return Err(LinkError::Timeout);
|
||||
}
|
||||
match transport::poll(ws)? {
|
||||
None => continue,
|
||||
Some(tungstenite::Message::Binary(b)) => return Ok(b.to_vec()),
|
||||
Some(tungstenite::Message::Text(t)) => {
|
||||
// an error frame from the Coordinator (the Host went away, a limit was hit)
|
||||
return match coord::parse_text(t.as_str()).map_err(LinkError::from)? {
|
||||
Frame::Error { code, message } => {
|
||||
Err(transport::from_error_frame(code, &message))
|
||||
}
|
||||
Frame::Close { .. } => Err(LinkError::HostOffline),
|
||||
_ => Err(LinkError::Protocol("an unexpected control frame".into())),
|
||||
};
|
||||
}
|
||||
Some(tungstenite::Message::Close(_)) => {
|
||||
return Err(LinkError::Io("the connection was closed".into()))
|
||||
}
|
||||
Some(_) => continue,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The next complete encrypted message (several frames when it is long).
|
||||
fn next_message(ws: &mut Ws, channel: &mut Channel, total: Duration) -> Result<Vec<u8>, LinkError> {
|
||||
let end = std::time::Instant::now() + total;
|
||||
loop {
|
||||
let left = end
|
||||
.checked_duration_since(std::time::Instant::now())
|
||||
.ok_or(LinkError::Timeout)?;
|
||||
let frame = next_binary(ws, left)?;
|
||||
if let Some(m) = channel.open_frame(&frame).map_err(LinkError::from)? {
|
||||
return Ok(m);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,435 +0,0 @@
|
||||
//! Outbound Host <-> Game Relay tunnel link (Phase 13).
|
||||
//! Maintains outbound connectivity from Host Manager to the Game Relay,
|
||||
//! opening local TCP connections strictly to 127.0.0.1:3724 (Auth) and 127.0.0.1:8085 (World).
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::io::{Read, Write};
|
||||
use std::net::TcpStream;
|
||||
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||||
use std::sync::mpsc::{channel, Receiver, Sender};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread::JoinHandle;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use base64::Engine;
|
||||
use coa_control_proto::relay::*;
|
||||
use coa_registry_proto::RealmId;
|
||||
use ed25519_dalek::SigningKey;
|
||||
use serde::Serialize;
|
||||
use tungstenite::Message;
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::service::RelayProvider;
|
||||
use super::transport::{self, LinkError};
|
||||
|
||||
const POLL: Duration = Duration::from_millis(2);
|
||||
|
||||
#[derive(Clone, Debug, Default, Serialize)]
|
||||
pub struct RelayStatus {
|
||||
pub connected: bool,
|
||||
pub last_error: Option<String>,
|
||||
pub active_streams: usize,
|
||||
}
|
||||
|
||||
pub struct RelayLink {
|
||||
stop: Arc<AtomicBool>,
|
||||
status: Arc<Mutex<RelayStatus>>,
|
||||
alloc_tx: Sender<(
|
||||
Uuid,
|
||||
Option<String>,
|
||||
Sender<Result<RelayAllocationInfo, LinkError>>,
|
||||
)>,
|
||||
join: Mutex<Option<JoinHandle<()>>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, serde::Deserialize)]
|
||||
pub struct RelayAllocationInfo {
|
||||
pub relay_host: String,
|
||||
pub auth_port: u16,
|
||||
pub world_port: u16,
|
||||
pub token: String,
|
||||
pub expires_at: i64,
|
||||
}
|
||||
|
||||
impl RelayProvider for RelayLink {
|
||||
fn allocate(
|
||||
&self,
|
||||
player_id: &Uuid,
|
||||
client_ip: Option<&str>,
|
||||
) -> crate::error::Result<RelayAllocationInfo> {
|
||||
let (tx, rx) = channel();
|
||||
self.alloc_tx
|
||||
.send((*player_id, client_ip.map(|s| s.to_string()), tx))
|
||||
.map_err(|_| {
|
||||
crate::error::Error::Invalid("relay link worker thread not running".to_string())
|
||||
})?;
|
||||
rx.recv_timeout(Duration::from_secs(10))
|
||||
.map_err(|_| crate::error::Error::Invalid("relay allocation timed out".to_string()))?
|
||||
.map_err(|e| crate::error::Error::Invalid(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
impl RelayLink {
|
||||
pub fn start(url: String, realm_id: RealmId, key: SigningKey) -> Arc<Self> {
|
||||
let stop = Arc::new(AtomicBool::new(false));
|
||||
let status = Arc::new(Mutex::new(RelayStatus::default()));
|
||||
let (alloc_tx, alloc_rx) = channel();
|
||||
|
||||
let s = stop.clone();
|
||||
let st = status.clone();
|
||||
let join = std::thread::Builder::new()
|
||||
.name("control-relay-host".into())
|
||||
.spawn(move || run(&url, realm_id, &key, alloc_rx, &s, &st))
|
||||
.ok();
|
||||
|
||||
Arc::new(RelayLink {
|
||||
stop,
|
||||
status,
|
||||
alloc_tx,
|
||||
join: Mutex::new(join),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn status(&self) -> RelayStatus {
|
||||
self.status.lock().unwrap().clone()
|
||||
}
|
||||
|
||||
pub fn stop(&self) {
|
||||
self.stop.store(true, Ordering::SeqCst);
|
||||
if let Ok(mut lock) = self.join.lock() {
|
||||
if let Some(j) = lock.take() {
|
||||
let _ = j.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for RelayLink {
|
||||
fn drop(&mut self) {
|
||||
self.stop();
|
||||
}
|
||||
}
|
||||
|
||||
fn set_status(status: &Mutex<RelayStatus>, f: impl FnOnce(&mut RelayStatus)) {
|
||||
if let Ok(mut s) = status.lock() {
|
||||
f(&mut s);
|
||||
}
|
||||
}
|
||||
|
||||
fn run(
|
||||
url: &str,
|
||||
realm: RealmId,
|
||||
key: &SigningKey,
|
||||
alloc_rx: Receiver<(
|
||||
Uuid,
|
||||
Option<String>,
|
||||
Sender<Result<RelayAllocationInfo, LinkError>>,
|
||||
)>,
|
||||
stop: &AtomicBool,
|
||||
status: &Mutex<RelayStatus>,
|
||||
) {
|
||||
let mut failures = 0u32;
|
||||
while !stop.load(Ordering::SeqCst) {
|
||||
let began = Instant::now();
|
||||
let result = session(url, realm, key, &alloc_rx, stop, status);
|
||||
set_status(status, |s| {
|
||||
s.connected = false;
|
||||
s.active_streams = 0;
|
||||
if let Err(e) = &result {
|
||||
s.last_error = Some(e.to_string());
|
||||
}
|
||||
});
|
||||
if stop.load(Ordering::SeqCst) {
|
||||
return;
|
||||
}
|
||||
|
||||
failures = if began.elapsed() > Duration::from_secs(60) {
|
||||
1
|
||||
} else {
|
||||
failures.saturating_add(1)
|
||||
};
|
||||
let wait = crate::realm_registry::host::backoff(
|
||||
failures,
|
||||
Duration::from_secs(2),
|
||||
Duration::from_secs(60),
|
||||
0.5,
|
||||
);
|
||||
let end = Instant::now() + wait;
|
||||
while Instant::now() < end && !stop.load(Ordering::SeqCst) {
|
||||
std::thread::sleep(Duration::from_millis(200));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct StreamWorker {
|
||||
tx: Sender<TunnelMsg>,
|
||||
}
|
||||
|
||||
fn session(
|
||||
url: &str,
|
||||
realm: RealmId,
|
||||
key: &SigningKey,
|
||||
alloc_rx: &Receiver<(
|
||||
Uuid,
|
||||
Option<String>,
|
||||
Sender<Result<RelayAllocationInfo, LinkError>>,
|
||||
)>,
|
||||
stop: &AtomicBool,
|
||||
status: &Mutex<RelayStatus>,
|
||||
) -> Result<(), LinkError> {
|
||||
let mut ws = transport::connect(url, POLL)?;
|
||||
|
||||
// 1. Read challenge
|
||||
let challenge_raw = match transport::poll(&mut ws)? {
|
||||
Some(Message::Text(t)) => t,
|
||||
_ => {
|
||||
// wait up to 10s
|
||||
let deadline = Instant::now() + Duration::from_secs(10);
|
||||
let mut got = None;
|
||||
while Instant::now() < deadline && got.is_none() {
|
||||
if let Some(Message::Text(t)) = transport::poll(&mut ws)? {
|
||||
got = Some(t);
|
||||
break;
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
got.ok_or(LinkError::Timeout)?
|
||||
}
|
||||
};
|
||||
|
||||
let challenge: RelayChallenge = serde_json::from_str(&challenge_raw)
|
||||
.map_err(|e| LinkError::Protocol(format!("invalid challenge: {e}")))?;
|
||||
|
||||
// 2. Send HostHello
|
||||
let sig = sign_relay_challenge(key, &realm, &challenge.nonce);
|
||||
let hello = HostHello {
|
||||
realm_id: realm,
|
||||
signature: sig,
|
||||
};
|
||||
ws.send(Message::Text(serde_json::to_string(&hello).unwrap().into()))
|
||||
.map_err(|e| LinkError::Io(e.to_string()))?;
|
||||
|
||||
// 3. Receive welcome
|
||||
let deadline = Instant::now() + Duration::from_secs(10);
|
||||
let mut welcome_ok = false;
|
||||
while Instant::now() < deadline {
|
||||
if let Some(Message::Text(t)) = transport::poll(&mut ws)? {
|
||||
let welcome: RelayWelcome = serde_json::from_str(&t)
|
||||
.map_err(|e| LinkError::Protocol(format!("invalid welcome: {e}")))?;
|
||||
if welcome.ok {
|
||||
welcome_ok = true;
|
||||
break;
|
||||
} else {
|
||||
return Err(LinkError::Auth("relay rejected host".into()));
|
||||
}
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
|
||||
if !welcome_ok {
|
||||
return Err(LinkError::Timeout);
|
||||
}
|
||||
|
||||
set_status(status, |s| {
|
||||
s.connected = true;
|
||||
s.last_error = None;
|
||||
});
|
||||
tracing::info!(%realm, "relay link active");
|
||||
|
||||
let mut streams: HashMap<u32, StreamWorker> = HashMap::new();
|
||||
let mut pending_allocs: HashMap<u64, Sender<Result<RelayAllocationInfo, LinkError>>> =
|
||||
HashMap::new();
|
||||
let req_counter = AtomicU64::new(1);
|
||||
let (ws_out_tx, ws_out_rx) = channel::<TunnelMsg>();
|
||||
|
||||
let mut last_ping = Instant::now();
|
||||
|
||||
while !stop.load(Ordering::SeqCst) {
|
||||
// Drain pending allocation requests
|
||||
while let Ok((player_id, expected_client_ip, reply_tx)) = alloc_rx.try_recv() {
|
||||
let rid = req_counter.fetch_add(1, Ordering::Relaxed);
|
||||
pending_allocs.insert(rid, reply_tx);
|
||||
let msg = TunnelMsg::Allocate {
|
||||
request_id: rid,
|
||||
player_id,
|
||||
expected_client_ip,
|
||||
};
|
||||
let _ = ws.send(Message::Text(serde_json::to_string(&msg).unwrap().into()));
|
||||
}
|
||||
|
||||
// Drain outgoing messages from streams to ws
|
||||
while let Ok(msg) = ws_out_rx.try_recv() {
|
||||
let _ = ws.send(Message::Text(serde_json::to_string(&msg).unwrap().into()));
|
||||
}
|
||||
|
||||
// Keepalive
|
||||
if last_ping.elapsed() > Duration::from_secs(30) {
|
||||
let _ = ws.send(Message::Text(
|
||||
serde_json::to_string(&TunnelMsg::Ping).unwrap().into(),
|
||||
));
|
||||
last_ping = Instant::now();
|
||||
}
|
||||
|
||||
// Poll WS incoming (drain available frames)
|
||||
for _ in 0..128 {
|
||||
match transport::poll(&mut ws)? {
|
||||
None => break,
|
||||
Some(Message::Text(text)) => {
|
||||
let Ok(msg) = serde_json::from_str::<TunnelMsg>(&text) else {
|
||||
continue;
|
||||
};
|
||||
match msg {
|
||||
TunnelMsg::AllocateOk {
|
||||
request_id,
|
||||
token,
|
||||
relay_host,
|
||||
auth_port,
|
||||
world_port,
|
||||
expires_at,
|
||||
} => {
|
||||
if let Some(tx) = pending_allocs.remove(&request_id) {
|
||||
let _ = tx.send(Ok(RelayAllocationInfo {
|
||||
relay_host,
|
||||
auth_port,
|
||||
world_port,
|
||||
token,
|
||||
expires_at,
|
||||
}));
|
||||
}
|
||||
}
|
||||
TunnelMsg::AllocateErr { request_id, error } => {
|
||||
if let Some(tx) = pending_allocs.remove(&request_id) {
|
||||
let _ = tx.send(Err(LinkError::Protocol(error)));
|
||||
}
|
||||
}
|
||||
TunnelMsg::Connect {
|
||||
stream_id, target, ..
|
||||
} => {
|
||||
// Strictly predefined local targets: Auth (3724) or World (8085)
|
||||
let local_port = target.local_port();
|
||||
match TcpStream::connect(("127.0.0.1", local_port)) {
|
||||
Ok(tcp) => {
|
||||
let (stream_tx, stream_rx) = channel::<TunnelMsg>();
|
||||
let ws_tx = ws_out_tx.clone();
|
||||
|
||||
// Spawn worker for this stream
|
||||
spawn_local_tcp_bridge(stream_id, tcp, stream_rx, ws_tx);
|
||||
streams.insert(stream_id, StreamWorker { tx: stream_tx });
|
||||
|
||||
// Reply ConnectOk
|
||||
let ok_msg = TunnelMsg::ConnectOk { stream_id };
|
||||
let _ = ws.send(Message::Text(
|
||||
serde_json::to_string(&ok_msg).unwrap().into(),
|
||||
));
|
||||
}
|
||||
Err(e) => {
|
||||
let err_msg = TunnelMsg::ConnectErr {
|
||||
stream_id,
|
||||
error: e.to_string(),
|
||||
};
|
||||
let _ = ws.send(Message::Text(
|
||||
serde_json::to_string(&err_msg).unwrap().into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
set_status(status, |s| s.active_streams = streams.len());
|
||||
}
|
||||
TunnelMsg::Data { stream_id, chunk } => {
|
||||
if let Some(w) = streams.get(&stream_id) {
|
||||
let _ = w.tx.send(TunnelMsg::Data { stream_id, chunk });
|
||||
}
|
||||
}
|
||||
TunnelMsg::Close { stream_id } => {
|
||||
if let Some(w) = streams.remove(&stream_id) {
|
||||
let _ = w.tx.send(TunnelMsg::Close { stream_id });
|
||||
}
|
||||
set_status(status, |s| s.active_streams = streams.len());
|
||||
}
|
||||
TunnelMsg::Reset { stream_id } => {
|
||||
if let Some(w) = streams.remove(&stream_id) {
|
||||
let _ = w.tx.send(TunnelMsg::Reset { stream_id });
|
||||
}
|
||||
set_status(status, |s| s.active_streams = streams.len());
|
||||
}
|
||||
TunnelMsg::Pong => {}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Some(Message::Close(_)) => {
|
||||
return Err(LinkError::Io("relay closed connection".into()))
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn spawn_local_tcp_bridge(
|
||||
stream_id: u32,
|
||||
mut tcp: TcpStream,
|
||||
from_tunnel: Receiver<TunnelMsg>,
|
||||
to_tunnel: Sender<TunnelMsg>,
|
||||
) {
|
||||
let _ = tcp.set_nonblocking(false);
|
||||
let _ = tcp.set_nodelay(true);
|
||||
let mut tcp_read = tcp.try_clone().expect("tcp clone");
|
||||
let to_tunnel_read = to_tunnel.clone();
|
||||
|
||||
// Local TCP read -> to tunnel
|
||||
std::thread::Builder::new()
|
||||
.name(format!("relay-stream-{stream_id}-read"))
|
||||
.spawn(move || {
|
||||
let mut buf = [0u8; 16384];
|
||||
loop {
|
||||
match tcp_read.read(&mut buf) {
|
||||
Ok(0) => {
|
||||
let _ = to_tunnel_read.send(TunnelMsg::Close { stream_id });
|
||||
break;
|
||||
}
|
||||
Ok(n) => {
|
||||
let chunk = base64::engine::general_purpose::STANDARD.encode(&buf[..n]);
|
||||
if to_tunnel_read
|
||||
.send(TunnelMsg::Data { stream_id, chunk })
|
||||
.is_err()
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
let _ = to_tunnel_read.send(TunnelMsg::Reset { stream_id });
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.ok();
|
||||
|
||||
// From tunnel -> local TCP write
|
||||
std::thread::Builder::new()
|
||||
.name(format!("relay-stream-{stream_id}-write"))
|
||||
.spawn(move || {
|
||||
while let Ok(msg) = from_tunnel.recv() {
|
||||
match msg {
|
||||
TunnelMsg::Data { chunk, .. } => {
|
||||
if let Ok(data) = base64::engine::general_purpose::STANDARD.decode(&chunk) {
|
||||
if tcp.write_all(&data).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
TunnelMsg::Close { .. } => {
|
||||
let _ = tcp.shutdown(std::net::Shutdown::Write);
|
||||
break;
|
||||
}
|
||||
TunnelMsg::Reset { .. } => {
|
||||
let _ = tcp.shutdown(std::net::Shutdown::Both);
|
||||
break;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
@@ -1,369 +0,0 @@
|
||||
//! Secrets the Manager keeps for the player: the Ed25519 key of the player's identity and the game account passwords per realm. They are never written to a
|
||||
//! JSON file, to SQLite, to a log or to a diagnostics report.
|
||||
//!
|
||||
//! * **Windows**: every secret is one file that holds a DPAPI blob (`CryptProtectData`, current-user scope, an application-specific entropy value). Only the
|
||||
//! same Windows user on the same machine can decrypt it; a copy of the file on another machine or account is useless. (The files live in the Manager's data
|
||||
//! folder. Windows Credential Manager would also have done; DPAPI is what it uses underneath, without its size and naming limits.)
|
||||
//! * **Other systems**: a file with mode 0600 in the Manager's data folder. **That is not encryption**: anybody who can read the user's files can read it. A
|
||||
//! desktop keyring (Secret Service / Keychain) is the proper store there and is not implemented yet; [`SecretStore::kind`] says which one is in use so that
|
||||
//! the interface can tell the player.
|
||||
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Mutex;
|
||||
|
||||
use crate::{Error, Result};
|
||||
|
||||
pub trait SecretStore: Send + Sync {
|
||||
fn get(&self, name: &str) -> Result<Option<Vec<u8>>>;
|
||||
fn put(&self, name: &str, value: &[u8]) -> Result<()>;
|
||||
fn delete(&self, name: &str) -> Result<()>;
|
||||
/// `dpapi`, `file` (mode 0600, not encrypted) or `memory`.
|
||||
fn kind(&self) -> &'static str;
|
||||
}
|
||||
|
||||
fn check_name(name: &str) -> Result<()> {
|
||||
if name.is_empty()
|
||||
|| name.len() > 96
|
||||
|| !name
|
||||
.bytes()
|
||||
.all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b))
|
||||
|| name.starts_with('.')
|
||||
{
|
||||
return Err(Error::Invalid("a secret has an invalid name".into()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The store this system should use, in `dir`.
|
||||
pub fn default_store(dir: &Path) -> Box<dyn SecretStore> {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
Box::new(DpapiStore::new(dir))
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
Box::new(FileStore::new(dir))
|
||||
}
|
||||
}
|
||||
|
||||
fn write_private(path: &Path, bytes: &[u8]) -> Result<()> {
|
||||
let dir = path
|
||||
.parent()
|
||||
.ok_or_else(|| Error::Invalid("a secret has no folder".into()))?;
|
||||
fs::create_dir_all(dir)?;
|
||||
let tmp = dir.join(format!(".{}.tmp", uuid::Uuid::new_v4().simple()));
|
||||
let result = (|| -> Result<()> {
|
||||
let mut f = fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.open(&tmp)?;
|
||||
restrict(&tmp)?;
|
||||
f.write_all(bytes)?;
|
||||
f.sync_all()?;
|
||||
drop(f);
|
||||
fs::rename(&tmp, path)?;
|
||||
Ok(())
|
||||
})();
|
||||
if result.is_err() {
|
||||
let _ = fs::remove_file(&tmp);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn restrict(file: &Path) -> Result<()> {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
fs::set_permissions(file, fs::Permissions::from_mode(0o600))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn restrict(file: &Path) -> Result<()> {
|
||||
let user = std::env::var("USERNAME")
|
||||
.map_err(|_| Error::Invalid("the current user is unknown".into()))?;
|
||||
let domain = std::env::var("USERDOMAIN").unwrap_or_default();
|
||||
let who = if domain.is_empty() {
|
||||
user
|
||||
} else {
|
||||
format!("{domain}\\{user}")
|
||||
};
|
||||
let out = std::process::Command::new("icacls")
|
||||
.arg(file)
|
||||
.arg("/inheritance:r")
|
||||
.arg("/grant:r")
|
||||
.arg(format!("{who}:(F)"))
|
||||
.output()?;
|
||||
if !out.status.success() {
|
||||
return Err(Error::Invalid(
|
||||
"a secret file's permissions could not be restricted".into(),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(any(unix, windows)))]
|
||||
fn restrict(_file: &Path) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_file(path: &Path) -> Result<Option<Vec<u8>>> {
|
||||
match fs::read(path) {
|
||||
Ok(b) => Ok(Some(b)),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_file(path: &Path) -> Result<()> {
|
||||
match fs::remove_file(path) {
|
||||
Ok(()) => Ok(()),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// A file with mode 0600 per secret. Not encrypted.
|
||||
pub struct FileStore {
|
||||
dir: PathBuf,
|
||||
}
|
||||
|
||||
impl FileStore {
|
||||
pub fn new(dir: impl Into<PathBuf>) -> Self {
|
||||
Self { dir: dir.into() }
|
||||
}
|
||||
|
||||
fn path(&self, name: &str) -> PathBuf {
|
||||
self.dir.join(format!("{name}.secret"))
|
||||
}
|
||||
}
|
||||
|
||||
impl SecretStore for FileStore {
|
||||
fn get(&self, name: &str) -> Result<Option<Vec<u8>>> {
|
||||
check_name(name)?;
|
||||
read_file(&self.path(name))
|
||||
}
|
||||
fn put(&self, name: &str, value: &[u8]) -> Result<()> {
|
||||
check_name(name)?;
|
||||
write_private(&self.path(name), value)
|
||||
}
|
||||
fn delete(&self, name: &str) -> Result<()> {
|
||||
check_name(name)?;
|
||||
remove_file(&self.path(name))
|
||||
}
|
||||
fn kind(&self) -> &'static str {
|
||||
"file"
|
||||
}
|
||||
}
|
||||
|
||||
/// Process memory only (tests).
|
||||
#[derive(Default)]
|
||||
pub struct MemoryStore(Mutex<std::collections::HashMap<String, Vec<u8>>>);
|
||||
|
||||
impl SecretStore for MemoryStore {
|
||||
fn get(&self, name: &str) -> Result<Option<Vec<u8>>> {
|
||||
check_name(name)?;
|
||||
Ok(self
|
||||
.0
|
||||
.lock()
|
||||
.map_err(|_| Error::Invalid("a lock was poisoned".into()))?
|
||||
.get(name)
|
||||
.cloned())
|
||||
}
|
||||
fn put(&self, name: &str, value: &[u8]) -> Result<()> {
|
||||
check_name(name)?;
|
||||
self.0
|
||||
.lock()
|
||||
.map_err(|_| Error::Invalid("a lock was poisoned".into()))?
|
||||
.insert(name.to_string(), value.to_vec());
|
||||
Ok(())
|
||||
}
|
||||
fn delete(&self, name: &str) -> Result<()> {
|
||||
check_name(name)?;
|
||||
self.0
|
||||
.lock()
|
||||
.map_err(|_| Error::Invalid("a lock was poisoned".into()))?
|
||||
.remove(name);
|
||||
Ok(())
|
||||
}
|
||||
fn kind(&self) -> &'static str {
|
||||
"memory"
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
pub use dpapi::DpapiStore;
|
||||
|
||||
#[cfg(windows)]
|
||||
mod dpapi {
|
||||
use super::*;
|
||||
use windows_sys::Win32::Foundation::LocalFree;
|
||||
use windows_sys::Win32::Security::Cryptography::{
|
||||
CryptProtectData, CryptUnprotectData, CRYPTPROTECT_UI_FORBIDDEN, CRYPT_INTEGER_BLOB,
|
||||
};
|
||||
|
||||
/// Mixed into every blob: a different program running as the same user does not decrypt the Manager's secrets by accident.
|
||||
const ENTROPY: &[u8] = b"coa-server-manager/secret-store/v1";
|
||||
|
||||
fn blob(data: &[u8]) -> CRYPT_INTEGER_BLOB {
|
||||
CRYPT_INTEGER_BLOB {
|
||||
cbData: data.len() as u32,
|
||||
pbData: data.as_ptr() as *mut u8,
|
||||
}
|
||||
}
|
||||
|
||||
fn take(out: CRYPT_INTEGER_BLOB) -> Vec<u8> {
|
||||
let v = unsafe { std::slice::from_raw_parts(out.pbData, out.cbData as usize) }.to_vec();
|
||||
unsafe { LocalFree(out.pbData as _) };
|
||||
v
|
||||
}
|
||||
|
||||
pub fn protect(plain: &[u8]) -> Result<Vec<u8>> {
|
||||
let input = blob(plain);
|
||||
let entropy = blob(ENTROPY);
|
||||
let mut out = CRYPT_INTEGER_BLOB {
|
||||
cbData: 0,
|
||||
pbData: std::ptr::null_mut(),
|
||||
};
|
||||
let ok = unsafe {
|
||||
CryptProtectData(
|
||||
&input,
|
||||
std::ptr::null(),
|
||||
&entropy,
|
||||
std::ptr::null(),
|
||||
std::ptr::null(),
|
||||
CRYPTPROTECT_UI_FORBIDDEN,
|
||||
&mut out,
|
||||
)
|
||||
};
|
||||
if ok == 0 {
|
||||
return Err(Error::Invalid("Windows could not protect a secret".into()));
|
||||
}
|
||||
Ok(take(out))
|
||||
}
|
||||
|
||||
pub fn unprotect(sealed: &[u8]) -> Result<Vec<u8>> {
|
||||
let input = blob(sealed);
|
||||
let entropy = blob(ENTROPY);
|
||||
let mut out = CRYPT_INTEGER_BLOB {
|
||||
cbData: 0,
|
||||
pbData: std::ptr::null_mut(),
|
||||
};
|
||||
let ok = unsafe {
|
||||
CryptUnprotectData(
|
||||
&input,
|
||||
std::ptr::null_mut(),
|
||||
&entropy,
|
||||
std::ptr::null(),
|
||||
std::ptr::null(),
|
||||
CRYPTPROTECT_UI_FORBIDDEN,
|
||||
&mut out,
|
||||
)
|
||||
};
|
||||
if ok == 0 {
|
||||
return Err(Error::Invalid("a secret could not be unlocked (it was made by another Windows user or on another computer)".into()));
|
||||
}
|
||||
Ok(take(out))
|
||||
}
|
||||
|
||||
pub struct DpapiStore {
|
||||
dir: PathBuf,
|
||||
}
|
||||
|
||||
impl DpapiStore {
|
||||
pub fn new(dir: impl Into<PathBuf>) -> Self {
|
||||
Self { dir: dir.into() }
|
||||
}
|
||||
fn path(&self, name: &str) -> PathBuf {
|
||||
self.dir.join(format!("{name}.dpapi"))
|
||||
}
|
||||
}
|
||||
|
||||
impl SecretStore for DpapiStore {
|
||||
fn get(&self, name: &str) -> Result<Option<Vec<u8>>> {
|
||||
check_name(name)?;
|
||||
read_file(&self.path(name))?
|
||||
.map(|b| unprotect(&b))
|
||||
.transpose()
|
||||
}
|
||||
fn put(&self, name: &str, value: &[u8]) -> Result<()> {
|
||||
check_name(name)?;
|
||||
write_private(&self.path(name), &protect(value)?)
|
||||
}
|
||||
fn delete(&self, name: &str) -> Result<()> {
|
||||
check_name(name)?;
|
||||
remove_file(&self.path(name))
|
||||
}
|
||||
fn kind(&self) -> &'static str {
|
||||
"dpapi"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn round_trip(store: &dyn SecretStore) {
|
||||
assert_eq!(store.get("realm-abc").unwrap(), None);
|
||||
store.put("realm-abc", b"hunter2-secret").unwrap();
|
||||
assert_eq!(
|
||||
store.get("realm-abc").unwrap().as_deref(),
|
||||
Some(b"hunter2-secret".as_slice())
|
||||
);
|
||||
store.put("realm-abc", b"another").unwrap();
|
||||
assert_eq!(
|
||||
store.get("realm-abc").unwrap().as_deref(),
|
||||
Some(b"another".as_slice()),
|
||||
"replaced"
|
||||
);
|
||||
store.delete("realm-abc").unwrap();
|
||||
store.delete("realm-abc").unwrap();
|
||||
assert_eq!(store.get("realm-abc").unwrap(), None);
|
||||
for bad in ["", "../x", "a/b", "a b", ".hidden", &"x".repeat(97)] {
|
||||
assert!(
|
||||
store.put(bad, b"x").is_err() && store.get(bad).is_err(),
|
||||
"{bad:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_memory_and_file_stores_keep_and_forget() {
|
||||
round_trip(&MemoryStore::default());
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
round_trip(&FileStore::new(dir.path().join("secrets")));
|
||||
assert_eq!(FileStore::new(dir.path()).kind(), "file");
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn dpapi_secrets_are_not_stored_in_the_clear_and_come_back() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let store = DpapiStore::new(dir.path().join("secrets"));
|
||||
round_trip(&store);
|
||||
store
|
||||
.put("player-key", b"VERY-SECRET-PLAYER-KEY-0123456789")
|
||||
.unwrap();
|
||||
let raw = fs::read(dir.path().join("secrets").join("player-key.dpapi")).unwrap();
|
||||
assert!(
|
||||
!raw.windows(11).any(|w| w == b"VERY-SECRET"),
|
||||
"the file holds no plaintext"
|
||||
);
|
||||
assert!(raw.len() > 40);
|
||||
let mut tampered = raw.clone();
|
||||
let last = tampered.len() - 1;
|
||||
tampered[last] ^= 1;
|
||||
fs::write(
|
||||
dir.path().join("secrets").join("player-key.dpapi"),
|
||||
tampered,
|
||||
)
|
||||
.unwrap();
|
||||
assert!(
|
||||
store.get("player-key").is_err(),
|
||||
"a damaged blob is an error, not garbage"
|
||||
);
|
||||
assert_eq!(store.kind(), "dpapi");
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,716 +0,0 @@
|
||||
//! The control plane's own SQLite database (`control.sqlite`). It holds **no secret**: no password, no key, no payload.
|
||||
//!
|
||||
//! * Host side: which PlayerId owns which game account of which local realm (and the public key the PlayerId first presented), and which realm
|
||||
//! characters were claimed by which PlayerId (a binding of a PortableCharacterId to a PlayerIdentity).
|
||||
//! * Player side: for each remote realm the account name the player plays with (the password is in the secret store) and which characters came from it.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::Path;
|
||||
|
||||
use rusqlite::{params, Connection, OptionalExtension};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{Error, Result};
|
||||
|
||||
const SCHEMA: &str = "
|
||||
CREATE TABLE IF NOT EXISTS host_player(
|
||||
realm_local_id TEXT NOT NULL,
|
||||
player_id TEXT NOT NULL,
|
||||
public_key TEXT NOT NULL,
|
||||
account_id INTEGER NOT NULL,
|
||||
username TEXT NOT NULL,
|
||||
kind TEXT NOT NULL CHECK (kind IN ('generated', 'linked')),
|
||||
created_at TEXT NOT NULL,
|
||||
PRIMARY KEY (realm_local_id, player_id)
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS host_player_account ON host_player(realm_local_id, account_id);
|
||||
CREATE TABLE IF NOT EXISTS host_claim(
|
||||
realm_local_id TEXT NOT NULL,
|
||||
local_guid INTEGER NOT NULL,
|
||||
character_id TEXT NOT NULL,
|
||||
player_id TEXT NOT NULL,
|
||||
state TEXT NOT NULL CHECK (state IN ('exported', 'acknowledged')),
|
||||
claimed_at TEXT NOT NULL,
|
||||
PRIMARY KEY (realm_local_id, local_guid)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS host_claim_player ON host_claim(realm_local_id, player_id);
|
||||
CREATE TABLE IF NOT EXISTS player_realm(
|
||||
realm_id TEXT PRIMARY KEY,
|
||||
username TEXT NOT NULL,
|
||||
kind TEXT NOT NULL CHECK (kind IN ('generated', 'linked')),
|
||||
created_at TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS realm_pin(
|
||||
realm_id TEXT PRIMARY KEY,
|
||||
public_key TEXT NOT NULL,
|
||||
first_seen TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS player_claim(
|
||||
realm_id TEXT NOT NULL,
|
||||
character_id TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
claimed_at TEXT NOT NULL,
|
||||
PRIMARY KEY (realm_id, character_id)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS host_transfer(
|
||||
realm_local_id TEXT NOT NULL,
|
||||
transfer_id TEXT NOT NULL PRIMARY KEY,
|
||||
character_id TEXT NOT NULL,
|
||||
player_id TEXT NOT NULL,
|
||||
revision INTEGER NOT NULL,
|
||||
content_hash TEXT NOT NULL,
|
||||
total_size INTEGER NOT NULL,
|
||||
received_size INTEGER NOT NULL,
|
||||
state TEXT NOT NULL CHECK (state IN ('receiving', 'committed', 'acknowledged')),
|
||||
local_guid INTEGER,
|
||||
session_id TEXT,
|
||||
projected_level INTEGER,
|
||||
notes TEXT,
|
||||
collections TEXT NOT NULL DEFAULT '{}',
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS host_transfer_char ON host_transfer(realm_local_id, character_id);
|
||||
";
|
||||
|
||||
fn now() -> String {
|
||||
chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
|
||||
}
|
||||
|
||||
fn db(e: rusqlite::Error) -> Error {
|
||||
Error::Invalid(format!("the control database failed: {e}"))
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum AccountKind {
|
||||
Generated,
|
||||
Linked,
|
||||
}
|
||||
|
||||
impl AccountKind {
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
AccountKind::Generated => "generated",
|
||||
AccountKind::Linked => "linked",
|
||||
}
|
||||
}
|
||||
fn parse(s: &str) -> Self {
|
||||
if s == "linked" {
|
||||
AccountKind::Linked
|
||||
} else {
|
||||
AccountKind::Generated
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct HostPlayer {
|
||||
pub player_id: Uuid,
|
||||
pub public_key: String,
|
||||
pub account_id: u32,
|
||||
pub username: String,
|
||||
pub kind: AccountKind,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ClaimState {
|
||||
Exported,
|
||||
Acknowledged,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct HostClaim {
|
||||
pub local_guid: u32,
|
||||
pub character_id: Uuid,
|
||||
pub player_id: Uuid,
|
||||
pub state: ClaimState,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum TransferState {
|
||||
Receiving,
|
||||
Committed,
|
||||
Acknowledged,
|
||||
}
|
||||
|
||||
impl TransferState {
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
TransferState::Receiving => "receiving",
|
||||
TransferState::Committed => "committed",
|
||||
TransferState::Acknowledged => "acknowledged",
|
||||
}
|
||||
}
|
||||
pub fn parse(s: &str) -> Self {
|
||||
match s {
|
||||
"committed" => TransferState::Committed,
|
||||
"acknowledged" => TransferState::Acknowledged,
|
||||
_ => TransferState::Receiving,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct HostTransfer {
|
||||
pub realm_local_id: String,
|
||||
pub transfer_id: Uuid,
|
||||
pub character_id: Uuid,
|
||||
pub player_id: Uuid,
|
||||
pub revision: u64,
|
||||
pub content_hash: String,
|
||||
pub total_size: usize,
|
||||
pub received_size: usize,
|
||||
pub state: TransferState,
|
||||
pub local_guid: Option<u32>,
|
||||
pub session_id: Option<Uuid>,
|
||||
pub projected_level: Option<u32>,
|
||||
pub notes: Vec<String>,
|
||||
pub collections: BTreeMap<String, Vec<u32>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct PlayerRealm {
|
||||
pub realm_id: String,
|
||||
pub username: String,
|
||||
pub kind: AccountKind,
|
||||
}
|
||||
|
||||
pub struct ControlStore {
|
||||
conn: Connection,
|
||||
}
|
||||
|
||||
fn uuid(s: String) -> Result<Uuid> {
|
||||
Uuid::parse_str(&s).map_err(|_| Error::Invalid("the control database holds a bad id".into()))
|
||||
}
|
||||
|
||||
impl ControlStore {
|
||||
pub fn open(dir: &Path) -> Result<Self> {
|
||||
std::fs::create_dir_all(dir)?;
|
||||
let conn = Connection::open(dir.join("control.sqlite")).map_err(db)?;
|
||||
Self::init(conn)
|
||||
}
|
||||
|
||||
pub fn open_in_memory() -> Result<Self> {
|
||||
Self::init(Connection::open_in_memory().map_err(db)?)
|
||||
}
|
||||
|
||||
fn init(conn: Connection) -> Result<Self> {
|
||||
conn.busy_timeout(std::time::Duration::from_secs(5))
|
||||
.map_err(db)?;
|
||||
let _ = conn.pragma_update(None, "journal_mode", "WAL");
|
||||
conn.execute_batch(SCHEMA).map_err(db)?;
|
||||
Ok(Self { conn })
|
||||
}
|
||||
|
||||
// ---- Host --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
pub fn host_player(&self, realm: &str, player: &Uuid) -> Result<Option<HostPlayer>> {
|
||||
self.conn
|
||||
.query_row("SELECT player_id, public_key, account_id, username, kind FROM host_player WHERE realm_local_id = ?1 AND player_id = ?2", params![realm, player.to_string()], row_player)
|
||||
.optional()
|
||||
.map_err(db)?
|
||||
.transpose()
|
||||
}
|
||||
|
||||
pub fn host_player_of_account(
|
||||
&self,
|
||||
realm: &str,
|
||||
account_id: u32,
|
||||
) -> Result<Option<HostPlayer>> {
|
||||
self.conn
|
||||
.query_row("SELECT player_id, public_key, account_id, username, kind FROM host_player WHERE realm_local_id = ?1 AND account_id = ?2", params![realm, account_id], row_player)
|
||||
.optional()
|
||||
.map_err(db)?
|
||||
.transpose()
|
||||
}
|
||||
|
||||
pub fn host_bind_player(
|
||||
&self,
|
||||
realm: &str,
|
||||
player: &Uuid,
|
||||
public_key: &str,
|
||||
account_id: u32,
|
||||
username: &str,
|
||||
kind: AccountKind,
|
||||
) -> Result<()> {
|
||||
self.conn
|
||||
.execute("INSERT INTO host_player(realm_local_id, player_id, public_key, account_id, username, kind, created_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)", params![realm, player.to_string(), public_key, account_id, username, kind.as_str(), now()])
|
||||
.map_err(db)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn host_forget_player(&self, realm: &str, player: &Uuid) -> Result<()> {
|
||||
self.conn
|
||||
.execute(
|
||||
"DELETE FROM host_player WHERE realm_local_id = ?1 AND player_id = ?2",
|
||||
params![realm, player.to_string()],
|
||||
)
|
||||
.map_err(db)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn host_claim(&self, realm: &str, local_guid: u32) -> Result<Option<HostClaim>> {
|
||||
self.conn
|
||||
.query_row("SELECT local_guid, character_id, player_id, state FROM host_claim WHERE realm_local_id = ?1 AND local_guid = ?2", params![realm, local_guid], row_claim)
|
||||
.optional()
|
||||
.map_err(db)?
|
||||
.transpose()
|
||||
}
|
||||
|
||||
pub fn host_claims_of(&self, realm: &str, player: &Uuid) -> Result<Vec<HostClaim>> {
|
||||
let mut stmt = self.conn.prepare("SELECT local_guid, character_id, player_id, state FROM host_claim WHERE realm_local_id = ?1 AND player_id = ?2 ORDER BY local_guid").map_err(db)?;
|
||||
let rows = stmt
|
||||
.query_map(params![realm, player.to_string()], row_claim)
|
||||
.map_err(db)?;
|
||||
rows.map(|r| r.map_err(db)?).collect()
|
||||
}
|
||||
|
||||
pub fn host_claim_set(
|
||||
&self,
|
||||
realm: &str,
|
||||
local_guid: u32,
|
||||
character: &Uuid,
|
||||
player: &Uuid,
|
||||
) -> Result<()> {
|
||||
self.conn
|
||||
.execute(
|
||||
"INSERT INTO host_claim(realm_local_id, local_guid, character_id, player_id, state, claimed_at) VALUES (?1, ?2, ?3, ?4, 'exported', ?5)
|
||||
ON CONFLICT(realm_local_id, local_guid) DO UPDATE SET character_id = excluded.character_id WHERE host_claim.player_id = excluded.player_id",
|
||||
params![realm, local_guid, character.to_string(), player.to_string(), now()],
|
||||
)
|
||||
.map_err(db)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn host_claim_acknowledge(
|
||||
&self,
|
||||
realm: &str,
|
||||
character: &Uuid,
|
||||
player: &Uuid,
|
||||
) -> Result<bool> {
|
||||
let n = self.conn.execute("UPDATE host_claim SET state = 'acknowledged' WHERE realm_local_id = ?1 AND character_id = ?2 AND player_id = ?3", params![realm, character.to_string(), player.to_string()]).map_err(db)?;
|
||||
Ok(n == 1)
|
||||
}
|
||||
|
||||
pub fn host_claim_of_character(
|
||||
&self,
|
||||
realm: &str,
|
||||
character: &Uuid,
|
||||
) -> Result<Option<HostClaim>> {
|
||||
self.conn
|
||||
.query_row("SELECT local_guid, character_id, player_id, state FROM host_claim WHERE realm_local_id = ?1 AND character_id = ?2 LIMIT 1", params![realm, character.to_string()], row_claim)
|
||||
.optional()
|
||||
.map_err(db)?
|
||||
.transpose()
|
||||
}
|
||||
|
||||
pub fn host_transfer(&self, realm: &str, transfer_id: &Uuid) -> Result<Option<HostTransfer>> {
|
||||
self.conn
|
||||
.query_row(
|
||||
"SELECT realm_local_id, transfer_id, character_id, player_id, revision, content_hash, total_size, received_size, state, local_guid, session_id, projected_level, notes, collections FROM host_transfer WHERE realm_local_id = ?1 AND transfer_id = ?2",
|
||||
params![realm, transfer_id.to_string()],
|
||||
row_transfer,
|
||||
)
|
||||
.optional()
|
||||
.map_err(db)?
|
||||
.transpose()
|
||||
}
|
||||
|
||||
pub fn host_transfer_latest(
|
||||
&self,
|
||||
realm: &str,
|
||||
character_id: &Uuid,
|
||||
) -> Result<Option<HostTransfer>> {
|
||||
self.conn
|
||||
.query_row(
|
||||
"SELECT realm_local_id, transfer_id, character_id, player_id, revision, content_hash, total_size, received_size, state, local_guid, session_id, projected_level, notes, collections FROM host_transfer WHERE realm_local_id = ?1 AND character_id = ?2 ORDER BY updated_at DESC LIMIT 1",
|
||||
params![realm, character_id.to_string()],
|
||||
row_transfer,
|
||||
)
|
||||
.optional()
|
||||
.map_err(db)?
|
||||
.transpose()
|
||||
}
|
||||
|
||||
pub fn host_transfer_save(&self, t: &HostTransfer) -> Result<()> {
|
||||
let notes_json = serde_json::to_string(&t.notes).unwrap_or_default();
|
||||
let coll_json = serde_json::to_string(&t.collections).unwrap_or_default();
|
||||
self.conn
|
||||
.execute(
|
||||
"INSERT INTO host_transfer(realm_local_id, transfer_id, character_id, player_id, revision, content_hash, total_size, received_size, state, local_guid, session_id, projected_level, notes, collections, created_at, updated_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16)
|
||||
ON CONFLICT(transfer_id) DO UPDATE SET
|
||||
received_size = excluded.received_size,
|
||||
state = excluded.state,
|
||||
local_guid = excluded.local_guid,
|
||||
session_id = excluded.session_id,
|
||||
projected_level = excluded.projected_level,
|
||||
notes = excluded.notes,
|
||||
collections = excluded.collections,
|
||||
updated_at = excluded.updated_at",
|
||||
params![
|
||||
t.realm_local_id,
|
||||
t.transfer_id.to_string(),
|
||||
t.character_id.to_string(),
|
||||
t.player_id.to_string(),
|
||||
t.revision as i64,
|
||||
t.content_hash,
|
||||
t.total_size as i64,
|
||||
t.received_size as i64,
|
||||
t.state.as_str(),
|
||||
t.local_guid,
|
||||
t.session_id.map(|s| s.to_string()),
|
||||
t.projected_level,
|
||||
notes_json,
|
||||
coll_json,
|
||||
now(),
|
||||
now(),
|
||||
],
|
||||
)
|
||||
.map_err(db)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn host_transfer_update_progress(&self, transfer_id: &Uuid, received: usize) -> Result<()> {
|
||||
self.conn
|
||||
.execute("UPDATE host_transfer SET received_size = ?1, updated_at = ?2 WHERE transfer_id = ?3", params![received as i64, now(), transfer_id.to_string()])
|
||||
.map_err(db)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn host_transfer_commit(
|
||||
&self,
|
||||
transfer_id: &Uuid,
|
||||
local_guid: u32,
|
||||
session_id: &Uuid,
|
||||
projected_level: Option<u32>,
|
||||
notes: &[String],
|
||||
) -> Result<()> {
|
||||
let notes_json = serde_json::to_string(notes).unwrap_or_default();
|
||||
self.conn
|
||||
.execute(
|
||||
"UPDATE host_transfer SET state = 'committed', local_guid = ?1, session_id = ?2, projected_level = ?3, notes = ?4, updated_at = ?5 WHERE transfer_id = ?6",
|
||||
params![local_guid, session_id.to_string(), projected_level, notes_json, now(), transfer_id.to_string()],
|
||||
)
|
||||
.map_err(db)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn host_transfer_ack(&self, transfer_id: &Uuid) -> Result<bool> {
|
||||
let n = self.conn.execute("UPDATE host_transfer SET state = 'acknowledged', updated_at = ?1 WHERE transfer_id = ?2", params![now(), transfer_id.to_string()]).map_err(db)?;
|
||||
Ok(n == 1)
|
||||
}
|
||||
|
||||
pub fn host_transfer_delete(&self, transfer_id: &Uuid) -> Result<()> {
|
||||
self.conn
|
||||
.execute(
|
||||
"DELETE FROM host_transfer WHERE transfer_id = ?1",
|
||||
params![transfer_id.to_string()],
|
||||
)
|
||||
.map_err(db)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---- Player ------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
pub fn player_realm(&self, realm_id: &str) -> Result<Option<PlayerRealm>> {
|
||||
self.conn
|
||||
.query_row(
|
||||
"SELECT realm_id, username, kind FROM player_realm WHERE realm_id = ?1",
|
||||
[realm_id],
|
||||
|r| {
|
||||
Ok(PlayerRealm {
|
||||
realm_id: r.get(0)?,
|
||||
username: r.get(1)?,
|
||||
kind: AccountKind::parse(&r.get::<_, String>(2)?),
|
||||
})
|
||||
},
|
||||
)
|
||||
.optional()
|
||||
.map_err(db)
|
||||
}
|
||||
|
||||
pub fn player_realm_set(
|
||||
&self,
|
||||
realm_id: &str,
|
||||
username: &str,
|
||||
kind: AccountKind,
|
||||
) -> Result<()> {
|
||||
self.conn
|
||||
.execute(
|
||||
"INSERT INTO player_realm(realm_id, username, kind, created_at) VALUES (?1, ?2, ?3, ?4)
|
||||
ON CONFLICT(realm_id) DO UPDATE SET username = excluded.username, kind = excluded.kind",
|
||||
params![realm_id, username, kind.as_str(), now()],
|
||||
)
|
||||
.map_err(db)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn player_realm_remove(&self, realm_id: &str) -> Result<()> {
|
||||
self.conn
|
||||
.execute("DELETE FROM player_realm WHERE realm_id = ?1", [realm_id])
|
||||
.map_err(db)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The public key a realm had when this player first reached it. A realm id never changes its key (a new key is a new realm), so a different one is refused.
|
||||
pub fn realm_pin(&self, realm_id: &str) -> Result<Option<String>> {
|
||||
self.conn
|
||||
.query_row(
|
||||
"SELECT public_key FROM realm_pin WHERE realm_id = ?1",
|
||||
[realm_id],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.optional()
|
||||
.map_err(db)
|
||||
}
|
||||
|
||||
pub fn realm_pin_set(&self, realm_id: &str, public_key: &str) -> Result<()> {
|
||||
self.conn.execute("INSERT OR IGNORE INTO realm_pin(realm_id, public_key, first_seen) VALUES (?1, ?2, ?3)", params![realm_id, public_key, now()]).map_err(db)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn player_claim_add(&self, realm_id: &str, character: &Uuid, name: &str) -> Result<()> {
|
||||
self.conn.execute("INSERT OR IGNORE INTO player_claim(realm_id, character_id, name, claimed_at) VALUES (?1, ?2, ?3, ?4)", params![realm_id, character.to_string(), name, now()]).map_err(db)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn player_claims(&self, realm_id: &str) -> Result<Vec<(Uuid, String)>> {
|
||||
let mut stmt = self.conn.prepare("SELECT character_id, name FROM player_claim WHERE realm_id = ?1 ORDER BY claimed_at").map_err(db)?;
|
||||
let rows = stmt
|
||||
.query_map([realm_id], |r| {
|
||||
Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?))
|
||||
})
|
||||
.map_err(db)?;
|
||||
rows.map(|r| {
|
||||
let (id, name) = r.map_err(db)?;
|
||||
Ok((uuid(id)?, name))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
fn row_player(r: &rusqlite::Row<'_>) -> rusqlite::Result<Result<HostPlayer>> {
|
||||
let (id, key, account, username, kind): (String, String, u32, String, String) =
|
||||
(r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?, r.get(4)?);
|
||||
Ok(uuid(id).map(|player_id| HostPlayer {
|
||||
player_id,
|
||||
public_key: key,
|
||||
account_id: account,
|
||||
username,
|
||||
kind: AccountKind::parse(&kind),
|
||||
}))
|
||||
}
|
||||
|
||||
fn row_claim(r: &rusqlite::Row<'_>) -> rusqlite::Result<Result<HostClaim>> {
|
||||
let (guid, cid, pid, state): (u32, String, String, String) =
|
||||
(r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?);
|
||||
Ok((|| {
|
||||
Ok(HostClaim {
|
||||
local_guid: guid,
|
||||
character_id: uuid(cid)?,
|
||||
player_id: uuid(pid)?,
|
||||
state: if state == "acknowledged" {
|
||||
ClaimState::Acknowledged
|
||||
} else {
|
||||
ClaimState::Exported
|
||||
},
|
||||
})
|
||||
})())
|
||||
}
|
||||
|
||||
fn row_transfer(r: &rusqlite::Row<'_>) -> rusqlite::Result<Result<HostTransfer>> {
|
||||
let realm_local_id: String = r.get(0)?;
|
||||
let tid: String = r.get(1)?;
|
||||
let cid: String = r.get(2)?;
|
||||
let pid: String = r.get(3)?;
|
||||
let revision: i64 = r.get(4)?;
|
||||
let content_hash: String = r.get(5)?;
|
||||
let total_size: i64 = r.get(6)?;
|
||||
let received_size: i64 = r.get(7)?;
|
||||
let state_str: String = r.get(8)?;
|
||||
let local_guid: Option<u32> = r.get(9)?;
|
||||
let session_str: Option<String> = r.get(10)?;
|
||||
let projected_level: Option<u32> = r.get(11)?;
|
||||
let notes_str: Option<String> = r.get(12)?;
|
||||
let notes = notes_str
|
||||
.and_then(|s| serde_json::from_str::<Vec<String>>(&s).ok())
|
||||
.unwrap_or_default();
|
||||
let coll_str: Option<String> = r.get(13)?;
|
||||
let collections = coll_str
|
||||
.and_then(|s| serde_json::from_str::<BTreeMap<String, Vec<u32>>>(&s).ok())
|
||||
.unwrap_or_default();
|
||||
Ok((|| {
|
||||
Ok(HostTransfer {
|
||||
realm_local_id,
|
||||
transfer_id: uuid(tid)?,
|
||||
character_id: uuid(cid)?,
|
||||
player_id: uuid(pid)?,
|
||||
revision: revision as u64,
|
||||
content_hash,
|
||||
total_size: total_size as usize,
|
||||
received_size: received_size as usize,
|
||||
state: TransferState::parse(&state_str),
|
||||
local_guid,
|
||||
session_id: session_str.map(uuid).transpose()?,
|
||||
projected_level,
|
||||
notes,
|
||||
collections,
|
||||
})
|
||||
})())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_player_owns_one_account_per_realm_and_an_account_one_player() {
|
||||
let s = ControlStore::open_in_memory().unwrap();
|
||||
let (a, b) = (Uuid::now_v7(), Uuid::now_v7());
|
||||
s.host_bind_player("srv-1", &a, "KEY-A", 7, "DMITRY", AccountKind::Generated)
|
||||
.unwrap();
|
||||
assert!(
|
||||
s.host_bind_player("srv-1", &a, "KEY-A", 8, "OTHER", AccountKind::Generated)
|
||||
.is_err(),
|
||||
"one account per player and realm"
|
||||
);
|
||||
assert!(
|
||||
s.host_bind_player("srv-1", &b, "KEY-B", 7, "DMITRY", AccountKind::Linked)
|
||||
.is_err(),
|
||||
"one player per account"
|
||||
);
|
||||
s.host_bind_player("srv-2", &a, "KEY-A", 7, "DMITRY", AccountKind::Generated)
|
||||
.unwrap();
|
||||
let p = s.host_player("srv-1", &a).unwrap().unwrap();
|
||||
assert_eq!(
|
||||
(
|
||||
p.account_id,
|
||||
p.username.as_str(),
|
||||
p.public_key.as_str(),
|
||||
p.kind
|
||||
),
|
||||
(7, "DMITRY", "KEY-A", AccountKind::Generated)
|
||||
);
|
||||
assert_eq!(
|
||||
s.host_player_of_account("srv-1", 7)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.player_id,
|
||||
a
|
||||
);
|
||||
assert!(s.host_player("srv-1", &b).unwrap().is_none());
|
||||
s.host_forget_player("srv-1", &a).unwrap();
|
||||
assert!(
|
||||
s.host_player("srv-1", &a).unwrap().is_none()
|
||||
&& s.host_player("srv-2", &a).unwrap().is_some()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_character_is_claimed_by_one_player_and_acknowledged_once() {
|
||||
let s = ControlStore::open_in_memory().unwrap();
|
||||
let (p, q, c) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
|
||||
s.host_claim_set("srv-1", 50, &c, &p).unwrap();
|
||||
assert_eq!(
|
||||
s.host_claim("srv-1", 50).unwrap().unwrap().state,
|
||||
ClaimState::Exported
|
||||
);
|
||||
s.host_claim_set("srv-1", 50, &c, &p).unwrap();
|
||||
s.host_claim_set("srv-1", 50, &Uuid::now_v7(), &q).unwrap();
|
||||
let held = s.host_claim("srv-1", 50).unwrap().unwrap();
|
||||
assert_eq!(
|
||||
(held.player_id, held.character_id),
|
||||
(p, c),
|
||||
"another player cannot take over a claim"
|
||||
);
|
||||
assert!(
|
||||
!s.host_claim_acknowledge("srv-1", &c, &q).unwrap(),
|
||||
"only the claiming player acknowledges"
|
||||
);
|
||||
assert!(s.host_claim_acknowledge("srv-1", &c, &p).unwrap());
|
||||
assert_eq!(
|
||||
s.host_claim("srv-1", 50).unwrap().unwrap().state,
|
||||
ClaimState::Acknowledged
|
||||
);
|
||||
assert_eq!(s.host_claims_of("srv-1", &p).unwrap().len(), 1);
|
||||
assert!(s.host_claims_of("srv-1", &q).unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_player_remembers_account_names_only() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
{
|
||||
let s = ControlStore::open(dir.path()).unwrap();
|
||||
s.player_realm_set("realm-1", "DMITRY_7K4M", AccountKind::Generated)
|
||||
.unwrap();
|
||||
s.player_claim_add("realm-1", &Uuid::now_v7(), "Thrall")
|
||||
.unwrap();
|
||||
}
|
||||
let s = ControlStore::open(dir.path()).unwrap();
|
||||
assert_eq!(
|
||||
s.player_realm("realm-1").unwrap().unwrap().username,
|
||||
"DMITRY_7K4M"
|
||||
);
|
||||
assert_eq!(s.player_claims("realm-1").unwrap().len(), 1);
|
||||
s.player_realm_remove("realm-1").unwrap();
|
||||
assert!(s.player_realm("realm-1").unwrap().is_none());
|
||||
let raw = std::fs::read(dir.path().join("control.sqlite")).unwrap();
|
||||
assert!(
|
||||
!String::from_utf8_lossy(&raw)
|
||||
.to_lowercase()
|
||||
.contains("password"),
|
||||
"the schema has no password column"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn host_transfer_roundtrip_and_lifecycle() {
|
||||
let s = ControlStore::open_in_memory().unwrap();
|
||||
let (p, c, t, sid) = (
|
||||
Uuid::now_v7(),
|
||||
Uuid::now_v7(),
|
||||
Uuid::now_v7(),
|
||||
Uuid::now_v7(),
|
||||
);
|
||||
let transfer = HostTransfer {
|
||||
realm_local_id: "live".into(),
|
||||
transfer_id: t,
|
||||
character_id: c,
|
||||
player_id: p,
|
||||
revision: 3,
|
||||
content_hash: "a".repeat(64),
|
||||
total_size: 4096,
|
||||
received_size: 0,
|
||||
state: TransferState::Receiving,
|
||||
local_guid: None,
|
||||
session_id: None,
|
||||
projected_level: None,
|
||||
notes: vec![],
|
||||
collections: BTreeMap::from([("coa:appearance".into(), vec![1, 2, 3])]),
|
||||
};
|
||||
s.host_transfer_save(&transfer).unwrap();
|
||||
let got = s.host_transfer("live", &t).unwrap().unwrap();
|
||||
assert_eq!(got.state, TransferState::Receiving);
|
||||
assert_eq!(got.total_size, 4096);
|
||||
|
||||
s.host_transfer_update_progress(&t, 2048).unwrap();
|
||||
let got = s.host_transfer("live", &t).unwrap().unwrap();
|
||||
assert_eq!(got.received_size, 2048);
|
||||
|
||||
s.host_transfer_commit(&t, 101, &sid, Some(60), &["projected 60".into()])
|
||||
.unwrap();
|
||||
let got = s.host_transfer("live", &t).unwrap().unwrap();
|
||||
assert_eq!(got.state, TransferState::Committed);
|
||||
assert_eq!(got.local_guid, Some(101));
|
||||
assert_eq!(got.session_id, Some(sid));
|
||||
assert_eq!(got.projected_level, Some(60));
|
||||
|
||||
let latest = s.host_transfer_latest("live", &c).unwrap().unwrap();
|
||||
assert_eq!(latest.transfer_id, t);
|
||||
|
||||
assert!(s.host_transfer_ack(&t).unwrap());
|
||||
let got = s.host_transfer("live", &t).unwrap().unwrap();
|
||||
assert_eq!(got.state, TransferState::Acknowledged);
|
||||
}
|
||||
}
|
||||
@@ -1,217 +0,0 @@
|
||||
//! WebSocket plumbing shared by the Host and the Player: connecting (TLS through the system's library for `wss://`), timeouts, and turning the Coordinator's frames
|
||||
//! into something to read. The Coordinator is untrusted for secrecy and authenticity; everything that matters is checked inside the end-to-end channel.
|
||||
|
||||
use std::io::ErrorKind;
|
||||
use std::net::TcpStream;
|
||||
use std::time::Duration;
|
||||
|
||||
use coa_control_proto::coord::{self, ErrorCode, Frame};
|
||||
use tungstenite::stream::MaybeTlsStream;
|
||||
use tungstenite::{Message, WebSocket};
|
||||
|
||||
pub type Ws = WebSocket<MaybeTlsStream<TcpStream>>;
|
||||
|
||||
/// Why a control connection failed, in terms the interface can explain.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum LinkError {
|
||||
/// The Coordinator said the realm's Manager is not connected.
|
||||
HostOffline,
|
||||
HostBusy,
|
||||
RateLimited,
|
||||
/// The Coordinator or the peer did not prove who it is, or sent something that is not the protocol.
|
||||
Auth(String),
|
||||
Protocol(String),
|
||||
Io(String),
|
||||
Timeout,
|
||||
}
|
||||
|
||||
impl LinkError {
|
||||
/// A stable code for the interface.
|
||||
pub fn code(&self) -> &'static str {
|
||||
match self {
|
||||
LinkError::HostOffline => "host_offline",
|
||||
LinkError::HostBusy => "host_busy",
|
||||
LinkError::RateLimited => "rate_limited",
|
||||
LinkError::Auth(_) => "host_not_verified",
|
||||
LinkError::Protocol(_) => "protocol",
|
||||
LinkError::Io(_) => "unreachable",
|
||||
LinkError::Timeout => "timeout",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for LinkError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
LinkError::HostOffline => write!(f, "The server's Manager is not online right now."),
|
||||
LinkError::HostBusy => {
|
||||
write!(f, "The server's Manager is busy; try again in a moment.")
|
||||
}
|
||||
LinkError::RateLimited => write!(f, "Too many attempts; wait a minute."),
|
||||
LinkError::Auth(m) => write!(f, "The server could not be verified: {m}"),
|
||||
LinkError::Protocol(m) => write!(f, "The connection broke the protocol: {m}"),
|
||||
LinkError::Io(m) => write!(f, "The connection failed: {m}"),
|
||||
LinkError::Timeout => write!(f, "The server did not answer in time."),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for LinkError {}
|
||||
|
||||
impl From<coa_control_proto::ControlError> for LinkError {
|
||||
fn from(e: coa_control_proto::ControlError) -> Self {
|
||||
match e {
|
||||
coa_control_proto::ControlError::Auth(m) => LinkError::Auth(m),
|
||||
other => LinkError::Protocol(other.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_error_frame(code: ErrorCode, message: &str) -> LinkError {
|
||||
match code {
|
||||
ErrorCode::HostOffline | ErrorCode::UnknownRealm => LinkError::HostOffline,
|
||||
ErrorCode::HostBusy => LinkError::HostBusy,
|
||||
ErrorCode::RateLimited => LinkError::RateLimited,
|
||||
ErrorCode::Timeout => LinkError::Timeout,
|
||||
ErrorCode::BadHello => LinkError::Auth(message.to_string()),
|
||||
ErrorCode::TooLarge | ErrorCode::Internal => LinkError::Protocol(message.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
/// `http(s)://host[:port]` -> `ws(s)://host[:port]` + `path`. The Registry's address is the Coordinator's address (one front door).
|
||||
pub fn coordinator_url(registry_url: &str, path: &str) -> Option<String> {
|
||||
let base = registry_url.trim().trim_end_matches('/');
|
||||
let rest = base
|
||||
.strip_prefix("https://")
|
||||
.map(|r| format!("wss://{r}"))
|
||||
.or_else(|| base.strip_prefix("http://").map(|r| format!("ws://{r}")))?;
|
||||
Some(format!("{rest}{path}"))
|
||||
}
|
||||
|
||||
pub fn relay_url(registry_url: &str, path: &str) -> Option<String> {
|
||||
coordinator_url(registry_url, path)
|
||||
}
|
||||
|
||||
pub fn connect(url: &str, read_timeout: Duration) -> Result<Ws, LinkError> {
|
||||
let (mut ws, _) =
|
||||
tungstenite::connect(url).map_err(|e| LinkError::Io(clean(&e.to_string())))?;
|
||||
set_timeouts(&mut ws, read_timeout);
|
||||
Ok(ws)
|
||||
}
|
||||
|
||||
fn clean(s: &str) -> String {
|
||||
s.chars().take(160).collect()
|
||||
}
|
||||
|
||||
pub fn set_timeouts(ws: &mut Ws, read: Duration) {
|
||||
let tcp = match ws.get_mut() {
|
||||
MaybeTlsStream::Plain(t) => Some(&*t),
|
||||
MaybeTlsStream::NativeTls(t) => Some(t.get_ref()),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(t) = tcp {
|
||||
let _ = t.set_read_timeout(Some(read));
|
||||
let _ = t.set_write_timeout(Some(Duration::from_secs(15)));
|
||||
let _ = t.set_nodelay(true);
|
||||
}
|
||||
}
|
||||
|
||||
/// One message with the read timeout applied: `Ok(None)` when nothing arrived within it.
|
||||
pub fn poll(ws: &mut Ws) -> Result<Option<Message>, LinkError> {
|
||||
match ws.read() {
|
||||
Ok(m) => Ok(Some(m)),
|
||||
Err(tungstenite::Error::Io(e))
|
||||
if matches!(e.kind(), ErrorKind::WouldBlock | ErrorKind::TimedOut) =>
|
||||
{
|
||||
Ok(None)
|
||||
}
|
||||
Err(tungstenite::Error::ConnectionClosed | tungstenite::Error::AlreadyClosed) => {
|
||||
Err(LinkError::Io("the connection was closed".into()))
|
||||
}
|
||||
Err(e) => Err(LinkError::Io(clean(&e.to_string()))),
|
||||
}
|
||||
}
|
||||
|
||||
/// The next control frame within `total`; transport pings and pongs are skipped. An error frame from the Coordinator becomes the error.
|
||||
pub fn next_frame(ws: &mut Ws, total: Duration) -> Result<Frame, LinkError> {
|
||||
let end = std::time::Instant::now() + total;
|
||||
loop {
|
||||
if std::time::Instant::now() >= end {
|
||||
return Err(LinkError::Timeout);
|
||||
}
|
||||
match poll(ws)? {
|
||||
None => continue,
|
||||
Some(Message::Text(t)) => {
|
||||
let frame = coord::parse_text(t.as_str()).map_err(LinkError::from)?;
|
||||
if let Frame::Error { code, message } = &frame {
|
||||
return Err(from_error_frame(*code, message));
|
||||
}
|
||||
return Ok(frame);
|
||||
}
|
||||
Some(Message::Ping(_) | Message::Pong(_)) => continue,
|
||||
Some(Message::Close(_)) => {
|
||||
return Err(LinkError::Io("the connection was closed".into()))
|
||||
}
|
||||
Some(_) => {
|
||||
return Err(LinkError::Protocol(
|
||||
"a binary frame arrived where a control frame was expected".into(),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_text(ws: &mut Ws, frame: &Frame) -> Result<(), LinkError> {
|
||||
ws.send(Message::Text(coord::to_text(frame).into()))
|
||||
.map_err(|e| LinkError::Io(clean(&e.to_string())))
|
||||
}
|
||||
|
||||
pub fn send_binary(ws: &mut Ws, data: Vec<u8>) -> Result<(), LinkError> {
|
||||
ws.send(Message::Binary(data.into()))
|
||||
.map_err(|e| LinkError::Io(clean(&e.to_string())))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_coordinator_address_follows_the_registrys() {
|
||||
assert_eq!(
|
||||
coordinator_url("https://registry.example/", "/coord/v1/host").unwrap(),
|
||||
"wss://registry.example/coord/v1/host"
|
||||
);
|
||||
assert_eq!(
|
||||
coordinator_url("http://127.0.0.1:8080", "/coord/v1/player?realm=x").unwrap(),
|
||||
"ws://127.0.0.1:8080/coord/v1/player?realm=x"
|
||||
);
|
||||
assert!(
|
||||
coordinator_url("ftp://x", "/").is_none()
|
||||
&& coordinator_url("registry.example", "/").is_none()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn coordinator_refusals_become_messages_a_player_can_act_on() {
|
||||
assert_eq!(
|
||||
from_error_frame(ErrorCode::HostOffline, "").code(),
|
||||
"host_offline"
|
||||
);
|
||||
assert_eq!(
|
||||
from_error_frame(ErrorCode::UnknownRealm, "").code(),
|
||||
"host_offline"
|
||||
);
|
||||
assert_eq!(
|
||||
from_error_frame(ErrorCode::HostBusy, "").code(),
|
||||
"host_busy"
|
||||
);
|
||||
assert_eq!(
|
||||
from_error_frame(ErrorCode::RateLimited, "").code(),
|
||||
"rate_limited"
|
||||
);
|
||||
assert_eq!(
|
||||
LinkError::HostOffline.to_string(),
|
||||
"The server's Manager is not online right now."
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
//! Crash log discovery and human-friendly explanation for worldserver crashes.
|
||||
|
||||
use crate::error::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::SystemTime;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::error::Result;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct CrashItem {
|
||||
@@ -30,10 +30,7 @@ pub fn list(root: &Path, limit: usize) -> Result<Vec<CrashItem>> {
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
if path.is_file() && path.extension().and_then(|e| e.to_str()) == Some("txt") {
|
||||
let mtime = entry
|
||||
.metadata()
|
||||
.and_then(|m| m.modified())
|
||||
.unwrap_or(SystemTime::UNIX_EPOCH);
|
||||
let mtime = entry.metadata().and_then(|m| m.modified()).unwrap_or(SystemTime::UNIX_EPOCH);
|
||||
files.push((path, mtime));
|
||||
}
|
||||
}
|
||||
@@ -53,15 +50,8 @@ pub fn list(root: &Path, limit: usize) -> Result<Vec<CrashItem>> {
|
||||
|
||||
/// Parses crash report content and generates human-friendly titles and explanations.
|
||||
pub fn parse_crash_content(path: &Path, mtime: SystemTime, content: &str) -> CrashItem {
|
||||
let filename = path
|
||||
.file_name()
|
||||
.unwrap_or_default()
|
||||
.to_string_lossy()
|
||||
.into_owned();
|
||||
let timestamp_ms = mtime
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.map(|d| d.as_millis() as u64)
|
||||
.unwrap_or(0);
|
||||
let filename = path.file_name().unwrap_or_default().to_string_lossy().into_owned();
|
||||
let timestamp_ms = mtime.duration_since(SystemTime::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0);
|
||||
|
||||
let mut date_str = String::new();
|
||||
let mut exception_code = None;
|
||||
@@ -87,20 +77,11 @@ pub fn parse_crash_content(path: &Path, mtime: SystemTime, content: &str) -> Cra
|
||||
if trimmed.starts_with("# Location:") {
|
||||
let loc = trimmed.trim_start_matches("# Location:").trim();
|
||||
let clean = loc.rsplit(['\\', '/']).take(2).collect::<Vec<_>>();
|
||||
location = Some(if clean.len() == 2 {
|
||||
format!("{}/{}", clean[1], clean[0])
|
||||
} else {
|
||||
loc.to_string()
|
||||
});
|
||||
location = Some(if clean.len() == 2 { format!("{}/{}", clean[1], clean[0]) } else { loc.to_string() });
|
||||
} else if trimmed.starts_with("# Function:") {
|
||||
function = Some(trimmed.trim_start_matches("# Function:").trim().to_string());
|
||||
} else if trimmed.starts_with("# Condition:") {
|
||||
condition = Some(
|
||||
trimmed
|
||||
.trim_start_matches("# Condition:")
|
||||
.trim()
|
||||
.to_string(),
|
||||
);
|
||||
condition = Some(trimmed.trim_start_matches("# Condition:").trim().to_string());
|
||||
} else if trimmed.starts_with("#---") && condition.is_some() {
|
||||
in_assertion = false;
|
||||
}
|
||||
@@ -115,12 +96,7 @@ pub fn parse_crash_content(path: &Path, mtime: SystemTime, content: &str) -> Cra
|
||||
}
|
||||
}
|
||||
|
||||
let (category, title, explanation) = explain(
|
||||
exception_code.as_deref(),
|
||||
function.as_deref(),
|
||||
condition.as_deref(),
|
||||
content,
|
||||
);
|
||||
let (category, title, explanation) = explain(exception_code.as_deref(), function.as_deref(), condition.as_deref(), content);
|
||||
|
||||
let preview_lines: Vec<&str> = lines.iter().take(25).copied().collect();
|
||||
let preview = preview_lines.join("\n");
|
||||
@@ -141,12 +117,7 @@ pub fn parse_crash_content(path: &Path, mtime: SystemTime, content: &str) -> Cra
|
||||
}
|
||||
}
|
||||
|
||||
fn explain(
|
||||
code: Option<&str>,
|
||||
func: Option<&str>,
|
||||
cond: Option<&str>,
|
||||
full: &str,
|
||||
) -> (String, String, String) {
|
||||
fn explain(code: Option<&str>, func: Option<&str>, cond: Option<&str>, full: &str) -> (String, String, String) {
|
||||
let lower_full = full.to_lowercase();
|
||||
let lower_func = func.map(|f| f.to_lowercase()).unwrap_or_default();
|
||||
let lower_cond = cond.map(|c| c.to_lowercase()).unwrap_or_default();
|
||||
@@ -171,8 +142,7 @@ fn explain(
|
||||
return (
|
||||
"assertion".into(),
|
||||
"Ошибка в модуле ботов (Playerbots)".into(),
|
||||
"Внутренний сбой в логике ИИ ботов (экипировка, заклинание или поиск пути)."
|
||||
.into(),
|
||||
"Внутренний сбой в логике ИИ ботов (экипировка, заклинание или поиск пути).".into(),
|
||||
);
|
||||
}
|
||||
let cond_label = cond.unwrap_or("Assert condition failed");
|
||||
@@ -204,8 +174,7 @@ fn explain(
|
||||
return (
|
||||
"exception".into(),
|
||||
"Необработанное исключение C++ (C++ Exception)".into(),
|
||||
"Произошло критическое исключение C++, которое не было перехвачено ядром сервера."
|
||||
.into(),
|
||||
"Произошло критическое исключение C++, которое не было перехвачено ядром сервера.".into(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -256,19 +225,12 @@ Assertion message: #------------------------------------------------------------
|
||||
|
||||
Fault address: 00007FF7ECD3645E
|
||||
"#;
|
||||
let item = parse_crash_content(
|
||||
Path::new("dummy_worldserver.exe_[3-10_20-20-51].txt"),
|
||||
SystemTime::UNIX_EPOCH,
|
||||
report,
|
||||
);
|
||||
let item = parse_crash_content(Path::new("dummy_worldserver.exe_[3-10_20-20-51].txt"), SystemTime::UNIX_EPOCH, report);
|
||||
println!("ITEM: {item:#?}");
|
||||
assert_eq!(item.exception_code.as_deref(), Some("C0000420"));
|
||||
assert_eq!(item.category, "assertion");
|
||||
assert_eq!(item.condition.as_deref(), Some("IsInGrid()"));
|
||||
assert_eq!(
|
||||
item.function.as_deref(),
|
||||
Some("GridObject<class Player>::RemoveFromGrid")
|
||||
);
|
||||
assert_eq!(item.function.as_deref(), Some("GridObject<class Player>::RemoveFromGrid"));
|
||||
assert!(item.title.contains("Grid"));
|
||||
assert!(item.explanation.contains("сетке карты"));
|
||||
}
|
||||
|
||||
+146
-712
File diff suppressed because it is too large.
Load diff
@@ -1,43 +1,10 @@
|
||||
{
|
||||
"Ascension.exe": "e2c347019d554a59639e5978fd68e50e906973ab313c6573cc56ca82e8e3c547",
|
||||
"Data/patch-T.MPQ": "fe597138ab7f56881b5f4f27d6dc9d81bb278b483ca0d91b7207ffd4a5ae7bfd",
|
||||
"Data/patch-ZE1.MPQ": "69d79dbbea54ec0312c37770debc79c7bc9ce2e21eedef99c03cd8d911955283",
|
||||
"Data/patch-ZE2.MPQ": "1a41cae77ad730e9cf69e8d63fd12ae3a579418bb550d351fff469573a97ac3c",
|
||||
"Data/patch-ZE3.MPQ": "b587202c21b6f580682b601b7c2e905fbfa766276e74733db55c1bddbffb7f87",
|
||||
"Data/patch-ZE5.MPQ": "476b5a1fcf69f64fdb281e3b445362b936d016066ccd12f897acfcf200d6c0cf",
|
||||
"Data/patch-ZE6.MPQ": "762592c9ed950b4a7b6c7d85d45050b30fa3f35d53e348ee1e1516fdbb7e5484",
|
||||
"Data/patch-ZE7.MPQ": "de54b0189d242763d2872223419a259b6b8a4b380c24b50a2ef9c6fa44466248",
|
||||
"Data/patch-ZE8.MPQ": "1bc5691fb84a84f3e7a0a70baaa9192a97b4ec5906ee37c85838ffe08adef934",
|
||||
"Data/patch-ZE9.MPQ": "d49c94463e9548e76378ce2c0454d8f204f0745c8c0de32b6dce7f0b0fd67c4a",
|
||||
"Data/patch-ZEA.MPQ": "78fd10ec5262ae2417eba9700d2172553bbc1e5cfb0b597aada8f1753ca46d9d",
|
||||
"Data/patch-ZES.MPQ": "a2d9348f917f745c7aa25b2eed96efa312f7b2d2f62cb86a4f2eaabc2dfa895f",
|
||||
"Data/patch-ZG.MPQ": "48979e1fda775f8deb47bffa48e0a468595d4c71286fee518f51dc5801c5ca39",
|
||||
"Data/patch-ZH.MPQ": "5e1d2fe287053abba703d7af381e10096c6525ba477ffed3971c2fc4f93e919c",
|
||||
"Data/patch-ZHA.MPQ": "0fc6ebeafe1d14192dc64bafc90bde3c49230df1297f98bf65900a3bcf953617",
|
||||
"Data/patch-ZHM.MPQ": "317d3ae00f0bd674b358af07544976acb7804aa6d8b19d1788ad96030e8c9bf0",
|
||||
"Data/patch-ZJ.MPQ": "f821e99add86ef68d108c5ec466328c97677d3ed52f7944e200f3fe38c13b4de",
|
||||
"Data/patch-ZK.MPQ": "444d0ecb72c1e990509d53e3cfa5b7634c816733533e8a97e5d7f5653ee32cce",
|
||||
"Data/patch-ZM.MPQ": "424ab1978986e854551633983a94bd15beba6328aabea8c02f98eaa32f6f0b61",
|
||||
"Data/patch-ZVU.MPQ": "c679b85a2e97344395756b54d2cf6abefe4e80e74944cb55b6e573ad17b7d020",
|
||||
"Data/patch-ZZE.MPQ": "4b1e289e12c82644cb78f50170e6b5fd53918eb2de834f9e0cebddd81873a8bb",
|
||||
"Data/patch-ZZR.MPQ": "5a7575e592dc0c8a6913e0d946bbe06fa0ca2e4d817ef1f8548d3d13142bcbd3",
|
||||
"Data/patch-ZZS.MPQ": "533fd69d5784d83e872a5a28d27301dc9ab5d0cb925f8b2116926854e4a0a5b8",
|
||||
"Data/patch-ZZT.MPQ": "aef3bbe4951333d62fb55224484724fbaa9c35773a19944ffb9935e250cfb59c",
|
||||
"Data/patch-ZZU.MPQ": "8ee1839415b56d46c6eff7eb4dfbaf6fbafc4c6b29b39380d69f4d827d02a99e",
|
||||
"Data/patch-ZZV.MPQ": "8d5e61d37008abf70478e5d5f46ec7afb0490b9a643323bd75a1c70cadd63290",
|
||||
"Data/patch-ZZW.MPQ": "3689c485f8ee8fccaa6dbae1d073bc9231229f9a040f79a272d80a87486d0c33",
|
||||
"Data/patch-ZZX.MPQ": "4c12608089cfe356f7c8882d66db90239ed3ec8ddd980d91d7a3a488680f3c88",
|
||||
"Data/patch-ZZZ.MPQ": "566aaadf501af42e29a828b1bc2edbd36d436288fb309fe423bd74b9619bf593",
|
||||
"Data/patch-ZZZC.MPQ": "691a53f6325c2615a64ced0802f18bc4d96e99c62aaa606fc930f9406ee74cdf",
|
||||
"Data/patch-ZZZD.MPQ": "e4ce9d8b363eb609d0d6d92627eb53454fa2e03e4dbc1795b3573a22b7d009c8",
|
||||
"EsteriaAppearance.bin": "90d9cfaeaf6de10f014f1d1610ea10453ec9b240255759aea99513cac488a3ec",
|
||||
"EsteriaAppearance.dll": "196a82f24dd287653e2b2fc4d12bfc796f8ee3f6687ca8308905a5ab528a51a9",
|
||||
"EsteriaAppearanceGeometry.bin": "19de5b4b4f79778ea5316659efc2f66c6edb6b5149c46efa47c46bfa4ec6904c",
|
||||
"EsteriaAppearance.bin": "b3fb7c69b7600c39f888ea1925701be5667097dcb02bb6ddd830e5489306bb50",
|
||||
"EsteriaAppearance.dll": "392c5a2a25f9a69d705bdf841bb899aab768d4a3ee62734d576286c7198710d8",
|
||||
"EsteriaAppearanceGeometry.bin": "c95c53ca19c53c3434e12b99c1966f9c015ec3944c46dff7784d5e4ffc472d5d",
|
||||
"EsteriaAppearanceMaterials.bin": "f72f027f54a55c05aa0f76b457b97b484fe0e1aedc0e068cc0e0ca53e1878cba",
|
||||
"EsteriaCosmeticWings.bin": "57352c28459d073a29b3014d33b70f3b3756afa37fa9583ef6b30d503422ac3f",
|
||||
"EsteriaDrakonid.bin": "95440ee9c065ef20bb7fed6158d20623d68745291cedc2726fc746ff54601f95",
|
||||
"EsteriaDrakonidCoverage.bin": "9cdc227f08329e1245d2b864a996e7a772c96ecdbe5d81925f4d60cdbe4fa11e",
|
||||
"EsteriaDrakonidTextures.bin": "1ba9133a75dad291d98a068b2dcde01f31a897a1764cd7f3e2c316c288a8f605",
|
||||
"EsteriaEarthen.bin": "8a0db93ba2d5c756b44ed84f2f7bc7c15eef13be6a7d87baf1383c6c0c624e02",
|
||||
"EsteriaGnoll.bin": "cfacc8e681bb8e54a4f01ee9c12a91bf0bd23a23527bb548d4dc21ec1b33eea2",
|
||||
"EsteriaGnollTextures.bin": "1ba9133a75dad291d98a068b2dcde01f31a897a1764cd7f3e2c316c288a8f605",
|
||||
@@ -51,8 +18,6 @@
|
||||
"EsteriaSaberonTextures.bin": "1ba9133a75dad291d98a068b2dcde01f31a897a1764cd7f3e2c316c288a8f605",
|
||||
"EsteriaSethrak.bin": "bd14857a8c11fdf8027128763293701586b2102f9a6539d33a727014789fd606",
|
||||
"EsteriaSethrakTextures.bin": "d2b2cf58087a391f00198008fe871edd400ddf28d54a3a05674df74c5198528a",
|
||||
"EsteriaTarasek.bin": "f718444d5ec36f7aff018883486ae1981151fe6e01f5b7781d68d74e9fadfa47",
|
||||
"EsteriaTarasekTextures.bin": "1ba9133a75dad291d98a068b2dcde01f31a897a1764cd7f3e2c316c288a8f605",
|
||||
"EsteriaThinHuman.bin": "75794d192ea0c228b3da7bf7d0bdff1119f79d3f9bfef6650cd5e1a2b5d106eb",
|
||||
"EsteriaThinHumanTextures.bin": "02aeb4c05893d06125b8b58fdb7c2a56fe3d66a7d094706a44b43cb503d1731e",
|
||||
"EsteriaTuskarr.bin": "ecbe7ef490ba67eca029a57639192db61238522618491549766fe38529914387",
|
||||
@@ -61,13 +26,30 @@
|
||||
"EsteriaVrykulTextures.bin": "46098750a0e235724c6e9cc8d9d4e26550aa47bef95fd5f0f82efe5e945bf296",
|
||||
"EsteriaVulpera.bin": "c5434993b9af4a17d881965e4735cd2ac379e39c7142aac9844adff9528562d4",
|
||||
"EsteriaVulperaTextures.bin": "1ab844e74684aced2b069f1b91cd563aade45e05bd5e23f866a6c0c301cb098e",
|
||||
"Interface/AddOns/CoAHideArmor/CoAHideArmor.lua": "8c1c7403524fc9bd56c0809b4101b141da700ba163701209bf1420f2ef106aa7",
|
||||
"Interface/AddOns/CoAHideArmor/CoAHideArmor.toc": "123580d92fd5ceee258e96296f42c359b7e83cdff151af49c50c6c4e2059677c",
|
||||
"Interface/AddOns/CoAWC2Cursor/CoAWC2Cursor.lua": "1cf9537ff5f10b77250fea4407aea1008da690afe102ed6ed2dac21f79638cfa",
|
||||
"Interface/AddOns/CoAWC2Cursor/CoAWC2Cursor.toc": "48ce695dce712b3643ebe13a77e318d623c964acc69479b8fc8cc675099801bf",
|
||||
"Interface/AddOns/CoA_TalentReset/CoA_TalentReset.lua": "827d756d5948ef918fafaf8b869290b3a1faa4e4cc21b19b512a00d4509f3d41",
|
||||
"Interface/AddOns/CoA_TalentReset/CoA_TalentReset.toc": "16298c04af68e68d6445e7a11f42aee525dd4145f85d2771ff434fb7c6e95a85",
|
||||
"dinput8.dll": "f8de78606c241fb3911dce22faf68f5b9f9885b142b183c1e2c09ddc8f7e380c",
|
||||
"wc2_human.cur": "f609736e29596bb386ef1597b157d211e01a86d32c2a929e256c581c37d71791",
|
||||
"wc2_orc.cur": "baa512c9b9045762ba35b6e0d35b8f79c54b7649f35cc9c0e0cc8c168a1e5a97"
|
||||
"Data/patch-T.MPQ": "fe597138ab7f56881b5f4f27d6dc9d81bb278b483ca0d91b7207ffd4a5ae7bfd",
|
||||
"Data/patch-ZE5.MPQ": "476b5a1fcf69f64fdb281e3b445362b936d016066ccd12f897acfcf200d6c0cf",
|
||||
"Data/patch-ZE6.MPQ": "762592c9ed950b4a7b6c7d85d45050b30fa3f35d53e348ee1e1516fdbb7e5484",
|
||||
"Data/patch-ZE7.MPQ": "de54b0189d242763d2872223419a259b6b8a4b380c24b50a2ef9c6fa44466248",
|
||||
"Data/patch-ZE8.MPQ": "1bc5691fb84a84f3e7a0a70baaa9192a97b4ec5906ee37c85838ffe08adef934",
|
||||
"Data/patch-ZE9.MPQ": "d49c94463e9548e76378ce2c0454d8f204f0745c8c0de32b6dce7f0b0fd67c4a",
|
||||
"Data/patch-ZES.MPQ": "a2d9348f917f745c7aa25b2eed96efa312f7b2d2f62cb86a4f2eaabc2dfa895f",
|
||||
"Data/patch-ZG.MPQ": "48979e1fda775f8deb47bffa48e0a468595d4c71286fee518f51dc5801c5ca39",
|
||||
"Data/patch-ZH.MPQ": "5e1d2fe287053abba703d7af381e10096c6525ba477ffed3971c2fc4f93e919c",
|
||||
"Data/patch-ZHM.MPQ": "317d3ae00f0bd674b358af07544976acb7804aa6d8b19d1788ad96030e8c9bf0",
|
||||
"Data/patch-ZJ.MPQ": "f821e99add86ef68d108c5ec466328c97677d3ed52f7944e200f3fe38c13b4de",
|
||||
"Data/patch-ZM.MPQ": "424ab1978986e854551633983a94bd15beba6328aabea8c02f98eaa32f6f0b61",
|
||||
"Data/patch-ZVU.MPQ": "c679b85a2e97344395756b54d2cf6abefe4e80e74944cb55b6e573ad17b7d020",
|
||||
"Data/patch-ZE1.MPQ": "69d79dbbea54ec0312c37770debc79c7bc9ce2e21eedef99c03cd8d911955283",
|
||||
"Data/patch-ZE2.MPQ": "1a41cae77ad730e9cf69e8d63fd12ae3a579418bb550d351fff469573a97ac3c",
|
||||
"Data/patch-ZE3.MPQ": "b587202c21b6f580682b601b7c2e905fbfa766276e74733db55c1bddbffb7f87",
|
||||
"Data/patch-ZEA.MPQ": "78fd10ec5262ae2417eba9700d2172553bbc1e5cfb0b597aada8f1753ca46d9d",
|
||||
"Data/patch-ZHA.MPQ": "0fc6ebeafe1d14192dc64bafc90bde3c49230df1297f98bf65900a3bcf953617",
|
||||
"Data/patch-ZZE.MPQ": "4b1e289e12c82644cb78f50170e6b5fd53918eb2de834f9e0cebddd81873a8bb",
|
||||
"Data/patch-ZZR.MPQ": "5a7575e592dc0c8a6913e0d946bbe06fa0ca2e4d817ef1f8548d3d13142bcbd3",
|
||||
"Data/patch-ZZS.MPQ": "533fd69d5784d83e872a5a28d27301dc9ab5d0cb925f8b2116926854e4a0a5b8",
|
||||
"Data/patch-ZZT.MPQ": "aef3bbe4951333d62fb55224484724fbaa9c35773a19944ffb9935e250cfb59c",
|
||||
"Data/patch-ZZU.MPQ": "8ee1839415b56d46c6eff7eb4dfbaf6fbafc4c6b29b39380d69f4d827d02a99e",
|
||||
"Data/patch-ZZV.MPQ": "8d5e61d37008abf70478e5d5f46ec7afb0490b9a643323bd75a1c70cadd63290",
|
||||
"Data/patch-ZZW.MPQ": "ed452a0a0118c9f4deaf02db753064ac95b6e4a84022bdabd39a7e70d775223f"
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
{
|
||||
"Ascension.exe": "e2c347019d554a59639e5978fd68e50e906973ab313c6573cc56ca82e8e3c547",
|
||||
"EsteriaAppearance.bin": "b3fb7c69b7600c39f888ea1925701be5667097dcb02bb6ddd830e5489306bb50",
|
||||
"EsteriaAppearance.dll": "392c5a2a25f9a69d705bdf841bb899aab768d4a3ee62734d576286c7198710d8",
|
||||
"EsteriaAppearanceGeometry.bin": "c95c53ca19c53c3434e12b99c1966f9c015ec3944c46dff7784d5e4ffc472d5d",
|
||||
"EsteriaAppearanceMaterials.bin": "f72f027f54a55c05aa0f76b457b97b484fe0e1aedc0e068cc0e0ca53e1878cba",
|
||||
"EsteriaCosmeticWings.bin": "57352c28459d073a29b3014d33b70f3b3756afa37fa9583ef6b30d503422ac3f",
|
||||
"EsteriaEarthen.bin": "8a0db93ba2d5c756b44ed84f2f7bc7c15eef13be6a7d87baf1383c6c0c624e02",
|
||||
"EsteriaGnoll.bin": "cfacc8e681bb8e54a4f01ee9c12a91bf0bd23a23527bb548d4dc21ec1b33eea2",
|
||||
"EsteriaGnollTextures.bin": "1ba9133a75dad291d98a068b2dcde01f31a897a1764cd7f3e2c316c288a8f605",
|
||||
"EsteriaHaranir.bin": "59f31d00e372fd2646e1cf0d9d3d5d671b31edd60ff7b862f8d1ce7e53ba1f2f",
|
||||
"EsteriaHaranirTextures.bin": "99948ca570be2d376f0c67e3b8088739b6fa9f9d2c73985e19f68df77689a229",
|
||||
"EsteriaHighmountain.bin": "e83349252ad2687ac23fcfc5623db7401cf32e5faf47a5a7be20c2f94b014938",
|
||||
"EsteriaNaga.bin": "e87547cff5d09500c294f8b06935d134cdd0bbb962e294a72de09404505bd8b3",
|
||||
"EsteriaNagaTextures.bin": "57ec560f99c1d48ac1e8f6a797c156513b99e7d349e3071afe405f812d159ae3",
|
||||
"EsteriaSaberon.bin": "7735c9325c807c5125760d87f323d6b7d4162fb542c1d8b5f435ea2d6b9a4daa",
|
||||
"EsteriaSaberonCoverage.bin": "9cdc227f08329e1245d2b864a996e7a772c96ecdbe5d81925f4d60cdbe4fa11e",
|
||||
"EsteriaSaberonTextures.bin": "1ba9133a75dad291d98a068b2dcde01f31a897a1764cd7f3e2c316c288a8f605",
|
||||
"EsteriaSethrak.bin": "bd14857a8c11fdf8027128763293701586b2102f9a6539d33a727014789fd606",
|
||||
"EsteriaSethrakTextures.bin": "d2b2cf58087a391f00198008fe871edd400ddf28d54a3a05674df74c5198528a",
|
||||
"EsteriaThinHuman.bin": "75794d192ea0c228b3da7bf7d0bdff1119f79d3f9bfef6650cd5e1a2b5d106eb",
|
||||
"EsteriaThinHumanTextures.bin": "02aeb4c05893d06125b8b58fdb7c2a56fe3d66a7d094706a44b43cb503d1731e",
|
||||
"EsteriaTuskarr.bin": "ecbe7ef490ba67eca029a57639192db61238522618491549766fe38529914387",
|
||||
"EsteriaTuskarrTextures.bin": "e2116c2c83adea079126fd5da765d20da24f044d4f161c4124e4368cb3d6bc71",
|
||||
"EsteriaVrykul.bin": "39e2b7b4c67997dbbd34bae840bd606a59290f7f2fc82565a0ba00090335415c",
|
||||
"EsteriaVrykulTextures.bin": "46098750a0e235724c6e9cc8d9d4e26550aa47bef95fd5f0f82efe5e945bf296",
|
||||
"EsteriaVulpera.bin": "c5434993b9af4a17d881965e4735cd2ac379e39c7142aac9844adff9528562d4",
|
||||
"EsteriaVulperaTextures.bin": "1ab844e74684aced2b069f1b91cd563aade45e05bd5e23f866a6c0c301cb098e",
|
||||
"dinput8.dll": "f8de78606c241fb3911dce22faf68f5b9f9885b142b183c1e2c09ddc8f7e380c",
|
||||
"Data/patch-T.MPQ": "fe597138ab7f56881b5f4f27d6dc9d81bb278b483ca0d91b7207ffd4a5ae7bfd",
|
||||
"Data/patch-ZE5.MPQ": "476b5a1fcf69f64fdb281e3b445362b936d016066ccd12f897acfcf200d6c0cf",
|
||||
"Data/patch-ZE6.MPQ": "762592c9ed950b4a7b6c7d85d45050b30fa3f35d53e348ee1e1516fdbb7e5484",
|
||||
"Data/patch-ZE7.MPQ": "de54b0189d242763d2872223419a259b6b8a4b380c24b50a2ef9c6fa44466248",
|
||||
"Data/patch-ZE8.MPQ": "1bc5691fb84a84f3e7a0a70baaa9192a97b4ec5906ee37c85838ffe08adef934",
|
||||
"Data/patch-ZE9.MPQ": "d49c94463e9548e76378ce2c0454d8f204f0745c8c0de32b6dce7f0b0fd67c4a",
|
||||
"Data/patch-ZES.MPQ": "a2d9348f917f745c7aa25b2eed96efa312f7b2d2f62cb86a4f2eaabc2dfa895f",
|
||||
"Data/patch-ZG.MPQ": "48979e1fda775f8deb47bffa48e0a468595d4c71286fee518f51dc5801c5ca39",
|
||||
"Data/patch-ZH.MPQ": "5e1d2fe287053abba703d7af381e10096c6525ba477ffed3971c2fc4f93e919c",
|
||||
"Data/patch-ZHM.MPQ": "317d3ae00f0bd674b358af07544976acb7804aa6d8b19d1788ad96030e8c9bf0",
|
||||
"Data/patch-ZJ.MPQ": "f821e99add86ef68d108c5ec466328c97677d3ed52f7944e200f3fe38c13b4de",
|
||||
"Data/patch-ZM.MPQ": "424ab1978986e854551633983a94bd15beba6328aabea8c02f98eaa32f6f0b61",
|
||||
"Data/patch-ZVU.MPQ": "c679b85a2e97344395756b54d2cf6abefe4e80e74944cb55b6e573ad17b7d020",
|
||||
"Data/patch-ZE1.MPQ": "69d79dbbea54ec0312c37770debc79c7bc9ce2e21eedef99c03cd8d911955283",
|
||||
"Data/patch-ZE2.MPQ": "1a41cae77ad730e9cf69e8d63fd12ae3a579418bb550d351fff469573a97ac3c",
|
||||
"Data/patch-ZE3.MPQ": "b587202c21b6f580682b601b7c2e905fbfa766276e74733db55c1bddbffb7f87",
|
||||
"Data/patch-ZEA.MPQ": "78fd10ec5262ae2417eba9700d2172553bbc1e5cfb0b597aada8f1753ca46d9d",
|
||||
"Data/patch-ZHA.MPQ": "0fc6ebeafe1d14192dc64bafc90bde3c49230df1297f98bf65900a3bcf953617",
|
||||
"Data/patch-ZZE.MPQ": "4b1e289e12c82644cb78f50170e6b5fd53918eb2de834f9e0cebddd81873a8bb",
|
||||
"Data/patch-ZZR.MPQ": "5a7575e592dc0c8a6913e0d946bbe06fa0ca2e4d817ef1f8548d3d13142bcbd3",
|
||||
"Data/patch-ZZS.MPQ": "533fd69d5784d83e872a5a28d27301dc9ab5d0cb925f8b2116926854e4a0a5b8",
|
||||
"Data/patch-ZZT.MPQ": "aef3bbe4951333d62fb55224484724fbaa9c35773a19944ffb9935e250cfb59c",
|
||||
"Data/patch-ZZU.MPQ": "8ee1839415b56d46c6eff7eb4dfbaf6fbafc4c6b29b39380d69f4d827d02a99e",
|
||||
"Data/patch-ZZV.MPQ": "8d5e61d37008abf70478e5d5f46ec7afb0490b9a643323bd75a1c70cadd63290",
|
||||
"Data/patch-ZZW.MPQ": "ed452a0a0118c9f4deaf02db753064ac95b6e4a84022bdabd39a7e70d775223f"
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
{
|
||||
"Ascension.exe": "e2c347019d554a59639e5978fd68e50e906973ab313c6573cc56ca82e8e3c547",
|
||||
"Data/patch-T.MPQ": "fe597138ab7f56881b5f4f27d6dc9d81bb278b483ca0d91b7207ffd4a5ae7bfd",
|
||||
"Data/patch-ZE1.MPQ": "69d79dbbea54ec0312c37770debc79c7bc9ce2e21eedef99c03cd8d911955283",
|
||||
"Data/patch-ZE2.MPQ": "1a41cae77ad730e9cf69e8d63fd12ae3a579418bb550d351fff469573a97ac3c",
|
||||
"Data/patch-ZE3.MPQ": "b587202c21b6f580682b601b7c2e905fbfa766276e74733db55c1bddbffb7f87",
|
||||
"Data/patch-ZE5.MPQ": "476b5a1fcf69f64fdb281e3b445362b936d016066ccd12f897acfcf200d6c0cf",
|
||||
"Data/patch-ZE6.MPQ": "762592c9ed950b4a7b6c7d85d45050b30fa3f35d53e348ee1e1516fdbb7e5484",
|
||||
"Data/patch-ZE7.MPQ": "de54b0189d242763d2872223419a259b6b8a4b380c24b50a2ef9c6fa44466248",
|
||||
"Data/patch-ZE8.MPQ": "1bc5691fb84a84f3e7a0a70baaa9192a97b4ec5906ee37c85838ffe08adef934",
|
||||
"Data/patch-ZE9.MPQ": "d49c94463e9548e76378ce2c0454d8f204f0745c8c0de32b6dce7f0b0fd67c4a",
|
||||
"Data/patch-ZEA.MPQ": "78fd10ec5262ae2417eba9700d2172553bbc1e5cfb0b597aada8f1753ca46d9d",
|
||||
"Data/patch-ZES.MPQ": "a2d9348f917f745c7aa25b2eed96efa312f7b2d2f62cb86a4f2eaabc2dfa895f",
|
||||
"Data/patch-ZG.MPQ": "48979e1fda775f8deb47bffa48e0a468595d4c71286fee518f51dc5801c5ca39",
|
||||
"Data/patch-ZH.MPQ": "5e1d2fe287053abba703d7af381e10096c6525ba477ffed3971c2fc4f93e919c",
|
||||
"Data/patch-ZHA.MPQ": "0fc6ebeafe1d14192dc64bafc90bde3c49230df1297f98bf65900a3bcf953617",
|
||||
"Data/patch-ZHM.MPQ": "317d3ae00f0bd674b358af07544976acb7804aa6d8b19d1788ad96030e8c9bf0",
|
||||
"Data/patch-ZJ.MPQ": "f821e99add86ef68d108c5ec466328c97677d3ed52f7944e200f3fe38c13b4de",
|
||||
"Data/patch-ZM.MPQ": "424ab1978986e854551633983a94bd15beba6328aabea8c02f98eaa32f6f0b61",
|
||||
"Data/patch-ZVU.MPQ": "c679b85a2e97344395756b54d2cf6abefe4e80e74944cb55b6e573ad17b7d020",
|
||||
"Data/patch-ZZE.MPQ": "4b1e289e12c82644cb78f50170e6b5fd53918eb2de834f9e0cebddd81873a8bb",
|
||||
"Data/patch-ZZR.MPQ": "5a7575e592dc0c8a6913e0d946bbe06fa0ca2e4d817ef1f8548d3d13142bcbd3",
|
||||
"Data/patch-ZZS.MPQ": "533fd69d5784d83e872a5a28d27301dc9ab5d0cb925f8b2116926854e4a0a5b8",
|
||||
"Data/patch-ZZT.MPQ": "aef3bbe4951333d62fb55224484724fbaa9c35773a19944ffb9935e250cfb59c",
|
||||
"Data/patch-ZZU.MPQ": "8ee1839415b56d46c6eff7eb4dfbaf6fbafc4c6b29b39380d69f4d827d02a99e",
|
||||
"Data/patch-ZZV.MPQ": "8d5e61d37008abf70478e5d5f46ec7afb0490b9a643323bd75a1c70cadd63290",
|
||||
"Data/patch-ZZW.MPQ": "3689c485f8ee8fccaa6dbae1d073bc9231229f9a040f79a272d80a87486d0c33",
|
||||
"Data/patch-ZZZ.MPQ": "566aaadf501af42e29a828b1bc2edbd36d436288fb309fe423bd74b9619bf593",
|
||||
"EsteriaAppearance.bin": "90d9cfaeaf6de10f014f1d1610ea10453ec9b240255759aea99513cac488a3ec",
|
||||
"EsteriaAppearance.dll": "64564aceac38b26faefbd51dba779fa338ab5e70a2a2509f6c8f3a991ac98518",
|
||||
"EsteriaAppearanceGeometry.bin": "fdc1cbba90b66c4533a66365d6d555c2d32ab01acb014fff042b4a6a9c6b6ddc",
|
||||
"EsteriaAppearanceMaterials.bin": "f72f027f54a55c05aa0f76b457b97b484fe0e1aedc0e068cc0e0ca53e1878cba",
|
||||
"EsteriaCosmeticWings.bin": "57352c28459d073a29b3014d33b70f3b3756afa37fa9583ef6b30d503422ac3f",
|
||||
"EsteriaDrakonid.bin": "95440ee9c065ef20bb7fed6158d20623d68745291cedc2726fc746ff54601f95",
|
||||
"EsteriaDrakonidCoverage.bin": "9cdc227f08329e1245d2b864a996e7a772c96ecdbe5d81925f4d60cdbe4fa11e",
|
||||
"EsteriaDrakonidTextures.bin": "1ba9133a75dad291d98a068b2dcde01f31a897a1764cd7f3e2c316c288a8f605",
|
||||
"EsteriaEarthen.bin": "8a0db93ba2d5c756b44ed84f2f7bc7c15eef13be6a7d87baf1383c6c0c624e02",
|
||||
"EsteriaGnoll.bin": "cfacc8e681bb8e54a4f01ee9c12a91bf0bd23a23527bb548d4dc21ec1b33eea2",
|
||||
"EsteriaGnollTextures.bin": "1ba9133a75dad291d98a068b2dcde01f31a897a1764cd7f3e2c316c288a8f605",
|
||||
"EsteriaHaranir.bin": "59f31d00e372fd2646e1cf0d9d3d5d671b31edd60ff7b862f8d1ce7e53ba1f2f",
|
||||
"EsteriaHaranirTextures.bin": "99948ca570be2d376f0c67e3b8088739b6fa9f9d2c73985e19f68df77689a229",
|
||||
"EsteriaHighmountain.bin": "e83349252ad2687ac23fcfc5623db7401cf32e5faf47a5a7be20c2f94b014938",
|
||||
"EsteriaNaga.bin": "e87547cff5d09500c294f8b06935d134cdd0bbb962e294a72de09404505bd8b3",
|
||||
"EsteriaNagaTextures.bin": "57ec560f99c1d48ac1e8f6a797c156513b99e7d349e3071afe405f812d159ae3",
|
||||
"EsteriaSaberon.bin": "7735c9325c807c5125760d87f323d6b7d4162fb542c1d8b5f435ea2d6b9a4daa",
|
||||
"EsteriaSaberonCoverage.bin": "9cdc227f08329e1245d2b864a996e7a772c96ecdbe5d81925f4d60cdbe4fa11e",
|
||||
"EsteriaSaberonTextures.bin": "1ba9133a75dad291d98a068b2dcde01f31a897a1764cd7f3e2c316c288a8f605",
|
||||
"EsteriaSethrak.bin": "bd14857a8c11fdf8027128763293701586b2102f9a6539d33a727014789fd606",
|
||||
"EsteriaSethrakTextures.bin": "d2b2cf58087a391f00198008fe871edd400ddf28d54a3a05674df74c5198528a",
|
||||
"EsteriaTarasek.bin": "f718444d5ec36f7aff018883486ae1981151fe6e01f5b7781d68d74e9fadfa47",
|
||||
"EsteriaTarasekTextures.bin": "1ba9133a75dad291d98a068b2dcde01f31a897a1764cd7f3e2c316c288a8f605",
|
||||
"EsteriaThinHuman.bin": "75794d192ea0c228b3da7bf7d0bdff1119f79d3f9bfef6650cd5e1a2b5d106eb",
|
||||
"EsteriaThinHumanTextures.bin": "02aeb4c05893d06125b8b58fdb7c2a56fe3d66a7d094706a44b43cb503d1731e",
|
||||
"EsteriaTuskarr.bin": "ecbe7ef490ba67eca029a57639192db61238522618491549766fe38529914387",
|
||||
"EsteriaTuskarrTextures.bin": "e2116c2c83adea079126fd5da765d20da24f044d4f161c4124e4368cb3d6bc71",
|
||||
"EsteriaVrykul.bin": "39e2b7b4c67997dbbd34bae840bd606a59290f7f2fc82565a0ba00090335415c",
|
||||
"EsteriaVrykulTextures.bin": "46098750a0e235724c6e9cc8d9d4e26550aa47bef95fd5f0f82efe5e945bf296",
|
||||
"EsteriaVulpera.bin": "c5434993b9af4a17d881965e4735cd2ac379e39c7142aac9844adff9528562d4",
|
||||
"EsteriaVulperaTextures.bin": "1ab844e74684aced2b069f1b91cd563aade45e05bd5e23f866a6c0c301cb098e",
|
||||
"Interface/AddOns/CoAWC2Cursor/CoAWC2Cursor.lua": "1cf9537ff5f10b77250fea4407aea1008da690afe102ed6ed2dac21f79638cfa",
|
||||
"Interface/AddOns/CoAWC2Cursor/CoAWC2Cursor.toc": "48ce695dce712b3643ebe13a77e318d623c964acc69479b8fc8cc675099801bf",
|
||||
"dinput8.dll": "f8de78606c241fb3911dce22faf68f5b9f9885b142b183c1e2c09ddc8f7e380c",
|
||||
"wc2_human.cur": "f609736e29596bb386ef1597b157d211e01a86d32c2a929e256c581c37d71791",
|
||||
"wc2_orc.cur": "baa512c9b9045762ba35b6e0d35b8f79c54b7649f35cc9c0e0cc8c168a1e5a97"
|
||||
}
|
||||
@@ -57,12 +57,7 @@ fn read_marker(root: &Path) -> Option<Marker> {
|
||||
|
||||
/// `v1.9.1` -> `[1, 9, 1]`.
|
||||
fn parse_tag(tag: &str) -> Option<Vec<u32>> {
|
||||
let parts: Vec<u32> = tag
|
||||
.trim()
|
||||
.trim_start_matches('v')
|
||||
.split('.')
|
||||
.map(|p| p.parse().ok())
|
||||
.collect::<Option<_>>()?;
|
||||
let parts: Vec<u32> = tag.trim().trim_start_matches('v').split('.').map(|p| p.parse().ok()).collect::<Option<_>>()?;
|
||||
(parts.len() >= 2).then_some(parts)
|
||||
}
|
||||
|
||||
@@ -73,25 +68,18 @@ fn same_series(a: &str, b: &str) -> bool {
|
||||
|
||||
/// The newest dashboard release of the bots' series (`v1.9` serves bots `v1.9` and `v1.9.1`).
|
||||
fn pick_tag(squid_tag: &str, available: &[String]) -> Option<String> {
|
||||
available
|
||||
.iter()
|
||||
available.iter()
|
||||
.filter(|tag| same_series(tag, squid_tag))
|
||||
.max_by_key(|tag| parse_tag(tag).unwrap_or_default())
|
||||
.cloned()
|
||||
}
|
||||
|
||||
fn safe_tag(tag: &str) -> bool {
|
||||
!tag.is_empty()
|
||||
&& tag.len() <= 40
|
||||
&& tag
|
||||
.chars()
|
||||
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'))
|
||||
!tag.is_empty() && tag.len() <= 40 && tag.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'))
|
||||
}
|
||||
|
||||
fn squid_tag(root: &Path) -> Option<String> {
|
||||
crate::squid::release(root)
|
||||
.and_then(|r| r.tag)
|
||||
.filter(|t| safe_tag(t))
|
||||
crate::squid::release(root).and_then(|r| r.tag).filter(|t| safe_tag(t))
|
||||
}
|
||||
|
||||
fn record_alive(root: &Path) -> Option<ProcessIdentity> {
|
||||
@@ -105,12 +93,7 @@ pub fn status(root: &Path) -> Status {
|
||||
let installed = dir(root).join("squidbots.py").is_file() && marker.is_some();
|
||||
let squid = squid_tag(root);
|
||||
let port = marker.as_ref().map(|m| m.port).unwrap_or(FIRST_PORT);
|
||||
let running = installed
|
||||
&& record_alive(root).is_some_and(|r| {
|
||||
process::listeners()
|
||||
.iter()
|
||||
.any(|(p, pid)| *p == port && *pid == r.pid)
|
||||
});
|
||||
let running = installed && record_alive(root).is_some_and(|r| process::listeners().iter().any(|(p, pid)| *p == port && *pid == r.pid));
|
||||
Status {
|
||||
installed,
|
||||
matches: matches!((&marker, &squid), (Some(m), Some(s)) if same_series(&m.tag, s)),
|
||||
@@ -136,28 +119,11 @@ fn download_error(e: impl std::fmt::Display) -> Error {
|
||||
|
||||
fn available_tags(client: &reqwest::blocking::Client) -> Result<Vec<String>> {
|
||||
#[derive(Deserialize)]
|
||||
struct Tag {
|
||||
name: String,
|
||||
}
|
||||
let response = client
|
||||
.get(format!(
|
||||
"https://api.github.com/repos/{REPO}/tags?per_page=100"
|
||||
))
|
||||
.send()
|
||||
.map_err(download_error)?;
|
||||
if !response.status().is_success() {
|
||||
return Err(download_error(format!(
|
||||
"GitHub answered {}",
|
||||
response.status()
|
||||
)));
|
||||
}
|
||||
let tags: Vec<Tag> =
|
||||
serde_json::from_str(&response.text().map_err(download_error)?).map_err(download_error)?;
|
||||
Ok(tags
|
||||
.into_iter()
|
||||
.map(|t| t.name)
|
||||
.filter(|t| safe_tag(t))
|
||||
.collect())
|
||||
struct Tag { name: String }
|
||||
let response = client.get(format!("https://api.github.com/repos/{REPO}/tags?per_page=100")).send().map_err(download_error)?;
|
||||
if !response.status().is_success() { return Err(download_error(format!("GitHub answered {}", response.status()))); }
|
||||
let tags: Vec<Tag> = serde_json::from_str(&response.text().map_err(download_error)?).map_err(download_error)?;
|
||||
Ok(tags.into_iter().map(|t| t.name).filter(|t| safe_tag(t)).collect())
|
||||
}
|
||||
|
||||
/// Download the dashboard release that matches the installed bots and unpack it. A running dashboard is stopped first;
|
||||
@@ -166,99 +132,45 @@ pub fn install(root: &Path, report: &dyn Fn(&str)) -> Result<Status> {
|
||||
let squid = squid_tag(root).ok_or_else(|| Error::Invalid("The version of the SQUID bots could not be read, so the matching dashboard cannot be chosen.".into()))?;
|
||||
report("Looking for the matching dashboard");
|
||||
let client = http()?;
|
||||
let tag = pick_tag(&squid, &available_tags(&client)?).ok_or_else(|| {
|
||||
Error::Invalid(format!(
|
||||
"There is no dashboard release for SQUID bots {squid} yet."
|
||||
))
|
||||
})?;
|
||||
let tag = pick_tag(&squid, &available_tags(&client)?)
|
||||
.ok_or_else(|| Error::Invalid(format!("There is no dashboard release for SQUID bots {squid} yet.")))?;
|
||||
report("Downloading the dashboard");
|
||||
let response = client
|
||||
.get(format!(
|
||||
"https://codeload.github.com/{REPO}/zip/refs/tags/{tag}"
|
||||
))
|
||||
.send()
|
||||
.map_err(download_error)?;
|
||||
if !response.status().is_success() {
|
||||
return Err(download_error(format!(
|
||||
"GitHub answered {}",
|
||||
response.status()
|
||||
)));
|
||||
}
|
||||
let response = client.get(format!("https://codeload.github.com/{REPO}/zip/refs/tags/{tag}")).send().map_err(download_error)?;
|
||||
if !response.status().is_success() { return Err(download_error(format!("GitHub answered {}", response.status()))); }
|
||||
let mut bytes = Vec::new();
|
||||
response
|
||||
.take(MAX_ARCHIVE + 1)
|
||||
.read_to_end(&mut bytes)
|
||||
.map_err(download_error)?;
|
||||
if bytes.len() as u64 > MAX_ARCHIVE {
|
||||
return Err(Error::Invalid(
|
||||
"The dashboard download is larger than expected and was rejected.".into(),
|
||||
));
|
||||
}
|
||||
response.take(MAX_ARCHIVE + 1).read_to_end(&mut bytes).map_err(download_error)?;
|
||||
if bytes.len() as u64 > MAX_ARCHIVE { return Err(Error::Invalid("The dashboard download is larger than expected and was rejected.".into())); }
|
||||
|
||||
stop(root)?;
|
||||
report("Unpacking the dashboard");
|
||||
let target = dir(root);
|
||||
std::fs::create_dir_all(&target)?;
|
||||
let mut archive = zip::ZipArchive::new(std::io::Cursor::new(bytes))
|
||||
.map_err(|e| Error::Invalid(format!("The dashboard download is damaged: {e}")))?;
|
||||
let mut archive = zip::ZipArchive::new(std::io::Cursor::new(bytes)).map_err(|e| Error::Invalid(format!("The dashboard download is damaged: {e}")))?;
|
||||
let mut total = 0u64;
|
||||
let mut wrote_main = false;
|
||||
for index in 0..archive.len() {
|
||||
let mut file = archive
|
||||
.by_index(index)
|
||||
.map_err(|e| Error::Invalid(format!("The dashboard download is damaged: {e}")))?;
|
||||
if file.is_dir() {
|
||||
continue;
|
||||
}
|
||||
let mut file = archive.by_index(index).map_err(|e| Error::Invalid(format!("The dashboard download is damaged: {e}")))?;
|
||||
if file.is_dir() { continue; }
|
||||
// GitHub wraps everything in one folder named after the repository and release.
|
||||
let Some(relative) = file
|
||||
.name()
|
||||
.split_once('/')
|
||||
.map(|(_, rest)| rest.to_string())
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
if relative.is_empty() || relative.starts_with(".github/") || relative.starts_with("docs/")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let Some(relative) = file.name().split_once('/').map(|(_, rest)| rest.to_string()) else { continue; };
|
||||
if relative.is_empty() || relative.starts_with(".github/") || relative.starts_with("docs/") { continue; }
|
||||
total += file.size();
|
||||
if total > MAX_UNPACKED {
|
||||
return Err(Error::Invalid(
|
||||
"The dashboard download unpacks to more than expected and was rejected.".into(),
|
||||
));
|
||||
}
|
||||
if total > MAX_UNPACKED { return Err(Error::Invalid("The dashboard download unpacks to more than expected and was rejected.".into())); }
|
||||
let destination = fsx::safe_join(&target, &relative)?;
|
||||
let mut content = Vec::with_capacity(file.size() as usize);
|
||||
file.read_to_end(&mut content)?;
|
||||
fsx::atomic_write(&destination, &content)?;
|
||||
wrote_main |= relative == "squidbots.py";
|
||||
}
|
||||
if !wrote_main {
|
||||
return Err(Error::Invalid(
|
||||
"The dashboard download did not contain the dashboard.".into(),
|
||||
));
|
||||
}
|
||||
if !wrote_main { return Err(Error::Invalid("The dashboard download did not contain the dashboard.".into())); }
|
||||
let port = read_marker(root).map(|m| m.port).unwrap_or(FIRST_PORT);
|
||||
fsx::atomic_write_json(
|
||||
&target.join(MARKER),
|
||||
&Marker {
|
||||
tag,
|
||||
squid_tag: squid,
|
||||
port,
|
||||
},
|
||||
)?;
|
||||
fsx::atomic_write_json(&target.join(MARKER), &Marker { tag, squid_tag: squid, port })?;
|
||||
Ok(status(root))
|
||||
}
|
||||
|
||||
fn free_port(preferred: u16) -> Result<u16> {
|
||||
let used: Vec<u16> = process::listeners()
|
||||
.into_iter()
|
||||
.map(|(port, _)| port)
|
||||
.collect();
|
||||
std::iter::once(preferred)
|
||||
.chain(FIRST_PORT..FIRST_PORT + 40)
|
||||
.find(|p| !used.contains(p))
|
||||
let used: Vec<u16> = process::listeners().into_iter().map(|(port, _)| port).collect();
|
||||
std::iter::once(preferred).chain(FIRST_PORT..FIRST_PORT + 40).find(|p| !used.contains(p))
|
||||
.ok_or_else(|| Error::Invalid("No free port was found for the dashboard.".into()))
|
||||
}
|
||||
|
||||
@@ -268,24 +180,13 @@ fn free_port(preferred: u16) -> Result<u16> {
|
||||
fn discard_damaged_data(folder: &Path) -> Vec<String> {
|
||||
const SHIPPED: &[&str] = &["worldmap.json", "dashboard.example.json"];
|
||||
let mut removed = Vec::new();
|
||||
let Ok(entries) = std::fs::read_dir(folder) else {
|
||||
return removed;
|
||||
};
|
||||
let Ok(entries) = std::fs::read_dir(folder) else { return removed; };
|
||||
for entry in entries.flatten() {
|
||||
let name = entry.file_name().to_string_lossy().into_owned();
|
||||
let path = entry.path();
|
||||
if !name.ends_with(".json")
|
||||
|| name.starts_with('.')
|
||||
|| SHIPPED.contains(&name.as_str())
|
||||
|| !path.is_file()
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let Ok(bytes) = std::fs::read(&path) else {
|
||||
continue;
|
||||
};
|
||||
if !bytes.is_empty() && bytes.iter().all(|b| *b == 0) && std::fs::remove_file(&path).is_ok()
|
||||
{
|
||||
if !name.ends_with(".json") || name.starts_with('.') || SHIPPED.contains(&name.as_str()) || !path.is_file() { continue; }
|
||||
let Ok(bytes) = std::fs::read(&path) else { continue; };
|
||||
if !bytes.is_empty() && bytes.iter().all(|b| *b == 0) && std::fs::remove_file(&path).is_ok() {
|
||||
tracing::warn!(file = %path.display(), "dashboard: removed a data file damaged by an interrupted write");
|
||||
removed.push(name);
|
||||
}
|
||||
@@ -297,13 +198,9 @@ fn discard_damaged_data(folder: &Path) -> Vec<String> {
|
||||
/// ships it, the GitHub release does not. The server's own reference file has the same content and shape.
|
||||
fn ensure_builds_file(root: &Path) {
|
||||
let target = dir(root).join("coa-level-builds.json");
|
||||
if target.is_file() {
|
||||
return;
|
||||
}
|
||||
if target.is_file() { return; }
|
||||
let reference = root.join("Core/reference/ascensionsidekick-level-builds.json");
|
||||
let content = std::fs::read(&reference).ok().filter(|bytes| {
|
||||
serde_json::from_slice::<serde_json::Value>(bytes).is_ok_and(|v| v.is_object())
|
||||
});
|
||||
let content = std::fs::read(&reference).ok().filter(|bytes| serde_json::from_slice::<serde_json::Value>(bytes).is_ok_and(|v| v.is_object()));
|
||||
let _ = fsx::atomic_write(&target, content.as_deref().unwrap_or(b"{}"));
|
||||
}
|
||||
|
||||
@@ -335,21 +232,12 @@ runpy.run_path(script, run_name="__main__")
|
||||
/// Start the dashboard with the repack's own Python. Returns when it answers on its port.
|
||||
pub fn start(root: &Path) -> Result<Status> {
|
||||
let current = status(root);
|
||||
if !current.installed {
|
||||
return Err(Error::Invalid("The dashboard is not installed.".into()));
|
||||
}
|
||||
if current.running {
|
||||
return Ok(current);
|
||||
}
|
||||
if !current.installed { return Err(Error::Invalid("The dashboard is not installed.".into())); }
|
||||
if current.running { return Ok(current); }
|
||||
let python = root.join("Runtime/python/python.exe");
|
||||
if !python.is_file() {
|
||||
return Err(Error::Invalid(
|
||||
"This server has no bundled Python, so the dashboard cannot run.".into(),
|
||||
));
|
||||
}
|
||||
if !python.is_file() { return Err(Error::Invalid("This server has no bundled Python, so the dashboard cannot run.".into())); }
|
||||
let folder = dir(root);
|
||||
let mut marker = read_marker(root)
|
||||
.ok_or_else(|| Error::Invalid("The dashboard installation is incomplete.".into()))?;
|
||||
let mut marker = read_marker(root).ok_or_else(|| Error::Invalid("The dashboard installation is incomplete.".into()))?;
|
||||
marker.port = free_port(marker.port)?;
|
||||
fsx::atomic_write_json(&folder.join(MARKER), &marker)?;
|
||||
|
||||
@@ -361,48 +249,29 @@ pub fn start(root: &Path) -> Result<Status> {
|
||||
let script = folder.join("squidbots.py");
|
||||
let launcher = folder.join("manager_launch.py");
|
||||
fsx::atomic_write(&launcher, LAUNCHER.as_bytes())?;
|
||||
command
|
||||
.arg("-B")
|
||||
.arg(&launcher)
|
||||
.arg(&script)
|
||||
.current_dir(&folder)
|
||||
.env("COA_DASHBOARD_PORT", marker.port.to_string())
|
||||
.stdin(std::process::Stdio::null())
|
||||
.stdout(log.try_clone()?)
|
||||
.stderr(log);
|
||||
command.arg("-B").arg(&launcher).arg(&script).current_dir(&folder).env("COA_DASHBOARD_PORT", marker.port.to_string())
|
||||
.stdin(std::process::Stdio::null()).stdout(log.try_clone()?).stderr(log);
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::os::windows::process::CommandExt;
|
||||
command.creation_flags(0x0800_0000); // CREATE_NO_WINDOW
|
||||
}
|
||||
let mut child = command.spawn()?;
|
||||
let identity = process::identity(child.id())
|
||||
.ok_or_else(|| Error::Invalid("The dashboard exited at once.".into()))?;
|
||||
let identity = process::identity(child.id()).ok_or_else(|| Error::Invalid("The dashboard exited at once.".into()))?;
|
||||
fsx::atomic_write_json(&folder.join(PROCESS), &identity)?;
|
||||
|
||||
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20);
|
||||
loop {
|
||||
if process::listeners()
|
||||
.iter()
|
||||
.any(|(port, pid)| *port == marker.port && *pid == identity.pid)
|
||||
{
|
||||
return Ok(status(root));
|
||||
}
|
||||
if process::listeners().iter().any(|(port, pid)| *port == marker.port && *pid == identity.pid) { return Ok(status(root)); }
|
||||
if let Ok(Some(_)) = child.try_wait() {
|
||||
let _ = std::fs::remove_file(folder.join(PROCESS));
|
||||
let tail = std::fs::read_to_string(&log_path).unwrap_or_default();
|
||||
let lines: Vec<&str> = tail.lines().rev().take(8).collect();
|
||||
return Err(Error::Invalid(format!(
|
||||
"The dashboard stopped right after starting. {}",
|
||||
lines.into_iter().rev().collect::<Vec<_>>().join("\n")
|
||||
)));
|
||||
return Err(Error::Invalid(format!("The dashboard stopped right after starting. {}", lines.into_iter().rev().collect::<Vec<_>>().join("\n"))));
|
||||
}
|
||||
if std::time::Instant::now() > deadline {
|
||||
let _ = stop(root);
|
||||
return Err(Error::Invalid(
|
||||
"The dashboard did not start in time. See Extras/SquidDashboard/dashboard.log."
|
||||
.into(),
|
||||
));
|
||||
return Err(Error::Invalid("The dashboard did not start in time. See Extras/SquidDashboard/dashboard.log.".into()));
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(300));
|
||||
}
|
||||
@@ -413,10 +282,7 @@ pub fn stop(root: &Path) -> Result<()> {
|
||||
let record_file = dir(root).join(PROCESS);
|
||||
if let Some(record) = record_alive(root) {
|
||||
let mut command = std::process::Command::new("taskkill");
|
||||
command
|
||||
.args(["/PID", &record.pid.to_string(), "/T", "/F"])
|
||||
.stdout(std::process::Stdio::null())
|
||||
.stderr(std::process::Stdio::null());
|
||||
command.args(["/PID", &record.pid.to_string(), "/T", "/F"]).stdout(std::process::Stdio::null()).stderr(std::process::Stdio::null());
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::os::windows::process::CommandExt;
|
||||
@@ -427,9 +293,7 @@ pub fn stop(root: &Path) -> Result<()> {
|
||||
while process::is_alive(&record) && std::time::Instant::now() < deadline {
|
||||
std::thread::sleep(std::time::Duration::from_millis(200));
|
||||
}
|
||||
if process::is_alive(&record) {
|
||||
return Err(Error::Invalid("The dashboard could not be stopped.".into()));
|
||||
}
|
||||
if process::is_alive(&record) { return Err(Error::Invalid("The dashboard could not be stopped.".into())); }
|
||||
}
|
||||
let _ = std::fs::remove_file(record_file);
|
||||
Ok(())
|
||||
@@ -439,29 +303,15 @@ pub fn stop(root: &Path) -> Result<()> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn tags(list: &[&str]) -> Vec<String> {
|
||||
list.iter().map(|s| s.to_string()).collect()
|
||||
}
|
||||
fn tags(list: &[&str]) -> Vec<String> { list.iter().map(|s| s.to_string()).collect() }
|
||||
|
||||
#[test]
|
||||
fn the_dashboard_follows_the_bots_release_series() {
|
||||
let available = tags(&["v1.8", "v1.9", "v1.9.2", "v2.0"]);
|
||||
assert_eq!(
|
||||
pick_tag("v1.9", &available).as_deref(),
|
||||
Some("v1.9.2"),
|
||||
"the newest of the same series"
|
||||
);
|
||||
assert_eq!(
|
||||
pick_tag("v1.9.1", &tags(&["v1.8", "v1.9"])).as_deref(),
|
||||
Some("v1.9"),
|
||||
"bots 1.9.1 use dashboard 1.9"
|
||||
);
|
||||
assert_eq!(pick_tag("v1.9", &available).as_deref(), Some("v1.9.2"), "the newest of the same series");
|
||||
assert_eq!(pick_tag("v1.9.1", &tags(&["v1.8", "v1.9"])).as_deref(), Some("v1.9"), "bots 1.9.1 use dashboard 1.9");
|
||||
assert_eq!(pick_tag("v1.8.1", &available).as_deref(), Some("v1.8"));
|
||||
assert_eq!(
|
||||
pick_tag("v1.10", &available),
|
||||
None,
|
||||
"a later series has none yet"
|
||||
);
|
||||
assert_eq!(pick_tag("v1.10", &available), None, "a later series has none yet");
|
||||
assert_eq!(pick_tag("main", &available), None);
|
||||
assert!(same_series("v1.9", "v1.9.1") && !same_series("v1.9", "v1.8.9"));
|
||||
}
|
||||
@@ -477,11 +327,7 @@ mod tests {
|
||||
let mut removed = discard_damaged_data(folder.path());
|
||||
removed.sort();
|
||||
assert_eq!(removed, ["history.json", "levels.json"]);
|
||||
assert!(
|
||||
folder.path().join("xp-history.json").exists()
|
||||
&& folder.path().join("worldmap.json").exists()
|
||||
&& folder.path().join("empty.json").exists()
|
||||
);
|
||||
assert!(folder.path().join("xp-history.json").exists() && folder.path().join("worldmap.json").exists() && folder.path().join("empty.json").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -494,17 +340,12 @@ mod tests {
|
||||
#[ignore]
|
||||
fn the_matching_release_is_downloaded_and_unpacked() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let settings = root
|
||||
.path()
|
||||
.join("Core/configs/modules/playerbots.conf.settings.json");
|
||||
let settings = root.path().join("Core/configs/modules/playerbots.conf.settings.json");
|
||||
fsx::atomic_write(&settings, br#"{"format":1,"tag":"v1.9.1","settings":[]}"#).unwrap();
|
||||
let installed = install(root.path(), &|step| println!("{step}")).unwrap();
|
||||
assert!(installed.installed && installed.matches);
|
||||
assert_eq!(installed.tag.as_deref(), Some("v1.9"));
|
||||
assert!(root
|
||||
.path()
|
||||
.join("Extras/SquidDashboard/squidbots.py")
|
||||
.is_file());
|
||||
assert!(root.path().join("Extras/SquidDashboard/squidbots.py").is_file());
|
||||
assert!(!installed.running);
|
||||
}
|
||||
|
||||
@@ -516,37 +357,16 @@ mod tests {
|
||||
let root = PathBuf::from(std::env::var("COA_TEST_ROOT").expect("COA_TEST_ROOT"));
|
||||
let started = start(&root).unwrap();
|
||||
assert!(started.running, "{started:?}");
|
||||
let client = reqwest::blocking::Client::builder()
|
||||
.timeout(std::time::Duration::from_secs(90))
|
||||
.build()
|
||||
.unwrap();
|
||||
let body = client
|
||||
.get(format!("{}/api/stats", started.url))
|
||||
.send()
|
||||
.unwrap()
|
||||
.bytes()
|
||||
.unwrap();
|
||||
println!(
|
||||
"stats: {} bytes: {}",
|
||||
body.len(),
|
||||
String::from_utf8_lossy(&body[..body.len().min(300)])
|
||||
);
|
||||
let live = client
|
||||
.get(format!("{}/api/live", started.url))
|
||||
.send()
|
||||
.unwrap()
|
||||
.bytes()
|
||||
.unwrap();
|
||||
let client = reqwest::blocking::Client::builder().timeout(std::time::Duration::from_secs(90)).build().unwrap();
|
||||
let body = client.get(format!("{}/api/stats", started.url)).send().unwrap().bytes().unwrap();
|
||||
println!("stats: {} bytes: {}", body.len(), String::from_utf8_lossy(&body[..body.len().min(300)]));
|
||||
let live = client.get(format!("{}/api/live", started.url)).send().unwrap().bytes().unwrap();
|
||||
stop(&root).unwrap();
|
||||
assert!(!status(&root).running);
|
||||
if String::from_utf8_lossy(&body).starts_with("{\"error\"") {
|
||||
println!("the database of this server is not running, so the large answer could not be checked");
|
||||
} else {
|
||||
assert!(
|
||||
body.len() > 100_000,
|
||||
"the statistics must arrive whole, got {} bytes",
|
||||
body.len()
|
||||
);
|
||||
assert!(body.len() > 100_000, "the statistics must arrive whole, got {} bytes", body.len());
|
||||
}
|
||||
assert!(!live.is_empty());
|
||||
}
|
||||
|
||||
+65
-266
@@ -68,62 +68,43 @@ fn no_window(cmd: &mut Command) {
|
||||
}
|
||||
|
||||
/// Schema names of the three game databases.
|
||||
pub const SCHEMAS: [(&str, &str); 3] = [
|
||||
("characters", "acore_characters"),
|
||||
("auth", "acore_auth"),
|
||||
("world", "acore_world"),
|
||||
];
|
||||
pub const SCHEMAS: [(&str, &str); 3] = [("characters", "acore_characters"), ("auth", "acore_auth"), ("world", "acore_world")];
|
||||
|
||||
pub fn schema_of(kind: &str) -> Result<&'static str> {
|
||||
if kind == "playerbots" { return Ok("acore_playerbots"); }
|
||||
if let Some(kind) = kind.strip_prefix("wildcard-") { return crate::realms::Mode::Wildcard.schema(kind); }
|
||||
if let Some(kind) = kind.strip_prefix("reborn-") { return crate::realms::Mode::Reborn.schema(kind); }
|
||||
if let Some(kind) = kind.strip_prefix("coa-") { return crate::realms::Mode::Coa.schema(kind); }
|
||||
SCHEMAS.iter().find(|(k, _)| *k == kind).map(|(_, s)| *s).ok_or_else(|| Error::Invalid(format!("unknown database {kind}")))
|
||||
}
|
||||
|
||||
fn ident_ok(name: &str) -> bool {
|
||||
!name.is_empty()
|
||||
&& name.len() <= 64
|
||||
&& name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
|
||||
!name.is_empty() && name.len() <= 64 && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
|
||||
}
|
||||
|
||||
/// (root password, application password) from `Settings/database.json`.
|
||||
pub(crate) fn credentials(root: &Path) -> Result<(String, String)> {
|
||||
let c: Credentials = fsx::read_json(&root.join("Settings/database.json")).map_err(|_| {
|
||||
Error::Invalid("The database settings of this server could not be read.".into())
|
||||
})?;
|
||||
let c: Credentials = fsx::read_json(&root.join("Settings/database.json"))
|
||||
.map_err(|_| Error::Invalid("The database settings of this server could not be read.".into()))?;
|
||||
Ok((c.root_password, c.app_password))
|
||||
}
|
||||
|
||||
impl Db {
|
||||
pub fn from_repack(root: &Path, account: Account) -> Result<Db> {
|
||||
let creds: Credentials =
|
||||
fsx::read_json(&root.join("Settings/database.json")).map_err(|_| {
|
||||
Error::Invalid("The database settings of this server could not be read.".into())
|
||||
})?;
|
||||
let creds: Credentials = fsx::read_json(&root.join("Settings/database.json"))
|
||||
.map_err(|_| Error::Invalid("The database settings of this server could not be read.".into()))?;
|
||||
let ports: Ports = read_ports(root);
|
||||
if crate::docker::is_docker(root) {
|
||||
return Db::in_container(root, creds, account);
|
||||
}
|
||||
let bin = root.join("mysql/bin");
|
||||
if !bin.join("mysqldump.exe").is_file() || !bin.join("mysql.exe").is_file() {
|
||||
return Err(Error::Invalid(
|
||||
"The bundled database tools are missing.".into(),
|
||||
));
|
||||
return Err(Error::Invalid("The bundled database tools are missing.".into()));
|
||||
}
|
||||
let (user, password) = match account {
|
||||
Account::Admin => ("root", creds.root_password),
|
||||
Account::App => ("acore", creds.app_password),
|
||||
};
|
||||
Ok(Db {
|
||||
bin,
|
||||
port: ports.mysql,
|
||||
user,
|
||||
password: Secret(password),
|
||||
realm: crate::realms::state(root)?.active,
|
||||
container: None,
|
||||
})
|
||||
Ok(Db { bin, port: ports.mysql, user, password: Secret(password), realm: crate::realms::state(root)?.active, container: None })
|
||||
}
|
||||
|
||||
/// The database of a Docker installation: the client tools of the database image, run with `docker exec`. The
|
||||
@@ -136,59 +117,24 @@ impl Db {
|
||||
};
|
||||
// Inside the container the server listens on its standard port; the commands below connect to it over TCP on
|
||||
// the container's own loopback, exactly like the commands of a repack do on the host.
|
||||
Ok(Db {
|
||||
bin: PathBuf::new(),
|
||||
port: 3306,
|
||||
user,
|
||||
password: Secret(password),
|
||||
realm: crate::realms::state(root)?.active,
|
||||
container: Some(container),
|
||||
})
|
||||
Ok(Db { bin: PathBuf::new(), port: 3306, user, password: Secret(password), realm: crate::realms::state(root)?.active, container: Some(container) })
|
||||
}
|
||||
|
||||
/// A database reached through explicitly given client tools. Used by tests against a disposable server; the
|
||||
/// installation-based constructors above are what the Manager itself uses.
|
||||
pub fn with_tools(
|
||||
bin: PathBuf,
|
||||
port: u16,
|
||||
user: &'static str,
|
||||
password: &str,
|
||||
realm: crate::realms::Mode,
|
||||
) -> Db {
|
||||
Db {
|
||||
bin,
|
||||
port,
|
||||
user,
|
||||
password: Secret(password.to_string()),
|
||||
realm,
|
||||
container: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn for_realm(mut self, realm: crate::realms::Mode) -> Self {
|
||||
self.realm = realm;
|
||||
self
|
||||
}
|
||||
pub fn realm(&self) -> crate::realms::Mode {
|
||||
self.realm
|
||||
}
|
||||
pub fn for_realm(mut self, realm: crate::realms::Mode) -> Self { self.realm = realm; self }
|
||||
pub fn realm(&self) -> crate::realms::Mode { self.realm }
|
||||
|
||||
pub fn realm_schema<'a>(&self, name: &'a str) -> &'a str {
|
||||
if self.realm != crate::realms::Mode::Coa {
|
||||
if self.realm == crate::realms::Mode::Wildcard {
|
||||
match name {
|
||||
"acore_world" => self.realm.schema("world").unwrap(),
|
||||
"acore_characters" => self.realm.schema("characters").unwrap(),
|
||||
"acore_world" => "acore_world_wildcard",
|
||||
"acore_characters" => "acore_characters_wildcard",
|
||||
_ => name,
|
||||
}
|
||||
} else {
|
||||
name
|
||||
}
|
||||
} else { name }
|
||||
}
|
||||
|
||||
pub fn clone_structure(&self, source: &str, dest: &str, cache: &Path) -> Result<()> {
|
||||
if !ident_ok(source) || !ident_ok(dest) {
|
||||
return Err(Error::Invalid("Invalid schema name.".into()));
|
||||
}
|
||||
if !ident_ok(source) || !ident_ok(dest) { return Err(Error::Invalid("Invalid schema name.".into())); }
|
||||
let path = cache.join("characters-structure.sql");
|
||||
self.dump_structure_to(source, &path)?;
|
||||
self.run_sql_file(dest, &path)?;
|
||||
@@ -197,22 +143,12 @@ impl Db {
|
||||
}
|
||||
|
||||
pub fn dump_structure_to(&self, source: &str, path: &Path) -> Result<()> {
|
||||
if !ident_ok(source) {
|
||||
return Err(Error::Invalid("Invalid schema name.".into()));
|
||||
}
|
||||
if !ident_ok(source) { return Err(Error::Invalid("Invalid schema name.".into())); }
|
||||
let mut c = self.command("mysqldump.exe");
|
||||
c.args([
|
||||
"--no-data",
|
||||
"--no-tablespaces",
|
||||
"--skip-comments",
|
||||
"--default-character-set=utf8mb4",
|
||||
source,
|
||||
]);
|
||||
c.stdout(Stdio::from(File::create(path)?));
|
||||
c.args(["--no-data", "--no-tablespaces", "--skip-comments", "--default-character-set=utf8mb4", source]);
|
||||
c.stdout(Stdio::from(File::create(&path)?));
|
||||
let out = c.output()?;
|
||||
if !out.status.success() {
|
||||
return Err(self.fail(&out.stderr));
|
||||
}
|
||||
if !out.status.success() { return Err(self.fail(&out.stderr)); }
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -222,14 +158,7 @@ impl Db {
|
||||
Some(name) => {
|
||||
// `-e MYSQL_PWD` without a value takes it from this process's environment: never on a command line.
|
||||
let mut c = Command::new("docker");
|
||||
c.args([
|
||||
"exec",
|
||||
"-i",
|
||||
"-e",
|
||||
"MYSQL_PWD",
|
||||
name,
|
||||
tool.trim_end_matches(".exe"),
|
||||
]);
|
||||
c.args(["exec", "-i", "-e", "MYSQL_PWD", name, tool.trim_end_matches(".exe")]);
|
||||
c
|
||||
}
|
||||
};
|
||||
@@ -249,31 +178,12 @@ impl Db {
|
||||
let text = String::from_utf8_lossy(stderr);
|
||||
// Only the client's own connection errors mean "not running": the number 2003 or the words can also appear inside
|
||||
// the message of an ordinary SQL error (a duplicate entry, a row number) and must not hide it.
|
||||
let down = text.contains("ERROR 2003")
|
||||
|| text.contains("ERROR 2002")
|
||||
|| text.contains("Can't connect to MySQL server")
|
||||
|| text.contains("No such container")
|
||||
|| text.contains("is not running");
|
||||
let code = if down {
|
||||
ErrorCode::DatabaseNotRunning
|
||||
} else {
|
||||
ErrorCode::Unknown
|
||||
};
|
||||
let down = text.contains("ERROR 2003") || text.contains("ERROR 2002") || text.contains("Can't connect to MySQL server")
|
||||
|| text.contains("No such container") || text.contains("is not running");
|
||||
let code = if down { ErrorCode::DatabaseNotRunning } else { ErrorCode::Unknown };
|
||||
match code {
|
||||
ErrorCode::DatabaseNotRunning => Error::Invalid(format!(
|
||||
"The database is not running. Start the server first. ({})",
|
||||
text.trim()
|
||||
.lines()
|
||||
.last()
|
||||
.unwrap_or("")
|
||||
.chars()
|
||||
.take(300)
|
||||
.collect::<String>()
|
||||
)),
|
||||
_ => Error::Invalid(format!(
|
||||
"database command failed: {}",
|
||||
text.trim().lines().last().unwrap_or("")
|
||||
)),
|
||||
ErrorCode::DatabaseNotRunning => Error::Invalid(format!("The database is not running. Start the server first. ({})", text.trim().lines().last().unwrap_or("").chars().take(300).collect::<String>())),
|
||||
_ => Error::Invalid(format!("database command failed: {}", text.trim().lines().last().unwrap_or(""))),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -287,17 +197,10 @@ impl Db {
|
||||
pub fn query(&self, sql: &str) -> Result<String> {
|
||||
let mut c = self.command("mysql.exe");
|
||||
c.args(["--batch", "--skip-column-names", "--connect-timeout=5"]);
|
||||
c.args([
|
||||
"--default-character-set=utf8mb4",
|
||||
"--max-allowed-packet=128M",
|
||||
]);
|
||||
c.args(["--default-character-set=utf8mb4", "--max-allowed-packet=128M"]);
|
||||
c.stdin(Stdio::piped());
|
||||
let mut child = c.spawn()?;
|
||||
child
|
||||
.stdin
|
||||
.take()
|
||||
.expect("piped")
|
||||
.write_all(route_sql(sql, self.realm).as_bytes())?;
|
||||
child.stdin.take().expect("piped").write_all(route_sql(sql, self.realm).as_bytes())?;
|
||||
let out = child.wait_with_output()?;
|
||||
if !out.status.success() {
|
||||
return Err(self.fail(&out.stderr));
|
||||
@@ -310,9 +213,7 @@ impl Db {
|
||||
if !ident_ok(name) {
|
||||
return Err(Error::Invalid("bad schema name".into()));
|
||||
}
|
||||
Ok(self.query(&format!(
|
||||
"SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name='{name}';"
|
||||
))? == "1")
|
||||
Ok(self.query(&format!("SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name='{name}';"))? == "1")
|
||||
}
|
||||
|
||||
pub fn tables(&self, schema: &str) -> Result<Vec<String>> {
|
||||
@@ -321,11 +222,7 @@ impl Db {
|
||||
return Err(Error::Invalid("bad schema name".into()));
|
||||
}
|
||||
let out = self.query(&format!("SELECT table_name FROM information_schema.tables WHERE table_schema='{schema}' AND table_type='BASE TABLE' ORDER BY table_name;"))?;
|
||||
Ok(out
|
||||
.lines()
|
||||
.map(str::to_string)
|
||||
.filter(|s| !s.is_empty())
|
||||
.collect())
|
||||
Ok(out.lines().map(str::to_string).filter(|s| !s.is_empty()).collect())
|
||||
}
|
||||
|
||||
/// Bytes of data + index the schema occupies (used to estimate backup size).
|
||||
@@ -335,9 +232,7 @@ impl Db {
|
||||
return Err(Error::Invalid("bad schema name".into()));
|
||||
}
|
||||
let out = self.query(&format!("SELECT COALESCE(SUM(data_length+index_length),0) FROM information_schema.tables WHERE table_schema='{schema}';"))?;
|
||||
out.trim()
|
||||
.parse()
|
||||
.map_err(|_| Error::Invalid("unexpected size reply".into()))
|
||||
out.trim().parse().map_err(|_| Error::Invalid("unexpected size reply".into()))
|
||||
}
|
||||
|
||||
/// Stored routines / triggers / views make a table-level swap unsafe; count them.
|
||||
@@ -349,10 +244,7 @@ impl Db {
|
||||
let sql = format!(
|
||||
"SELECT (SELECT COUNT(*) FROM information_schema.routines WHERE routine_schema='{schema}') + (SELECT COUNT(*) FROM information_schema.triggers WHERE trigger_schema='{schema}') + (SELECT COUNT(*) FROM information_schema.views WHERE table_schema='{schema}');"
|
||||
);
|
||||
self.query(&sql)?
|
||||
.trim()
|
||||
.parse()
|
||||
.map_err(|_| Error::Invalid("unexpected reply".into()))
|
||||
self.query(&sql)?.trim().parse().map_err(|_| Error::Invalid("unexpected reply".into()))
|
||||
}
|
||||
|
||||
pub fn recovery_objects(&self, schema: &str) -> Result<Vec<serde_json::Value>> {
|
||||
@@ -369,16 +261,7 @@ impl Db {
|
||||
return Err(Error::Invalid("bad schema name".into()));
|
||||
}
|
||||
let mut c = self.command("mysqldump.exe");
|
||||
c.args([
|
||||
"--single-transaction",
|
||||
"--quick",
|
||||
"--routines",
|
||||
"--triggers",
|
||||
"--default-character-set=utf8mb4",
|
||||
"--no-tablespaces",
|
||||
"--skip-comments",
|
||||
"--hex-blob",
|
||||
]);
|
||||
c.args(["--single-transaction", "--quick", "--routines", "--triggers", "--default-character-set=utf8mb4", "--no-tablespaces", "--skip-comments", "--hex-blob"]);
|
||||
c.arg(schema);
|
||||
let mut child = c.spawn()?;
|
||||
let mut stdout = child.stdout.take().expect("piped");
|
||||
@@ -411,19 +294,13 @@ impl Db {
|
||||
if !ident_ok(schema) {
|
||||
return Err(Error::Invalid("bad schema name".into()));
|
||||
}
|
||||
if self.realm != crate::realms::Mode::Coa {
|
||||
if self.realm == crate::realms::Mode::Wildcard {
|
||||
let sql = std::fs::read_to_string(file)?;
|
||||
self.query(&format!(
|
||||
"SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci; USE `{schema}`;\n{sql}"
|
||||
))?;
|
||||
self.query(&format!("SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci; USE `{schema}`;\n{sql}"))?;
|
||||
return Ok(());
|
||||
}
|
||||
let mut c = self.command("mysql.exe");
|
||||
c.args([
|
||||
"--default-character-set=utf8mb4",
|
||||
"--init-command=SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci",
|
||||
"--max-allowed-packet=128M",
|
||||
]);
|
||||
c.args(["--default-character-set=utf8mb4", "--init-command=SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci", "--max-allowed-packet=128M"]);
|
||||
c.arg(schema);
|
||||
c.stdin(Stdio::from(File::open(file)?));
|
||||
let out = c.output()?;
|
||||
@@ -440,11 +317,7 @@ impl Db {
|
||||
return Err(Error::Invalid("bad schema name".into()));
|
||||
}
|
||||
let mut c = self.command("mysql.exe");
|
||||
c.args([
|
||||
"--default-character-set=utf8mb4",
|
||||
"--init-command=SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci",
|
||||
"--max-allowed-packet=128M",
|
||||
]);
|
||||
c.args(["--default-character-set=utf8mb4", "--init-command=SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci", "--max-allowed-packet=128M"]);
|
||||
c.arg(schema);
|
||||
c.stdin(Stdio::piped());
|
||||
let mut child = c.spawn()?;
|
||||
@@ -502,9 +375,7 @@ impl Db {
|
||||
pub fn write_console_credentials(root: &Path, password: &str) -> Result<()> {
|
||||
let path = root.join("Settings/repack.json");
|
||||
let mut v: serde_json::Value = fsx::read_json(&path)?;
|
||||
let obj = v
|
||||
.as_object_mut()
|
||||
.ok_or_else(|| Error::Invalid("repack.json is not an object".into()))?;
|
||||
let obj = v.as_object_mut().ok_or_else(|| Error::Invalid("repack.json is not an object".into()))?;
|
||||
obj.insert("raUsername".into(), SERVICE_ACCOUNT.into());
|
||||
obj.insert("raPassword".into(), password.into());
|
||||
fsx::atomic_write_json(&path, &v)
|
||||
@@ -516,55 +387,36 @@ pub fn valid_identifier(name: &str) -> bool {
|
||||
|
||||
// Route only SQL identifiers. User names, values, comments and longer identifiers remain untouched.
|
||||
fn route_sql(sql: &str, realm: crate::realms::Mode) -> String {
|
||||
if realm == crate::realms::Mode::Coa {
|
||||
return sql.into();
|
||||
}
|
||||
if realm == crate::realms::Mode::Coa { return sql.into(); }
|
||||
let bytes = sql.as_bytes();
|
||||
let mut out = String::new();
|
||||
let mut i = 0;
|
||||
while i < bytes.len() {
|
||||
let start = i;
|
||||
if matches!(bytes[i], b'\'' | b'"') {
|
||||
let quote = bytes[i];
|
||||
i += 1;
|
||||
let quote = bytes[i]; i += 1;
|
||||
while i < bytes.len() {
|
||||
if bytes[i] == b'\\' {
|
||||
i = (i + 2).min(bytes.len());
|
||||
} else if bytes[i] == quote {
|
||||
if bytes[i] == b'\\' { i = (i + 2).min(bytes.len()); }
|
||||
else if bytes[i] == quote {
|
||||
i += 1;
|
||||
if i < bytes.len() && bytes[i] == quote {
|
||||
i += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
i += 1;
|
||||
}
|
||||
if i < bytes.len() && bytes[i] == quote { i += 1; } else { break; }
|
||||
} else { i += 1; }
|
||||
}
|
||||
out.push_str(&sql[start..i]);
|
||||
} else if bytes[i] == b'#' || sql[i..].starts_with("--") || sql[i..].starts_with("/*") {
|
||||
if sql[i..].starts_with("/*") {
|
||||
i = sql[i + 2..]
|
||||
.find("*/")
|
||||
.map(|p| i + p + 4)
|
||||
.unwrap_or(bytes.len());
|
||||
} else {
|
||||
i = sql[i..].find('\n').map(|p| i + p).unwrap_or(bytes.len());
|
||||
}
|
||||
i = sql[i + 2..].find("*/").map(|p| i + p + 4).unwrap_or(bytes.len());
|
||||
} else { i = sql[i..].find('\n').map(|p| i + p).unwrap_or(bytes.len()); }
|
||||
out.push_str(&sql[start..i]);
|
||||
} else if bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_' {
|
||||
while i < bytes.len() && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_') {
|
||||
i += 1;
|
||||
}
|
||||
while i < bytes.len() && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_') { i += 1; }
|
||||
out.push_str(match &sql[start..i] {
|
||||
"acore_world" => realm.schema("world").unwrap(),
|
||||
"acore_characters" => realm.schema("characters").unwrap(),
|
||||
"acore_world" => "acore_world_wildcard",
|
||||
"acore_characters" => "acore_characters_wildcard",
|
||||
other => other,
|
||||
});
|
||||
} else {
|
||||
let c = sql[i..].chars().next().unwrap();
|
||||
out.push(c);
|
||||
i += c.len_utf8();
|
||||
let c = sql[i..].chars().next().unwrap(); out.push(c); i += c.len_utf8();
|
||||
}
|
||||
}
|
||||
out
|
||||
@@ -582,14 +434,6 @@ mod realm_tests {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn reborn_sql_routes_only_database_identifiers() {
|
||||
use crate::realms::Mode;
|
||||
let sql = "SELECT * FROM `acore_characters`.characters WHERE name='acore_world'; -- acore_world\nSELECT * FROM acore_world.items; SELECT * FROM acore_world_wildcard.items;";
|
||||
assert_eq!(super::route_sql(sql, Mode::Reborn), "SELECT * FROM `acore_characters_reborn`.characters WHERE name='acore_world'; -- acore_world\nSELECT * FROM acore_world_reborn.items; SELECT * FROM acore_world_wildcard.items;");
|
||||
assert_eq!(super::schema_of("reborn-characters").unwrap(), "acore_characters_reborn");
|
||||
assert_eq!(super::schema_of("reborn-auth").unwrap(), "acore_auth");
|
||||
}
|
||||
use super::*;
|
||||
use std::ffi::OsStr;
|
||||
|
||||
@@ -613,11 +457,7 @@ mod tests {
|
||||
let d = tempfile::tempdir().unwrap();
|
||||
let root = d.path().join("srv");
|
||||
std::fs::create_dir_all(root.join("Settings")).unwrap();
|
||||
std::fs::write(
|
||||
root.join("Settings/database.json"),
|
||||
format!(r#"{{"rootPassword":"{ROOT_PW}","appPassword":"{APP_PW}"}}"#),
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::write(root.join("Settings/database.json"), format!(r#"{{"rootPassword":"{ROOT_PW}","appPassword":"{APP_PW}"}}"#)).unwrap();
|
||||
if docker {
|
||||
std::fs::write(root.join("Settings/docker.json"), r#"{"project":"t1"}"#).unwrap();
|
||||
}
|
||||
@@ -625,48 +465,28 @@ mod tests {
|
||||
}
|
||||
|
||||
fn args(c: &Command) -> Vec<String> {
|
||||
c.get_args()
|
||||
.map(|a| a.to_string_lossy().into_owned())
|
||||
.collect()
|
||||
c.get_args().map(|a| a.to_string_lossy().into_owned()).collect()
|
||||
}
|
||||
|
||||
fn env_of(c: &Command, key: &str) -> Option<String> {
|
||||
c.get_envs()
|
||||
.find(|(k, _)| *k == OsStr::new(key))
|
||||
.and_then(|(_, v)| v.map(|v| v.to_string_lossy().into_owned()))
|
||||
c.get_envs().find(|(k, _)| *k == OsStr::new(key)).and_then(|(_, v)| v.map(|v| v.to_string_lossy().into_owned()))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_docker_installation_runs_the_client_tools_inside_the_database_container() {
|
||||
let (_d, root) = folder(true);
|
||||
let db = Db::from_repack(&root, Account::Admin).unwrap();
|
||||
for (tool, program) in [
|
||||
("mysql.exe", "mysql"),
|
||||
("mysqldump.exe", "mysqldump"),
|
||||
("mysqladmin.exe", "mysqladmin"),
|
||||
] {
|
||||
for (tool, program) in [("mysql.exe", "mysql"), ("mysqldump.exe", "mysqldump"), ("mysqladmin.exe", "mysqladmin")] {
|
||||
let c = db.command(tool);
|
||||
assert_eq!(c.get_program(), "docker");
|
||||
let a = args(&c);
|
||||
assert_eq!(
|
||||
&a[..6],
|
||||
["exec", "-i", "-e", "MYSQL_PWD", "coa-t1-db", program]
|
||||
);
|
||||
assert!(
|
||||
a.contains(&"--protocol=tcp".to_string())
|
||||
&& a.contains(&"--host=127.0.0.1".to_string())
|
||||
&& a.contains(&"--port=3306".to_string())
|
||||
);
|
||||
assert_eq!(&a[..6], ["exec", "-i", "-e", "MYSQL_PWD", "coa-t1-db", program]);
|
||||
assert!(a.contains(&"--protocol=tcp".to_string()) && a.contains(&"--host=127.0.0.1".to_string()) && a.contains(&"--port=3306".to_string()));
|
||||
assert!(a.contains(&"--user=root".to_string()));
|
||||
assert!(
|
||||
a.iter().all(|x| !x.contains(ROOT_PW)),
|
||||
"the password must not be on the command line"
|
||||
);
|
||||
assert!(a.iter().all(|x| !x.contains(ROOT_PW)), "the password must not be on the command line");
|
||||
assert_eq!(env_of(&c, "MYSQL_PWD").as_deref(), Some(ROOT_PW));
|
||||
}
|
||||
let app = Db::from_repack(&root, Account::App)
|
||||
.unwrap()
|
||||
.command("mysql.exe");
|
||||
let app = Db::from_repack(&root, Account::App).unwrap().command("mysql.exe");
|
||||
assert!(args(&app).contains(&"--user=acore".to_string()));
|
||||
assert_eq!(env_of(&app, "MYSQL_PWD").as_deref(), Some(APP_PW));
|
||||
}
|
||||
@@ -679,18 +499,11 @@ mod tests {
|
||||
std::fs::write(root.join("mysql/bin").join(f), b"x").unwrap();
|
||||
}
|
||||
std::fs::write(root.join("Settings/repack.json"), r#"{"mysqlPort":3999}"#).unwrap();
|
||||
let c = Db::from_repack(&root, Account::Admin)
|
||||
.unwrap()
|
||||
.command("mysql.exe");
|
||||
assert_eq!(
|
||||
c.get_program(),
|
||||
root.join("mysql/bin").join("mysql.exe").as_os_str()
|
||||
);
|
||||
let c = Db::from_repack(&root, Account::Admin).unwrap().command("mysql.exe");
|
||||
assert_eq!(c.get_program(), root.join("mysql/bin").join("mysql.exe").as_os_str());
|
||||
let a = args(&c);
|
||||
assert!(!a.contains(&"exec".to_string()));
|
||||
assert!(
|
||||
a.contains(&"--port=3999".to_string()) && a.contains(&"--host=127.0.0.1".to_string())
|
||||
);
|
||||
assert!(a.contains(&"--port=3999".to_string()) && a.contains(&"--host=127.0.0.1".to_string()));
|
||||
assert_eq!(env_of(&c, "MYSQL_PWD").as_deref(), Some(ROOT_PW));
|
||||
}
|
||||
|
||||
@@ -700,11 +513,7 @@ mod tests {
|
||||
assert!(!root.join("mysql").exists());
|
||||
assert!(Db::from_repack(&root, Account::Admin).is_ok());
|
||||
// ...but a broken Docker settings file is reported, not ignored.
|
||||
std::fs::write(
|
||||
root.join("Settings/docker.json"),
|
||||
r#"{"project":"Bad Name"}"#,
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::write(root.join("Settings/docker.json"), r#"{"project":"Bad Name"}"#).unwrap();
|
||||
assert!(Db::from_repack(&root, Account::Admin).is_err());
|
||||
}
|
||||
|
||||
@@ -712,19 +521,9 @@ mod tests {
|
||||
fn a_stopped_database_container_is_reported_as_a_database_that_is_not_running() {
|
||||
let (_d, root) = folder(true);
|
||||
let db = Db::from_repack(&root, Account::Admin).unwrap();
|
||||
for text in [
|
||||
"Error response from daemon: container abc is not running",
|
||||
"Error response from daemon: No such container: coa-t1-db",
|
||||
"ERROR 2003 (HY000): Can't connect to MySQL server",
|
||||
] {
|
||||
assert!(
|
||||
db.fail(text.as_bytes()).to_string().contains("not running"),
|
||||
"{text}"
|
||||
);
|
||||
for text in ["Error response from daemon: container abc is not running", "Error response from daemon: No such container: coa-t1-db", "ERROR 2003 (HY000): Can't connect to MySQL server"] {
|
||||
assert!(db.fail(text.as_bytes()).to_string().contains("not running"), "{text}");
|
||||
}
|
||||
assert!(db
|
||||
.fail(b"ERROR 1045 (28000): Access denied for user 'root'")
|
||||
.to_string()
|
||||
.contains("Access denied"));
|
||||
assert!(db.fail(b"ERROR 1045 (28000): Access denied for user 'root'").to_string().contains("Access denied"));
|
||||
}
|
||||
}
|
||||
+124
-818
File diff suppressed because it is too large.
Load diff
@@ -23,9 +23,7 @@ impl Output {
|
||||
|
||||
/// stdout and stderr, trimmed, for the technical details shown to the user.
|
||||
pub fn text(&self) -> String {
|
||||
format!("{}\n{}", self.stdout.trim(), self.stderr.trim())
|
||||
.trim()
|
||||
.to_string()
|
||||
format!("{}\n{}", self.stdout.trim(), self.stderr.trim()).trim().to_string()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,12 +38,7 @@ pub struct Call<'a> {
|
||||
|
||||
impl<'a> Call<'a> {
|
||||
pub fn new(args: &[&str], timeout: Duration) -> Call<'a> {
|
||||
Call {
|
||||
args: args.iter().map(|a| a.to_string()).collect(),
|
||||
env: Vec::new(),
|
||||
stdin: None,
|
||||
timeout,
|
||||
}
|
||||
Call { args: args.iter().map(|a| a.to_string()).collect(), env: Vec::new(), stdin: None, timeout }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,17 +50,12 @@ pub trait Docker {
|
||||
/// when nothing listens inside the container, and closes them at once; so a connection that stays open (or gets
|
||||
/// data, as the world server's greeting) is the proof, a connect alone is not.
|
||||
fn port_open(&self, addr: SocketAddr) -> bool {
|
||||
let Ok(mut s) = TcpStream::connect_timeout(&addr, Duration::from_millis(400)) else {
|
||||
return false;
|
||||
};
|
||||
let Ok(mut s) = TcpStream::connect_timeout(&addr, Duration::from_millis(400)) else { return false };
|
||||
let _ = s.set_read_timeout(Some(Duration::from_millis(300)));
|
||||
match s.read(&mut [0u8; 1]) {
|
||||
Ok(0) => false,
|
||||
Ok(_) => true,
|
||||
Err(e) => matches!(
|
||||
e.kind(),
|
||||
std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut
|
||||
),
|
||||
Err(e) => matches!(e.kind(), std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,23 +69,14 @@ pub struct SystemDocker;
|
||||
impl Docker for SystemDocker {
|
||||
fn run(&self, call: &Call) -> Result<Output> {
|
||||
let mut cmd = Command::new("docker");
|
||||
cmd.args(&call.args)
|
||||
.envs(call.env.iter().map(|(k, v)| (k, v)))
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped());
|
||||
cmd.stdin(if call.stdin.is_some() {
|
||||
Stdio::piped()
|
||||
} else {
|
||||
Stdio::null()
|
||||
});
|
||||
cmd.args(&call.args).envs(call.env.iter().map(|(k, v)| (k, v))).stdout(Stdio::piped()).stderr(Stdio::piped());
|
||||
cmd.stdin(if call.stdin.is_some() { Stdio::piped() } else { Stdio::null() });
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::os::windows::process::CommandExt;
|
||||
cmd.creation_flags(0x0800_0000); // CREATE_NO_WINDOW
|
||||
}
|
||||
let mut child = cmd
|
||||
.spawn()
|
||||
.map_err(|e| Error::Invalid(format!("docker could not be started: {e}")))?;
|
||||
let mut child = cmd.spawn().map_err(|e| Error::Invalid(format!("docker could not be started: {e}")))?;
|
||||
if let (Some(data), Some(mut stdin)) = (call.stdin, child.stdin.take()) {
|
||||
// Closing stdin at the end of this block tells docker the input is complete.
|
||||
let _ = stdin.write_all(data);
|
||||
@@ -127,11 +106,7 @@ impl Docker for SystemDocker {
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
};
|
||||
Ok(Output {
|
||||
code,
|
||||
stdout: out_t.join().unwrap_or_default(),
|
||||
stderr: err_t.join().unwrap_or_default(),
|
||||
})
|
||||
Ok(Output { code, stdout: out_t.join().unwrap_or_default(), stderr: err_t.join().unwrap_or_default() })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loaded 100 of 413 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user