mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 05:46:03 +00:00
Vendor OpenNOW's core and native streamer (MIT, by Zortos)
From OpenCloudGaming/OpenNOW a49b124c (2026-10-18): native/opennow-core and the native/opennow-streamer crates DroidDeck's GeForce NOW store builds on, unmodified, with OpenNOW's MIT license and a NOTICE crediting Zortos and the OpenNOW contributors.
This commit is contained in:
1 parent
830c753312
commit
f0f17b9b63
763 files changed
+338298
No files matched your search
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
# DroidDeck changes to the vendored OpenNOW code
|
||||
|
||||
None yet (pristine import of a49b124c).
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 Zortos
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
# OpenNOW (vendored)
|
||||
|
||||
This folder holds parts of **OpenNOW**, an open-source GeForce NOW client by
|
||||
**Zortos** ([zortos293](https://github.com/zortos293)) and the OpenNOW contributors:
|
||||
<https://github.com/OpenCloudGaming/OpenNOW>, MIT License (see `LICENSE`).
|
||||
|
||||
Imported from OpenCloudGaming/OpenNOW commit `a49b124ce31e87113fd4961f6a321fbf0ad1fdc5`
|
||||
(2026-10-18). Only the parts DroidDeck's GeForce NOW store uses are kept:
|
||||
|
||||
- `native/opennow-core`: accounts, sign-in, catalog and CloudMatch session services
|
||||
(newline-delimited JSON over stdin/stdout, `docs/core-protocol.md`).
|
||||
- `native/opennow-streamer`: the native NVST streamer (protocol, transport, core,
|
||||
platform, FFI) plus its vendored `str0m` and `audiopus-sys`.
|
||||
|
||||
DroidDeck's own changes to these files are listed in `CHANGES.md`. NVIDIA and
|
||||
GeForce NOW are trademarks of NVIDIA Corporation; neither OpenNOW nor DroidDeck is
|
||||
affiliated with or endorsed by NVIDIA.
|
||||
+1857
File diff suppressed because it is too large.
Load diff
+69
@@ -0,0 +1,69 @@
|
||||
# Qt native streamer lifecycle events
|
||||
|
||||
ABI version 13 delivers these events through the generic JSON event callback
|
||||
without a new C ABI layout.
|
||||
The native streamer owns wire parsing and termination classification. Qt does not
|
||||
infer resumability from a wire code or reason.
|
||||
|
||||
## Session timers
|
||||
|
||||
```json
|
||||
{"type":"session-timer","startId":"start-42","timerType":4,"secondsLeft":60,"active":true}
|
||||
```
|
||||
|
||||
`timerType` is 4 or 5, `secondsLeft` is an unsigned 32-bit integer, and `active`
|
||||
is a boolean. `NativeStreamRuntime` validates these fields and emits
|
||||
`sessionTimerReceived(quint8, quint32, bool)` on the Qt thread. Malformed timer
|
||||
events do not reach QML. `startId` must match the current start-command generation.
|
||||
Timer events from a replaced or stopped session, or a previous runtime instance,
|
||||
are discarded.
|
||||
|
||||
`ShellStore.acceptSessionTimer` owns warning state for the current stream. Type 4
|
||||
with `active:true` starts a display-only countdown from the server-provided seconds.
|
||||
The countdown clamps at zero and never clears the warning or ends the session.
|
||||
Type 4 with `active:false` clears the warning after the transport confirms successful input. Type 5 clears
|
||||
the warning. Qt neither clears on local input submission nor ends a session when
|
||||
a timer arrives. Session end, replacement, native restart, and terminal native
|
||||
status also clear the warning.
|
||||
|
||||
Both desktop and console compose `StreamIdleWarning` above the same
|
||||
`StreamVideoItem`. It has no input handlers, focus requests, or overlay route, so
|
||||
it does not pause gameplay input or recreate the session. A refreshed server timer
|
||||
resets the display-only countdown.
|
||||
|
||||
## Server termination
|
||||
|
||||
Terminal error and status events can include this object:
|
||||
|
||||
```json
|
||||
{"source":"nvst-server","code":"server-terminated","startId":"start-42","wireCode":2147680291,"reason":56,"extendedCode":2147680263,"resumable":false}
|
||||
```
|
||||
|
||||
`reason` and `extendedCode` can be null. `resumable` can be true, false, or null.
|
||||
The native streamer retains the numeric values through the existing JSON callback.
|
||||
Qt rejects server termination events whose `startId` does not match the current
|
||||
start-command generation, before invalidating input or presentation. The matching
|
||||
error and stopped pair can both arrive after presentation has been invalidated.
|
||||
Qt treats `nvst-server` with `code:"server-terminated"` and `resumable:false` as an
|
||||
authoritative session end, cancels pending recovery, and never auto-resumes that
|
||||
session. It retains the server message in `lastError` and the accessibility
|
||||
announcement, and the full termination object in the streamer snapshot.
|
||||
True or unknown classification follows the existing bounded
|
||||
CloudMatch reconciliation path. Existing authoritative CloudMatch status 7 and
|
||||
HTTP 404 handling remains unchanged.
|
||||
|
||||
The evidence baseline is `reports/opennow-gfn-parity/audio-input-lifecycle.md`,
|
||||
rows L3 through L5, and Windows `transport-feedback.md`, section 8. Offline Qt
|
||||
callback, orchestration, and smoke tests cover delivery and passive composition;
|
||||
they do not connect to GFN or execute GFN binaries.
|
||||
|
||||
## Text retry feedback
|
||||
|
||||
`text-input-error` carries the current `startId` and a fixed `reason`, with no
|
||||
paste contents. C++ rejects stale generations before forwarding it through the
|
||||
existing `eventReceived` signal. ShellStore announces localized retry feedback,
|
||||
without changing streamer state, input ownership, error telemetry, or incidents.
|
||||
Desktop and console display it for five seconds in the existing passive input
|
||||
notice, including when the Guide is closed. The normal stream video item and
|
||||
gameplay focus remain unchanged. Temporary SCTP deferral queues one bounded paste
|
||||
behind older input; only rejected local paste admission needs this notice.
|
||||
+3128
File diff suppressed because it is too large.
Load diff
+47
@@ -0,0 +1,47 @@
|
||||
[package]
|
||||
name = "opennow-core"
|
||||
version = "1.0.0"
|
||||
edition = "2024"
|
||||
license = "MIT"
|
||||
rust-version = "1.85"
|
||||
|
||||
[dependencies]
|
||||
aes-gcm = "0.10"
|
||||
base64 = "0.22"
|
||||
chrono = { version = "0.4", default-features = false, features = ["clock"] }
|
||||
ed25519-dalek = "2.2"
|
||||
flate2 = { version = "1", default-features = false, features = ["rust_backend"] }
|
||||
hkdf = "0.12"
|
||||
httpdate = "1"
|
||||
md5 = "0.7"
|
||||
keyring = { version = "3.6", features = ["apple-native", "windows-native", "sync-secret-service", "crypto-rust"] }
|
||||
p256 = { version = "0.13", features = ["ecdh", "pkcs8"] }
|
||||
qrcode = { version = "0.14", default-features = false }
|
||||
rand = "0.9"
|
||||
regex = "1"
|
||||
reqwest = { version = "0.12", default-features = false, features = ["blocking", "json", "multipart", "rustls-tls", "socks"] }
|
||||
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
sha2 = "0.10"
|
||||
hmac = "0.12"
|
||||
icu_normalizer = { version = "2.1", default-features = false, features = ["compiled_data"] }
|
||||
scrypt = "0.11"
|
||||
semver = "1"
|
||||
subtle = "2"
|
||||
url = "2"
|
||||
webpki-roots = "1"
|
||||
zip = { version = "2.4", default-features = false, features = ["deflate"] }
|
||||
fs2 = "0.4"
|
||||
libc = "0.2"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
windows-sys = { version = "0.59", features = ["Win32_Foundation", "Win32_Security", "Win32_Security_Authorization", "Win32_System_Threading", "Win32_System_JobObjects", "Win32_System_ProcessStatus", "Win32_System_ApplicationInstallationAndServicing", "Win32_System_SystemServices", "Win32_System_IO", "Win32_Storage_FileSystem", "Wdk_System_Threading"] }
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3"
|
||||
|
||||
[profile.release]
|
||||
lto = "thin"
|
||||
codegen-units = 1
|
||||
strip = true
|
||||
@@ -0,0 +1,116 @@
|
||||
{
|
||||
"version": 1,
|
||||
"source": "opennow-stable/src/shared/gfn/api.ts:OpenNowApi",
|
||||
"columns": ["legacy", "kind", "requestSchema", "resultSchema", "owner", "replacement", "status", "requestFixture", "resultFixture"],
|
||||
"operations": [
|
||||
["getAuthSession","request","AuthSessionRequest | undefined","AuthSessionResult","core","auth.session.get","ported",{}, {"session":null}],
|
||||
["getLoginProviders","request","void","LoginProvider[]","core","auth.providers.list","ported",{}, []],
|
||||
["getRegions","request","RegionsFetchRequest | undefined","StreamRegion[]","core","network.regions.list","ported",{}, []],
|
||||
["login","request","AuthLoginRequest","AuthSession","core","auth.device.start + auth.device.poll + auth.device.complete","superseded",{"provider":"nvidia"},{"session":null}],
|
||||
["startDeviceLogin","request","AuthDeviceLoginStartRequest","AuthDeviceLoginChallenge","core","auth.device.start","ported",{"provider":"nvidia"},{"attemptId":"fixture","userCode":"ABCD-EFGH"}],
|
||||
["pollDeviceLogin","request","AuthDeviceLoginPollRequest","AuthDeviceLoginPollResult","core","auth.device.poll","ported",{"attemptId":"fixture","deviceCode":"redacted-fixture"},{"status":"pending"}],
|
||||
["completeDeviceLogin","request","AuthDeviceLoginAttemptRequest","AuthSession","core","auth.device.complete","ported",{"attemptId":"fixture"},{"session":null}],
|
||||
["cancelDeviceLogin","request","AuthDeviceLoginAttemptRequest","void","core","auth.device.cancel","ported",{"attemptId":"fixture"},null],
|
||||
["logout","request","void","void","core","auth.logout","ported",{},null],
|
||||
["logoutAll","request","void","void","core","auth.accounts.logoutAll","ported",{},null],
|
||||
["getSavedAccounts","request","void","SavedAccount[]","core","auth.accounts.list","ported",{},[]],
|
||||
["switchAccount","request","{userId:string,pin?:string}","AuthSession","core","auth.accounts.switch","ported",{"userId":"fixture-user"},{"session":null}],
|
||||
["removeAccount","request","{userId:string}","void","core","auth.accounts.remove","ported",{"userId":"fixture-user"},null],
|
||||
["getConsolePinStatus","request","{userId:string}","ConsolePinStatus","core","auth.pin.status","ported",{"userId":"fixture-user"},{"configured":false}],
|
||||
["setConsolePin","request","ConsolePinSetRequest","ConsolePinMutationResult","core","auth.pin.set","ported",{"userId":"fixture-user","pin":"0000"},{"ok":true}],
|
||||
["clearConsolePin","request","ConsolePinClearRequest","ConsolePinMutationResult","core","auth.pin.clear","ported",{"userId":"fixture-user","pin":"0000"},{"ok":true}],
|
||||
["verifyConsolePin","request","ConsolePinVerifyRequest","ConsolePinVerifyResult","core","auth.pin.verify","ported",{"userId":"fixture-user","pin":"0000"},{"verified":true}],
|
||||
["fetchSubscription","request","SubscriptionFetchRequest","SubscriptionInfo","core","account.subscription.get","ported",{},{}],
|
||||
["fetchPersistentStorageLocations","request","PersistentStorageLocationsFetchRequest | undefined","PersistentStorageLocationsResult","core","account.storage.locations","ported",{}, {"locations":[]}],
|
||||
["resetPersistentStorage","request","PersistentStorageResetRequest | undefined","PersistentStorageResetResult","core","account.storage.reset","ported",{}, {"ok":true}],
|
||||
["fetchGameAccountConnections","request","void","GameAccountConnectionsResult","core","account.connections.list","ported",{}, {"connections":[]}],
|
||||
["linkGameAccount","request","GameAccountOperationRequest","GameAccountOperationResult","core","account.connections.link.start + account.connections.link.poll","superseded",{"provider":"steam"},{"status":"pending"}],
|
||||
["unlinkGameAccount","request","GameAccountOperationRequest","GameAccountOperationResult","core","account.connections.unlink","ported",{"provider":"steam"},{"ok":true}],
|
||||
["resyncGameAccount","request","GameAccountOperationRequest","GameAccountOperationResult","core","account.connections.sync","ported",{"provider":"steam"},{"ok":true}],
|
||||
["fetchMainGames","request","GamesFetchRequest","GameInfo[]","core","catalog.public.list","superseded",{},[]],
|
||||
["fetchStorePanels","request","GamesFetchRequest","GamePanelResult[]","core","catalog.public.list + Qt catalog projection","superseded",{},[]],
|
||||
["fetchFeaturedGames","request","GamesFetchRequest","GameInfo[]","core","catalog.public.list + Qt featured projection","superseded",{},[]],
|
||||
["fetchLibraryGames","request","GamesFetchRequest","GameInfo[]","core","catalog.library.list","ported",{},[]],
|
||||
["browseCatalog","request","CatalogBrowseRequest","CatalogBrowseResult","core","catalog.public.list + Qt filters","superseded",{}, {"games":[]}],
|
||||
["fetchPublicGames","request","void","GameInfo[]","core","catalog.public.list","ported",{},[]],
|
||||
["resolveLaunchAppId","request","ResolveLaunchIdRequest","string | null","core","catalog public/library normalized launchAppId","superseded",{"appId":"123"},"123"],
|
||||
["resolveStoreUrl","request","ResolveStoreUrlRequest","string | null","core","catalog normalized storeUrl","superseded",{"appId":"123"},null],
|
||||
["markGameOwned","request","MarkGameOwnedRequest","MarkGameOwnedResult","core","account.connections.sync + catalog.library.list","superseded",{"appId":"123"},{"ok":true}],
|
||||
["getPendingDirectLaunchRequest","request","void","DirectLaunchRequest | null","qt","AppController.handleArguments","superseded",{},null],
|
||||
["onDirectLaunchRequest","event","void","DirectLaunchRequest","qt","AppController.directLaunchRequested","ported",{}, {"appId":"123","title":"Fixture Game"}],
|
||||
["createSession","request","SessionCreateRequest","SessionInfo","core","session.create","ported",{"appId":"123"},{"status":1}],
|
||||
["pollSession","request","SessionPollRequest","SessionInfo","core","session.poll","ported",{"sessionId":"fixture-session"},{"status":1}],
|
||||
["reportSessionAd","request","SessionAdReportRequest","SessionInfo","core","session.ad.report","ported",{"sessionId":"fixture-session"},{"status":1}],
|
||||
["stopSession","request","SessionStopRequest","void","core","session.stop","ported",{"sessionId":"fixture-session"},null],
|
||||
["getActiveSessions","request","{token?:string,streamingBaseUrl?:string}","ActiveSessionInfo[]","core","session.remote.list","ported",{},[]],
|
||||
["claimSession","request","SessionClaimRequest","SessionInfo","core","session.claim","ported",{"sessionId":"fixture-session"},{"status":2}],
|
||||
["getNativeStreamerStatus","request","void","NativeStreamerStatus","core","streamer.status.get","ported",{}, {"state":"idle"}],
|
||||
["getNativeCloudGsyncCapabilities","request","void","NativeCloudGsyncCapabilities","streamer","capabilities event surfaced by streamer.status.get","superseded",{}, {"supported":false}],
|
||||
["showSessionConflictDialog","request","void","SessionConflictChoice","qt","SessionConflictScreen","ported",{},"cancel"],
|
||||
["connectSignaling","request","SignalingConnectRequest","void","core","streamer.start owns signaling lifecycle","superseded",{},null],
|
||||
["disconnectSignaling","request","void","void","core","streamer.stop owns signaling lifecycle","superseded",{},null],
|
||||
["sendAnswer","request","SendAnswerRequest","void","core","streamer.start child protocol relay","superseded",{},null],
|
||||
["sendIceCandidate","request","IceCandidatePayload","void","core","streamer.start child protocol relay","superseded",{},null],
|
||||
["sendNativeInput","send","NativeInputPacket","void","streamer","native input transport","superseded",{"kind":"keyboard"},null],
|
||||
["setNativeInputPaused","send","boolean","void","core","streamer.input.pause","ported",{"paused":true},null],
|
||||
["updateNativeRenderSurface","send","NativeRenderSurfaceUpdate","void","core","streamer.surface.update","ported",{"visible":true},null],
|
||||
["updateNativeShortcuts","send","NativeStreamerShortcutBindings","void","streamer","streamer.start shortcut bindings","superseded",{},null],
|
||||
["requestKeyframe","request","KeyframeRequest","void","core","streamer.control request-keyframe","superseded",{},null],
|
||||
["onSignalingEvent","event","void","MainToRendererSignalingEvent","core","streamer.changed event","superseded",{}, {"type":"status"}],
|
||||
["onToggleFullscreen","event","void","void","qt","streamer shortcut routed through ShellStore","superseded",{},null],
|
||||
["onExitFullscreen","event","void","void","qt","deterministic overlay/back handling","superseded",{},null],
|
||||
["quitApp","request","void","void","qt","QCoreApplication.quit","ported",{},null],
|
||||
["getUpdaterState","request","void","AppUpdaterState","core","updater.state.get","ported",{}, {"status":"idle"}],
|
||||
["checkForUpdates","request","void","AppUpdaterState","core","updater.check","ported",{}, {"status":"not-available"}],
|
||||
["downloadUpdate","request","void","AppUpdaterState","core","updater.download","ported",{}, {"status":"downloaded"}],
|
||||
["installUpdateAndRestart","request","void","AppUpdaterState","core","updater.install","ported",{}, {"status":"installing"}],
|
||||
["onUpdaterStateChanged","event","void","AppUpdaterState","core","updater.changed event","ported",{}, {"status":"idle"}],
|
||||
["setFullscreen","request","boolean","void","qt","Window visibility/fullscreen state","ported",{"value":true},null],
|
||||
["toggleFullscreen","request","void","void","qt","Window visibility/fullscreen state","ported",{},null],
|
||||
["togglePointerLock","request","void","void","streamer","native input ownership contract","superseded",{},null],
|
||||
["notifyPointerLockChange","send","{active:boolean,suppressEscapeFullscreenGrace?:boolean}","void","streamer","native input ownership contract","superseded",{"active":true},null],
|
||||
["notifyNativeInputModeChange","send","{active:boolean,rawInputOwnsEscape:boolean}","void","streamer","streamer.input.pause + native ownership","superseded",{"active":true,"rawInputOwnsEscape":true},null],
|
||||
["readClipboardText","request","void","string","qt","AppController.readClipboardText","ported",{},"fixture text"],
|
||||
["getSettings","request","void","Settings","core","settings.get","ported",{},{}],
|
||||
["setSetting","request","{key:keyof Settings,value:Settings[key]}","void","core","settings.set","ported",{"key":"resolution","value":"1920x1080"},null],
|
||||
["getGpuInfo","request","void","GpuBackendInfo","core","streamer.detect","superseded",{}, {"gpuName":null,"hardwareDecodeCodecs":[]}],
|
||||
["resetSettings","request","void","Settings","core","settings.reset","ported",{},{}],
|
||||
["selectNativeStreamerExecutable","request","void","string | null","qt","AppController.normalizeNativeStreamerExecutable + FileDialog","ported",{},null],
|
||||
["getMicrophonePermission","request","void","MicrophonePermissionResult","streamer","native microphone permission/start status","superseded",{}, {"status":"unknown"}],
|
||||
["exportLogs","request","'text' | 'json' | undefined","string","core","diagnostics.export","ported",{"format":"text"},"/tmp/opennow-diagnostics.txt"],
|
||||
["submitBugReport","request","DesktopBugReportRequest","DesktopBugReportReceipt","core","bug_report.submit","ported",{"summary":"Fixture report"},{"ok":true}],
|
||||
["pingRegions","request","StreamRegion[]","PingResult[]","core","network.regions.ping","ported",{"regions":[]},[]],
|
||||
["saveScreenshot","request","ScreenshotSaveRequest","ScreenshotEntry","qt","AppController.captureScreenRegion + media.list","superseded",{}, {"id":"fixture.png","kind":"screenshot"}],
|
||||
["listScreenshots","request","void","ScreenshotEntry[]","core","media.list screenshot projection","superseded",{},[]],
|
||||
["deleteScreenshot","request","ScreenshotDeleteRequest","void","core","media.delete","superseded",{"id":"fixture.png"},null],
|
||||
["saveScreenshotAs","request","ScreenshotSaveAsRequest","ScreenshotSaveAsResult","qt","AppController.copyScreenshotTo + FileDialog","ported",{"id":"fixture.png"},{"cancelled":true}],
|
||||
["onTriggerScreenshot","event","void","void","streamer","screenshotRequestGeneration in streamer.changed","superseded",{},null],
|
||||
["onExternalEscape","event","void","void","qt","deterministic back/overlay state","superseded",{},null],
|
||||
["onStreamShortcutAction","event","void","NativeStreamerShortcutAction","streamer","shortcut action in streamer.changed","superseded",{},"screenshot"],
|
||||
["setStreamShortcutInterceptionGate","send","StreamShortcutInterceptionGate","void","streamer","streamer.input.pause ownership gate","superseded",{"enabled":true},null],
|
||||
["openExternalUrl","request","string","void","qt","AppController.openExternalUrl","ported",{"url":"https://opennow.app"},null],
|
||||
["beginRecording","request","RecordingBeginRequest","RecordingBeginResult","core","media.recording.target + streamer.recording.start","superseded",{}, {"recordingId":"fixture"}],
|
||||
["sendRecordingChunk","request","RecordingChunkRequest","void","streamer","source-stream recorder tap","superseded",{},null],
|
||||
["finishRecording","request","RecordingFinishRequest","RecordingEntry","core","streamer.recording.stop","superseded",{}, {"id":"fixture.mkv","kind":"recording"}],
|
||||
["abortRecording","request","RecordingAbortRequest","void","streamer","recording disconnect/fail-closed cleanup","superseded",{},null],
|
||||
["listRecordings","request","void","RecordingEntry[]","core","media.list recording projection","superseded",{},[]],
|
||||
["deleteRecording","request","RecordingDeleteRequest","void","core","media.delete","superseded",{"id":"fixture.mkv"},null],
|
||||
["showRecordingInFolder","request","string","void","qt","AppController.openLocalPath(reveal)","ported",{"id":"fixture.mkv"},null],
|
||||
["listMediaByGame","request","{gameTitle?:string} | undefined","MediaListingResult","core","media.list","ported",{}, {"entries":[]}],
|
||||
["getMediaThumbnail","request","{filePath:string}","string | null","core","media.list thumbnailPath","superseded",{"filePath":"fixture.mkv"},null],
|
||||
["showMediaInFolder","request","{filePath:string}","void","qt","AppController.openLocalPath(reveal)","ported",{"filePath":"fixture.mkv"},null],
|
||||
["getMediaPlaybackUrl","request","{filePath:string}","string | null","qt","trusted local media URL in QML","superseded",{"filePath":"fixture.mkv"},null],
|
||||
["deleteMediaFile","request","{filePath:string}","{ok:boolean}","core","media.delete","superseded",{"filePath":"fixture.mkv"},{"ok":true}],
|
||||
["regenMediaThumbnail","request","{filePath:string}","{ok:boolean,thumbnailDataUrl:string|null}","qt","ThumbnailGenerator.regenerate","superseded",{"filePath":"fixture.mkv"},{"ok":true,"thumbnailDataUrl":null}],
|
||||
["deleteCache","request","void","void","core","cache.delete","ported",{},null],
|
||||
["fetchPrintedWasteQueue","request","void","PrintedWasteQueueData","core","queue.status.get","ported",{}, {"zone-fixture":{"QueuePosition":1,"Last Updated":1,"Region":"EU"}}],
|
||||
["fetchPrintedWasteServerMapping","request","void","PrintedWasteServerMapping","core","queue.serverMapping.get","ported",{}, {"zone-fixture":{"title":"Fixture","region":"EU"}}],
|
||||
["getThanksData","request","void","ThankYouDataResult","core","thanks.data.get","ported",{}, {"contributors":[],"supporters":[]}],
|
||||
["provisionZortosCommunityProxy","request","void","CommunityProxyProvisionResult","core","communityProxy.provision","ported",{}, {"proxyUrl":"http://fixture:fixture@altaria.proxy.rlwy.net:51545/"}],
|
||||
["setDiscordActivity","request","DiscordActivityUpdate","void","core","discord.activity.sync","ported",{},null],
|
||||
["clearDiscordActivity","request","void","void","core","discord.activity.clear","ported",{},null],
|
||||
["getReleaseHighlights","request","string | undefined","ReleaseHighlightsPayload","core","updater.highlights.get","ported",{"version":"0.5.4"},{"version":"0.5.4"}],
|
||||
["ackReleaseHighlights","request","void","void","core","updater.highlights.ack","ported",{},null],
|
||||
["onReleaseHighlightsShow","event","void","ReleaseHighlightsPayload","core","updater.highlights.show event","ported",{}, {"version":"0.5.4"}]
|
||||
]
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "https://opennow.app/contracts/legacy-open-now-api.schema.json",
|
||||
"title": "OpenNOW Electron bridge parity inventory",
|
||||
"type": "object",
|
||||
"required": ["version", "source", "columns", "operations"],
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
"version": { "const": 1 },
|
||||
"source": { "type": "string", "minLength": 1 },
|
||||
"columns": {
|
||||
"const": ["legacy", "kind", "requestSchema", "resultSchema", "owner", "replacement", "status", "requestFixture", "resultFixture"]
|
||||
},
|
||||
"operations": {
|
||||
"type": "array",
|
||||
"minItems": 109,
|
||||
"maxItems": 109,
|
||||
"items": {
|
||||
"type": "array",
|
||||
"prefixItems": [
|
||||
{ "type": "string", "pattern": "^[A-Za-z][A-Za-z0-9]*$" },
|
||||
{ "enum": ["request", "send", "event"] },
|
||||
{ "type": "string", "minLength": 1 },
|
||||
{ "type": "string", "minLength": 1 },
|
||||
{ "enum": ["core", "qt", "streamer"] },
|
||||
{ "type": "string", "minLength": 1 },
|
||||
{ "enum": ["ported", "superseded", "pending"] },
|
||||
{},
|
||||
{}
|
||||
],
|
||||
"minItems": 9,
|
||||
"maxItems": 9
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,863 @@
|
||||
use reqwest::blocking::Client;
|
||||
use serde_json::{Map, Value, json};
|
||||
use std::collections::VecDeque;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Condvar, Mutex, mpsc};
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
const MAX_QUEUED_EVENTS: usize = 1_000;
|
||||
const FLUSH_INTERVAL: Duration = Duration::from_secs(30);
|
||||
const FLUSH_THRESHOLD: usize = 50;
|
||||
const MAX_BATCH_EVENTS: usize = 100;
|
||||
const MAX_SPOOL_BYTES: usize = 1024 * 1024;
|
||||
const SHUTDOWN_BUDGET: Duration = Duration::from_millis(1_000);
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum Emitter {
|
||||
Core,
|
||||
Shell,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum Prop {
|
||||
Text(usize),
|
||||
Id,
|
||||
Code,
|
||||
Number(f64),
|
||||
Bool,
|
||||
Choice(&'static [&'static str]),
|
||||
Resolution,
|
||||
}
|
||||
|
||||
const STAGES: &[&str] = &[
|
||||
"create",
|
||||
"poll",
|
||||
"claim",
|
||||
"prepare",
|
||||
"start",
|
||||
"stream",
|
||||
"presentation",
|
||||
];
|
||||
const REPORT_KINDS: &[&str] = &[
|
||||
"session_error",
|
||||
"stream_error",
|
||||
"frame_drops",
|
||||
"library_error",
|
||||
"manual",
|
||||
];
|
||||
|
||||
type EventSpec = (&'static str, Emitter, &'static [(&'static str, Prop)]);
|
||||
|
||||
const EVENTS: &[EventSpec] = &[
|
||||
(
|
||||
"app_opened",
|
||||
Emitter::Core,
|
||||
&[
|
||||
("gpu_vendor", Prop::Code),
|
||||
(
|
||||
"package_kind",
|
||||
Prop::Choice(&["msi", "appimage", "flatpak", "deb", "dmg", "zip"]),
|
||||
),
|
||||
],
|
||||
),
|
||||
(
|
||||
"signed_in",
|
||||
Emitter::Core,
|
||||
&[
|
||||
("provider_code", Prop::Id),
|
||||
("alliance_partner", Prop::Bool),
|
||||
("membership_tier", Prop::Id),
|
||||
("restored", Prop::Bool),
|
||||
],
|
||||
),
|
||||
(
|
||||
"game_launch_requested",
|
||||
Emitter::Core,
|
||||
&[
|
||||
("game_id", Prop::Id),
|
||||
("game_title", Prop::Text(120)),
|
||||
("store", Prop::Text(64)),
|
||||
("zone", Prop::Id),
|
||||
],
|
||||
),
|
||||
(
|
||||
"session_started",
|
||||
Emitter::Shell,
|
||||
&[
|
||||
("game_id", Prop::Id),
|
||||
("game_title", Prop::Text(120)),
|
||||
("codec", Prop::Choice(&["h264", "h265", "av1"])),
|
||||
("resolution", Prop::Resolution),
|
||||
("fps_target", Prop::Number(1_000.0)),
|
||||
("hdr", Prop::Bool),
|
||||
("decoder_backend", Prop::Code),
|
||||
("first_frame_ms", Prop::Number(3_600_000.0)),
|
||||
("queue_wait_s", Prop::Number(86_400.0)),
|
||||
],
|
||||
),
|
||||
(
|
||||
"session_ended",
|
||||
Emitter::Shell,
|
||||
&[
|
||||
("game_id", Prop::Id),
|
||||
("game_title", Prop::Text(120)),
|
||||
("duration_s", Prop::Number(604_800.0)),
|
||||
(
|
||||
"outcome",
|
||||
Prop::Choice(&["clean", "user_stopped", "remote_ended", "error"]),
|
||||
),
|
||||
("error_code", Prop::Code),
|
||||
("recoveries", Prop::Number(100_000.0)),
|
||||
("video_drop_count", Prop::Number(1e12)),
|
||||
("avg_fps", Prop::Number(1_000.0)),
|
||||
("avg_ping_ms", Prop::Number(100_000.0)),
|
||||
("avg_packet_loss_pct", Prop::Number(100.0)),
|
||||
("decoder_errors", Prop::Number(1e12)),
|
||||
],
|
||||
),
|
||||
(
|
||||
"session_error",
|
||||
Emitter::Shell,
|
||||
&[
|
||||
("stage", Prop::Choice(STAGES)),
|
||||
("code", Prop::Code),
|
||||
("game_id", Prop::Id),
|
||||
],
|
||||
),
|
||||
(
|
||||
"frame_drops_detected",
|
||||
Emitter::Shell,
|
||||
&[
|
||||
("dropped", Prop::Number(1e12)),
|
||||
("window_s", Prop::Number(3_600.0)),
|
||||
("game_id", Prop::Id),
|
||||
("decoder_backend", Prop::Code),
|
||||
],
|
||||
),
|
||||
(
|
||||
"library_load_failed",
|
||||
Emitter::Core,
|
||||
&[("code", Prop::Code)],
|
||||
),
|
||||
(
|
||||
"bug_report_sent",
|
||||
Emitter::Core,
|
||||
&[
|
||||
("report_id", Prop::Id),
|
||||
("issue_id", Prop::Id),
|
||||
("kind", Prop::Choice(REPORT_KINDS)),
|
||||
("code", Prop::Code),
|
||||
("automatic", Prop::Bool),
|
||||
],
|
||||
),
|
||||
(
|
||||
"consent_changed",
|
||||
Emitter::Core,
|
||||
&[
|
||||
("value", Prop::Choice(&["enabled", "disabled"])),
|
||||
(
|
||||
"source",
|
||||
Prop::Choice(&["signin_notice", "first_run_sheet", "settings"]),
|
||||
),
|
||||
],
|
||||
),
|
||||
];
|
||||
|
||||
pub struct ShellEvent {
|
||||
pub name: &'static str,
|
||||
pub props: Map<String, Value>,
|
||||
}
|
||||
|
||||
pub fn ui_surface(params: &Value) -> Result<Option<&'static str>, String> {
|
||||
match params["uiSurface"].as_str() {
|
||||
None => Ok(None),
|
||||
Some("desktop") => Ok(Some("desktop")),
|
||||
Some("console") => Ok(Some("console")),
|
||||
Some(_) => Err("Unsupported analytics surface".to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn shell_event(params: &Value) -> Result<ShellEvent, String> {
|
||||
let name = params["event"].as_str().unwrap_or_default();
|
||||
let (name, emitter, _) = EVENTS
|
||||
.iter()
|
||||
.find(|(event, _, _)| *event == name)
|
||||
.ok_or_else(|| "Unsupported analytics event".to_owned())?;
|
||||
if *emitter != Emitter::Shell {
|
||||
return Err("Unsupported analytics event".to_owned());
|
||||
}
|
||||
Ok(ShellEvent {
|
||||
name,
|
||||
props: sanitize(name, ¶ms["props"]),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn sanitize(event: &str, props: &Value) -> Map<String, Value> {
|
||||
let allowed = EVENTS
|
||||
.iter()
|
||||
.find(|(name, _, _)| *name == event)
|
||||
.map_or(&[][..], |(_, _, props)| *props);
|
||||
let mut clean = Map::new();
|
||||
for (key, prop) in allowed {
|
||||
let value = &props[*key];
|
||||
let accepted = match prop {
|
||||
Prop::Text(limit) => value
|
||||
.as_str()
|
||||
.map(|text| bounded_text(text, *limit))
|
||||
.filter(|text| !text.is_empty())
|
||||
.map(Value::from),
|
||||
Prop::Id => value
|
||||
.as_str()
|
||||
.filter(|text| {
|
||||
(1..=64).contains(&text.len())
|
||||
&& text.bytes().all(|byte| {
|
||||
byte.is_ascii_alphanumeric()
|
||||
|| matches!(byte, b'_' | b'-' | b'.' | b':')
|
||||
})
|
||||
})
|
||||
.map(Value::from),
|
||||
Prop::Code => value
|
||||
.as_str()
|
||||
.filter(|text| valid_code(text))
|
||||
.map(Value::from),
|
||||
Prop::Number(maximum) => value
|
||||
.as_f64()
|
||||
.filter(|number| number.is_finite() && (0.0..=*maximum).contains(number))
|
||||
.map(|number| (number * 100.0).round() / 100.0)
|
||||
.map(|number| {
|
||||
if number.fract() == 0.0 && number < 9_007_199_254_740_992.0 {
|
||||
json!(number as u64)
|
||||
} else {
|
||||
json!(number)
|
||||
}
|
||||
}),
|
||||
Prop::Bool => value.as_bool().map(Value::from),
|
||||
Prop::Choice(choices) => value
|
||||
.as_str()
|
||||
.filter(|text| choices.contains(text))
|
||||
.map(Value::from),
|
||||
Prop::Resolution => value
|
||||
.as_str()
|
||||
.filter(|text| {
|
||||
text.split_once('x').is_some_and(|(width, height)| {
|
||||
[width, height].iter().all(|part| {
|
||||
(1..=5).contains(&part.len())
|
||||
&& part.bytes().all(|b| b.is_ascii_digit())
|
||||
})
|
||||
})
|
||||
})
|
||||
.map(Value::from),
|
||||
};
|
||||
if let Some(accepted) = accepted {
|
||||
clean.insert((*key).to_owned(), accepted);
|
||||
}
|
||||
}
|
||||
clean
|
||||
}
|
||||
|
||||
pub fn valid_code(value: &str) -> bool {
|
||||
(1..=64).contains(&value.len())
|
||||
&& value
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'_')
|
||||
}
|
||||
|
||||
pub fn gpu_vendor(name: Option<&str>) -> &'static str {
|
||||
let Some(name) = name.map(str::to_ascii_lowercase) else {
|
||||
return "unknown";
|
||||
};
|
||||
[
|
||||
("nvidia", &["nvidia", "geforce", "quadro", "rtx", "gtx"][..]),
|
||||
("amd", &["amd", "radeon", "ati "]),
|
||||
("intel", &["intel", "iris", "arc "]),
|
||||
("apple", &["apple"]),
|
||||
("qualcomm", &["qualcomm", "adreno"]),
|
||||
]
|
||||
.iter()
|
||||
.find(|(_, needles)| needles.iter().any(|needle| name.contains(needle)))
|
||||
.map_or("other", |(vendor, _)| vendor)
|
||||
}
|
||||
|
||||
fn bounded_text(value: &str, limit: usize) -> String {
|
||||
value
|
||||
.chars()
|
||||
.map(|character| {
|
||||
if character.is_control() {
|
||||
' '
|
||||
} else {
|
||||
character
|
||||
}
|
||||
})
|
||||
.take(limit)
|
||||
.collect::<String>()
|
||||
.trim()
|
||||
.to_owned()
|
||||
}
|
||||
|
||||
struct Queued {
|
||||
install_id: String,
|
||||
account: Value,
|
||||
event: Value,
|
||||
}
|
||||
|
||||
struct State {
|
||||
queue: VecDeque<Queued>,
|
||||
dropped: u64,
|
||||
generation: u64,
|
||||
ui_surface: &'static str,
|
||||
signed_in_user: Option<String>,
|
||||
app_opened: bool,
|
||||
device: Value,
|
||||
shutdown: bool,
|
||||
}
|
||||
|
||||
pub struct AnalyticsService {
|
||||
client: Client,
|
||||
endpoint: String,
|
||||
spool_path: PathBuf,
|
||||
state: Mutex<State>,
|
||||
wake: Condvar,
|
||||
spool_lock: Mutex<()>,
|
||||
}
|
||||
|
||||
impl AnalyticsService {
|
||||
pub fn new(base: &str, data_dir: &Path) -> Result<Self, String> {
|
||||
Ok(Self {
|
||||
client: Client::builder()
|
||||
.connect_timeout(Duration::from_secs(10))
|
||||
.timeout(Duration::from_secs(20))
|
||||
.build()
|
||||
.map_err(|error| error.to_string())?,
|
||||
endpoint: format!("{base}/v1/events"),
|
||||
spool_path: data_dir.join("analytics").join("spool.jsonl"),
|
||||
state: Mutex::new(State {
|
||||
queue: VecDeque::new(),
|
||||
dropped: 0,
|
||||
generation: 0,
|
||||
ui_surface: "desktop",
|
||||
signed_in_user: None,
|
||||
app_opened: false,
|
||||
device: json!({"gpu":null, "decoderBackend":null}),
|
||||
shutdown: false,
|
||||
}),
|
||||
wake: Condvar::new(),
|
||||
spool_lock: Mutex::new(()),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn start(self: &Arc<Self>) -> Result<mpsc::Receiver<()>, String> {
|
||||
let (done, finished) = mpsc::channel();
|
||||
let service = Arc::clone(self);
|
||||
thread::Builder::new()
|
||||
.name("opennow-analytics".to_owned())
|
||||
.spawn(move || {
|
||||
service.run();
|
||||
let _ = done.send(());
|
||||
})
|
||||
.map_err(|error| error.to_string())?;
|
||||
Ok(finished)
|
||||
}
|
||||
|
||||
pub fn shutdown(&self, finished: &mpsc::Receiver<()>) {
|
||||
self.state.lock().expect("analytics poisoned").shutdown = true;
|
||||
self.wake.notify_all();
|
||||
let _ = finished.recv_timeout(SHUTDOWN_BUDGET + Duration::from_millis(200));
|
||||
}
|
||||
|
||||
pub fn enqueue(
|
||||
&self,
|
||||
install_id: &str,
|
||||
account: Value,
|
||||
event: &str,
|
||||
props: Map<String, Value>,
|
||||
) -> u64 {
|
||||
let mut state = self.state.lock().expect("analytics poisoned");
|
||||
let mut props = props;
|
||||
props.insert(
|
||||
"app_version".to_owned(),
|
||||
json!(crate::version::APPLICATION_VERSION),
|
||||
);
|
||||
props.insert("os".to_owned(), json!(std::env::consts::OS));
|
||||
props.insert("arch".to_owned(), json!(std::env::consts::ARCH));
|
||||
props.insert("ui_surface".to_owned(), json!(state.ui_surface));
|
||||
props.insert(
|
||||
"channel".to_owned(),
|
||||
json!(crate::version::update_channel(
|
||||
crate::version::APPLICATION_VERSION
|
||||
)),
|
||||
);
|
||||
if state.queue.len() >= MAX_QUEUED_EVENTS {
|
||||
state.queue.pop_front();
|
||||
state.dropped += 1;
|
||||
}
|
||||
state.queue.push_back(Queued {
|
||||
install_id: install_id.to_owned(),
|
||||
account,
|
||||
event: json!({"event":event, "ts":now_ms(), "props":props}),
|
||||
});
|
||||
if state.queue.len() >= FLUSH_THRESHOLD {
|
||||
self.wake.notify_all();
|
||||
}
|
||||
state.dropped
|
||||
}
|
||||
|
||||
pub fn wipe(&self) {
|
||||
let _spool = self.spool_lock.lock().expect("analytics spool poisoned");
|
||||
let mut state = self.state.lock().expect("analytics poisoned");
|
||||
state.queue.clear();
|
||||
state.generation += 1;
|
||||
drop(state);
|
||||
let _ = fs::remove_file(&self.spool_path);
|
||||
}
|
||||
|
||||
pub fn set_ui_surface(&self, surface: &'static str) {
|
||||
self.state.lock().expect("analytics poisoned").ui_surface = surface;
|
||||
}
|
||||
|
||||
pub fn observe_sign_in(&self, user_id: &str) -> bool {
|
||||
let mut state = self.state.lock().expect("analytics poisoned");
|
||||
if state.signed_in_user.as_deref() == Some(user_id) {
|
||||
return false;
|
||||
}
|
||||
state.signed_in_user = Some(user_id.to_owned());
|
||||
true
|
||||
}
|
||||
|
||||
pub fn observe_sign_out(&self) {
|
||||
self.state
|
||||
.lock()
|
||||
.expect("analytics poisoned")
|
||||
.signed_in_user = None;
|
||||
}
|
||||
|
||||
pub fn observe_runtime(&self, capabilities: &Value) {
|
||||
let gpu = crate::streamer::active_gpu_label(capabilities);
|
||||
let backend = capabilities["videoBackends"]
|
||||
.as_array()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter(|backend| backend["available"].as_bool() == Some(true))
|
||||
.find_map(|backend| backend["backend"].as_str())
|
||||
.filter(|backend| valid_code(backend));
|
||||
if gpu.is_some() || backend.is_some() {
|
||||
self.state.lock().expect("analytics poisoned").device =
|
||||
json!({"gpu":gpu, "decoderBackend":backend});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_app(&self, capabilities: &Value) -> Option<Map<String, Value>> {
|
||||
self.observe_runtime(capabilities);
|
||||
let mut state = self.state.lock().expect("analytics poisoned");
|
||||
if std::mem::replace(&mut state.app_opened, true) {
|
||||
return None;
|
||||
}
|
||||
let mut props = Map::new();
|
||||
props.insert(
|
||||
"gpu_vendor".to_owned(),
|
||||
json!(gpu_vendor(state.device["gpu"].as_str())),
|
||||
);
|
||||
if let Some(kind) = package_kind() {
|
||||
props.insert("package_kind".to_owned(), json!(kind));
|
||||
}
|
||||
Some(props)
|
||||
}
|
||||
|
||||
pub fn device(&self) -> Value {
|
||||
self.state
|
||||
.lock()
|
||||
.expect("analytics poisoned")
|
||||
.device
|
||||
.clone()
|
||||
}
|
||||
|
||||
fn run(&self) {
|
||||
let generation = self.state.lock().expect("analytics poisoned").generation;
|
||||
self.deliver(Vec::new(), generation, None);
|
||||
loop {
|
||||
let (batches, generation, deadline) = {
|
||||
let mut state = self.state.lock().expect("analytics poisoned");
|
||||
let wake_at = Instant::now() + FLUSH_INTERVAL;
|
||||
while !state.shutdown && state.queue.len() < FLUSH_THRESHOLD {
|
||||
let now = Instant::now();
|
||||
if now >= wake_at {
|
||||
break;
|
||||
}
|
||||
state = self
|
||||
.wake
|
||||
.wait_timeout(state, wake_at - now)
|
||||
.expect("analytics poisoned")
|
||||
.0;
|
||||
}
|
||||
let deadline = state.shutdown.then(|| Instant::now() + SHUTDOWN_BUDGET);
|
||||
(batches(state.queue.drain(..)), state.generation, deadline)
|
||||
};
|
||||
if !batches.is_empty() || deadline.is_none() {
|
||||
self.deliver(batches, generation, deadline);
|
||||
}
|
||||
if deadline.is_some() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn deliver(&self, batches: Vec<Value>, generation: u64, deadline: Option<Instant>) {
|
||||
let mut failed = Vec::new();
|
||||
for batch in batches {
|
||||
if !failed.is_empty() || self.post(&batch, deadline) == Delivery::Retry {
|
||||
failed.push(batch);
|
||||
}
|
||||
}
|
||||
if !failed.is_empty() {
|
||||
self.append_spool(&failed, generation);
|
||||
} else if deadline.is_none() {
|
||||
self.resend_spool(generation);
|
||||
}
|
||||
}
|
||||
|
||||
fn post(&self, batch: &Value, deadline: Option<Instant>) -> Delivery {
|
||||
let mut request = self.client.post(&self.endpoint).json(batch);
|
||||
if let Some(deadline) = deadline {
|
||||
let remaining = deadline.saturating_duration_since(Instant::now());
|
||||
if remaining.is_zero() {
|
||||
return Delivery::Retry;
|
||||
}
|
||||
request = request.timeout(remaining);
|
||||
}
|
||||
match request.send() {
|
||||
Ok(response) if response.status().is_success() => Delivery::Sent,
|
||||
Ok(response)
|
||||
if response.status().is_client_error()
|
||||
&& !matches!(response.status().as_u16(), 408 | 429) =>
|
||||
{
|
||||
Delivery::Rejected
|
||||
}
|
||||
_ => Delivery::Retry,
|
||||
}
|
||||
}
|
||||
|
||||
fn append_spool(&self, batches: &[Value], generation: u64) {
|
||||
let _spool = self.spool_lock.lock().expect("analytics spool poisoned");
|
||||
if self.state.lock().expect("analytics poisoned").generation != generation {
|
||||
return;
|
||||
}
|
||||
let mut lines = read_lines(&self.spool_path);
|
||||
lines.extend(batches.iter().map(Value::to_string));
|
||||
self.write_spool(lines);
|
||||
}
|
||||
|
||||
fn resend_spool(&self, generation: u64) {
|
||||
let lines = {
|
||||
let _spool = self.spool_lock.lock().expect("analytics spool poisoned");
|
||||
read_lines(&self.spool_path)
|
||||
};
|
||||
if lines.is_empty() {
|
||||
return;
|
||||
}
|
||||
let mut sent = 0;
|
||||
for line in &lines {
|
||||
let Ok(batch) = serde_json::from_str::<Value>(line) else {
|
||||
sent += 1;
|
||||
continue;
|
||||
};
|
||||
if self.post(&batch, None) == Delivery::Retry {
|
||||
break;
|
||||
}
|
||||
sent += 1;
|
||||
}
|
||||
let _spool = self.spool_lock.lock().expect("analytics spool poisoned");
|
||||
if self.state.lock().expect("analytics poisoned").generation != generation {
|
||||
return;
|
||||
}
|
||||
let mut current = read_lines(&self.spool_path);
|
||||
current.drain(..sent.min(current.len()));
|
||||
self.write_spool(current);
|
||||
}
|
||||
|
||||
fn write_spool(&self, mut lines: Vec<String>) {
|
||||
let mut total = lines.iter().map(|line| line.len() + 1).sum::<usize>();
|
||||
let mut skip = 0;
|
||||
while total > MAX_SPOOL_BYTES && skip < lines.len() {
|
||||
total -= lines[skip].len() + 1;
|
||||
skip += 1;
|
||||
}
|
||||
lines.drain(..skip);
|
||||
if lines.is_empty() {
|
||||
let _ = fs::remove_file(&self.spool_path);
|
||||
return;
|
||||
}
|
||||
let Some(parent) = self.spool_path.parent() else {
|
||||
return;
|
||||
};
|
||||
let temporary = self.spool_path.with_extension("jsonl.tmp");
|
||||
let mut text = lines.join("\n");
|
||||
text.push('\n');
|
||||
if fs::create_dir_all(parent).is_ok() && fs::write(&temporary, text).is_ok() {
|
||||
let _ = fs::rename(&temporary, &self.spool_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
enum Delivery {
|
||||
Sent,
|
||||
Rejected,
|
||||
Retry,
|
||||
}
|
||||
|
||||
fn batches(queue: impl Iterator<Item = Queued>) -> Vec<Value> {
|
||||
let mut batches: Vec<(String, Value, Vec<Value>)> = Vec::new();
|
||||
for item in queue {
|
||||
match batches.last_mut() {
|
||||
Some((install_id, account, events))
|
||||
if *install_id == item.install_id
|
||||
&& *account == item.account
|
||||
&& events.len() < MAX_BATCH_EVENTS =>
|
||||
{
|
||||
events.push(item.event)
|
||||
}
|
||||
_ => batches.push((item.install_id, item.account, vec![item.event])),
|
||||
}
|
||||
}
|
||||
batches
|
||||
.into_iter()
|
||||
.map(|(install_id, account, events)| {
|
||||
json!({"installId":install_id, "account":account, "events":events})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn read_lines(path: &Path) -> Vec<String> {
|
||||
fs::read_to_string(path)
|
||||
.map(|text| {
|
||||
text.lines()
|
||||
.filter(|line| !line.trim().is_empty())
|
||||
.map(ToOwned::to_owned)
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn package_kind() -> Option<&'static str> {
|
||||
if opennow_core::update_apply::external_update_message().is_some() {
|
||||
return Some("flatpak");
|
||||
}
|
||||
opennow_core::update_apply::compatible_package_extension().ok()
|
||||
}
|
||||
|
||||
fn now_ms() -> u64 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map_or(0, |elapsed| elapsed.as_millis() as u64)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::io::{BufRead, BufReader, Read, Write};
|
||||
use std::net::TcpListener;
|
||||
|
||||
const INSTALL: &str = "0123456789abcdef0123456789abcdef";
|
||||
|
||||
fn service(base: &str) -> (tempfile::TempDir, AnalyticsService) {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let service = AnalyticsService::new(base, directory.path()).unwrap();
|
||||
(directory, service)
|
||||
}
|
||||
|
||||
fn server(statuses: Vec<u16>) -> (String, thread::JoinHandle<Vec<Value>>) {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let base = format!("http://{}", listener.local_addr().unwrap());
|
||||
let worker = thread::spawn(move || {
|
||||
let mut bodies = Vec::new();
|
||||
for status in statuses {
|
||||
let (mut stream, _) = listener.accept().unwrap();
|
||||
let mut reader = BufReader::new(&stream);
|
||||
let mut length = 0;
|
||||
let mut line = String::new();
|
||||
reader.read_line(&mut line).unwrap();
|
||||
assert_eq!(line.trim(), "POST /v1/events HTTP/1.1");
|
||||
loop {
|
||||
line.clear();
|
||||
reader.read_line(&mut line).unwrap();
|
||||
if line == "\r\n" {
|
||||
break;
|
||||
}
|
||||
if let Some(value) = line.to_ascii_lowercase().strip_prefix("content-length:") {
|
||||
length = value.trim().parse().unwrap();
|
||||
}
|
||||
}
|
||||
let mut body = vec![0; length];
|
||||
reader.read_exact(&mut body).unwrap();
|
||||
bodies.push(serde_json::from_slice(&body).unwrap());
|
||||
write!(stream, "HTTP/1.1 {status} Fixture\r\nContent-Length: 2\r\nConnection: close\r\n\r\n{{}}").unwrap();
|
||||
}
|
||||
bodies
|
||||
});
|
||||
(base, worker)
|
||||
}
|
||||
|
||||
fn drain(service: &AnalyticsService) -> (Vec<Value>, u64) {
|
||||
let mut state = service.state.lock().unwrap();
|
||||
(batches(state.queue.drain(..)), state.generation)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shell_events_are_allowlisted_and_validated() {
|
||||
let event = shell_event(&json!({"event":"session_ended", "uiSurface":"console", "props":{
|
||||
"game_id":"100", "game_title":"Portal\u{0007} 2", "duration_s":61.256, "outcome":"clean",
|
||||
"error_code":"Bad Code", "avg_fps":-1, "avg_ping_ms":23.4, "token":"secret", "recoveries":0.0,
|
||||
"avg_packet_loss_pct":140
|
||||
}}))
|
||||
.unwrap();
|
||||
assert_eq!(event.name, "session_ended");
|
||||
assert_eq!(
|
||||
ui_surface(&json!({"uiSurface":"console"})),
|
||||
Ok(Some("console"))
|
||||
);
|
||||
assert!(ui_surface(&json!({"uiSurface":"web"})).is_err());
|
||||
assert_eq!(
|
||||
Value::Object(event.props),
|
||||
json!({"game_id":"100", "game_title":"Portal 2", "duration_s":61.26,
|
||||
"outcome":"clean", "avg_ping_ms":23.4, "recoveries":0})
|
||||
);
|
||||
let started = shell_event(&json!({"event":"session_started", "props":{
|
||||
"resolution":"1920x1080", "codec":"h265", "hdr":true}}))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
Value::Object(started.props),
|
||||
json!({"codec":"h265", "resolution":"1920x1080", "hdr":true})
|
||||
);
|
||||
assert!(
|
||||
shell_event(&json!({"event":"session_started", "props":{"resolution":"1920*1080"}}))
|
||||
.unwrap()
|
||||
.props
|
||||
.is_empty()
|
||||
);
|
||||
for rejected in [
|
||||
json!({"event":"app_opened"}),
|
||||
json!({"event":"consent_changed"}),
|
||||
json!({"event":"purchase"}),
|
||||
] {
|
||||
assert!(shell_event(&rejected).is_err());
|
||||
}
|
||||
assert_eq!(
|
||||
sanitize(
|
||||
"bug_report_sent",
|
||||
&json!({"report_id":"br-7F3A21", "kind":"manual",
|
||||
"code":"manual", "automatic":false, "issue_id":"iss_01J"})
|
||||
),
|
||||
json!({"report_id":"br-7F3A21", "issue_id":"iss_01J", "kind":"manual",
|
||||
"code":"manual", "automatic":false})
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.clone()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gpu_vendors_are_coarse() {
|
||||
assert_eq!(gpu_vendor(Some("NVIDIA GeForce RTX 4070")), "nvidia");
|
||||
assert_eq!(gpu_vendor(Some("AMD Radeon RX 7900")), "amd");
|
||||
assert_eq!(gpu_vendor(Some("Intel(R) Iris(R) Xe Graphics")), "intel");
|
||||
assert_eq!(gpu_vendor(Some("Virtual Display")), "other");
|
||||
assert_eq!(gpu_vendor(None), "unknown");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_queue_is_bounded_and_drops_the_oldest_events() {
|
||||
let (_directory, service) = service("http://127.0.0.1:9");
|
||||
for index in 0..MAX_QUEUED_EVENTS + 3 {
|
||||
let mut props = Map::new();
|
||||
props.insert("code".to_owned(), json!(format!("e{index}")));
|
||||
service.enqueue(INSTALL, Value::Null, "library_load_failed", props);
|
||||
}
|
||||
let state = service.state.lock().unwrap();
|
||||
assert_eq!(state.queue.len(), MAX_QUEUED_EVENTS);
|
||||
assert_eq!(state.dropped, 3);
|
||||
assert_eq!(state.queue[0].event["props"]["code"], "e3");
|
||||
assert_eq!(state.queue[0].event["props"]["ui_surface"], "desktop");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn batches_group_by_account_and_cap_at_one_hundred_events() {
|
||||
let (_directory, service) = service("http://127.0.0.1:9");
|
||||
let account = json!({"userId":"sub", "providerIdpId":"idp"});
|
||||
for _ in 0..150 {
|
||||
service.enqueue(INSTALL, account.clone(), "library_load_failed", Map::new());
|
||||
}
|
||||
service.enqueue(INSTALL, Value::Null, "consent_changed", Map::new());
|
||||
let (batches, _) = drain(&service);
|
||||
assert_eq!(batches.len(), 3);
|
||||
assert_eq!(batches[0]["events"].as_array().unwrap().len(), 100);
|
||||
assert_eq!(batches[1]["events"].as_array().unwrap().len(), 50);
|
||||
assert_eq!(batches[2]["account"], Value::Null);
|
||||
assert_eq!(batches[2]["installId"], INSTALL);
|
||||
assert_eq!(batches[2]["events"][0]["event"], "consent_changed");
|
||||
assert!(batches[2]["events"][0]["ts"].as_u64().unwrap() > 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_batches_spool_and_resend_once_the_service_recovers() {
|
||||
let (base, worker) = server(vec![503, 202, 202]);
|
||||
let (_directory, service) = service(&base);
|
||||
service.enqueue(INSTALL, Value::Null, "library_load_failed", Map::new());
|
||||
let (batches, generation) = drain(&service);
|
||||
service.deliver(batches, generation, None);
|
||||
assert_eq!(read_lines(&service.spool_path).len(), 1);
|
||||
service.enqueue(INSTALL, Value::Null, "app_opened", Map::new());
|
||||
let (batches, generation) = drain(&service);
|
||||
service.deliver(batches, generation, None);
|
||||
assert!(!service.spool_path.exists());
|
||||
let bodies = worker.join().unwrap();
|
||||
assert_eq!(bodies[0]["events"][0]["event"], "library_load_failed");
|
||||
assert_eq!(bodies[1]["events"][0]["event"], "app_opened");
|
||||
assert_eq!(bodies[2], bodies[0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_spool_is_capped_at_one_megabyte_keeping_the_newest_batches() {
|
||||
let (_directory, service) = service("http://127.0.0.1:9");
|
||||
let line = "x".repeat(300 * 1024);
|
||||
service.write_spool((0..5).map(|index| format!("{index}{line}")).collect());
|
||||
let lines = read_lines(&service.spool_path);
|
||||
assert_eq!(lines.len(), 3);
|
||||
assert!(lines[0].starts_with('2'));
|
||||
assert!(fs::metadata(&service.spool_path).unwrap().len() <= MAX_SPOOL_BYTES as u64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn withdrawing_consent_wipes_the_queue_spool_and_in_flight_batches() {
|
||||
let (_directory, service) = service("http://127.0.0.1:9");
|
||||
service.enqueue(INSTALL, Value::Null, "library_load_failed", Map::new());
|
||||
service.write_spool(vec![json!({"events":[]}).to_string()]);
|
||||
let (batches, generation) = drain(&service);
|
||||
service.enqueue(INSTALL, Value::Null, "library_load_failed", Map::new());
|
||||
service.wipe();
|
||||
assert!(service.state.lock().unwrap().queue.is_empty());
|
||||
assert!(!service.spool_path.exists());
|
||||
service.append_spool(&batches, generation);
|
||||
assert!(!service.spool_path.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn runtime_capabilities_open_the_app_once_and_describe_the_device() {
|
||||
let (_directory, service) = service("http://127.0.0.1:9");
|
||||
let capabilities = json!({
|
||||
"graphicsAdapters":[{"name":"NVIDIA GeForce RTX 4070", "active":true}],
|
||||
"videoBackends":[{"backend":"software","available":false},{"backend":"d3d11","available":true}]
|
||||
});
|
||||
let opened = service.open_app(&capabilities).unwrap();
|
||||
assert_eq!(opened["gpu_vendor"], "nvidia");
|
||||
assert!(service.open_app(&capabilities).is_none());
|
||||
assert_eq!(
|
||||
service.device(),
|
||||
json!({"gpu":"NVIDIA GeForce RTX 4070", "decoderBackend":"d3d11"})
|
||||
);
|
||||
assert!(service.observe_sign_in("sub"));
|
||||
assert!(!service.observe_sign_in("sub"));
|
||||
service.observe_sign_out();
|
||||
assert!(service.observe_sign_in("sub"));
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,211 @@
|
||||
use serde_json::Value;
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Command;
|
||||
|
||||
const MAX_LICENSE_BYTES: u64 = 2 * 1024 * 1024;
|
||||
|
||||
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
|
||||
struct Package {
|
||||
name: String,
|
||||
version: String,
|
||||
license: String,
|
||||
source: String,
|
||||
directory: PathBuf,
|
||||
license_file: Option<PathBuf>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
if let Err(error) = run() {
|
||||
eprintln!("opennow-license-report: {error}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fn run() -> Result<(), String> {
|
||||
let mut arguments = env::args_os().skip(1);
|
||||
let base = arguments
|
||||
.next()
|
||||
.map(PathBuf::from)
|
||||
.ok_or_else(|| usage("missing base notice path"))?;
|
||||
let output = arguments
|
||||
.next()
|
||||
.map(PathBuf::from)
|
||||
.ok_or_else(|| usage("missing output path"))?;
|
||||
let manifests = arguments.map(PathBuf::from).collect::<Vec<_>>();
|
||||
if manifests.is_empty() {
|
||||
return Err(usage("at least one Cargo manifest is required"));
|
||||
}
|
||||
|
||||
let mut packages = BTreeSet::new();
|
||||
for manifest in manifests {
|
||||
packages.extend(packages_for_manifest(&manifest)?);
|
||||
}
|
||||
|
||||
let mut texts: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
|
||||
let mut metadata_only = Vec::new();
|
||||
for package in packages {
|
||||
let label = format!(
|
||||
"{} {} — {} — {}",
|
||||
package.name, package.version, package.license, package.source
|
||||
);
|
||||
let license_files = find_license_files(&package)?;
|
||||
if license_files.is_empty() {
|
||||
metadata_only.push(label);
|
||||
continue;
|
||||
}
|
||||
for path in license_files {
|
||||
let contents = fs::read_to_string(&path)
|
||||
.map_err(|error| format!("could not read {}: {error}", path.display()))?;
|
||||
texts.entry(contents).or_default().insert(label.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let mut notice = fs::read_to_string(&base)
|
||||
.map_err(|error| format!("could not read {}: {error}", base.display()))?;
|
||||
notice.push_str("\n\nGenerated Rust dependency notices\n=================================\n\n");
|
||||
notice.push_str(
|
||||
"This section is generated from the exact Cargo dependency graphs used by the OpenNOW application core and native streamer. Packages sharing identical license text are grouped together.\n",
|
||||
);
|
||||
for (index, (license_text, labels)) in texts.into_iter().enumerate() {
|
||||
notice.push_str(&format!(
|
||||
"\nLicense text group {}\n---------------------\n",
|
||||
index + 1
|
||||
));
|
||||
for label in labels {
|
||||
notice.push_str("- ");
|
||||
notice.push_str(&label);
|
||||
notice.push('\n');
|
||||
}
|
||||
notice.push('\n');
|
||||
notice.push_str(license_text.trim());
|
||||
notice.push('\n');
|
||||
}
|
||||
if !metadata_only.is_empty() {
|
||||
notice.push_str("\nSPDX metadata-only packages\n---------------------------\n");
|
||||
notice.push_str("These packages declared an SPDX expression but shipped no standalone license file in the resolved source archive:\n");
|
||||
for label in metadata_only {
|
||||
notice.push_str("- ");
|
||||
notice.push_str(&label);
|
||||
notice.push('\n');
|
||||
}
|
||||
}
|
||||
|
||||
let parent = output
|
||||
.parent()
|
||||
.ok_or_else(|| "output path has no parent directory".to_owned())?;
|
||||
fs::create_dir_all(parent)
|
||||
.map_err(|error| format!("could not create {}: {error}", parent.display()))?;
|
||||
let temporary = output.with_extension("tmp");
|
||||
fs::write(&temporary, notice)
|
||||
.map_err(|error| format!("could not write {}: {error}", temporary.display()))?;
|
||||
if output.exists() {
|
||||
fs::remove_file(&output)
|
||||
.map_err(|error| format!("could not replace {}: {error}", output.display()))?;
|
||||
}
|
||||
fs::rename(&temporary, &output)
|
||||
.map_err(|error| format!("could not publish {}: {error}", output.display()))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn packages_for_manifest(manifest: &Path) -> Result<BTreeSet<Package>, String> {
|
||||
let cargo = env::var_os("CARGO").unwrap_or_else(|| "cargo".into());
|
||||
let output = Command::new(cargo)
|
||||
.args(["metadata", "--format-version", "1", "--manifest-path"])
|
||||
.arg(manifest)
|
||||
.output()
|
||||
.map_err(|error| format!("could not execute cargo metadata: {error}"))?;
|
||||
if !output.status.success() {
|
||||
return Err(format!(
|
||||
"cargo metadata failed for {}: {}",
|
||||
manifest.display(),
|
||||
String::from_utf8_lossy(&output.stderr).trim()
|
||||
));
|
||||
}
|
||||
let metadata: Value = serde_json::from_slice(&output.stdout)
|
||||
.map_err(|error| format!("cargo metadata returned invalid JSON: {error}"))?;
|
||||
let mut packages = BTreeSet::new();
|
||||
for value in metadata["packages"].as_array().into_iter().flatten() {
|
||||
let name = value["name"].as_str().unwrap_or_default();
|
||||
if name == "opennow-core" || name.starts_with("opennow-streamer") {
|
||||
continue;
|
||||
}
|
||||
let manifest_path = value["manifest_path"]
|
||||
.as_str()
|
||||
.map(PathBuf::from)
|
||||
.ok_or_else(|| format!("package {name} has no manifest path"))?;
|
||||
let directory = manifest_path
|
||||
.parent()
|
||||
.ok_or_else(|| format!("package {name} has an invalid manifest path"))?
|
||||
.to_path_buf();
|
||||
let source = value["repository"]
|
||||
.as_str()
|
||||
.or_else(|| value["homepage"].as_str())
|
||||
.or_else(|| value["source"].as_str())
|
||||
.unwrap_or("resolved local source")
|
||||
.to_owned();
|
||||
packages.insert(Package {
|
||||
name: name.to_owned(),
|
||||
version: value["version"].as_str().unwrap_or("unknown").to_owned(),
|
||||
license: value["license"]
|
||||
.as_str()
|
||||
.unwrap_or("NOASSERTION")
|
||||
.to_owned(),
|
||||
source,
|
||||
directory,
|
||||
license_file: value["license_file"].as_str().map(PathBuf::from),
|
||||
});
|
||||
}
|
||||
Ok(packages)
|
||||
}
|
||||
|
||||
fn find_license_files(package: &Package) -> Result<Vec<PathBuf>, String> {
|
||||
if let Some(path) = package.license_file.as_ref() {
|
||||
let path = if path.is_absolute() {
|
||||
path.clone()
|
||||
} else {
|
||||
package.directory.join(path)
|
||||
};
|
||||
validate_license_file(&path)?;
|
||||
return Ok(vec![path]);
|
||||
}
|
||||
|
||||
let mut paths = fs::read_dir(&package.directory)
|
||||
.map_err(|error| format!("could not inspect {}: {error}", package.directory.display()))?
|
||||
.filter_map(Result::ok)
|
||||
.map(|entry| entry.path())
|
||||
.filter(|path| {
|
||||
path.is_file()
|
||||
&& path
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.map(str::to_ascii_lowercase)
|
||||
.is_some_and(|name| {
|
||||
name.starts_with("license")
|
||||
|| name.starts_with("copying")
|
||||
|| name.starts_with("notice")
|
||||
|| name.starts_with("copyright")
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
paths.sort();
|
||||
for path in &paths {
|
||||
validate_license_file(path)?;
|
||||
}
|
||||
Ok(paths)
|
||||
}
|
||||
|
||||
fn validate_license_file(path: &Path) -> Result<(), String> {
|
||||
let metadata = fs::metadata(path)
|
||||
.map_err(|error| format!("could not inspect {}: {error}", path.display()))?;
|
||||
if !metadata.is_file() || metadata.len() > MAX_LICENSE_BYTES {
|
||||
return Err(format!("unusable license file: {}", path.display()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn usage(message: &str) -> String {
|
||||
format!("{message}; usage: opennow-license-report BASE_NOTICE OUTPUT MANIFEST [MANIFEST ...]")
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
use opennow_core::update_apply;
|
||||
|
||||
fn main() {
|
||||
let mut arguments = std::env::args_os().skip(1);
|
||||
let result = match (arguments.next(), arguments.next(), arguments.next()) {
|
||||
(Some(flag), Some(path), None) if flag == "--apply" => {
|
||||
update_apply::run_helper(std::path::Path::new(&path))
|
||||
}
|
||||
_ => Err("Usage: opennow-update-helper --apply <prepared-plan.json>".to_owned()),
|
||||
};
|
||||
if let Err(error) = result {
|
||||
eprintln!("opennow-update-helper: {error}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
use base64::Engine as _;
|
||||
use base64::engine::general_purpose::STANDARD as BASE64;
|
||||
use ed25519_dalek::{Signer as _, SigningKey, VerifyingKey};
|
||||
use serde::Serialize;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::io::{Read, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct Manifest {
|
||||
schema_version: u32,
|
||||
version: String,
|
||||
asset: String,
|
||||
size: u64,
|
||||
sha256: String,
|
||||
signature: String,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
if let Err(error) = run() {
|
||||
eprintln!("opennow-update-manifest: {error}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fn run() -> Result<(), String> {
|
||||
let mut arguments = env::args_os().skip(1);
|
||||
let asset = arguments
|
||||
.next()
|
||||
.map(PathBuf::from)
|
||||
.ok_or_else(|| "usage: opennow-update-manifest <asset> <version> [output]".to_owned())?;
|
||||
let version = arguments
|
||||
.next()
|
||||
.and_then(|value| value.into_string().ok())
|
||||
.ok_or_else(|| "update version is required".to_owned())?;
|
||||
let version = semver::Version::parse(version.strip_prefix('v').unwrap_or(&version))
|
||||
.map_err(|_| "update version must be valid semver".to_owned())?
|
||||
.to_string();
|
||||
let output = arguments.next().map(PathBuf::from).unwrap_or_else(|| {
|
||||
let name = asset.file_name().unwrap_or_default().to_string_lossy();
|
||||
asset.with_file_name(format!("{name}.manifest.json"))
|
||||
});
|
||||
if arguments.next().is_some() {
|
||||
return Err("update manifest arguments are invalid".to_owned());
|
||||
}
|
||||
let asset_name = asset
|
||||
.file_name()
|
||||
.and_then(|value| value.to_str())
|
||||
.filter(|value| safe_asset_name(value))
|
||||
.ok_or_else(|| "update asset name is unsafe".to_owned())?
|
||||
.to_owned();
|
||||
let key = signing_key()?;
|
||||
let verifying_key = configured_verifying_key()?;
|
||||
if key.verifying_key() != verifying_key {
|
||||
return Err(
|
||||
"OPENNOW_UPDATE_ED25519_PUBLIC_KEY does not match the protected signing key".to_owned(),
|
||||
);
|
||||
}
|
||||
let (size, sha256) = hash_file(&asset)?;
|
||||
let mut manifest = Manifest {
|
||||
schema_version: 1,
|
||||
version,
|
||||
asset: asset_name,
|
||||
size,
|
||||
sha256,
|
||||
signature: String::new(),
|
||||
};
|
||||
let signature = key.sign(signature_payload(&manifest).as_bytes());
|
||||
verifying_key
|
||||
.verify_strict(signature_payload(&manifest).as_bytes(), &signature)
|
||||
.map_err(|_| "generated update signature did not verify".to_owned())?;
|
||||
manifest.signature = BASE64.encode(signature.to_bytes());
|
||||
let bytes = serde_json::to_vec_pretty(&manifest).map_err(|error| error.to_string())?;
|
||||
let temporary = output.with_extension("json.tmp");
|
||||
let mut file = fs::File::create(&temporary)
|
||||
.map_err(|error| format!("could not create manifest: {error}"))?;
|
||||
file.write_all(&bytes)
|
||||
.and_then(|()| file.write_all(b"\n"))
|
||||
.and_then(|()| file.sync_all())
|
||||
.map_err(|error| format!("could not write manifest: {error}"))?;
|
||||
fs::rename(&temporary, &output)
|
||||
.map_err(|error| format!("could not finalize manifest: {error}"))?;
|
||||
println!("{}", output.display());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn signing_key() -> Result<SigningKey, String> {
|
||||
let encoded = env::var("OPENNOW_UPDATE_ED25519_PRIVATE_KEY")
|
||||
.map_err(|_| "OPENNOW_UPDATE_ED25519_PRIVATE_KEY is required".to_owned())?;
|
||||
let bytes = BASE64
|
||||
.decode(encoded.trim())
|
||||
.map_err(|_| "update signing key is not valid base64".to_owned())?;
|
||||
let bytes: [u8; 32] = bytes
|
||||
.try_into()
|
||||
.map_err(|_| "update signing key must be a 32-byte Ed25519 seed".to_owned())?;
|
||||
Ok(SigningKey::from_bytes(&bytes))
|
||||
}
|
||||
|
||||
fn configured_verifying_key() -> Result<VerifyingKey, String> {
|
||||
let encoded = env::var("OPENNOW_UPDATE_ED25519_PUBLIC_KEY")
|
||||
.map_err(|_| "OPENNOW_UPDATE_ED25519_PUBLIC_KEY is required".to_owned())?;
|
||||
decode_verifying_key(&encoded)
|
||||
}
|
||||
|
||||
fn decode_verifying_key(encoded: &str) -> Result<VerifyingKey, String> {
|
||||
let bytes = BASE64
|
||||
.decode(encoded.trim())
|
||||
.map_err(|_| "update verifying key is not valid base64".to_owned())?;
|
||||
let bytes: [u8; 32] = bytes
|
||||
.try_into()
|
||||
.map_err(|_| "update verifying key must be 32 bytes".to_owned())?;
|
||||
VerifyingKey::from_bytes(&bytes).map_err(|_| "update verifying key is invalid".to_owned())
|
||||
}
|
||||
|
||||
fn hash_file(path: &Path) -> Result<(u64, String), String> {
|
||||
let mut file =
|
||||
fs::File::open(path).map_err(|error| format!("could not read update asset: {error}"))?;
|
||||
let mut hasher = Sha256::new();
|
||||
let mut size = 0_u64;
|
||||
let mut buffer = [0_u8; 128 * 1024];
|
||||
loop {
|
||||
let count = file
|
||||
.read(&mut buffer)
|
||||
.map_err(|error| format!("could not hash update asset: {error}"))?;
|
||||
if count == 0 {
|
||||
break;
|
||||
}
|
||||
size = size
|
||||
.checked_add(count as u64)
|
||||
.ok_or_else(|| "update asset size overflowed".to_owned())?;
|
||||
hasher.update(&buffer[..count]);
|
||||
}
|
||||
if size == 0 {
|
||||
return Err("update asset is empty".to_owned());
|
||||
}
|
||||
Ok((size, format!("{:x}", hasher.finalize())))
|
||||
}
|
||||
|
||||
fn signature_payload(manifest: &Manifest) -> String {
|
||||
format!(
|
||||
"OpenNOW update manifest v1\nversion={}\nasset={}\nsize={}\nsha256={}\n",
|
||||
manifest.version, manifest.asset, manifest.size, manifest.sha256
|
||||
)
|
||||
}
|
||||
|
||||
fn safe_asset_name(value: &str) -> bool {
|
||||
!value.is_empty()
|
||||
&& value.len() <= 240
|
||||
&& !value.contains(['/', '\\'])
|
||||
&& !value.contains("..")
|
||||
&& value
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'-' | b'_'))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn public_key_decoder_rejects_malformed_and_accepts_the_signing_pair() {
|
||||
assert!(decode_verifying_key("not-base64").is_err());
|
||||
assert!(decode_verifying_key(&BASE64.encode([0_u8; 31])).is_err());
|
||||
let signing = SigningKey::from_bytes(&[7_u8; 32]);
|
||||
let encoded = BASE64.encode(signing.verifying_key().as_bytes());
|
||||
assert_eq!(
|
||||
decode_verifying_key(&encoded).unwrap(),
|
||||
signing.verifying_key()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn canonical_manifest_signature_verifies_and_detects_tampering() {
|
||||
let signing = SigningKey::from_bytes(&[9_u8; 32]);
|
||||
let manifest = Manifest {
|
||||
schema_version: 1,
|
||||
version: "1.2.3".to_owned(),
|
||||
asset: "OpenNOW-Qt-1.2.3.deb".to_owned(),
|
||||
size: 42,
|
||||
sha256: "0".repeat(64),
|
||||
signature: String::new(),
|
||||
};
|
||||
let signature = signing.sign(signature_payload(&manifest).as_bytes());
|
||||
assert!(
|
||||
signing
|
||||
.verifying_key()
|
||||
.verify_strict(signature_payload(&manifest).as_bytes(), &signature)
|
||||
.is_ok()
|
||||
);
|
||||
let mut tampered = manifest;
|
||||
tampered.size += 1;
|
||||
assert!(
|
||||
signing
|
||||
.verifying_key()
|
||||
.verify_strict(signature_payload(&tampered).as_bytes(), &signature)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nightly_signature_binds_the_full_version() {
|
||||
let signing = SigningKey::from_bytes(&[9_u8; 32]);
|
||||
let mut manifest = Manifest {
|
||||
schema_version: 1,
|
||||
version: semver::Version::parse("1.0.0-nightly.123456.2")
|
||||
.unwrap()
|
||||
.to_string(),
|
||||
asset: "OpenNOW-Qt-1.0.0-nightly.123456.2-Linux-x64.deb".to_owned(),
|
||||
size: 42,
|
||||
sha256: "0".repeat(64),
|
||||
signature: String::new(),
|
||||
};
|
||||
let signature = signing.sign(signature_payload(&manifest).as_bytes());
|
||||
signing
|
||||
.verifying_key()
|
||||
.verify_strict(signature_payload(&manifest).as_bytes(), &signature)
|
||||
.unwrap();
|
||||
for version in ["1.0.0-nightly.123456.3", "1.0.0"] {
|
||||
manifest.version = version.to_owned();
|
||||
assert!(
|
||||
signing
|
||||
.verifying_key()
|
||||
.verify_strict(signature_payload(&manifest).as_bytes(), &signature)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,340 @@
|
||||
use serde_json::{Map, Value, json};
|
||||
use std::collections::{HashSet, VecDeque};
|
||||
use std::sync::Mutex;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
const ENABLED_WHEN_UNSET: bool = true;
|
||||
const MAXIMUM_REPORTS_PER_RUN: usize = 5;
|
||||
const MINIMUM_REPORT_INTERVAL: Duration = Duration::from_secs(30);
|
||||
const RECENT_GAME_LIMIT: usize = 5;
|
||||
|
||||
const REPORTED_METHODS: &[(&str, Kind)] = &[
|
||||
("session.create", Kind::SessionError),
|
||||
("session.poll", Kind::SessionError),
|
||||
("session.claim", Kind::SessionError),
|
||||
("streamer.prepare", Kind::StreamError),
|
||||
("streamer.start", Kind::StreamError),
|
||||
("catalog.library.list", Kind::LibraryError),
|
||||
];
|
||||
|
||||
const EXPECTED_CODES: &[&str] = &[
|
||||
"authentication_required",
|
||||
"busy",
|
||||
"cancelled",
|
||||
"catalog_changed",
|
||||
"catalog_mutation_busy",
|
||||
"confirmation_required",
|
||||
"launch_not_ready",
|
||||
"rate_limited",
|
||||
"routing_busy",
|
||||
"session_cleanup_pending",
|
||||
"session_conflict",
|
||||
"session_update_busy",
|
||||
"stale_account",
|
||||
"update_pending",
|
||||
];
|
||||
|
||||
const SHELL_METRICS: &[&str] = &[
|
||||
"droppedFrames",
|
||||
"windowSeconds",
|
||||
"framesPerSecond",
|
||||
"packetLossPercent",
|
||||
"pingMs",
|
||||
"decodeTimeMs",
|
||||
];
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Kind {
|
||||
SessionError,
|
||||
StreamError,
|
||||
FrameDrops,
|
||||
LibraryError,
|
||||
}
|
||||
|
||||
impl Kind {
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::SessionError => "session_error",
|
||||
Self::StreamError => "stream_error",
|
||||
Self::FrameDrops => "frame_drops",
|
||||
Self::LibraryError => "library_error",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Incident {
|
||||
pub kind: Kind,
|
||||
pub code: String,
|
||||
pub message: String,
|
||||
pub details: Value,
|
||||
}
|
||||
|
||||
impl Incident {
|
||||
pub fn trigger(&self) -> Value {
|
||||
json!({
|
||||
"kind": self.kind.as_str(),
|
||||
"code": self.code,
|
||||
"message": self.message,
|
||||
"metrics": self.details.get("metrics").cloned().unwrap_or_else(|| json!({}))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct Game {
|
||||
title: String,
|
||||
app_id: String,
|
||||
}
|
||||
|
||||
impl Game {
|
||||
fn value(&self) -> Value {
|
||||
json!({"title":self.title, "appId":self.app_id})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct State {
|
||||
current: Option<Game>,
|
||||
recent: VecDeque<Game>,
|
||||
reported: HashSet<String>,
|
||||
admitted: usize,
|
||||
last_admitted: Option<Instant>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct BugReporter {
|
||||
state: Mutex<State>,
|
||||
}
|
||||
|
||||
pub fn enabled(settings: &Value) -> bool {
|
||||
match settings["automaticBugReports"].as_str() {
|
||||
Some("enabled") => true,
|
||||
Some("disabled") => false,
|
||||
_ => ENABLED_WHEN_UNSET,
|
||||
}
|
||||
}
|
||||
|
||||
impl BugReporter {
|
||||
pub fn observe_launch(&self, params: &Value) -> String {
|
||||
let game = Game {
|
||||
title: bounded_text(params["title"].as_str().unwrap_or("Unknown game"), 120),
|
||||
app_id: bounded_text(params["appId"].as_str().unwrap_or_default(), 32),
|
||||
};
|
||||
let detail = format!("title={} appId={}", game.title, game.app_id);
|
||||
let mut state = self.state.lock().expect("bug reporter poisoned");
|
||||
state.recent.retain(|recent| recent.app_id != game.app_id);
|
||||
state.recent.push_front(game.clone());
|
||||
state.recent.truncate(RECENT_GAME_LIMIT);
|
||||
state.current = Some(game);
|
||||
detail
|
||||
}
|
||||
|
||||
pub fn observe_stop(&self) {
|
||||
self.state.lock().expect("bug reporter poisoned").current = None;
|
||||
}
|
||||
|
||||
pub fn activity(&self) -> Value {
|
||||
let state = self.state.lock().expect("bug reporter poisoned");
|
||||
json!({
|
||||
"currentGame": state.current.as_ref().map(Game::value),
|
||||
"recentGames": state.recent.iter().map(Game::value).collect::<Vec<_>>()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn rpc_incident(method: &str, code: &str, message: &str) -> Option<Incident> {
|
||||
let kind = REPORTED_METHODS
|
||||
.iter()
|
||||
.find(|(reported, _)| *reported == method)
|
||||
.map(|(_, kind)| *kind)?;
|
||||
if EXPECTED_CODES.contains(&code) {
|
||||
return None;
|
||||
}
|
||||
Some(Incident {
|
||||
kind,
|
||||
code: bounded_code(code),
|
||||
message: crate::diagnostics::runtime_failure_reason(message),
|
||||
details: json!({"method":method}),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn shell_incident(params: &Value) -> Result<Incident, String> {
|
||||
let kind = match params["kind"].as_str() {
|
||||
Some("stream_error") => Kind::StreamError,
|
||||
Some("frame_drops") => Kind::FrameDrops,
|
||||
_ => return Err("Unsupported automatic report kind".to_owned()),
|
||||
};
|
||||
let code = params["code"]
|
||||
.as_str()
|
||||
.filter(|code| {
|
||||
(1..=64).contains(&code.len())
|
||||
&& code.bytes().all(|byte| {
|
||||
byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'_'
|
||||
})
|
||||
})
|
||||
.ok_or_else(|| "Automatic report code is invalid".to_owned())?;
|
||||
let mut metrics = Map::new();
|
||||
for key in SHELL_METRICS {
|
||||
if let Some(value) = params["metrics"][*key]
|
||||
.as_f64()
|
||||
.filter(|value| value.is_finite() && (0.0..=1_000_000.0).contains(value))
|
||||
{
|
||||
metrics.insert((*key).to_owned(), json!(value));
|
||||
}
|
||||
}
|
||||
Ok(Incident {
|
||||
kind,
|
||||
code: code.to_owned(),
|
||||
message: crate::diagnostics::runtime_failure_reason(
|
||||
params["message"].as_str().unwrap_or_default(),
|
||||
),
|
||||
details: json!({"source":"shell", "metrics":metrics}),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn admit(&self, incident: &Incident, now: Instant) -> Option<Value> {
|
||||
let mut state = self.state.lock().expect("bug reporter poisoned");
|
||||
let signature = format!("{}:{}", incident.kind.as_str(), incident.code);
|
||||
if state.reported.contains(&signature)
|
||||
|| state.admitted >= MAXIMUM_REPORTS_PER_RUN
|
||||
|| state
|
||||
.last_admitted
|
||||
.is_some_and(|last| now.duration_since(last) < MINIMUM_REPORT_INTERVAL)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
state.reported.insert(signature);
|
||||
state.admitted += 1;
|
||||
state.last_admitted = Some(now);
|
||||
drop(state);
|
||||
Some(self.activity())
|
||||
}
|
||||
}
|
||||
|
||||
fn bounded_text(value: &str, limit: usize) -> String {
|
||||
value
|
||||
.chars()
|
||||
.map(|character| {
|
||||
if character.is_control() && character != '\n' {
|
||||
' '
|
||||
} else {
|
||||
character
|
||||
}
|
||||
})
|
||||
.take(limit)
|
||||
.collect::<String>()
|
||||
.trim()
|
||||
.to_owned()
|
||||
}
|
||||
|
||||
fn bounded_code(value: &str) -> String {
|
||||
let code = value
|
||||
.chars()
|
||||
.filter(|character| character.is_ascii_alphanumeric() || *character == '_')
|
||||
.map(|character| character.to_ascii_lowercase())
|
||||
.take(64)
|
||||
.collect::<String>();
|
||||
if code.is_empty() {
|
||||
"unknown".to_owned()
|
||||
} else {
|
||||
code
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn unset_consent_is_opt_out_and_explicit_choices_win() {
|
||||
assert!(enabled(&json!({"automaticBugReports":"unset"})));
|
||||
assert!(enabled(&json!({})));
|
||||
assert!(enabled(&json!({"automaticBugReports":"enabled"})));
|
||||
assert!(!enabled(&json!({"automaticBugReports":"disabled"})));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_unexpected_failures_of_reported_methods_become_incidents() {
|
||||
let incident = BugReporter::rpc_incident("catalog.library.list", "network_error", "boom")
|
||||
.expect("library failures are reported");
|
||||
assert_eq!(incident.kind, Kind::LibraryError);
|
||||
assert!(BugReporter::rpc_incident("session.create", "cancelled", "user").is_none());
|
||||
assert!(BugReporter::rpc_incident("settings.set", "invalid_setting", "x").is_none());
|
||||
let incident =
|
||||
BugReporter::rpc_incident("session.poll", "session_error", "Bearer abc.def failed")
|
||||
.unwrap();
|
||||
assert!(!incident.message.contains("abc.def"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shell_incidents_accept_only_known_kinds_codes_and_metrics() {
|
||||
let incident = BugReporter::shell_incident(&json!({
|
||||
"kind":"frame_drops", "code":"sustained_frame_drops",
|
||||
"metrics":{"droppedFrames":412, "windowSeconds":60, "secret":"x", "pingMs":-1}
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(incident.kind, Kind::FrameDrops);
|
||||
assert_eq!(
|
||||
incident.details["metrics"],
|
||||
json!({"droppedFrames":412.0, "windowSeconds":60.0})
|
||||
);
|
||||
assert!(BugReporter::shell_incident(&json!({"kind":"library_error","code":"x"})).is_err());
|
||||
assert!(
|
||||
BugReporter::shell_incident(&json!({"kind":"stream_error","code":"Bad Code"})).is_err()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reports_are_deduplicated_spaced_and_capped_per_run() {
|
||||
let reporter = BugReporter::default();
|
||||
let start = Instant::now();
|
||||
let incident = |code: &str| Incident {
|
||||
kind: Kind::StreamError,
|
||||
code: code.to_owned(),
|
||||
message: String::new(),
|
||||
details: Value::Null,
|
||||
};
|
||||
assert!(reporter.admit(&incident("a"), start).is_some());
|
||||
assert!(
|
||||
reporter
|
||||
.admit(&incident("a"), start + Duration::from_secs(60))
|
||||
.is_none()
|
||||
);
|
||||
assert!(
|
||||
reporter
|
||||
.admit(&incident("b"), start + Duration::from_secs(5))
|
||||
.is_none()
|
||||
);
|
||||
for (index, code) in ["b", "c", "d", "e"].iter().enumerate() {
|
||||
let at = start + MINIMUM_REPORT_INTERVAL * (index as u32 + 1);
|
||||
assert!(reporter.admit(&incident(code), at).is_some());
|
||||
}
|
||||
assert!(
|
||||
reporter
|
||||
.admit(&incident("f"), start + Duration::from_secs(3600))
|
||||
.is_none()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reports_carry_the_current_game_recent_games_and_the_trigger() {
|
||||
let reporter = BugReporter::default();
|
||||
reporter.observe_launch(&json!({"title":"Portal 2", "appId":"100"}));
|
||||
reporter.observe_launch(&json!({"title":"Cyberpunk 2077", "appId":"200"}));
|
||||
reporter.observe_launch(&json!({"title":"Portal 2", "appId":"100"}));
|
||||
let incident =
|
||||
BugReporter::rpc_incident("session.create", "session_error", "seat lost").unwrap();
|
||||
let activity = reporter.admit(&incident, Instant::now()).unwrap();
|
||||
assert_eq!(
|
||||
activity,
|
||||
json!({"currentGame":{"title":"Portal 2","appId":"100"},
|
||||
"recentGames":[{"title":"Portal 2","appId":"100"},{"title":"Cyberpunk 2077","appId":"200"}]})
|
||||
);
|
||||
assert_eq!(
|
||||
incident.trigger(),
|
||||
json!({"kind":"session_error", "code":"session_error", "message":"seat lost", "metrics":{}})
|
||||
);
|
||||
reporter.observe_stop();
|
||||
assert_eq!(reporter.activity()["currentGame"], Value::Null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,367 @@
|
||||
use crate::gfn::ServiceError;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[serde(tag = "__typename")]
|
||||
pub enum StoreFeature {
|
||||
AccountLinkingSso {
|
||||
supported: Option<bool>,
|
||||
#[serde(rename = "displayProposition")]
|
||||
display_proposition: Option<String>,
|
||||
},
|
||||
AccountGamesSyncing {
|
||||
supported: Option<bool>,
|
||||
#[serde(rename = "displayProposition")]
|
||||
display_proposition: Option<String>,
|
||||
},
|
||||
AccountSubscriptions {
|
||||
#[serde(rename = "displayProposition")]
|
||||
display_proposition: Option<String>,
|
||||
},
|
||||
#[serde(other)]
|
||||
Unknown,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum GameFeature {
|
||||
Rtx,
|
||||
Hdr,
|
||||
Reflex,
|
||||
}
|
||||
|
||||
impl GameFeature {
|
||||
pub fn from_variant_features(features: &Value) -> Vec<GameFeature> {
|
||||
let entries: Vec<&Value> = match features {
|
||||
Value::Array(items) => items.iter().collect(),
|
||||
Value::Object(_) => vec![features],
|
||||
_ => Vec::new(),
|
||||
};
|
||||
let enabled = |key: &str| {
|
||||
entries.iter().any(|feature| {
|
||||
feature["key"].as_str() == Some(key)
|
||||
&& (feature["value"].as_bool() == Some(true)
|
||||
|| feature["value"]
|
||||
.as_str()
|
||||
.is_some_and(|value| value.eq_ignore_ascii_case("true")))
|
||||
})
|
||||
};
|
||||
let hdr_version = entries.iter().any(|feature| {
|
||||
feature["key"].as_str() == Some("SUPPORTED_HDR_VERSION")
|
||||
&& [&feature["value"], &feature["values"]]
|
||||
.into_iter()
|
||||
.any(|value| match value {
|
||||
Value::String(text) => !text.is_empty(),
|
||||
Value::Array(items) => items
|
||||
.iter()
|
||||
.any(|item| item.as_str().is_some_and(|text| !text.is_empty())),
|
||||
_ => false,
|
||||
})
|
||||
});
|
||||
let mut result = Vec::new();
|
||||
if enabled("RTX_ENABLED") {
|
||||
result.push(GameFeature::Rtx);
|
||||
}
|
||||
if enabled("HDR_ENABLED") || hdr_version {
|
||||
result.push(GameFeature::Hdr);
|
||||
}
|
||||
if enabled("REFLEX_ENABLED") {
|
||||
result.push(GameFeature::Reflex);
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct LinkingMetadata {
|
||||
pub supported_variant_ids: Option<Vec<String>>,
|
||||
pub is_supported: Option<bool>,
|
||||
pub is_required: Option<bool>,
|
||||
pub label: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct StoreDefinition {
|
||||
pub store: String,
|
||||
pub label: String,
|
||||
pub sort_order: Option<i64>,
|
||||
pub small_image_url: Option<String>,
|
||||
#[serde(default)]
|
||||
pub features: Vec<StoreFeature>,
|
||||
pub account_linking_metadata: Option<LinkingMetadata>,
|
||||
}
|
||||
|
||||
impl StoreDefinition {
|
||||
pub fn connection_definition(&self) -> Value {
|
||||
let provider = normalize_store(&self.store);
|
||||
let known = matches!(
|
||||
provider.as_str(),
|
||||
"UPLAY" | "BATTLENET" | "EPIC" | "GAIJIN" | "GOG" | "STEAM" | "XBOX"
|
||||
);
|
||||
json!({
|
||||
"provider":provider,"store":self.store,"label":self.label,"sortOrder":self.sort_order,
|
||||
"supportsLinking":known && self.features.iter().any(|feature| matches!(feature, StoreFeature::AccountLinkingSso { supported:Some(true), .. })),
|
||||
"supportsSync":known && self.features.iter().any(|feature| matches!(feature, StoreFeature::AccountGamesSyncing { supported:Some(true), .. })),
|
||||
"isRequired":self.account_linking_metadata.as_ref().and_then(|metadata| metadata.is_required),
|
||||
"accountLinkingMetadata":self.account_linking_metadata,"features":self.features,
|
||||
"capabilitySource":"server"
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct GenreDefinition {
|
||||
pub genre: String,
|
||||
pub label: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SubscriptionDefinition {
|
||||
pub subscription: String,
|
||||
pub label: String,
|
||||
#[serde(rename = "logoURL")]
|
||||
pub logo_url: Option<String>,
|
||||
#[serde(rename = "buySubscriptionURL")]
|
||||
pub buy_subscription_url: Option<String>,
|
||||
pub primary_store: Option<String>,
|
||||
pub additional_stores: Option<Vec<SubscriptionStore>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SubscriptionStore {
|
||||
pub store: String,
|
||||
pub third_party_linking_redirect_keyword: Option<String>,
|
||||
#[serde(rename = "thirdPartyLinkingShortURL")]
|
||||
pub third_party_linking_short_url: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[serde(tag = "__typename")]
|
||||
pub enum PatchDetails {
|
||||
VariantGfnAutoPatchingMetadata {
|
||||
#[serde(rename = "subType")]
|
||||
sub_type: Option<String>,
|
||||
#[serde(rename = "startTime")]
|
||||
start_time: Option<String>,
|
||||
#[serde(rename = "endTime")]
|
||||
end_time: Option<String>,
|
||||
#[serde(rename = "historicalEtaMins")]
|
||||
historical_eta_mins: Option<f64>,
|
||||
#[serde(rename = "etaPredictionType")]
|
||||
eta_prediction_type: Option<String>,
|
||||
},
|
||||
VariantGfnManualPatchingMetadata {
|
||||
#[serde(rename = "subType")]
|
||||
sub_type: Option<String>,
|
||||
#[serde(rename = "startTime")]
|
||||
start_time: Option<String>,
|
||||
#[serde(rename = "endTime")]
|
||||
end_time: Option<String>,
|
||||
},
|
||||
VariantGfnMaintenanceMetadata {
|
||||
#[serde(rename = "subType")]
|
||||
sub_type: Option<String>,
|
||||
},
|
||||
#[serde(other)]
|
||||
Unknown,
|
||||
}
|
||||
|
||||
pub fn normalize_store(value: &str) -> String {
|
||||
match value
|
||||
.trim()
|
||||
.to_ascii_uppercase()
|
||||
.replace([' ', '-'], "_")
|
||||
.as_str()
|
||||
{
|
||||
"UBISOFT" | "UBISOFT_CONNECT" => "UPLAY".into(),
|
||||
"BATTLE_NET" | "BLIZZARD" => "BATTLENET".into(),
|
||||
"EPIC_GAMES" | "EPIC_GAMES_STORE" => "EPIC".into(),
|
||||
value => value.into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_list<T: serde::de::DeserializeOwned + Serialize>(
|
||||
value: &Value,
|
||||
) -> Result<Value, ServiceError> {
|
||||
let values = value
|
||||
.as_array()
|
||||
.filter(|items| items.len() <= 512)
|
||||
.ok_or_else(invalid_metadata)?;
|
||||
if serde_json::to_vec(value).map_or(true, |bytes| bytes.len() > 256 * 1024) {
|
||||
return Err(invalid_metadata());
|
||||
}
|
||||
let parsed: Vec<T> = values
|
||||
.iter()
|
||||
.map(|item| serde_json::from_value(item.clone()))
|
||||
.collect::<Result<_, _>>()
|
||||
.map_err(|_| invalid_metadata())?;
|
||||
serde_json::to_value(parsed).map_err(|_| invalid_metadata())
|
||||
}
|
||||
|
||||
pub fn invalid_metadata() -> ServiceError {
|
||||
ServiceError {
|
||||
code: "invalid_upstream_response",
|
||||
message: "Catalog metadata is missing, invalid, or exceeds its size limit".into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn filter_expression(value: &Value) -> Option<Value> {
|
||||
fn merge(target: &mut Value, source: Value, depth: usize) -> Option<()> {
|
||||
if depth > 8 {
|
||||
return None;
|
||||
}
|
||||
match (target, source) {
|
||||
(Value::Object(target), Value::Object(source)) => {
|
||||
if source.len() > 64 {
|
||||
return None;
|
||||
}
|
||||
for (key, value) in source {
|
||||
if let Some(target) = target.get_mut(&key) {
|
||||
merge(target, value, depth + 1)?;
|
||||
} else {
|
||||
let mut field = if value.is_object() {
|
||||
json!({})
|
||||
} else if value.is_array() {
|
||||
json!([])
|
||||
} else {
|
||||
Value::Null
|
||||
};
|
||||
merge(&mut field, value, depth + 1)?;
|
||||
target.insert(key, field);
|
||||
}
|
||||
}
|
||||
}
|
||||
(Value::Array(target), Value::Array(source)) => {
|
||||
if target.len() + source.len() > 128 {
|
||||
return None;
|
||||
}
|
||||
target.extend(source);
|
||||
}
|
||||
(target, source) => *target = source,
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
let inputs = value.as_array().filter(|items| items.len() <= 32)?;
|
||||
let mut output = json!({});
|
||||
for input in inputs {
|
||||
let text = input.as_str().filter(|text| text.len() <= 8192)?;
|
||||
let parsed: Value = serde_json::from_str(text).ok()?;
|
||||
if !parsed.is_object() {
|
||||
return None;
|
||||
}
|
||||
merge(&mut output, parsed, 0)?;
|
||||
}
|
||||
Some(output)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn game_features_follow_catalog_keys_and_ignore_unbacked_ones() {
|
||||
let features = json!([
|
||||
{"__typename":"GfnSubscriptionFeatureValue","key":"RTX_ENABLED","value":"true"},
|
||||
{"__typename":"GfnSubscriptionFeatureValue","key":"SUPPORTED_HDR_VERSION","value":null},
|
||||
{"__typename":"GfnSubscriptionFeatureValueList","key":"SUPPORTED_HDR_VERSION","values":["HDR"]},
|
||||
{"__typename":"GfnSubscriptionFeatureValue","key":"REFLEX_ENABLED","value":"true"},
|
||||
{"__typename":"GfnSubscriptionFeatureValue","key":"DLSS_ENABLED","value":"true"},
|
||||
{"__typename":"GfnSubscriptionFeatureValue","key":"IN_GAME_SETTINGS_PERSISTENCE_ENABLED","value":"true"}
|
||||
]);
|
||||
assert_eq!(
|
||||
GameFeature::from_variant_features(&features),
|
||||
vec![GameFeature::Rtx, GameFeature::Hdr, GameFeature::Reflex]
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::to_value(GameFeature::from_variant_features(&features)).unwrap(),
|
||||
json!(["rtx", "hdr", "reflex"])
|
||||
);
|
||||
let disabled = json!([
|
||||
{"key":"RTX_ENABLED","value":"false"},
|
||||
{"key":"SUPPORTED_HDR_VERSION","value":null},
|
||||
{"key":"HDR_ENABLED","value":null}
|
||||
]);
|
||||
assert!(GameFeature::from_variant_features(&disabled).is_empty());
|
||||
assert_eq!(
|
||||
GameFeature::from_variant_features(&json!({"key":"HDR_ENABLED","value":true})),
|
||||
vec![GameFeature::Hdr]
|
||||
);
|
||||
assert!(GameFeature::from_variant_features(&Value::Null).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gog_connection_capabilities_follow_server_features() {
|
||||
for supported in [false, true] {
|
||||
let store: StoreDefinition = serde_json::from_value(json!({
|
||||
"store":"GOG","label":"GOG","features":[
|
||||
{"__typename":"AccountLinkingSso","supported":supported},
|
||||
{"__typename":"AccountGamesSyncing","supported":supported}
|
||||
]
|
||||
}))
|
||||
.unwrap();
|
||||
let connection = store.connection_definition();
|
||||
assert_eq!(connection["provider"], "GOG");
|
||||
assert_eq!(connection["supportsLinking"], supported);
|
||||
assert_eq!(connection["supportsSync"], supported);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_definitions_preserve_nullable_variant_scope_and_link_requirements() {
|
||||
for variants in [Value::Null, json!([]), json!(["123"])] {
|
||||
let input = json!([{"store":"XBOX","label":"Xbox","features":[],
|
||||
"accountLinkingMetadata":{"supportedVariantIds":variants,"isRequired":true}}]);
|
||||
let parsed = parse_list::<StoreDefinition>(&input).unwrap();
|
||||
let store: StoreDefinition = serde_json::from_value(parsed[0].clone()).unwrap();
|
||||
let connection = store.connection_definition();
|
||||
assert_eq!(
|
||||
connection["accountLinkingMetadata"]["supportedVariantIds"],
|
||||
variants
|
||||
);
|
||||
assert_eq!(connection["isRequired"], true);
|
||||
}
|
||||
for invalid in [json!("123"), json!([123]), json!([null])] {
|
||||
assert!(
|
||||
parse_list::<StoreDefinition>(&json!([{"store":"XBOX","label":"Xbox",
|
||||
"accountLinkingMetadata":{"supportedVariantIds":invalid}}]))
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subscription_definitions_accept_null_but_reject_invalid_additional_stores() {
|
||||
for stores in [Value::Null, json!([]), json!([{"store":"UPLAY"}])] {
|
||||
let parsed = parse_list::<SubscriptionDefinition>(&json!([{
|
||||
"subscription":"STORE_PASS","label":"Store Pass","additionalStores":stores
|
||||
}]))
|
||||
.unwrap();
|
||||
assert_eq!(parsed[0]["additionalStores"].is_null(), stores.is_null());
|
||||
if let Some(stores) = stores.as_array() {
|
||||
assert_eq!(
|
||||
parsed[0]["additionalStores"].as_array().unwrap().len(),
|
||||
stores.len()
|
||||
);
|
||||
for (index, store) in stores.iter().enumerate() {
|
||||
assert_eq!(
|
||||
parsed[0]["additionalStores"][index]["store"],
|
||||
store["store"]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
for invalid in [json!("UPLAY"), json!(["UPLAY"]), json!([null])] {
|
||||
assert!(
|
||||
parse_list::<SubscriptionDefinition>(&json!([{
|
||||
"subscription":"STORE_PASS","label":"Store Pass","additionalStores":invalid
|
||||
}]))
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
+7398
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,268 @@
|
||||
use reqwest::blocking::Client;
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Map, Value, json};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use url::Url;
|
||||
|
||||
const QUEUE_URL: &str = "https://api.printedwaste.com/gfn/queue/";
|
||||
const SERVER_MAPPING_URL: &str =
|
||||
"https://remote.printedwaste.com/config/GFN_SERVERID_TO_REGION_MAPPING";
|
||||
const PROVISION_URL: &str = "https://opennow-proxy-production.up.railway.app/api/public/proxy";
|
||||
const COMMUNITY_PROXY_HOST: &str = "altaria.proxy.rlwy.net";
|
||||
const COMMUNITY_PROXY_PORT: u16 = 51_545;
|
||||
const MAXIMUM_RESPONSE_BYTES: u64 = 4 * 1024 * 1024;
|
||||
|
||||
pub struct CommunityService {
|
||||
client: Client,
|
||||
}
|
||||
|
||||
impl CommunityService {
|
||||
pub fn new() -> Result<Self, String> {
|
||||
let client = Client::builder()
|
||||
.connect_timeout(Duration::from_secs(8))
|
||||
.timeout(Duration::from_secs(45))
|
||||
.pool_idle_timeout(Duration::from_secs(30))
|
||||
.build()
|
||||
.map_err(|error| error.to_string())?;
|
||||
Ok(Self { client })
|
||||
}
|
||||
|
||||
pub fn queue(&self) -> Result<Value, String> {
|
||||
let payload = self.get_json(QUEUE_URL, Duration::from_secs(7))?;
|
||||
normalize_queue(&payload)
|
||||
}
|
||||
|
||||
pub fn server_mapping(&self) -> Result<Value, String> {
|
||||
let payload = self.get_json(SERVER_MAPPING_URL, Duration::from_secs(7))?;
|
||||
normalize_server_mapping(&payload)
|
||||
}
|
||||
|
||||
pub fn provision_proxy(&self, client_id: &str) -> Result<Value, String> {
|
||||
if client_id.len() < 16 || client_id.len() > 128 {
|
||||
return Err("Stable device identity is invalid".to_owned());
|
||||
}
|
||||
let mut response = self.provision_request(client_id)?;
|
||||
if matches!(
|
||||
response.status().as_u16(),
|
||||
408 | 425 | 429 | 500 | 502 | 503 | 504
|
||||
) {
|
||||
thread::sleep(Duration::from_millis(1_500));
|
||||
response = self.provision_request(client_id)?;
|
||||
}
|
||||
if !response.status().is_success() {
|
||||
return Err(format!(
|
||||
"Community proxy provision failed ({})",
|
||||
response.status().as_u16()
|
||||
));
|
||||
}
|
||||
let length = response.content_length().unwrap_or(0);
|
||||
if length > MAXIMUM_RESPONSE_BYTES {
|
||||
return Err("Community proxy response exceeded the size limit".to_owned());
|
||||
}
|
||||
let payload = response
|
||||
.json::<ProvisionResponse>()
|
||||
.map_err(|_| "Community proxy provision returned invalid JSON".to_owned())?;
|
||||
let proxy_url = if let Some(value) = payload.proxy_url {
|
||||
normalize_community_proxy_url(&value)?
|
||||
} else {
|
||||
build_community_proxy_url(
|
||||
payload
|
||||
.username
|
||||
.as_deref()
|
||||
.ok_or_else(|| "Community proxy response omitted credentials".to_owned())?,
|
||||
payload
|
||||
.password
|
||||
.as_deref()
|
||||
.ok_or_else(|| "Community proxy response omitted credentials".to_owned())?,
|
||||
)?
|
||||
};
|
||||
Ok(json!({"proxyUrl":proxy_url}))
|
||||
}
|
||||
|
||||
fn get_json(&self, url: &str, timeout: Duration) -> Result<Value, String> {
|
||||
let response = self
|
||||
.client
|
||||
.get(url)
|
||||
.timeout(timeout)
|
||||
.header(
|
||||
"User-Agent",
|
||||
format!("opennow/{}", crate::version::APPLICATION_VERSION),
|
||||
)
|
||||
.header("Accept", "application/json")
|
||||
.send()
|
||||
.map_err(|_| "Community service is unavailable right now".to_owned())?;
|
||||
if !response.status().is_success() {
|
||||
return Err(format!(
|
||||
"Community service returned HTTP {}",
|
||||
response.status().as_u16()
|
||||
));
|
||||
}
|
||||
if response.content_length().unwrap_or(0) > MAXIMUM_RESPONSE_BYTES {
|
||||
return Err("Community response exceeded the size limit".to_owned());
|
||||
}
|
||||
response
|
||||
.json::<Value>()
|
||||
.map_err(|_| "Community service returned invalid JSON".to_owned())
|
||||
}
|
||||
|
||||
fn provision_request(&self, client_id: &str) -> Result<reqwest::blocking::Response, String> {
|
||||
self.client
|
||||
.post(PROVISION_URL)
|
||||
.timeout(Duration::from_secs(45))
|
||||
.header(
|
||||
"User-Agent",
|
||||
format!(
|
||||
"OpenNOW-DesktopClient/{}",
|
||||
crate::version::APPLICATION_VERSION
|
||||
),
|
||||
)
|
||||
.header("Accept", "application/json")
|
||||
.json(&json!({"clientId":client_id}))
|
||||
.send()
|
||||
.map_err(|_| "Community proxy provisioning is unavailable right now".to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct ProvisionResponse {
|
||||
proxy_url: Option<String>,
|
||||
username: Option<String>,
|
||||
password: Option<String>,
|
||||
}
|
||||
|
||||
fn data_object<'a>(payload: &'a Value, context: &str) -> Result<&'a Map<String, Value>, String> {
|
||||
if payload["status"].as_bool() != Some(true) {
|
||||
return Err(format!("{context} returned status:false"));
|
||||
}
|
||||
payload["data"]
|
||||
.as_object()
|
||||
.ok_or_else(|| format!("{context} response omitted its data object"))
|
||||
}
|
||||
|
||||
fn normalize_queue(payload: &Value) -> Result<Value, String> {
|
||||
let data = data_object(payload, "PrintedWaste queue")?;
|
||||
let mut output = Map::new();
|
||||
for (zone_id, raw) in data {
|
||||
let Some(zone) = raw.as_object() else {
|
||||
continue;
|
||||
};
|
||||
let (Some(position), Some(updated), Some(region)) = (
|
||||
zone.get("QueuePosition").and_then(Value::as_f64),
|
||||
zone.get("Last Updated").and_then(Value::as_f64),
|
||||
zone.get("Region")
|
||||
.and_then(Value::as_str)
|
||||
.filter(|value| !value.is_empty()),
|
||||
) else {
|
||||
continue;
|
||||
};
|
||||
if !position.is_finite() || !updated.is_finite() {
|
||||
continue;
|
||||
}
|
||||
let mut normalized = json!({
|
||||
"QueuePosition":position,
|
||||
"Last Updated":updated,
|
||||
"Region":region
|
||||
});
|
||||
if let Some(eta) = zone
|
||||
.get("eta")
|
||||
.and_then(Value::as_f64)
|
||||
.filter(|value| value.is_finite())
|
||||
{
|
||||
normalized["eta"] = json!(eta);
|
||||
}
|
||||
output.insert(zone_id.clone(), normalized);
|
||||
}
|
||||
if output.is_empty() {
|
||||
return Err("PrintedWaste queue returned no valid zones".to_owned());
|
||||
}
|
||||
Ok(Value::Object(output))
|
||||
}
|
||||
|
||||
fn normalize_server_mapping(payload: &Value) -> Result<Value, String> {
|
||||
let data = data_object(payload, "PrintedWaste server mapping")?;
|
||||
let mut output = Map::new();
|
||||
for (zone_id, raw) in data {
|
||||
let Some(zone) = raw.as_object() else {
|
||||
continue;
|
||||
};
|
||||
let mut normalized = Map::new();
|
||||
for key in ["title", "region"] {
|
||||
if let Some(value) = zone.get(key).and_then(Value::as_str) {
|
||||
normalized.insert(key.to_owned(), json!(value));
|
||||
}
|
||||
}
|
||||
for key in ["is4080Server", "is5080Server", "nuked"] {
|
||||
if let Some(value) = zone.get(key).and_then(Value::as_bool) {
|
||||
normalized.insert(key.to_owned(), json!(value));
|
||||
}
|
||||
}
|
||||
output.insert(zone_id.clone(), Value::Object(normalized));
|
||||
}
|
||||
Ok(Value::Object(output))
|
||||
}
|
||||
|
||||
fn build_community_proxy_url(username: &str, password: &str) -> Result<String, String> {
|
||||
if username.is_empty() {
|
||||
return Err("Community proxy response omitted credentials".to_owned());
|
||||
}
|
||||
let mut url = Url::parse(&format!(
|
||||
"http://{COMMUNITY_PROXY_HOST}:{COMMUNITY_PROXY_PORT}"
|
||||
))
|
||||
.map_err(|_| "Community proxy endpoint is invalid".to_owned())?;
|
||||
url.set_username(username)
|
||||
.map_err(|_| "Community proxy username is invalid".to_owned())?;
|
||||
url.set_password(Some(password))
|
||||
.map_err(|_| "Community proxy password is invalid".to_owned())?;
|
||||
Ok(url.to_string())
|
||||
}
|
||||
|
||||
fn normalize_community_proxy_url(raw: &str) -> Result<String, String> {
|
||||
let url = Url::parse(raw).map_err(|_| "Community proxy returned an invalid URL".to_owned())?;
|
||||
if url.scheme() != "http"
|
||||
|| url.host_str() != Some(COMMUNITY_PROXY_HOST)
|
||||
|| url.port() != Some(COMMUNITY_PROXY_PORT)
|
||||
|| url.username().is_empty()
|
||||
|| url.password().is_none()
|
||||
|| (url.path() != "" && url.path() != "/")
|
||||
|| url.query().is_some()
|
||||
|| url.fragment().is_some()
|
||||
{
|
||||
return Err("Community proxy returned an invalid URL".to_owned());
|
||||
}
|
||||
Ok(url.to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn printed_waste_payloads_are_strictly_normalized() {
|
||||
let queue = normalize_queue(&json!({"status":true,"data":{
|
||||
"zone-a":{"QueuePosition":42,"Last Updated":123,"Region":"EU","eta":9},
|
||||
"bad":{"QueuePosition":"many"}
|
||||
}}))
|
||||
.unwrap();
|
||||
assert_eq!(queue.as_object().unwrap().len(), 1);
|
||||
assert_eq!(queue["zone-a"]["Region"], "EU");
|
||||
let mapping = normalize_server_mapping(&json!({"status":true,"data":{
|
||||
"zone-a":{"title":"Paris","region":"EU","nuked":false,"ignored":"x"}
|
||||
}}))
|
||||
.unwrap();
|
||||
assert_eq!(mapping["zone-a"]["title"], "Paris");
|
||||
assert!(mapping["zone-a"].get("ignored").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn community_proxy_credentials_are_encoded_and_endpoint_scoped() {
|
||||
let value = build_community_proxy_url("player name", "p@ss/word").unwrap();
|
||||
let parsed = Url::parse(&value).unwrap();
|
||||
assert_eq!(parsed.host_str(), Some(COMMUNITY_PROXY_HOST));
|
||||
assert_eq!(parsed.port(), Some(COMMUNITY_PROXY_PORT));
|
||||
assert_eq!(parsed.username(), "player%20name");
|
||||
assert!(normalize_community_proxy_url(&value).is_ok());
|
||||
assert!(normalize_community_proxy_url("http://evil.example:51545/u").is_err());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,506 @@
|
||||
use base64::Engine as _;
|
||||
use base64::engine::general_purpose::STANDARD as BASE64;
|
||||
use rand::RngCore as _;
|
||||
use scrypt::{Params, scrypt};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, json};
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Mutex;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use subtle::ConstantTimeEq as _;
|
||||
|
||||
const PIN_MAX_ATTEMPTS: u32 = 5;
|
||||
const LOCKOUT_STEPS_MS: [u64; 4] = [30_000, 60_000, 300_000, 900_000];
|
||||
const SCRYPT_LOG_N: u8 = 15;
|
||||
const SCRYPT_R: u32 = 8;
|
||||
const SCRYPT_P: u32 = 1;
|
||||
const KEY_LENGTH: usize = 32;
|
||||
const SALT_LENGTH: usize = 16;
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct PinRecord {
|
||||
version: u32,
|
||||
algorithm: String,
|
||||
log_n: u8,
|
||||
r: u32,
|
||||
p: u32,
|
||||
key_length: usize,
|
||||
salt: String,
|
||||
hash: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct AttemptState {
|
||||
failed_attempts: u32,
|
||||
lockout_level: usize,
|
||||
locked_until_ms: Option<u64>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct Profile {
|
||||
user_id: String,
|
||||
pin: Option<PinRecord>,
|
||||
attempts: AttemptState,
|
||||
updated_at_ms: u64,
|
||||
}
|
||||
|
||||
#[derive(Default, Serialize, Deserialize)]
|
||||
struct Document {
|
||||
version: u32,
|
||||
profiles: Vec<Profile>,
|
||||
}
|
||||
|
||||
pub struct ConsoleProfiles {
|
||||
path: PathBuf,
|
||||
mutation: Mutex<()>,
|
||||
profiles: Mutex<HashMap<String, Profile>>,
|
||||
}
|
||||
|
||||
impl ConsoleProfiles {
|
||||
pub fn load(data_dir: &Path) -> Self {
|
||||
// Electron owns console-profiles.json and wraps it with safeStorage.
|
||||
// Keep the Qt/Rust format separate so a migration trial never makes
|
||||
// the shipping rollback client unable to read its profile locks.
|
||||
let path = data_dir.join("console-profiles-qt.json");
|
||||
let profiles = fs::read_to_string(&path)
|
||||
.ok()
|
||||
.and_then(|text| serde_json::from_str::<Document>(&text).ok())
|
||||
.map(|document| {
|
||||
document
|
||||
.profiles
|
||||
.into_iter()
|
||||
.filter(|profile| !profile.user_id.is_empty())
|
||||
.map(|profile| (profile.user_id.clone(), profile))
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
Self {
|
||||
path,
|
||||
mutation: Mutex::new(()),
|
||||
profiles: Mutex::new(profiles),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn has_pin(&self, user_id: &str) -> bool {
|
||||
self.profiles
|
||||
.lock()
|
||||
.expect("console profiles poisoned")
|
||||
.get(user_id)
|
||||
.is_some_and(|profile| profile.pin.is_some())
|
||||
}
|
||||
|
||||
pub fn status(&self, user_id: &str) -> Value {
|
||||
let profiles = self.profiles.lock().expect("console profiles poisoned");
|
||||
let Some(profile) = profiles
|
||||
.get(user_id)
|
||||
.filter(|profile| profile.pin.is_some())
|
||||
else {
|
||||
return json!({
|
||||
"userId":user_id,"hasPin":false,"lockedUntilMs":null,
|
||||
"remainingAttempts":PIN_MAX_ATTEMPTS
|
||||
});
|
||||
};
|
||||
let gate = gate(&profile.attempts, now_ms());
|
||||
json!({
|
||||
"userId":user_id,"hasPin":true,"lockedUntilMs":gate.locked_until_ms,
|
||||
"remainingAttempts":gate.remaining_attempts
|
||||
})
|
||||
}
|
||||
|
||||
pub fn set_pin(
|
||||
&self,
|
||||
user_id: &str,
|
||||
pin: &str,
|
||||
current_pin: Option<&str>,
|
||||
) -> Result<Value, String> {
|
||||
let _mutation = self
|
||||
.mutation
|
||||
.lock()
|
||||
.expect("console profile mutation poisoned");
|
||||
if !valid_pin(pin) {
|
||||
return Ok(
|
||||
json!({"ok":false,"reason":"invalid_format","hasPin":self.has_pin(user_id)}),
|
||||
);
|
||||
}
|
||||
let mut next = self
|
||||
.profiles
|
||||
.lock()
|
||||
.expect("console profiles poisoned")
|
||||
.clone();
|
||||
let now = now_ms();
|
||||
let profile = next.entry(user_id.to_owned()).or_insert_with(|| Profile {
|
||||
user_id: user_id.to_owned(),
|
||||
pin: None,
|
||||
attempts: AttemptState::default(),
|
||||
updated_at_ms: 0,
|
||||
});
|
||||
if let Some(existing) = profile.pin.as_ref() {
|
||||
let gate = gate(&profile.attempts, now);
|
||||
if !gate.allowed {
|
||||
return Ok(
|
||||
json!({"ok":false,"reason":"locked_out","hasPin":true,"lockedUntilMs":gate.locked_until_ms}),
|
||||
);
|
||||
}
|
||||
let Some(current_pin) = current_pin.filter(|pin| valid_pin(pin)) else {
|
||||
return Ok(json!({"ok":false,"reason":"invalid_format","hasPin":true}));
|
||||
};
|
||||
if !verify_hash(current_pin, existing) {
|
||||
register_failure(&mut profile.attempts, now);
|
||||
self.persist_and_replace(next)?;
|
||||
return Ok(json!({"ok":false,"reason":"invalid_pin","hasPin":true}));
|
||||
}
|
||||
}
|
||||
profile.pin = Some(hash_pin(pin)?);
|
||||
profile.attempts = AttemptState::default();
|
||||
profile.updated_at_ms = now;
|
||||
self.persist_and_replace(next)?;
|
||||
Ok(json!({"ok":true,"hasPin":true}))
|
||||
}
|
||||
|
||||
pub fn clear_pin(&self, user_id: &str, current_pin: &str) -> Result<Value, String> {
|
||||
let _mutation = self
|
||||
.mutation
|
||||
.lock()
|
||||
.expect("console profile mutation poisoned");
|
||||
let mut next = self
|
||||
.profiles
|
||||
.lock()
|
||||
.expect("console profiles poisoned")
|
||||
.clone();
|
||||
let Some(profile) = next.get_mut(user_id) else {
|
||||
return Ok(json!({"ok":false,"reason":"no_pin_set","hasPin":false}));
|
||||
};
|
||||
let Some(existing) = profile.pin.as_ref() else {
|
||||
return Ok(json!({"ok":false,"reason":"no_pin_set","hasPin":false}));
|
||||
};
|
||||
let now = now_ms();
|
||||
let gate = gate(&profile.attempts, now);
|
||||
if !gate.allowed {
|
||||
return Ok(
|
||||
json!({"ok":false,"reason":"locked_out","hasPin":true,"lockedUntilMs":gate.locked_until_ms}),
|
||||
);
|
||||
}
|
||||
if !valid_pin(current_pin) {
|
||||
return Ok(json!({"ok":false,"reason":"invalid_format","hasPin":true}));
|
||||
}
|
||||
if !verify_hash(current_pin, existing) {
|
||||
register_failure(&mut profile.attempts, now);
|
||||
self.persist_and_replace(next)?;
|
||||
return Ok(json!({"ok":false,"reason":"invalid_pin","hasPin":true}));
|
||||
}
|
||||
profile.pin = None;
|
||||
profile.attempts = AttemptState::default();
|
||||
profile.updated_at_ms = now;
|
||||
self.persist_and_replace(next)?;
|
||||
Ok(json!({"ok":true,"hasPin":false}))
|
||||
}
|
||||
|
||||
pub fn verify(&self, user_id: &str, pin: &str) -> Result<Value, String> {
|
||||
let _mutation = self
|
||||
.mutation
|
||||
.lock()
|
||||
.expect("console profile mutation poisoned");
|
||||
let mut next = self
|
||||
.profiles
|
||||
.lock()
|
||||
.expect("console profiles poisoned")
|
||||
.clone();
|
||||
let Some(profile) = next.get_mut(user_id) else {
|
||||
return Ok(
|
||||
json!({"ok":true,"reason":"no_pin_set","remainingAttempts":PIN_MAX_ATTEMPTS,"lockedUntilMs":null}),
|
||||
);
|
||||
};
|
||||
let Some(record) = profile.pin.as_ref() else {
|
||||
return Ok(
|
||||
json!({"ok":true,"reason":"no_pin_set","remainingAttempts":PIN_MAX_ATTEMPTS,"lockedUntilMs":null}),
|
||||
);
|
||||
};
|
||||
let now = now_ms();
|
||||
let current_gate = gate(&profile.attempts, now);
|
||||
if !current_gate.allowed {
|
||||
return Ok(
|
||||
json!({"ok":false,"reason":"locked_out","remainingAttempts":0,"lockedUntilMs":current_gate.locked_until_ms}),
|
||||
);
|
||||
}
|
||||
if !valid_pin(pin) {
|
||||
return Ok(
|
||||
json!({"ok":false,"reason":"invalid_format","remainingAttempts":current_gate.remaining_attempts,"lockedUntilMs":null}),
|
||||
);
|
||||
}
|
||||
if verify_hash(pin, record) {
|
||||
profile.attempts = AttemptState::default();
|
||||
self.persist_and_replace(next)?;
|
||||
return Ok(
|
||||
json!({"ok":true,"remainingAttempts":PIN_MAX_ATTEMPTS,"lockedUntilMs":null}),
|
||||
);
|
||||
}
|
||||
register_failure(&mut profile.attempts, now);
|
||||
let next_gate = gate(&profile.attempts, now);
|
||||
self.persist_and_replace(next)?;
|
||||
Ok(json!({
|
||||
"ok":false,
|
||||
"reason":if next_gate.allowed { "invalid_pin" } else { "locked_out" },
|
||||
"remainingAttempts":next_gate.remaining_attempts,
|
||||
"lockedUntilMs":next_gate.locked_until_ms
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn forget(&self, user_id: &str) -> Result<(), String> {
|
||||
let _mutation = self
|
||||
.mutation
|
||||
.lock()
|
||||
.expect("console profile mutation poisoned");
|
||||
let mut next = self
|
||||
.profiles
|
||||
.lock()
|
||||
.expect("console profiles poisoned")
|
||||
.clone();
|
||||
if next.remove(user_id).is_some() {
|
||||
self.persist_and_replace(next)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn forget_all(&self) -> Result<(), String> {
|
||||
let _mutation = self
|
||||
.mutation
|
||||
.lock()
|
||||
.expect("console profile mutation poisoned");
|
||||
self.persist_and_replace(HashMap::new())
|
||||
}
|
||||
|
||||
fn persist_and_replace(&self, profiles: HashMap<String, Profile>) -> Result<(), String> {
|
||||
let mut values = profiles.values().cloned().collect::<Vec<_>>();
|
||||
values.sort_by(|left, right| left.user_id.cmp(&right.user_id));
|
||||
let document = Document {
|
||||
version: 1,
|
||||
profiles: values,
|
||||
};
|
||||
let parent = self.path.parent().unwrap_or_else(|| Path::new("."));
|
||||
fs::create_dir_all(parent).map_err(|error| error.to_string())?;
|
||||
let temporary = self.path.with_extension("json.tmp");
|
||||
let bytes = serde_json::to_vec_pretty(&document).map_err(|error| error.to_string())?;
|
||||
write_private_file(&temporary, &bytes).map_err(|error| error.to_string())?;
|
||||
fs::rename(&temporary, &self.path).map_err(|error| error.to_string())?;
|
||||
*self.profiles.lock().expect("console profiles poisoned") = profiles;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct Gate {
|
||||
allowed: bool,
|
||||
locked_until_ms: Option<u64>,
|
||||
remaining_attempts: u32,
|
||||
}
|
||||
|
||||
fn gate(state: &AttemptState, now: u64) -> Gate {
|
||||
let locked = state.locked_until_ms.is_some_and(|until| now < until);
|
||||
Gate {
|
||||
allowed: !locked,
|
||||
locked_until_ms: locked.then_some(state.locked_until_ms).flatten(),
|
||||
remaining_attempts: if locked {
|
||||
0
|
||||
} else {
|
||||
PIN_MAX_ATTEMPTS.saturating_sub(state.failed_attempts)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn register_failure(state: &mut AttemptState, now: u64) {
|
||||
state.failed_attempts += 1;
|
||||
if state.failed_attempts < PIN_MAX_ATTEMPTS {
|
||||
state.locked_until_ms = None;
|
||||
return;
|
||||
}
|
||||
let duration = LOCKOUT_STEPS_MS[state.lockout_level.min(LOCKOUT_STEPS_MS.len() - 1)];
|
||||
state.failed_attempts = 0;
|
||||
state.lockout_level = (state.lockout_level + 1).min(LOCKOUT_STEPS_MS.len());
|
||||
state.locked_until_ms = Some(now.saturating_add(duration));
|
||||
}
|
||||
|
||||
fn hash_pin(pin: &str) -> Result<PinRecord, String> {
|
||||
let mut salt = [0_u8; SALT_LENGTH];
|
||||
rand::rng().fill_bytes(&mut salt);
|
||||
let params = Params::new(SCRYPT_LOG_N, SCRYPT_R, SCRYPT_P, KEY_LENGTH)
|
||||
.map_err(|error| error.to_string())?;
|
||||
let mut hash = [0_u8; KEY_LENGTH];
|
||||
scrypt(pin.as_bytes(), &salt, ¶ms, &mut hash).map_err(|error| error.to_string())?;
|
||||
Ok(PinRecord {
|
||||
version: 1,
|
||||
algorithm: "scrypt".to_owned(),
|
||||
log_n: SCRYPT_LOG_N,
|
||||
r: SCRYPT_R,
|
||||
p: SCRYPT_P,
|
||||
key_length: KEY_LENGTH,
|
||||
salt: BASE64.encode(salt),
|
||||
hash: BASE64.encode(hash),
|
||||
})
|
||||
}
|
||||
|
||||
fn verify_hash(pin: &str, record: &PinRecord) -> bool {
|
||||
if record.version != 1
|
||||
|| record.algorithm != "scrypt"
|
||||
|| record.key_length == 0
|
||||
|| record.key_length > 64
|
||||
{
|
||||
return false;
|
||||
}
|
||||
let Ok(salt) = BASE64.decode(&record.salt) else {
|
||||
return false;
|
||||
};
|
||||
let Ok(expected) = BASE64.decode(&record.hash) else {
|
||||
return false;
|
||||
};
|
||||
if expected.len() != record.key_length {
|
||||
return false;
|
||||
}
|
||||
let Ok(params) = Params::new(record.log_n, record.r, record.p, record.key_length) else {
|
||||
return false;
|
||||
};
|
||||
let mut actual = vec![0_u8; record.key_length];
|
||||
if scrypt(pin.as_bytes(), &salt, ¶ms, &mut actual).is_err() {
|
||||
return false;
|
||||
}
|
||||
bool::from(actual.ct_eq(&expected))
|
||||
}
|
||||
|
||||
fn valid_pin(pin: &str) -> bool {
|
||||
pin.len() == 4 && pin.bytes().all(|character| character.is_ascii_digit())
|
||||
}
|
||||
|
||||
fn now_ms() -> u64 {
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_millis() as u64
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn write_private_file(path: &Path, data: &[u8]) -> io::Result<()> {
|
||||
use std::io::Write as _;
|
||||
use std::os::unix::fs::OpenOptionsExt as _;
|
||||
let mut file = fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.write(true)
|
||||
.mode(0o600)
|
||||
.open(path)?;
|
||||
file.write_all(data)?;
|
||||
file.sync_all()
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn write_private_file(path: &Path, data: &[u8]) -> io::Result<()> {
|
||||
fs::write(path, data)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn temporary_store() -> (ConsoleProfiles, PathBuf) {
|
||||
let path = std::env::temp_dir().join(format!(
|
||||
"opennow-profiles-{}-{}-{}",
|
||||
std::process::id(),
|
||||
now_ms(),
|
||||
rand::random::<u64>()
|
||||
));
|
||||
(ConsoleProfiles::load(&path), path)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overlapping_pin_writes_preserve_every_acknowledged_account() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let profiles = ConsoleProfiles::load(directory.path());
|
||||
let barrier = std::sync::Barrier::new(4);
|
||||
std::thread::scope(|scope| {
|
||||
let workers: Vec<_> = (0..4)
|
||||
.map(|index| {
|
||||
let profiles = &profiles;
|
||||
let barrier = &barrier;
|
||||
scope.spawn(move || {
|
||||
barrier.wait();
|
||||
profiles.set_pin(&format!("user-{index}"), "1234", None)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
for worker in workers {
|
||||
assert_eq!(worker.join().unwrap().unwrap()["ok"], true);
|
||||
}
|
||||
});
|
||||
let loaded = ConsoleProfiles::load(directory.path());
|
||||
for index in 0..4 {
|
||||
assert!(profiles.has_pin(&format!("user-{index}")));
|
||||
assert!(loaded.has_pin(&format!("user-{index}")));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overlapping_pin_failures_all_count_toward_lockout() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let profiles = ConsoleProfiles::load(directory.path());
|
||||
profiles.set_pin("user", "1234", None).unwrap();
|
||||
let barrier = std::sync::Barrier::new(PIN_MAX_ATTEMPTS as usize);
|
||||
std::thread::scope(|scope| {
|
||||
let workers: Vec<_> = (0..PIN_MAX_ATTEMPTS)
|
||||
.map(|_| {
|
||||
scope.spawn(|| {
|
||||
barrier.wait();
|
||||
profiles.verify("user", "0000")
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
for worker in workers {
|
||||
assert_eq!(worker.join().unwrap().unwrap()["ok"], false);
|
||||
}
|
||||
});
|
||||
assert_eq!(profiles.status("user")["remainingAttempts"], 0);
|
||||
let loaded = ConsoleProfiles::load(directory.path());
|
||||
assert_eq!(loaded.status("user")["remainingAttempts"], 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn profile_persistence_failure_preserves_committed_state() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let profiles = ConsoleProfiles::load(directory.path());
|
||||
profiles.set_pin("user", "1234", None).unwrap();
|
||||
let persisted = fs::read(&profiles.path).unwrap();
|
||||
fs::create_dir(profiles.path.with_extension("json.tmp")).unwrap();
|
||||
assert!(profiles.clear_pin("user", "1234").is_err());
|
||||
assert!(profiles.forget_all().is_err());
|
||||
assert!(profiles.has_pin("user"));
|
||||
assert_eq!(fs::read(&profiles.path).unwrap(), persisted);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pin_round_trip_and_lockout_are_persisted() {
|
||||
let (profiles, path) = temporary_store();
|
||||
assert_eq!(profiles.set_pin("user", "1234", None).unwrap()["ok"], true);
|
||||
assert!(profiles.has_pin("user"));
|
||||
assert_eq!(profiles.verify("user", "1234").unwrap()["ok"], true);
|
||||
for _ in 0..PIN_MAX_ATTEMPTS {
|
||||
let _ = profiles.verify("user", "0000").unwrap();
|
||||
}
|
||||
assert_eq!(profiles.status("user")["remainingAttempts"], 0);
|
||||
let loaded = ConsoleProfiles::load(&path);
|
||||
assert_eq!(loaded.status("user")["remainingAttempts"], 0);
|
||||
let _ = fs::remove_dir_all(path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_pin_does_not_consume_an_attempt() {
|
||||
let (profiles, path) = temporary_store();
|
||||
profiles.set_pin("user", "1234", None).unwrap();
|
||||
let result = profiles.verify("user", "1234").unwrap();
|
||||
assert_eq!(result["reason"], "invalid_format");
|
||||
assert_eq!(result["remainingAttempts"], PIN_MAX_ATTEMPTS);
|
||||
let _ = fs::remove_dir_all(path);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
+451
@@ -0,0 +1,451 @@
|
||||
use super::SecretStore;
|
||||
use aes_gcm::aead::{Aead, Payload};
|
||||
use aes_gcm::{Aes256Gcm, KeyInit, Nonce};
|
||||
use base64::Engine as _;
|
||||
use base64::engine::general_purpose::STANDARD;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::fs;
|
||||
use std::io::{self, Read as _, Write as _};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
const MAX_SESSION_BYTES: usize = 4 * 1024 * 1024;
|
||||
const NONCE_BYTES: usize = 12;
|
||||
const TAG_BYTES: usize = 16;
|
||||
|
||||
pub(super) struct EncryptedSecretStore {
|
||||
root: PathBuf,
|
||||
keys: Box<dyn SecretStore>,
|
||||
legacy: Box<dyn SecretStore>,
|
||||
}
|
||||
|
||||
impl EncryptedSecretStore {
|
||||
pub(super) fn new(
|
||||
root: PathBuf,
|
||||
keys: Box<dyn SecretStore>,
|
||||
legacy: Box<dyn SecretStore>,
|
||||
) -> Self {
|
||||
Self { root, keys, legacy }
|
||||
}
|
||||
|
||||
fn path(&self, user_id: &str) -> PathBuf {
|
||||
let digest = Sha256::digest(user_id.as_bytes());
|
||||
self.root.join(format!("{digest:x}.bin"))
|
||||
}
|
||||
|
||||
fn read_key(&self, user_id: &str) -> Result<Option<[u8; 32]>, String> {
|
||||
let Some(encoded) = self.keys.get(user_id)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
if encoded.len() != 44 {
|
||||
return Err("Saved session encryption key is invalid".into());
|
||||
}
|
||||
let bytes = STANDARD
|
||||
.decode(encoded)
|
||||
.map_err(|_| "Saved session encryption key is invalid")?;
|
||||
let key = bytes
|
||||
.try_into()
|
||||
.map_err(|_| "Saved session encryption key is invalid")?;
|
||||
Ok(Some(key))
|
||||
}
|
||||
|
||||
fn write_encrypted(&self, path: &Path, user_id: &str, encoded: &str) -> Result<(), String> {
|
||||
let key = match self.read_key(user_id)? {
|
||||
Some(key) => key,
|
||||
None => {
|
||||
let key = rand::random::<[u8; 32]>();
|
||||
if path.exists() {
|
||||
return Err("Saved session encryption key is missing".into());
|
||||
}
|
||||
self.keys.set(user_id, &STANDARD.encode(key))?;
|
||||
key
|
||||
}
|
||||
};
|
||||
let nonce = rand::random::<[u8; NONCE_BYTES]>();
|
||||
let cipher = Aes256Gcm::new_from_slice(&key)
|
||||
.map_err(|_| "Saved session encryption key is invalid")?;
|
||||
let ciphertext = cipher
|
||||
.encrypt(
|
||||
Nonce::from_slice(&nonce),
|
||||
Payload {
|
||||
msg: encoded.as_bytes(),
|
||||
aad: user_id.as_bytes(),
|
||||
},
|
||||
)
|
||||
.map_err(|_| "Could not encrypt saved session")?;
|
||||
fs::create_dir_all(&self.root).map_err(|_| "Could not create secure session directory")?;
|
||||
let temporary = self
|
||||
.root
|
||||
.join(format!("{:032x}.tmp", rand::random::<u128>()));
|
||||
let result = (|| -> io::Result<()> {
|
||||
let mut file = fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.open(&temporary)?;
|
||||
file.write_all(&nonce)?;
|
||||
file.write_all(&ciphertext)?;
|
||||
file.sync_all()?;
|
||||
fs::rename(&temporary, path)?;
|
||||
Ok(())
|
||||
})();
|
||||
if result.is_err() {
|
||||
let _ = fs::remove_file(&temporary);
|
||||
}
|
||||
result.map_err(|_| "Could not write encrypted session".into())
|
||||
}
|
||||
}
|
||||
|
||||
impl SecretStore for EncryptedSecretStore {
|
||||
fn get(&self, user_id: &str) -> Result<Option<String>, String> {
|
||||
let file = match fs::File::open(self.path(user_id)) {
|
||||
Ok(file) => file,
|
||||
Err(error) if error.kind() == io::ErrorKind::NotFound => {
|
||||
let legacy = self.legacy.get(user_id)?;
|
||||
if let Some(encoded) = &legacy {
|
||||
let _ = self.set(user_id, encoded);
|
||||
}
|
||||
return Ok(legacy);
|
||||
}
|
||||
Err(_) => return Err("Encrypted session could not be read".into()),
|
||||
};
|
||||
let mut bytes = Vec::new();
|
||||
file.take((MAX_SESSION_BYTES + NONCE_BYTES + TAG_BYTES + 1) as u64)
|
||||
.read_to_end(&mut bytes)
|
||||
.map_err(|_| "Encrypted session could not be read")?;
|
||||
if bytes.len() < NONCE_BYTES + TAG_BYTES
|
||||
|| bytes.len() > MAX_SESSION_BYTES + NONCE_BYTES + TAG_BYTES
|
||||
{
|
||||
return Err("Encrypted session exceeds size or format limits".into());
|
||||
}
|
||||
let key = self
|
||||
.read_key(user_id)?
|
||||
.ok_or("Saved session encryption key is missing")?;
|
||||
let cipher = Aes256Gcm::new_from_slice(&key)
|
||||
.map_err(|_| "Saved session encryption key is invalid")?;
|
||||
let plaintext = cipher
|
||||
.decrypt(
|
||||
Nonce::from_slice(&bytes[..NONCE_BYTES]),
|
||||
Payload {
|
||||
msg: &bytes[NONCE_BYTES..],
|
||||
aad: user_id.as_bytes(),
|
||||
},
|
||||
)
|
||||
.map_err(|_| "Encrypted session authentication failed")?;
|
||||
let encoded =
|
||||
String::from_utf8(plaintext).map_err(|_| "Encrypted session contains invalid text")?;
|
||||
let _ = self.legacy.delete(user_id);
|
||||
Ok(Some(encoded))
|
||||
}
|
||||
|
||||
fn set(&self, user_id: &str, encoded: &str) -> Result<(), String> {
|
||||
if encoded.len() > MAX_SESSION_BYTES || user_id.trim().is_empty() {
|
||||
return Err("Saved credential exceeds size or identity limits".into());
|
||||
}
|
||||
self.write_encrypted(&self.path(user_id), user_id, encoded)
|
||||
}
|
||||
|
||||
fn delete(&self, user_id: &str) -> Result<(), String> {
|
||||
let path = self.path(user_id);
|
||||
let file = match fs::remove_file(path) {
|
||||
Ok(()) => Ok(()),
|
||||
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()),
|
||||
Err(_) => Err("Encrypted session could not be removed".into()),
|
||||
};
|
||||
let key = self.keys.delete(user_id);
|
||||
let legacy = self.legacy.delete(user_id);
|
||||
file.and(key).and(legacy)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct TestStore {
|
||||
entries: Arc<Mutex<HashMap<String, String>>>,
|
||||
limit: Option<usize>,
|
||||
fail_set: bool,
|
||||
}
|
||||
|
||||
impl SecretStore for TestStore {
|
||||
fn get(&self, user_id: &str) -> Result<Option<String>, String> {
|
||||
Ok(self.entries.lock().unwrap().get(user_id).cloned())
|
||||
}
|
||||
|
||||
fn set(&self, user_id: &str, encoded: &str) -> Result<(), String> {
|
||||
if self.fail_set
|
||||
|| self
|
||||
.limit
|
||||
.is_some_and(|limit| encoded.encode_utf16().count() * 2 > limit)
|
||||
{
|
||||
return Err("credential write failed".into());
|
||||
}
|
||||
self.entries
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(user_id.into(), encoded.into());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn delete(&self, user_id: &str) -> Result<(), String> {
|
||||
self.entries.lock().unwrap().remove(user_id);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn store(root: &Path, keys: TestStore, legacy: TestStore) -> EncryptedSecretStore {
|
||||
EncryptedSecretStore::new(root.into(), Box::new(keys), Box::new(legacy))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn large_session_reopens_without_plaintext_or_credential_blob_overflow() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let root = directory.path().join("secure-sessions");
|
||||
let keys = TestStore {
|
||||
limit: Some(2560),
|
||||
..Default::default()
|
||||
};
|
||||
let legacy = TestStore {
|
||||
limit: Some(2560),
|
||||
..Default::default()
|
||||
};
|
||||
let session = "opaque-payload".repeat(300);
|
||||
assert!(session.encode_utf16().count() * 2 > 2560);
|
||||
let first = store(&root, keys.clone(), legacy.clone());
|
||||
first.set("account", &session).unwrap();
|
||||
assert!(
|
||||
!fs::read(first.path("account"))
|
||||
.unwrap()
|
||||
.windows(session.len())
|
||||
.any(|window| window == session.as_bytes())
|
||||
);
|
||||
assert_eq!(keys.get("account").unwrap().unwrap().len(), 44);
|
||||
assert_eq!(
|
||||
store(&root, keys, legacy).get("account").unwrap(),
|
||||
Some(session)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_migration_survives_failed_write_and_removes_old_entry_after_reopen() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let root = directory.path().join("secure-sessions");
|
||||
let keys = TestStore::default();
|
||||
let legacy = TestStore::default();
|
||||
legacy.set("account", "legacy-session").unwrap();
|
||||
let blocked = store(
|
||||
&root,
|
||||
TestStore {
|
||||
fail_set: true,
|
||||
..keys.clone()
|
||||
},
|
||||
legacy.clone(),
|
||||
);
|
||||
assert_eq!(
|
||||
blocked.get("account").unwrap().as_deref(),
|
||||
Some("legacy-session")
|
||||
);
|
||||
assert_eq!(
|
||||
legacy.get("account").unwrap().as_deref(),
|
||||
Some("legacy-session")
|
||||
);
|
||||
assert!(keys.get("account").unwrap().is_none());
|
||||
keys.set("account", &STANDARD.encode(rand::random::<[u8; 32]>()))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
store(&root, keys.clone(), legacy.clone())
|
||||
.get("account")
|
||||
.unwrap()
|
||||
.as_deref(),
|
||||
Some("legacy-session")
|
||||
);
|
||||
assert_eq!(
|
||||
store(&root, keys, legacy.clone())
|
||||
.get("account")
|
||||
.unwrap()
|
||||
.as_deref(),
|
||||
Some("legacy-session")
|
||||
);
|
||||
assert!(legacy.get("account").unwrap().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn corrupted_or_missing_key_does_not_fall_back_or_rekey_ciphertext() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let root = directory.path().join("secure-sessions");
|
||||
let keys = TestStore::default();
|
||||
let legacy = TestStore::default();
|
||||
let first = store(&root, keys.clone(), legacy.clone());
|
||||
first.set("account", "current-session").unwrap();
|
||||
legacy.set("account", "stale-session").unwrap();
|
||||
let path = first.path("account");
|
||||
let original = fs::read(&path).unwrap();
|
||||
keys.delete("account").unwrap();
|
||||
assert!(first.get("account").unwrap_err().contains("key is missing"));
|
||||
assert!(
|
||||
first
|
||||
.set("account", "replacement")
|
||||
.unwrap_err()
|
||||
.contains("key is missing")
|
||||
);
|
||||
assert_eq!(fs::read(&path).unwrap(), original);
|
||||
keys.set("account", &STANDARD.encode(rand::random::<[u8; 32]>()))
|
||||
.unwrap();
|
||||
assert!(
|
||||
first
|
||||
.get("account")
|
||||
.unwrap_err()
|
||||
.contains("authentication failed")
|
||||
);
|
||||
assert_eq!(
|
||||
legacy.get("account").unwrap().as_deref(),
|
||||
Some("stale-session")
|
||||
);
|
||||
first.delete("account").unwrap();
|
||||
assert!(!path.exists());
|
||||
assert!(keys.get("account").unwrap().is_none());
|
||||
assert!(legacy.get("account").unwrap().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oversized_input_and_ciphertext_are_rejected_without_migration() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let root = directory.path().join("secure-sessions");
|
||||
let keys = TestStore::default();
|
||||
let legacy = TestStore::default();
|
||||
let first = store(&root, keys.clone(), legacy.clone());
|
||||
assert!(
|
||||
first
|
||||
.set("account", &"x".repeat(MAX_SESSION_BYTES + 1))
|
||||
.is_err()
|
||||
);
|
||||
assert!(keys.get("account").unwrap().is_none());
|
||||
fs::create_dir_all(&root).unwrap();
|
||||
fs::write(
|
||||
first.path("account"),
|
||||
vec![0; MAX_SESSION_BYTES + NONCE_BYTES + TAG_BYTES + 1],
|
||||
)
|
||||
.unwrap();
|
||||
legacy.set("account", "old-session").unwrap();
|
||||
assert!(first.get("account").unwrap_err().contains("size or format"));
|
||||
assert_eq!(
|
||||
legacy.get("account").unwrap().as_deref(),
|
||||
Some("old-session")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sign_out_deletes_both_stores_and_next_sign_in_uses_new_key() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let root = directory.path().join("secure-sessions");
|
||||
let keys = TestStore::default();
|
||||
let legacy = TestStore::default();
|
||||
let vault = store(&root, keys.clone(), legacy.clone());
|
||||
vault.set("account", "old-session").unwrap();
|
||||
let old_key = keys.get("account").unwrap().unwrap();
|
||||
vault.delete("account").unwrap();
|
||||
assert!(vault.get("account").unwrap().is_none());
|
||||
vault.set("account", "new-session").unwrap();
|
||||
assert_ne!(keys.get("account").unwrap().unwrap(), old_key);
|
||||
assert_eq!(
|
||||
store(&root, keys, legacy)
|
||||
.get("account")
|
||||
.unwrap()
|
||||
.as_deref(),
|
||||
Some("new-session")
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn windows_credential_manager_migrates_and_restores_after_restart() {
|
||||
use crate::credential_vault::{
|
||||
CredentialVault, KEY_SERVICE_NAME, SERVICE_NAME, credential,
|
||||
};
|
||||
|
||||
struct Cleanup(String);
|
||||
impl Drop for Cleanup {
|
||||
fn drop(&mut self) {
|
||||
let _ = credential(SERVICE_NAME, &self.0)
|
||||
.and_then(|entry| entry.delete_credential().map_err(|error| error.to_string()));
|
||||
let _ = credential(KEY_SERVICE_NAME, &self.0)
|
||||
.and_then(|entry| entry.delete_credential().map_err(|error| error.to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let user_id = format!("opennow-vault-test-{:032x}", rand::random::<u128>());
|
||||
let _cleanup = Cleanup(user_id.clone());
|
||||
let mut session = super::super::tests::sample_session(&user_id);
|
||||
let legacy = serde_json::to_string(&session).unwrap();
|
||||
credential(SERVICE_NAME, &user_id)
|
||||
.unwrap()
|
||||
.set_password(&legacy)
|
||||
.unwrap();
|
||||
let vault = CredentialVault::new(directory.path().into());
|
||||
vault
|
||||
.write_metadata(&super::super::Metadata {
|
||||
active_user_id: Some(user_id.clone()),
|
||||
accounts: vec![super::super::tests::sample_identity(&user_id)],
|
||||
..Default::default()
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(vault.load_active().unwrap().unwrap().user.user_id, user_id);
|
||||
drop(vault);
|
||||
let reopened = CredentialVault::new(directory.path().into());
|
||||
assert_eq!(
|
||||
reopened.load_active().unwrap().unwrap().user.user_id,
|
||||
user_id
|
||||
);
|
||||
assert!(matches!(
|
||||
credential(SERVICE_NAME, &user_id).unwrap().get_password(),
|
||||
Err(keyring::Error::NoEntry)
|
||||
));
|
||||
|
||||
session.tokens.access_token = "x".repeat(1800);
|
||||
let size = serde_json::to_string(&session).unwrap().len();
|
||||
assert!((1944..2560).contains(&size), "fixture size: {size}");
|
||||
reopened.save(&session).unwrap();
|
||||
drop(reopened);
|
||||
let restored = CredentialVault::new(directory.path().into());
|
||||
assert_eq!(
|
||||
restored.load_active().unwrap().unwrap().tokens.access_token,
|
||||
session.tokens.access_token
|
||||
);
|
||||
let encrypted = directory
|
||||
.path()
|
||||
.join("secure-sessions")
|
||||
.join(format!("{:x}.bin", Sha256::digest(user_id.as_bytes())));
|
||||
assert!(
|
||||
!fs::read(encrypted)
|
||||
.unwrap()
|
||||
.windows(1800)
|
||||
.any(|window| window == session.tokens.access_token.as_bytes())
|
||||
);
|
||||
session.tokens.access_token = "x".repeat(3200);
|
||||
assert!(
|
||||
serde_json::to_string(&session)
|
||||
.unwrap()
|
||||
.encode_utf16()
|
||||
.count()
|
||||
* 2
|
||||
> 2560
|
||||
);
|
||||
restored.save(&session).unwrap();
|
||||
drop(restored);
|
||||
let restored = CredentialVault::new(directory.path().into());
|
||||
assert_eq!(
|
||||
restored.load_active().unwrap().unwrap().tokens.access_token,
|
||||
session.tokens.access_token
|
||||
);
|
||||
restored.remove(&user_id).unwrap();
|
||||
assert!(
|
||||
CredentialVault::new(directory.path().into())
|
||||
.load(&user_id)
|
||||
.unwrap()
|
||||
.is_none()
|
||||
);
|
||||
}
|
||||
}
|
||||
+721
@@ -0,0 +1,721 @@
|
||||
use super::{SecretStore, decode_session, read_bounded};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::fs;
|
||||
use std::io::{self, Write as _};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub(super) struct JsonFallbackStore {
|
||||
root: PathBuf,
|
||||
secure: Box<dyn SecretStore>,
|
||||
}
|
||||
|
||||
impl JsonFallbackStore {
|
||||
pub(super) fn new(root: PathBuf, secure: Box<dyn SecretStore>) -> Self {
|
||||
Self { root, secure }
|
||||
}
|
||||
|
||||
fn path(&self, user_id: &str) -> PathBuf {
|
||||
let digest = Sha256::digest(user_id.as_bytes());
|
||||
self.root.join(format!("{digest:x}.json"))
|
||||
}
|
||||
|
||||
pub(super) fn cleanup_temporary_files(&self) -> Result<(), String> {
|
||||
let entries = match fs::read_dir(&self.root) {
|
||||
Ok(entries) => entries,
|
||||
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(()),
|
||||
Err(_) => return Err("Local session temporary-file cleanup is pending".into()),
|
||||
};
|
||||
for entry in entries {
|
||||
let entry = entry.map_err(|_| "Local session temporary-file cleanup is pending")?;
|
||||
let name = entry.file_name();
|
||||
let name = name.to_string_lossy();
|
||||
if name.strip_suffix(".tmp").is_some_and(|stem| {
|
||||
stem.len() == 64 && stem.bytes().all(|byte| byte.is_ascii_hexdigit())
|
||||
}) {
|
||||
fs::remove_file(entry.path())
|
||||
.map_err(|_| "Local session temporary-file cleanup is pending")?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write(&self, user_id: &str, encoded: &str) -> Result<(), String> {
|
||||
let result = (|| -> io::Result<()> {
|
||||
fs::create_dir_all(&self.root)?;
|
||||
if !fs::symlink_metadata(&self.root)?.is_dir() {
|
||||
return Err(io::Error::other("Invalid credential directory"));
|
||||
}
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
fs::set_permissions(&self.root, fs::Permissions::from_mode(0o700))?;
|
||||
}
|
||||
let temporary = self.path(user_id).with_extension("tmp");
|
||||
match fs::remove_file(&temporary) {
|
||||
Ok(()) => (),
|
||||
Err(error) if error.kind() == io::ErrorKind::NotFound => (),
|
||||
Err(error) => return Err(error),
|
||||
}
|
||||
let result = (|| -> io::Result<()> {
|
||||
let mut file = create_private_file(&temporary)?;
|
||||
file.write_all(encoded.as_bytes())?;
|
||||
file.sync_all()?;
|
||||
drop(file);
|
||||
fs::rename(&temporary, self.path(user_id))?;
|
||||
#[cfg(unix)]
|
||||
fs::File::open(&self.root)?.sync_all()?;
|
||||
Ok(())
|
||||
})();
|
||||
if result.is_err() {
|
||||
let _ = fs::remove_file(&temporary);
|
||||
}
|
||||
result
|
||||
})();
|
||||
result.map_err(|_| "Could not save the local session file".into())
|
||||
}
|
||||
|
||||
fn remove_file(&self, user_id: &str) -> Result<(), String> {
|
||||
let mut result = Ok(());
|
||||
for path in [self.path(user_id), self.path(user_id).with_extension("tmp")] {
|
||||
match fs::remove_file(path) {
|
||||
Ok(()) => (),
|
||||
Err(error) if error.kind() == io::ErrorKind::NotFound => (),
|
||||
Err(_) => result = Err("Could not remove the local session file".into()),
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
impl SecretStore for JsonFallbackStore {
|
||||
fn get(&self, user_id: &str) -> Result<Option<String>, String> {
|
||||
match read_bounded(&self.path(user_id)) {
|
||||
Ok(bytes) => {
|
||||
let encoded = String::from_utf8(bytes).map_err(|_| "Local session is invalid")?;
|
||||
decode_session(&encoded, user_id)?;
|
||||
Ok(Some(encoded))
|
||||
}
|
||||
Err(error) if error.kind() == io::ErrorKind::NotFound => self.secure.get(user_id),
|
||||
Err(_) => Err("Could not read the local session file".into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn set(&self, user_id: &str, encoded: &str) -> Result<(), String> {
|
||||
decode_session(encoded, user_id)?;
|
||||
let local = self
|
||||
.path(user_id)
|
||||
.try_exists()
|
||||
.map_err(|_| "Could not inspect local session")?;
|
||||
if local {
|
||||
self.write(user_id, encoded)?;
|
||||
}
|
||||
if self.secure.set(user_id, encoded).is_ok()
|
||||
&& self.secure.get(user_id).ok().flatten().as_deref() == Some(encoded)
|
||||
{
|
||||
self.remove_file(user_id)?;
|
||||
return Ok(());
|
||||
}
|
||||
if !local {
|
||||
self.write(user_id, encoded)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn delete(&self, user_id: &str) -> Result<(), String> {
|
||||
let local = self.remove_file(user_id);
|
||||
let secure = self.secure.delete(user_id);
|
||||
local.and(secure)
|
||||
}
|
||||
|
||||
fn remove_all_local(
|
||||
&self,
|
||||
remove_account: &dyn Fn(&str) -> Result<(), String>,
|
||||
) -> Result<(), String> {
|
||||
match fs::symlink_metadata(&self.root) {
|
||||
Ok(metadata) if metadata.is_dir() => (),
|
||||
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(()),
|
||||
_ => return Err("Local session directory cleanup is pending".into()),
|
||||
}
|
||||
let entries =
|
||||
fs::read_dir(&self.root).map_err(|_| "Local session directory cleanup is pending")?;
|
||||
let mut failure = None;
|
||||
for entry in entries {
|
||||
let entry = match entry {
|
||||
Ok(entry) => entry,
|
||||
Err(_) => {
|
||||
failure.get_or_insert_with(|| "Local session entry cleanup is pending".into());
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let path = entry.path();
|
||||
let Some(extension @ ("json" | "tmp")) =
|
||||
path.extension().and_then(|value| value.to_str())
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
if !path
|
||||
.file_stem()
|
||||
.and_then(|value| value.to_str())
|
||||
.is_some_and(|stem| {
|
||||
stem.len() == 64 && stem.bytes().all(|byte| byte.is_ascii_hexdigit())
|
||||
})
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let session = if entry.file_type().is_ok_and(|kind| kind.is_file()) {
|
||||
match read_bounded(&path) {
|
||||
Ok(bytes) => serde_json::from_slice::<super::AuthSession>(&bytes).ok(),
|
||||
Err(error) if error.kind() == io::ErrorKind::NotFound => continue,
|
||||
Err(_) => None,
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if let Some(session) = session.filter(|session| {
|
||||
!session.user.user_id.trim().is_empty()
|
||||
&& self.path(&session.user.user_id).with_extension(extension) == path
|
||||
}) {
|
||||
if let Err(error) = remove_account(&session.user.user_id) {
|
||||
failure.get_or_insert(error);
|
||||
}
|
||||
} else {
|
||||
failure.get_or_insert_with(|| {
|
||||
"Local session identity could not be recovered; secure cleanup is pending"
|
||||
.into()
|
||||
});
|
||||
}
|
||||
match fs::remove_file(&path) {
|
||||
Ok(()) => (),
|
||||
Err(error) if error.kind() == io::ErrorKind::NotFound => (),
|
||||
Err(_) => {
|
||||
failure.get_or_insert_with(|| "Could not remove the local session file".into());
|
||||
}
|
||||
}
|
||||
}
|
||||
failure.map_or(Ok(()), Err)
|
||||
}
|
||||
|
||||
fn is_plaintext(&self, user_id: &str) -> bool {
|
||||
self.path(user_id).exists()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn create_private_file(path: &Path) -> io::Result<fs::File> {
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.mode(0o600)
|
||||
.open(path)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn create_private_file(path: &Path) -> io::Result<fs::File> {
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
use std::os::windows::io::FromRawHandle;
|
||||
use windows_sys::Win32::Foundation::{INVALID_HANDLE_VALUE, LocalFree};
|
||||
use windows_sys::Win32::Security::Authorization::ConvertStringSecurityDescriptorToSecurityDescriptorW;
|
||||
use windows_sys::Win32::Security::SECURITY_ATTRIBUTES;
|
||||
use windows_sys::Win32::Storage::FileSystem::{
|
||||
CREATE_NEW, CreateFileW, FILE_ATTRIBUTE_NORMAL, FILE_GENERIC_WRITE,
|
||||
};
|
||||
|
||||
let text: Vec<u16> = "D:P(A;;FA;;;OW)(A;;FA;;;SY)"
|
||||
.encode_utf16()
|
||||
.chain(Some(0))
|
||||
.collect();
|
||||
let mut descriptor = std::ptr::null_mut();
|
||||
if unsafe {
|
||||
ConvertStringSecurityDescriptorToSecurityDescriptorW(
|
||||
text.as_ptr(),
|
||||
1,
|
||||
&mut descriptor,
|
||||
std::ptr::null_mut(),
|
||||
)
|
||||
} == 0
|
||||
{
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
let attributes = SECURITY_ATTRIBUTES {
|
||||
nLength: std::mem::size_of::<SECURITY_ATTRIBUTES>() as u32,
|
||||
lpSecurityDescriptor: descriptor,
|
||||
bInheritHandle: 0,
|
||||
};
|
||||
let wide: Vec<u16> = path.as_os_str().encode_wide().chain(Some(0)).collect();
|
||||
let handle = unsafe {
|
||||
CreateFileW(
|
||||
wide.as_ptr(),
|
||||
FILE_GENERIC_WRITE,
|
||||
0,
|
||||
&attributes,
|
||||
CREATE_NEW,
|
||||
FILE_ATTRIBUTE_NORMAL,
|
||||
std::ptr::null_mut(),
|
||||
)
|
||||
};
|
||||
let error = io::Error::last_os_error();
|
||||
unsafe {
|
||||
LocalFree(descriptor);
|
||||
}
|
||||
if handle == INVALID_HANDLE_VALUE {
|
||||
return Err(error);
|
||||
}
|
||||
Ok(unsafe { fs::File::from_raw_handle(handle) })
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::credential_vault::{CredentialVault, MemorySecretStore, tests::sample_session};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct Store {
|
||||
entries: Arc<Mutex<HashMap<String, String>>>,
|
||||
unavailable: Arc<AtomicBool>,
|
||||
discard_writes: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl SecretStore for Store {
|
||||
fn get(&self, user_id: &str) -> Result<Option<String>, String> {
|
||||
if self.unavailable.load(Ordering::SeqCst) {
|
||||
return Err("unavailable".into());
|
||||
}
|
||||
Ok(self.entries.lock().unwrap().get(user_id).cloned())
|
||||
}
|
||||
|
||||
fn set(&self, user_id: &str, encoded: &str) -> Result<(), String> {
|
||||
if self.unavailable.load(Ordering::SeqCst) {
|
||||
return Err("unavailable".into());
|
||||
}
|
||||
if !self.discard_writes.load(Ordering::SeqCst) {
|
||||
self.entries
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(user_id.into(), encoded.into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn delete(&self, user_id: &str) -> Result<(), String> {
|
||||
if self.unavailable.load(Ordering::SeqCst) {
|
||||
return Err("unavailable".into());
|
||||
}
|
||||
self.entries.lock().unwrap().remove(user_id);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn secure_store_is_preferred_without_creating_json() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let vault = CredentialVault::with_store(directory.path().into(), Box::<Store>::default());
|
||||
let session = sample_session("user");
|
||||
vault.save(&session).unwrap();
|
||||
assert_eq!(vault.persistence_state(&session), "secure-store");
|
||||
assert!(!directory.path().join("fallback-sessions").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn logout_all_cleans_orphans_without_readable_metadata() {
|
||||
for corrupt in [false, true] {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let secure = Store::default();
|
||||
let vault =
|
||||
CredentialVault::with_store(directory.path().into(), Box::new(secure.clone()));
|
||||
let session = sample_session("orphan");
|
||||
vault.save(&session).unwrap();
|
||||
secure.unavailable.store(true, Ordering::SeqCst);
|
||||
vault.save(&session).unwrap();
|
||||
secure.unavailable.store(false, Ordering::SeqCst);
|
||||
if corrupt {
|
||||
fs::write(directory.path().join("accounts.json"), "invalid metadata").unwrap();
|
||||
} else {
|
||||
fs::remove_file(directory.path().join("accounts.json")).unwrap();
|
||||
}
|
||||
let result = vault.remove_all();
|
||||
assert_eq!(result.is_err(), corrupt);
|
||||
assert_eq!(
|
||||
fs::read_dir(directory.path().join("fallback-sessions"))
|
||||
.unwrap()
|
||||
.count(),
|
||||
0
|
||||
);
|
||||
assert!(secure.get("orphan").unwrap().is_none());
|
||||
if corrupt {
|
||||
assert_eq!(
|
||||
fs::read_to_string(directory.path().join("accounts.json")).unwrap(),
|
||||
"invalid metadata"
|
||||
);
|
||||
} else {
|
||||
assert!(vault.load("orphan").unwrap().is_none());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn logout_all_suppresses_orphans_when_secure_delete_fails() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let secure = Store::default();
|
||||
let vault = CredentialVault::with_store(directory.path().into(), Box::new(secure.clone()));
|
||||
let session = sample_session("orphan");
|
||||
vault.save(&session).unwrap();
|
||||
secure.unavailable.store(true, Ordering::SeqCst);
|
||||
vault.save(&session).unwrap();
|
||||
fs::remove_file(directory.path().join("accounts.json")).unwrap();
|
||||
assert!(vault.remove_all().is_err());
|
||||
assert_eq!(
|
||||
fs::read_dir(directory.path().join("fallback-sessions"))
|
||||
.unwrap()
|
||||
.count(),
|
||||
0
|
||||
);
|
||||
secure.unavailable.store(false, Ordering::SeqCst);
|
||||
assert!(secure.get("orphan").unwrap().is_some());
|
||||
let reopened =
|
||||
CredentialVault::with_store(directory.path().into(), Box::new(secure.clone()));
|
||||
assert!(reopened.load_active().unwrap().is_none());
|
||||
assert!(reopened.load("orphan").unwrap().is_none());
|
||||
reopened.remove_all().unwrap();
|
||||
assert!(secure.get("orphan").unwrap().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn logout_all_removes_invalid_and_partial_local_files_without_skipping_valid_accounts() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let secure = Store::default();
|
||||
let vault = CredentialVault::with_store(directory.path().into(), Box::new(secure.clone()));
|
||||
let root = directory.path().join("fallback-sessions");
|
||||
let store = JsonFallbackStore::new(root.clone(), Box::new(secure.clone()));
|
||||
fs::create_dir_all(&root).unwrap();
|
||||
let invalid = store.path("invalid");
|
||||
fs::write(&invalid, "invalid-json").unwrap();
|
||||
let encoded = serde_json::to_string(&sample_session("partial")).unwrap();
|
||||
secure.set("partial", &encoded).unwrap();
|
||||
let partial = store.path("partial").with_extension("tmp");
|
||||
fs::write(&partial, encoded).unwrap();
|
||||
let other = root.join("unrelated.json");
|
||||
fs::write(&other, "unrelated").unwrap();
|
||||
assert!(vault.remove_all().is_err());
|
||||
assert!(!invalid.exists());
|
||||
assert!(!partial.exists());
|
||||
assert!(other.exists());
|
||||
assert!(secure.get("partial").unwrap().is_none());
|
||||
assert!(vault.load("partial").unwrap().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn metadata_failure_does_not_hide_plaintext_storage() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let vault = CredentialVault::without_os_store(directory.path().into());
|
||||
let mut session = sample_session("user");
|
||||
vault.save(&session).unwrap();
|
||||
fs::create_dir(directory.path().join("accounts.json.tmp")).unwrap();
|
||||
session.tokens.refresh_token = Some("new-test-token".into());
|
||||
assert!(vault.save(&session).is_err());
|
||||
assert_eq!(vault.failed_save_state(&session), "local-file");
|
||||
assert!(!vault.warnings().is_empty());
|
||||
drop(vault);
|
||||
let restored = CredentialVault::without_os_store(directory.path().into());
|
||||
assert_eq!(
|
||||
restored
|
||||
.load_active()
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.tokens
|
||||
.refresh_token,
|
||||
session.tokens.refresh_token
|
||||
);
|
||||
fs::remove_dir(directory.path().join("accounts.json.tmp")).unwrap();
|
||||
restored.save(&session).unwrap();
|
||||
assert!(restored.warnings().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn restart_cleans_interrupted_plaintext_writes_without_account_metadata() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let root = directory.path().join("fallback-sessions");
|
||||
fs::create_dir(&root).unwrap();
|
||||
let store = JsonFallbackStore::new(root.clone(), Box::<Store>::default());
|
||||
let session = serde_json::to_string(&sample_session("unindexed")).unwrap();
|
||||
let pending = store.path("unindexed").with_extension("tmp");
|
||||
fs::write(&pending, &session).unwrap();
|
||||
let committed = store.path("other");
|
||||
fs::write(&committed, "not-temporary").unwrap();
|
||||
CredentialVault::without_os_store(directory.path().into());
|
||||
assert!(!pending.exists());
|
||||
assert!(committed.exists());
|
||||
assert!(!directory.path().join("accounts.json").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn logout_and_save_remove_only_the_accounts_interrupted_write() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let store = JsonFallbackStore::new(
|
||||
directory.path().into(),
|
||||
Box::new(MemorySecretStore {
|
||||
unavailable: true,
|
||||
..Default::default()
|
||||
}),
|
||||
);
|
||||
let encoded = serde_json::to_string(&sample_session("user")).unwrap();
|
||||
let pending = store.path("user").with_extension("tmp");
|
||||
let other = store.path("other").with_extension("tmp");
|
||||
fs::write(&pending, &encoded).unwrap();
|
||||
fs::write(&other, &encoded).unwrap();
|
||||
assert!(store.delete("user").is_err());
|
||||
assert!(!pending.exists());
|
||||
assert!(other.exists());
|
||||
fs::write(&pending, &encoded).unwrap();
|
||||
store.set("user", &encoded).unwrap();
|
||||
assert!(!pending.exists());
|
||||
assert!(other.exists());
|
||||
assert_eq!(
|
||||
store.get("user").unwrap().as_deref(),
|
||||
Some(encoded.as_str())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn latest_json_wins_until_a_verified_secure_save() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let secure = Store::default();
|
||||
let vault = CredentialVault::with_store(directory.path().into(), Box::new(secure.clone()));
|
||||
let mut session = sample_session("user");
|
||||
vault.save(&session).unwrap();
|
||||
secure.unavailable.store(true, Ordering::SeqCst);
|
||||
session.tokens.refresh_token = Some("refreshed-test-token".into());
|
||||
vault.save(&session).unwrap();
|
||||
assert_eq!(vault.persistence_state(&session), "local-file");
|
||||
drop(vault);
|
||||
secure.unavailable.store(false, Ordering::SeqCst);
|
||||
let restored =
|
||||
CredentialVault::with_store(directory.path().into(), Box::new(secure.clone()));
|
||||
assert_eq!(
|
||||
restored
|
||||
.load_active()
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.tokens
|
||||
.refresh_token,
|
||||
Some("refreshed-test-token".into())
|
||||
);
|
||||
assert_eq!(restored.persistence_state(&session), "local-file");
|
||||
secure.discard_writes.store(true, Ordering::SeqCst);
|
||||
restored.save(&session).unwrap();
|
||||
assert_eq!(restored.persistence_state(&session), "local-file");
|
||||
secure.discard_writes.store(false, Ordering::SeqCst);
|
||||
restored.save(&session).unwrap();
|
||||
assert_eq!(restored.persistence_state(&session), "secure-store");
|
||||
assert_eq!(
|
||||
fs::read_dir(directory.path().join("fallback-sessions"))
|
||||
.unwrap()
|
||||
.count(),
|
||||
0
|
||||
);
|
||||
assert_eq!(
|
||||
restored
|
||||
.load_active()
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.tokens
|
||||
.refresh_token,
|
||||
Some("refreshed-test-token".into())
|
||||
);
|
||||
restored.remove("user").unwrap();
|
||||
assert!(secure.get("user").unwrap().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn logout_removes_json_and_suppresses_old_secure_grant() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let secure = Store::default();
|
||||
let vault = CredentialVault::with_store(directory.path().into(), Box::new(secure.clone()));
|
||||
let mut session = sample_session("user");
|
||||
vault.save(&session).unwrap();
|
||||
secure.unavailable.store(true, Ordering::SeqCst);
|
||||
session.tokens.refresh_token = Some("new-test-token".into());
|
||||
vault.save(&session).unwrap();
|
||||
assert!(vault.remove("user").is_err());
|
||||
assert_eq!(
|
||||
fs::read_dir(directory.path().join("fallback-sessions"))
|
||||
.unwrap()
|
||||
.count(),
|
||||
0
|
||||
);
|
||||
secure.unavailable.store(false, Ordering::SeqCst);
|
||||
let restored = CredentialVault::with_store(directory.path().into(), Box::new(secure));
|
||||
assert!(restored.load("user").unwrap().is_none());
|
||||
assert!(restored.load_active().unwrap().is_none());
|
||||
restored.remove("user").unwrap();
|
||||
assert!(restored.warnings().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn windows_encryption_store_without_vault_keys_uses_json() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let unavailable = || {
|
||||
Box::new(MemorySecretStore {
|
||||
unavailable: true,
|
||||
..Default::default()
|
||||
}) as Box<dyn SecretStore>
|
||||
};
|
||||
let secure = super::super::encrypted_secret_store::EncryptedSecretStore::new(
|
||||
directory.path().join("secure-sessions"),
|
||||
unavailable(),
|
||||
unavailable(),
|
||||
);
|
||||
let vault = CredentialVault::with_store(directory.path().into(), Box::new(secure));
|
||||
let session = sample_session("user");
|
||||
vault.save(&session).unwrap();
|
||||
assert_eq!(vault.persistence_state(&session), "local-file");
|
||||
assert!(vault.durable(&session));
|
||||
assert!(!directory.path().join("secure-sessions").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_or_mismatched_local_json_does_not_restore_stale_secure_tokens() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let store = JsonFallbackStore::new(directory.path().into(), Box::<Store>::default());
|
||||
let encoded = serde_json::to_string(&sample_session("user")).unwrap();
|
||||
store.secure.set("user", &encoded).unwrap();
|
||||
fs::write(store.path("user"), "invalid-json").unwrap();
|
||||
assert!(store.get("user").is_err());
|
||||
fs::write(
|
||||
store.path("user"),
|
||||
serde_json::to_string(&sample_session("other")).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(store.get("user").is_err());
|
||||
assert!(store.set("other", &encoded).is_err());
|
||||
assert!(store.set("user", &"x".repeat(4 * 1024 * 1024 + 1)).is_err());
|
||||
assert_ne!(store.path("a/b"), store.path("a_b"));
|
||||
assert_eq!(store.path("../../outside").parent(), Some(directory.path()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unwritable_fallback_reports_failure_without_temporary_files() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let root = directory.path().join("not-a-directory");
|
||||
fs::write(&root, "blocked").unwrap();
|
||||
let store = JsonFallbackStore::new(
|
||||
root,
|
||||
Box::new(MemorySecretStore {
|
||||
unavailable: true,
|
||||
..Default::default()
|
||||
}),
|
||||
);
|
||||
assert!(
|
||||
store
|
||||
.set(
|
||||
"user",
|
||||
&serde_json::to_string(&sample_session("user")).unwrap()
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(fs::read_dir(directory.path()).unwrap().count(), 1);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn json_permissions_remain_private_across_atomic_replacements() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let store = JsonFallbackStore::new(
|
||||
directory.path().join("fallback"),
|
||||
Box::new(MemorySecretStore {
|
||||
unavailable: true,
|
||||
..Default::default()
|
||||
}),
|
||||
);
|
||||
let mut session = sample_session("user");
|
||||
for token in ["first-test-token", "second-test-token"] {
|
||||
session.tokens.refresh_token = Some(token.into());
|
||||
let encoded = serde_json::to_string(&session).unwrap();
|
||||
store.set("user", &encoded).unwrap();
|
||||
assert_eq!(
|
||||
fs::metadata(store.path("user"))
|
||||
.unwrap()
|
||||
.permissions()
|
||||
.mode()
|
||||
& 0o777,
|
||||
0o600
|
||||
);
|
||||
assert_eq!(
|
||||
fs::metadata(&store.root).unwrap().permissions().mode() & 0o777,
|
||||
0o700
|
||||
);
|
||||
assert_eq!(
|
||||
store.get("user").unwrap().as_deref(),
|
||||
Some(encoded.as_str())
|
||||
);
|
||||
assert_eq!(fs::read_dir(&store.root).unwrap().count(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn windows_json_has_a_protected_owner_and_system_acl_after_replacement() {
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
use windows_sys::Win32::Foundation::LocalFree;
|
||||
use windows_sys::Win32::Security::Authorization::{GetNamedSecurityInfoW, SE_FILE_OBJECT};
|
||||
use windows_sys::Win32::Security::{
|
||||
DACL_SECURITY_INFORMATION, GetSecurityDescriptorControl, SE_DACL_PROTECTED,
|
||||
};
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let store = JsonFallbackStore::new(
|
||||
directory.path().join("fallback"),
|
||||
Box::new(MemorySecretStore {
|
||||
unavailable: true,
|
||||
..Default::default()
|
||||
}),
|
||||
);
|
||||
let mut session = sample_session("user");
|
||||
for token in ["first-test-token", "second-test-token"] {
|
||||
session.tokens.refresh_token = Some(token.into());
|
||||
let encoded = serde_json::to_string(&session).unwrap();
|
||||
store.set("user", &encoded).unwrap();
|
||||
assert_eq!(
|
||||
store.get("user").unwrap().as_deref(),
|
||||
Some(encoded.as_str())
|
||||
);
|
||||
let path: Vec<u16> = store
|
||||
.path("user")
|
||||
.as_os_str()
|
||||
.encode_wide()
|
||||
.chain(Some(0))
|
||||
.collect();
|
||||
let mut descriptor = std::ptr::null_mut();
|
||||
let mut acl = std::ptr::null_mut();
|
||||
assert_eq!(
|
||||
unsafe {
|
||||
GetNamedSecurityInfoW(
|
||||
path.as_ptr(),
|
||||
SE_FILE_OBJECT,
|
||||
DACL_SECURITY_INFORMATION,
|
||||
std::ptr::null_mut(),
|
||||
std::ptr::null_mut(),
|
||||
&mut acl,
|
||||
std::ptr::null_mut(),
|
||||
&mut descriptor,
|
||||
)
|
||||
},
|
||||
0
|
||||
);
|
||||
let mut control = 0;
|
||||
let mut revision = 0;
|
||||
let success =
|
||||
unsafe { GetSecurityDescriptorControl(descriptor, &mut control, &mut revision) };
|
||||
let entries = unsafe { (*acl).AceCount };
|
||||
unsafe {
|
||||
LocalFree(descriptor);
|
||||
}
|
||||
assert_ne!(success, 0);
|
||||
assert_ne!(control & SE_DACL_PROTECTED, 0);
|
||||
assert_eq!(entries, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
use fs2::FileExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs::{self, OpenOptions};
|
||||
use std::io::{Read, Write};
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Identity {
|
||||
version: u8,
|
||||
id: String,
|
||||
}
|
||||
|
||||
pub fn load(data_dir: &Path, legacy_id: impl FnOnce() -> String) -> Result<String, String> {
|
||||
let load = || -> Result<String, Box<dyn std::error::Error>> {
|
||||
fs::create_dir_all(data_dir)?;
|
||||
let lock = OpenOptions::new()
|
||||
.create(true)
|
||||
.truncate(false)
|
||||
.read(true)
|
||||
.write(true)
|
||||
.open(data_dir.join("device-identity.lock"))?;
|
||||
lock.lock_exclusive()?;
|
||||
let path = data_dir.join("device-identity.json");
|
||||
match fs::File::open(&path) {
|
||||
Ok(file) => {
|
||||
let mut bytes = Vec::new();
|
||||
file.take(4097).read_to_end(&mut bytes)?;
|
||||
let identity: Identity = serde_json::from_slice(&bytes)?;
|
||||
if identity.version != 1
|
||||
|| identity.id.len() != 64
|
||||
|| !identity.id.bytes().all(|byte| byte.is_ascii_hexdigit())
|
||||
{
|
||||
return Err("Device identity is invalid".into());
|
||||
}
|
||||
Ok(identity.id)
|
||||
}
|
||||
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
|
||||
let upgraded = [
|
||||
"accounts.json",
|
||||
"auth-state.json",
|
||||
"sessions",
|
||||
"pending-session-cleanup.json",
|
||||
]
|
||||
.iter()
|
||||
.any(|name| data_dir.join(name).exists());
|
||||
let id = if upgraded {
|
||||
legacy_id()
|
||||
} else {
|
||||
use rand::RngCore as _;
|
||||
let mut bytes = [0u8; 32];
|
||||
rand::rng().fill_bytes(&mut bytes);
|
||||
bytes.iter().map(|byte| format!("{byte:02x}")).collect()
|
||||
};
|
||||
let temporary = path.with_extension("json.tmp");
|
||||
let mut file = fs::File::create(&temporary)?;
|
||||
file.write_all(&serde_json::to_vec(&Identity {
|
||||
version: 1,
|
||||
id: id.clone(),
|
||||
})?)?;
|
||||
file.sync_all()?;
|
||||
fs::rename(temporary, path)?;
|
||||
#[cfg(unix)]
|
||||
fs::File::open(data_dir)?.sync_all()?;
|
||||
Ok(id)
|
||||
}
|
||||
Err(error) => Err(error.into()),
|
||||
}
|
||||
};
|
||||
load().map_err(|_| "Persistent device identity is unavailable or invalid; restore access to the data directory before signing in".into())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn freezes_legacy_identity_and_refuses_corruption() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
fs::write(directory.path().join("accounts.json"), "{}").unwrap();
|
||||
let original = "a".repeat(64);
|
||||
assert_eq!(
|
||||
load(directory.path(), || original.clone()).unwrap(),
|
||||
original
|
||||
);
|
||||
assert_eq!(load(directory.path(), || "b".repeat(64)).unwrap(), original);
|
||||
fs::write(directory.path().join("device-identity.json"), "broken").unwrap();
|
||||
assert!(load(directory.path(), || "c".repeat(64)).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn concurrent_fresh_initialization_chooses_one_identity() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
std::thread::scope(|scope| {
|
||||
let workers: Vec<_> = (0..8)
|
||||
.map(|_| scope.spawn(|| load(directory.path(), || panic!("fresh installation"))))
|
||||
.collect();
|
||||
let ids: Vec<_> = workers
|
||||
.into_iter()
|
||||
.map(|worker| worker.join().unwrap().unwrap())
|
||||
.collect();
|
||||
assert!(ids.iter().all(|id| id == &ids[0] && id.len() == 64));
|
||||
});
|
||||
}
|
||||
}
|
||||
+1255
File diff suppressed because it is too large.
Load diff
+2113
File diff suppressed because it is too large.
Load diff
+292
@@ -0,0 +1,292 @@
|
||||
//! Cloud DLSS overrides, mirroring the official GeForce NOW client.
|
||||
//!
|
||||
//! The setting is stored as the exact wire object the official client sends in
|
||||
//! `requestedStreamingFeatures.dlssOverrides`. Every field is an enum value from the
|
||||
//! client's `DlssSrMode`, `DlssSrModel`, `DlssRrModel`, `DlssFgMode`, and `DlssFgModel`;
|
||||
//! zero means "use the game's own setting".
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
pub const SETTING: &str = "dlssOverrides";
|
||||
|
||||
const SR_MODE_CUSTOM: i64 = 6;
|
||||
const SR_CUSTOM_DEFAULT_SCALE: i64 = 75;
|
||||
const SR_MINIMUM_SCALE: i64 = 33;
|
||||
const SR_MAXIMUM_SCALE: i64 = 100;
|
||||
const SR_MAXIMUM_MODEL: i64 = 14;
|
||||
const RR_MAXIMUM_MODEL: i64 = 7;
|
||||
const FG_MAXIMUM_MODEL: i64 = 3;
|
||||
const FG_MODE_DYNAMIC: i64 = 1;
|
||||
const FG_MULTIPLIERS: [i64; 4] = [3, 4, 5, 6];
|
||||
|
||||
pub fn default_overrides() -> Value {
|
||||
json!({"srMode":0,"srModel":0,"srScale":100,"rrModel":0,"fgMode":0,"fgModel":0,"fgMultiplier":0})
|
||||
}
|
||||
|
||||
/// Input resolution percentage the official client pairs with each fixed SR mode.
|
||||
fn fixed_sr_scale(sr_mode: i64) -> Option<i64> {
|
||||
match sr_mode {
|
||||
0 | 5 => Some(100),
|
||||
3 => Some(67),
|
||||
2 => Some(58),
|
||||
1 => Some(50),
|
||||
4 => Some(33),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn integer(value: &Value, key: &str) -> Option<i64> {
|
||||
value[key]
|
||||
.as_f64()
|
||||
.filter(|number| number.is_finite() && number.fract() == 0.0)
|
||||
.map(|number| number as i64)
|
||||
}
|
||||
|
||||
fn bounded(value: &Value, key: &str, maximum: i64) -> i64 {
|
||||
integer(value, key)
|
||||
.filter(|number| (0..=maximum).contains(number))
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
/// Clamp every field to a known enum value and apply the official client's coupling:
|
||||
/// a model left to the game also leaves its mode to the game, fixed SR modes carry their
|
||||
/// fixed input resolution, and the multiplier only exists for Dynamic frame generation.
|
||||
pub fn normalize(value: &Value) -> Value {
|
||||
let sr_model = bounded(value, "srModel", SR_MAXIMUM_MODEL);
|
||||
let sr_mode = if sr_model == 0 {
|
||||
0
|
||||
} else {
|
||||
bounded(value, "srMode", SR_MODE_CUSTOM)
|
||||
};
|
||||
let sr_scale = fixed_sr_scale(sr_mode).unwrap_or_else(|| {
|
||||
integer(value, "srScale")
|
||||
.unwrap_or(SR_CUSTOM_DEFAULT_SCALE)
|
||||
.clamp(SR_MINIMUM_SCALE, SR_MAXIMUM_SCALE)
|
||||
});
|
||||
let fg_model = bounded(value, "fgModel", FG_MAXIMUM_MODEL);
|
||||
let fg_mode = if fg_model == 0 {
|
||||
0
|
||||
} else {
|
||||
bounded(value, "fgMode", FG_MODE_DYNAMIC)
|
||||
};
|
||||
let fg_multiplier = if fg_mode == FG_MODE_DYNAMIC {
|
||||
integer(value, "fgMultiplier")
|
||||
.filter(|multiplier| FG_MULTIPLIERS.contains(multiplier))
|
||||
.unwrap_or(FG_MULTIPLIERS[0])
|
||||
} else {
|
||||
0
|
||||
};
|
||||
json!({
|
||||
"srMode":sr_mode,
|
||||
"srModel":sr_model,
|
||||
"srScale":sr_scale,
|
||||
"rrModel":bounded(value, "rrModel", RR_MAXIMUM_MODEL),
|
||||
"fgMode":fg_mode,
|
||||
"fgModel":fg_model,
|
||||
"fgMultiplier":fg_multiplier
|
||||
})
|
||||
}
|
||||
|
||||
/// The `requestedStreamingFeatures.dlssOverrides` object. Like the official client, the
|
||||
/// multiplier is omitted unless frame generation is Dynamic.
|
||||
pub fn session_request(settings: &Value) -> Value {
|
||||
let mut overrides = normalize(&settings[SETTING]);
|
||||
if overrides["fgMode"] != FG_MODE_DYNAMIC {
|
||||
overrides
|
||||
.as_object_mut()
|
||||
.expect("normalized overrides are an object")
|
||||
.remove("fgMultiplier");
|
||||
}
|
||||
overrides
|
||||
}
|
||||
|
||||
/// Which overrides the MES subscription response entitles or upsells, read the way the
|
||||
/// official client's ecommerce service reads `features.features` and `upsells`.
|
||||
pub fn entitlements(subscription: &Value) -> Value {
|
||||
let features = subscription["features"]["features"]
|
||||
.as_array()
|
||||
.map(Vec::as_slice)
|
||||
.unwrap_or_default();
|
||||
let feature = |key: &str| {
|
||||
features
|
||||
.iter()
|
||||
.find(|feature| feature["key"].as_str() == Some(key))
|
||||
};
|
||||
let entitled = |key: &str| {
|
||||
feature(key)
|
||||
.and_then(|feature| feature["textValue"].as_str())
|
||||
.and_then(|text| serde_json::from_str::<Value>(text).ok())
|
||||
.is_some_and(|value| match value {
|
||||
Value::Null => false,
|
||||
Value::Bool(enabled) => enabled,
|
||||
Value::Number(number) => number.as_f64().is_some_and(|number| number != 0.0),
|
||||
Value::String(text) => !text.is_empty(),
|
||||
Value::Array(items) => !items.is_empty(),
|
||||
Value::Object(_) => true,
|
||||
})
|
||||
};
|
||||
let upsell = |key: &str| {
|
||||
subscription["upsells"]
|
||||
.as_array()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.any(|upsell| upsell["key"].as_str() == Some(key) && upsell["booleanValue"] == true)
|
||||
};
|
||||
let capability = |name: &str| {
|
||||
json!({
|
||||
"entitled":entitled(&format!("SUPPORTED_DLSS_OVERRIDE_{name}")),
|
||||
"upsell":upsell(&format!("SUPPORT_DLSS_OVERRIDE_{name}"))
|
||||
})
|
||||
};
|
||||
let multiplier_entitled = |multiplier: i64| {
|
||||
let label = format!("{multiplier}X");
|
||||
let Some(feature) = feature("SUPPORTED_DLSS_FG_MULTIPLIERS") else {
|
||||
return false;
|
||||
};
|
||||
if let Some(text) = feature["textValue"].as_str() {
|
||||
return match serde_json::from_str::<Value>(text) {
|
||||
Ok(Value::Array(items)) => items.iter().any(|item| {
|
||||
item["value"].as_str() == Some(label.as_str()) && item["isEntitled"] == true
|
||||
}),
|
||||
Ok(Value::Bool(enabled)) => enabled,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
feature["setValue"]
|
||||
.as_array()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.any(|value| value.as_str() == Some(label.as_str()))
|
||||
};
|
||||
json!({
|
||||
"superResolution":capability("SR"),
|
||||
"frameGeneration":capability("FG"),
|
||||
"rayReconstruction":capability("RR"),
|
||||
"frameGenerationMultipliers":FG_MULTIPLIERS
|
||||
.into_iter()
|
||||
.filter(|multiplier| multiplier_entitled(*multiplier))
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn defaults_leave_every_feature_to_the_game() {
|
||||
assert_eq!(normalize(&Value::Null), default_overrides());
|
||||
assert_eq!(
|
||||
session_request(&json!({})),
|
||||
json!({"srMode":0,"srModel":0,"srScale":100,"rrModel":0,"fgMode":0,"fgModel":0})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fixed_sr_modes_carry_the_official_input_resolution() {
|
||||
for (mode, scale) in [(0, 100), (1, 50), (2, 58), (3, 67), (4, 33), (5, 100)] {
|
||||
let normalized = normalize(&json!({"srModel":1,"srMode":mode,"srScale":90}));
|
||||
assert_eq!(normalized["srMode"], mode);
|
||||
assert_eq!(normalized["srScale"], scale, "mode {mode}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn custom_sr_scale_is_clamped_and_defaults_to_75() {
|
||||
assert_eq!(
|
||||
normalize(&json!({"srModel":1,"srMode":6,"srScale":12}))["srScale"],
|
||||
33
|
||||
);
|
||||
assert_eq!(
|
||||
normalize(&json!({"srModel":1,"srMode":6,"srScale":140}))["srScale"],
|
||||
100
|
||||
);
|
||||
assert_eq!(
|
||||
normalize(&json!({"srModel":1,"srMode":6,"srScale":64.0}))["srScale"],
|
||||
64
|
||||
);
|
||||
assert_eq!(normalize(&json!({"srModel":1,"srMode":6}))["srScale"], 75);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_model_left_to_the_game_leaves_its_mode_to_the_game() {
|
||||
let normalized =
|
||||
normalize(&json!({"srModel":0,"srMode":2,"fgModel":0,"fgMode":1,"fgMultiplier":4}));
|
||||
assert_eq!(normalized, default_overrides());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiplier_exists_only_for_dynamic_frame_generation() {
|
||||
let dynamic = json!({"dlssOverrides":{"fgModel":1,"fgMode":1,"fgMultiplier":4}});
|
||||
assert_eq!(session_request(&dynamic)["fgMultiplier"], 4);
|
||||
let invalid = normalize(&json!({"fgModel":1,"fgMode":1,"fgMultiplier":2}));
|
||||
assert_eq!(invalid["fgMultiplier"], 3);
|
||||
let fixed = normalize(&json!({"fgModel":1,"fgMode":2,"fgMultiplier":4}));
|
||||
assert_eq!(fixed["fgMode"], 0);
|
||||
assert_eq!(fixed["fgMultiplier"], 0);
|
||||
assert!(
|
||||
session_request(&json!({"dlssOverrides":fixed}))
|
||||
.get("fgMultiplier")
|
||||
.is_none()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_enum_values_fall_back_to_the_game() {
|
||||
let normalized = normalize(
|
||||
&json!({"srModel":15,"srMode":7,"rrModel":8,"fgModel":4,"fgMode":"1","srScale":"58"}),
|
||||
);
|
||||
assert_eq!(normalized, default_overrides());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_screenshot_configuration_reaches_the_wire() {
|
||||
let settings = json!({"dlssOverrides":{"srMode":2,"srModel":1,"srScale":10,"rrModel":1,"fgMode":1,"fgModel":1,"fgMultiplier":3}});
|
||||
assert_eq!(
|
||||
session_request(&settings),
|
||||
json!({"srMode":2,"srModel":1,"srScale":58,"rrModel":1,"fgMode":1,"fgModel":1,"fgMultiplier":3})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entitlements_follow_the_official_feature_and_upsell_readers() {
|
||||
let subscription = json!({
|
||||
"features":{"features":[
|
||||
{"key":"SUPPORTED_DLSS_OVERRIDE_SR","textValue":"true"},
|
||||
{"key":"SUPPORTED_DLSS_OVERRIDE_FG","textValue":"[\"DYNAMIC\"]"},
|
||||
{"key":"SUPPORTED_DLSS_OVERRIDE_RR","textValue":"false"},
|
||||
{"key":"SUPPORTED_DLSS_FG_MULTIPLIERS","textValue":"[{\"value\":\"3X\",\"isEntitled\":true},{\"value\":\"4X\",\"isEntitled\":true},{\"value\":\"5X\",\"isEntitled\":false}]"}
|
||||
]},
|
||||
"upsells":[
|
||||
{"key":"SUPPORT_DLSS_OVERRIDE_RR","booleanValue":true},
|
||||
{"key":"SUPPORT_DLSS_OVERRIDE_FG","booleanValue":false}
|
||||
]
|
||||
});
|
||||
assert_eq!(
|
||||
entitlements(&subscription),
|
||||
json!({
|
||||
"superResolution":{"entitled":true,"upsell":false},
|
||||
"frameGeneration":{"entitled":true,"upsell":false},
|
||||
"rayReconstruction":{"entitled":false,"upsell":true},
|
||||
"frameGenerationMultipliers":[3,4]
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiplier_set_values_and_missing_features_are_read() {
|
||||
let subscription = json!({"features":{"features":[
|
||||
{"key":"SUPPORTED_DLSS_FG_MULTIPLIERS","setValue":["3X","4X","5X","6X"]}
|
||||
]}});
|
||||
let read = entitlements(&subscription);
|
||||
assert_eq!(read["frameGenerationMultipliers"], json!([3, 4, 5, 6]));
|
||||
assert_eq!(
|
||||
read["superResolution"],
|
||||
json!({"entitled":false,"upsell":false})
|
||||
);
|
||||
assert_eq!(
|
||||
entitlements(&json!({}))["frameGenerationMultipliers"],
|
||||
json!([])
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,403 @@
|
||||
use serde_json::{Value, json};
|
||||
|
||||
pub const SUPPORTED_FRAME_RATES: [i64; 8] = [30, 60, 90, 120, 144, 165, 240, 360];
|
||||
|
||||
pub fn snap_frame_rate(requested: i64) -> i64 {
|
||||
if requested == 59 {
|
||||
return 60;
|
||||
}
|
||||
SUPPORTED_FRAME_RATES
|
||||
.into_iter()
|
||||
.rev()
|
||||
.find(|rate| *rate <= requested)
|
||||
.unwrap_or(30)
|
||||
}
|
||||
|
||||
pub fn resolution_ceiling(width: i64, height: i64) -> i64 {
|
||||
if width == 1920 && matches!(height, 1080 | 1200) {
|
||||
360
|
||||
} else {
|
||||
240
|
||||
}
|
||||
}
|
||||
|
||||
fn is_full_hd(width: i64, height: i64) -> bool {
|
||||
resolution_ceiling(width, height) > 240
|
||||
}
|
||||
|
||||
fn resolution(settings: &Value) -> (i64, i64) {
|
||||
settings["resolution"]
|
||||
.as_str()
|
||||
.and_then(|value| value.split_once(['x', 'X']))
|
||||
.and_then(|(width, height)| Some((width.parse().ok()?, height.parse().ok()?)))
|
||||
.unwrap_or((1920, 1080))
|
||||
}
|
||||
|
||||
fn hardware_decode_available(settings: &Value, capabilities: &Value) -> bool {
|
||||
let requested_backend = crate::streamer::requested_embedded_backend(settings);
|
||||
let requested_codec = settings["codec"]
|
||||
.as_str()
|
||||
.unwrap_or("auto")
|
||||
.trim()
|
||||
.to_ascii_lowercase();
|
||||
let explicit_codec = match requested_codec.as_str() {
|
||||
"" | "auto" => None,
|
||||
value => Some(value),
|
||||
};
|
||||
capabilities["videoBackends"]
|
||||
.as_array()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter(|backend| backend["available"].as_bool().unwrap_or(false))
|
||||
.filter(|backend| {
|
||||
let name = backend["backend"].as_str().unwrap_or("");
|
||||
!matches!(name, "software" | "ffmpeg")
|
||||
&& (requested_backend == "auto"
|
||||
|| requested_backend == name
|
||||
|| (requested_backend == "nvdec" && name == "cuda"))
|
||||
})
|
||||
.flat_map(|backend| backend["codecs"].as_array().into_iter().flatten())
|
||||
.any(|entry| {
|
||||
if entry["available"].as_bool() != Some(true) {
|
||||
return false;
|
||||
}
|
||||
let name = entry["codec"].as_str().unwrap_or("");
|
||||
match &explicit_codec {
|
||||
Some(codec) => name.eq_ignore_ascii_case(codec),
|
||||
None => ["h264", "h265", "av1"]
|
||||
.iter()
|
||||
.any(|codec| name.eq_ignore_ascii_case(codec)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const BASE_FRAME_RATE_CEILING: i64 = 240;
|
||||
|
||||
pub fn request_frame_rate(settings: &Value, params: &Value, width: i64, height: i64) -> i64 {
|
||||
let requested = settings["fps"].as_i64().unwrap_or(60).clamp(30, 360);
|
||||
let mut rate = requested.min(resolution_ceiling(width, height));
|
||||
if rate > BASE_FRAME_RATE_CEILING {
|
||||
let capabilities = ¶ms["runtimeCapabilities"];
|
||||
let entitled = params["maxEntitledFps"].as_i64().unwrap_or(0).clamp(0, 360);
|
||||
let cap = if entitled > 0 {
|
||||
entitled.min(BASE_FRAME_RATE_CEILING)
|
||||
} else {
|
||||
BASE_FRAME_RATE_CEILING
|
||||
};
|
||||
if !hardware_decode_available(settings, capabilities) || entitled < rate {
|
||||
rate = cap;
|
||||
}
|
||||
}
|
||||
snap_frame_rate(rate)
|
||||
}
|
||||
|
||||
pub fn frame_rate_choices(settings: &Value, capabilities: &Value) -> Value {
|
||||
let (width, height) = resolution(settings);
|
||||
let full_hd = is_full_hd(width, height);
|
||||
let hardware = hardware_decode_available(settings, capabilities);
|
||||
json!(
|
||||
SUPPORTED_FRAME_RATES
|
||||
.into_iter()
|
||||
.map(|value| {
|
||||
let reason = if value <= BASE_FRAME_RATE_CEILING {
|
||||
None
|
||||
} else if !full_hd {
|
||||
Some(
|
||||
"360 FPS is offered at full HD (1920x1080 or 1920x1200) only. Choose a full HD resolution first."
|
||||
.to_owned(),
|
||||
)
|
||||
} else if capabilities["videoBackends"].as_array().is_none() {
|
||||
Some(
|
||||
"360 FPS needs a confirmed hardware video decoder for the selected codec. This device has not reported one."
|
||||
.to_owned(),
|
||||
)
|
||||
} else if !hardware {
|
||||
Some(
|
||||
"360 FPS needs a hardware video decoder for the selected codec. This device has none available."
|
||||
.to_owned(),
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
json!({"value":value, "disabled":reason.is_some(), "reason":reason})
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn capabilities(backend: &str, codec: &str) -> Value {
|
||||
json!({"protocolVersion":7, "videoBackends":[{"backend":backend, "platform":"linux",
|
||||
"available":true, "codecs":[{"codec":codec, "available":true,
|
||||
"colorQualities":["8bit_420"]}]}]})
|
||||
}
|
||||
|
||||
fn descriptor(choices: &Value, value: i64) -> Value {
|
||||
choices
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.find(|choice| choice["value"] == value)
|
||||
.cloned()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn supported_frame_rates_end_at_the_documented_top_tier() {
|
||||
assert_eq!(SUPPORTED_FRAME_RATES, [30, 60, 90, 120, 144, 165, 240, 360]);
|
||||
assert_eq!(SUPPORTED_FRAME_RATES.last(), Some(&360));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frame_rates_snap_to_supported_modes_before_requesting_allocation() {
|
||||
for (requested, expected) in [
|
||||
(0, 30),
|
||||
(58, 30),
|
||||
(59, 60),
|
||||
(89, 60),
|
||||
(90, 90),
|
||||
(119, 90),
|
||||
(120, 120),
|
||||
(143, 120),
|
||||
(144, 144),
|
||||
(164, 144),
|
||||
(165, 165),
|
||||
(239, 165),
|
||||
(240, 240),
|
||||
(359, 240),
|
||||
(360, 360),
|
||||
(999, 360),
|
||||
] {
|
||||
assert_eq!(snap_frame_rate(requested), expected);
|
||||
}
|
||||
assert_eq!(
|
||||
request_frame_rate(&json!({"fps":164,"codec":"h265"}), &json!({}), 1920, 1080),
|
||||
144
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolution_ceiling_is_documented_full_hd_only() {
|
||||
assert_eq!(resolution_ceiling(1920, 1080), 360);
|
||||
assert_eq!(resolution_ceiling(1920, 1200), 360);
|
||||
for (width, height) in [
|
||||
(1280, 720),
|
||||
(1600, 900),
|
||||
(1920, 1440),
|
||||
(2560, 1440),
|
||||
(2560, 1600),
|
||||
(3440, 1440),
|
||||
(3840, 2160),
|
||||
(5120, 1440),
|
||||
(3840, 1080),
|
||||
(2560, 1080),
|
||||
] {
|
||||
assert_eq!(
|
||||
resolution_ceiling(width, height),
|
||||
240,
|
||||
"{width}x{height} must not request the full-HD-only tier"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_the_top_tier_is_conditional_and_every_rate_reports_a_reason() {
|
||||
let settings =
|
||||
json!({"resolution":"1920x1080", "codec":"h265", "nativeVideoBackend":"auto"});
|
||||
let choices = frame_rate_choices(&settings, &capabilities("vaapi", "h265"));
|
||||
assert_eq!(
|
||||
choices.as_array().unwrap().len(),
|
||||
SUPPORTED_FRAME_RATES.len()
|
||||
);
|
||||
for rate in [30, 60, 90, 120, 144, 165, 240] {
|
||||
let entry = descriptor(&choices, rate);
|
||||
assert_eq!(entry["disabled"], false, "{rate} stays selectable");
|
||||
assert_eq!(entry["reason"], Value::Null, "{rate} needs no reason");
|
||||
}
|
||||
assert_eq!(descriptor(&choices, 360)["disabled"], false);
|
||||
|
||||
let non_full_hd = frame_rate_choices(
|
||||
&json!({"resolution":"2560x1440", "codec":"h265", "nativeVideoBackend":"auto"}),
|
||||
&capabilities("vaapi", "h265"),
|
||||
);
|
||||
let entry = descriptor(&non_full_hd, 360);
|
||||
assert_eq!(entry["disabled"], true);
|
||||
assert!(
|
||||
entry["reason"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.contains("full HD (1920x1080 or 1920x1200) only")
|
||||
);
|
||||
assert_eq!(descriptor(&non_full_hd, 240)["disabled"], false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn top_tier_requires_a_hardware_decoder_for_the_selected_codec() {
|
||||
let full_hd = json!({"resolution":"1920x1200", "codec":"av1", "nativeVideoBackend":"auto"});
|
||||
let software_only = json!({"videoBackends":[{"backend":"software", "available":true,
|
||||
"codecs":[{"codec":"av1", "available":true}]}]});
|
||||
let entry = descriptor(&frame_rate_choices(&full_hd, &software_only), 360);
|
||||
assert_eq!(entry["disabled"], true);
|
||||
assert!(
|
||||
entry["reason"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.contains("needs a hardware video decoder")
|
||||
);
|
||||
|
||||
let mismatched_codec = capabilities("vaapi", "h265");
|
||||
assert_eq!(
|
||||
descriptor(&frame_rate_choices(&full_hd, &mismatched_codec), 360)["disabled"],
|
||||
true,
|
||||
"an unrelated hardware decoder must not unlock the top tier"
|
||||
);
|
||||
|
||||
let explicit_backend =
|
||||
json!({"resolution":"1920x1200", "codec":"h264", "nativeVideoBackend":"cuda"});
|
||||
assert_eq!(
|
||||
descriptor(
|
||||
&frame_rate_choices(&explicit_backend, &capabilities("vaapi", "h264")),
|
||||
360
|
||||
)["disabled"],
|
||||
true,
|
||||
"a backend the preference excludes cannot unlock the top tier"
|
||||
);
|
||||
let nvdec_alias =
|
||||
json!({"resolution":"1920x1200", "codec":"av1", "nativeVideoBackend":"nvdec"});
|
||||
assert_eq!(
|
||||
descriptor(
|
||||
&frame_rate_choices(&nvdec_alias, &capabilities("cuda", "av1")),
|
||||
360
|
||||
)["disabled"],
|
||||
false,
|
||||
"the nvdec alias still matches the CUDA backend"
|
||||
);
|
||||
|
||||
let unavailable = json!({"videoBackends":[{"backend":"vaapi", "available":false,
|
||||
"codecs":[{"codec":"h265", "available":true}]}]});
|
||||
assert_eq!(
|
||||
descriptor(&frame_rate_choices(&full_hd, &unavailable), 360)["disabled"],
|
||||
true,
|
||||
"an unavailable backend cannot unlock the top tier"
|
||||
);
|
||||
assert_eq!(
|
||||
descriptor(&frame_rate_choices(&full_hd, &json!({})), 360)["disabled"],
|
||||
true,
|
||||
"unreported capabilities never unlock the top tier"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn request_rate_applies_the_resolution_device_and_entitlement_ceiling() {
|
||||
let hardware = capabilities("vaapi", "h265");
|
||||
let software = json!({"videoBackends":[{"backend":"software", "available":true,
|
||||
"codecs":[{"codec":"h265", "available":true}]}]});
|
||||
let settings = json!({"resolution":"1920x1080", "fps":360, "codec":"h265",
|
||||
"nativeVideoBackend":"auto"});
|
||||
let request = |capabilities: &Value, entitled: i64, width: i64, height: i64| {
|
||||
request_frame_rate(
|
||||
&settings,
|
||||
&json!({"runtimeCapabilities":capabilities, "maxEntitledFps":entitled}),
|
||||
width,
|
||||
height,
|
||||
)
|
||||
};
|
||||
assert_eq!(request(&hardware, 360, 1920, 1080), 360);
|
||||
assert_eq!(
|
||||
request(&software, 360, 1920, 1080),
|
||||
240,
|
||||
"a reported software-only probe cannot request the top tier"
|
||||
);
|
||||
assert_eq!(
|
||||
request(&json!({}), 360, 1920, 1080),
|
||||
240,
|
||||
"an unreported probe is not affirmative capability"
|
||||
);
|
||||
assert_eq!(
|
||||
request(&json!({"videoBackends":[]}), 360, 1920, 1080),
|
||||
240,
|
||||
"a probe that reported no backend cannot request the top tier"
|
||||
);
|
||||
assert_eq!(
|
||||
request(&hardware, 0, 1920, 1080),
|
||||
240,
|
||||
"unconfirmed entitlement cannot request the top tier"
|
||||
);
|
||||
assert_eq!(
|
||||
request(&hardware, 240, 1920, 1080),
|
||||
240,
|
||||
"a 240 FPS entitlement cannot request the top tier"
|
||||
);
|
||||
assert_eq!(
|
||||
request(&hardware, 120, 1920, 1080),
|
||||
120,
|
||||
"a lower entitlement bounds the request"
|
||||
);
|
||||
assert_eq!(
|
||||
request(&hardware, 360, 2560, 1440),
|
||||
240,
|
||||
"the resolution ceiling applies regardless of entitlement"
|
||||
);
|
||||
assert_eq!(
|
||||
request_frame_rate(
|
||||
&json!({"resolution":"1920x1080", "fps":999, "codec":"h265"}),
|
||||
&json!({"runtimeCapabilities":hardware, "maxEntitledFps":360}),
|
||||
1920,
|
||||
1080
|
||||
),
|
||||
360,
|
||||
"a stored value above the ceiling is still bounded"
|
||||
);
|
||||
assert_eq!(
|
||||
request_frame_rate(
|
||||
&json!({"resolution":"1920x1080", "fps":120, "codec":"h265"}),
|
||||
&json!({"runtimeCapabilities":software, "maxEntitledFps":0}),
|
||||
1920,
|
||||
1080
|
||||
),
|
||||
120,
|
||||
"base rates ignore the capability and entitlement verdicts"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_software_preference_never_qualifies_for_the_top_tier() {
|
||||
let hardware = capabilities("vaapi", "h264");
|
||||
let settings = json!({"resolution":"1920x1080", "fps":360, "codec":"h264",
|
||||
"nativeVideoBackend":"auto", "decoderPreference":"software"});
|
||||
let params = json!({"runtimeCapabilities":hardware, "maxEntitledFps":360});
|
||||
assert_eq!(
|
||||
request_frame_rate(&settings, ¶ms, 1920, 1080),
|
||||
240,
|
||||
"the legacy software preference is a software decode path"
|
||||
);
|
||||
assert_eq!(
|
||||
request_frame_rate(
|
||||
&json!({"resolution":"1920x1080", "fps":360, "codec":"h264",
|
||||
"nativeVideoBackend":"auto", "decoderPreference":"auto"}),
|
||||
¶ms,
|
||||
1920,
|
||||
1080
|
||||
),
|
||||
360,
|
||||
"the same hardware profile stays eligible without the software preference"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn auto_selection_accepts_any_hardware_codec_without_mutating_inputs() {
|
||||
let settings =
|
||||
json!({"resolution":"1920x1080", "codec":"auto", "nativeVideoBackend":"auto"});
|
||||
let original = settings.clone();
|
||||
let choices = frame_rate_choices(&settings, &capabilities("vulkan", "h265"));
|
||||
assert_eq!(descriptor(&choices, 360)["disabled"], false);
|
||||
assert_eq!(settings, original);
|
||||
|
||||
let missing = frame_rate_choices(&settings, &json!({}));
|
||||
assert_eq!(descriptor(&missing, 240)["disabled"], false);
|
||||
assert_eq!(descriptor(&missing, 360)["disabled"], true);
|
||||
}
|
||||
}
|
||||
+5252
File diff suppressed because it is too large.
Load diff
+1216
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,607 @@
|
||||
use super::*;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Serialize, PartialEq, Eq)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub(super) enum LaunchStatus {
|
||||
Ready,
|
||||
OwnershipRequired,
|
||||
SelectionRequired,
|
||||
LinkRequired,
|
||||
SubscriptionRequired,
|
||||
Patching,
|
||||
Maintenance,
|
||||
Unavailable,
|
||||
MetadataUnconfirmed,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub(super) struct LaunchDecision {
|
||||
pub(super) status: LaunchStatus,
|
||||
pub(super) message: &'static str,
|
||||
}
|
||||
|
||||
pub(super) enum PlayabilityMetadata {
|
||||
Required,
|
||||
WhenPresent,
|
||||
}
|
||||
|
||||
pub(super) fn selected_variant<'a>(game: &'a Value, variant_id: &str) -> Option<&'a Value> {
|
||||
game["variants"]
|
||||
.as_array()?
|
||||
.iter()
|
||||
.find(|variant| variant["id"] == variant_id)
|
||||
}
|
||||
|
||||
pub(super) fn readiness_decision(
|
||||
game: &Value,
|
||||
variant: &Value,
|
||||
playability: PlayabilityMetadata,
|
||||
) -> Option<LaunchDecision> {
|
||||
use LaunchStatus::*;
|
||||
let decide = |status, message| LaunchDecision { status, message };
|
||||
match game["playabilityState"].as_str() {
|
||||
Some("PLAYABLE") => (),
|
||||
Some("UNPLAYABLE_DUE_TO_UPGRADE" | "UNPLAYABLE_DUE_TO_TIME_CAPPED_LIMIT") => {
|
||||
return Some(decide(
|
||||
SubscriptionRequired,
|
||||
"Your GeForce NOW membership does not currently allow this game. Check your membership and available playtime.",
|
||||
));
|
||||
}
|
||||
Some("UNKNOWN") => {
|
||||
return Some(decide(
|
||||
MetadataUnconfirmed,
|
||||
"Game availability could not be confirmed. Refresh and try again.",
|
||||
));
|
||||
}
|
||||
None if matches!(playability, PlayabilityMetadata::Required) => {
|
||||
return Some(decide(
|
||||
MetadataUnconfirmed,
|
||||
"Game availability could not be confirmed. Refresh and try again.",
|
||||
));
|
||||
}
|
||||
None => (),
|
||||
_ => {
|
||||
return Some(decide(
|
||||
Unavailable,
|
||||
"This game is currently unavailable on GeForce NOW.",
|
||||
));
|
||||
}
|
||||
}
|
||||
match variant["stateDetails"]["__typename"].as_str() {
|
||||
Some("VariantGfnAutoPatchingMetadata" | "VariantGfnManualPatchingMetadata") => {
|
||||
return Some(decide(
|
||||
Patching,
|
||||
"This store version is being patched. Try again after the patch finishes.",
|
||||
));
|
||||
}
|
||||
Some("VariantGfnMaintenanceMetadata") => {
|
||||
return Some(decide(
|
||||
Maintenance,
|
||||
"This store version is under maintenance.",
|
||||
));
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
match variant["gfnStatus"].as_str() {
|
||||
Some("AVAILABLE") => (),
|
||||
Some("PATCHING") => {
|
||||
return Some(decide(
|
||||
Patching,
|
||||
"This store version is being patched. Try again later.",
|
||||
));
|
||||
}
|
||||
Some("SERVER_MAINTENANCE") => {
|
||||
return Some(decide(
|
||||
Maintenance,
|
||||
"This store version is under maintenance.",
|
||||
));
|
||||
}
|
||||
None | Some("UNKNOWN") => {
|
||||
return Some(decide(
|
||||
MetadataUnconfirmed,
|
||||
"Store version readiness could not be confirmed. Refresh and try again.",
|
||||
));
|
||||
}
|
||||
_ => {
|
||||
return Some(decide(
|
||||
Unavailable,
|
||||
"This store version is currently unavailable.",
|
||||
));
|
||||
}
|
||||
}
|
||||
if variant["playStatus"] == "NOT_PLAYABLE" {
|
||||
return Some(decide(
|
||||
Unavailable,
|
||||
"This store version cannot currently be played. Check your store account, subscription, and library sync.",
|
||||
));
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub(super) fn account_decision(
|
||||
variant: &Value,
|
||||
variant_id: &str,
|
||||
access: &Value,
|
||||
) -> Option<LaunchDecision> {
|
||||
use LaunchStatus::*;
|
||||
let decide = |status, message| LaunchDecision { status, message };
|
||||
if access["definitions"]["stores"]["status"] != "success" {
|
||||
return Some(decide(
|
||||
MetadataUnconfirmed,
|
||||
"Store account requirements could not be confirmed. Refresh and try again.",
|
||||
));
|
||||
}
|
||||
let store =
|
||||
crate::catalog_types::normalize_store(variant["store"].as_str().unwrap_or_default());
|
||||
let Some(account) = access["accounts"]
|
||||
.as_array()
|
||||
.and_then(|accounts| accounts.iter().find(|account| account["provider"] == store))
|
||||
else {
|
||||
return Some(decide(
|
||||
MetadataUnconfirmed,
|
||||
"The selected store's account requirements are unavailable. Refresh and try again.",
|
||||
));
|
||||
};
|
||||
let linking = &account["accountLinkingMetadata"];
|
||||
let applies = linking["supportedVariantIds"]
|
||||
.as_array()
|
||||
.is_none_or(|ids| ids.is_empty() || ids.iter().any(|id| id == variant_id));
|
||||
if applies
|
||||
&& account["isRequired"] == true
|
||||
&& (account["isConnected"] != true || account["status"] == "expired")
|
||||
{
|
||||
return Some(decide(
|
||||
LinkRequired,
|
||||
"Link or reconnect the selected store account in Settings before launching this version.",
|
||||
));
|
||||
}
|
||||
if applies && account["supportsLinking"] == true && !account["isRequired"].is_boolean() {
|
||||
return Some(decide(
|
||||
MetadataUnconfirmed,
|
||||
"The selected store's linking requirement could not be confirmed.",
|
||||
));
|
||||
}
|
||||
if let Some(id) = variant["subscription"].as_str().filter(|id| !id.is_empty()) {
|
||||
if !access["subscriptions"]
|
||||
.as_array()
|
||||
.is_some_and(|subscriptions| {
|
||||
subscriptions
|
||||
.iter()
|
||||
.any(|subscription| subscription["id"] == id)
|
||||
})
|
||||
{
|
||||
return Some(decide(
|
||||
SubscriptionRequired,
|
||||
"The store subscription recorded for this version is not active. Check your store account and sync your library.",
|
||||
));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub(super) fn requires_paid_membership(game: &Value) -> bool {
|
||||
game["membershipTierLabel"].as_str().is_some_and(|tier| {
|
||||
!tier.trim().is_empty() && !tier.trim().to_ascii_lowercase().starts_with("free")
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn paid_membership_decision(subscription: &Value) -> Option<LaunchDecision> {
|
||||
use LaunchStatus::*;
|
||||
let decide = |status, message| LaunchDecision { status, message };
|
||||
match subscription["membershipTier"]
|
||||
.as_str()
|
||||
.filter(|tier| !tier.trim().is_empty())
|
||||
{
|
||||
None => Some(decide(
|
||||
MetadataUnconfirmed,
|
||||
"Membership details could not be confirmed. Refresh and try again.",
|
||||
)),
|
||||
Some(tier) if tier.trim().to_ascii_lowercase().starts_with("free") => Some(decide(
|
||||
SubscriptionRequired,
|
||||
"This game requires a paid GeForce NOW membership.",
|
||||
)),
|
||||
_ => game_play_denial(subscription),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn game_play_denial(subscription: &Value) -> Option<LaunchDecision> {
|
||||
(subscription["isGamePlayAllowed"] == false).then_some(LaunchDecision {
|
||||
status: LaunchStatus::SubscriptionRequired,
|
||||
message: "Your membership does not currently allow gameplay. Check your available playtime.",
|
||||
})
|
||||
}
|
||||
|
||||
fn launch_decision(
|
||||
game: &Value,
|
||||
app_id: &str,
|
||||
variant_id: &str,
|
||||
access: &Value,
|
||||
membership: impl FnOnce() -> Result<Value, ServiceError>,
|
||||
) -> Result<LaunchDecision, ServiceError> {
|
||||
use LaunchStatus::*;
|
||||
let decide = |status, message| LaunchDecision { status, message };
|
||||
if game["id"] != app_id || variant_id.parse::<i32>().ok().is_none_or(|id| id <= 0) {
|
||||
return Ok(decide(
|
||||
MetadataUnconfirmed,
|
||||
"The exact store version could not be confirmed. Refresh and try again.",
|
||||
));
|
||||
}
|
||||
let Some(variant) = selected_variant(game, variant_id) else {
|
||||
return Ok(decide(
|
||||
MetadataUnconfirmed,
|
||||
"The selected store version is no longer available. Choose a store version again.",
|
||||
));
|
||||
};
|
||||
if let Some(decision) = readiness_decision(game, variant, PlayabilityMetadata::Required) {
|
||||
return Ok(decision);
|
||||
}
|
||||
match variant["libraryStatus"].as_str() {
|
||||
Some("MANUAL" | "PLATFORM_SYNC") => (),
|
||||
Some("NOT_OWNED") => {
|
||||
return Ok(decide(
|
||||
OwnershipRequired,
|
||||
"Confirm that you already own this game on the selected store before adding it to your GeForce NOW library.",
|
||||
));
|
||||
}
|
||||
_ => {
|
||||
return Ok(decide(
|
||||
MetadataUnconfirmed,
|
||||
"Ownership of this store version could not be confirmed. Refresh or sync your library.",
|
||||
));
|
||||
}
|
||||
}
|
||||
if let Some(decision) = account_decision(variant, variant_id, access) {
|
||||
return Ok(decision);
|
||||
}
|
||||
if requires_paid_membership(game)
|
||||
&& let Some(decision) = paid_membership_decision(&membership()?)
|
||||
{
|
||||
return Ok(decision);
|
||||
}
|
||||
if variant["librarySelected"] == false {
|
||||
return Ok(decide(
|
||||
SelectionRequired,
|
||||
"Choose this owned store version for play before launching.",
|
||||
));
|
||||
}
|
||||
if variant["librarySelected"] != true {
|
||||
return Ok(decide(
|
||||
MetadataUnconfirmed,
|
||||
"Your preferred store version could not be confirmed. Refresh and try again.",
|
||||
));
|
||||
}
|
||||
Ok(decide(Ready, "Ready to play this store version."))
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum Mutation {
|
||||
AddFavorite,
|
||||
RemoveFavorite,
|
||||
AddOwned,
|
||||
RemoveOwned,
|
||||
SelectOwned,
|
||||
}
|
||||
|
||||
impl Mutation {
|
||||
fn parse(method: &str) -> Result<Self, ServiceError> {
|
||||
match method {
|
||||
"catalog.favorites.add" => Ok(Self::AddFavorite),
|
||||
"catalog.favorites.remove" => Ok(Self::RemoveFavorite),
|
||||
"catalog.ownership.add" => Ok(Self::AddOwned),
|
||||
"catalog.ownership.remove" => Ok(Self::RemoveOwned),
|
||||
"catalog.ownership.select" => Ok(Self::SelectOwned),
|
||||
_ => Err(ServiceError::invalid("Unknown catalog mutation")),
|
||||
}
|
||||
}
|
||||
|
||||
fn root(self) -> &'static str {
|
||||
match self {
|
||||
Self::AddFavorite => "addFavoriteApp",
|
||||
Self::RemoveFavorite => "removeFavoriteApp",
|
||||
Self::AddOwned => "addOwnedVariant",
|
||||
Self::RemoveOwned => "removeOwnedVariant",
|
||||
Self::SelectOwned => "selectOwnedVariant",
|
||||
}
|
||||
}
|
||||
|
||||
fn favorite(self) -> bool {
|
||||
matches!(self, Self::AddFavorite | Self::RemoveFavorite)
|
||||
}
|
||||
|
||||
fn confirmed(self, game: &Value, variant_id: &str) -> bool {
|
||||
let variant = selected_variant(game, variant_id);
|
||||
match self {
|
||||
Self::AddFavorite => game["favorited"] == true,
|
||||
Self::RemoveFavorite => game["favorited"] == false,
|
||||
Self::AddOwned => variant.is_some_and(|variant| {
|
||||
matches!(
|
||||
variant["libraryStatus"].as_str(),
|
||||
Some("MANUAL" | "PLATFORM_SYNC")
|
||||
)
|
||||
}),
|
||||
Self::RemoveOwned => {
|
||||
variant.is_some_and(|variant| variant["libraryStatus"] == "NOT_OWNED")
|
||||
}
|
||||
Self::SelectOwned => variant.is_some_and(|variant| {
|
||||
variant["librarySelected"] == true
|
||||
&& matches!(
|
||||
variant["libraryStatus"].as_str(),
|
||||
Some("MANUAL" | "PLATFORM_SYNC")
|
||||
)
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Eq, Hash, PartialEq)]
|
||||
pub(super) struct CatalogActionKey {
|
||||
provider_id: String,
|
||||
user_id: String,
|
||||
app_id: String,
|
||||
}
|
||||
|
||||
pub(super) struct MutationPermit<'a> {
|
||||
pending: &'a Mutex<std::collections::HashSet<CatalogActionKey>>,
|
||||
key: CatalogActionKey,
|
||||
}
|
||||
|
||||
impl Drop for MutationPermit<'_> {
|
||||
fn drop(&mut self) {
|
||||
self.pending
|
||||
.lock()
|
||||
.expect("catalog mutations poisoned")
|
||||
.remove(&self.key);
|
||||
}
|
||||
}
|
||||
|
||||
impl GfnService {
|
||||
pub(super) fn admit_catalog_action(
|
||||
&self,
|
||||
session: &AuthSession,
|
||||
app_id: &str,
|
||||
) -> Result<MutationPermit<'_>, ServiceError> {
|
||||
let key = CatalogActionKey {
|
||||
provider_id: session.provider.idp_id.clone(),
|
||||
user_id: session.user.user_id.clone(),
|
||||
app_id: app_id.to_owned(),
|
||||
};
|
||||
let mut pending = crate::store_requests::lock(&self.catalog_mutations)?;
|
||||
if !pending.insert(key.clone()) {
|
||||
return Err(ServiceError {
|
||||
code: "catalog_mutation_busy",
|
||||
message: "An update or launch for this game is already in progress.".into(),
|
||||
});
|
||||
}
|
||||
Ok(MutationPermit {
|
||||
pending: &self.catalog_mutations,
|
||||
key,
|
||||
})
|
||||
}
|
||||
pub fn catalog_launch_inspect(
|
||||
&self,
|
||||
params: &Value,
|
||||
settings: &Value,
|
||||
) -> Result<Value, ServiceError> {
|
||||
let app_id = bounded_id(params, "appId", true)?;
|
||||
let variant_id = bounded_id(params, "variantId", true)?;
|
||||
if super::store_launch::store_launch_intent(params)? {
|
||||
return self.store_launch_inspect(params, Some((&app_id, &variant_id)), settings);
|
||||
}
|
||||
let mut result = self.catalog_game(&json!({"appId":app_id}), settings)?;
|
||||
let access = if selected_variant(&result["game"], &variant_id).is_some_and(|variant| {
|
||||
matches!(
|
||||
variant["libraryStatus"].as_str(),
|
||||
Some("MANUAL" | "PLATFORM_SYNC")
|
||||
)
|
||||
}) {
|
||||
let access = self.account_connections(settings)?;
|
||||
if access["scope"] != result["scope"] {
|
||||
return Err(ServiceError {
|
||||
code: "stale_account",
|
||||
message: "The store account changed.".into(),
|
||||
});
|
||||
}
|
||||
access
|
||||
} else {
|
||||
Value::Null
|
||||
};
|
||||
let decision = launch_decision(&result["game"], &app_id, &variant_id, &access, || {
|
||||
let subscription = self.subscription(settings)?;
|
||||
if subscription["scope"] != result["scope"] {
|
||||
return Err(ServiceError {
|
||||
code: "stale_account",
|
||||
message: "The membership account changed.".into(),
|
||||
});
|
||||
}
|
||||
Ok(subscription["subscription"].clone())
|
||||
})?;
|
||||
result["decision"] = serde_json::to_value(decision).expect("launch decision serializable");
|
||||
result["appId"] = json!(app_id);
|
||||
result["variantId"] = json!(variant_id);
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn catalog_mutate(
|
||||
&self,
|
||||
method: &str,
|
||||
params: &Value,
|
||||
settings: &Value,
|
||||
) -> Result<Value, ServiceError> {
|
||||
let mutation = Mutation::parse(method)?;
|
||||
let app_id = bounded_id(params, "appId", true)?;
|
||||
let variant_id = if mutation.favorite() {
|
||||
String::new()
|
||||
} else {
|
||||
bounded_id(params, "variantId", true)?
|
||||
};
|
||||
if matches!(mutation, Mutation::AddOwned) && params["confirmedExistingLicense"] != true {
|
||||
return Err(ServiceError::invalid(
|
||||
"Confirm existing ownership of the selected store license first",
|
||||
));
|
||||
}
|
||||
let (session, generation) = self.authenticated_snapshot(TokenPurpose::ServiceId, false)?;
|
||||
if params["scope"] != scoped_result(json!({}), &session, generation)["scope"] {
|
||||
return Err(ServiceError {
|
||||
code: "stale_account",
|
||||
message: "The catalog update belongs to a different account context.".into(),
|
||||
});
|
||||
}
|
||||
let _permit = self.admit_catalog_action(&session, &app_id)?;
|
||||
let before = self.catalog_game(&json!({"appId":app_id}), settings)?;
|
||||
self.check_scope(&session, generation)?;
|
||||
if !mutation.favorite() && selected_variant(&before["game"], &variant_id).is_none() {
|
||||
return Err(ServiceError::invalid(
|
||||
"The selected variant does not belong to this game",
|
||||
));
|
||||
}
|
||||
if matches!(mutation, Mutation::SelectOwned)
|
||||
&& !Mutation::AddOwned.confirmed(&before["game"], &variant_id)
|
||||
{
|
||||
return Err(ServiceError::invalid(
|
||||
"Only an owned store version can be selected for play",
|
||||
));
|
||||
}
|
||||
let root = mutation.root();
|
||||
let operation = format!("{}{}", root[..1].to_uppercase(), &root[1..]);
|
||||
let (variable, argument, id) = if mutation.favorite() {
|
||||
("appId", "appId", &app_id)
|
||||
} else {
|
||||
("cmsId", "variantId", &variant_id)
|
||||
};
|
||||
let query = format!(
|
||||
"mutation {operation}(${variable}: String!, $locale: String!) {{ {root}(language: $locale, {argument}: ${variable}) {{ app {{ id }} }} }}"
|
||||
);
|
||||
let client = client_for_settings(&self.client, settings).map_err(ServiceError::invalid)?;
|
||||
let current =
|
||||
self.authenticated_snapshot_for(&session, generation, TokenPurpose::ServiceId)?;
|
||||
let token = current
|
||||
.tokens
|
||||
.id_token
|
||||
.as_deref()
|
||||
.unwrap_or(¤t.tokens.access_token);
|
||||
self.check_scope(&session, generation)?;
|
||||
self.invalidate_catalog()?;
|
||||
let sent = self.store_cache.requests.send(
|
||||
client
|
||||
.post(&self.endpoints.graphql)
|
||||
.headers(graphql_headers(token)?)
|
||||
.json(&json!({"query":query,"variables":{variable:id,"locale":"en_US"}})),
|
||||
"Catalog update could not be confirmed",
|
||||
);
|
||||
let mut http_status = None;
|
||||
let mut graphql_errors = Vec::new();
|
||||
let error_code = match sent {
|
||||
Ok(response) => {
|
||||
http_status = Some(response.status().as_u16());
|
||||
let success = response.status().is_success();
|
||||
let mut bytes = Vec::new();
|
||||
let payload = response
|
||||
.take(64 * 1024 + 1)
|
||||
.read_to_end(&mut bytes)
|
||||
.ok()
|
||||
.filter(|_| bytes.len() <= 64 * 1024)
|
||||
.and_then(|_| serde_json::from_slice::<Value>(&bytes).ok());
|
||||
if let Some(payload) = &payload {
|
||||
for error in payload["errors"].as_array().into_iter().flatten().take(8) {
|
||||
let safe = |value: &Value| {
|
||||
value
|
||||
.as_str()
|
||||
.filter(|text| {
|
||||
text.len() <= 128
|
||||
&& text
|
||||
.chars()
|
||||
.all(|c| c.is_ascii_alphanumeric() || "_.-".contains(c))
|
||||
})
|
||||
.map(ToOwned::to_owned)
|
||||
};
|
||||
graphql_errors.push(json!({"code":safe(&error["extensions"]["code"]),"path":error["path"].as_array().map(|path| path.iter().take(8).filter_map(safe).collect::<Vec<_>>())}));
|
||||
}
|
||||
}
|
||||
if !success {
|
||||
Some(if http_status == Some(401) {
|
||||
"http_unauthorized"
|
||||
} else {
|
||||
"upstream_error"
|
||||
})
|
||||
} else if payload.as_ref().is_some_and(|payload| {
|
||||
!payload["errors"].is_null()
|
||||
&& payload["errors"]
|
||||
.as_array()
|
||||
.is_none_or(|errors| !errors.is_empty())
|
||||
}) {
|
||||
Some("graphql_error")
|
||||
} else if payload.is_none() {
|
||||
Some("invalid_upstream_response")
|
||||
} else if payload
|
||||
.as_ref()
|
||||
.is_none_or(|payload| payload["data"][root]["app"]["id"] != app_id)
|
||||
{
|
||||
Some("mutation_identity_mismatch")
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Err(error) => Some(error.code),
|
||||
};
|
||||
let invalidation = self.invalidate_catalog();
|
||||
self.check_scope(&session, generation)?;
|
||||
let fresh = self.catalog_game(&json!({"appId":app_id}), settings);
|
||||
self.check_scope(&session, generation)?;
|
||||
let confirmed = invalidation.is_ok()
|
||||
&& fresh
|
||||
.as_ref()
|
||||
.is_ok_and(|result| mutation.confirmed(&result["game"], &variant_id));
|
||||
let message = if confirmed {
|
||||
"The current cloud library state is confirmed."
|
||||
} else {
|
||||
"Could not confirm the update. Refresh this game before trying again; the update may already have reached GeForce NOW."
|
||||
};
|
||||
Ok(scoped_result(
|
||||
json!({"appId":app_id,"variantId":variant_id,"operation":method,
|
||||
"outcome":if error_code.is_none() {"acknowledged"} else {"unconfirmed"},
|
||||
"reconciliation":if confirmed {"confirmed"} else {"unconfirmed"},"message":message,
|
||||
"error":{"code":error_code,"httpStatus":http_status,"graphql":graphql_errors,
|
||||
"reconciliationCode":fresh.as_ref().err().map(|error| error.code),"invalidationCode":invalidation.as_ref().err().map(|error| error.code)},
|
||||
"game":fresh.as_ref().ok().map(|result| &result["game"]),
|
||||
"catalogRevision":self.catalog_revision.load(std::sync::atomic::Ordering::Acquire)}),
|
||||
&session,
|
||||
generation,
|
||||
))
|
||||
}
|
||||
|
||||
pub fn catalog_favorites(&self, settings: &Value) -> Result<Value, ServiceError> {
|
||||
self.authenticated_read(|session, generation| {
|
||||
let revision = self.catalog_revision.load(std::sync::atomic::Ordering::Acquire);
|
||||
let client = client_for_settings(&self.client, settings).map_err(ServiceError::invalid)?;
|
||||
let token = session.tokens.id_token.as_deref().unwrap_or(&session.tokens.access_token);
|
||||
let vpc = self.vpc_id(&client, session, generation, settings, token, Some(&self.store_cache.requests))?;
|
||||
let query = STORE_PANELS_QUERY.replace("GetStorePanels", "GetGameSection")
|
||||
.replace(" title\n items", " title\n seeMoreInfo { filterTileId title filterIds minTiles sortOrderId }\n items");
|
||||
let payload = self.catalog_document(&client, token, &query, json!({"vpcId":vpc,"locale":"en_US","panelNames":["FAVORITES"]}))?;
|
||||
let panels = payload["data"]["panels"].as_array().ok_or_else(|| ServiceError::invalid("Favorites panels were not returned"))?;
|
||||
let mut games = Vec::new();
|
||||
let mut sections = Vec::new();
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
for panel in panels {
|
||||
if panel["name"] != "FAVORITES" {
|
||||
return Err(ServiceError::invalid("The returned panel is not the requested favorites panel"));
|
||||
}
|
||||
for section in panel["sections"].as_array().ok_or_else(|| ServiceError::invalid("Favorites sections were not returned"))? {
|
||||
sections.push(json!({"id":section["id"],"title":section["title"],"seeMoreInfo":section["seeMoreInfo"]}));
|
||||
for item in section["items"].as_array().ok_or_else(|| ServiceError::invalid("Favorites items were not returned"))? {
|
||||
if item["__typename"] != "GameItem" { continue; }
|
||||
let game = app_to_game(&item["app"]).ok_or_else(|| ServiceError::invalid("A favorites game could not be parsed"))?;
|
||||
if seen.insert(game["id"].as_str().unwrap().to_owned()) { games.push(game); }
|
||||
if games.len() > 1000 { return Err(ServiceError::invalid("Favorites exceed the bounded panel budget")); }
|
||||
}
|
||||
}
|
||||
}
|
||||
self.check_scope(session, generation)?;
|
||||
if revision != self.catalog_revision.load(std::sync::atomic::Ordering::Acquire) { return Err(ServiceError {code:"stale_account",message:"Favorites changed while loading. Refresh again.".into()}); }
|
||||
crate::store_catalog_page::bounded_result(json!({"games":games,"sections":sections,"coverage":"unknown","complete":false,"fetchedAt":now_ms(),"catalogRevision":revision}))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,914 @@
|
||||
use super::*;
|
||||
use crate::gfn::tests::{auth_fixture, jwt, mock_requests, test_service};
|
||||
|
||||
fn service(url: &str) -> (GfnService, PathBuf) {
|
||||
let (mut service, path) = test_service(url);
|
||||
service.endpoints.graphql = format!("{url}/graphql");
|
||||
service.endpoints.server_info = Some(format!("{url}/vpc"));
|
||||
let mut state = service.state.lock().unwrap();
|
||||
state.session = Some(auth_fixture("fixture-account"));
|
||||
state.restore_attempted = true;
|
||||
drop(state);
|
||||
(service, path)
|
||||
}
|
||||
|
||||
fn app(status: &str, selected: bool, favorite: bool) -> Value {
|
||||
json!({"id":"parent-app","title":"Fixture game","library":{"favorited":favorite},
|
||||
"gfn":{"playabilityState":"PLAYABLE"},"variants":[
|
||||
{"id":"123","appStore":"STEAM","gfn":{"status":"AVAILABLE","library":{"status":status,"selected":selected,"playStatus":"PLAYABLE"}}},
|
||||
{"id":"456","appStore":"EPIC","gfn":{"status":"AVAILABLE","library":{"status":"NOT_OWNED","selected":false,"playStatus":"UNKNOWN"}}}
|
||||
]})
|
||||
}
|
||||
|
||||
fn read(app: Value) -> (u16, Value) {
|
||||
(200, json!({"data":{"apps":{"items":[app]}}}))
|
||||
}
|
||||
|
||||
fn vpc() -> (u16, Value) {
|
||||
(200, json!({"requestStatus":{"serverId":"fixture-vpc"}}))
|
||||
}
|
||||
|
||||
fn scoped(mut params: Value) -> Value {
|
||||
params["scope"] =
|
||||
scoped_result(json!({}), &auth_fixture("fixture-account"), 0)["scope"].clone();
|
||||
params
|
||||
}
|
||||
|
||||
fn access() -> Value {
|
||||
json!({"definitions":{"stores":{"status":"success"}},"accounts":[{"provider":"STEAM","isRequired":false,"supportsLinking":true,"isConnected":false}],"subscriptions":[]})
|
||||
}
|
||||
|
||||
fn decision(game: &Value, app_id: &str, variant_id: &str, subscription: &Value) -> LaunchDecision {
|
||||
super::launch_decision(game, app_id, variant_id, &access(), || {
|
||||
Ok(subscription.clone())
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_store_link_and_subscription_requirements_use_current_account_metadata() {
|
||||
let mut game = app_to_game(&app("MANUAL", true, false)).unwrap();
|
||||
let mut account = access();
|
||||
account["accounts"][0]["isRequired"] = json!(true);
|
||||
assert_eq!(
|
||||
super::launch_decision(&game, "parent-app", "123", &account, || Ok(Value::Null))
|
||||
.unwrap()
|
||||
.status,
|
||||
LaunchStatus::LinkRequired
|
||||
);
|
||||
account["accounts"][0]["isConnected"] = json!(true);
|
||||
assert_eq!(
|
||||
super::launch_decision(&game, "parent-app", "123", &account, || Ok(Value::Null))
|
||||
.unwrap()
|
||||
.status,
|
||||
LaunchStatus::Ready
|
||||
);
|
||||
account["accounts"][0]["status"] = json!("expired");
|
||||
assert_eq!(
|
||||
super::launch_decision(&game, "parent-app", "123", &account, || Ok(Value::Null))
|
||||
.unwrap()
|
||||
.status,
|
||||
LaunchStatus::LinkRequired
|
||||
);
|
||||
account["accounts"][0]["status"] = json!("connected");
|
||||
game["variants"][0]["subscription"] = json!("STORE_PASS");
|
||||
assert_eq!(
|
||||
super::launch_decision(&game, "parent-app", "123", &account, || Ok(Value::Null))
|
||||
.unwrap()
|
||||
.status,
|
||||
LaunchStatus::SubscriptionRequired
|
||||
);
|
||||
account["subscriptions"] = json!([{"id":"STORE_PASS"}]);
|
||||
assert_eq!(
|
||||
super::launch_decision(&game, "parent-app", "123", &account, || Ok(Value::Null))
|
||||
.unwrap()
|
||||
.status,
|
||||
LaunchStatus::Ready
|
||||
);
|
||||
account["definitions"]["stores"]["status"] = json!("stale");
|
||||
assert_eq!(
|
||||
super::launch_decision(&game, "parent-app", "123", &account, || Ok(Value::Null))
|
||||
.unwrap()
|
||||
.status,
|
||||
LaunchStatus::MetadataUnconfirmed
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nullable_variant_scope_preserves_required_account_linking() {
|
||||
let game = app_to_game(&app("PLATFORM_SYNC", true, false)).unwrap();
|
||||
for required in [false, true] {
|
||||
let definition: crate::catalog_types::StoreDefinition = serde_json::from_value(json!({
|
||||
"store":"STEAM","label":"Steam","features":[],
|
||||
"accountLinkingMetadata":{"supportedVariantIds":null,"isRequired":required}
|
||||
}))
|
||||
.unwrap();
|
||||
let mut account = access();
|
||||
account["accounts"][0] = definition.connection_definition();
|
||||
assert_eq!(
|
||||
super::launch_decision(&game, "parent-app", "123", &account, || Ok(Value::Null))
|
||||
.unwrap()
|
||||
.status,
|
||||
if required {
|
||||
LaunchStatus::LinkRequired
|
||||
} else {
|
||||
LaunchStatus::Ready
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_subscription_sentinel_is_normalized_without_bypassing_real_subscriptions() {
|
||||
for subscription in [
|
||||
Value::Null,
|
||||
json!("NONE"),
|
||||
json!("STORE_PASS"),
|
||||
json!("UNKNOWN_PASS"),
|
||||
] {
|
||||
let mut metadata = app("PLATFORM_SYNC", true, false);
|
||||
metadata["variants"][0]["gfn"]["library"]["subscription"] = subscription.clone();
|
||||
let game = app_to_game(&metadata).unwrap();
|
||||
let requires_subscription = subscription.is_string() && subscription != "NONE";
|
||||
assert_eq!(
|
||||
game["variants"][0]["subscription"],
|
||||
if requires_subscription {
|
||||
subscription
|
||||
} else {
|
||||
Value::Null
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
decision(&game, "parent-app", "123", &Value::Null).status,
|
||||
if requires_subscription {
|
||||
LaunchStatus::SubscriptionRequired
|
||||
} else {
|
||||
LaunchStatus::Ready
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exact_selected_variant_policy_never_infers_ownership_from_aggregate_or_labels() {
|
||||
let mut game = app_to_game(&app("MANUAL", true, true)).unwrap();
|
||||
for key in ["isInLibrary", "accountLinked", "favorited"] {
|
||||
game[key] = json!(true);
|
||||
}
|
||||
game["paymentModels"] = json!([{"__typename":"FreeToPlayPaymentModel"}]);
|
||||
game["variants"][1]["subscription"] = json!("GAME_PASS");
|
||||
assert_eq!(
|
||||
decision(&game, "parent-app", "123", &Value::Null).status,
|
||||
LaunchStatus::Ready
|
||||
);
|
||||
assert_eq!(
|
||||
decision(&game, "parent-app", "456", &Value::Null).status,
|
||||
LaunchStatus::OwnershipRequired
|
||||
);
|
||||
for id in ["789", "0", "2147483648", "parent-app"] {
|
||||
assert_eq!(
|
||||
decision(&game, "parent-app", id, &Value::Null).status,
|
||||
LaunchStatus::MetadataUnconfirmed
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
decision(&game, "other-app", "123", &Value::Null).status,
|
||||
LaunchStatus::MetadataUnconfirmed
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn library_status_and_missing_readiness_are_explicit() {
|
||||
for (status, expected) in [
|
||||
("MANUAL", LaunchStatus::Ready),
|
||||
("PLATFORM_SYNC", LaunchStatus::Ready),
|
||||
("NOT_OWNED", LaunchStatus::OwnershipRequired),
|
||||
("IN_LIBRARY", LaunchStatus::MetadataUnconfirmed),
|
||||
("UNKNOWN", LaunchStatus::MetadataUnconfirmed),
|
||||
] {
|
||||
let mut game = app_to_game(&app(status, true, false)).unwrap();
|
||||
for play in [Value::Null, json!("UNKNOWN"), json!("PLAYABLE")] {
|
||||
game["variants"][0]["playStatus"] = play;
|
||||
assert_eq!(
|
||||
decision(&game, "parent-app", "123", &Value::Null).status,
|
||||
expected
|
||||
);
|
||||
}
|
||||
game["variants"][0]["playStatus"] = json!("NOT_PLAYABLE");
|
||||
assert_eq!(
|
||||
decision(&game, "parent-app", "123", &Value::Null).status,
|
||||
LaunchStatus::Unavailable
|
||||
);
|
||||
}
|
||||
for key in ["libraryStatus", "gfnStatus", "librarySelected"] {
|
||||
let mut game = app_to_game(&app("MANUAL", true, false)).unwrap();
|
||||
game["variants"][0][key] = Value::Null;
|
||||
assert_eq!(
|
||||
decision(&game, "parent-app", "123", &Value::Null).status,
|
||||
LaunchStatus::MetadataUnconfirmed,
|
||||
"{key}"
|
||||
);
|
||||
}
|
||||
let game = app_to_game(&app("MANUAL", false, false)).unwrap();
|
||||
assert_eq!(
|
||||
decision(&game, "parent-app", "123", &Value::Null).status,
|
||||
LaunchStatus::SelectionRequired
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn patch_maintenance_membership_and_missing_app_metadata_block_launch() {
|
||||
let baseline = app_to_game(&app("MANUAL", true, false)).unwrap();
|
||||
for (status, expected) in [
|
||||
("PATCHING", LaunchStatus::Patching),
|
||||
("SERVER_MAINTENANCE", LaunchStatus::Maintenance),
|
||||
("UNAVAILABLE", LaunchStatus::Unavailable),
|
||||
("UNKNOWN", LaunchStatus::MetadataUnconfirmed),
|
||||
] {
|
||||
let mut game = baseline.clone();
|
||||
game["variants"][0]["gfnStatus"] = json!(status);
|
||||
assert_eq!(
|
||||
decision(&game, "parent-app", "123", &Value::Null).status,
|
||||
expected
|
||||
);
|
||||
}
|
||||
for (state, expected) in [
|
||||
(Value::Null, LaunchStatus::MetadataUnconfirmed),
|
||||
(json!("UNKNOWN"), LaunchStatus::MetadataUnconfirmed),
|
||||
(
|
||||
json!("UNPLAYABLE_DUE_TO_UPGRADE"),
|
||||
LaunchStatus::SubscriptionRequired,
|
||||
),
|
||||
(
|
||||
json!("UNPLAYABLE_DUE_TO_TIME_CAPPED_LIMIT"),
|
||||
LaunchStatus::SubscriptionRequired,
|
||||
),
|
||||
] {
|
||||
let mut game = baseline.clone();
|
||||
game["playabilityState"] = state;
|
||||
assert_eq!(
|
||||
decision(&game, "parent-app", "123", &Value::Null).status,
|
||||
expected
|
||||
);
|
||||
}
|
||||
let mut game = baseline;
|
||||
game["membershipTierLabel"] = json!("Performance");
|
||||
for (subscription, expected) in [
|
||||
(Value::Null, LaunchStatus::MetadataUnconfirmed),
|
||||
(
|
||||
json!({"membershipTier":"FREE"}),
|
||||
LaunchStatus::SubscriptionRequired,
|
||||
),
|
||||
(
|
||||
json!({"membershipTier":"ULTIMATE","isGamePlayAllowed":true}),
|
||||
LaunchStatus::Ready,
|
||||
),
|
||||
(
|
||||
json!({"membershipTier":"ULTIMATE","isGamePlayAllowed":false}),
|
||||
LaunchStatus::SubscriptionRequired,
|
||||
),
|
||||
] {
|
||||
assert_eq!(
|
||||
decision(&game, "parent-app", "123", &subscription).status,
|
||||
expected
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_mutation_uses_exact_wire_identity_and_fresh_reconciliation() {
|
||||
for (method, mutation, desired) in [
|
||||
(
|
||||
"catalog.favorites.add",
|
||||
Mutation::AddFavorite,
|
||||
app("MANUAL", true, true),
|
||||
),
|
||||
(
|
||||
"catalog.favorites.remove",
|
||||
Mutation::RemoveFavorite,
|
||||
app("MANUAL", true, false),
|
||||
),
|
||||
(
|
||||
"catalog.ownership.add",
|
||||
Mutation::AddOwned,
|
||||
app("MANUAL", true, false),
|
||||
),
|
||||
(
|
||||
"catalog.ownership.remove",
|
||||
Mutation::RemoveOwned,
|
||||
app("NOT_OWNED", false, false),
|
||||
),
|
||||
(
|
||||
"catalog.ownership.select",
|
||||
Mutation::SelectOwned,
|
||||
app("MANUAL", true, false),
|
||||
),
|
||||
] {
|
||||
let root = mutation.root();
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
read(app("MANUAL", false, false)),
|
||||
(200, json!({"data":{root:{"app":{"id":"parent-app"}}}})),
|
||||
read(desired),
|
||||
],
|
||||
move |index, request| {
|
||||
if index != 2 {
|
||||
return;
|
||||
}
|
||||
let body: Value =
|
||||
serde_json::from_str(request.split("\r\n\r\n").nth(1).unwrap()).unwrap();
|
||||
let query = body["query"].as_str().unwrap();
|
||||
assert!(query.starts_with("mutation "));
|
||||
assert!(query.contains(&format!("{root}(language: $locale,")));
|
||||
assert_eq!(body["variables"]["locale"], "en_US");
|
||||
assert_eq!(
|
||||
body["variables"][if mutation.favorite() {
|
||||
"appId"
|
||||
} else {
|
||||
"cmsId"
|
||||
}],
|
||||
if mutation.favorite() {
|
||||
"parent-app"
|
||||
} else {
|
||||
"123"
|
||||
}
|
||||
);
|
||||
assert_eq!(body["variables"].as_object().unwrap().len(), 2);
|
||||
},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service
|
||||
.catalog_mutate(
|
||||
method,
|
||||
&scoped(
|
||||
json!({"appId":"parent-app","variantId":"123","confirmedExistingLicense":true}),
|
||||
),
|
||||
&json!({}),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(result["outcome"], "acknowledged");
|
||||
assert_eq!(result["reconciliation"], "confirmed");
|
||||
assert_eq!(result["catalogRevision"], 2);
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ambiguous_or_invalid_acknowledgements_are_never_replayed_or_mislabeled() {
|
||||
for (status, payload) in [
|
||||
(401, json!({})),
|
||||
(404, json!({})),
|
||||
(503, json!({})),
|
||||
(
|
||||
200,
|
||||
json!({"data":{"addFavoriteApp":{"app":{"id":"other-app"}}}}),
|
||||
),
|
||||
(200, json!({"data":{"addFavoriteApp":null}})),
|
||||
(
|
||||
200,
|
||||
json!({"data":{"addFavoriteApp":{"app":{"id":"parent-app"}}},"errors":{}}),
|
||||
),
|
||||
(
|
||||
200,
|
||||
json!({"errors":[{"message":"not public","extensions":{"code":"DENIED"},"path":["addFavoriteApp"]}]}),
|
||||
),
|
||||
] {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
read(app("MANUAL", true, false)),
|
||||
(status, payload),
|
||||
read(app("MANUAL", true, true)),
|
||||
],
|
||||
|_, _| {},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service
|
||||
.catalog_mutate(
|
||||
"catalog.favorites.add",
|
||||
&scoped(json!({"appId":"parent-app"})),
|
||||
&json!({}),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(result["outcome"], "unconfirmed");
|
||||
assert_eq!(result["reconciliation"], "confirmed");
|
||||
assert_eq!(result["error"]["httpStatus"], status);
|
||||
assert!(result["error"]["code"].is_string());
|
||||
assert!(!result.to_string().contains("not public"));
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn acknowledgement_without_convergence_or_failed_read_remains_unconfirmed() {
|
||||
for last in [read(app("MANUAL", true, false)), (503, json!({}))] {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
read(app("MANUAL", true, false)),
|
||||
(
|
||||
200,
|
||||
json!({"data":{"addFavoriteApp":{"app":{"id":"parent-app"}}}}),
|
||||
),
|
||||
last,
|
||||
],
|
||||
|_, _| {},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service
|
||||
.catalog_mutate(
|
||||
"catalog.favorites.add",
|
||||
&scoped(json!({"appId":"parent-app"})),
|
||||
&json!({}),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(result["outcome"], "acknowledged");
|
||||
assert_eq!(result["reconciliation"], "unconfirmed");
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ownership_requires_explicit_confirmation_and_parent_variant_match() {
|
||||
let (url, worker) = mock_requests(vec![vpc(), read(app("MANUAL", true, false))], |_, _| {});
|
||||
let (service, path) = service(&url);
|
||||
assert!(
|
||||
service
|
||||
.catalog_mutate(
|
||||
"catalog.ownership.add",
|
||||
&scoped(json!({"appId":"parent-app","variantId":"123"})),
|
||||
&json!({})
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
service
|
||||
.catalog_mutate(
|
||||
"catalog.ownership.add",
|
||||
&scoped(
|
||||
json!({"appId":"parent-app","variantId":"999","confirmedExistingLicense":true})
|
||||
),
|
||||
&json!({})
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(
|
||||
service
|
||||
.catalog_revision
|
||||
.load(std::sync::atomic::Ordering::Acquire),
|
||||
0
|
||||
);
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn favorites_preserve_more_than_twenty_four_and_unknown_coverage() {
|
||||
let items: Vec<_> = (0..75)
|
||||
.map(|index| {
|
||||
let mut game = app("MANUAL", true, true);
|
||||
game["id"] = json!(format!("parent-{index}"));
|
||||
json!({"__typename":"GameItem","app":game})
|
||||
})
|
||||
.collect();
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
(
|
||||
200,
|
||||
json!({"data":{"panels":[{"id":"favorites","name":"FAVORITES","sections":[{"id":"section","seeMoreInfo":{"filterIds":["favorites"]},"items":items}]}]}}),
|
||||
),
|
||||
],
|
||||
|index, request| {
|
||||
if index == 1 {
|
||||
let payload: Value =
|
||||
serde_json::from_str(request.split("\r\n\r\n").nth(1).unwrap()).unwrap();
|
||||
assert_eq!(payload["variables"]["panelNames"], json!(["FAVORITES"]));
|
||||
assert!(payload["variables"].get("cursor").is_none());
|
||||
assert!(payload["query"].as_str().unwrap().contains("seeMoreInfo"));
|
||||
}
|
||||
},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service.catalog_favorites(&json!({})).unwrap();
|
||||
assert_eq!(result["games"].as_array().unwrap().len(), 75);
|
||||
assert_eq!(result["coverage"], "unknown");
|
||||
assert_eq!(result["complete"], false);
|
||||
assert_eq!(
|
||||
result["sections"][0]["seeMoreInfo"]["filterIds"],
|
||||
json!(["favorites"])
|
||||
);
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fresh_create_rejects_unowned_variant_before_allocation() {
|
||||
let (url, worker) = mock_requests(vec![vpc(), read(app("MANUAL", true, false))], |_, _| {});
|
||||
let (service, path) = service(&url);
|
||||
let result = service.create_session(
|
||||
&scoped(json!({"catalogAppId":"parent-app","variantId":"456","appId":"456"})),
|
||||
&json!({}),
|
||||
|settings| Ok(settings.clone()),
|
||||
);
|
||||
assert_eq!(result.unwrap_err().code, "launch_not_ready");
|
||||
assert!(service.cloudmatch.active()["session"].is_null());
|
||||
assert!(service.cloudmatch.admit_create().is_ok());
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_rejects_mismatched_launch_identity_without_network() {
|
||||
let (service, path) = service("http://127.0.0.1:1");
|
||||
assert!(
|
||||
service
|
||||
.create_session(
|
||||
&json!({"catalogAppId":"parent-app","variantId":"456","appId":"123"}),
|
||||
&json!({}),
|
||||
|settings| Ok(settings.clone())
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
assert!(service.cloudmatch.admit_create().is_ok());
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn old_account_mutations_are_rejected_before_network() {
|
||||
let (service, path) = service("http://127.0.0.1:1");
|
||||
let mut params = scoped(json!({"appId":"parent-app"}));
|
||||
params["scope"]["generation"] = json!(99);
|
||||
assert_eq!(
|
||||
service
|
||||
.catalog_mutate("catalog.favorites.add", ¶ms, &json!({}))
|
||||
.unwrap_err()
|
||||
.code,
|
||||
"stale_account"
|
||||
);
|
||||
assert_eq!(
|
||||
service
|
||||
.catalog_revision
|
||||
.load(std::sync::atomic::Ordering::Acquire),
|
||||
0
|
||||
);
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mutation_and_create_admission_serialize_only_the_same_account_app() {
|
||||
let (service, path) = service("http://127.0.0.1:1");
|
||||
let auth = auth_fixture("fixture-account");
|
||||
let permit = service.admit_catalog_action(&auth, "parent-app").unwrap();
|
||||
assert!(service.admit_catalog_action(&auth, "parent-app").is_err());
|
||||
assert!(service.admit_catalog_action(&auth, "different-app").is_ok());
|
||||
drop(permit);
|
||||
assert!(service.admit_catalog_action(&auth, "parent-app").is_ok());
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn account_change_during_mutation_cannot_reconcile_under_the_new_account() {
|
||||
let (entered_tx, entered_rx) = std::sync::mpsc::channel();
|
||||
let (release_tx, release_rx) = std::sync::mpsc::channel();
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
read(app("MANUAL", true, false)),
|
||||
(
|
||||
200,
|
||||
json!({"data":{"addFavoriteApp":{"app":{"id":"parent-app"}}}}),
|
||||
),
|
||||
],
|
||||
move |index, _| {
|
||||
if index == 2 {
|
||||
entered_tx.send(()).unwrap();
|
||||
release_rx.recv_timeout(Duration::from_secs(5)).unwrap();
|
||||
}
|
||||
},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
std::thread::scope(|threads| {
|
||||
let mutation = threads.spawn(|| {
|
||||
service.catalog_mutate(
|
||||
"catalog.favorites.add",
|
||||
&scoped(json!({"appId":"parent-app"})),
|
||||
&json!({}),
|
||||
)
|
||||
});
|
||||
entered_rx.recv_timeout(Duration::from_secs(5)).unwrap();
|
||||
service.state.lock().unwrap().generation += 1;
|
||||
release_tx.send(()).unwrap();
|
||||
assert_eq!(mutation.join().unwrap().unwrap_err().code, "stale_account");
|
||||
});
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
fn held_mutation_remains_admitted_after_generation_change(switch_accounts: bool) {
|
||||
let (entered_tx, entered_rx) = std::sync::mpsc::channel();
|
||||
let (release_tx, release_rx) = std::sync::mpsc::channel();
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
read(app("MANUAL", true, false)),
|
||||
(
|
||||
200,
|
||||
json!({"data":{"addFavoriteApp":{"app":{"id":"parent-app"}}}}),
|
||||
),
|
||||
],
|
||||
move |index, request| {
|
||||
if index == 2 {
|
||||
assert!(request.contains("mutation AddFavoriteApp"));
|
||||
entered_tx.send(()).unwrap();
|
||||
release_rx.recv_timeout(Duration::from_secs(5)).unwrap();
|
||||
}
|
||||
},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let owner = auth_fixture("fixture-account");
|
||||
std::thread::scope(|threads| {
|
||||
let mutation = threads.spawn(|| {
|
||||
service.catalog_mutate(
|
||||
"catalog.favorites.add",
|
||||
&scoped(json!({"appId":"parent-app"})),
|
||||
&json!({}),
|
||||
)
|
||||
});
|
||||
entered_rx.recv_timeout(Duration::from_secs(5)).unwrap();
|
||||
if switch_accounts {
|
||||
let other = auth_fixture("other-account");
|
||||
{
|
||||
let mut state = service.state.lock().unwrap();
|
||||
state.session = Some(other.clone());
|
||||
state.generation += 1;
|
||||
}
|
||||
assert!(service.admit_catalog_action(&other, "parent-app").is_ok());
|
||||
let mut state = service.state.lock().unwrap();
|
||||
state.session = Some(owner.clone());
|
||||
state.generation += 1;
|
||||
} else {
|
||||
service.clear_cache();
|
||||
let mut state = service.state.lock().unwrap();
|
||||
state.providers = vec![owner.provider.clone()];
|
||||
state.providers_expires = Some(Instant::now() + Duration::from_secs(60));
|
||||
}
|
||||
let generation = service.auth_generation();
|
||||
let blocked = service
|
||||
.admit_catalog_action(&owner, "parent-app")
|
||||
.err()
|
||||
.map(|error| error.code);
|
||||
assert!(service.admit_catalog_action(&owner, "another-app").is_ok());
|
||||
assert!(
|
||||
service
|
||||
.admit_catalog_action(&auth_fixture("another-account"), "parent-app")
|
||||
.is_ok()
|
||||
);
|
||||
let mut partner = owner.clone();
|
||||
partner.provider.idp_id = "another-provider".into();
|
||||
assert!(service.admit_catalog_action(&partner, "parent-app").is_ok());
|
||||
if blocked.is_some() {
|
||||
let scope = scoped_result(json!({}), &owner, generation)["scope"].clone();
|
||||
assert_eq!(
|
||||
service
|
||||
.catalog_mutate(
|
||||
"catalog.favorites.remove",
|
||||
&json!({"appId":"parent-app","scope":scope}),
|
||||
&json!({})
|
||||
)
|
||||
.unwrap_err()
|
||||
.code,
|
||||
"catalog_mutation_busy"
|
||||
);
|
||||
assert_eq!(service.create_session(&json!({"catalogAppId":"parent-app","variantId":"123","appId":"123","scope":scope}), &json!({}), |settings| Ok(settings.clone())).unwrap_err().code, "catalog_mutation_busy");
|
||||
}
|
||||
release_tx.send(()).unwrap();
|
||||
assert_eq!(mutation.join().unwrap().unwrap_err().code, "stale_account");
|
||||
assert_eq!(blocked, Some("catalog_mutation_busy"));
|
||||
assert!(service.admit_catalog_action(&owner, "parent-app").is_ok());
|
||||
});
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn held_mutation_blocks_same_app_after_account_switch_back() {
|
||||
held_mutation_remains_admitted_after_generation_change(true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn held_mutation_blocks_same_app_after_cache_generation_bump() {
|
||||
held_mutation_remains_admitted_after_generation_change(false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mutation_preflight_401_renews_before_exactly_one_send() {
|
||||
for mutation_status in [200, 401] {
|
||||
let old_id = jwt("fixture-account", now_ms() + 3_600_000);
|
||||
let renewed_id = jwt("fixture-account", now_ms() + 7_200_000);
|
||||
let requests = std::sync::Arc::new(Mutex::new(Vec::new()));
|
||||
let observed = requests.clone();
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
(401, json!({})),
|
||||
(
|
||||
200,
|
||||
json!({"access_token":"renewed-access","id_token":renewed_id,"expires_in":7200}),
|
||||
),
|
||||
vpc(),
|
||||
read(app("MANUAL", true, false)),
|
||||
(
|
||||
mutation_status,
|
||||
json!({"data":{"addFavoriteApp":{"app":{"id":"parent-app"}}}}),
|
||||
),
|
||||
read(app("MANUAL", true, true)),
|
||||
],
|
||||
move |_, request| observed.lock().unwrap().push(request.to_owned()),
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
{
|
||||
let mut state = service.state.lock().unwrap();
|
||||
let session = state.session.as_mut().unwrap();
|
||||
session.tokens.id_token = Some(old_id.clone());
|
||||
session.tokens.id_token_expires_at = Some(now_ms() + 3_600_000);
|
||||
}
|
||||
let result = service
|
||||
.catalog_mutate(
|
||||
"catalog.favorites.add",
|
||||
&scoped(json!({"appId":"parent-app"})),
|
||||
&json!({}),
|
||||
)
|
||||
.unwrap();
|
||||
worker.join().unwrap();
|
||||
let requests = requests.lock().unwrap();
|
||||
assert_eq!(requests.len(), 7);
|
||||
assert!(requests[0].contains(&format!("GFNJWT {old_id}")));
|
||||
assert!(requests[1].contains(&format!("GFNJWT {old_id}")));
|
||||
assert!(requests[2].starts_with("POST / "));
|
||||
let refresh: HashMap<_, _> =
|
||||
url::form_urlencoded::parse(requests[2].split("\r\n\r\n").nth(1).unwrap().as_bytes())
|
||||
.into_owned()
|
||||
.collect();
|
||||
assert_eq!(
|
||||
refresh["grant_type"],
|
||||
"urn:ietf:params:oauth:grant-type:client_token"
|
||||
);
|
||||
assert_eq!(refresh["client_id"], "test-client-id");
|
||||
assert!(requests[4].contains(&format!("GFNJWT {renewed_id}")));
|
||||
let sends: Vec<_> = requests
|
||||
.iter()
|
||||
.filter(|request| request.contains("mutation AddFavoriteApp"))
|
||||
.collect();
|
||||
assert_eq!(sends.len(), 1);
|
||||
assert!(
|
||||
sends[0].contains(&format!("GFNJWT {renewed_id}")),
|
||||
"mutation retained the preflight credential after renewal"
|
||||
);
|
||||
assert!(!sends[0].contains(&format!("GFNJWT {old_id}")));
|
||||
assert!(requests[6].contains(&format!("GFNJWT {renewed_id}")));
|
||||
assert_eq!(result["scope"]["generation"], 0);
|
||||
assert_eq!(
|
||||
result["outcome"],
|
||||
if mutation_status == 200 {
|
||||
"acknowledged"
|
||||
} else {
|
||||
"unconfirmed"
|
||||
}
|
||||
);
|
||||
assert_eq!(result["reconciliation"], "confirmed");
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn membership_requirements_preserve_all_existing_tier_cases_in_the_shared_policy() {
|
||||
for (required, membership, expected) in [
|
||||
("Premium", "FREE", LaunchStatus::SubscriptionRequired),
|
||||
(
|
||||
"Performance",
|
||||
" Free Tier ",
|
||||
LaunchStatus::SubscriptionRequired,
|
||||
),
|
||||
("Ultimate", "free-tier", LaunchStatus::SubscriptionRequired),
|
||||
("Premium", "PERFORMANCE", LaunchStatus::Ready),
|
||||
("Premium", "Priority", LaunchStatus::Ready),
|
||||
("", "FREE", LaunchStatus::Ready),
|
||||
(" ", "", LaunchStatus::Ready),
|
||||
(" FREE ", "FREE", LaunchStatus::Ready),
|
||||
("Free Tier", "", LaunchStatus::Ready),
|
||||
("free-tier", "", LaunchStatus::Ready),
|
||||
("Premium", "", LaunchStatus::MetadataUnconfirmed),
|
||||
("Premium", " ", LaunchStatus::MetadataUnconfirmed),
|
||||
] {
|
||||
let mut game = app_to_game(&app("MANUAL", true, false)).unwrap();
|
||||
game["membershipTierLabel"] = json!(required);
|
||||
assert_eq!(
|
||||
decision(
|
||||
&game,
|
||||
"parent-app",
|
||||
"123",
|
||||
&json!({"membershipTier":membership})
|
||||
)
|
||||
.status,
|
||||
expected,
|
||||
"{required}/{membership}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn premium_service(url: &str) -> (GfnService, PathBuf) {
|
||||
let (mut service, path) = service(url);
|
||||
service.endpoints.account_linking = format!("{url}/graphql");
|
||||
service.endpoints.subscription = format!("{url}/subscription");
|
||||
(service, path)
|
||||
}
|
||||
|
||||
fn premium_app(playability: &str) -> Value {
|
||||
let mut game = app("MANUAL", true, false);
|
||||
game["gfn"] = json!({"playabilityState":playability,"minimumMembershipTierLabel":"Premium"});
|
||||
game
|
||||
}
|
||||
|
||||
fn store_access() -> Vec<(u16, Value)> {
|
||||
vec![
|
||||
(
|
||||
200,
|
||||
json!({"data":{"appStoreDefinitions":[{"store":"STEAM","label":"Steam","features":[],
|
||||
"accountLinkingMetadata":{"isRequired":false,"isSupported":true,"supportedVariantIds":[]}}]}}),
|
||||
),
|
||||
(200, json!({"data":{"genreDefinitions":[]}})),
|
||||
(200, json!({"data":{"subscriptionDefinitions":[]}})),
|
||||
(
|
||||
200,
|
||||
json!({"data":{"userAccount":{"storesData":[{"store":"STEAM","isConnected":true,"status":"connected"}],"subscriptions":[]}}}),
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn account_playability_decides_premium_launches_without_the_membership_service() {
|
||||
let mut responses = vec![vpc(), read(premium_app("UNPLAYABLE_DUE_TO_UPGRADE"))];
|
||||
responses.extend(store_access());
|
||||
let (url, worker) = mock_requests(responses, |_, request| {
|
||||
assert!(
|
||||
!request.starts_with("GET /subscription"),
|
||||
"membership was requested"
|
||||
);
|
||||
});
|
||||
let (service, path) = premium_service(&url);
|
||||
let result = service
|
||||
.catalog_launch_inspect(&json!({"appId":"parent-app","variantId":"123"}), &json!({}))
|
||||
.unwrap();
|
||||
assert_eq!(result["decision"]["status"], "subscription_required");
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn playable_premium_launches_still_require_a_confirmed_membership() {
|
||||
for (membership, expected) in [
|
||||
((404, json!({})), Err("upstream_error")),
|
||||
((500, json!({})), Err("upstream_error")),
|
||||
(
|
||||
(200, json!({"membershipTier":"FREE"})),
|
||||
Ok("subscription_required"),
|
||||
),
|
||||
(
|
||||
(
|
||||
200,
|
||||
json!({"membershipTier":"ULTIMATE","currentSubscriptionState":{"isGamePlayAllowed":true}}),
|
||||
),
|
||||
Ok("ready"),
|
||||
),
|
||||
] {
|
||||
let status = membership.0;
|
||||
let mut responses = vec![vpc(), read(premium_app("PLAYABLE"))];
|
||||
responses.extend(store_access());
|
||||
responses.push(membership);
|
||||
let (url, worker) = mock_requests(responses, |index, request| {
|
||||
assert_eq!(index == 6, request.starts_with("GET /subscription?"));
|
||||
});
|
||||
let (service, path) = premium_service(&url);
|
||||
let result = service
|
||||
.catalog_launch_inspect(&json!({"appId":"parent-app","variantId":"123"}), &json!({}));
|
||||
match expected {
|
||||
Ok(decision) => assert_eq!(result.unwrap()["decision"]["status"], decision),
|
||||
Err(code) => {
|
||||
let error = result.unwrap_err();
|
||||
assert_eq!(error.code, code);
|
||||
assert!(
|
||||
error.message.contains(&status.to_string()),
|
||||
"{}",
|
||||
error.message
|
||||
);
|
||||
}
|
||||
}
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,396 @@
|
||||
use super::tests::{auth_fixture, mock_requests, test_service};
|
||||
use super::*;
|
||||
|
||||
fn service(url: &str) -> (GfnService, PathBuf) {
|
||||
let (mut service, path) = test_service(url);
|
||||
service.endpoints.graphql = format!("{url}/graphql");
|
||||
service.endpoints.public_graphql = format!("{url}/public");
|
||||
service.endpoints.server_info = Some(format!("{url}/vpc"));
|
||||
let mut state = service.state.lock().unwrap();
|
||||
state.session = Some(auth_fixture("fixture-account"));
|
||||
state.restore_attempted = true;
|
||||
drop(state);
|
||||
(service, path)
|
||||
}
|
||||
|
||||
fn app(id: usize) -> Value {
|
||||
json!({"id":format!("app-{id}"),"title":format!("Game {id}"),"library":{"favorited":true},
|
||||
"gfn":{"playabilityState":"PLAYABLE","catalogSkuStrings":{"SKU_BASED_TAG":["Fixture"]}},
|
||||
"variants":[{"id":"123","appStore":"STEAM","paymentModels":[{"__typename":"Paid"}],
|
||||
"gfn":{"status":"PATCHING","library":{"status":"MANUAL","selected":true,"playStatus":"NOT_PLAYABLE"},
|
||||
"stateDetails":{"__typename":"VariantGfnAutoPatchingMetadata","historicalEtaMins":12.5}}}]})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn library_traverses_thirteen_complete_pages_without_a_thousand_game_ceiling() {
|
||||
let mut responses = vec![(200, json!({"requestStatus":{"serverId":"fixture-vpc"}}))];
|
||||
for page in 0..13 {
|
||||
responses.push((200,json!({"data":{"apps":{"items":(page*100..page*100+100).map(app).collect::<Vec<_>>(),
|
||||
"pageInfo":{"hasNextPage":page < 12,"endCursor":format!("cursor-{}",page+1),"totalCount":1300}}}})));
|
||||
}
|
||||
let (url, worker) = mock_requests(responses, |index, request| {
|
||||
if index == 0 {
|
||||
assert!(request.starts_with("GET /vpc "));
|
||||
return;
|
||||
}
|
||||
let payload: Value =
|
||||
serde_json::from_str(request.split("\r\n\r\n").nth(1).unwrap()).unwrap();
|
||||
assert_eq!(payload["variables"]["fetchCount"], 100);
|
||||
assert_eq!(
|
||||
payload["variables"]["cursor"],
|
||||
if index == 1 {
|
||||
String::new()
|
||||
} else {
|
||||
format!("cursor-{}", index - 1)
|
||||
}
|
||||
);
|
||||
assert!(payload["query"].as_str().unwrap().contains("playStatus"));
|
||||
});
|
||||
let (service, path) = service(&url);
|
||||
let mut cursor = String::new();
|
||||
let mut context = Value::Null;
|
||||
let mut count = 0;
|
||||
for _ in 0..13 {
|
||||
let result = service
|
||||
.library_catalog(
|
||||
&json!({"limit":100,"cursor":cursor,"catalogRevision":0,"catalogContext":context,"traversalId":"fixture"}),
|
||||
&json!({}),
|
||||
)
|
||||
.unwrap();
|
||||
count += result["games"].as_array().unwrap().len();
|
||||
cursor = result["nextCursor"].as_str().unwrap().into();
|
||||
context = result["catalogContext"].clone();
|
||||
assert_eq!(result["traversalId"], "fixture");
|
||||
}
|
||||
assert_eq!(count, 1300);
|
||||
assert!(cursor.is_empty());
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exact_game_lookup_preserves_selected_variant_readiness_and_rejects_wrong_identity() {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
(200, json!({"requestStatus":{"serverId":"fixture-vpc"}})),
|
||||
(200, json!({"data":{"apps":{"items":[app(1)]}}})),
|
||||
(200, json!({"data":{"apps":{"items":[app(2)]}}})),
|
||||
],
|
||||
|index, request| {
|
||||
if index == 0 {
|
||||
return;
|
||||
}
|
||||
let payload: Value =
|
||||
serde_json::from_str(request.split("\r\n\r\n").nth(1).unwrap()).unwrap();
|
||||
let query = payload["query"].as_str().unwrap();
|
||||
assert_eq!(query.matches('{').count(), query.matches('}').count());
|
||||
assert!(query.contains("appIds:$ids"));
|
||||
assert!(query.contains("supportedLanguages"));
|
||||
assert_eq!(payload["variables"]["ids"], json!(["app-1"]));
|
||||
},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service
|
||||
.catalog_game(&json!({"appId":"app-1"}), &json!({}))
|
||||
.unwrap();
|
||||
assert_eq!(result["game"]["favorited"], true);
|
||||
assert_eq!(result["game"]["variants"][0]["playStatus"], "NOT_PLAYABLE");
|
||||
assert_eq!(
|
||||
result["game"]["variants"][0]["stateDetails"]["historicalEtaMins"],
|
||||
12.5
|
||||
);
|
||||
assert_eq!(
|
||||
service
|
||||
.catalog_game(&json!({"appId":"app-1"}), &json!({}))
|
||||
.unwrap_err()
|
||||
.code,
|
||||
"catalog_game_not_found"
|
||||
);
|
||||
assert!(
|
||||
service
|
||||
.catalog_game(&json!({"appId":"app-1","variantId":"123"}), &json!({}))
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
service
|
||||
.catalog_game(&json!({"variantId":"2147483648"}), &json!({}))
|
||||
.is_err()
|
||||
);
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn targeted_game_lookups_omit_campaign_dependencies_and_keep_launch_metadata() {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
(200, json!({"requestStatus":{"serverId":"fixture-vpc"}})),
|
||||
(200, json!({"data":{"apps":{"items":[app(1)]}}})),
|
||||
(200, json!({"data":{"apps":{"items":[app(1)]}}})),
|
||||
],
|
||||
|index, request| {
|
||||
if index == 0 {
|
||||
return;
|
||||
}
|
||||
let payload: Value =
|
||||
serde_json::from_str(request.split("\r\n\r\n").nth(1).unwrap()).unwrap();
|
||||
let query = payload["query"].as_str().unwrap();
|
||||
assert!(!query.contains("itemMetadata"));
|
||||
assert!(!query.contains("campaignIds"));
|
||||
for field in [
|
||||
"favorited",
|
||||
"playStatus",
|
||||
"selected",
|
||||
"installed",
|
||||
"subscription",
|
||||
"stateDetails",
|
||||
"supportedLanguages",
|
||||
"paymentModels",
|
||||
"minimumMembershipTierLabel",
|
||||
"playabilityState",
|
||||
] {
|
||||
assert!(query.contains(field), "missing {field}");
|
||||
}
|
||||
assert_eq!(payload["variables"]["vpcId"], "fixture-vpc");
|
||||
assert_eq!(payload["variables"]["locale"], "en_US");
|
||||
if index == 1 {
|
||||
assert!(query.contains("appIds:$ids"));
|
||||
assert_eq!(payload["variables"]["ids"], json!(["app-1"]));
|
||||
} else {
|
||||
assert!(query.contains("variantIds:$ids"));
|
||||
assert_eq!(payload["variables"]["ids"], json!([123]));
|
||||
}
|
||||
},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
for params in [json!({"appId":"app-1"}), json!({"variantId":"123"})] {
|
||||
let result = service.catalog_game(¶ms, &json!({})).unwrap();
|
||||
assert_eq!(result["game"]["id"], "app-1");
|
||||
assert_eq!(result["game"]["variants"][0]["libraryStatus"], "MANUAL");
|
||||
assert_eq!(result["game"]["variants"][0]["playStatus"], "NOT_PLAYABLE");
|
||||
assert_eq!(result["game"]["variants"][0]["gfnStatus"], "PATCHING");
|
||||
}
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overall_languages_are_anonymous_variable_free_cached_and_truthfully_stale() {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
(
|
||||
200,
|
||||
json!({"data":{"overallGfnSupportedLanguages":[{"language":"en_US"},{"language":"es_419"},{"language":"zh_Hant_TW"},{"language":"en_US"}]}}),
|
||||
),
|
||||
(
|
||||
200,
|
||||
json!({"data":{"overallGfnSupportedLanguages":[]},"errors":[{"message":"fixture failure"}]}),
|
||||
),
|
||||
],
|
||||
|_, request| {
|
||||
assert!(request.starts_with("POST /public "));
|
||||
assert!(!request.to_ascii_lowercase().contains("authorization:"));
|
||||
let payload: Value =
|
||||
serde_json::from_str(request.split("\r\n\r\n").nth(1).unwrap()).unwrap();
|
||||
assert_eq!(
|
||||
payload,
|
||||
json!({"query":"{ overallGfnSupportedLanguages { language } }"})
|
||||
);
|
||||
},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
service.state.lock().unwrap().session = None;
|
||||
let result = service.catalog_languages(&json!({}), &json!({})).unwrap();
|
||||
assert_eq!(
|
||||
result["languages"],
|
||||
json!(["en_US", "es_419", "zh_Hant_TW"])
|
||||
);
|
||||
assert_eq!(result["status"], "success");
|
||||
assert_eq!(
|
||||
result["expiresAt"].as_u64().unwrap() - result["fetchedAt"].as_u64().unwrap(),
|
||||
14 * 24 * 60 * 60 * 1000
|
||||
);
|
||||
assert_eq!(
|
||||
service.catalog_languages(&json!({}), &json!({})).unwrap()["cacheHit"],
|
||||
true
|
||||
);
|
||||
let stale = service
|
||||
.catalog_languages(&json!({"refresh":true}), &json!({}))
|
||||
.unwrap();
|
||||
assert_eq!(stale["status"], "stale");
|
||||
assert_eq!(stale["languages"], result["languages"]);
|
||||
assert_eq!(stale["error"]["code"], "graphql_error");
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_language_metadata_is_not_cached_as_success() {
|
||||
for value in [
|
||||
Value::Null,
|
||||
json!([]),
|
||||
json!([{"language":""}]),
|
||||
json!([{"language":"auto"}]),
|
||||
json!([{"language":"system"}]),
|
||||
json!([{"language":"en__US"}]),
|
||||
json!([{"language":"en_US/../../"}]),
|
||||
json!([{"language":"x".repeat(65)}]),
|
||||
json!([{"language":42}]),
|
||||
] {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![(200, json!({"data":{"overallGfnSupportedLanguages":value}}))],
|
||||
|_, _| {},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service.catalog_languages(&json!({}), &json!({})).unwrap();
|
||||
assert_eq!(result["status"], "error");
|
||||
assert_eq!(result["languages"], json!([]));
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn revised_catalog_rejects_continuation_before_network_dispatch() {
|
||||
let (service, path) = service("http://127.0.0.1:1");
|
||||
service.invalidate_catalog().unwrap();
|
||||
assert_eq!(
|
||||
service
|
||||
.library_catalog(&json!({"cursor":"old","catalogRevision":0}), &json!({}))
|
||||
.unwrap_err()
|
||||
.code,
|
||||
"catalog_changed"
|
||||
);
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_store_refresh_revalidates_a_warm_disk_chain_and_reports_coverage() {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
(200, json!({"requestStatus":{"serverId":"fixture-vpc"}})),
|
||||
(
|
||||
200,
|
||||
json!({"data":{"apps":{"items":[app(1)],"pageInfo":{"hasNextPage":true,"endCursor":"more","totalCount":50}}}}),
|
||||
),
|
||||
(
|
||||
200,
|
||||
json!({"data":{"apps":{"items":[app(2)],"pageInfo":{"hasNextPage":false,"totalCount":1}}}}),
|
||||
),
|
||||
],
|
||||
|_, _| {},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let first = service.store_local_catalog(&json!({}), &json!({})).unwrap();
|
||||
assert_eq!(first["games"][0]["id"], "app-1");
|
||||
assert_eq!(first["cacheComplete"], false);
|
||||
assert_eq!(first["upstreamCoverage"], "partial");
|
||||
assert_eq!(first["localHasNextPage"], false);
|
||||
let refreshed = service
|
||||
.store_local_catalog(&json!({"refresh":true}), &json!({}))
|
||||
.unwrap();
|
||||
assert_eq!(refreshed["games"][0]["id"], "app-2");
|
||||
assert_eq!(refreshed["cacheComplete"], true);
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_store_bootstrap_uses_scope_refreshed_during_nested_read() {
|
||||
for params in [json!({}), json!({"refresh":true})] {
|
||||
let renewed_id = super::tests::jwt("fixture-account", now_ms() + 7_200_000);
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
(200, json!({"requestStatus":{"serverId":"old-vpc"}})),
|
||||
(401, json!({})),
|
||||
(
|
||||
200,
|
||||
json!({"access_token":"renewed-access","id_token":renewed_id,"expires_in":7200}),
|
||||
),
|
||||
(200, json!({"requestStatus":{"serverId":"new-vpc"}})),
|
||||
(
|
||||
200,
|
||||
json!({"data":{"apps":{"items":[app(1)],"pageInfo":{"hasNextPage":false,"totalCount":1}}}}),
|
||||
),
|
||||
],
|
||||
|index, request| {
|
||||
if index == 1 {
|
||||
assert!(request.contains("old-vpc"));
|
||||
}
|
||||
if index == 4 {
|
||||
assert!(request.contains("new-vpc"));
|
||||
}
|
||||
},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service.store_local_catalog(¶ms, &json!({}));
|
||||
worker.join().unwrap();
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"nested renewal left the local index in the old catalog scope: {:?}",
|
||||
result.err()
|
||||
);
|
||||
let result = result.unwrap();
|
||||
assert_eq!(result["games"][0]["id"], "app-1");
|
||||
assert_eq!(result["scope"]["generation"], 0);
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn optional_definition_failure_does_not_hide_server_disabled_store_actions() {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
(
|
||||
200,
|
||||
json!({"data":{"appStoreDefinitions":[{"store":"STEAM","label":"Steam","features":[
|
||||
{"__typename":"AccountGamesSyncing","supported":false}]}]}}),
|
||||
),
|
||||
(503, json!({})),
|
||||
(
|
||||
200,
|
||||
json!({"data":{"subscriptionDefinitions":[{"subscription":"GAMEPASS","label":"Game Pass","primaryStore":"XBOX"}]}}),
|
||||
),
|
||||
(
|
||||
200,
|
||||
json!({"data":{"userAccount":{"subscriptions":[{"id":"GAMEPASS"}],"storesData":[]}}}),
|
||||
),
|
||||
],
|
||||
|_, _| {},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service.account_connections(&json!({})).unwrap();
|
||||
assert_eq!(result["accounts"][0]["supportsSync"], false);
|
||||
assert_eq!(result["accounts"][0]["capabilitySource"], "success");
|
||||
assert_eq!(result["definitions"]["genres"]["status"], "error");
|
||||
assert_eq!(result["subscriptions"][0]["id"], "GAMEPASS");
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delayed_anonymous_metadata_is_rejected_after_a_context_change() {
|
||||
let (entered_tx, entered_rx) = std::sync::mpsc::channel();
|
||||
let (release_tx, release_rx) = std::sync::mpsc::channel();
|
||||
let (url, worker) = mock_requests(
|
||||
vec![(
|
||||
200,
|
||||
json!({"data":{"overallGfnSupportedLanguages":[{"language":"en_US"}]}}),
|
||||
)],
|
||||
move |_, _| {
|
||||
entered_tx.send(()).unwrap();
|
||||
release_rx.recv_timeout(Duration::from_secs(5)).unwrap();
|
||||
},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
std::thread::scope(|threads| {
|
||||
let pending = threads.spawn(|| service.catalog_languages(&json!({}), &json!({})));
|
||||
entered_rx.recv_timeout(Duration::from_secs(5)).unwrap();
|
||||
service.state.lock().unwrap().generation += 1;
|
||||
release_tx.send(()).unwrap();
|
||||
assert_eq!(pending.join().unwrap().unwrap_err().code, "stale_account");
|
||||
});
|
||||
worker.join().unwrap();
|
||||
assert!(!path.join("store-cache-v2").exists());
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
@@ -0,0 +1,763 @@
|
||||
//! Per-game DLSS support derived from NVIDIA's published DLSS games list.
|
||||
//!
|
||||
//! The GeForce NOW catalog has no per-game DLSS key. NVIDIA's RTX games page loads its
|
||||
//! table from a public JSON list, so the core matches catalog titles against that list.
|
||||
//! The list only decorates catalog responses: when it is missing, oversized, or invalid,
|
||||
//! every game carries `dlss: null` and the next catalog request retries.
|
||||
|
||||
use super::{ServiceError, now_ms};
|
||||
use crate::proxy::{client_for_settings, config_from_settings};
|
||||
use crate::store_cache::{CachePolicy, StoreCache};
|
||||
use icu_normalizer::DecomposingNormalizerBorrowed;
|
||||
use reqwest::blocking::Client;
|
||||
use reqwest::header::ACCEPT;
|
||||
use serde::Serialize;
|
||||
use serde_json::{Value, json};
|
||||
use std::collections::HashMap;
|
||||
use std::io::Read;
|
||||
use std::sync::{Arc, Condvar, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
pub const LIST_URL: &str = "https://www.nvidia.com/content/dam/en-zz/Solutions/geforce/news/nvidia-rtx-games-engines-apps/dlss-rt-games-apps-overrides.json";
|
||||
const SOURCE: &str = "nvidia-rtx-games-list";
|
||||
const MAX_LIST_BYTES: usize = 4 * 1024 * 1024;
|
||||
const MAX_ROWS: usize = 5_000;
|
||||
const MAX_NAME_BYTES: usize = 512;
|
||||
const MAX_CELL_BYTES: usize = 64;
|
||||
const TTL_MS: u64 = 24 * 60 * 60 * 1000;
|
||||
const RETRY_MS: u64 = 60 * 1000;
|
||||
const FIRST_LOAD_WAIT: Duration = Duration::from_millis(1500);
|
||||
const FETCH_TIMEOUT: Duration = Duration::from_secs(15);
|
||||
const MAX_ANNOTATION_DEPTH: usize = 10;
|
||||
|
||||
/// Column order of a cached row; the cache stores only these cells.
|
||||
const COLUMNS: [&str; 5] = [
|
||||
"dlss super resolution",
|
||||
"dlss frame generation",
|
||||
"dlss multi frame generation",
|
||||
"dlss ray reconstruction",
|
||||
"dlaa",
|
||||
];
|
||||
|
||||
/// Trailing title words that name an edition of the same game. "Remastered" is
|
||||
/// deliberately absent: remasters are listed separately and differ in support.
|
||||
const EDITION_SUFFIXES: [&[&str]; 11] = [
|
||||
&["game", "of", "year"],
|
||||
&["goty"],
|
||||
&["definitive"],
|
||||
&["enhanced"],
|
||||
&["complete"],
|
||||
&["deluxe"],
|
||||
&["standard"],
|
||||
&["ultimate"],
|
||||
&["directors", "cut"],
|
||||
&["director", "s", "cut"],
|
||||
&["anniversary"],
|
||||
];
|
||||
|
||||
type Row = (String, [String; 5]);
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Support {
|
||||
Native,
|
||||
Override,
|
||||
}
|
||||
|
||||
impl Support {
|
||||
fn from_cell(cell: &str) -> Option<Self> {
|
||||
if cell.eq_ignore_ascii_case("yes") {
|
||||
Some(Self::Native)
|
||||
} else if cell.starts_with("NV") {
|
||||
Some(Self::Override)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct MultiFrameGeneration {
|
||||
max_multiplier: u8,
|
||||
}
|
||||
|
||||
impl MultiFrameGeneration {
|
||||
fn from_cell(cell: &str) -> Option<Self> {
|
||||
cell.split(|character: char| !character.is_ascii_alphanumeric())
|
||||
.find_map(|token| token.strip_suffix(['X', 'x'])?.parse::<u8>().ok())
|
||||
.filter(|multiplier| (2..=16).contains(multiplier))
|
||||
.map(|max_multiplier| Self { max_multiplier })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DlssSupport {
|
||||
super_resolution: Option<Support>,
|
||||
frame_generation: Option<Support>,
|
||||
multi_frame_generation: Option<MultiFrameGeneration>,
|
||||
ray_reconstruction: Option<Support>,
|
||||
dlaa: Option<Support>,
|
||||
}
|
||||
|
||||
impl DlssSupport {
|
||||
/// None when the row lists no DLSS technology (for example ray tracing only).
|
||||
fn from_cells([sr, fg, mfg, rr, dlaa]: &[String; 5]) -> Option<Self> {
|
||||
let support = Self {
|
||||
super_resolution: Support::from_cell(sr),
|
||||
frame_generation: Support::from_cell(fg),
|
||||
multi_frame_generation: MultiFrameGeneration::from_cell(mfg),
|
||||
ray_reconstruction: Support::from_cell(rr),
|
||||
dlaa: Support::from_cell(dlaa),
|
||||
};
|
||||
let listed = support.super_resolution.is_some()
|
||||
|| support.frame_generation.is_some()
|
||||
|| support.multi_frame_generation.is_some()
|
||||
|| support.ray_reconstruction.is_some()
|
||||
|| support.dlaa.is_some();
|
||||
listed.then_some(support)
|
||||
}
|
||||
|
||||
fn to_json(self) -> Value {
|
||||
let mut value = serde_json::to_value(self).expect("DLSS support serializes");
|
||||
value["source"] = json!(SOURCE);
|
||||
value
|
||||
}
|
||||
}
|
||||
|
||||
fn invalid_list(message: &str) -> ServiceError {
|
||||
ServiceError {
|
||||
code: "invalid_upstream_response",
|
||||
message: format!("DLSS games list rejected: {message}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses NVIDIA's list into bounded Game rows. Apps and malformed rows are skipped;
|
||||
/// an oversized or structurally invalid document is rejected as a whole.
|
||||
pub fn parse_list(bytes: &[u8]) -> Result<Vec<Row>, ServiceError> {
|
||||
if bytes.len() > MAX_LIST_BYTES {
|
||||
return Err(invalid_list("larger than 4 MiB"));
|
||||
}
|
||||
let document: Value =
|
||||
serde_json::from_slice(bytes).map_err(|_| invalid_list("not valid JSON"))?;
|
||||
let data = document["data"]
|
||||
.as_array()
|
||||
.ok_or_else(|| invalid_list("no data array"))?;
|
||||
if data.len() > MAX_ROWS {
|
||||
return Err(invalid_list("more than 5,000 rows"));
|
||||
}
|
||||
Ok(data
|
||||
.iter()
|
||||
.filter(|row| row["type"].as_str() == Some("Game"))
|
||||
.filter_map(|row| {
|
||||
let name = match &row["name"] {
|
||||
Value::String(name) => name.trim().to_owned(),
|
||||
Value::Number(number) => number.to_string(),
|
||||
_ => return None,
|
||||
};
|
||||
if name.is_empty() || name.len() > MAX_NAME_BYTES {
|
||||
return None;
|
||||
}
|
||||
let cells = COLUMNS.map(|column| {
|
||||
row[column]
|
||||
.as_str()
|
||||
.map(str::trim)
|
||||
.filter(|cell| cell.len() <= MAX_CELL_BYTES)
|
||||
.unwrap_or("")
|
||||
.to_owned()
|
||||
});
|
||||
Some((name, cells))
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Lowercase ASCII words: trademark and zero-width characters removed, NFKD folded,
|
||||
/// "&" spelled "and", and every other non-alphanumeric run collapsed to one space.
|
||||
pub fn normalize_title(title: &str) -> String {
|
||||
let stripped: String = title
|
||||
.chars()
|
||||
.filter(|character| {
|
||||
!matches!(
|
||||
character,
|
||||
'\u{2122}'
|
||||
| '\u{00AE}'
|
||||
| '\u{00A9}'
|
||||
| '\u{200B}'
|
||||
| '\u{200C}'
|
||||
| '\u{200D}'
|
||||
| '\u{2060}'
|
||||
| '\u{FEFF}'
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let ascii: String = DecomposingNormalizerBorrowed::new_nfkd()
|
||||
.normalize(&stripped)
|
||||
.chars()
|
||||
.filter(char::is_ascii)
|
||||
.collect::<String>()
|
||||
.to_ascii_lowercase()
|
||||
.replace('&', " and ");
|
||||
ascii
|
||||
.split(|character: char| !character.is_ascii_alphanumeric())
|
||||
.filter(|word| !word.is_empty())
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
}
|
||||
|
||||
/// A normalized title without "the"/"tm" and without trailing edition words.
|
||||
fn loose_title(normalized: &str) -> String {
|
||||
let mut words: Vec<&str> = normalized
|
||||
.split(' ')
|
||||
.filter(|word| !word.is_empty() && *word != "the" && *word != "tm")
|
||||
.collect();
|
||||
loop {
|
||||
let before = words.len();
|
||||
if words.len() > 1 && matches!(words.last(), Some(&"edition" | &"pc")) {
|
||||
words.pop();
|
||||
}
|
||||
if let Some(suffix) = EDITION_SUFFIXES
|
||||
.iter()
|
||||
.find(|suffix| words.len() > suffix.len() && words.ends_with(suffix))
|
||||
{
|
||||
words.truncate(words.len() - suffix.len());
|
||||
}
|
||||
if words.len() == before {
|
||||
return words.join(" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct DlssIndex {
|
||||
exact: HashMap<String, Option<DlssSupport>>,
|
||||
loose: HashMap<String, Option<DlssSupport>>,
|
||||
}
|
||||
|
||||
impl DlssIndex {
|
||||
/// Every Game row is indexed, including rows without DLSS, so an exact title
|
||||
/// without DLSS never falls through to a loosely matching edition that has it.
|
||||
pub fn from_rows(rows: &[Row]) -> Self {
|
||||
let mut index = Self::default();
|
||||
for (name, cells) in rows {
|
||||
let exact = normalize_title(name);
|
||||
if exact.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let support = DlssSupport::from_cells(cells);
|
||||
index.loose.entry(loose_title(&exact)).or_insert(support);
|
||||
index.exact.entry(exact).or_insert(support);
|
||||
}
|
||||
index
|
||||
}
|
||||
|
||||
pub fn lookup(&self, title: &str) -> Option<DlssSupport> {
|
||||
let exact = normalize_title(title);
|
||||
if exact.is_empty() {
|
||||
return None;
|
||||
}
|
||||
match self.exact.get(&exact) {
|
||||
Some(support) => *support,
|
||||
None => self.loose.get(&loose_title(&exact)).copied().flatten(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets `dlss` on every catalog game in a response. A game is an object with a
|
||||
/// string `title`, a `variants` array, and a `launchAppId` key, the shape both
|
||||
/// catalog mappers produce. Games are never nested inside games.
|
||||
pub fn annotate(value: &mut Value, index: Option<&DlssIndex>) {
|
||||
fn visit(value: &mut Value, index: Option<&DlssIndex>, depth: usize) {
|
||||
if depth > MAX_ANNOTATION_DEPTH {
|
||||
return;
|
||||
}
|
||||
match value {
|
||||
Value::Array(items) => {
|
||||
for item in items {
|
||||
visit(item, index, depth + 1);
|
||||
}
|
||||
}
|
||||
Value::Object(object) => {
|
||||
if object.get("variants").is_some_and(Value::is_array)
|
||||
&& object.contains_key("launchAppId")
|
||||
&& let Some(title) = object.get("title").and_then(Value::as_str)
|
||||
{
|
||||
match index {
|
||||
Some(index) => {
|
||||
let dlss = index
|
||||
.lookup(title)
|
||||
.map_or(Value::Null, DlssSupport::to_json);
|
||||
object.insert("dlss".to_owned(), dlss);
|
||||
}
|
||||
None => {
|
||||
object.remove("dlss");
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
for child in object.values_mut() {
|
||||
visit(child, index, depth + 1);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
visit(value, index, 0);
|
||||
}
|
||||
|
||||
fn fetch_list(client: &Client, url: &str) -> Result<Value, ServiceError> {
|
||||
let response = client
|
||||
.get(url)
|
||||
.header(ACCEPT, "application/json")
|
||||
.timeout(FETCH_TIMEOUT)
|
||||
.send()
|
||||
.map_err(|error| ServiceError::network("DLSS games list fetch failed", error))?;
|
||||
if !response.status().is_success() {
|
||||
return Err(ServiceError::response(
|
||||
"DLSS games list fetch failed",
|
||||
response,
|
||||
));
|
||||
}
|
||||
if response
|
||||
.content_length()
|
||||
.is_some_and(|length| length > MAX_LIST_BYTES as u64)
|
||||
{
|
||||
return Err(invalid_list("larger than 4 MiB"));
|
||||
}
|
||||
let mut bytes = Vec::new();
|
||||
response
|
||||
.take(MAX_LIST_BYTES as u64 + 1)
|
||||
.read_to_end(&mut bytes)
|
||||
.map_err(|_| invalid_list("the download was interrupted"))?;
|
||||
let rows = parse_list(&bytes)?;
|
||||
Ok(json!({"metadataKind":"dlssGames","source":SOURCE,"rows":rows}))
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct LoadState {
|
||||
index: Option<Arc<DlssIndex>>,
|
||||
expires_at: u64,
|
||||
retry_at: u64,
|
||||
loading: bool,
|
||||
first_attempt_done: bool,
|
||||
first_wait_claimed: bool,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Shared {
|
||||
state: Mutex<LoadState>,
|
||||
settled: Condvar,
|
||||
}
|
||||
|
||||
/// Owns the in-memory list and its background refresh. Catalog requests never wait on
|
||||
/// a download except the process's first attempt, and only for `FIRST_LOAD_WAIT`.
|
||||
pub struct DlssCatalog {
|
||||
client: Client,
|
||||
url: String,
|
||||
cache: Arc<StoreCache>,
|
||||
shared: Arc<Shared>,
|
||||
}
|
||||
|
||||
impl DlssCatalog {
|
||||
pub fn new(client: Client, url: String, cache: Arc<StoreCache>) -> Self {
|
||||
Self {
|
||||
client,
|
||||
url,
|
||||
cache,
|
||||
shared: Arc::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn index(&self, settings: &Value) -> Option<Arc<DlssIndex>> {
|
||||
let mut state = self.shared.state.lock().expect("DLSS list poisoned");
|
||||
let now = now_ms();
|
||||
if !state.loading
|
||||
&& now >= state.retry_at
|
||||
&& (state.index.is_none() || now >= state.expires_at)
|
||||
{
|
||||
state.loading = self.spawn_load(settings);
|
||||
if !state.loading {
|
||||
state.first_attempt_done = true;
|
||||
state.retry_at = now.saturating_add(RETRY_MS);
|
||||
}
|
||||
}
|
||||
if state.index.is_none() && !state.first_attempt_done && !state.first_wait_claimed {
|
||||
state.first_wait_claimed = true;
|
||||
state = self
|
||||
.shared
|
||||
.settled
|
||||
.wait_timeout_while(state, FIRST_LOAD_WAIT, |state| !state.first_attempt_done)
|
||||
.expect("DLSS list poisoned")
|
||||
.0;
|
||||
}
|
||||
state.index.clone()
|
||||
}
|
||||
|
||||
fn spawn_load(&self, settings: &Value) -> bool {
|
||||
let (Ok(client), Ok(proxy)) = (
|
||||
client_for_settings(&self.client, settings),
|
||||
config_from_settings(settings),
|
||||
) else {
|
||||
return false;
|
||||
};
|
||||
let scope = json!([
|
||||
"dlss-games-v1",
|
||||
self.url,
|
||||
proxy.map_or_else(|| "direct".to_owned(), |config| config.cache_scope)
|
||||
]);
|
||||
let (url, cache, shared) = (self.url.clone(), self.cache.clone(), self.shared.clone());
|
||||
std::thread::Builder::new()
|
||||
.name("dlss-games-list".into())
|
||||
.spawn(move || {
|
||||
let loaded = cache
|
||||
.load_with_policy(
|
||||
&scope,
|
||||
&json!(["dlss-games"]),
|
||||
false,
|
||||
CachePolicy {
|
||||
ttl_ms: TTL_MS,
|
||||
allow_stale: true,
|
||||
},
|
||||
|| fetch_list(&client, &url),
|
||||
)
|
||||
.and_then(|value| {
|
||||
if let Some(code) = value["error"]["code"].as_str() {
|
||||
eprintln!("dlss games list: refresh failed ({code})");
|
||||
}
|
||||
let rows: Vec<Row> = serde_json::from_value(value["rows"].clone())
|
||||
.map_err(|_| invalid_list("no cached rows"))?;
|
||||
if rows.len() > MAX_ROWS {
|
||||
return Err(invalid_list("more than 5,000 rows"));
|
||||
}
|
||||
Ok((
|
||||
DlssIndex::from_rows(&rows),
|
||||
value["expiresAt"].as_u64().unwrap_or(0),
|
||||
))
|
||||
});
|
||||
let mut state = shared.state.lock().expect("DLSS list poisoned");
|
||||
let now = now_ms();
|
||||
match loaded {
|
||||
Ok((index, expires_at)) => {
|
||||
state.index = Some(Arc::new(index));
|
||||
state.expires_at = expires_at;
|
||||
}
|
||||
Err(error) => eprintln!("dlss games list: unavailable ({})", error.code),
|
||||
}
|
||||
if state.expires_at <= now {
|
||||
state.retry_at = now.saturating_add(RETRY_MS);
|
||||
}
|
||||
state.loading = false;
|
||||
state.first_attempt_done = true;
|
||||
shared.settled.notify_all();
|
||||
})
|
||||
.is_ok()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn list(rows: Value) -> Vec<u8> {
|
||||
serde_json::to_vec(&json!({"columns":[],"footer":"","data":rows})).unwrap()
|
||||
}
|
||||
|
||||
fn row(name: Value, cells: [&str; 5]) -> Value {
|
||||
let mut row = json!({"sno":1,"name":name,"type":"Game","ray tracing":"","ai":""});
|
||||
for (column, cell) in COLUMNS.iter().zip(cells) {
|
||||
row[*column] = json!(cell);
|
||||
}
|
||||
row
|
||||
}
|
||||
|
||||
fn index(rows: Value) -> DlssIndex {
|
||||
DlssIndex::from_rows(&parse_list(&list(rows)).unwrap())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_string_and_numeric_names_and_skips_apps() {
|
||||
let mut app = row(json!("Broadcast"), ["Yes", "", "", "", ""]);
|
||||
app["type"] = json!("App");
|
||||
let rows = parse_list(&list(json!([
|
||||
row(json!("007 First Light"), ["NV, T", "NV, U", "NV, 6X", "NV, T", "NV, T"]),
|
||||
row(json!(83), ["NV, T", "", "", "", ""]),
|
||||
app,
|
||||
{"name":null,"type":"Game"},
|
||||
"not a row",
|
||||
])))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
rows.iter()
|
||||
.map(|(name, _)| name.as_str())
|
||||
.collect::<Vec<_>>(),
|
||||
["007 First Light", "83"]
|
||||
);
|
||||
assert_eq!(rows[0].1[2], "NV, 6X");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maps_cells_to_native_override_and_multiplier() {
|
||||
let index = index(json!([
|
||||
row(
|
||||
json!("Cyberpunk 2077"),
|
||||
["Yes", "Yes", "NV, 6X", "NV, T", "Yes"]
|
||||
),
|
||||
row(
|
||||
json!("Black Myth: Wukong"),
|
||||
["NV, T", "Yes", "NV, 4X", "", ""]
|
||||
),
|
||||
row(json!("Ray Traced Only"), ["", "", "", "", ""]),
|
||||
]));
|
||||
assert_eq!(
|
||||
index.lookup("Cyberpunk 2077").unwrap().to_json(),
|
||||
json!({"superResolution":"native","frameGeneration":"native",
|
||||
"multiFrameGeneration":{"maxMultiplier":6},"rayReconstruction":"override",
|
||||
"dlaa":"native","source":"nvidia-rtx-games-list"})
|
||||
);
|
||||
let wukong = index.lookup("Black Myth: Wukong").unwrap();
|
||||
assert_eq!(wukong.super_resolution, Some(Support::Override));
|
||||
assert_eq!(
|
||||
wukong.multi_frame_generation,
|
||||
Some(MultiFrameGeneration { max_multiplier: 4 })
|
||||
);
|
||||
assert_eq!(wukong.ray_reconstruction, None);
|
||||
assert_eq!(index.lookup("Ray Traced Only"), None);
|
||||
assert_eq!(MultiFrameGeneration::from_cell("Yes"), None);
|
||||
assert_eq!(Support::from_cell("Maybe"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_oversized_or_invalid_lists() {
|
||||
assert!(parse_list(&vec![b' '; MAX_LIST_BYTES + 1]).is_err());
|
||||
assert!(parse_list(b"not json").is_err());
|
||||
assert!(parse_list(br#"{"data":{}}"#).is_err());
|
||||
assert!(parse_list(br#"[]"#).is_err());
|
||||
let rows = vec![json!({"name":"x","type":"App"}); MAX_ROWS + 1];
|
||||
assert!(parse_list(&list(Value::Array(rows))).is_err());
|
||||
let rows = vec![json!({"name":"x","type":"App"}); MAX_ROWS];
|
||||
assert!(parse_list(&list(Value::Array(rows))).unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalizes_marks_accents_and_punctuation() {
|
||||
assert_eq!(normalize_title("Cyberpunk 2077®"), "cyberpunk 2077");
|
||||
assert_eq!(normalize_title("Fortnite™"), "fortnite");
|
||||
assert_eq!(
|
||||
normalize_title("Shadow of the Tomb Raider\u{200B}™ Definitive Edition"),
|
||||
"shadow of the tomb raider definitive edition"
|
||||
);
|
||||
assert_eq!(normalize_title("Pokémon & Friends©"), "pokemon and friends");
|
||||
assert_eq!(
|
||||
normalize_title(" S.T.A.L.K.E.R. 2: Heart of Chornobyl "),
|
||||
"s t a l k e r 2 heart of chornobyl"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn matches_exact_then_loose_editions_but_not_remasters() {
|
||||
let index = index(json!([
|
||||
row(
|
||||
json!("Cyberpunk 2077"),
|
||||
["Yes", "Yes", "NV, 6X", "NV, T", "Yes"]
|
||||
),
|
||||
row(json!("Fortnite"), ["NV, T", "", "", "", "NV, T"]),
|
||||
row(json!("Control"), ["Yes", "", "", "", ""]),
|
||||
row(json!("Alan Wake Remastered"), ["Yes", "", "", "", ""]),
|
||||
row(json!("Goat Simulator: Remastered"), ["Yes", "", "", "", ""]),
|
||||
row(json!("Battlefield V"), ["Yes", "", "", "", ""]),
|
||||
row(json!("The Witcher 3: Wild Hunt"), ["Yes", "", "", "", ""]),
|
||||
]));
|
||||
assert!(index.lookup("Cyberpunk 2077®").is_some());
|
||||
let fortnite = index.lookup("Fortnite®").unwrap();
|
||||
assert_eq!(fortnite.super_resolution, Some(Support::Override));
|
||||
assert_eq!(fortnite.frame_generation, None);
|
||||
assert!(index.lookup("Control Ultimate Edition").is_some());
|
||||
assert!(index.lookup("Battlefield™ V: Definitive").is_some());
|
||||
assert!(
|
||||
index
|
||||
.lookup("The Witcher 3: Wild Hunt - Game of the Year Edition")
|
||||
.is_some()
|
||||
);
|
||||
assert!(
|
||||
index
|
||||
.lookup("Witcher 3: Wild Hunt Complete Edition")
|
||||
.is_some()
|
||||
);
|
||||
assert_eq!(index.lookup("Alan Wake"), None);
|
||||
assert_eq!(index.lookup("Goat Simulator"), None);
|
||||
assert_eq!(index.lookup("Control 2"), None);
|
||||
assert_eq!(index.lookup("Ultimate"), None);
|
||||
assert_eq!(index.lookup("™"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exact_rows_without_dlss_do_not_fall_back_to_editions() {
|
||||
let index = index(json!([
|
||||
row(json!("Puzzle Quest"), ["", "", "", "", ""]),
|
||||
row(json!("Puzzle Quest Deluxe"), ["Yes", "", "", "", ""]),
|
||||
]));
|
||||
assert_eq!(index.lookup("Puzzle Quest"), None);
|
||||
assert!(index.lookup("Puzzle Quest™ Deluxe").is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn catalog_games_gain_dlss_or_null() {
|
||||
let index = index(json!([row(
|
||||
json!("Cyberpunk 2077"),
|
||||
["Yes", "Yes", "NV, 6X", "NV, T", "Yes"]
|
||||
)]));
|
||||
let app = |id: &str, title: &str| {
|
||||
json!({"id":id,"title":title,"variants":[{"id":format!("{id}1"),"appStore":"Steam",
|
||||
"gfn":{"features":[{"key":"RTX_ENABLED","value":"true"}]}}],"gfn":{"playabilityState":"PLAYABLE"}})
|
||||
};
|
||||
let matched = super::super::catalog::app_to_game(&app("cms-1", "Cyberpunk 2077®")).unwrap();
|
||||
let unmatched = super::super::catalog::app_to_game(&app("cms-2", "Alan Wake")).unwrap();
|
||||
let mut response = json!({
|
||||
"games":[matched.clone(), unmatched],
|
||||
"items":[{"sections":[{"games":[matched.clone()]}]},{"kind":"game","game":matched}],
|
||||
"catalogRevision":3
|
||||
});
|
||||
annotate(&mut response, Some(&index));
|
||||
assert_eq!(
|
||||
response["games"][0]["dlss"]["multiFrameGeneration"],
|
||||
json!({"maxMultiplier":6})
|
||||
);
|
||||
assert_eq!(
|
||||
response["games"][0]["dlss"]["source"],
|
||||
"nvidia-rtx-games-list"
|
||||
);
|
||||
assert_eq!(
|
||||
response["games"][0]["variants"][0]["features"],
|
||||
json!(["rtx"])
|
||||
);
|
||||
assert!(response["games"][0]["variants"][0].get("dlss").is_none());
|
||||
assert!(response["games"][1]["dlss"].is_null());
|
||||
assert!(response["games"][1].get("dlss").is_some());
|
||||
assert_eq!(
|
||||
response["items"][0]["sections"][0]["games"][0]["dlss"]["superResolution"],
|
||||
"native"
|
||||
);
|
||||
assert_eq!(
|
||||
response["items"][1]["game"]["dlss"]["rayReconstruction"],
|
||||
"override"
|
||||
);
|
||||
assert!(response.get("dlss").is_none());
|
||||
|
||||
let mut unavailable = json!({"game":response["games"][0].clone()});
|
||||
annotate(&mut unavailable, None);
|
||||
assert!(unavailable["game"].get("dlss").is_none());
|
||||
}
|
||||
|
||||
fn serve(body: &'static [u8], requests: usize) -> String {
|
||||
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let url = format!("http://{}/dlss.json", listener.local_addr().unwrap());
|
||||
std::thread::spawn(move || {
|
||||
use std::io::Write;
|
||||
for stream in listener.incoming().take(requests) {
|
||||
let mut stream = stream.unwrap();
|
||||
let mut request = [0_u8; 4096];
|
||||
let _ = stream.read(&mut request);
|
||||
let _ = write!(
|
||||
stream,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
|
||||
body.len()
|
||||
);
|
||||
let _ = stream.write_all(body);
|
||||
}
|
||||
});
|
||||
url
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn catalog_loads_once_then_serves_from_the_disk_cache() {
|
||||
let body: &'static [u8] = Box::leak(
|
||||
list(json!([row(
|
||||
json!("Fortnite"),
|
||||
["NV, T", "", "", "", "NV, T"]
|
||||
)]))
|
||||
.into_boxed_slice(),
|
||||
);
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let url = serve(body, 1);
|
||||
let cache = Arc::new(StoreCache::new(directory.path().to_path_buf()));
|
||||
let catalog = DlssCatalog::new(Client::new(), url.clone(), cache.clone());
|
||||
let index = catalog
|
||||
.index(&json!({}))
|
||||
.expect("first load within the bound");
|
||||
assert!(index.lookup("Fortnite®").is_some());
|
||||
|
||||
let restarted = DlssCatalog::new(Client::new(), url, cache);
|
||||
assert!(
|
||||
restarted
|
||||
.index(&json!({}))
|
||||
.unwrap()
|
||||
.lookup("Fortnite")
|
||||
.is_some()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_one_request_waits_for_a_slow_first_download() {
|
||||
let silent = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let url = format!("http://{}/dlss.json", silent.local_addr().unwrap());
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let catalog = Arc::new(DlssCatalog::new(
|
||||
Client::new(),
|
||||
url,
|
||||
Arc::new(StoreCache::new(directory.path().to_path_buf())),
|
||||
));
|
||||
let started = std::time::Instant::now();
|
||||
let concurrent = {
|
||||
let catalog = catalog.clone();
|
||||
std::thread::spawn(move || {
|
||||
std::thread::sleep(Duration::from_millis(200));
|
||||
let started = std::time::Instant::now();
|
||||
assert!(catalog.index(&json!({})).is_none());
|
||||
started.elapsed()
|
||||
})
|
||||
};
|
||||
assert!(catalog.index(&json!({})).is_none());
|
||||
assert!(started.elapsed() >= FIRST_LOAD_WAIT);
|
||||
assert!(concurrent.join().unwrap() < Duration::from_millis(100));
|
||||
let started = std::time::Instant::now();
|
||||
assert!(catalog.index(&json!({})).is_none());
|
||||
assert!(started.elapsed() < Duration::from_millis(100));
|
||||
assert!(catalog.shared.state.lock().unwrap().loading);
|
||||
drop(silent);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unavailable_list_yields_no_index_without_failing() {
|
||||
let closed = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let url = format!("http://{}/dlss.json", closed.local_addr().unwrap());
|
||||
drop(closed);
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let catalog = DlssCatalog::new(
|
||||
Client::new(),
|
||||
url,
|
||||
Arc::new(StoreCache::new(directory.path().to_path_buf())),
|
||||
);
|
||||
let started = std::time::Instant::now();
|
||||
assert!(catalog.index(&json!({})).is_none());
|
||||
assert!(started.elapsed() <= FIRST_LOAD_WAIT + Duration::from_millis(500));
|
||||
let started = std::time::Instant::now();
|
||||
assert!(catalog.index(&json!({})).is_none());
|
||||
assert!(started.elapsed() < Duration::from_millis(100));
|
||||
let state = catalog
|
||||
.shared
|
||||
.settled
|
||||
.wait_timeout_while(
|
||||
catalog.shared.state.lock().unwrap(),
|
||||
FETCH_TIMEOUT * 2,
|
||||
|state| state.loading,
|
||||
)
|
||||
.unwrap()
|
||||
.0;
|
||||
assert!(!state.loading && state.index.is_none() && state.retry_at > now_ms());
|
||||
drop(state);
|
||||
let started = std::time::Instant::now();
|
||||
assert!(catalog.index(&json!({})).is_none());
|
||||
assert!(started.elapsed() < Duration::from_millis(100));
|
||||
assert!(!catalog.shared.state.lock().unwrap().loading);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,262 @@
|
||||
use super::catalog_actions::{
|
||||
LaunchDecision, LaunchStatus, PlayabilityMetadata, account_decision, game_play_denial,
|
||||
paid_membership_decision, readiness_decision, requires_paid_membership, selected_variant,
|
||||
};
|
||||
use super::*;
|
||||
|
||||
const STORE_GAME_QUERY: &str = r#"query OpenNowStoreClientApps($vpcId: String!, $locale: String!) {
|
||||
apps(vpcId: $vpcId, language: $locale, filters: {
|
||||
type: {
|
||||
in: ["PLATFORM_CLIENT"]
|
||||
}
|
||||
}) {
|
||||
items {
|
||||
id
|
||||
title
|
||||
images {
|
||||
HERO_IMAGE
|
||||
}
|
||||
variants {
|
||||
appStore
|
||||
id
|
||||
gfn {
|
||||
status
|
||||
}
|
||||
}
|
||||
gfn {
|
||||
playType
|
||||
}
|
||||
}
|
||||
}
|
||||
}"#;
|
||||
|
||||
const SUPPORTED_STORES: [&str; 1] = ["STEAM"];
|
||||
|
||||
pub(super) fn store_launch_intent(params: &Value) -> Result<bool, ServiceError> {
|
||||
match params.get("storeLaunch") {
|
||||
None | Some(Value::Bool(false)) => Ok(false),
|
||||
Some(Value::Bool(true)) => Ok(true),
|
||||
Some(_) => Err(ServiceError::invalid("storeLaunch must be a boolean")),
|
||||
}
|
||||
}
|
||||
|
||||
fn requested_store(params: &Value) -> Result<&'static str, ServiceError> {
|
||||
match params.get("store") {
|
||||
None => Ok(SUPPORTED_STORES[0]),
|
||||
Some(Value::String(store)) if store == SUPPORTED_STORES[0] => Ok(SUPPORTED_STORES[0]),
|
||||
Some(Value::String(_)) => Err(ServiceError::invalid(
|
||||
"Only Steam store launches are supported",
|
||||
)),
|
||||
Some(_) => Err(ServiceError::invalid("store must be a string")),
|
||||
}
|
||||
}
|
||||
|
||||
fn launch_id(variant: &Value) -> Option<&str> {
|
||||
variant["id"]
|
||||
.as_str()
|
||||
.filter(|id| !id.is_empty() && id.len() <= 256)
|
||||
}
|
||||
|
||||
pub(super) fn eligible_store_target<'a>(
|
||||
games: &'a [Value],
|
||||
store: &str,
|
||||
) -> Option<(&'a Value, &'a Value)> {
|
||||
games.iter().find_map(|game| {
|
||||
let variant = game["variants"]
|
||||
.as_array()?
|
||||
.iter()
|
||||
.find(|variant| variant["store"] == store && launch_id(variant).is_some())?;
|
||||
Some((game, variant))
|
||||
})
|
||||
}
|
||||
|
||||
fn resolved_store_target(
|
||||
candidates: &[Value],
|
||||
store: &str,
|
||||
target: Option<(&str, &str)>,
|
||||
) -> Option<(Value, Value)> {
|
||||
match target {
|
||||
Some((app_id, variant_id)) => {
|
||||
let game = candidates.iter().find(|game| game["id"] == app_id)?;
|
||||
let variant = selected_variant(game, variant_id)?;
|
||||
Some((game.clone(), variant.clone()))
|
||||
}
|
||||
None => eligible_store_target(candidates, store)
|
||||
.map(|(game, variant)| (game.clone(), variant.clone())),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn store_launch_decision(
|
||||
game: &Value,
|
||||
app_id: &str,
|
||||
variant_id: &str,
|
||||
store: &str,
|
||||
subscription: &Value,
|
||||
access: &Value,
|
||||
exact: bool,
|
||||
) -> LaunchDecision {
|
||||
use LaunchStatus::*;
|
||||
let decide = |status, message| LaunchDecision { status, message };
|
||||
if game["id"] != app_id || variant_id.parse::<i32>().ok().is_none_or(|id| id <= 0) {
|
||||
return decide(
|
||||
MetadataUnconfirmed,
|
||||
"The exact store version could not be confirmed. Refresh and try again.",
|
||||
);
|
||||
}
|
||||
let Some(variant) = selected_variant(game, variant_id) else {
|
||||
return decide(
|
||||
MetadataUnconfirmed,
|
||||
"The selected store version is no longer available. Refresh and try again.",
|
||||
);
|
||||
};
|
||||
if variant["store"] != store {
|
||||
return decide(
|
||||
MetadataUnconfirmed,
|
||||
"The selected store version changed. Refresh and try again.",
|
||||
);
|
||||
}
|
||||
let playability = if exact {
|
||||
PlayabilityMetadata::Required
|
||||
} else {
|
||||
PlayabilityMetadata::WhenPresent
|
||||
};
|
||||
if let Some(decision) = readiness_decision(game, variant, playability) {
|
||||
return decision;
|
||||
}
|
||||
if !subscription["storageAddon"].is_object() {
|
||||
return decide(
|
||||
SubscriptionRequired,
|
||||
"Launching the Steam store for persistent game management requires the GeForce NOW persistent storage add-on.",
|
||||
);
|
||||
}
|
||||
if let Some(decision) = game_play_denial(subscription) {
|
||||
return decision;
|
||||
}
|
||||
if let Some(decision) = account_decision(variant, variant_id, access) {
|
||||
return decision;
|
||||
}
|
||||
if requires_paid_membership(game)
|
||||
&& let Some(decision) = paid_membership_decision(subscription)
|
||||
{
|
||||
return decision;
|
||||
}
|
||||
decide(
|
||||
Ready,
|
||||
"Ready to open the Steam store for persistent game management.",
|
||||
)
|
||||
}
|
||||
|
||||
impl GfnService {
|
||||
fn store_games(&self, settings: &Value) -> Result<Value, ServiceError> {
|
||||
self.authenticated_read(|session, generation| {
|
||||
let revision = self.catalog_revision.load(std::sync::atomic::Ordering::Acquire);
|
||||
let client = client_for_settings(&self.client, settings).map_err(ServiceError::invalid)?;
|
||||
let token = session
|
||||
.tokens
|
||||
.id_token
|
||||
.as_deref()
|
||||
.unwrap_or(&session.tokens.access_token);
|
||||
let vpc = self.vpc_id(
|
||||
&client,
|
||||
session,
|
||||
generation,
|
||||
settings,
|
||||
token,
|
||||
Some(&self.store_cache.requests),
|
||||
)?;
|
||||
let payload = self.catalog_document(
|
||||
&client,
|
||||
token,
|
||||
STORE_GAME_QUERY,
|
||||
json!({"vpcId":vpc,"locale":"en_US"}),
|
||||
)?;
|
||||
let games = complete_games(&payload["data"]["apps"]["items"])?;
|
||||
self.check_scope(session, generation)?;
|
||||
self.check_catalog_revision(revision)?;
|
||||
Ok(json!({"games":games,"catalogRevision":revision,"freshness":"fresh","fetchedAt":now_ms()}))
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn store_launch_inspect(
|
||||
&self,
|
||||
params: &Value,
|
||||
target: Option<(&str, &str)>,
|
||||
settings: &Value,
|
||||
) -> Result<Value, ServiceError> {
|
||||
let store = requested_store(params)?;
|
||||
let games = self.store_games(settings)?;
|
||||
let subscription = self.subscription(settings)?;
|
||||
if subscription["scope"] != games["scope"] {
|
||||
return Err(ServiceError {
|
||||
code: "stale_account",
|
||||
message: "The membership account changed.".into(),
|
||||
});
|
||||
}
|
||||
let candidates = games["games"].as_array().cloned().unwrap_or_default();
|
||||
let (app_id, variant_id, decision, game) = match resolved_store_target(
|
||||
&candidates,
|
||||
store,
|
||||
target,
|
||||
) {
|
||||
Some((game, variant)) => {
|
||||
let app_id = game["id"].as_str().unwrap_or_default().to_owned();
|
||||
let variant_id = variant["id"].as_str().unwrap_or_default().to_owned();
|
||||
let access = self.account_connections(settings)?;
|
||||
if access["scope"] != games["scope"] {
|
||||
return Err(ServiceError {
|
||||
code: "stale_account",
|
||||
message: "The store account changed.".into(),
|
||||
});
|
||||
}
|
||||
let detail = self.catalog_game(&json!({"appId":app_id}), settings);
|
||||
let (metadata, exact) = match detail {
|
||||
Ok(detail) => (detail["game"].clone(), true),
|
||||
Err(error) if error.code == "catalog_game_not_found" => (game, false),
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
let decision = store_launch_decision(
|
||||
&metadata,
|
||||
&app_id,
|
||||
&variant_id,
|
||||
store,
|
||||
&subscription["subscription"],
|
||||
&access,
|
||||
exact,
|
||||
);
|
||||
(app_id, variant_id, decision, metadata)
|
||||
}
|
||||
None => match target {
|
||||
Some((app_id, variant_id)) => (
|
||||
app_id.to_owned(),
|
||||
variant_id.to_owned(),
|
||||
LaunchDecision {
|
||||
status: LaunchStatus::MetadataUnconfirmed,
|
||||
message: "The exact store version could not be confirmed. Refresh and try again.",
|
||||
},
|
||||
Value::Null,
|
||||
),
|
||||
None => (
|
||||
String::new(),
|
||||
String::new(),
|
||||
LaunchDecision {
|
||||
status: LaunchStatus::Unavailable,
|
||||
message: "No Steam store launch is available for this account.",
|
||||
},
|
||||
Value::Null,
|
||||
),
|
||||
},
|
||||
};
|
||||
let mut result = json!({
|
||||
"store":store,
|
||||
"appId":if app_id.is_empty() { Value::Null } else { json!(app_id) },
|
||||
"variantId":if variant_id.is_empty() { Value::Null } else { json!(variant_id) },
|
||||
"game":game,
|
||||
"decision":decision,
|
||||
"catalogRevision":games["catalogRevision"],
|
||||
"fetchedAt":games["fetchedAt"],
|
||||
"freshness":games["freshness"],
|
||||
});
|
||||
result["scope"] = games["scope"].clone();
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,736 @@
|
||||
use super::catalog_actions::LaunchStatus;
|
||||
use super::tests::{auth_fixture, mock_requests, test_service};
|
||||
use super::*;
|
||||
|
||||
fn service(url: &str) -> (GfnService, PathBuf) {
|
||||
let (mut service, path) = test_service(url);
|
||||
service.endpoints.graphql = format!("{url}/graphql");
|
||||
service.endpoints.account_linking = format!("{url}/graphql");
|
||||
service.endpoints.subscription = format!("{url}/subscription");
|
||||
service.endpoints.server_info = Some(format!("{url}/vpc"));
|
||||
let mut state = service.state.lock().unwrap();
|
||||
state.session = Some(auth_fixture("fixture-account"));
|
||||
state.restore_attempted = true;
|
||||
drop(state);
|
||||
(service, path)
|
||||
}
|
||||
|
||||
fn vpc() -> (u16, Value) {
|
||||
(200, json!({"requestStatus":{"serverId":"fixture-vpc"}}))
|
||||
}
|
||||
|
||||
fn platform_client(
|
||||
id: &str,
|
||||
title: &str,
|
||||
app_store: &str,
|
||||
variant_id: &str,
|
||||
status: Value,
|
||||
) -> Value {
|
||||
json!({"id":id,"title":title,
|
||||
"images":{"HERO_IMAGE":[{"url":format!("https://cdn.example.invalid/{id}.jpg"),"width":1920,"height":1080}]},
|
||||
"variants":[{"appStore":app_store,"id":variant_id,"gfn":{"status":status}}],
|
||||
"gfn":{"playType":"PLATFORM_CLIENT"}})
|
||||
}
|
||||
|
||||
fn store_apps(items: Vec<Value>) -> (u16, Value) {
|
||||
(200, json!({"data":{"apps":{"items":items}}}))
|
||||
}
|
||||
|
||||
fn subscription(addon: bool) -> (u16, Value) {
|
||||
let addons = if addon {
|
||||
json!([{"type":"STORAGE","subType":"PERMANENT_STORAGE","status":"OK",
|
||||
"attributes":[{"key":"TOTAL_STORAGE_SIZE_IN_GB","textValue":"512"}]}])
|
||||
} else {
|
||||
json!([])
|
||||
};
|
||||
(
|
||||
200,
|
||||
json!({"allottedTimeInMinutes":600.0,"purchasedTimeInMinutes":0.0,"rolledOverTimeInMinutes":0.0,
|
||||
"totalTimeInMinutes":600.0,"remainingTimeInMinutes":500.0,"membershipTier":"ULTIMATE",
|
||||
"type":"SUBSCRIPTION","subType":"UNLIMITED","addons":addons,
|
||||
"features":{"resolutions":[{"isEntitled":true,"widthInPixels":1920,"heightInPixels":1080,"framesPerSecond":60}]},
|
||||
"currentSubscriptionState":{"state":"ACTIVE","isGamePlayAllowed":true},
|
||||
"notifications":{"notifyUserWhenTimeRemainingInMinutes":15,"notifyUserOnSessionWhenRemainingTimeInMinutes":5},
|
||||
"firstEntitlementStartDateTime":"2026-01-01T00:00:00Z",
|
||||
"currentSpanStartDateTime":"2026-01-01T00:00:00Z",
|
||||
"currentSpanEndDateTime":"2026-02-01T00:00:00Z"}),
|
||||
)
|
||||
}
|
||||
|
||||
fn store_definitions() -> Vec<(u16, Value)> {
|
||||
vec![
|
||||
(
|
||||
200,
|
||||
json!({"data":{"appStoreDefinitions":[{"store":"STEAM","label":"Steam","features":[],
|
||||
"accountLinkingMetadata":{"isRequired":false,"isSupported":true,"supportedVariantIds":[]}}]}}),
|
||||
),
|
||||
(200, json!({"data":{"genreDefinitions":[]}})),
|
||||
(200, json!({"data":{"subscriptionDefinitions":[]}})),
|
||||
(
|
||||
200,
|
||||
json!({"data":{"userAccount":{"storesData":[{"store":"STEAM","isConnected":true,"status":"connected"}],"subscriptions":[]}}}),
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
fn detail_game(app_id: &str, variant_id: &str, playability: &str) -> (u16, Value) {
|
||||
(
|
||||
200,
|
||||
json!({"data":{"apps":{"items":[{"id":app_id,"title":"Steam",
|
||||
"gfn":{"playabilityState":playability},"variants":[{"id":variant_id,"appStore":"STEAM",
|
||||
"gfn":{"status":"AVAILABLE","library":{"status":"MANUAL","selected":true,"playStatus":"PLAYABLE"}}}]}]}}}),
|
||||
)
|
||||
}
|
||||
|
||||
fn no_game() -> (u16, Value) {
|
||||
(200, json!({"data":{"apps":{"items":[]}}}))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_launch_discovers_the_eligible_steam_client_and_carries_the_exact_target() {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
store_apps(vec![
|
||||
platform_client(
|
||||
"platform-steam",
|
||||
"Steam",
|
||||
"STEAM",
|
||||
"12345",
|
||||
json!("AVAILABLE"),
|
||||
),
|
||||
platform_client("platform-epic", "Epic", "EPIC", "999", json!("AVAILABLE")),
|
||||
]),
|
||||
subscription(true),
|
||||
store_definitions()[0].clone(),
|
||||
store_definitions()[1].clone(),
|
||||
store_definitions()[2].clone(),
|
||||
store_definitions()[3].clone(),
|
||||
detail_game("platform-steam", "12345", "PLAYABLE"),
|
||||
],
|
||||
|index, request| match index {
|
||||
0 => assert!(request.starts_with("GET /vpc ")),
|
||||
1 => {
|
||||
let payload: Value =
|
||||
serde_json::from_str(request.split("\r\n\r\n").nth(1).unwrap()).unwrap();
|
||||
let query = payload["query"].as_str().unwrap();
|
||||
assert!(query.contains("PLATFORM_CLIENT"));
|
||||
assert!(query.contains("appStore"));
|
||||
assert!(query.contains("HERO_IMAGE"));
|
||||
assert!(query.contains("filters"));
|
||||
assert!(!query.contains("appIds"));
|
||||
}
|
||||
2 => assert!(request.starts_with("GET /subscription")),
|
||||
7 => {
|
||||
let payload: Value =
|
||||
serde_json::from_str(request.split("\r\n\r\n").nth(1).unwrap()).unwrap();
|
||||
assert_eq!(payload["variables"]["ids"], json!(["platform-steam"]));
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service
|
||||
.store_launch_inspect(&json!({}), None, &json!({}))
|
||||
.unwrap();
|
||||
assert_eq!(result["store"], "STEAM");
|
||||
assert_eq!(result["appId"], "platform-steam");
|
||||
assert_eq!(result["variantId"], "12345");
|
||||
assert_eq!(result["game"]["id"], "platform-steam");
|
||||
assert_eq!(result["game"]["title"], "Steam");
|
||||
assert_eq!(result["decision"]["status"], "ready");
|
||||
assert_eq!(result["catalogRevision"], 0);
|
||||
assert_eq!(result["freshness"], "fresh");
|
||||
assert_eq!(result["scope"]["userId"], "fixture-account");
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_launch_requires_the_persistent_storage_addon() {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
store_apps(vec![platform_client(
|
||||
"platform-steam",
|
||||
"Steam",
|
||||
"STEAM",
|
||||
"12345",
|
||||
json!("AVAILABLE"),
|
||||
)]),
|
||||
subscription(false),
|
||||
store_definitions()[0].clone(),
|
||||
store_definitions()[1].clone(),
|
||||
store_definitions()[2].clone(),
|
||||
store_definitions()[3].clone(),
|
||||
detail_game("platform-steam", "12345", "PLAYABLE"),
|
||||
],
|
||||
|_, _| {},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service
|
||||
.store_launch_inspect(&json!({}), None, &json!({}))
|
||||
.unwrap();
|
||||
assert_eq!(result["decision"]["status"], "subscription_required");
|
||||
assert_eq!(result["appId"], "platform-steam");
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_launch_ephemeral_only_storage_does_not_authorize_the_store_launch() {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
store_apps(vec![platform_client(
|
||||
"platform-steam",
|
||||
"Steam",
|
||||
"STEAM",
|
||||
"12345",
|
||||
json!("AVAILABLE"),
|
||||
)]),
|
||||
(
|
||||
200,
|
||||
json!({"allottedTimeInMinutes":600.0,"membershipTier":"ULTIMATE","type":"SUBSCRIPTION",
|
||||
"subType":"UNLIMITED","addons":[{"type":"STORAGE","subType":"EPHEMERAL_STORAGE","status":"OK"}],
|
||||
"features":{"resolutions":[]},"currentSubscriptionState":{"state":"ACTIVE","isGamePlayAllowed":true},
|
||||
"notifications":{}}),
|
||||
),
|
||||
store_definitions()[0].clone(),
|
||||
store_definitions()[1].clone(),
|
||||
store_definitions()[2].clone(),
|
||||
store_definitions()[3].clone(),
|
||||
detail_game("platform-steam", "12345", "PLAYABLE"),
|
||||
],
|
||||
|_, _| {},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service
|
||||
.store_launch_inspect(&json!({}), None, &json!({}))
|
||||
.unwrap();
|
||||
assert_eq!(result["decision"]["status"], "subscription_required");
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_launch_reports_unavailable_without_an_eligible_steam_variant() {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
store_apps(vec![platform_client(
|
||||
"platform-epic",
|
||||
"Epic",
|
||||
"EPIC",
|
||||
"999",
|
||||
json!("AVAILABLE"),
|
||||
)]),
|
||||
subscription(true),
|
||||
],
|
||||
|index, _| assert!(index < 3),
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service
|
||||
.store_launch_inspect(&json!({}), None, &json!({}))
|
||||
.unwrap();
|
||||
assert_eq!(result["decision"]["status"], "unavailable");
|
||||
assert!(result["appId"].is_null());
|
||||
assert!(result["variantId"].is_null());
|
||||
assert!(result["game"].is_null());
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_launch_reports_unavailable_without_platform_clients() {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![vpc(), store_apps(vec![]), subscription(true)],
|
||||
|_, _| {},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service
|
||||
.store_launch_inspect(&json!({}), None, &json!({}))
|
||||
.unwrap();
|
||||
assert_eq!(result["decision"]["status"], "unavailable");
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_launch_rejects_unsupported_stores_and_non_boolean_intent() {
|
||||
let (url, worker) = mock_requests(vec![], |_, _| {});
|
||||
let (service, path) = service(&url);
|
||||
assert_eq!(
|
||||
service
|
||||
.store_launch_inspect(&json!({"store":"EPIC"}), None, &json!({}))
|
||||
.unwrap_err()
|
||||
.code,
|
||||
"invalid_params"
|
||||
);
|
||||
assert_eq!(
|
||||
service
|
||||
.store_launch_inspect(&json!({"store":7}), None, &json!({}))
|
||||
.unwrap_err()
|
||||
.code,
|
||||
"invalid_params"
|
||||
);
|
||||
assert_eq!(
|
||||
store_launch::store_launch_intent(&json!({"storeLaunch":"yes"}))
|
||||
.unwrap_err()
|
||||
.code,
|
||||
"invalid_params"
|
||||
);
|
||||
assert!(!store_launch::store_launch_intent(&json!({})).unwrap());
|
||||
assert!(!store_launch::store_launch_intent(&json!({"storeLaunch":false})).unwrap());
|
||||
assert!(store_launch::store_launch_intent(&json!({"storeLaunch":true})).unwrap());
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_launch_enforces_variant_readiness_from_the_discovery_source() {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
store_apps(vec![platform_client(
|
||||
"platform-steam",
|
||||
"Steam",
|
||||
"STEAM",
|
||||
"12345",
|
||||
json!("PATCHING"),
|
||||
)]),
|
||||
subscription(true),
|
||||
store_definitions()[0].clone(),
|
||||
store_definitions()[1].clone(),
|
||||
store_definitions()[2].clone(),
|
||||
store_definitions()[3].clone(),
|
||||
no_game(),
|
||||
],
|
||||
|_, _| {},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service
|
||||
.store_launch_inspect(&json!({}), None, &json!({}))
|
||||
.unwrap();
|
||||
assert_eq!(result["decision"]["status"], "patching");
|
||||
assert_eq!(result["variantId"], "12345");
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_launch_reports_unconfirmed_readiness_when_the_status_is_missing() {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
store_apps(vec![
|
||||
json!({"id":"platform-steam","title":"Steam","images":{},
|
||||
"variants":[{"appStore":"STEAM","id":"12345","gfn":{}}],"gfn":{"playType":"PLATFORM_CLIENT"}}),
|
||||
]),
|
||||
subscription(true),
|
||||
store_definitions()[0].clone(),
|
||||
store_definitions()[1].clone(),
|
||||
store_definitions()[2].clone(),
|
||||
store_definitions()[3].clone(),
|
||||
no_game(),
|
||||
],
|
||||
|_, _| {},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service
|
||||
.store_launch_inspect(&json!({}), None, &json!({}))
|
||||
.unwrap();
|
||||
assert_eq!(result["decision"]["status"], "metadata_unconfirmed");
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_launch_falls_back_to_discovery_metadata_when_the_exact_game_is_not_browsable() {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
store_apps(vec![platform_client(
|
||||
"platform-steam",
|
||||
"Steam",
|
||||
"STEAM",
|
||||
"12345",
|
||||
json!("AVAILABLE"),
|
||||
)]),
|
||||
subscription(true),
|
||||
store_definitions()[0].clone(),
|
||||
store_definitions()[1].clone(),
|
||||
store_definitions()[2].clone(),
|
||||
store_definitions()[3].clone(),
|
||||
no_game(),
|
||||
],
|
||||
|_, _| {},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service
|
||||
.store_launch_inspect(&json!({}), None, &json!({}))
|
||||
.unwrap();
|
||||
assert_eq!(result["decision"]["status"], "ready");
|
||||
assert_eq!(result["appId"], "platform-steam");
|
||||
assert_eq!(result["variantId"], "12345");
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_launch_blocks_explicit_unplayability_from_the_exact_resolution() {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
store_apps(vec![platform_client(
|
||||
"platform-steam",
|
||||
"Steam",
|
||||
"STEAM",
|
||||
"12345",
|
||||
json!("AVAILABLE"),
|
||||
)]),
|
||||
subscription(true),
|
||||
store_definitions()[0].clone(),
|
||||
store_definitions()[1].clone(),
|
||||
store_definitions()[2].clone(),
|
||||
store_definitions()[3].clone(),
|
||||
detail_game("platform-steam", "12345", "UNPLAYABLE_DUE_TO_UPGRADE"),
|
||||
],
|
||||
|_, _| {},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service
|
||||
.store_launch_inspect(&json!({}), None, &json!({}))
|
||||
.unwrap();
|
||||
assert_eq!(result["decision"]["status"], "subscription_required");
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_launch_requires_a_linked_store_account_when_the_store_requires_it() {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
store_apps(vec![platform_client(
|
||||
"platform-steam",
|
||||
"Steam",
|
||||
"STEAM",
|
||||
"12345",
|
||||
json!("AVAILABLE"),
|
||||
)]),
|
||||
subscription(true),
|
||||
(
|
||||
200,
|
||||
json!({"data":{"appStoreDefinitions":[{"store":"STEAM","label":"Steam","features":[],
|
||||
"accountLinkingMetadata":{"isRequired":true,"isSupported":true,"supportedVariantIds":["12345"]}}]}}),
|
||||
),
|
||||
store_definitions()[1].clone(),
|
||||
store_definitions()[2].clone(),
|
||||
(
|
||||
200,
|
||||
json!({"data":{"userAccount":{"storesData":[{"store":"STEAM","isConnected":false,"status":"disconnected"}],"subscriptions":[]}}}),
|
||||
),
|
||||
detail_game("platform-steam", "12345", "PLAYABLE"),
|
||||
],
|
||||
|_, _| {},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service
|
||||
.store_launch_inspect(&json!({}), None, &json!({}))
|
||||
.unwrap();
|
||||
assert_eq!(result["decision"]["status"], "link_required");
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn targeted_store_launch_requires_the_exact_platform_client_and_variant() {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
store_apps(vec![platform_client(
|
||||
"platform-steam",
|
||||
"Steam",
|
||||
"STEAM",
|
||||
"12345",
|
||||
json!("AVAILABLE"),
|
||||
)]),
|
||||
subscription(true),
|
||||
],
|
||||
|_, _| {},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service
|
||||
.store_launch_inspect(&json!({}), Some(("platform-epic", "999")), &json!({}))
|
||||
.unwrap();
|
||||
assert_eq!(result["decision"]["status"], "metadata_unconfirmed");
|
||||
assert_eq!(result["appId"], "platform-epic");
|
||||
assert_eq!(result["variantId"], "999");
|
||||
assert!(result["game"].is_null());
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_launch_requires_an_identifier_and_the_supported_store_for_the_exact_variant() {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
store_apps(vec![
|
||||
json!({"id":"platform-steam","title":"Steam","images":{},
|
||||
"variants":[{"appStore":"STEAM","id":"not-numeric","gfn":{"status":"AVAILABLE"}}],
|
||||
"gfn":{"playType":"PLATFORM_CLIENT"}}),
|
||||
]),
|
||||
subscription(true),
|
||||
store_definitions()[0].clone(),
|
||||
store_definitions()[1].clone(),
|
||||
store_definitions()[2].clone(),
|
||||
store_definitions()[3].clone(),
|
||||
no_game(),
|
||||
],
|
||||
|_, _| {},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service
|
||||
.store_launch_inspect(&json!({}), None, &json!({}))
|
||||
.unwrap();
|
||||
assert_eq!(result["decision"]["status"], "metadata_unconfirmed");
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_launch_rejects_a_changed_membership_context() {
|
||||
let (entered_tx, entered_rx) = std::sync::mpsc::channel();
|
||||
let (release_tx, release_rx) = std::sync::mpsc::channel();
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
store_apps(vec![platform_client(
|
||||
"platform-steam",
|
||||
"Steam",
|
||||
"STEAM",
|
||||
"12345",
|
||||
json!("AVAILABLE"),
|
||||
)]),
|
||||
subscription(true),
|
||||
],
|
||||
move |index, _| {
|
||||
if index != 2 {
|
||||
return;
|
||||
}
|
||||
entered_tx.send(()).unwrap();
|
||||
release_rx
|
||||
.recv_timeout(std::time::Duration::from_secs(5))
|
||||
.unwrap();
|
||||
},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
std::thread::scope(|threads| {
|
||||
let pending = threads.spawn(|| service.store_launch_inspect(&json!({}), None, &json!({})));
|
||||
entered_rx
|
||||
.recv_timeout(std::time::Duration::from_secs(5))
|
||||
.unwrap();
|
||||
service.state.lock().unwrap().generation += 1;
|
||||
release_tx.send(()).unwrap();
|
||||
assert_eq!(pending.join().unwrap().unwrap_err().code, "stale_account");
|
||||
});
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_launch_never_borrows_a_registered_launch_target() {
|
||||
let game = json!({"id":"platform-steam","title":"Steam",
|
||||
"variants":[{"id":"12345","store":"STEAM","gfnStatus":"AVAILABLE"}]});
|
||||
let subscription =
|
||||
json!({"storageAddon":{"type":"PERMANENT_STORAGE"},"isGamePlayAllowed":true});
|
||||
let access = json!({"definitions":{"stores":{"status":"success"}},
|
||||
"accounts":[{"provider":"STEAM","isRequired":false,"supportsLinking":true,"isConnected":true}],
|
||||
"subscriptions":[]});
|
||||
assert_eq!(
|
||||
store_launch::store_launch_decision(
|
||||
&game,
|
||||
"platform-steam",
|
||||
"999",
|
||||
"STEAM",
|
||||
&subscription,
|
||||
&access,
|
||||
false,
|
||||
)
|
||||
.status,
|
||||
LaunchStatus::MetadataUnconfirmed
|
||||
);
|
||||
assert_eq!(
|
||||
store_launch::store_launch_decision(
|
||||
&game,
|
||||
"other-app",
|
||||
"12345",
|
||||
"STEAM",
|
||||
&subscription,
|
||||
&access,
|
||||
false,
|
||||
)
|
||||
.status,
|
||||
LaunchStatus::MetadataUnconfirmed
|
||||
);
|
||||
let epic_variant = json!({"id":"999","store":"EPIC","gfnStatus":"AVAILABLE"});
|
||||
let games = vec![
|
||||
json!({"id":"platform-epic","variants":[epic_variant.clone()]}),
|
||||
json!({"id":"platform-steam","variants":game["variants"].clone()}),
|
||||
];
|
||||
let (owner, variant) =
|
||||
store_launch::eligible_store_target(&games, "STEAM").expect("steam client present");
|
||||
assert_eq!(owner["id"], "platform-steam");
|
||||
assert_eq!(variant["id"], "12345");
|
||||
assert!(store_launch::eligible_store_target(&games, "XBOX").is_none());
|
||||
assert_eq!(
|
||||
store_launch::store_launch_decision(
|
||||
&game,
|
||||
"platform-steam",
|
||||
"12345",
|
||||
"EPIC",
|
||||
&subscription,
|
||||
&access,
|
||||
false,
|
||||
)
|
||||
.status,
|
||||
LaunchStatus::MetadataUnconfirmed
|
||||
);
|
||||
assert_eq!(
|
||||
store_launch::store_launch_decision(
|
||||
&game,
|
||||
"platform-steam",
|
||||
"12345",
|
||||
"STEAM",
|
||||
&subscription,
|
||||
&access,
|
||||
false,
|
||||
)
|
||||
.status,
|
||||
LaunchStatus::Ready
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_launch_preserves_explicit_gameplay_denial_without_a_membership_tier() {
|
||||
let (_, mut subscription) = subscription(true);
|
||||
subscription["currentSubscriptionState"]["isGamePlayAllowed"] = json!(false);
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
store_apps(vec![platform_client(
|
||||
"platform-steam",
|
||||
"Steam",
|
||||
"STEAM",
|
||||
"12345",
|
||||
json!("AVAILABLE"),
|
||||
)]),
|
||||
(200, subscription),
|
||||
store_definitions()[0].clone(),
|
||||
store_definitions()[1].clone(),
|
||||
store_definitions()[2].clone(),
|
||||
store_definitions()[3].clone(),
|
||||
no_game(),
|
||||
],
|
||||
|_, _| {},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service
|
||||
.store_launch_inspect(&json!({}), None, &json!({}))
|
||||
.unwrap();
|
||||
assert_eq!(result["decision"]["status"], "subscription_required");
|
||||
assert_eq!(result["appId"], "platform-steam");
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_launch_propagates_detail_failures_that_are_not_missing_games() {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
store_apps(vec![platform_client(
|
||||
"platform-steam",
|
||||
"Steam",
|
||||
"STEAM",
|
||||
"12345",
|
||||
json!("AVAILABLE"),
|
||||
)]),
|
||||
subscription(true),
|
||||
store_definitions()[0].clone(),
|
||||
store_definitions()[1].clone(),
|
||||
store_definitions()[2].clone(),
|
||||
store_definitions()[3].clone(),
|
||||
(503, json!({})),
|
||||
],
|
||||
|_, _| {},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
let result = service.store_launch_inspect(&json!({}), None, &json!({}));
|
||||
assert_eq!(result.unwrap_err().code, "upstream_error");
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_launch_preserves_the_active_seat_guard() {
|
||||
let (url, worker) = mock_requests(
|
||||
vec![
|
||||
vpc(),
|
||||
store_apps(vec![platform_client(
|
||||
"platform-steam",
|
||||
"Steam",
|
||||
"STEAM",
|
||||
"12345",
|
||||
json!("AVAILABLE"),
|
||||
)]),
|
||||
subscription(true),
|
||||
store_definitions()[0].clone(),
|
||||
store_definitions()[1].clone(),
|
||||
store_definitions()[2].clone(),
|
||||
store_definitions()[3].clone(),
|
||||
detail_game("platform-steam", "12345", "PLAYABLE"),
|
||||
],
|
||||
|_, _| {},
|
||||
);
|
||||
let (service, path) = service(&url);
|
||||
service
|
||||
.cloudmatch
|
||||
.seed_owned_session(json!({"sessionId":"owned-seat","status":3}));
|
||||
let result = service.create_session(
|
||||
&json!({"appId":"12345","variantId":"12345","catalogAppId":"platform-steam",
|
||||
"storeLaunch":true,
|
||||
"scope":scoped_result(json!({}), &auth_fixture("fixture-account"), 0)["scope"]}),
|
||||
&json!({}),
|
||||
|settings| Ok(settings.clone()),
|
||||
);
|
||||
assert_eq!(result.unwrap_err().code, "session_update_busy");
|
||||
assert_eq!(
|
||||
service.cloudmatch.active()["session"]["sessionId"],
|
||||
"owned-seat"
|
||||
);
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subscription_reports_missing_remaining_playtime_as_unknown() {
|
||||
let (_, mut missing) = subscription(false);
|
||||
missing
|
||||
.as_object_mut()
|
||||
.unwrap()
|
||||
.remove("remainingTimeInMinutes");
|
||||
let (url, worker) = mock_requests(vec![vpc(), subscription(false), (200, missing)], |_, _| {});
|
||||
let (service, path) = service(&url);
|
||||
let known = service.subscription(&json!({})).unwrap();
|
||||
assert_eq!(known["subscription"]["remainingHours"], json!(500.0 / 60.0));
|
||||
assert_eq!(known["subscription"]["usedHours"], json!(100.0 / 60.0));
|
||||
let unknown = service.subscription(&json!({})).unwrap();
|
||||
assert_eq!(unknown["subscription"]["remainingHours"], Value::Null);
|
||||
assert_eq!(unknown["subscription"]["usedHours"], Value::Null);
|
||||
assert_eq!(unknown["subscription"]["totalHours"], json!(10.0));
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
use serde_json::{Value, json};
|
||||
|
||||
const KEYBOARDS: &[(&str, &str, &[&str])] = &[
|
||||
("en-US", "English (US)", &[]),
|
||||
("en-GB", "English (UK)", &[]),
|
||||
("tr-TR", "Turkish Q", &[]),
|
||||
("de-DE", "German", &[]),
|
||||
("fr-FR", "French", &[]),
|
||||
("es-ES_tradnl", "Spanish (traditional)", &["es-ES"]),
|
||||
("es-MX", "Spanish (Latin America)", &[]),
|
||||
("it-IT", "Italian", &[]),
|
||||
("pt-PT", "Portuguese (Portugal)", &[]),
|
||||
("pt-BR", "Portuguese (Brazil)", &[]),
|
||||
("pl-PL", "Polish", &[]),
|
||||
("da-DK", "Danish", &[]),
|
||||
("nb-NO", "Norwegian", &[]),
|
||||
("sv-SE", "Swedish", &[]),
|
||||
("fi-FI", "Finnish", &[]),
|
||||
("ru-RU", "Russian", &[]),
|
||||
("uk-UA", "Ukrainian", &[]),
|
||||
("ja-106", "Japanese 106", &["ja-JP", "Japanese106"]),
|
||||
("ko-KR", "Korean", &[]),
|
||||
("zh-CN", "Chinese (Simplified)", &[]),
|
||||
("zh-TW", "Chinese (Traditional)", &[]),
|
||||
];
|
||||
|
||||
pub fn valid_game_language(value: &str) -> bool {
|
||||
if value.len() > 64 || matches!(value.to_ascii_lowercase().as_str(), "auto" | "system") {
|
||||
return false;
|
||||
}
|
||||
let mut parts = value.split(['-', '_']);
|
||||
let first = parts.next().unwrap_or_default();
|
||||
(2..=8).contains(&first.len())
|
||||
&& first.bytes().all(|byte| byte.is_ascii_alphabetic())
|
||||
&& parts.all(|part| {
|
||||
(1..=8).contains(&part.len()) && part.bytes().all(|byte| byte.is_ascii_alphanumeric())
|
||||
})
|
||||
}
|
||||
|
||||
fn keyboard_wire_id(value: &str) -> Option<&'static str> {
|
||||
KEYBOARDS
|
||||
.iter()
|
||||
.find_map(|(id, _, aliases)| (*id == value || aliases.contains(&value)).then_some(*id))
|
||||
}
|
||||
|
||||
pub fn validate_setting(key: &str, value: &Value) -> Result<(), String> {
|
||||
let valid = value.as_str().is_some_and(|value| match key {
|
||||
"gameLanguage" => valid_game_language(value),
|
||||
"keyboardLayout" => keyboard_wire_id(value).is_some(),
|
||||
_ => true,
|
||||
});
|
||||
if valid {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!("Invalid {key} identifier"))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn keyboard_choices() -> Value {
|
||||
json!(
|
||||
KEYBOARDS
|
||||
.iter()
|
||||
.map(|(id, label, aliases)| { json!({"value":id, "label":label, "aliases":aliases}) })
|
||||
.collect::<Vec<_>>()
|
||||
)
|
||||
}
|
||||
|
||||
pub fn session_keyboard_layout(settings: &Value) -> &'static str {
|
||||
settings["keyboardLayout"]
|
||||
.as_str()
|
||||
.and_then(keyboard_wire_id)
|
||||
.unwrap_or("en-US")
|
||||
}
|
||||
|
||||
pub fn append_session_preferences(url: &mut url::Url, settings: &Value) {
|
||||
let language = settings["gameLanguage"]
|
||||
.as_str()
|
||||
.filter(|value| valid_game_language(value))
|
||||
.unwrap_or("en_US");
|
||||
url.query_pairs_mut()
|
||||
.append_pair("keyboardLayout", session_keyboard_layout(settings))
|
||||
.append_pair("languageCode", language);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn game_identifiers_preserve_wire_spelling_and_allow_safe_future_values() {
|
||||
for value in [
|
||||
"en_US",
|
||||
"es_419",
|
||||
"zh_Hant_TW",
|
||||
"sr-Latn-RS",
|
||||
"future_001",
|
||||
"EN-us",
|
||||
] {
|
||||
assert!(valid_game_language(value), "{value}");
|
||||
}
|
||||
for value in [
|
||||
"", "auto", "AUTO", "system", "en US", "en\nUS", "en__US", "en/US", "1en", "en_", "éé",
|
||||
] {
|
||||
assert!(!valid_game_language(value), "{value:?}");
|
||||
}
|
||||
assert!(!valid_game_language(&format!(
|
||||
"en{}",
|
||||
"_abcdefgh".repeat(8)
|
||||
)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keyboard_table_and_legacy_aliases_have_one_wire_identity() {
|
||||
for (id, _, aliases) in KEYBOARDS {
|
||||
assert_eq!(keyboard_wire_id(id), Some(*id));
|
||||
for alias in *aliases {
|
||||
assert_eq!(keyboard_wire_id(alias), Some(*id));
|
||||
}
|
||||
}
|
||||
assert_eq!(keyboard_wire_id("ja-JP"), Some("ja-106"));
|
||||
assert_eq!(keyboard_wire_id("Japanese106"), Some("ja-106"));
|
||||
assert_eq!(keyboard_wire_id("es-ES"), Some("es-ES_tradnl"));
|
||||
for value in ["auto", "system", "en_US", "m-us", "unknown"] {
|
||||
assert!(keyboard_wire_id(value).is_none());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn request_preferences_are_independent_and_corrupt_restore_is_safe() {
|
||||
for (settings, language, keyboard) in [
|
||||
(
|
||||
json!({"appLanguage":"ja", "gameLanguage":"es_419", "keyboardLayout":"de-DE"}),
|
||||
"es_419",
|
||||
"de-DE",
|
||||
),
|
||||
(
|
||||
json!({"gameLanguage":"zh_Hant_TW", "keyboardLayout":"ja-JP"}),
|
||||
"zh_Hant_TW",
|
||||
"ja-106",
|
||||
),
|
||||
(
|
||||
json!({"gameLanguage":"system", "keyboardLayout":"m-us"}),
|
||||
"en_US",
|
||||
"en-US",
|
||||
),
|
||||
(
|
||||
json!({"gameLanguage":"auto", "keyboardLayout":"es-ES"}),
|
||||
"en_US",
|
||||
"es-ES_tradnl",
|
||||
),
|
||||
] {
|
||||
let mut url = url::Url::parse("https://fixture.invalid/session?existing=1").unwrap();
|
||||
append_session_preferences(&mut url, &settings);
|
||||
let pairs: std::collections::HashMap<_, _> = url.query_pairs().collect();
|
||||
assert_eq!(pairs["languageCode"], language);
|
||||
assert_eq!(pairs["keyboardLayout"], keyboard);
|
||||
assert_eq!(pairs["existing"], "1");
|
||||
assert_eq!(pairs.len(), 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
pub mod push;
|
||||
pub mod update_apply;
|
||||
+1735
File diff suppressed because it is too large.
Load diff
+552
@@ -0,0 +1,552 @@
|
||||
use serde_json::{Value, json};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::env;
|
||||
use std::ffi::OsString;
|
||||
use std::fs;
|
||||
use std::io::{self, BufReader, Read};
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use url::Url;
|
||||
|
||||
const LIST_LIMIT: usize = 100;
|
||||
|
||||
const MEDIA_UNAVAILABLE: &str = "No pictures directory is available for the media library";
|
||||
|
||||
pub struct MediaService {
|
||||
root: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl MediaService {
|
||||
pub fn new() -> io::Result<Self> {
|
||||
let Some(root) = pictures_directory() else {
|
||||
return Ok(Self { root: None });
|
||||
};
|
||||
let root = root.join("OpenNOW");
|
||||
fs::create_dir_all(root.join("Screenshots"))?;
|
||||
fs::create_dir_all(root.join("Recordings"))?;
|
||||
Ok(Self { root: Some(root) })
|
||||
}
|
||||
|
||||
fn available_root(&self) -> Result<&PathBuf, String> {
|
||||
self.root
|
||||
.as_ref()
|
||||
.ok_or_else(|| MEDIA_UNAVAILABLE.to_owned())
|
||||
}
|
||||
|
||||
pub fn root(&self) -> Result<Value, String> {
|
||||
Ok(json!({ "path": self.available_root()?.to_string_lossy() }))
|
||||
}
|
||||
|
||||
pub fn recording_target(&self, params: &Value) -> Result<Value, String> {
|
||||
let title = params["gameTitle"]
|
||||
.as_str()
|
||||
.map(sanitized_title)
|
||||
.filter(|value| !value.is_empty())
|
||||
.unwrap_or_else(|| "opennow".to_owned());
|
||||
let timestamp = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_millis();
|
||||
let directory = self.directory_for_kind("recording")?;
|
||||
for suffix in 0_u16..=999 {
|
||||
let suffix = if suffix == 0 {
|
||||
String::new()
|
||||
} else {
|
||||
format!("-{suffix}")
|
||||
};
|
||||
let stem = format!("OpenNOW-{title}-{timestamp}{suffix}");
|
||||
let path = directory.join(format!("{stem}.mkv"));
|
||||
let part = directory.join(format!(".{stem}.mkv.part"));
|
||||
if !path.exists() && !part.exists() {
|
||||
return Ok(json!({
|
||||
"path":path.to_string_lossy(),
|
||||
"thumbnailPath":directory.join(format!("{stem}-thumb.jpg")).to_string_lossy(),
|
||||
}));
|
||||
}
|
||||
}
|
||||
Err("Could not allocate a unique recording file name".to_owned())
|
||||
}
|
||||
|
||||
pub fn validate_recording_target(&self, params: &Value) -> Result<Value, String> {
|
||||
let raw = params["outputPath"]
|
||||
.as_str()
|
||||
.filter(|value| !value.is_empty())
|
||||
.ok_or_else(|| "Native recording requires an outputPath".to_owned())?;
|
||||
let path = PathBuf::from(raw);
|
||||
let file_name = path
|
||||
.file_name()
|
||||
.and_then(|value| value.to_str())
|
||||
.filter(|value| value.starts_with("OpenNOW-") && !value.contains(".."))
|
||||
.ok_or_else(|| "Recording output file name is invalid".to_owned())?;
|
||||
if path.extension().and_then(|value| value.to_str()) != Some("mkv") || path.exists() {
|
||||
return Err("Recording output must be a new .mkv file".to_owned());
|
||||
}
|
||||
let recordings = self.directory_for_kind("recording")?;
|
||||
let expected_parent = recordings
|
||||
.canonicalize()
|
||||
.map_err(|error| format!("Could not validate recordings directory: {error}"))?;
|
||||
let actual_parent = path
|
||||
.parent()
|
||||
.ok_or_else(|| "Recording output has no parent directory".to_owned())?
|
||||
.canonicalize()
|
||||
.map_err(|error| format!("Could not validate recording output directory: {error}"))?;
|
||||
if actual_parent != expected_parent {
|
||||
return Err("Recording output is outside the OpenNOW recordings directory".to_owned());
|
||||
}
|
||||
let part = recordings.join(format!(".{file_name}.part"));
|
||||
if part.exists() {
|
||||
return Err("Recording output is already being written".to_owned());
|
||||
}
|
||||
Ok(json!({"outputPath":path.to_string_lossy()}))
|
||||
}
|
||||
|
||||
pub fn list(&self, params: &Value) -> Result<Value, String> {
|
||||
let filter = params["gameTitle"]
|
||||
.as_str()
|
||||
.map(sanitized_title)
|
||||
.filter(|value| !value.is_empty());
|
||||
let mut entries = Vec::new();
|
||||
self.collect("screenshot", "Screenshots", &mut entries)?;
|
||||
self.collect("recording", "Recordings", &mut entries)?;
|
||||
if let Some(filter) = filter {
|
||||
entries.retain(|entry| {
|
||||
entry["fileName"]
|
||||
.as_str()
|
||||
.is_some_and(|name| name.to_ascii_lowercase().contains(&filter))
|
||||
});
|
||||
}
|
||||
entries.sort_by_key(|entry| std::cmp::Reverse(entry["createdAtMs"].as_u64().unwrap_or(0)));
|
||||
entries.truncate(LIST_LIMIT);
|
||||
let screenshot_count = entries
|
||||
.iter()
|
||||
.filter(|entry| entry["kind"] == "screenshot")
|
||||
.count();
|
||||
let recording_count = entries.len() - screenshot_count;
|
||||
Ok(json!({
|
||||
"items": entries,
|
||||
"screenshots": screenshot_count,
|
||||
"recordings": recording_count,
|
||||
"rootPath": self.available_root()?.to_string_lossy()
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn acceptance_evidence(&self, since_ms: Option<u128>) -> Result<Value, String> {
|
||||
let listed = self.list(&json!({}))?;
|
||||
let items = listed["items"]
|
||||
.as_array()
|
||||
.ok_or_else(|| "Media listing did not contain items".to_owned())?;
|
||||
let after_start = |item: &&Value| {
|
||||
since_ms.is_none_or(|minimum| {
|
||||
u128::from(item["createdAtMs"].as_u64().unwrap_or_default()) >= minimum
|
||||
})
|
||||
};
|
||||
let screenshot = items
|
||||
.iter()
|
||||
.filter(|item| item["kind"] == "screenshot")
|
||||
.find(after_start)
|
||||
.map(|item| self.evidence_for_item(item))
|
||||
.transpose()?;
|
||||
let recording = items
|
||||
.iter()
|
||||
.filter(|item| item["kind"] == "recording")
|
||||
.find(after_start)
|
||||
.map(|item| self.evidence_for_item(item))
|
||||
.transpose()?;
|
||||
let recording_has_thumbnail = recording
|
||||
.as_ref()
|
||||
.is_some_and(|item| item["thumbnail"].is_object());
|
||||
Ok(json!({
|
||||
"schemaVersion": 1,
|
||||
"sinceMs": since_ms.map(|value| value.to_string()),
|
||||
"screenshot": screenshot,
|
||||
"recording": recording,
|
||||
"complete": screenshot.is_some() && recording.is_some() && recording_has_thumbnail
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn delete(&self, params: &Value) -> Result<Value, String> {
|
||||
if params["confirmed"].as_bool() != Some(true) {
|
||||
return Err("Media deletion requires explicit confirmation".to_owned());
|
||||
}
|
||||
let kind = params["kind"]
|
||||
.as_str()
|
||||
.ok_or_else(|| "media.delete requires kind".to_owned())?;
|
||||
let id = safe_id(¶ms["id"])?;
|
||||
let directory = self.directory_for_kind(kind)?;
|
||||
let path = directory.join(id);
|
||||
if !path.is_file() {
|
||||
return Err("Media item was not found".to_owned());
|
||||
}
|
||||
fs::remove_file(&path).map_err(|error| format!("Could not delete media: {error}"))?;
|
||||
if kind == "recording" {
|
||||
let stem = path
|
||||
.file_stem()
|
||||
.and_then(|value| value.to_str())
|
||||
.unwrap_or_default();
|
||||
let thumbnail = directory.join(format!("{stem}-thumb.jpg"));
|
||||
let _ = fs::remove_file(thumbnail);
|
||||
}
|
||||
Ok(json!({ "deleted": true, "id": id, "kind": kind }))
|
||||
}
|
||||
|
||||
fn collect(&self, kind: &str, folder: &str, output: &mut Vec<Value>) -> Result<(), String> {
|
||||
let directory = self.available_root()?.join(folder);
|
||||
let items = fs::read_dir(&directory)
|
||||
.map_err(|error| format!("Could not read media directory: {error}"))?;
|
||||
for item in items.flatten() {
|
||||
let path = item.path();
|
||||
if !path.is_file() || !allowed_extension(kind, &path) || is_thumbnail(&path) {
|
||||
continue;
|
||||
}
|
||||
let Ok(metadata) = item.metadata() else {
|
||||
continue;
|
||||
};
|
||||
let Some(file_name) = path.file_name().and_then(|value| value.to_str()) else {
|
||||
continue;
|
||||
};
|
||||
let created = metadata
|
||||
.created()
|
||||
.or_else(|_| metadata.modified())
|
||||
.unwrap_or(UNIX_EPOCH)
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_millis() as u64;
|
||||
let media_url = Url::from_file_path(&path)
|
||||
.ok()
|
||||
.map(|value| value.to_string())
|
||||
.unwrap_or_default();
|
||||
let thumbnail_url = if kind == "recording" {
|
||||
let stem = path
|
||||
.file_stem()
|
||||
.and_then(|value| value.to_str())
|
||||
.unwrap_or_default();
|
||||
let thumbnail = directory.join(format!("{stem}-thumb.jpg"));
|
||||
thumbnail
|
||||
.is_file()
|
||||
.then(|| {
|
||||
Url::from_file_path(thumbnail)
|
||||
.ok()
|
||||
.map(|value| value.to_string())
|
||||
})
|
||||
.flatten()
|
||||
.unwrap_or_default()
|
||||
} else {
|
||||
media_url.clone()
|
||||
};
|
||||
output.push(json!({
|
||||
"id": file_name,
|
||||
"kind": kind,
|
||||
"fileName": file_name,
|
||||
"filePath": path.to_string_lossy(),
|
||||
"mediaUrl": media_url,
|
||||
"thumbnailUrl": thumbnail_url,
|
||||
"createdAtMs": created,
|
||||
"sizeBytes": metadata.len()
|
||||
}));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn evidence_for_item(&self, item: &Value) -> Result<Value, String> {
|
||||
let path = PathBuf::from(
|
||||
item["filePath"]
|
||||
.as_str()
|
||||
.ok_or_else(|| "Media evidence path is unavailable".to_owned())?,
|
||||
);
|
||||
let (size, digest) = digest_regular_file(&path)?;
|
||||
let thumbnail = if item["kind"] == "recording" {
|
||||
let stem = path
|
||||
.file_stem()
|
||||
.and_then(|value| value.to_str())
|
||||
.unwrap_or_default();
|
||||
let path = path
|
||||
.parent()
|
||||
.unwrap_or_else(|| Path::new("."))
|
||||
.join(format!("{stem}-thumb.jpg"));
|
||||
if path.exists() {
|
||||
let (thumbnail_size, thumbnail_digest) = digest_regular_file(&path)?;
|
||||
Some(json!({
|
||||
"fileName": path.file_name().and_then(|value| value.to_str()).unwrap_or_default(),
|
||||
"sizeBytes": thumbnail_size,
|
||||
"sha256": thumbnail_digest
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Ok(json!({
|
||||
"kind": item["kind"],
|
||||
"fileName": item["fileName"],
|
||||
"createdAtMs": item["createdAtMs"],
|
||||
"sizeBytes": size,
|
||||
"sha256": digest,
|
||||
"thumbnail": thumbnail
|
||||
}))
|
||||
}
|
||||
|
||||
fn directory_for_kind(&self, kind: &str) -> Result<PathBuf, String> {
|
||||
let root = self.available_root()?;
|
||||
match kind {
|
||||
"screenshot" => Ok(root.join("Screenshots")),
|
||||
"recording" => Ok(root.join("Recordings")),
|
||||
_ => Err("Unsupported media kind".to_owned()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn digest_regular_file(path: &Path) -> Result<(u64, String), String> {
|
||||
let metadata = fs::symlink_metadata(path)
|
||||
.map_err(|error| format!("Could not inspect media evidence: {error}"))?;
|
||||
if !metadata.file_type().is_file() || metadata.file_type().is_symlink() {
|
||||
return Err("Media evidence must be a regular, non-symlink file".to_owned());
|
||||
}
|
||||
let file =
|
||||
fs::File::open(path).map_err(|error| format!("Could not open media evidence: {error}"))?;
|
||||
let mut reader = BufReader::new(file);
|
||||
let mut hasher = Sha256::new();
|
||||
let mut buffer = [0_u8; 64 * 1024];
|
||||
loop {
|
||||
let read = reader
|
||||
.read(&mut buffer)
|
||||
.map_err(|error| format!("Could not hash media evidence: {error}"))?;
|
||||
if read == 0 {
|
||||
break;
|
||||
}
|
||||
hasher.update(&buffer[..read]);
|
||||
}
|
||||
Ok((metadata.len(), format!("{:x}", hasher.finalize())))
|
||||
}
|
||||
|
||||
fn pictures_directory() -> Option<PathBuf> {
|
||||
#[cfg(target_os = "windows")]
|
||||
let profile_root = env::var_os("USERPROFILE");
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
let profile_root = env::var_os("HOME");
|
||||
resolve_pictures_directory(env::var_os("OPENNOW_PICTURES_DIR"), profile_root)
|
||||
}
|
||||
|
||||
fn resolve_pictures_directory(
|
||||
override_value: Option<OsString>,
|
||||
profile_root: Option<OsString>,
|
||||
) -> Option<PathBuf> {
|
||||
match override_value {
|
||||
Some(value) if !value.is_empty() => normalize_root(PathBuf::from(value)),
|
||||
Some(_) => None,
|
||||
None => profile_root
|
||||
.filter(|value| !value.is_empty())
|
||||
.map(PathBuf::from)
|
||||
.filter(|path| path.is_absolute())
|
||||
.map(|path| path.join("Pictures")),
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_root(path: PathBuf) -> Option<PathBuf> {
|
||||
std::path::absolute(&path)
|
||||
.ok()
|
||||
.filter(|resolved| resolved.is_absolute())
|
||||
}
|
||||
|
||||
fn safe_id(value: &Value) -> Result<&str, String> {
|
||||
let id = value
|
||||
.as_str()
|
||||
.filter(|value| !value.is_empty())
|
||||
.ok_or_else(|| "media.delete requires an id".to_owned())?;
|
||||
let path = Path::new(id);
|
||||
if path.components().count() != 1
|
||||
|| !matches!(path.components().next(), Some(Component::Normal(_)))
|
||||
|| id.contains("..")
|
||||
{
|
||||
return Err("Invalid media id".to_owned());
|
||||
}
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
fn allowed_extension(kind: &str, path: &Path) -> bool {
|
||||
let extension = path
|
||||
.extension()
|
||||
.and_then(|value| value.to_str())
|
||||
.unwrap_or_default()
|
||||
.to_ascii_lowercase();
|
||||
match kind {
|
||||
"screenshot" => matches!(extension.as_str(), "png" | "jpg" | "jpeg" | "webp"),
|
||||
"recording" => matches!(extension.as_str(), "mp4" | "webm" | "mkv" | "mov" | "avi"),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_thumbnail(path: &Path) -> bool {
|
||||
path.file_stem()
|
||||
.and_then(|value| value.to_str())
|
||||
.is_some_and(|value| value.ends_with("-thumb"))
|
||||
}
|
||||
|
||||
fn sanitized_title(value: &str) -> String {
|
||||
value
|
||||
.to_ascii_lowercase()
|
||||
.chars()
|
||||
.map(|value| {
|
||||
if value.is_ascii_alphanumeric() {
|
||||
value
|
||||
} else {
|
||||
'-'
|
||||
}
|
||||
})
|
||||
.collect::<String>()
|
||||
.trim_matches('-')
|
||||
.to_owned()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::Mutex;
|
||||
use std::time::SystemTime;
|
||||
|
||||
static ENVIRONMENT_LOCK: Mutex<()> = Mutex::new(());
|
||||
|
||||
fn lock_environment() -> std::sync::MutexGuard<'static, ()> {
|
||||
ENVIRONMENT_LOCK
|
||||
.lock()
|
||||
.unwrap_or_else(|error| error.into_inner())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pictures_root_prefers_an_explicit_override_and_rejects_blank_values() {
|
||||
let captures = env::temp_dir().join("opennow-media-captures");
|
||||
assert_eq!(
|
||||
resolve_pictures_directory(Some(captures.clone().into_os_string()), None),
|
||||
Some(captures)
|
||||
);
|
||||
let relative = resolve_pictures_directory(Some(OsString::from("relative/captures")), None)
|
||||
.expect("a relative override must resolve to an absolute path");
|
||||
assert!(relative.is_absolute());
|
||||
assert!(relative.ends_with("relative/captures"));
|
||||
|
||||
let home = env::temp_dir().join("opennow-media-home");
|
||||
assert_eq!(
|
||||
resolve_pictures_directory(None, Some(home.clone().into_os_string())),
|
||||
Some(home.join("Pictures"))
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_pictures_directory(Some(OsString::new()), Some(home.clone().into_os_string())),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_pictures_directory(None, Some(OsString::from("home/user"))),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_pictures_directory(None, Some(OsString::new())),
|
||||
None
|
||||
);
|
||||
assert_eq!(resolve_pictures_directory(None, None), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn override_normalization_is_always_absolute_or_unavailable() {
|
||||
let absolute = env::temp_dir().join("opennow-media-absolute-root");
|
||||
assert_eq!(normalize_root(absolute.clone()), Some(absolute.clone()));
|
||||
let relative = normalize_root(PathBuf::from("relative/root"))
|
||||
.expect("a relative override must resolve to an absolute path");
|
||||
assert!(relative.is_absolute());
|
||||
assert!(relative.ends_with("relative/root"));
|
||||
assert_eq!(normalize_root(PathBuf::new()), None);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
#[test]
|
||||
fn windows_partial_overrides_resolve_to_fully_qualified_paths() {
|
||||
for value in [r"C:Pictures", r"\Pictures"] {
|
||||
let resolved = normalize_root(PathBuf::from(value))
|
||||
.expect("a partially rooted override must resolve on Windows");
|
||||
assert!(resolved.is_absolute());
|
||||
assert!(resolved.ends_with("Pictures"));
|
||||
assert_ne!(resolved, PathBuf::from(value));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn media_service_reports_unavailable_root_without_failing_construction() {
|
||||
let _guard = lock_environment();
|
||||
let previous = env::var_os("OPENNOW_PICTURES_DIR");
|
||||
unsafe { env::set_var("OPENNOW_PICTURES_DIR", "") };
|
||||
let service = MediaService::new().expect("an unavailable root must not fail construction");
|
||||
assert!(service.root().is_err());
|
||||
assert!(service.list(&json!({})).is_err());
|
||||
assert!(
|
||||
service
|
||||
.recording_target(&json!({"gameTitle":"Test"}))
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
service
|
||||
.delete(&json!({"kind":"screenshot","id":"shot.png","confirmed":true}))
|
||||
.is_err()
|
||||
);
|
||||
match previous {
|
||||
Some(value) => unsafe { env::set_var("OPENNOW_PICTURES_DIR", value) },
|
||||
None => unsafe { env::remove_var("OPENNOW_PICTURES_DIR") },
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn media_listing_is_bounded_and_deletion_is_scoped() {
|
||||
let _guard = lock_environment();
|
||||
let directory = env::temp_dir().join(format!(
|
||||
"opennow-media-{}",
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos()
|
||||
));
|
||||
unsafe { env::set_var("OPENNOW_PICTURES_DIR", &directory) };
|
||||
let service = MediaService::new().unwrap();
|
||||
let root = service.available_root().unwrap().clone();
|
||||
let target = service
|
||||
.recording_target(&json!({"gameTitle":"Test / Game"}))
|
||||
.unwrap();
|
||||
let validated = service
|
||||
.validate_recording_target(&json!({"outputPath":target["path"]}))
|
||||
.unwrap();
|
||||
assert_eq!(validated["outputPath"], target["path"]);
|
||||
assert!(
|
||||
service
|
||||
.validate_recording_target(&json!({"outputPath":directory.join("escape.mkv")}))
|
||||
.is_err()
|
||||
);
|
||||
fs::write(root.join("Screenshots/shot.png"), b"png").unwrap();
|
||||
fs::write(root.join("Recordings/OpenNOW-test.mkv"), b"mkv").unwrap();
|
||||
fs::write(root.join("Recordings/OpenNOW-test-thumb.jpg"), b"jpg").unwrap();
|
||||
fs::write(root.join("Screenshots/nope.txt"), b"no").unwrap();
|
||||
let listed = service.list(&json!({})).unwrap();
|
||||
assert_eq!(listed["items"].as_array().unwrap().len(), 2);
|
||||
let evidence = service.acceptance_evidence(Some(0)).unwrap();
|
||||
assert_eq!(evidence["complete"], true);
|
||||
assert_eq!(evidence["screenshot"]["sizeBytes"], 3);
|
||||
assert_eq!(
|
||||
evidence["screenshot"]["sha256"],
|
||||
"8f8cbb7dcf46e0bc7d53265749a6c17d116093a6ba95e442764060c76fd4a86c"
|
||||
);
|
||||
assert_eq!(evidence["recording"]["thumbnail"]["sizeBytes"], 3);
|
||||
let rendered = evidence.to_string();
|
||||
assert!(!rendered.contains(directory.to_string_lossy().as_ref()));
|
||||
assert!(
|
||||
service
|
||||
.delete(&json!({"kind":"screenshot","id":"../shot.png","confirmed":true}))
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
service
|
||||
.delete(&json!({"kind":"screenshot","id":"shot.png"}))
|
||||
.is_err()
|
||||
);
|
||||
service
|
||||
.delete(&json!({"kind":"screenshot","id":"shot.png","confirmed":true}))
|
||||
.unwrap();
|
||||
assert!(!root.join("Screenshots/shot.png").exists());
|
||||
let _ = fs::remove_dir_all(directory);
|
||||
unsafe { env::remove_var("OPENNOW_PICTURES_DIR") };
|
||||
}
|
||||
}
|
||||
+321
@@ -0,0 +1,321 @@
|
||||
use serde_json::{Value, json};
|
||||
use std::net::{SocketAddr, TcpStream, ToSocketAddrs};
|
||||
use std::sync::{Arc, Mutex, OnceLock, mpsc};
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
use url::Url;
|
||||
|
||||
const MAX_REGIONS: usize = 32;
|
||||
const CONNECT_TIMEOUT: Duration = Duration::from_secs(3);
|
||||
const QUEUE_PROBE_TIMEOUT: Duration = Duration::from_millis(750);
|
||||
const RESOLVER_WORKERS: usize = 32;
|
||||
|
||||
struct ResolveJob {
|
||||
host: String,
|
||||
port: u16,
|
||||
deadline: Instant,
|
||||
cancellation: crate::requests::Cancellation,
|
||||
result: mpsc::SyncSender<Vec<SocketAddr>>,
|
||||
}
|
||||
|
||||
fn resolver() -> Option<&'static mpsc::SyncSender<ResolveJob>> {
|
||||
static RESOLVER: OnceLock<Option<mpsc::SyncSender<ResolveJob>>> = OnceLock::new();
|
||||
RESOLVER
|
||||
.get_or_init(|| {
|
||||
let (sender, receiver) = mpsc::sync_channel::<ResolveJob>(RESOLVER_WORKERS);
|
||||
let receiver = Arc::new(Mutex::new(receiver));
|
||||
for index in 0..RESOLVER_WORKERS {
|
||||
let receiver = receiver.clone();
|
||||
thread::Builder::new()
|
||||
.name(format!("queue-dns-{index}"))
|
||||
.spawn(move || {
|
||||
loop {
|
||||
let job = match receiver.lock() {
|
||||
Ok(receiver) => receiver.recv(),
|
||||
Err(_) => return,
|
||||
};
|
||||
let Ok(job) = job else { return };
|
||||
if job.cancellation.cancelled() || Instant::now() >= job.deadline {
|
||||
continue;
|
||||
}
|
||||
let addresses = (job.host.as_str(), job.port)
|
||||
.to_socket_addrs()
|
||||
.map(|addresses| addresses.take(8).collect())
|
||||
.unwrap_or_default();
|
||||
let _ = job.result.try_send(addresses);
|
||||
}
|
||||
})
|
||||
.ok()?;
|
||||
}
|
||||
Some(sender)
|
||||
})
|
||||
.as_ref()
|
||||
}
|
||||
|
||||
pub(super) fn resolve_bounded(host: String, port: u16) -> Option<Vec<SocketAddr>> {
|
||||
let cancellation = crate::requests::current();
|
||||
if cancellation.cancelled() {
|
||||
return None;
|
||||
}
|
||||
let deadline = Instant::now() + QUEUE_PROBE_TIMEOUT;
|
||||
let (result, receive) = mpsc::sync_channel(1);
|
||||
resolver()?
|
||||
.try_send(ResolveJob {
|
||||
host,
|
||||
port,
|
||||
deadline,
|
||||
cancellation: cancellation.clone(),
|
||||
result,
|
||||
})
|
||||
.ok()?;
|
||||
receive_addresses(&receive, deadline, &cancellation)
|
||||
}
|
||||
|
||||
fn receive_addresses(
|
||||
receive: &mpsc::Receiver<Vec<SocketAddr>>,
|
||||
deadline: Instant,
|
||||
cancellation: &crate::requests::Cancellation,
|
||||
) -> Option<Vec<SocketAddr>> {
|
||||
loop {
|
||||
let remaining = deadline.saturating_duration_since(Instant::now());
|
||||
if remaining.is_zero() || cancellation.cancelled() {
|
||||
return None;
|
||||
}
|
||||
match receive.recv_timeout(remaining.min(Duration::from_millis(50))) {
|
||||
Ok(addresses) => return Some(addresses),
|
||||
Err(mpsc::RecvTimeoutError::Disconnected) => return None,
|
||||
Err(mpsc::RecvTimeoutError::Timeout) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn measure_queue_region(region_url: &str) -> Option<u64> {
|
||||
if crate::requests::check().is_err() {
|
||||
return None;
|
||||
}
|
||||
let parsed = Url::parse(region_url).ok()?;
|
||||
let addresses = resolve_bounded(
|
||||
parsed.host_str()?.to_owned(),
|
||||
parsed.port_or_known_default()?,
|
||||
)?;
|
||||
let _ = bounded_tcp_ping(&addresses);
|
||||
let mut samples = Vec::with_capacity(2);
|
||||
for attempt in 0..2 {
|
||||
if crate::requests::check().is_err() {
|
||||
return None;
|
||||
}
|
||||
if attempt > 0 {
|
||||
thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
if let Some(sample) = bounded_tcp_ping(&addresses) {
|
||||
samples.push(sample);
|
||||
}
|
||||
}
|
||||
(!samples.is_empty()).then(|| samples.iter().sum::<u64>().div_ceil(samples.len() as u64))
|
||||
}
|
||||
|
||||
fn bounded_tcp_ping(addresses: &[SocketAddr]) -> Option<u64> {
|
||||
let started = Instant::now();
|
||||
for address in addresses {
|
||||
let remaining = QUEUE_PROBE_TIMEOUT.saturating_sub(started.elapsed());
|
||||
if remaining.is_zero() || crate::requests::check().is_err() {
|
||||
return None;
|
||||
}
|
||||
if TcpStream::connect_timeout(address, remaining).is_ok() {
|
||||
return Some(started.elapsed().as_millis() as u64);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn ping_regions(params: &Value) -> Result<Value, String> {
|
||||
let cancellation = crate::requests::current();
|
||||
cancellation.check().map_err(|error| error.message)?;
|
||||
let regions = params["regions"]
|
||||
.as_array()
|
||||
.ok_or_else(|| "network.regions.ping requires a regions array".to_owned())?;
|
||||
if regions.len() > MAX_REGIONS {
|
||||
return Err(format!(
|
||||
"At most {MAX_REGIONS} regions can be measured at once"
|
||||
));
|
||||
}
|
||||
let inputs = regions
|
||||
.iter()
|
||||
.map(|region| region["url"].as_str().unwrap_or_default().to_owned())
|
||||
.collect::<Vec<_>>();
|
||||
let results = Arc::new(Mutex::new(vec![Value::Null; inputs.len()]));
|
||||
thread::scope(|scope| {
|
||||
for (index, region_url) in inputs.into_iter().enumerate() {
|
||||
let output = Arc::clone(&results);
|
||||
let cancellation = cancellation.clone();
|
||||
scope.spawn(move || {
|
||||
let result = crate::requests::scope(cancellation, || measure_region(®ion_url));
|
||||
output.lock().expect("region ping output poisoned")[index] = result;
|
||||
});
|
||||
}
|
||||
});
|
||||
cancellation.check().map_err(|error| error.message)?;
|
||||
let results = Arc::try_unwrap(results)
|
||||
.map_err(|_| "Region measurement workers did not finish".to_owned())?
|
||||
.into_inner()
|
||||
.map_err(|_| "Region measurement output was poisoned".to_owned())?;
|
||||
Ok(json!({"results":results}))
|
||||
}
|
||||
|
||||
fn measure_region(region_url: &str) -> Value {
|
||||
if crate::requests::check().is_err() {
|
||||
return Value::Null;
|
||||
}
|
||||
let endpoint = match region_endpoint(region_url) {
|
||||
Ok(endpoint) => endpoint,
|
||||
Err(error) => return json!({"url":region_url,"pingMs":null,"error":error}),
|
||||
};
|
||||
let _ = tcp_ping(&endpoint);
|
||||
let mut samples = Vec::with_capacity(3);
|
||||
for attempt in 0..3 {
|
||||
if crate::requests::check().is_err() {
|
||||
return Value::Null;
|
||||
}
|
||||
if attempt > 0 {
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
if let Some(sample) = tcp_ping(&endpoint) {
|
||||
samples.push(sample);
|
||||
}
|
||||
}
|
||||
if samples.is_empty() {
|
||||
return json!({"url":region_url,"pingMs":null,"error":"All TCP measurements failed"});
|
||||
}
|
||||
let total = samples.iter().copied().sum::<u128>();
|
||||
let average = total.div_ceil(samples.len() as u128);
|
||||
json!({"url":region_url,"pingMs":u64::try_from(average).unwrap_or(u64::MAX)})
|
||||
}
|
||||
|
||||
fn region_endpoint(region_url: &str) -> Result<Vec<SocketAddr>, String> {
|
||||
let parsed = Url::parse(region_url).map_err(|_| "Invalid region URL".to_owned())?;
|
||||
if !matches!(parsed.scheme(), "https" | "http") {
|
||||
return Err("Unsupported region URL scheme".to_owned());
|
||||
}
|
||||
let host = parsed
|
||||
.host_str()
|
||||
.filter(|host| !host.is_empty())
|
||||
.ok_or_else(|| "Region URL has no host".to_owned())?;
|
||||
let port = parsed
|
||||
.port_or_known_default()
|
||||
.ok_or_else(|| "Region URL has no TCP port".to_owned())?;
|
||||
let addresses = (host, port)
|
||||
.to_socket_addrs()
|
||||
.map_err(|_| "Region host could not be resolved".to_owned())?
|
||||
.collect::<Vec<_>>();
|
||||
if addresses.is_empty() {
|
||||
return Err("Region host has no routable address".to_owned());
|
||||
}
|
||||
Ok(addresses)
|
||||
}
|
||||
|
||||
fn tcp_ping(addresses: &[SocketAddr]) -> Option<u128> {
|
||||
let started = Instant::now();
|
||||
for address in addresses {
|
||||
if crate::requests::check().is_err() {
|
||||
return None;
|
||||
}
|
||||
if TcpStream::connect_timeout(address, CONNECT_TIMEOUT).is_ok() {
|
||||
return Some(started.elapsed().as_millis());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::net::TcpListener;
|
||||
|
||||
#[test]
|
||||
fn queue_tcp_measurement_warms_up_then_takes_two_samples_without_http() {
|
||||
let listener = TcpListener::bind(("127.0.0.1", 0)).unwrap();
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
let worker = thread::spawn(move || {
|
||||
use std::io::Read;
|
||||
for _ in 0..3 {
|
||||
let (mut connection, _) = listener.accept().unwrap();
|
||||
connection
|
||||
.set_read_timeout(Some(Duration::from_secs(1)))
|
||||
.unwrap();
|
||||
assert_eq!(connection.read(&mut [0]).unwrap(), 0);
|
||||
}
|
||||
});
|
||||
assert!(measure_queue_region(&format!("http://127.0.0.1:{port}")).is_some());
|
||||
worker.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stalled_resolution_obeys_deadline_and_cancellation() {
|
||||
let (_sender, receive) = mpsc::sync_channel(1);
|
||||
let cancellation = crate::requests::Cancellation::default();
|
||||
let started = Instant::now();
|
||||
assert!(
|
||||
receive_addresses(&receive, started + Duration::from_millis(20), &cancellation)
|
||||
.is_none()
|
||||
);
|
||||
assert!(started.elapsed() < Duration::from_secs(1));
|
||||
let requests = Arc::new(crate::requests::Requests::default());
|
||||
let permit = requests.admit("dns", "queue.servers.list").unwrap();
|
||||
requests.cancel("dns");
|
||||
assert!(
|
||||
receive_addresses(
|
||||
&receive,
|
||||
Instant::now() + Duration::from_secs(60),
|
||||
&permit.token
|
||||
)
|
||||
.is_none()
|
||||
);
|
||||
crate::requests::scope(permit.token.clone(), || {
|
||||
assert!(resolve_bounded("unresolvable.invalid".into(), 443).is_none());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cancelled_region_measurement_does_not_start_network_work() {
|
||||
let requests = Arc::new(crate::requests::Requests::default());
|
||||
let permit = requests.admit("ping", "network.regions.ping").unwrap();
|
||||
requests.cancel("ping");
|
||||
crate::requests::scope(permit.token.clone(), || {
|
||||
assert_eq!(
|
||||
ping_regions(&json!({"regions":[]})).unwrap_err(),
|
||||
"Request cancelled"
|
||||
);
|
||||
assert_eq!(measure_region("https://unresolvable.invalid"), Value::Null);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn region_ping_rejects_invalid_and_unbounded_input() {
|
||||
let invalid = ping_regions(&json!({"regions":[{"url":"file:///tmp/nope"}]}))
|
||||
.expect("invalid regions return per-item results");
|
||||
assert_eq!(invalid["results"][0]["pingMs"], Value::Null);
|
||||
assert_eq!(
|
||||
invalid["results"][0]["error"],
|
||||
"Unsupported region URL scheme"
|
||||
);
|
||||
|
||||
let regions = (0..=MAX_REGIONS)
|
||||
.map(|_| json!({"url":"https://example.invalid"}))
|
||||
.collect::<Vec<_>>();
|
||||
assert!(ping_regions(&json!({"regions":regions})).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn offline_region_is_a_scoped_result_instead_of_a_request_failure() {
|
||||
let listener = TcpListener::bind(("127.0.0.1", 0)).expect("ephemeral port");
|
||||
let port = listener.local_addr().expect("listener address").port();
|
||||
drop(listener);
|
||||
|
||||
let result = ping_regions(&json!({
|
||||
"regions":[{"url":format!("http://127.0.0.1:{port}")}]
|
||||
}))
|
||||
.expect("offline measurements remain serializable");
|
||||
assert_eq!(result["results"][0]["pingMs"], Value::Null);
|
||||
assert_eq!(result["results"][0]["error"], "All TCP measurements failed");
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,193 @@
|
||||
use crate::gfn::{AuthSession, ServiceError};
|
||||
use reqwest::blocking::Client;
|
||||
use reqwest::header::{ACCEPT, CONTENT_TYPE, HeaderMap, HeaderValue};
|
||||
use serde_json::{Value, json};
|
||||
use url::Url;
|
||||
|
||||
const PAYWALL_BASE: &str = "https://api-prod.nvidia.com/gfn-paywall-api/api/v2";
|
||||
|
||||
pub struct PersistentStorageService {
|
||||
client: Client,
|
||||
}
|
||||
|
||||
impl PersistentStorageService {
|
||||
pub fn new(client: Client) -> Self {
|
||||
Self { client }
|
||||
}
|
||||
|
||||
pub fn locations(&self, params: &Value, auth: &AuthSession) -> Result<Value, ServiceError> {
|
||||
let current_code = params["currentRegionCode"]
|
||||
.as_str()
|
||||
.filter(|value| !value.trim().is_empty());
|
||||
let current_name = params["currentRegionName"]
|
||||
.as_str()
|
||||
.filter(|value| !value.trim().is_empty());
|
||||
let mut url =
|
||||
Url::parse(&format!("{PAYWALL_BASE}/products")).expect("constant paywall URL is valid");
|
||||
url.query_pairs_mut()
|
||||
.append_pair("locale", params["locale"].as_str().unwrap_or("en_US"));
|
||||
if let Some(vpc_id) = params["serverRegionId"]
|
||||
.as_str()
|
||||
.filter(|value| !value.trim().is_empty())
|
||||
{
|
||||
url.query_pairs_mut().append_pair("vpcId", vpc_id);
|
||||
}
|
||||
let token = session_token(auth)?;
|
||||
let response = self
|
||||
.client
|
||||
.get(url)
|
||||
.headers(paywall_headers(token)?)
|
||||
.send()
|
||||
.map_err(|error| ServiceError::network("Storage region discovery failed", error))?;
|
||||
let status = response.status();
|
||||
if !status.is_success() {
|
||||
return Err(ServiceError {
|
||||
code: if status.as_u16() == 401 {
|
||||
"http_unauthorized"
|
||||
} else {
|
||||
"upstream_error"
|
||||
},
|
||||
message: format!("Storage region discovery failed ({status})"),
|
||||
});
|
||||
}
|
||||
let payload = response
|
||||
.json::<Value>()
|
||||
.map_err(|error| ServiceError::network("Invalid storage region response", error))?;
|
||||
if !payload["products"].is_array() {
|
||||
return Err(ServiceError {
|
||||
code: "invalid_upstream_response",
|
||||
message: "Storage region response has no products".into(),
|
||||
});
|
||||
}
|
||||
let mut locations = locations_from_products(&payload, current_code);
|
||||
if let Some(code) = current_code
|
||||
&& !locations
|
||||
.iter()
|
||||
.any(|location| location["code"].as_str() == Some(code))
|
||||
{
|
||||
locations.insert(0, json!({"code":code,"name":current_name.unwrap_or(code),"isAvailable":false,"isCurrent":true}));
|
||||
}
|
||||
Ok(
|
||||
json!({"locations":locations,"currentRegionCode":current_code,"currentRegionName":current_name}),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn reset(&self, params: &Value, auth: &AuthSession) -> Result<Value, ServiceError> {
|
||||
if params["confirmed"].as_bool() != Some(true) {
|
||||
return Err(ServiceError {
|
||||
code: "confirmation_required",
|
||||
message: "Persistent storage reset requires explicit confirmation".to_owned(),
|
||||
});
|
||||
}
|
||||
let region = params["storageRegion"]
|
||||
.as_str()
|
||||
.filter(|value| !value.trim().is_empty());
|
||||
let mut url = Url::parse(&format!("{PAYWALL_BASE}/reset/storage"))
|
||||
.expect("constant paywall URL is valid");
|
||||
url.query_pairs_mut()
|
||||
.append_pair("storageRegion", region.unwrap_or("null"));
|
||||
let response = self
|
||||
.client
|
||||
.post(url)
|
||||
.headers(paywall_headers(session_token(auth)?)?)
|
||||
.send()
|
||||
.map_err(|error| ServiceError {
|
||||
code: "network_error",
|
||||
message: format!("Persistent storage reset failed: {error}"),
|
||||
})?;
|
||||
let status = response.status();
|
||||
let payload = response.json::<Value>().unwrap_or(Value::Null);
|
||||
if !status.is_success() {
|
||||
let message = payload["message"]
|
||||
.as_str()
|
||||
.or_else(|| payload["errors"]["errorMessage"].as_str())
|
||||
.unwrap_or("Persistent storage reset failed");
|
||||
let message = if message.contains("idToken") {
|
||||
"NVIDIA requires a web-account session for this storage action. Open NVIDIA Storage Manager in your browser."
|
||||
} else {
|
||||
message
|
||||
};
|
||||
return Err(ServiceError {
|
||||
code: if matches!(status.as_u16(), 401 | 403) {
|
||||
"authentication_required"
|
||||
} else {
|
||||
"upstream_error"
|
||||
},
|
||||
message: message.to_owned(),
|
||||
});
|
||||
}
|
||||
Ok(json!({"ok":true,"storageRegion":region,"message":payload["message"]}))
|
||||
}
|
||||
}
|
||||
|
||||
fn locations_from_products(payload: &Value, current_code: Option<&str>) -> Vec<Value> {
|
||||
let mut products = payload["products"].as_array().cloned().unwrap_or_default();
|
||||
let addons = products
|
||||
.iter()
|
||||
.flat_map(|product| product["add_on"].as_array().cloned().unwrap_or_default())
|
||||
.collect::<Vec<_>>();
|
||||
products.extend(addons);
|
||||
let regions = products
|
||||
.iter()
|
||||
.find_map(|product| {
|
||||
let kind = product["productType"].as_str().unwrap_or_default();
|
||||
(kind.eq_ignore_ascii_case("STORAGE")
|
||||
|| kind.eq_ignore_ascii_case("PAID")
|
||||
|| product["regions"].is_array())
|
||||
.then(|| product["regions"].as_array().cloned())
|
||||
.flatten()
|
||||
.filter(|values| !values.is_empty())
|
||||
})
|
||||
.unwrap_or_default();
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
regions.into_iter().filter_map(|region| {
|
||||
let code = region["metroRegion"].as_str()?.trim();
|
||||
if code.is_empty() || !seen.insert(code.to_owned()) { return None; }
|
||||
Some(json!({
|
||||
"code":code,"name":region["metroRegionName"].as_str().unwrap_or(code),
|
||||
"isAvailable":region["isAvailable"].as_bool().unwrap_or(true),
|
||||
"isCurrent":current_code == Some(code),"isRecommended":region["isRecommendedRegion"].as_bool().unwrap_or(false)
|
||||
}))
|
||||
}).collect()
|
||||
}
|
||||
|
||||
fn paywall_headers(token: &str) -> Result<HeaderMap, ServiceError> {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(ACCEPT, HeaderValue::from_static("application/json"));
|
||||
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
|
||||
headers.insert(
|
||||
"idtoken",
|
||||
HeaderValue::from_str(token).map_err(|_| ServiceError {
|
||||
code: "invalid_params",
|
||||
message: "Invalid authentication token".to_owned(),
|
||||
})?,
|
||||
);
|
||||
Ok(headers)
|
||||
}
|
||||
|
||||
fn session_token(auth: &AuthSession) -> Result<&str, ServiceError> {
|
||||
auth.tokens
|
||||
.id_token
|
||||
.as_deref()
|
||||
.or(Some(auth.tokens.access_token.as_str()))
|
||||
.filter(|value| !value.is_empty())
|
||||
.ok_or_else(|| ServiceError {
|
||||
code: "authentication_required",
|
||||
message: "No authenticated token is available".to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn product_regions_are_deduplicated() {
|
||||
let payload = json!({"products":[{"productType":"STORAGE","regions":[
|
||||
{"metroRegion":"NP-AMS-07","metroRegionName":"Amsterdam","isAvailable":true},
|
||||
{"metroRegion":"NP-AMS-07","metroRegionName":"Duplicate"}
|
||||
]}]});
|
||||
let result = locations_from_products(&payload, Some("NP-AMS-07"));
|
||||
assert_eq!(result.len(), 1);
|
||||
assert_eq!(result[0]["isCurrent"], true);
|
||||
}
|
||||
}
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
use reqwest::Proxy;
|
||||
use reqwest::blocking::{Client, ClientBuilder};
|
||||
use serde_json::Value;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::time::Duration;
|
||||
use url::Url;
|
||||
|
||||
const INVALID_PROXY: &str = "Invalid session proxy URL. Use http://host:port, https://host:port, socks4://host:port, or socks5://host:port.";
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct ProxyConfig {
|
||||
pub normalized_url: String,
|
||||
pub cache_scope: String,
|
||||
pub has_credentials: bool,
|
||||
}
|
||||
|
||||
pub fn config_from_settings(settings: &Value) -> Result<Option<ProxyConfig>, String> {
|
||||
if settings["sessionProxyEnabled"].as_bool() != Some(true) {
|
||||
return Ok(None);
|
||||
}
|
||||
let raw = settings["sessionProxyUrl"].as_str().unwrap_or("");
|
||||
normalize_proxy_url(raw).map(Some)
|
||||
}
|
||||
|
||||
pub fn client_for_settings(base: &Client, settings: &Value) -> Result<Client, String> {
|
||||
client_for_settings_with(base, settings, |builder| builder)
|
||||
}
|
||||
|
||||
pub fn client_for_settings_with(
|
||||
base: &Client,
|
||||
settings: &Value,
|
||||
configure: impl FnOnce(ClientBuilder) -> ClientBuilder,
|
||||
) -> Result<Client, String> {
|
||||
let Some(config) = config_from_settings(settings)? else {
|
||||
return Ok(base.clone());
|
||||
};
|
||||
let proxy = Proxy::all(&config.normalized_url).map_err(|_| INVALID_PROXY.to_owned())?;
|
||||
configure(
|
||||
Client::builder()
|
||||
.redirect(reqwest::redirect::Policy::none())
|
||||
.connect_timeout(Duration::from_secs(8))
|
||||
.timeout(Duration::from_secs(20))
|
||||
.pool_idle_timeout(Duration::from_secs(60))
|
||||
.proxy(proxy),
|
||||
)
|
||||
.build()
|
||||
.map_err(|_| "Could not initialize the configured session proxy".to_owned())
|
||||
}
|
||||
|
||||
pub fn normalize_proxy_url(raw: &str) -> Result<ProxyConfig, String> {
|
||||
let trimmed = raw.trim();
|
||||
if trimmed.is_empty() {
|
||||
return Err(INVALID_PROXY.to_owned());
|
||||
}
|
||||
let candidate = if trimmed.contains("://") {
|
||||
trimmed.to_owned()
|
||||
} else {
|
||||
format!("http://{trimmed}")
|
||||
};
|
||||
let parsed = Url::parse(&candidate).map_err(|_| INVALID_PROXY.to_owned())?;
|
||||
if !matches!(parsed.scheme(), "http" | "https" | "socks4" | "socks5")
|
||||
|| parsed.host_str().is_none()
|
||||
|| parsed.port().is_none()
|
||||
|| (parsed.password().is_some() && parsed.username().is_empty())
|
||||
|| (parsed.path() != "" && parsed.path() != "/")
|
||||
|| parsed.query().is_some()
|
||||
|| parsed.fragment().is_some()
|
||||
{
|
||||
return Err(INVALID_PROXY.to_owned());
|
||||
}
|
||||
let endpoint = format!(
|
||||
"{}://{}:{}",
|
||||
parsed.scheme(),
|
||||
parsed.host_str().unwrap_or_default().to_ascii_lowercase(),
|
||||
parsed.port().unwrap_or_default()
|
||||
);
|
||||
let digest = Sha256::digest(endpoint.as_bytes());
|
||||
let digest = format!("{digest:x}");
|
||||
Ok(ProxyConfig {
|
||||
normalized_url: parsed.to_string(),
|
||||
cache_scope: format!("proxy:{}", &digest[..16]),
|
||||
has_credentials: !parsed.username().is_empty(),
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn proxy_configuration_is_strict_and_credential_safe() {
|
||||
let direct = json!({"sessionProxyEnabled":false,"sessionProxyUrl":"ignored:80"});
|
||||
assert_eq!(config_from_settings(&direct).unwrap(), None);
|
||||
|
||||
let first = config_from_settings(&json!({
|
||||
"sessionProxyEnabled":true,
|
||||
"sessionProxyUrl":"user:secret@Proxy.Example:8080"
|
||||
}))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let second = config_from_settings(&json!({
|
||||
"sessionProxyEnabled":true,
|
||||
"sessionProxyUrl":"http://other:password@proxy.example:8080"
|
||||
}))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert!(first.has_credentials);
|
||||
assert_eq!(first.cache_scope, second.cache_scope);
|
||||
assert!(!first.cache_scope.contains("secret"));
|
||||
assert!(
|
||||
config_from_settings(&json!({
|
||||
"sessionProxyEnabled":true,
|
||||
"sessionProxyUrl":"ftp://proxy.example:21"
|
||||
}))
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
config_from_settings(&json!({
|
||||
"sessionProxyEnabled":true,
|
||||
"sessionProxyUrl":"proxy.example"
|
||||
}))
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,347 @@
|
||||
use crate::push::PushOwnerConfig;
|
||||
use crate::push::registration::{PushEndpoints, PushIdentity};
|
||||
use serde_json::Value;
|
||||
use std::io::Read;
|
||||
use std::path::Path;
|
||||
|
||||
pub const FILE_NAME: &str = "push.json";
|
||||
pub const MAXIMUM_CONFIG_BYTES: u64 = 64 * 1024;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum PushConfig {
|
||||
Absent,
|
||||
Configured(Box<PushOwnerConfig>),
|
||||
Suppressed,
|
||||
}
|
||||
|
||||
pub fn load_for_provider(data_dir: &Path, provider_idp_id: &str, device_id: String) -> PushConfig {
|
||||
let path = data_dir.join(FILE_NAME);
|
||||
match std::fs::symlink_metadata(&path) {
|
||||
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return PushConfig::Absent,
|
||||
Err(_) => return PushConfig::Suppressed,
|
||||
Ok(_) => {}
|
||||
}
|
||||
match std::fs::metadata(&path) {
|
||||
Ok(metadata) if metadata.is_file() => {}
|
||||
_ => return PushConfig::Suppressed,
|
||||
}
|
||||
let Some(bytes) = read_bounded(&path) else {
|
||||
return PushConfig::Suppressed;
|
||||
};
|
||||
let Ok(raw) = serde_json::from_slice::<Value>(&bytes) else {
|
||||
return PushConfig::Suppressed;
|
||||
};
|
||||
let Some(entry) = select_entry(&raw, provider_idp_id) else {
|
||||
return PushConfig::Suppressed;
|
||||
};
|
||||
from_json(entry, provider_idp_id, device_id)
|
||||
.map(|config| PushConfig::Configured(Box::new(config)))
|
||||
.unwrap_or(PushConfig::Suppressed)
|
||||
}
|
||||
|
||||
fn read_bounded(path: &Path) -> Option<Vec<u8>> {
|
||||
let file = std::fs::File::open(path).ok()?;
|
||||
let mut bytes = Vec::new();
|
||||
file.take(MAXIMUM_CONFIG_BYTES + 1)
|
||||
.read_to_end(&mut bytes)
|
||||
.ok()?;
|
||||
(bytes.len() as u64 <= MAXIMUM_CONFIG_BYTES).then_some(bytes)
|
||||
}
|
||||
|
||||
fn select_entry<'a>(raw: &'a Value, provider_idp_id: &str) -> Option<&'a Value> {
|
||||
match raw {
|
||||
Value::Array(entries) => entries.iter().find(|entry| {
|
||||
entry
|
||||
.get("providerIdpId")
|
||||
.and_then(Value::as_str)
|
||||
.is_some_and(|provider| provider == provider_idp_id)
|
||||
}),
|
||||
Value::Object(_) => Some(raw),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn pns_base_is_usable(base: &str) -> bool {
|
||||
let Ok(url) = url::Url::parse(base) else {
|
||||
return false;
|
||||
};
|
||||
url.scheme() == "https"
|
||||
&& url.host_str().is_some_and(|host| !host.is_empty())
|
||||
&& url.username().is_empty()
|
||||
&& url.password().is_none()
|
||||
&& url.query().is_none()
|
||||
&& url.fragment().is_none()
|
||||
}
|
||||
|
||||
pub fn from_json(raw: &Value, provider_idp_id: &str, device_id: String) -> Option<PushOwnerConfig> {
|
||||
let object = raw.as_object()?;
|
||||
if let Some(enabled) = object.get("enabled") {
|
||||
if enabled.as_bool() != Some(true) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
let string = |key: &str| -> Option<String> {
|
||||
object
|
||||
.get(key)?
|
||||
.as_str()
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.map(str::to_owned)
|
||||
};
|
||||
let configured_provider = string("providerIdpId")?;
|
||||
if configured_provider != provider_idp_id {
|
||||
return None;
|
||||
}
|
||||
let project_id = string("projectId")?;
|
||||
let api_key = string("apiKey")?;
|
||||
let sender_id = string("senderId")?;
|
||||
let app_id = string("appId")?;
|
||||
let firebase_app_id = string("firebaseAppId")?;
|
||||
let pns_server = string("pnsServer")?;
|
||||
let pns_version = string("pnsVersion").unwrap_or_else(|| "v1".to_owned());
|
||||
let pns_client_id = string("pnsClientId")?;
|
||||
let pns_base = format!(
|
||||
"{}/{}",
|
||||
pns_server.trim_end_matches('/'),
|
||||
pns_version.trim_matches('/')
|
||||
);
|
||||
if !pns_base_is_usable(&pns_server) || !pns_base_is_usable(&pns_base) {
|
||||
return None;
|
||||
}
|
||||
let endpoints = PushEndpoints {
|
||||
pns: pns_base,
|
||||
pns_client_id,
|
||||
..PushEndpoints::default()
|
||||
};
|
||||
let identity = PushIdentity {
|
||||
project_id,
|
||||
api_key,
|
||||
sender_id,
|
||||
app_id,
|
||||
firebase_app_id,
|
||||
vapid_key: string("vapidKey"),
|
||||
};
|
||||
Some(PushOwnerConfig::bounded(
|
||||
endpoints,
|
||||
identity,
|
||||
configured_provider,
|
||||
device_id,
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
fn raw() -> Value {
|
||||
json!({
|
||||
"providerIdpId": "nvidia",
|
||||
"projectId": "project",
|
||||
"apiKey": "api-key",
|
||||
"senderId": "123456789012",
|
||||
"appId": "app",
|
||||
"firebaseAppId": "1:1:web:abc",
|
||||
"vapidKey": "vapid",
|
||||
"pnsServer": "https://pns.example/",
|
||||
"pnsVersion": "v1",
|
||||
"pnsClientId": "client"
|
||||
})
|
||||
}
|
||||
|
||||
fn data_dir(name: &str) -> std::path::PathBuf {
|
||||
let dir = std::env::temp_dir().join(format!("opennow-push-config-{name}"));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
fn load(dir: &Path, provider: &str) -> PushConfig {
|
||||
load_for_provider(dir, provider, "device".into())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_config_produces_a_bounded_owner_config() {
|
||||
let config = from_json(&raw(), "nvidia", "device".into()).unwrap();
|
||||
assert_eq!(config.identity.project_id, "project");
|
||||
assert_eq!(config.identity.sender_id, "123456789012");
|
||||
assert_eq!(config.endpoints.pns, "https://pns.example/v1");
|
||||
assert_eq!(config.endpoints.pns_client_id, "client");
|
||||
assert_eq!(config.device_id, "device");
|
||||
assert_eq!(config.endpoints.mcs_host, "mtalk.google.com");
|
||||
assert!(config.maximum_frame_bytes <= 64 * 1024);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provider_mismatch_never_starts_the_vendor_config() {
|
||||
assert!(from_json(&raw(), "alliance", "device".into()).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incomplete_or_blank_config_is_rejected() {
|
||||
let mut value = raw();
|
||||
value["projectId"] = json!(" ");
|
||||
assert!(from_json(&value, "nvidia", "device".into()).is_none());
|
||||
let mut value = raw();
|
||||
value.as_object_mut().unwrap().remove("pnsServer");
|
||||
assert!(from_json(&value, "nvidia", "device".into()).is_none());
|
||||
assert!(from_json(&json!("not an object"), "nvidia", "device".into()).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_vapid_key_is_allowed_and_version_defaults() {
|
||||
let mut value = raw();
|
||||
value.as_object_mut().unwrap().remove("vapidKey");
|
||||
value.as_object_mut().unwrap().remove("pnsVersion");
|
||||
let config = from_json(&value, "nvidia", "device".into()).unwrap();
|
||||
assert!(config.identity.vapid_key.is_none());
|
||||
assert_eq!(config.endpoints.pns, "https://pns.example/v1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_explicit_disable_suppresses_the_override() {
|
||||
assert!(from_json(&json!({"enabled": false}), "nvidia", "device".into()).is_none());
|
||||
let mut value = raw();
|
||||
value["enabled"] = json!(false);
|
||||
assert!(from_json(&value, "nvidia", "device".into()).is_none());
|
||||
assert!(from_json(&json!({"enabled": "no"}), "nvidia", "device".into()).is_none());
|
||||
let mut value = raw();
|
||||
value["enabled"] = json!(true);
|
||||
assert!(from_json(&value, "nvidia", "device".into()).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_config_file_is_absent_without_an_override() {
|
||||
let dir = data_dir("absent");
|
||||
assert_eq!(load(&dir, "nvidia"), PushConfig::Absent);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_config_file_is_configured_for_a_complete_matching_entry() {
|
||||
let dir = data_dir("configured");
|
||||
std::fs::write(dir.join(FILE_NAME), raw().to_string()).unwrap();
|
||||
let PushConfig::Configured(config) = load(&dir, "nvidia") else {
|
||||
panic!("a complete entry must be configured");
|
||||
};
|
||||
assert_eq!(config.identity.project_id, "project");
|
||||
assert_eq!(config.provider_id, "nvidia");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_existing_override_never_falls_back_to_a_default() {
|
||||
let dir = data_dir("suppressed");
|
||||
std::fs::write(dir.join(FILE_NAME), raw().to_string()).unwrap();
|
||||
assert_eq!(load(&dir, "alliance"), PushConfig::Suppressed);
|
||||
std::fs::write(dir.join(FILE_NAME), b"not json").unwrap();
|
||||
assert_eq!(load(&dir, "nvidia"), PushConfig::Suppressed);
|
||||
std::fs::write(dir.join(FILE_NAME), json!({"enabled": false}).to_string()).unwrap();
|
||||
assert_eq!(load(&dir, "nvidia"), PushConfig::Suppressed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_config_file_selects_the_matching_provider_entry() {
|
||||
let dir = data_dir("array");
|
||||
let entries = json!([
|
||||
{"providerIdpId": "alliance", "projectId": "other"},
|
||||
raw()
|
||||
]);
|
||||
std::fs::write(dir.join(FILE_NAME), entries.to_string()).unwrap();
|
||||
let PushConfig::Configured(config) = load(&dir, "nvidia") else {
|
||||
panic!("the matching entry must be configured");
|
||||
};
|
||||
assert_eq!(config.identity.project_id, "project");
|
||||
assert_eq!(load(&dir, "alliance"), PushConfig::Suppressed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oversized_config_files_are_suppressed() {
|
||||
let dir = data_dir("oversized");
|
||||
std::fs::write(
|
||||
dir.join(FILE_NAME),
|
||||
vec![b' '; MAXIMUM_CONFIG_BYTES as usize + 1],
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(load(&dir, "nvidia"), PushConfig::Suppressed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_config_file_is_suppressed() {
|
||||
let dir = data_dir("empty");
|
||||
std::fs::write(dir.join(FILE_NAME), b"").unwrap();
|
||||
assert_eq!(load(&dir, "nvidia"), PushConfig::Suppressed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_insecure_or_ambiguous_pns_base_is_rejected() {
|
||||
for server in [
|
||||
"http://pns.example",
|
||||
"https://",
|
||||
"https://user@pns.example",
|
||||
"https://user:pw@pns.example",
|
||||
"https://pns.example/?x=1",
|
||||
"https://pns.example/#f",
|
||||
"pns.example",
|
||||
"not a url",
|
||||
] {
|
||||
let mut value = raw();
|
||||
value["pnsServer"] = json!(server);
|
||||
assert!(
|
||||
from_json(&value, "nvidia", "device".into()).is_none(),
|
||||
"the pns base {server} must be rejected"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_valid_pns_base_keeps_the_documented_shape() {
|
||||
let mut value = raw();
|
||||
value["pnsServer"] = json!("https://pns.example/api");
|
||||
value["pnsVersion"] = json!("v2");
|
||||
let config = from_json(&value, "nvidia", "device".into()).unwrap();
|
||||
assert_eq!(config.endpoints.pns, "https://pns.example/api/v2");
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_dangling_config_link_is_suppressed() {
|
||||
let dir = data_dir("dangling");
|
||||
let link = dir.join(FILE_NAME);
|
||||
std::os::unix::fs::symlink(dir.join("missing-push.json"), &link).unwrap();
|
||||
assert_eq!(load(&dir, "nvidia"), PushConfig::Suppressed);
|
||||
std::fs::remove_file(&link).unwrap();
|
||||
assert_eq!(load(&dir, "nvidia"), PushConfig::Absent);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_config_link_to_a_valid_file_is_configured() {
|
||||
let dir = data_dir("link-valid");
|
||||
let target = dir.join("target.json");
|
||||
std::fs::write(&target, raw().to_string()).unwrap();
|
||||
std::os::unix::fs::symlink(&target, dir.join(FILE_NAME)).unwrap();
|
||||
let PushConfig::Configured(config) = load(&dir, "nvidia") else {
|
||||
panic!("a valid link target must stay configured");
|
||||
};
|
||||
assert_eq!(config.identity.project_id, "project");
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_non_regular_override_entry_is_suppressed_without_blocking() {
|
||||
use std::os::unix::ffi::OsStrExt as _;
|
||||
let dir = data_dir("fifo");
|
||||
let path = dir.join(FILE_NAME);
|
||||
let c_path = std::ffi::CString::new(path.as_os_str().as_bytes()).unwrap();
|
||||
assert_eq!(unsafe { libc::mkfifo(c_path.as_ptr(), 0o600) }, 0);
|
||||
let (sender, receiver) = std::sync::mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
let _ = sender.send(load(&dir, "nvidia"));
|
||||
});
|
||||
assert_eq!(
|
||||
receiver
|
||||
.recv_timeout(std::time::Duration::from_secs(2))
|
||||
.expect("a non-regular override entry must not block the loader"),
|
||||
PushConfig::Suppressed
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,368 @@
|
||||
use crate::push::PushError;
|
||||
use aes_gcm::aead::{Aead, KeyInit};
|
||||
use aes_gcm::{Aes128Gcm, Nonce};
|
||||
use hkdf::Hkdf;
|
||||
use p256::ecdh;
|
||||
use p256::elliptic_curve::sec1::ToEncodedPoint;
|
||||
use p256::{PublicKey, SecretKey};
|
||||
use rand::RngCore;
|
||||
use sha2::Sha256;
|
||||
|
||||
const AUTH_SECRET_BYTES: usize = 16;
|
||||
const PUBLIC_KEY_BYTES: usize = 65;
|
||||
const SALT_BYTES: usize = 16;
|
||||
const NONCE_BYTES: usize = 12;
|
||||
const TAG_BYTES: usize = 16;
|
||||
const MAXIMUM_RECORD_SIZE: u32 = 16 * 1024;
|
||||
const MINIMUM_RECORD_SIZE: u32 = 18;
|
||||
const AES128GCM_HEADER_BYTES: usize = 21;
|
||||
|
||||
pub struct EceKeyPair {
|
||||
secret: SecretKey,
|
||||
public: Vec<u8>,
|
||||
auth_secret: Vec<u8>,
|
||||
}
|
||||
|
||||
impl EceKeyPair {
|
||||
pub fn generate() -> Result<Self, PushError> {
|
||||
let mut scalar = [0_u8; 32];
|
||||
rand::rng().fill_bytes(&mut scalar);
|
||||
let secret = SecretKey::from_slice(&scalar)
|
||||
.map_err(|_| PushError::new("push_key_generation", "Could not create a push key"))?;
|
||||
let mut auth_secret = vec![0_u8; AUTH_SECRET_BYTES];
|
||||
rand::rng().fill_bytes(&mut auth_secret);
|
||||
Ok(Self {
|
||||
public: secret
|
||||
.public_key()
|
||||
.to_encoded_point(false)
|
||||
.as_bytes()
|
||||
.to_vec(),
|
||||
secret,
|
||||
auth_secret,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn from_parts(
|
||||
private: &[u8],
|
||||
public: &[u8],
|
||||
auth_secret: &[u8],
|
||||
) -> Result<Self, PushError> {
|
||||
if public.len() != PUBLIC_KEY_BYTES || auth_secret.len() != AUTH_SECRET_BYTES {
|
||||
return Err(PushError::new(
|
||||
"push_key_invalid",
|
||||
"The stored push key material is malformed",
|
||||
));
|
||||
}
|
||||
let secret = SecretKey::from_slice(private)
|
||||
.map_err(|_| PushError::new("push_key_invalid", "The stored push key is invalid"))?;
|
||||
let derived = secret.public_key().to_encoded_point(false);
|
||||
if derived.as_bytes() != public {
|
||||
return Err(PushError::new(
|
||||
"push_key_mismatch",
|
||||
"The stored push key does not match its public key",
|
||||
));
|
||||
}
|
||||
Ok(Self {
|
||||
secret,
|
||||
public: public.to_vec(),
|
||||
auth_secret: auth_secret.to_vec(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn public_key(&self) -> &[u8] {
|
||||
&self.public
|
||||
}
|
||||
|
||||
pub fn auth_secret(&self) -> &[u8] {
|
||||
&self.auth_secret
|
||||
}
|
||||
|
||||
pub fn private_key(&self) -> Vec<u8> {
|
||||
self.secret.to_bytes().to_vec()
|
||||
}
|
||||
|
||||
fn shared_secret(&self, server_public: &[u8]) -> Result<Vec<u8>, PushError> {
|
||||
let key = PublicKey::from_sec1_bytes(server_public).map_err(|_| {
|
||||
PushError::new(
|
||||
"push_decrypt_invalid",
|
||||
"The message key header is not a valid public key",
|
||||
)
|
||||
})?;
|
||||
let shared = ecdh::diffie_hellman(self.secret.to_nonzero_scalar(), key.as_affine());
|
||||
Ok(shared.raw_secret_bytes().to_vec())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decrypt_web_push(
|
||||
keys: &EceKeyPair,
|
||||
content_encoding: Option<&str>,
|
||||
crypto_key: Option<&str>,
|
||||
encryption: Option<&str>,
|
||||
body: &[u8],
|
||||
) -> Result<Vec<u8>, PushError> {
|
||||
match content_encoding {
|
||||
Some(encoding) if encoding.eq_ignore_ascii_case("aes128gcm") => {
|
||||
decrypt_aes128gcm_body(keys, body)
|
||||
}
|
||||
Some(encoding) if encoding.eq_ignore_ascii_case("aesgcm") => {
|
||||
decrypt_legacy_body(keys, crypto_key, encryption, body)
|
||||
}
|
||||
Some(_) => Err(PushError::new(
|
||||
"push_decrypt_unsupported",
|
||||
"The message declares an unsupported content encoding",
|
||||
)),
|
||||
None if crypto_key.is_some() && encryption.is_some() => {
|
||||
decrypt_legacy_body(keys, crypto_key, encryption, body)
|
||||
}
|
||||
None => decrypt_aes128gcm_body(keys, body),
|
||||
}
|
||||
}
|
||||
|
||||
fn decrypt_legacy_body(
|
||||
keys: &EceKeyPair,
|
||||
crypto_key: Option<&str>,
|
||||
encryption: Option<&str>,
|
||||
body: &[u8],
|
||||
) -> Result<Vec<u8>, PushError> {
|
||||
let crypto_key = crypto_key.ok_or_else(|| {
|
||||
PushError::new(
|
||||
"push_decrypt_invalid",
|
||||
"The message is missing its key header",
|
||||
)
|
||||
})?;
|
||||
let encryption = encryption.ok_or_else(|| {
|
||||
PushError::new(
|
||||
"push_decrypt_invalid",
|
||||
"The message is missing its salt header",
|
||||
)
|
||||
})?;
|
||||
let dh = parameter(crypto_key, "dh").ok_or_else(|| {
|
||||
PushError::new(
|
||||
"push_decrypt_invalid",
|
||||
"The message is missing its key header",
|
||||
)
|
||||
})?;
|
||||
let salt = parameter(encryption, "salt").ok_or_else(|| {
|
||||
PushError::new(
|
||||
"push_decrypt_invalid",
|
||||
"The message is missing its salt header",
|
||||
)
|
||||
})?;
|
||||
decrypt_aesgcm(keys, &dh, &salt, body)
|
||||
}
|
||||
|
||||
fn parameter(value: &str, name: &str) -> Option<Vec<u8>> {
|
||||
value.split(';').find_map(|part| {
|
||||
part.trim()
|
||||
.strip_prefix(name)
|
||||
.and_then(|rest| rest.strip_prefix('='))
|
||||
.and_then(base64_url_decode)
|
||||
})
|
||||
}
|
||||
|
||||
fn base64_url_decode(value: &str) -> Option<Vec<u8>> {
|
||||
use base64::Engine as _;
|
||||
base64::engine::general_purpose::URL_SAFE_NO_PAD
|
||||
.decode(value.trim())
|
||||
.ok()
|
||||
}
|
||||
|
||||
pub fn decrypt_aes128gcm_body(keys: &EceKeyPair, body: &[u8]) -> Result<Vec<u8>, PushError> {
|
||||
if body.len() < AES128GCM_HEADER_BYTES + TAG_BYTES {
|
||||
return Err(PushError::new(
|
||||
"push_decrypt_invalid",
|
||||
"The encrypted message is too short",
|
||||
));
|
||||
}
|
||||
let salt = &body[0..16];
|
||||
let record_size = u32::from_be_bytes([body[16], body[17], body[18], body[19]]);
|
||||
let id_length = body[20] as usize;
|
||||
if body.len() < AES128GCM_HEADER_BYTES + id_length + TAG_BYTES {
|
||||
return Err(PushError::new(
|
||||
"push_decrypt_invalid",
|
||||
"The encrypted message header is malformed",
|
||||
));
|
||||
}
|
||||
let server_public = &body[AES128GCM_HEADER_BYTES..AES128GCM_HEADER_BYTES + id_length];
|
||||
let records = &body[AES128GCM_HEADER_BYTES + id_length..];
|
||||
let shared = keys.shared_secret(server_public)?;
|
||||
|
||||
let mut key_info = Vec::with_capacity(19 + PUBLIC_KEY_BYTES * 2);
|
||||
key_info.extend_from_slice(b"WebPush: info\0");
|
||||
key_info.extend_from_slice(&keys.public);
|
||||
key_info.extend_from_slice(server_public);
|
||||
|
||||
let mut input_key = [0_u8; 32];
|
||||
Hkdf::<Sha256>::new(Some(&keys.auth_secret), &shared)
|
||||
.expand(&key_info, &mut input_key)
|
||||
.map_err(|_| PushError::new("push_decrypt_failed", "The message key derivation failed"))?;
|
||||
let (content_key, base_nonce) = derive_content_keys(&input_key, salt)?;
|
||||
decode_aes128gcm_records(&content_key, &base_nonce, record_size, records)
|
||||
}
|
||||
|
||||
pub fn derive_content_keys(
|
||||
input_key: &[u8],
|
||||
salt: &[u8],
|
||||
) -> Result<([u8; 16], [u8; 12]), PushError> {
|
||||
let prk = Hkdf::<Sha256>::new(Some(salt), input_key);
|
||||
let mut content_key = [0_u8; 16];
|
||||
let mut nonce = [0_u8; 12];
|
||||
prk.expand(b"Content-Encoding: aes128gcm\0", &mut content_key)
|
||||
.map_err(|_| PushError::new("push_decrypt_failed", "The message key derivation failed"))?;
|
||||
prk.expand(b"Content-Encoding: nonce\0", &mut nonce)
|
||||
.map_err(|_| {
|
||||
PushError::new("push_decrypt_failed", "The message nonce derivation failed")
|
||||
})?;
|
||||
Ok((content_key, nonce))
|
||||
}
|
||||
|
||||
pub fn decode_aes128gcm_records(
|
||||
content_key: &[u8; 16],
|
||||
base_nonce: &[u8; 12],
|
||||
record_size: u32,
|
||||
records: &[u8],
|
||||
) -> Result<Vec<u8>, PushError> {
|
||||
if !(MINIMUM_RECORD_SIZE..=MAXIMUM_RECORD_SIZE).contains(&record_size) {
|
||||
return Err(PushError::new(
|
||||
"push_decrypt_invalid",
|
||||
"The message record size is outside the accepted range",
|
||||
));
|
||||
}
|
||||
if records.is_empty() {
|
||||
return Err(PushError::new(
|
||||
"push_decrypt_invalid",
|
||||
"The encrypted message carries no record",
|
||||
));
|
||||
}
|
||||
let cipher = Aes128Gcm::new_from_slice(content_key)
|
||||
.map_err(|_| PushError::new("push_decrypt_failed", "The message cipher could not start"))?;
|
||||
let mut plaintext = Vec::new();
|
||||
let mut sequence = 0_u64;
|
||||
let mut remaining = records;
|
||||
while !remaining.is_empty() {
|
||||
let take = remaining.len().min(record_size as usize);
|
||||
let (record, rest) = remaining.split_at(take);
|
||||
remaining = rest;
|
||||
if record.len() < TAG_BYTES + 1 {
|
||||
return Err(PushError::new(
|
||||
"push_decrypt_invalid",
|
||||
"A message record is too short",
|
||||
));
|
||||
}
|
||||
let last = remaining.is_empty();
|
||||
plaintext.extend_from_slice(&decrypt_record(
|
||||
&cipher, base_nonce, sequence, record, last,
|
||||
)?);
|
||||
sequence += 1;
|
||||
}
|
||||
Ok(plaintext)
|
||||
}
|
||||
|
||||
fn decrypt_record(
|
||||
cipher: &Aes128Gcm,
|
||||
base_nonce: &[u8; 12],
|
||||
sequence: u64,
|
||||
record: &[u8],
|
||||
last: bool,
|
||||
) -> Result<Vec<u8>, PushError> {
|
||||
let sequence_bytes = sequence.to_be_bytes();
|
||||
let mut nonce = *base_nonce;
|
||||
for (target, byte) in nonce[4..].iter_mut().zip(sequence_bytes.iter()) {
|
||||
*target ^= *byte;
|
||||
}
|
||||
let decrypted = cipher
|
||||
.decrypt(Nonce::from_slice(&nonce), record)
|
||||
.map_err(|_| PushError::new("push_decrypt_failed", "The message could not be decrypted"))?;
|
||||
let end = decrypted
|
||||
.iter()
|
||||
.rposition(|byte| *byte != 0)
|
||||
.ok_or_else(|| {
|
||||
PushError::new(
|
||||
"push_decrypt_invalid",
|
||||
"The decrypted record carries no padding delimiter",
|
||||
)
|
||||
})?;
|
||||
let delimiter = decrypted[end];
|
||||
let expected = if last { 2 } else { 1 };
|
||||
if delimiter != expected {
|
||||
return Err(PushError::new(
|
||||
"push_decrypt_invalid",
|
||||
"The decrypted record carries an invalid padding delimiter",
|
||||
));
|
||||
}
|
||||
Ok(decrypted[..end].to_vec())
|
||||
}
|
||||
|
||||
pub fn decrypt_aesgcm(
|
||||
keys: &EceKeyPair,
|
||||
server_public: &[u8],
|
||||
salt: &[u8],
|
||||
payload: &[u8],
|
||||
) -> Result<Vec<u8>, PushError> {
|
||||
if salt.len() != SALT_BYTES {
|
||||
return Err(PushError::new(
|
||||
"push_decrypt_invalid",
|
||||
"The message salt is outside the accepted range",
|
||||
));
|
||||
}
|
||||
if payload.len() <= TAG_BYTES + 2 {
|
||||
return Err(PushError::new(
|
||||
"push_decrypt_invalid",
|
||||
"The encrypted message is too short",
|
||||
));
|
||||
}
|
||||
if server_public.len() != PUBLIC_KEY_BYTES {
|
||||
return Err(PushError::new(
|
||||
"push_decrypt_invalid",
|
||||
"The message key header is malformed",
|
||||
));
|
||||
}
|
||||
let shared = keys.shared_secret(server_public)?;
|
||||
let mut input_key = [0_u8; 32];
|
||||
Hkdf::<Sha256>::new(Some(&keys.auth_secret), &shared)
|
||||
.expand(b"Content-Encoding: auth\0", &mut input_key)
|
||||
.map_err(|_| PushError::new("push_decrypt_failed", "The message key derivation failed"))?;
|
||||
let mut context = Vec::with_capacity(134);
|
||||
context.extend_from_slice(&[0, PUBLIC_KEY_BYTES as u8]);
|
||||
context.extend_from_slice(&keys.public);
|
||||
context.extend_from_slice(&[0, PUBLIC_KEY_BYTES as u8]);
|
||||
context.extend_from_slice(server_public);
|
||||
let mut key_info = b"Content-Encoding: aesgcm\0P-256\0".to_vec();
|
||||
key_info.extend_from_slice(&context);
|
||||
let mut nonce_info = b"Content-Encoding: nonce\0P-256\0".to_vec();
|
||||
nonce_info.extend_from_slice(&context);
|
||||
let prk = Hkdf::<Sha256>::new(Some(salt), &input_key);
|
||||
let mut content_key = [0_u8; 16];
|
||||
let mut nonce = [0_u8; NONCE_BYTES];
|
||||
prk.expand(&key_info, &mut content_key)
|
||||
.map_err(|_| PushError::new("push_decrypt_failed", "The message key derivation failed"))?;
|
||||
prk.expand(&nonce_info, &mut nonce).map_err(|_| {
|
||||
PushError::new("push_decrypt_failed", "The message nonce derivation failed")
|
||||
})?;
|
||||
let cipher = Aes128Gcm::new_from_slice(&content_key)
|
||||
.map_err(|_| PushError::new("push_decrypt_failed", "The message cipher could not start"))?;
|
||||
let mut plaintext = cipher
|
||||
.decrypt(Nonce::from_slice(&nonce), payload)
|
||||
.map_err(|_| PushError::new("push_decrypt_failed", "The message could not be decrypted"))?;
|
||||
if plaintext.len() <= 2 {
|
||||
return Err(PushError::new(
|
||||
"push_decrypt_invalid",
|
||||
"The decrypted message is too short",
|
||||
));
|
||||
}
|
||||
let padding = ((plaintext[0] as usize) << 8) | plaintext[1] as usize;
|
||||
let start = 2 + padding;
|
||||
if start >= plaintext.len() {
|
||||
return Err(PushError::new(
|
||||
"push_decrypt_invalid",
|
||||
"The decrypted message carries invalid padding",
|
||||
));
|
||||
}
|
||||
if plaintext[2..start].iter().any(|byte| *byte != 0) {
|
||||
return Err(PushError::new(
|
||||
"push_decrypt_invalid",
|
||||
"The decrypted message carries invalid padding",
|
||||
));
|
||||
}
|
||||
Ok(plaintext.split_off(start))
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
use crate::push::PushEvent;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
pub const EVENT_NAME: &str = "account.push.changed";
|
||||
|
||||
pub fn payload(event: &PushEvent, generation: u64) -> Value {
|
||||
match event {
|
||||
PushEvent::LibraryChanged { changed_ids } => json!({
|
||||
"generation": generation,
|
||||
"kind": "library",
|
||||
"changedIds": changed_ids,
|
||||
}),
|
||||
PushEvent::FavoritesChanged { changed_ids } => json!({
|
||||
"generation": generation,
|
||||
"kind": "favorites",
|
||||
"changedIds": changed_ids,
|
||||
}),
|
||||
PushEvent::SubscriptionChanged { changed_ids } => json!({
|
||||
"generation": generation,
|
||||
"kind": "subscription",
|
||||
"changedIds": changed_ids,
|
||||
}),
|
||||
PushEvent::LinkedAccountChanged {
|
||||
account_type,
|
||||
linked,
|
||||
} => json!({
|
||||
"generation": generation,
|
||||
"kind": "linked-account",
|
||||
"accountType": account_type,
|
||||
"linked": linked,
|
||||
}),
|
||||
PushEvent::PlatformSyncChanged {
|
||||
platform_code,
|
||||
sync_state,
|
||||
sync_date,
|
||||
sync_game_count,
|
||||
} => json!({
|
||||
"generation": generation,
|
||||
"kind": "platform-sync",
|
||||
"platformCode": platform_code,
|
||||
"syncState": sync_state,
|
||||
"syncDate": sync_date,
|
||||
"syncGameCount": sync_game_count,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn library_events_carry_the_generation_and_changed_ids() {
|
||||
let value = payload(
|
||||
&PushEvent::LibraryChanged {
|
||||
changed_ids: vec!["app-1".into()],
|
||||
},
|
||||
7,
|
||||
);
|
||||
assert_eq!(value["kind"], "library");
|
||||
assert_eq!(value["generation"], 7);
|
||||
assert_eq!(value["changedIds"][0], "app-1");
|
||||
assert_eq!(EVENT_NAME, "account.push.changed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn platform_sync_events_never_claim_completion() {
|
||||
let value = payload(
|
||||
&PushEvent::PlatformSyncChanged {
|
||||
platform_code: Some("STEAM".into()),
|
||||
sync_state: Some("SYNCING".into()),
|
||||
sync_date: Some("2026-01-01T00:00:00Z".into()),
|
||||
sync_game_count: Some(12),
|
||||
},
|
||||
9,
|
||||
);
|
||||
assert_eq!(value["kind"], "platform-sync");
|
||||
assert_eq!(value["syncState"], "SYNCING");
|
||||
assert!(value.get("completed").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn linked_account_and_favorites_payloads_are_explicit() {
|
||||
let linked = payload(
|
||||
&PushEvent::LinkedAccountChanged {
|
||||
account_type: Some("STEAM".into()),
|
||||
linked: Some(true),
|
||||
},
|
||||
1,
|
||||
);
|
||||
assert_eq!(linked["accountType"], "STEAM");
|
||||
assert_eq!(linked["linked"], true);
|
||||
let favorites = payload(
|
||||
&PushEvent::FavoritesChanged {
|
||||
changed_ids: vec![],
|
||||
},
|
||||
2,
|
||||
);
|
||||
assert_eq!(favorites["kind"], "favorites");
|
||||
assert_eq!(favorites["changedIds"].as_array().unwrap().len(), 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
mod config;
|
||||
mod decrypt;
|
||||
mod event;
|
||||
mod owner;
|
||||
mod protocol;
|
||||
mod registration;
|
||||
mod store;
|
||||
mod transport;
|
||||
mod vendor_default;
|
||||
|
||||
pub use config::{
|
||||
FILE_NAME as PUSH_CONFIG_FILE, PushConfig, load_for_provider as push_config_for_provider,
|
||||
};
|
||||
pub use decrypt::{EceKeyPair, decrypt_web_push};
|
||||
pub use event::{EVENT_NAME as PUSH_EVENT_NAME, payload as push_event_payload};
|
||||
pub use owner::{
|
||||
GfnTokenSource, PushEvent, PushOwner, PushOwnerConfig, PushOwnerDeps, PushScope, PushSink,
|
||||
ScopeSource,
|
||||
};
|
||||
pub use registration::{
|
||||
DEVICE_IDENTITY_REJECTED, HttpMethod, HttpRequest, HttpResponse, PushEndpoints, PushHttp,
|
||||
PushIdentity, Registration, RegistrationClient, ReqwestPushHttp, checkin_and_register,
|
||||
pns_unregister,
|
||||
};
|
||||
pub use store::{PushStateStore, RegistrationStore};
|
||||
pub use vendor_default::config_for_provider as bundled_push_config;
|
||||
|
||||
pub const EVENT_NAME: &str = crate::push::event::EVENT_NAME;
|
||||
pub use transport::{
|
||||
PushTransport, PushTransportFactory, TlsPushTransport, TlsPushTransportFactory,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PushError {
|
||||
pub code: &'static str,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl PushError {
|
||||
pub fn new(code: &'static str, message: impl Into<String>) -> Self {
|
||||
Self {
|
||||
code,
|
||||
message: message.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum MessageType {
|
||||
ProductChange,
|
||||
AssetChange,
|
||||
SessionChange,
|
||||
LayoutChange,
|
||||
GswsSync,
|
||||
SectionChange,
|
||||
LinkedAccountChange,
|
||||
AppChange,
|
||||
ServerInfoSync,
|
||||
ConfigurationChange,
|
||||
SubscriptionChange,
|
||||
PatchingEvent,
|
||||
KvStoreChange,
|
||||
LibraryChange,
|
||||
PlatformSyncChange,
|
||||
CampaignChange,
|
||||
FavoritesChange,
|
||||
}
|
||||
|
||||
impl MessageType {
|
||||
pub fn parse(value: &str) -> Option<Self> {
|
||||
Some(match value {
|
||||
"PRODUCT_CHANGE" => Self::ProductChange,
|
||||
"ASSET_CHANGE" => Self::AssetChange,
|
||||
"SESSION_CHANGE" => Self::SessionChange,
|
||||
"LAYOUT_CHANGE" => Self::LayoutChange,
|
||||
"GSWS_SYNC" => Self::GswsSync,
|
||||
"SECTION_CHANGE" => Self::SectionChange,
|
||||
"LINKEDACCOUNT_CHANGE" => Self::LinkedAccountChange,
|
||||
"APP_CHANGE" => Self::AppChange,
|
||||
"SERVER_INFO_SYNC" => Self::ServerInfoSync,
|
||||
"CONFIGURATION_CHANGE" => Self::ConfigurationChange,
|
||||
"SUBSCRIPTION_CHANGE" => Self::SubscriptionChange,
|
||||
"PATCHING_EVENT" => Self::PatchingEvent,
|
||||
"KV_STORE_CHANGE" => Self::KvStoreChange,
|
||||
"LIBRARY_CHANGE" => Self::LibraryChange,
|
||||
"PLATFORM_SYNC_CHANGE" => Self::PlatformSyncChange,
|
||||
"CAMPAIGN_CHANGE" => Self::CampaignChange,
|
||||
"FAVORITES_CHANGE" => Self::FavoritesChange,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct PushMessage {
|
||||
pub message_type: Option<MessageType>,
|
||||
pub region_ids: Vec<String>,
|
||||
pub changed_ids: Vec<String>,
|
||||
pub change_id_type: Option<String>,
|
||||
pub platform_code: Option<String>,
|
||||
pub sync_state: Option<String>,
|
||||
pub sync_date: Option<String>,
|
||||
pub sync_game_count: Option<u64>,
|
||||
pub account_type: Option<String>,
|
||||
pub account_linked: Option<bool>,
|
||||
}
|
||||
|
||||
impl PushMessage {
|
||||
pub fn parse(data: &serde_json::Map<String, serde_json::Value>) -> Option<Self> {
|
||||
let mut message = PushMessage::default();
|
||||
let mut saw_type = false;
|
||||
for (key, value) in data {
|
||||
match key.as_str() {
|
||||
"messageType" => {
|
||||
let text = json_string(value)?;
|
||||
message.message_type = MessageType::parse(&text);
|
||||
message.message_type?;
|
||||
saw_type = true;
|
||||
}
|
||||
"changedIds" => message.changed_ids = json_string_list(value),
|
||||
"changeIdType" => message.change_id_type = json_string(value),
|
||||
"regionIds" => message.region_ids = json_string_list(value),
|
||||
"platformSyncInfo" => {
|
||||
let text = json_string(value)?;
|
||||
let info: serde_json::Value = serde_json::from_str(&text).ok()?;
|
||||
message.platform_code = info["platformCode"].as_str().map(str::to_owned);
|
||||
message.sync_state = info["syncState"].as_str().map(str::to_owned);
|
||||
message.sync_date = info["syncDate"].as_str().map(str::to_owned);
|
||||
message.sync_game_count = info["syncGameCount"].as_u64();
|
||||
}
|
||||
"accountLinkedInfo" => {
|
||||
let text = json_string(value)?;
|
||||
let info: serde_json::Value = serde_json::from_str(&text).ok()?;
|
||||
message.account_type = info["accountType"].as_str().map(str::to_owned);
|
||||
message.account_linked = info["linked"].as_bool();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
saw_type.then_some(message)
|
||||
}
|
||||
}
|
||||
|
||||
fn json_string(value: &serde_json::Value) -> Option<String> {
|
||||
if let Some(text) = value.as_str() {
|
||||
if let Ok(inner) = serde_json::from_str::<String>(text) {
|
||||
return Some(inner);
|
||||
}
|
||||
return Some(text.to_owned());
|
||||
}
|
||||
Some(value.to_string())
|
||||
}
|
||||
|
||||
fn json_string_list(value: &serde_json::Value) -> Vec<String> {
|
||||
let text = json_string(value).unwrap_or_default();
|
||||
serde_json::from_str::<Vec<String>>(&text).unwrap_or_default()
|
||||
}
|
||||
|
||||
pub const MAXIMUM_FRAME_BYTES: usize = 64 * 1024;
|
||||
pub const MAXIMUM_CHANGED_IDS: usize = 64;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
+1179
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,480 @@
|
||||
use crate::push::PushError;
|
||||
|
||||
pub const MCS_VERSION: u8 = 41;
|
||||
|
||||
pub const TAG_HEARTBEAT_PING: u8 = 0;
|
||||
pub const TAG_HEARTBEAT_ACK: u8 = 1;
|
||||
pub const TAG_LOGIN_REQUEST: u8 = 2;
|
||||
pub const TAG_LOGIN_RESPONSE: u8 = 3;
|
||||
pub const TAG_CLOSE: u8 = 4;
|
||||
pub const TAG_IQ_STANZA: u8 = 7;
|
||||
pub const TAG_DATA_MESSAGE_STANZA: u8 = 8;
|
||||
|
||||
pub const STREAM_ACK_EXTENSION_ID: u64 = 13;
|
||||
pub const IQ_TYPE_SET: u64 = 1;
|
||||
|
||||
pub const AUTH_SERVICE_ANDROID_ID: u64 = 2;
|
||||
|
||||
pub struct CheckinRequest {
|
||||
pub android_id: Option<i64>,
|
||||
pub security_token: Option<u64>,
|
||||
}
|
||||
|
||||
pub struct CheckinResponse {
|
||||
pub android_id: Option<u64>,
|
||||
pub security_token: Option<u64>,
|
||||
}
|
||||
|
||||
pub struct LoginResponse {
|
||||
pub error_code: Option<i32>,
|
||||
pub heartbeat_interval_ms: Option<i32>,
|
||||
}
|
||||
|
||||
pub struct DataMessage {
|
||||
pub from: String,
|
||||
pub category: String,
|
||||
pub app_data: Vec<(String, String)>,
|
||||
pub persistent_id: Option<String>,
|
||||
pub raw_data: Option<Vec<u8>>,
|
||||
pub immediate_ack: bool,
|
||||
}
|
||||
|
||||
pub enum Frame {
|
||||
HeartbeatPing,
|
||||
HeartbeatAck,
|
||||
LoginResponse(LoginResponse),
|
||||
DataMessage(Box<DataMessage>),
|
||||
Close,
|
||||
Other,
|
||||
}
|
||||
|
||||
fn write_frame(versioned: bool, tag: u8, payload: &[u8]) -> Vec<u8> {
|
||||
let mut frame = Vec::with_capacity(payload.len() + 8);
|
||||
if versioned {
|
||||
frame.push(MCS_VERSION);
|
||||
}
|
||||
frame.push(tag);
|
||||
write_varint(payload.len() as u64, &mut frame);
|
||||
frame.extend_from_slice(payload);
|
||||
frame
|
||||
}
|
||||
|
||||
fn heartbeat(last_stream_id_received: Option<u64>) -> Vec<u8> {
|
||||
let mut payload = Vec::with_capacity(12);
|
||||
if let Some(stream_id) = last_stream_id_received {
|
||||
write_varint_field(2, stream_id, &mut payload);
|
||||
}
|
||||
payload
|
||||
}
|
||||
|
||||
pub fn encode_heartbeat_ack(last_stream_id_received: Option<u64>) -> Vec<u8> {
|
||||
write_frame(
|
||||
false,
|
||||
TAG_HEARTBEAT_ACK,
|
||||
&heartbeat(last_stream_id_received),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn encode_heartbeat_ping(last_stream_id_received: Option<u64>) -> Vec<u8> {
|
||||
write_frame(
|
||||
false,
|
||||
TAG_HEARTBEAT_PING,
|
||||
&heartbeat(last_stream_id_received),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn encode_close() -> Vec<u8> {
|
||||
write_frame(false, TAG_CLOSE, &[])
|
||||
}
|
||||
|
||||
pub fn encode_stream_ack(last_stream_id_received: Option<u64>) -> Vec<u8> {
|
||||
let mut payload = Vec::with_capacity(24);
|
||||
write_varint_field(2, IQ_TYPE_SET, &mut payload);
|
||||
write_string(3, "", &mut payload);
|
||||
let mut extension = Vec::with_capacity(8);
|
||||
write_varint_field(1, STREAM_ACK_EXTENSION_ID, &mut extension);
|
||||
write_bytes(2, &[], &mut extension);
|
||||
write_bytes(7, &extension, &mut payload);
|
||||
if let Some(stream_id) = last_stream_id_received {
|
||||
write_varint_field(10, stream_id, &mut payload);
|
||||
}
|
||||
write_frame(false, TAG_IQ_STANZA, &payload)
|
||||
}
|
||||
|
||||
pub fn encode_login_request(
|
||||
android_id: i64,
|
||||
security_token: u64,
|
||||
received_persistent_ids: &[String],
|
||||
) -> Vec<u8> {
|
||||
let mut payload = Vec::with_capacity(256 + received_persistent_ids.len() * 32);
|
||||
write_string(
|
||||
1,
|
||||
&format!("chrome-{}", env!("CARGO_PKG_VERSION")),
|
||||
&mut payload,
|
||||
);
|
||||
write_string(2, "mcs.android.com", &mut payload);
|
||||
write_string(3, &android_id.to_string(), &mut payload);
|
||||
write_string(4, &android_id.to_string(), &mut payload);
|
||||
write_string(5, &security_token.to_string(), &mut payload);
|
||||
write_string(6, &format!("android-{android_id:x}"), &mut payload);
|
||||
write_setting("new_vc", "1", &mut payload);
|
||||
for id in received_persistent_ids.iter().take(64) {
|
||||
write_string(10, id, &mut payload);
|
||||
}
|
||||
write_bool(12, false, &mut payload);
|
||||
write_bool(14, true, &mut payload);
|
||||
write_varint_field(16, AUTH_SERVICE_ANDROID_ID, &mut payload);
|
||||
write_varint_field(17, 1, &mut payload);
|
||||
write_frame(true, TAG_LOGIN_REQUEST, &payload)
|
||||
}
|
||||
|
||||
pub fn decode_frame(tag: u8, payload: &[u8]) -> Result<Frame, PushError> {
|
||||
Ok(match tag {
|
||||
TAG_HEARTBEAT_PING => Frame::HeartbeatPing,
|
||||
TAG_HEARTBEAT_ACK => Frame::HeartbeatAck,
|
||||
TAG_CLOSE => Frame::Close,
|
||||
TAG_LOGIN_RESPONSE => Frame::LoginResponse(decode_login_response(payload)?),
|
||||
TAG_DATA_MESSAGE_STANZA => Frame::DataMessage(Box::new(decode_data_message(payload)?)),
|
||||
_ => Frame::Other,
|
||||
})
|
||||
}
|
||||
|
||||
fn decode_login_response(payload: &[u8]) -> Result<LoginResponse, PushError> {
|
||||
let mut reader = Reader::new(payload);
|
||||
let mut response = LoginResponse {
|
||||
error_code: None,
|
||||
heartbeat_interval_ms: None,
|
||||
};
|
||||
while let Some((field, wire)) = reader.next_tag()? {
|
||||
match (field, wire) {
|
||||
(3, WIRE_LENGTH) => {
|
||||
let nested = reader.take_length()?;
|
||||
let mut error = Reader::new(nested);
|
||||
while let Some((inner, inner_wire)) = error.next_tag()? {
|
||||
if inner == 1 && inner_wire == WIRE_VARINT {
|
||||
response.error_code = Some(error.read_varint()? as i32);
|
||||
} else {
|
||||
error.skip(inner_wire)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
(7, WIRE_LENGTH) => {
|
||||
let nested = reader.take_length()?;
|
||||
let mut config = Reader::new(nested);
|
||||
while let Some((inner, inner_wire)) = config.next_tag()? {
|
||||
if inner == 3 && inner_wire == WIRE_VARINT {
|
||||
response.heartbeat_interval_ms = Some(config.read_varint()? as i32);
|
||||
} else {
|
||||
config.skip(inner_wire)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => reader.skip(wire)?,
|
||||
}
|
||||
}
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
fn decode_data_message(payload: &[u8]) -> Result<DataMessage, PushError> {
|
||||
let mut reader = Reader::new(payload);
|
||||
let mut message = DataMessage {
|
||||
from: String::new(),
|
||||
category: String::new(),
|
||||
app_data: Vec::new(),
|
||||
persistent_id: None,
|
||||
raw_data: None,
|
||||
immediate_ack: false,
|
||||
};
|
||||
while let Some((field, wire)) = reader.next_tag()? {
|
||||
match (field, wire) {
|
||||
(3, WIRE_LENGTH) => message.from = reader.read_string()?,
|
||||
(5, WIRE_LENGTH) => message.category = reader.read_string()?,
|
||||
(7, WIRE_LENGTH) => {
|
||||
let nested = reader.take_length()?;
|
||||
let mut app = Reader::new(nested);
|
||||
let mut key = String::new();
|
||||
let mut value = String::new();
|
||||
while let Some((inner, inner_wire)) = app.next_tag()? {
|
||||
match (inner, inner_wire) {
|
||||
(1, WIRE_LENGTH) => key = app.read_string()?,
|
||||
(2, WIRE_LENGTH) => value = app.read_string()?,
|
||||
_ => app.skip(inner_wire)?,
|
||||
}
|
||||
}
|
||||
if !key.is_empty() && message.app_data.len() < 128 {
|
||||
message.app_data.push((key, value));
|
||||
}
|
||||
}
|
||||
(9, WIRE_LENGTH) => message.persistent_id = Some(reader.read_string()?),
|
||||
(21, WIRE_LENGTH) => message.raw_data = Some(reader.take_length()?.to_vec()),
|
||||
(24, WIRE_VARINT) => message.immediate_ack = reader.read_varint()? != 0,
|
||||
_ => reader.skip(wire)?,
|
||||
}
|
||||
}
|
||||
Ok(message)
|
||||
}
|
||||
|
||||
pub fn encode_checkin_request(request: &CheckinRequest) -> Vec<u8> {
|
||||
let mut payload = Vec::with_capacity(128);
|
||||
if let Some(id) = request.android_id {
|
||||
write_varint_field(2, id as u64, &mut payload);
|
||||
}
|
||||
let mut checkin = Vec::with_capacity(48);
|
||||
write_varint_field(12, 3, &mut checkin);
|
||||
let mut chrome_build = Vec::with_capacity(32);
|
||||
write_varint_field(1, 3, &mut chrome_build);
|
||||
write_string(2, "63.0.3234.0", &mut chrome_build);
|
||||
write_varint_field(3, 1, &mut chrome_build);
|
||||
write_bytes(13, &chrome_build, &mut checkin);
|
||||
write_bytes(4, &checkin, &mut payload);
|
||||
write_varint_field(14, 3, &mut payload);
|
||||
write_varint_field(22, 0, &mut payload);
|
||||
if let Some(token) = request.security_token {
|
||||
write_fixed64(13, token, &mut payload);
|
||||
}
|
||||
payload
|
||||
}
|
||||
|
||||
pub fn decode_checkin_response(payload: &[u8]) -> Result<CheckinResponse, PushError> {
|
||||
let mut reader = Reader::new(payload);
|
||||
let mut response = CheckinResponse {
|
||||
android_id: None,
|
||||
security_token: None,
|
||||
};
|
||||
while let Some((field, wire)) = reader.next_tag()? {
|
||||
match (field, wire) {
|
||||
(7, WIRE_FIXED64) => response.android_id = Some(reader.read_fixed64()?),
|
||||
(8, WIRE_FIXED64) => response.security_token = Some(reader.read_fixed64()?),
|
||||
_ => reader.skip(wire)?,
|
||||
}
|
||||
}
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
pub struct FrameReader {
|
||||
buffer: Vec<u8>,
|
||||
expects_version: bool,
|
||||
}
|
||||
|
||||
impl Default for FrameReader {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl FrameReader {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
buffer: Vec::with_capacity(4096),
|
||||
expects_version: true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn extend(&mut self, bytes: &[u8]) {
|
||||
self.buffer.extend_from_slice(bytes);
|
||||
}
|
||||
|
||||
pub fn next_frame(&mut self, maximum: usize) -> Result<Option<(u8, Vec<u8>)>, PushError> {
|
||||
let mut position = 0;
|
||||
if self.expects_version {
|
||||
if self.buffer.len() < 2 {
|
||||
return Ok(None);
|
||||
}
|
||||
let version = self.buffer[0];
|
||||
if version < MCS_VERSION && version != 38 {
|
||||
return Err(PushError::new(
|
||||
"push_protocol_version",
|
||||
"The push service reported an unsupported protocol version",
|
||||
));
|
||||
}
|
||||
position = 1;
|
||||
}
|
||||
if self.buffer.len() <= position {
|
||||
return Ok(None);
|
||||
}
|
||||
let tag = self.buffer[position];
|
||||
position += 1;
|
||||
let mut size = 0_u64;
|
||||
let mut shift = 0;
|
||||
let mut header = 0;
|
||||
loop {
|
||||
let Some(byte) = self.buffer.get(position + header) else {
|
||||
return Ok(None);
|
||||
};
|
||||
if header >= 5 {
|
||||
return Err(PushError::new(
|
||||
"push_protocol_frame",
|
||||
"The push service sent an invalid frame size",
|
||||
));
|
||||
}
|
||||
size |= u64::from(byte & 0x7f) << shift;
|
||||
header += 1;
|
||||
if byte & 0x80 == 0 {
|
||||
break;
|
||||
}
|
||||
shift += 7;
|
||||
}
|
||||
if size as usize > maximum {
|
||||
return Err(PushError::new(
|
||||
"push_protocol_frame",
|
||||
"The push service sent an oversized frame",
|
||||
));
|
||||
}
|
||||
let start = position + header;
|
||||
let end = start + size as usize;
|
||||
if self.buffer.len() < end {
|
||||
return Ok(None);
|
||||
}
|
||||
let payload = self.buffer[start..end].to_vec();
|
||||
self.buffer.drain(..end);
|
||||
self.expects_version = false;
|
||||
Ok(Some((tag, payload)))
|
||||
}
|
||||
}
|
||||
|
||||
const WIRE_VARINT: u64 = 0;
|
||||
const WIRE_FIXED64: u64 = 1;
|
||||
const WIRE_LENGTH: u64 = 2;
|
||||
|
||||
struct Reader<'a> {
|
||||
data: &'a [u8],
|
||||
position: usize,
|
||||
}
|
||||
|
||||
impl<'a> Reader<'a> {
|
||||
fn new(data: &'a [u8]) -> Self {
|
||||
Self { data, position: 0 }
|
||||
}
|
||||
|
||||
fn next_tag(&mut self) -> Result<Option<(u64, u64)>, PushError> {
|
||||
if self.position >= self.data.len() {
|
||||
return Ok(None);
|
||||
}
|
||||
let tag = self.read_varint()?;
|
||||
Ok(Some((tag >> 3, tag & 0x7)))
|
||||
}
|
||||
|
||||
fn read_varint(&mut self) -> Result<u64, PushError> {
|
||||
let mut value = 0_u64;
|
||||
let mut shift = 0;
|
||||
loop {
|
||||
let byte = *self.data.get(self.position).ok_or_else(|| {
|
||||
PushError::new("push_protocol_frame", "The frame ended unexpectedly")
|
||||
})?;
|
||||
self.position += 1;
|
||||
if shift >= 64 {
|
||||
return Err(PushError::new(
|
||||
"push_protocol_frame",
|
||||
"The frame carried an oversized integer",
|
||||
));
|
||||
}
|
||||
value |= u64::from(byte & 0x7f) << shift;
|
||||
if byte & 0x80 == 0 {
|
||||
return Ok(value);
|
||||
}
|
||||
shift += 7;
|
||||
}
|
||||
}
|
||||
|
||||
fn read_fixed64(&mut self) -> Result<u64, PushError> {
|
||||
let end = self.position + 8;
|
||||
let bytes = self
|
||||
.data
|
||||
.get(self.position..end)
|
||||
.ok_or_else(|| PushError::new("push_protocol_frame", "The frame ended unexpectedly"))?;
|
||||
self.position = end;
|
||||
Ok(u64::from_le_bytes(bytes.try_into().map_err(|_| {
|
||||
PushError::new(
|
||||
"push_protocol_frame",
|
||||
"The frame carried an invalid integer",
|
||||
)
|
||||
})?))
|
||||
}
|
||||
|
||||
fn take_length(&mut self) -> Result<&'a [u8], PushError> {
|
||||
let length = self.read_varint()? as usize;
|
||||
if length > self.data.len().saturating_sub(self.position) {
|
||||
return Err(PushError::new(
|
||||
"push_protocol_frame",
|
||||
"The frame carried an oversized field",
|
||||
));
|
||||
}
|
||||
let end = self.position + length;
|
||||
let slice = &self.data[self.position..end];
|
||||
self.position = end;
|
||||
Ok(slice)
|
||||
}
|
||||
|
||||
fn read_string(&mut self) -> Result<String, PushError> {
|
||||
String::from_utf8(self.take_length()?.to_vec())
|
||||
.map_err(|_| PushError::new("push_protocol_frame", "The frame carried invalid text"))
|
||||
}
|
||||
|
||||
fn skip(&mut self, wire: u64) -> Result<(), PushError> {
|
||||
match wire {
|
||||
WIRE_VARINT => {
|
||||
self.read_varint()?;
|
||||
}
|
||||
WIRE_FIXED64 => {
|
||||
self.read_fixed64()?;
|
||||
}
|
||||
WIRE_LENGTH => {
|
||||
self.take_length()?;
|
||||
}
|
||||
_ => {
|
||||
return Err(PushError::new(
|
||||
"push_protocol_frame",
|
||||
"The frame used an unsupported field type",
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn write_varint(mut value: u64, out: &mut Vec<u8>) {
|
||||
loop {
|
||||
let byte = (value & 0x7f) as u8;
|
||||
value >>= 7;
|
||||
if value == 0 {
|
||||
out.push(byte);
|
||||
return;
|
||||
}
|
||||
out.push(byte | 0x80);
|
||||
}
|
||||
}
|
||||
|
||||
fn write_tag(field: u64, wire: u64, out: &mut Vec<u8>) {
|
||||
write_varint((field << 3) | wire, out);
|
||||
}
|
||||
|
||||
fn write_varint_field(field: u64, value: u64, out: &mut Vec<u8>) {
|
||||
write_tag(field, WIRE_VARINT, out);
|
||||
write_varint(value, out);
|
||||
}
|
||||
|
||||
fn write_bool(field: u64, value: bool, out: &mut Vec<u8>) {
|
||||
write_varint_field(field, u64::from(value), out);
|
||||
}
|
||||
|
||||
fn write_bytes(field: u64, value: &[u8], out: &mut Vec<u8>) {
|
||||
write_tag(field, WIRE_LENGTH, out);
|
||||
write_varint(value.len() as u64, out);
|
||||
out.extend_from_slice(value);
|
||||
}
|
||||
|
||||
fn write_string(field: u64, value: &str, out: &mut Vec<u8>) {
|
||||
write_bytes(field, value.as_bytes(), out);
|
||||
}
|
||||
|
||||
fn write_fixed64(field: u64, value: u64, out: &mut Vec<u8>) {
|
||||
write_tag(field, WIRE_FIXED64, out);
|
||||
out.extend_from_slice(&value.to_le_bytes());
|
||||
}
|
||||
|
||||
fn write_setting(name: &str, value: &str, out: &mut Vec<u8>) {
|
||||
let mut setting = Vec::with_capacity(name.len() + value.len() + 8);
|
||||
write_string(1, name, &mut setting);
|
||||
write_string(2, value, &mut setting);
|
||||
write_bytes(8, &setting, out);
|
||||
}
|
||||
@@ -0,0 +1,595 @@
|
||||
use crate::push::PushError;
|
||||
use crate::push::decrypt::EceKeyPair;
|
||||
use crate::push::protocol::{CheckinRequest, decode_checkin_response, encode_checkin_request};
|
||||
use base64::Engine as _;
|
||||
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io::Read as _;
|
||||
use std::time::Duration;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct PushIdentity {
|
||||
pub project_id: String,
|
||||
pub api_key: String,
|
||||
pub sender_id: String,
|
||||
pub app_id: String,
|
||||
pub firebase_app_id: String,
|
||||
pub vapid_key: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct PushEndpoints {
|
||||
pub checkin: String,
|
||||
pub c2dm: String,
|
||||
pub fis: String,
|
||||
pub fcm_registration: String,
|
||||
pub fcm_send: String,
|
||||
pub mcs_host: String,
|
||||
pub mcs_port: u16,
|
||||
pub pns: String,
|
||||
pub pns_client_id: String,
|
||||
}
|
||||
|
||||
impl Default for PushEndpoints {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
checkin: "https://android.clients.google.com/checkin".into(),
|
||||
c2dm: "https://android.clients.google.com/c2dm/register3".into(),
|
||||
fis: "https://firebaseinstallations.googleapis.com/v1".into(),
|
||||
fcm_registration: "https://fcmregistrations.googleapis.com/v1".into(),
|
||||
fcm_send: "https://fcm.googleapis.com/fcm/send".into(),
|
||||
mcs_host: "mtalk.google.com".into(),
|
||||
mcs_port: 5228,
|
||||
pns: String::new(),
|
||||
pns_client_id: String::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum HttpMethod {
|
||||
Get,
|
||||
Post,
|
||||
}
|
||||
|
||||
pub struct HttpRequest {
|
||||
pub url: String,
|
||||
pub method: HttpMethod,
|
||||
pub headers: Vec<(String, String)>,
|
||||
pub body: Vec<u8>,
|
||||
pub timeout: Duration,
|
||||
}
|
||||
|
||||
pub struct HttpResponse {
|
||||
pub status: u16,
|
||||
pub body: Vec<u8>,
|
||||
}
|
||||
|
||||
pub trait PushHttp: Send + Sync {
|
||||
fn execute(&self, request: &HttpRequest) -> Result<HttpResponse, PushError>;
|
||||
}
|
||||
|
||||
pub struct RegistrationClient<'a> {
|
||||
pub http: &'a dyn PushHttp,
|
||||
pub endpoints: &'a PushEndpoints,
|
||||
pub timeout: Duration,
|
||||
}
|
||||
|
||||
pub struct ReqwestPushHttp {
|
||||
client: reqwest::blocking::Client,
|
||||
}
|
||||
|
||||
impl ReqwestPushHttp {
|
||||
pub fn new() -> Result<Self, PushError> {
|
||||
let client = reqwest::blocking::Client::builder()
|
||||
.timeout(Duration::from_secs(30))
|
||||
.build()
|
||||
.map_err(|_| PushError::new("push_http_failed", "Could not start the push requests"))?;
|
||||
Ok(Self { client })
|
||||
}
|
||||
}
|
||||
|
||||
pub const MAXIMUM_RESPONSE_BYTES: u64 = 64 * 1024;
|
||||
pub const DEVICE_IDENTITY_REJECTED: &str = "push_device_identity_rejected";
|
||||
|
||||
pub(crate) fn read_bounded(reader: impl std::io::Read, limit: u64) -> Result<Vec<u8>, PushError> {
|
||||
let mut bytes = Vec::new();
|
||||
reader
|
||||
.take(limit + 1)
|
||||
.read_to_end(&mut bytes)
|
||||
.map_err(|_| PushError::new("push_http_failed", "The push response could not be read"))?;
|
||||
if bytes.len() as u64 > limit {
|
||||
return Err(PushError::new(
|
||||
"push_http_failed",
|
||||
"The push response exceeded the accepted size",
|
||||
));
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
impl PushHttp for ReqwestPushHttp {
|
||||
fn execute(&self, request: &HttpRequest) -> Result<HttpResponse, PushError> {
|
||||
let mut builder = match request.method {
|
||||
HttpMethod::Get => self.client.get(&request.url),
|
||||
HttpMethod::Post => self.client.post(&request.url),
|
||||
}
|
||||
.timeout(request.timeout);
|
||||
for (name, value) in &request.headers {
|
||||
builder = builder.header(name, value);
|
||||
}
|
||||
let response = builder
|
||||
.body(request.body.clone())
|
||||
.send()
|
||||
.map_err(|_| PushError::new("push_http_failed", "The push request failed"))?;
|
||||
let status = response.status().as_u16();
|
||||
let body = read_bounded(response, MAXIMUM_RESPONSE_BYTES)?;
|
||||
Ok(HttpResponse { status, body })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
pub struct Registration {
|
||||
pub android_id: i64,
|
||||
pub security_token: u64,
|
||||
pub gcm_token: String,
|
||||
pub fcm_token: String,
|
||||
pub endpoint: String,
|
||||
pub private_key: Vec<u8>,
|
||||
pub public_key: Vec<u8>,
|
||||
pub auth_secret: Vec<u8>,
|
||||
#[serde(default)]
|
||||
pub received_persistent_ids: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub created_at_seconds: u64,
|
||||
#[serde(default)]
|
||||
pub fingerprint: String,
|
||||
}
|
||||
|
||||
impl Registration {
|
||||
pub fn public_key(&self) -> &[u8] {
|
||||
&self.public_key
|
||||
}
|
||||
|
||||
pub fn auth_secret(&self) -> &[u8] {
|
||||
&self.auth_secret
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CheckinResult {
|
||||
pub android_id: i64,
|
||||
pub security_token: u64,
|
||||
}
|
||||
|
||||
pub fn checkin(
|
||||
client: &RegistrationClient<'_>,
|
||||
android_id: Option<i64>,
|
||||
security_token: Option<u64>,
|
||||
) -> Result<CheckinResult, PushError> {
|
||||
let body = encode_checkin_request(&CheckinRequest {
|
||||
android_id,
|
||||
security_token,
|
||||
});
|
||||
let response = client.http.execute(&HttpRequest {
|
||||
url: client.endpoints.checkin.clone(),
|
||||
method: HttpMethod::Post,
|
||||
headers: vec![("Content-Type".into(), "application/x-protobuf".into())],
|
||||
body,
|
||||
timeout: client.timeout,
|
||||
})?;
|
||||
if response.status == 401 || response.status == 400 {
|
||||
return Err(PushError::new(
|
||||
DEVICE_IDENTITY_REJECTED,
|
||||
"The device registration service rejected the stored device identity",
|
||||
));
|
||||
}
|
||||
if response.status != 200 {
|
||||
return Err(PushError::new(
|
||||
"push_checkin_refused",
|
||||
"The device registration service refused the check-in",
|
||||
));
|
||||
}
|
||||
let decoded = decode_checkin_response(&response.body)?;
|
||||
let android_id = decoded.android_id.ok_or_else(|| {
|
||||
PushError::new(
|
||||
"push_checkin_incomplete",
|
||||
"The check-in response did not carry a device identity",
|
||||
)
|
||||
})?;
|
||||
let security_token = decoded.security_token.ok_or_else(|| {
|
||||
PushError::new(
|
||||
"push_checkin_incomplete",
|
||||
"The check-in response did not carry a device token",
|
||||
)
|
||||
})?;
|
||||
if android_id == 0 || security_token == 0 {
|
||||
return Err(PushError::new(
|
||||
"push_checkin_incomplete",
|
||||
"The check-in response carried an empty device identity",
|
||||
));
|
||||
}
|
||||
Ok(CheckinResult {
|
||||
android_id: i64::try_from(android_id).map_err(|_| {
|
||||
PushError::new(
|
||||
"push_checkin_incomplete",
|
||||
"The device identity is outside the accepted range",
|
||||
)
|
||||
})?,
|
||||
security_token,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn gcm_register(
|
||||
client: &RegistrationClient<'_>,
|
||||
android_id: i64,
|
||||
security_token: u64,
|
||||
identity: &PushIdentity,
|
||||
) -> Result<String, PushError> {
|
||||
let mut form = format!(
|
||||
"app=org.chromium.linux&X-subtype={}&device={android_id}",
|
||||
urlencode(&identity.app_id)
|
||||
);
|
||||
if !identity.sender_id.is_empty() {
|
||||
form.push_str(&format!("&sender={}", urlencode(&identity.sender_id)));
|
||||
}
|
||||
let response = client.http.execute(&HttpRequest {
|
||||
url: client.endpoints.c2dm.clone(),
|
||||
method: HttpMethod::Post,
|
||||
headers: vec![
|
||||
(
|
||||
"Content-Type".into(),
|
||||
"application/x-www-form-urlencoded".into(),
|
||||
),
|
||||
(
|
||||
"Authorization".into(),
|
||||
format!("AidLogin {android_id}:{security_token}"),
|
||||
),
|
||||
],
|
||||
body: form.into_bytes(),
|
||||
timeout: client.timeout,
|
||||
})?;
|
||||
if response.status != 200 {
|
||||
return Err(PushError::new(
|
||||
"push_gcm_refused",
|
||||
"The messaging service refused the registration",
|
||||
));
|
||||
}
|
||||
let text = String::from_utf8_lossy(&response.body);
|
||||
let mut parts = text.split('=');
|
||||
let name = parts.next().unwrap_or_default();
|
||||
let value = parts.next().unwrap_or_default();
|
||||
if name != "token" || value.is_empty() {
|
||||
return Err(PushError::new(
|
||||
"push_gcm_refused",
|
||||
"The messaging service did not return a token",
|
||||
));
|
||||
}
|
||||
Ok(value.to_owned())
|
||||
}
|
||||
|
||||
pub fn firebase_installation_token(
|
||||
client: &RegistrationClient<'_>,
|
||||
identity: &PushIdentity,
|
||||
fid: &str,
|
||||
) -> Result<String, PushError> {
|
||||
if identity.project_id.is_empty() || identity.api_key.is_empty() {
|
||||
return Err(PushError::new(
|
||||
"push_config_incomplete",
|
||||
"The push identity is incomplete",
|
||||
));
|
||||
}
|
||||
let body = serde_json::json!({
|
||||
"fid": fid,
|
||||
"authVersion": "FIS_v2",
|
||||
"appId": identity.firebase_app_id,
|
||||
"sdkVersion": "w:0.1.0",
|
||||
})
|
||||
.to_string();
|
||||
let response = client.http.execute(&HttpRequest {
|
||||
url: format!(
|
||||
"{}/projects/{}/installations",
|
||||
client.endpoints.fis.trim_end_matches('/'),
|
||||
identity.project_id
|
||||
),
|
||||
method: HttpMethod::Post,
|
||||
headers: vec![
|
||||
("Content-Type".into(), "application/json".into()),
|
||||
("Accept".into(), "application/json".into()),
|
||||
("x-goog-api-key".into(), identity.api_key.clone()),
|
||||
(
|
||||
"x-firebase-client".into(),
|
||||
URL_SAFE_NO_PAD.encode(b"{\"heartbeats\": [], \"version\": 2}"),
|
||||
),
|
||||
],
|
||||
body: body.into_bytes(),
|
||||
timeout: client.timeout,
|
||||
})?;
|
||||
if response.status != 200 {
|
||||
return Err(PushError::new(
|
||||
"push_fis_refused",
|
||||
"The installation service refused the request",
|
||||
));
|
||||
}
|
||||
let value: serde_json::Value = serde_json::from_slice(&response.body).map_err(|_| {
|
||||
PushError::new(
|
||||
"push_fis_incomplete",
|
||||
"The installation response could not be read",
|
||||
)
|
||||
})?;
|
||||
value["authToken"]["token"]
|
||||
.as_str()
|
||||
.map(str::to_owned)
|
||||
.ok_or_else(|| {
|
||||
PushError::new(
|
||||
"push_fis_incomplete",
|
||||
"The installation response did not carry a token",
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn fcm_register(
|
||||
client: &RegistrationClient<'_>,
|
||||
identity: &PushIdentity,
|
||||
installation_token: &str,
|
||||
gcm_token: &str,
|
||||
public_key: &[u8],
|
||||
auth_secret: &[u8],
|
||||
) -> Result<(String, String), PushError> {
|
||||
let endpoint = format!(
|
||||
"{}/{}",
|
||||
client.endpoints.fcm_send.trim_end_matches('/'),
|
||||
gcm_token
|
||||
);
|
||||
let mut web = serde_json::json!({
|
||||
"endpoint": endpoint,
|
||||
"auth": URL_SAFE_NO_PAD.encode(auth_secret),
|
||||
"p256dh": URL_SAFE_NO_PAD.encode(public_key),
|
||||
});
|
||||
if let Some(vapid) = identity.vapid_key.as_deref() {
|
||||
if !vapid.is_empty() {
|
||||
web["application_pub_key"] = serde_json::Value::String(vapid.to_owned());
|
||||
}
|
||||
}
|
||||
let body = serde_json::json!({ "web": web }).to_string();
|
||||
let response = client.http.execute(&HttpRequest {
|
||||
url: format!(
|
||||
"{}/projects/{}/registrations",
|
||||
client.endpoints.fcm_registration.trim_end_matches('/'),
|
||||
identity.project_id
|
||||
),
|
||||
method: HttpMethod::Post,
|
||||
headers: vec![
|
||||
("Content-Type".into(), "application/json".into()),
|
||||
("Accept".into(), "application/json".into()),
|
||||
("x-goog-api-key".into(), identity.api_key.clone()),
|
||||
(
|
||||
"x-goog-firebase-installations-auth".into(),
|
||||
format!("FIS {installation_token}"),
|
||||
),
|
||||
],
|
||||
body: body.into_bytes(),
|
||||
timeout: client.timeout,
|
||||
})?;
|
||||
if response.status != 200 {
|
||||
return Err(PushError::new(
|
||||
"push_fcm_refused",
|
||||
"The messaging service refused the client registration",
|
||||
));
|
||||
}
|
||||
let value: serde_json::Value = serde_json::from_slice(&response.body).map_err(|_| {
|
||||
PushError::new(
|
||||
"push_fcm_incomplete",
|
||||
"The registration response could not be read",
|
||||
)
|
||||
})?;
|
||||
let token = value["token"].as_str().ok_or_else(|| {
|
||||
PushError::new(
|
||||
"push_fcm_incomplete",
|
||||
"The registration response did not carry a token",
|
||||
)
|
||||
})?;
|
||||
if token.is_empty() {
|
||||
return Err(PushError::new(
|
||||
"push_fcm_incomplete",
|
||||
"The registration response carried an empty token",
|
||||
));
|
||||
}
|
||||
Ok((token.to_owned(), endpoint))
|
||||
}
|
||||
|
||||
pub fn pns_register(
|
||||
client: &RegistrationClient<'_>,
|
||||
registration_token: &str,
|
||||
device_id: &str,
|
||||
previous: &[String],
|
||||
gfnjwt: &str,
|
||||
) -> Result<(), PushError> {
|
||||
if client.endpoints.pns.is_empty() || client.endpoints.pns_client_id.is_empty() {
|
||||
return Err(PushError::new(
|
||||
"push_config_incomplete",
|
||||
"The notification service is not configured",
|
||||
));
|
||||
}
|
||||
let body = serde_json::json!({
|
||||
"registrationToken": registration_token,
|
||||
"deviceId": device_id,
|
||||
"previousRegistrationTokens": previous,
|
||||
})
|
||||
.to_string();
|
||||
let response = client.http.execute(&HttpRequest {
|
||||
url: client.endpoints.pns_registrations_url(),
|
||||
method: HttpMethod::Post,
|
||||
headers: vec![
|
||||
("Content-Type".into(), "application/json".into()),
|
||||
("Accept".into(), "application/json".into()),
|
||||
(
|
||||
"NV-Client-ID".into(),
|
||||
client.endpoints.pns_client_id.clone(),
|
||||
),
|
||||
("Authorization".into(), format!("GFNJWT {gfnjwt}")),
|
||||
],
|
||||
body: body.into_bytes(),
|
||||
timeout: client.timeout,
|
||||
})?;
|
||||
match response.status {
|
||||
200 | 201 | 202 | 204 => Ok(()),
|
||||
_ => Err(PushError::new(
|
||||
"push_pns_refused",
|
||||
"The notification service refused the registration",
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pns_unregister(
|
||||
client: &RegistrationClient<'_>,
|
||||
previous_tokens: &[String],
|
||||
gfnjwt: &str,
|
||||
) -> Result<(), PushError> {
|
||||
if client.endpoints.pns.is_empty() || client.endpoints.pns_client_id.is_empty() {
|
||||
return Err(PushError::new(
|
||||
"push_config_incomplete",
|
||||
"The notification service is not configured",
|
||||
));
|
||||
}
|
||||
let body = serde_json::json!({ "previousRegistrationTokens": previous_tokens }).to_string();
|
||||
let response = client.http.execute(&HttpRequest {
|
||||
url: client.endpoints.pns_unregister_url(),
|
||||
method: HttpMethod::Post,
|
||||
headers: vec![
|
||||
("Content-Type".into(), "application/json".into()),
|
||||
("Accept".into(), "application/json".into()),
|
||||
(
|
||||
"NV-Client-ID".into(),
|
||||
client.endpoints.pns_client_id.clone(),
|
||||
),
|
||||
("Authorization".into(), format!("GFNJWT {gfnjwt}")),
|
||||
],
|
||||
body: body.into_bytes(),
|
||||
timeout: client.timeout,
|
||||
})?;
|
||||
match response.status {
|
||||
200 | 202 | 204 => Ok(()),
|
||||
_ => Err(PushError::new(
|
||||
"push_pns_refused",
|
||||
"The notification service refused the unregistration",
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
impl PushEndpoints {
|
||||
pub fn pns_registrations_url(&self) -> String {
|
||||
format!("{}/registrations", self.pns.trim_end_matches('/'))
|
||||
}
|
||||
|
||||
pub fn pns_unregister_url(&self) -> String {
|
||||
format!("{}/unregister", self.pns.trim_end_matches('/'))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register(
|
||||
client: &RegistrationClient<'_>,
|
||||
identity: &PushIdentity,
|
||||
existing: Option<&Registration>,
|
||||
gfnjwt: &str,
|
||||
device_id: &str,
|
||||
guard: &mut dyn FnMut() -> bool,
|
||||
) -> Result<Registration, PushError> {
|
||||
if identity.project_id.is_empty() || identity.api_key.is_empty() || identity.app_id.is_empty() {
|
||||
return Err(PushError::new(
|
||||
"push_config_incomplete",
|
||||
"The push identity is incomplete",
|
||||
));
|
||||
}
|
||||
let (android_id, security_token, received) = match existing {
|
||||
Some(registration) => (
|
||||
Some(registration.android_id),
|
||||
Some(registration.security_token),
|
||||
registration.received_persistent_ids.clone(),
|
||||
),
|
||||
None => (None, None, Vec::new()),
|
||||
};
|
||||
stage(guard)?;
|
||||
let checked = checkin(client, android_id, security_token)?;
|
||||
stage(guard)?;
|
||||
let gcm_token = gcm_register(client, checked.android_id, checked.security_token, identity)?;
|
||||
let keys = EceKeyPair::generate()?;
|
||||
let fid = random_fid();
|
||||
stage(guard)?;
|
||||
let installation_token = firebase_installation_token(client, identity, &fid)?;
|
||||
stage(guard)?;
|
||||
let (fcm_token, endpoint) = fcm_register(
|
||||
client,
|
||||
identity,
|
||||
&installation_token,
|
||||
&gcm_token,
|
||||
keys.public_key(),
|
||||
keys.auth_secret(),
|
||||
)?;
|
||||
let previous: Vec<String> = existing
|
||||
.map(|registration| registration.fcm_token.as_str())
|
||||
.filter(|token| !token.is_empty() && *token != fcm_token)
|
||||
.map(str::to_owned)
|
||||
.into_iter()
|
||||
.collect();
|
||||
stage(guard)?;
|
||||
pns_register(client, &fcm_token, device_id, &previous, gfnjwt)?;
|
||||
Ok(Registration {
|
||||
android_id: checked.android_id,
|
||||
security_token: checked.security_token,
|
||||
gcm_token,
|
||||
fcm_token,
|
||||
endpoint,
|
||||
private_key: keys.private_key(),
|
||||
public_key: keys.public_key().to_vec(),
|
||||
auth_secret: keys.auth_secret().to_vec(),
|
||||
received_persistent_ids: received,
|
||||
created_at_seconds: std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|duration| duration.as_secs())
|
||||
.unwrap_or_default(),
|
||||
fingerprint: String::new(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn checkin_and_register(
|
||||
client: &RegistrationClient<'_>,
|
||||
identity: &PushIdentity,
|
||||
existing: Option<&Registration>,
|
||||
gfnjwt: &str,
|
||||
device_id: &str,
|
||||
guard: &mut dyn FnMut() -> bool,
|
||||
) -> Result<Registration, PushError> {
|
||||
register(client, identity, existing, gfnjwt, device_id, guard)
|
||||
}
|
||||
|
||||
fn stage(guard: &mut dyn FnMut() -> bool) -> Result<(), PushError> {
|
||||
if guard() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(PushError::new(
|
||||
"push_scope_stale",
|
||||
"The account changed while the push registration was running",
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn random_fid() -> String {
|
||||
use rand::RngCore;
|
||||
let mut bytes = [0_u8; 17];
|
||||
rand::rng().fill_bytes(&mut bytes);
|
||||
bytes[0] = 0b0111_0000 + (bytes[0] % 0b0001_0000);
|
||||
let encoded = URL_SAFE_NO_PAD.encode(bytes);
|
||||
encoded[..22].to_owned()
|
||||
}
|
||||
|
||||
fn urlencode(value: &str) -> String {
|
||||
let mut encoded = String::with_capacity(value.len());
|
||||
for byte in value.bytes() {
|
||||
match byte {
|
||||
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
|
||||
encoded.push(byte as char);
|
||||
}
|
||||
other => encoded.push_str(&format!("%{other:02X}")),
|
||||
}
|
||||
}
|
||||
encoded
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
use crate::push::PushError;
|
||||
use crate::push::registration::Registration;
|
||||
|
||||
const PUSH_SERVICE_NAME: &str = "app.opennow.push";
|
||||
|
||||
pub trait PushStateStore: Send + Sync {
|
||||
fn load(&self, account: &str) -> Result<Option<Registration>, PushError>;
|
||||
fn save(&self, account: &str, registration: &Registration) -> Result<(), PushError>;
|
||||
fn clear(&self, account: &str) -> Result<(), PushError>;
|
||||
}
|
||||
|
||||
pub struct RegistrationStore {
|
||||
service: String,
|
||||
}
|
||||
|
||||
impl RegistrationStore {
|
||||
pub fn new(service: &str) -> Self {
|
||||
Self {
|
||||
service: service.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn default_service() -> Self {
|
||||
Self::new(PUSH_SERVICE_NAME)
|
||||
}
|
||||
|
||||
fn entry(&self, account: &str) -> Result<keyring::Entry, PushError> {
|
||||
keyring::Entry::new(&self.service, &format!("registration:{account}")).map_err(|_| {
|
||||
PushError::new(
|
||||
"push_store_unavailable",
|
||||
"The OS credential store is unavailable for the push registration",
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl PushStateStore for RegistrationStore {
|
||||
fn load(&self, account: &str) -> Result<Option<Registration>, PushError> {
|
||||
match self.entry(account)?.get_password() {
|
||||
Ok(encoded) => serde_json::from_str(&encoded).map(Some).map_err(|_| {
|
||||
PushError::new(
|
||||
"push_store_corrupt",
|
||||
"The stored push registration could not be read",
|
||||
)
|
||||
}),
|
||||
Err(keyring::Error::NoEntry) => Ok(None),
|
||||
Err(_) => Err(PushError::new(
|
||||
"push_store_unavailable",
|
||||
"The OS credential store is unavailable for the push registration",
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn save(&self, account: &str, registration: &Registration) -> Result<(), PushError> {
|
||||
let encoded = serde_json::to_string(registration).map_err(|_| {
|
||||
PushError::new(
|
||||
"push_store_failed",
|
||||
"The push registration could not be encoded",
|
||||
)
|
||||
})?;
|
||||
self.entry(account)?.set_password(&encoded).map_err(|_| {
|
||||
PushError::new(
|
||||
"push_store_failed",
|
||||
"The push registration could not be stored",
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn clear(&self, account: &str) -> Result<(), PushError> {
|
||||
match self.entry(account)?.delete_credential() {
|
||||
Ok(()) | Err(keyring::Error::NoEntry) => Ok(()),
|
||||
Err(_) => Err(PushError::new(
|
||||
"push_store_failed",
|
||||
"The push registration could not be removed",
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
+2881
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,320 @@
|
||||
use crate::push::PushError;
|
||||
use std::io::{Read, Write};
|
||||
use std::net::{SocketAddr, TcpStream, ToSocketAddrs};
|
||||
use std::sync::mpsc::{Receiver, TryRecvError};
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
pub trait PushTransport: Send {
|
||||
fn send(&mut self, frame: &[u8], timeout: Duration) -> Result<(), PushError>;
|
||||
fn recv(&mut self, timeout: Duration) -> Result<Vec<u8>, PushError>;
|
||||
fn close(&mut self);
|
||||
}
|
||||
|
||||
pub trait PushTransportFactory: Send + Sync {
|
||||
fn connect(
|
||||
&self,
|
||||
host: &str,
|
||||
port: u16,
|
||||
timeout: Duration,
|
||||
) -> Result<Box<dyn PushTransport>, PushError>;
|
||||
}
|
||||
|
||||
pub struct TlsPushTransport {
|
||||
connection: rustls::ClientConnection,
|
||||
socket: TcpStream,
|
||||
}
|
||||
|
||||
enum Resolution {
|
||||
Addrs(Vec<SocketAddr>),
|
||||
Failed,
|
||||
}
|
||||
|
||||
pub struct TlsPushTransportFactory {
|
||||
pending: Mutex<Option<(String, u16, Receiver<Resolution>)>>,
|
||||
}
|
||||
|
||||
impl Default for TlsPushTransportFactory {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl TlsPushTransportFactory {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
pending: Mutex::new(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve(
|
||||
&self,
|
||||
host: &str,
|
||||
port: u16,
|
||||
timeout: Duration,
|
||||
) -> Result<Vec<SocketAddr>, PushError> {
|
||||
let mut pending = self.pending.lock().expect("push resolver poisoned");
|
||||
if let Some((pending_host, pending_port, receiver)) = pending.take() {
|
||||
match receiver.try_recv() {
|
||||
Ok(Resolution::Addrs(addrs)) => {
|
||||
if pending_host == host && pending_port == port {
|
||||
return Ok(addrs);
|
||||
}
|
||||
}
|
||||
Ok(Resolution::Failed) | Err(TryRecvError::Disconnected) => {
|
||||
if pending_host == host && pending_port == port {
|
||||
return Err(PushError::new(
|
||||
"push_transport_failed",
|
||||
"The push service could not be resolved",
|
||||
));
|
||||
}
|
||||
}
|
||||
Err(TryRecvError::Empty) => {
|
||||
*pending = Some((pending_host, pending_port, receiver));
|
||||
return Err(PushError::new(
|
||||
"push_transport_resolving",
|
||||
"A previous push service resolution is still in progress",
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
let (sender, receiver) = std::sync::mpsc::channel();
|
||||
let owned_host = host.to_owned();
|
||||
std::thread::Builder::new()
|
||||
.name("opennow-push-resolver".to_owned())
|
||||
.spawn(move || {
|
||||
let resolved = (owned_host.as_str(), port)
|
||||
.to_socket_addrs()
|
||||
.map(|addrs| Resolution::Addrs(addrs.collect()))
|
||||
.unwrap_or(Resolution::Failed);
|
||||
let _ = sender.send(resolved);
|
||||
})
|
||||
.map_err(|_| {
|
||||
PushError::new(
|
||||
"push_transport_failed",
|
||||
"The push service resolver could not start",
|
||||
)
|
||||
})?;
|
||||
match receiver.recv_timeout(timeout) {
|
||||
Ok(Resolution::Addrs(addrs)) => {
|
||||
if addrs.is_empty() {
|
||||
return Err(PushError::new(
|
||||
"push_transport_failed",
|
||||
"The push service could not be resolved",
|
||||
));
|
||||
}
|
||||
Ok(addrs)
|
||||
}
|
||||
Ok(Resolution::Failed) => Err(PushError::new(
|
||||
"push_transport_failed",
|
||||
"The push service could not be resolved",
|
||||
)),
|
||||
Err(_) => {
|
||||
*pending = Some((host.to_owned(), port, receiver));
|
||||
Err(PushError::new(
|
||||
"push_transport_failed",
|
||||
"The push service resolution timed out",
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn tls_config() -> Arc<rustls::ClientConfig> {
|
||||
static CONFIG: OnceLock<Arc<rustls::ClientConfig>> = OnceLock::new();
|
||||
Arc::clone(CONFIG.get_or_init(|| {
|
||||
let mut roots = rustls::RootCertStore::empty();
|
||||
roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
|
||||
Arc::new(
|
||||
rustls::ClientConfig::builder()
|
||||
.with_root_certificates(roots)
|
||||
.with_no_client_auth(),
|
||||
)
|
||||
}))
|
||||
}
|
||||
|
||||
fn is_timeout(error: &std::io::Error) -> bool {
|
||||
matches!(
|
||||
error.kind(),
|
||||
std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut
|
||||
)
|
||||
}
|
||||
|
||||
fn apply_timeout(deadline: Instant) -> Option<Duration> {
|
||||
let remaining = deadline.saturating_duration_since(Instant::now());
|
||||
(!remaining.is_zero()).then_some(remaining)
|
||||
}
|
||||
|
||||
impl TlsPushTransport {
|
||||
fn open(host: &str, address: SocketAddr, deadline: Instant) -> Result<Self, PushError> {
|
||||
let server_name =
|
||||
rustls::pki_types::ServerName::try_from(host.to_owned()).map_err(|_| {
|
||||
PushError::new("push_transport_invalid", "The push service host is invalid")
|
||||
})?;
|
||||
let connection =
|
||||
rustls::ClientConnection::new(tls_config(), server_name).map_err(|_| {
|
||||
PushError::new(
|
||||
"push_transport_failed",
|
||||
"The push connection could not start",
|
||||
)
|
||||
})?;
|
||||
let Some(remaining) = apply_timeout(deadline) else {
|
||||
return Err(PushError::new(
|
||||
"push_transport_failed",
|
||||
"The push service connection timed out",
|
||||
));
|
||||
};
|
||||
let socket = TcpStream::connect_timeout(&address, remaining).map_err(|_| {
|
||||
PushError::new("push_transport_failed", "The push service is unreachable")
|
||||
})?;
|
||||
let _ = socket.set_nodelay(true);
|
||||
Ok(Self { connection, socket })
|
||||
}
|
||||
|
||||
fn finish_handshake(&mut self, deadline: Instant) -> Result<(), PushError> {
|
||||
while self.connection.is_handshaking() {
|
||||
let Some(remaining) = apply_timeout(deadline) else {
|
||||
return Err(PushError::new(
|
||||
"push_transport_failed",
|
||||
"The push service handshake timed out",
|
||||
));
|
||||
};
|
||||
let _ = self.socket.set_read_timeout(Some(remaining));
|
||||
let _ = self.socket.set_write_timeout(Some(remaining));
|
||||
match self.connection.complete_io(&mut self.socket) {
|
||||
Ok(_) => {}
|
||||
Err(error) if is_timeout(&error) => continue,
|
||||
Err(_) => {
|
||||
return Err(PushError::new(
|
||||
"push_transport_failed",
|
||||
"The push service handshake failed",
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn drain(&mut self, buffer: &mut [u8]) -> Result<usize, PushError> {
|
||||
self.connection
|
||||
.reader()
|
||||
.read(buffer)
|
||||
.map_err(|_| PushError::new("push_transport_failed", "The push connection failed"))
|
||||
}
|
||||
}
|
||||
|
||||
impl PushTransport for TlsPushTransport {
|
||||
fn send(&mut self, frame: &[u8], timeout: Duration) -> Result<(), PushError> {
|
||||
let deadline = Instant::now() + timeout;
|
||||
self.finish_handshake(deadline)?;
|
||||
self.connection
|
||||
.writer()
|
||||
.write_all(frame)
|
||||
.map_err(|_| PushError::new("push_transport_failed", "The push connection failed"))?;
|
||||
while self.connection.wants_write() {
|
||||
let Some(remaining) = apply_timeout(deadline) else {
|
||||
return Err(PushError::new(
|
||||
"push_transport_failed",
|
||||
"The push message could not be sent in time",
|
||||
));
|
||||
};
|
||||
let _ = self.socket.set_write_timeout(Some(remaining));
|
||||
match self.connection.write_tls(&mut self.socket) {
|
||||
Ok(0) => {
|
||||
return Err(PushError::new(
|
||||
"push_transport_closed",
|
||||
"The push connection ended",
|
||||
));
|
||||
}
|
||||
Ok(_) => {}
|
||||
Err(error) if is_timeout(&error) => continue,
|
||||
Err(_) => {
|
||||
return Err(PushError::new(
|
||||
"push_transport_failed",
|
||||
"The push connection failed",
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn recv(&mut self, timeout: Duration) -> Result<Vec<u8>, PushError> {
|
||||
let deadline = Instant::now() + timeout;
|
||||
let mut buffer = [0_u8; 16 * 1024];
|
||||
loop {
|
||||
match self.drain(&mut buffer) {
|
||||
Ok(0) => {}
|
||||
Ok(count) => return Ok(buffer[..count].to_vec()),
|
||||
Err(error) => return Err(error),
|
||||
}
|
||||
let Some(remaining) = apply_timeout(deadline) else {
|
||||
return Ok(Vec::new());
|
||||
};
|
||||
let _ = self.socket.set_read_timeout(Some(remaining));
|
||||
match self.connection.read_tls(&mut self.socket) {
|
||||
Ok(0) => {
|
||||
return Err(PushError::new(
|
||||
"push_transport_closed",
|
||||
"The push connection ended",
|
||||
));
|
||||
}
|
||||
Ok(_) => {
|
||||
self.connection.process_new_packets().map_err(|_| {
|
||||
PushError::new("push_transport_failed", "The push connection failed")
|
||||
})?;
|
||||
}
|
||||
Err(error) if is_timeout(&error) => {}
|
||||
Err(_) => {
|
||||
return Err(PushError::new(
|
||||
"push_transport_failed",
|
||||
"The push connection failed",
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn close(&mut self) {
|
||||
self.connection.send_close_notify();
|
||||
let deadline = Instant::now() + Duration::from_millis(500);
|
||||
while self.connection.wants_write() {
|
||||
let Some(remaining) = apply_timeout(deadline) else {
|
||||
break;
|
||||
};
|
||||
let _ = self.socket.set_write_timeout(Some(remaining));
|
||||
if self.connection.write_tls(&mut self.socket).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let _ = self.socket.shutdown(std::net::Shutdown::Both);
|
||||
}
|
||||
}
|
||||
|
||||
impl PushTransportFactory for TlsPushTransportFactory {
|
||||
fn connect(
|
||||
&self,
|
||||
host: &str,
|
||||
port: u16,
|
||||
timeout: Duration,
|
||||
) -> Result<Box<dyn PushTransport>, PushError> {
|
||||
let deadline = Instant::now() + timeout;
|
||||
let addresses = self.resolve(host, port, timeout)?;
|
||||
let mut last_error = None;
|
||||
for address in addresses.into_iter().take(8) {
|
||||
match TlsPushTransport::open(host, address, deadline) {
|
||||
Ok(mut transport) => {
|
||||
transport.finish_handshake(deadline)?;
|
||||
return Ok(Box::new(transport));
|
||||
}
|
||||
Err(error) => last_error = Some(error),
|
||||
}
|
||||
if apply_timeout(deadline).is_none() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(last_error.unwrap_or_else(|| {
|
||||
PushError::new("push_transport_failed", "The push service is unreachable")
|
||||
}))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
use crate::push::owner::PushOwnerConfig;
|
||||
use crate::push::registration::{PushEndpoints, PushIdentity};
|
||||
|
||||
const PROJECT_ID: &str = "nv-pushnotificationss-20220823";
|
||||
const API_KEY: &str = "AIzaSyCyGIQv16BXoVfMOSXesGclbZnC6krMBmU";
|
||||
const SENDER_ID: &str = "950535643107";
|
||||
const APP_ID: &str = "nv-pushnotificationss-20220823";
|
||||
const FIREBASE_APP_ID: &str = "1:950535643107:web:a2e08529da36c9456e0ad6";
|
||||
const VAPID_KEY: &str =
|
||||
"BKXL_5beeXnhyvgZV-1BzEB6DH-bYL9gsql510ilbB8gYBxJavlpdHPF6VtjyrmowsskhD3qn72uvIMhiffB6UY";
|
||||
const PNS_SERVER: &str = "https://pns.geforcenow.com";
|
||||
const PNS_VERSION: &str = "v1";
|
||||
const PNS_CLIENT_ID: &str = "ec7e38d4-03af-4b58-b131-cfb0495903ab";
|
||||
|
||||
pub fn config_for_provider(provider_idp_id: &str, device_id: String) -> PushOwnerConfig {
|
||||
PushOwnerConfig::bounded(
|
||||
PushEndpoints {
|
||||
pns: format!(
|
||||
"{}/{}",
|
||||
PNS_SERVER.trim_end_matches('/'),
|
||||
PNS_VERSION.trim_matches('/')
|
||||
),
|
||||
pns_client_id: PNS_CLIENT_ID.to_owned(),
|
||||
..PushEndpoints::default()
|
||||
},
|
||||
PushIdentity {
|
||||
project_id: PROJECT_ID.to_owned(),
|
||||
api_key: API_KEY.to_owned(),
|
||||
sender_id: SENDER_ID.to_owned(),
|
||||
app_id: APP_ID.to_owned(),
|
||||
firebase_app_id: FIREBASE_APP_ID.to_owned(),
|
||||
vapid_key: Some(VAPID_KEY.to_owned()),
|
||||
},
|
||||
provider_idp_id.to_owned(),
|
||||
device_id,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_bundled_default_is_complete_and_public_only() {
|
||||
let config = config_for_provider("nvidia", "device".into());
|
||||
assert_eq!(config.provider_id, "nvidia");
|
||||
assert_eq!(config.device_id, "device");
|
||||
assert_eq!(config.identity.project_id, PROJECT_ID);
|
||||
assert!(!config.identity.api_key.is_empty());
|
||||
assert!(!config.identity.sender_id.is_empty());
|
||||
assert!(!config.identity.app_id.is_empty());
|
||||
assert!(!config.identity.firebase_app_id.is_empty());
|
||||
assert!(
|
||||
config
|
||||
.identity
|
||||
.vapid_key
|
||||
.as_deref()
|
||||
.is_some_and(|key| !key.is_empty())
|
||||
);
|
||||
assert!(!config.endpoints.pns.is_empty());
|
||||
assert!(!config.endpoints.pns.ends_with('/'));
|
||||
assert!(!config.endpoints.pns_client_id.is_empty());
|
||||
assert_eq!(
|
||||
config.endpoints.pns,
|
||||
format!(
|
||||
"{}/{}",
|
||||
PNS_SERVER.trim_end_matches('/'),
|
||||
PNS_VERSION.trim_matches('/')
|
||||
)
|
||||
);
|
||||
assert_eq!(config.endpoints.mcs_host, PushEndpoints::default().mcs_host);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_bundled_default_is_bound_to_the_authenticated_provider() {
|
||||
assert_eq!(
|
||||
config_for_provider("nvidia", "device".into()).provider_id,
|
||||
"nvidia"
|
||||
);
|
||||
assert_eq!(
|
||||
config_for_provider("alliance", "device".into()).provider_id,
|
||||
"alliance"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_bundled_default_uses_a_usable_https_pns_base() {
|
||||
let config = config_for_provider("nvidia", "device".into());
|
||||
assert!(
|
||||
crate::push::config::pns_base_is_usable(&config.endpoints.pns),
|
||||
"the bundled default must keep the evidenced https pns base"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_bundled_default_carries_no_device_or_user_secret() {
|
||||
let first = config_for_provider("nvidia", "device-a".into());
|
||||
let second = config_for_provider("nvidia", "device-b".into());
|
||||
assert_eq!(first.identity, second.identity);
|
||||
assert_eq!(first.endpoints, second.endpoints);
|
||||
assert_ne!(first, second);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,943 @@
|
||||
use crate::gfn::GfnService;
|
||||
use opennow_core::push::{
|
||||
GfnTokenSource, PUSH_EVENT_NAME, PushConfig, PushError, PushOwner, PushOwnerConfig,
|
||||
PushOwnerDeps, PushScope, PushSink, RegistrationStore, ReqwestPushHttp, ScopeSource,
|
||||
TlsPushTransportFactory, bundled_push_config, push_config_for_provider, push_event_payload,
|
||||
};
|
||||
use serde_json::Value;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::time::Duration;
|
||||
|
||||
pub struct DesiredPush {
|
||||
pub config: PushOwnerConfig,
|
||||
}
|
||||
|
||||
pub fn desired_push(data_dir: &Path, scope: &PushScope, device_id: &str) -> Option<DesiredPush> {
|
||||
let config = match push_config_for_provider(data_dir, &scope.provider_id, device_id.to_owned())
|
||||
{
|
||||
PushConfig::Absent => bundled_push_config(&scope.provider_id, device_id.to_owned()),
|
||||
PushConfig::Configured(config) => *config,
|
||||
PushConfig::Suppressed => return None,
|
||||
};
|
||||
if config.endpoints.pns.is_empty() || config.endpoints.pns_client_id.is_empty() {
|
||||
return None;
|
||||
}
|
||||
Some(DesiredPush { config })
|
||||
}
|
||||
|
||||
pub type DepsFactory = dyn Fn(Arc<GfnService>, ScopeSource, Sender<Value>) -> Result<PushOwnerDeps, PushError>
|
||||
+ Send
|
||||
+ Sync;
|
||||
|
||||
pub struct PushRegistry {
|
||||
gfn: Arc<GfnService>,
|
||||
output: Sender<Value>,
|
||||
data_dir: PathBuf,
|
||||
scope_source: ScopeSource,
|
||||
owner: Option<PushOwner>,
|
||||
deps_factory: Arc<DepsFactory>,
|
||||
}
|
||||
|
||||
impl PushRegistry {
|
||||
pub fn new(gfn: Arc<GfnService>, output: Sender<Value>, data_dir: PathBuf) -> Self {
|
||||
let scope_source: ScopeSource = {
|
||||
let gfn = Arc::clone(&gfn);
|
||||
Arc::new(move || gfn.push_scope())
|
||||
};
|
||||
Self::with_deps_factory(
|
||||
gfn,
|
||||
output,
|
||||
data_dir,
|
||||
scope_source,
|
||||
Arc::new(production_deps),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn with_deps_factory(
|
||||
gfn: Arc<GfnService>,
|
||||
output: Sender<Value>,
|
||||
data_dir: PathBuf,
|
||||
scope_source: ScopeSource,
|
||||
deps_factory: Arc<DepsFactory>,
|
||||
) -> Self {
|
||||
Self {
|
||||
gfn,
|
||||
output,
|
||||
data_dir,
|
||||
scope_source,
|
||||
owner: None,
|
||||
deps_factory,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reconcile(&mut self) -> Result<(), PushError> {
|
||||
let scope = (self.scope_source)();
|
||||
let desired = scope
|
||||
.as_ref()
|
||||
.and_then(|scope| desired_push(&self.data_dir, scope, self.gfn.device_id()));
|
||||
if let Some(owner) = self.owner.as_mut() {
|
||||
let Some(desired) = desired else {
|
||||
owner.stop();
|
||||
return Ok(());
|
||||
};
|
||||
if owner.config().0 != desired.config {
|
||||
owner.update_config(desired.config);
|
||||
}
|
||||
return owner.start();
|
||||
}
|
||||
let Some(desired) = desired else {
|
||||
return Ok(());
|
||||
};
|
||||
let deps = (self.deps_factory)(
|
||||
Arc::clone(&self.gfn),
|
||||
Arc::clone(&self.scope_source),
|
||||
self.output.clone(),
|
||||
)?;
|
||||
let mut owner = PushOwner::new(desired.config, deps);
|
||||
owner.start()?;
|
||||
self.owner = Some(owner);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn stop(&mut self) {
|
||||
if let Some(mut owner) = self.owner.take() {
|
||||
owner.shutdown(Duration::from_secs(5));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn production_deps(
|
||||
gfn: Arc<GfnService>,
|
||||
scope_source: ScopeSource,
|
||||
output: Sender<Value>,
|
||||
) -> Result<PushOwnerDeps, PushError> {
|
||||
let token_source: GfnTokenSource = {
|
||||
let gfn = Arc::clone(&gfn);
|
||||
Arc::new(move |scope: &PushScope| gfn.push_token_for_scope(scope))
|
||||
};
|
||||
let sink: PushSink = Arc::new(move |event, generation| {
|
||||
let _ = output.send(serde_json::json!({
|
||||
"type": "event",
|
||||
"name": PUSH_EVENT_NAME,
|
||||
"payload": push_event_payload(&event, generation),
|
||||
}));
|
||||
});
|
||||
Ok(PushOwnerDeps {
|
||||
http: Arc::new(ReqwestPushHttp::new()?),
|
||||
transport: Arc::new(TlsPushTransportFactory::new()),
|
||||
store: Arc::new(RegistrationStore::default_service()),
|
||||
sink,
|
||||
scope_source,
|
||||
token_source,
|
||||
})
|
||||
}
|
||||
|
||||
impl Drop for PushRegistry {
|
||||
fn drop(&mut self) {
|
||||
self.stop();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use opennow_core::push::{
|
||||
HttpRequest, HttpResponse, PushEvent, PushHttp, PushScope, PushStateStore, PushTransport,
|
||||
PushTransportFactory, Registration,
|
||||
};
|
||||
use serde_json::json;
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
use std::sync::{Condvar, Mutex};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
const HOLD_DEADLINE: Duration = Duration::from_secs(30);
|
||||
|
||||
fn scope() -> PushScope {
|
||||
PushScope {
|
||||
user_id: "user-1".into(),
|
||||
provider_id: "nvidia".into(),
|
||||
generation: 3,
|
||||
}
|
||||
}
|
||||
|
||||
fn config_dir(name: &str) -> PathBuf {
|
||||
let dir = std::env::temp_dir().join(format!("opennow-push-registry-{name}"));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
std::fs::write(
|
||||
dir.join(opennow_core::push::PUSH_CONFIG_FILE),
|
||||
raw_config().to_string(),
|
||||
)
|
||||
.unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
fn empty_dir(name: &str) -> PathBuf {
|
||||
let dir = std::env::temp_dir().join(format!("opennow-push-registry-{name}"));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
fn raw_config() -> Value {
|
||||
json!({
|
||||
"providerIdpId": "nvidia",
|
||||
"projectId": "project",
|
||||
"apiKey": "api-key",
|
||||
"senderId": "123456789012",
|
||||
"appId": "app",
|
||||
"firebaseAppId": "1:1:web:abc",
|
||||
"pnsServer": "https://pns.example",
|
||||
"pnsClientId": "client",
|
||||
})
|
||||
}
|
||||
|
||||
fn registration_body() -> Vec<u8> {
|
||||
let mut body = vec![0x39];
|
||||
body.extend_from_slice(&42_u64.to_le_bytes());
|
||||
body.push(0x41);
|
||||
body.extend_from_slice(&7_u64.to_le_bytes());
|
||||
body
|
||||
}
|
||||
|
||||
fn registration_responses() -> Vec<HttpResponse> {
|
||||
vec![
|
||||
HttpResponse {
|
||||
status: 200,
|
||||
body: registration_body(),
|
||||
},
|
||||
HttpResponse {
|
||||
status: 200,
|
||||
body: b"token=gcm-token".to_vec(),
|
||||
},
|
||||
HttpResponse {
|
||||
status: 200,
|
||||
body: br#"{"authToken":{"token":"fis-token"}}"#.to_vec(),
|
||||
},
|
||||
HttpResponse {
|
||||
status: 200,
|
||||
body: br#"{"token":"fcm-token"}"#.to_vec(),
|
||||
},
|
||||
HttpResponse {
|
||||
status: 204,
|
||||
body: Vec::new(),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
struct FakeHttp {
|
||||
responses: Mutex<Vec<HttpResponse>>,
|
||||
calls: Arc<AtomicUsize>,
|
||||
release: Option<Arc<Blocking>>,
|
||||
}
|
||||
|
||||
struct Blocking {
|
||||
released: Mutex<bool>,
|
||||
wake: Condvar,
|
||||
abandoned: AtomicBool,
|
||||
}
|
||||
|
||||
impl Blocking {
|
||||
fn new() -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
released: Mutex::new(false),
|
||||
wake: Condvar::new(),
|
||||
abandoned: AtomicBool::new(false),
|
||||
})
|
||||
}
|
||||
|
||||
fn release(&self) {
|
||||
*self.released.lock().unwrap() = true;
|
||||
self.wake.notify_all();
|
||||
}
|
||||
|
||||
fn abandoned(&self) -> bool {
|
||||
self.abandoned.load(Ordering::SeqCst)
|
||||
}
|
||||
|
||||
fn wait(&self) {
|
||||
let mut released = self.released.lock().unwrap();
|
||||
let deadline = Instant::now() + HOLD_DEADLINE;
|
||||
loop {
|
||||
if *released {
|
||||
return;
|
||||
}
|
||||
let remaining = deadline.saturating_duration_since(Instant::now());
|
||||
if remaining.is_zero() {
|
||||
drop(released);
|
||||
self.abandoned.store(true, Ordering::SeqCst);
|
||||
panic!("the blocked fixture hold was not released within {HOLD_DEADLINE:?}");
|
||||
}
|
||||
let (state, _) = self.wake.wait_timeout(released, remaining).unwrap();
|
||||
released = state;
|
||||
}
|
||||
}
|
||||
|
||||
fn wait_bounded(&self, timeout: Duration) {
|
||||
let mut released = self.released.lock().unwrap();
|
||||
let deadline = Instant::now() + timeout;
|
||||
while !*released {
|
||||
let remaining = deadline.saturating_duration_since(Instant::now());
|
||||
if remaining.is_zero() {
|
||||
return;
|
||||
}
|
||||
let (state, _) = self.wake.wait_timeout(released, remaining).unwrap();
|
||||
released = state;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PushHttp for FakeHttp {
|
||||
fn execute(&self, _request: &HttpRequest) -> Result<HttpResponse, PushError> {
|
||||
self.calls.fetch_add(1, Ordering::SeqCst);
|
||||
if let Some(release) = &self.release {
|
||||
release.wait();
|
||||
}
|
||||
let mut responses = self.responses.lock().unwrap();
|
||||
if responses.is_empty() {
|
||||
return Err(PushError::new("push_http_failed", "No scripted response"));
|
||||
}
|
||||
Ok(responses.remove(0))
|
||||
}
|
||||
}
|
||||
|
||||
struct MemoryStore(Mutex<BTreeMap<String, Registration>>);
|
||||
|
||||
impl MemoryStore {
|
||||
fn empty() -> Arc<Self> {
|
||||
Arc::new(Self(Mutex::new(BTreeMap::new())))
|
||||
}
|
||||
}
|
||||
|
||||
impl PushStateStore for MemoryStore {
|
||||
fn load(&self, account: &str) -> Result<Option<Registration>, PushError> {
|
||||
Ok(self.0.lock().unwrap().get(account).cloned())
|
||||
}
|
||||
|
||||
fn save(&self, account: &str, registration: &Registration) -> Result<(), PushError> {
|
||||
self.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(account.to_owned(), registration.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn clear(&self, account: &str) -> Result<(), PushError> {
|
||||
self.0.lock().unwrap().remove(account);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct BlockingTransport {
|
||||
active: Arc<AtomicUsize>,
|
||||
}
|
||||
|
||||
impl PushTransport for BlockingTransport {
|
||||
fn send(&mut self, _frame: &[u8], _timeout: Duration) -> Result<(), PushError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn recv(&mut self, _timeout: Duration) -> Result<Vec<u8>, PushError> {
|
||||
std::thread::sleep(Duration::from_millis(5));
|
||||
Ok(Vec::new())
|
||||
}
|
||||
|
||||
fn close(&mut self) {}
|
||||
}
|
||||
|
||||
impl Drop for BlockingTransport {
|
||||
fn drop(&mut self) {
|
||||
self.active.fetch_sub(1, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
struct BlockingFactory {
|
||||
connection: Arc<Blocking>,
|
||||
connects: Arc<AtomicUsize>,
|
||||
active: Arc<AtomicUsize>,
|
||||
maximum: Arc<AtomicUsize>,
|
||||
}
|
||||
|
||||
impl PushTransportFactory for BlockingFactory {
|
||||
fn connect(
|
||||
&self,
|
||||
_host: &str,
|
||||
_port: u16,
|
||||
_timeout: Duration,
|
||||
) -> Result<Box<dyn PushTransport>, PushError> {
|
||||
self.connects.fetch_add(1, Ordering::SeqCst);
|
||||
self.connection.wait_bounded(Duration::from_millis(20));
|
||||
let active = self.active.fetch_add(1, Ordering::SeqCst) + 1;
|
||||
self.maximum.fetch_max(active, Ordering::SeqCst);
|
||||
Ok(Box::new(BlockingTransport {
|
||||
active: Arc::clone(&self.active),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
struct Harness {
|
||||
registry: PushRegistry,
|
||||
scope: Arc<Mutex<Option<PushScope>>>,
|
||||
tokens: Arc<Mutex<Vec<PushScope>>>,
|
||||
connect: Arc<Blocking>,
|
||||
connects: Arc<AtomicUsize>,
|
||||
active: Arc<AtomicUsize>,
|
||||
maximum: Arc<AtomicUsize>,
|
||||
factories: Arc<AtomicUsize>,
|
||||
http_calls: Arc<AtomicUsize>,
|
||||
}
|
||||
|
||||
impl Drop for Harness {
|
||||
fn drop(&mut self) {
|
||||
self.connect.release();
|
||||
if !std::thread::panicking() {
|
||||
assert!(
|
||||
!self.connect.abandoned(),
|
||||
"the blocked fixture hold must be released before its safety deadline"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn workers(harness: &Harness) -> usize {
|
||||
harness
|
||||
.registry
|
||||
.owner
|
||||
.as_ref()
|
||||
.map_or(0, PushOwner::worker_count)
|
||||
}
|
||||
|
||||
fn harness(data_dir: PathBuf, script_registration: bool) -> Harness {
|
||||
harness_with(data_dir, script_registration, false)
|
||||
}
|
||||
|
||||
fn harness_with(
|
||||
data_dir: PathBuf,
|
||||
script_registration: bool,
|
||||
hold_first_stage: bool,
|
||||
) -> Harness {
|
||||
let (output, _rx) = std::sync::mpsc::channel();
|
||||
let gfn = Arc::new(GfnService::new(std::env::temp_dir()).unwrap());
|
||||
let scope_state = Arc::new(Mutex::new(Some(scope())));
|
||||
let registry_scope: ScopeSource = {
|
||||
let scope_state = Arc::clone(&scope_state);
|
||||
Arc::new(move || scope_state.lock().unwrap().clone())
|
||||
};
|
||||
let tokens = Arc::new(Mutex::new(Vec::new()));
|
||||
let connect = Blocking::new();
|
||||
let connects = Arc::new(AtomicUsize::new(0));
|
||||
let active = Arc::new(AtomicUsize::new(0));
|
||||
let maximum = Arc::new(AtomicUsize::new(0));
|
||||
let factories = Arc::new(AtomicUsize::new(0));
|
||||
let http_calls = Arc::new(AtomicUsize::new(0));
|
||||
let holds = Arc::new(AtomicBool::new(hold_first_stage));
|
||||
let scripted = Arc::new(AtomicBool::new(script_registration));
|
||||
let deps_scope = Arc::clone(&scope_state);
|
||||
let deps_tokens = Arc::clone(&tokens);
|
||||
let deps_connect = Arc::clone(&connect);
|
||||
let deps_connects = Arc::clone(&connects);
|
||||
let deps_active = Arc::clone(&active);
|
||||
let deps_maximum = Arc::clone(&maximum);
|
||||
let deps_factories = Arc::clone(&factories);
|
||||
let deps_http_calls = Arc::clone(&http_calls);
|
||||
let deps_holds = Arc::clone(&holds);
|
||||
let deps_scripted = Arc::clone(&scripted);
|
||||
let deps_factory: Arc<DepsFactory> = Arc::new(move |_gfn, _scope_source, _output| {
|
||||
deps_factories.fetch_add(1, Ordering::SeqCst);
|
||||
let scope_source: ScopeSource = {
|
||||
let scope_state = Arc::clone(&deps_scope);
|
||||
Arc::new(move || scope_state.lock().unwrap().clone())
|
||||
};
|
||||
let token_source: GfnTokenSource = {
|
||||
let scope_state = Arc::clone(&deps_scope);
|
||||
let tokens = Arc::clone(&deps_tokens);
|
||||
Arc::new(move |scope: &PushScope| {
|
||||
let current = scope_state.lock().unwrap().clone()?;
|
||||
if current.user_id != scope.user_id
|
||||
|| current.provider_id != scope.provider_id
|
||||
|| current.generation != scope.generation
|
||||
{
|
||||
return None;
|
||||
}
|
||||
tokens.lock().unwrap().push(scope.clone());
|
||||
Some("gfnjwt".to_owned())
|
||||
})
|
||||
};
|
||||
let release = deps_holds
|
||||
.load(Ordering::SeqCst)
|
||||
.then(|| Arc::clone(&deps_connect));
|
||||
let http: Arc<dyn PushHttp> = Arc::new(FakeHttp {
|
||||
responses: Mutex::new(if deps_scripted.load(Ordering::SeqCst) {
|
||||
registration_responses()
|
||||
} else {
|
||||
Vec::new()
|
||||
}),
|
||||
calls: Arc::clone(&deps_http_calls),
|
||||
release,
|
||||
});
|
||||
Ok(PushOwnerDeps {
|
||||
http,
|
||||
transport: Arc::new(BlockingFactory {
|
||||
connection: Arc::clone(&deps_connect),
|
||||
connects: Arc::clone(&deps_connects),
|
||||
active: Arc::clone(&deps_active),
|
||||
maximum: Arc::clone(&deps_maximum),
|
||||
}),
|
||||
store: MemoryStore::empty(),
|
||||
sink: Arc::new(|_: PushEvent, _: u64| {}),
|
||||
scope_source,
|
||||
token_source,
|
||||
})
|
||||
});
|
||||
let registry =
|
||||
PushRegistry::with_deps_factory(gfn, output, data_dir, registry_scope, deps_factory);
|
||||
Harness {
|
||||
registry,
|
||||
scope: scope_state,
|
||||
tokens,
|
||||
connect,
|
||||
connects,
|
||||
active,
|
||||
maximum,
|
||||
factories,
|
||||
http_calls,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn desired_push_requires_a_matching_provider_and_service_configuration() {
|
||||
let dir = config_dir("provider");
|
||||
assert!(desired_push(&dir, &scope(), "device").is_some());
|
||||
let mut other_provider = scope();
|
||||
other_provider.provider_id = "alliance".into();
|
||||
assert!(desired_push(&dir, &other_provider, "device").is_none());
|
||||
let missing_dir = config_dir("missing-client");
|
||||
let mut missing_client = raw_config();
|
||||
missing_client["pnsClientId"] = json!("");
|
||||
std::fs::write(
|
||||
missing_dir.join(opennow_core::push::PUSH_CONFIG_FILE),
|
||||
missing_client.to_string(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(desired_push(&missing_dir, &scope(), "device").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn desired_push_uses_the_bundled_default_without_an_override() {
|
||||
let dir = empty_dir("bundled-default");
|
||||
let desired = desired_push(&dir, &scope(), "device").expect("the bundled default applies");
|
||||
assert_eq!(desired.config.provider_id, "nvidia");
|
||||
assert_eq!(desired.config.device_id, "device");
|
||||
assert!(!desired.config.endpoints.pns.is_empty());
|
||||
assert!(!desired.config.endpoints.pns_client_id.is_empty());
|
||||
assert!(!desired.config.identity.api_key.is_empty());
|
||||
assert!(
|
||||
dir.read_dir().unwrap().next().is_none(),
|
||||
"the bundled default must never write to the data directory"
|
||||
);
|
||||
let mut other_provider = scope();
|
||||
other_provider.provider_id = "alliance".into();
|
||||
let switched = desired_push(&dir, &other_provider, "device")
|
||||
.expect("the bundled default follows the authenticated provider");
|
||||
assert_eq!(switched.config.provider_id, "alliance");
|
||||
assert_eq!(
|
||||
desired.config.identity, switched.config.identity,
|
||||
"the bundled public identity is provider independent"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn desired_push_config_changes_with_device_and_configuration() {
|
||||
let dir = config_dir("key");
|
||||
let first = desired_push(&dir, &scope(), "device").unwrap();
|
||||
assert_eq!(
|
||||
first.config,
|
||||
desired_push(&dir, &scope(), "device").unwrap().config
|
||||
);
|
||||
let other_device = desired_push(&dir, &scope(), "other").unwrap();
|
||||
assert_ne!(first.config, other_device.config);
|
||||
let mut changed = raw_config();
|
||||
changed["pnsServer"] = json!("https://other.example");
|
||||
std::fs::write(
|
||||
dir.join(opennow_core::push::PUSH_CONFIG_FILE),
|
||||
changed.to_string(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_ne!(
|
||||
first.config,
|
||||
desired_push(&dir, &scope(), "device").unwrap().config
|
||||
);
|
||||
let mut rotated = raw_config();
|
||||
rotated["apiKey"] = json!("rotated-api-key");
|
||||
std::fs::write(
|
||||
dir.join(opennow_core::push::PUSH_CONFIG_FILE),
|
||||
rotated.to_string(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_ne!(
|
||||
first.config,
|
||||
desired_push(&dir, &scope(), "device").unwrap().config,
|
||||
"a rotated identity key must change the desired configuration"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn registry_propagates_identity_changes_across_the_same_owner() {
|
||||
let dir = config_dir("identity-churn");
|
||||
let mut harness = harness(dir.clone(), true);
|
||||
harness.registry.reconcile().unwrap();
|
||||
let deadline = Instant::now() + Duration::from_secs(2);
|
||||
while harness.tokens.lock().unwrap().is_empty() && Instant::now() < deadline {
|
||||
std::thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
let mut rotated = raw_config();
|
||||
rotated["apiKey"] = json!("rotated-api-key");
|
||||
std::fs::write(
|
||||
dir.join(opennow_core::push::PUSH_CONFIG_FILE),
|
||||
rotated.to_string(),
|
||||
)
|
||||
.unwrap();
|
||||
harness.registry.reconcile().unwrap();
|
||||
let owner_config = harness.registry.owner.as_ref().unwrap().config().0;
|
||||
assert_eq!(
|
||||
owner_config.identity.api_key, "rotated-api-key",
|
||||
"a changed identity must reach the live owner"
|
||||
);
|
||||
assert_eq!(
|
||||
harness.factories.load(Ordering::SeqCst),
|
||||
1,
|
||||
"the configuration change must reuse the single owner"
|
||||
);
|
||||
harness.connect.release();
|
||||
harness.registry.stop();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn registry_creates_no_owner_without_a_signed_in_scope() {
|
||||
let mut harness = harness(config_dir("no-scope"), true);
|
||||
*harness.scope.lock().unwrap() = None;
|
||||
harness.registry.reconcile().unwrap();
|
||||
harness.registry.reconcile().unwrap();
|
||||
assert_eq!(harness.factories.load(Ordering::SeqCst), 0);
|
||||
assert!(harness.registry.owner.is_none());
|
||||
harness.registry.stop();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn registry_stays_inactive_without_an_authenticated_scope() {
|
||||
let mut harness = harness(empty_dir("no-auth"), true);
|
||||
*harness.scope.lock().unwrap() = None;
|
||||
harness.registry.reconcile().unwrap();
|
||||
assert!(
|
||||
harness.registry.owner.is_none(),
|
||||
"the bundled default must not activate while signed out"
|
||||
);
|
||||
assert_eq!(harness.factories.load(Ordering::SeqCst), 0);
|
||||
*harness.scope.lock().unwrap() = Some(scope());
|
||||
harness.registry.reconcile().unwrap();
|
||||
assert!(
|
||||
harness.registry.owner.is_some(),
|
||||
"signing in must activate the bundled default"
|
||||
);
|
||||
*harness.scope.lock().unwrap() = None;
|
||||
harness.registry.reconcile().unwrap();
|
||||
assert!(!harness.registry.owner.as_ref().unwrap().is_running());
|
||||
harness.connect.release();
|
||||
harness.registry.stop();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn registry_activates_the_bundled_default_without_an_override_file() {
|
||||
let mut harness = harness(empty_dir("bundled-activation"), true);
|
||||
harness.registry.reconcile().unwrap();
|
||||
let config = harness
|
||||
.registry
|
||||
.owner
|
||||
.as_ref()
|
||||
.expect("the bundled default must activate the owner")
|
||||
.config()
|
||||
.0;
|
||||
assert_eq!(config.provider_id, "nvidia");
|
||||
assert!(!config.identity.api_key.is_empty());
|
||||
assert!(!config.endpoints.pns.is_empty());
|
||||
assert_eq!(harness.factories.load(Ordering::SeqCst), 1);
|
||||
harness.connect.release();
|
||||
harness.registry.stop();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn registry_prefers_an_explicit_override_over_the_bundled_default() {
|
||||
let mut harness = harness(config_dir("override-wins"), true);
|
||||
harness.registry.reconcile().unwrap();
|
||||
let config = harness
|
||||
.registry
|
||||
.owner
|
||||
.as_ref()
|
||||
.expect("the override must activate the owner")
|
||||
.config()
|
||||
.0;
|
||||
assert_eq!(config.identity.api_key, "api-key");
|
||||
assert_eq!(config.endpoints.pns, "https://pns.example/v1");
|
||||
harness.connect.release();
|
||||
harness.registry.stop();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn desired_push_rejects_an_insecure_override_base() {
|
||||
let dir = empty_dir("insecure-base");
|
||||
let mut config = raw_config();
|
||||
config["pnsServer"] = json!("http://pns.example");
|
||||
std::fs::write(
|
||||
dir.join(opennow_core::push::PUSH_CONFIG_FILE),
|
||||
config.to_string(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(
|
||||
desired_push(&dir, &scope(), "device").is_none(),
|
||||
"an insecure PNS override must not activate a subscription"
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn desired_push_never_activates_a_dangling_override_link() {
|
||||
let dir = empty_dir("dangling-link");
|
||||
std::os::unix::fs::symlink(
|
||||
dir.join("missing.json"),
|
||||
dir.join(opennow_core::push::PUSH_CONFIG_FILE),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(
|
||||
desired_push(&dir, &scope(), "device").is_none(),
|
||||
"a dangling override link must suppress the bundled default"
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn desired_push_returns_without_a_writer_on_a_non_regular_override() {
|
||||
use std::os::unix::ffi::OsStrExt as _;
|
||||
let dir = empty_dir("fifo");
|
||||
let path = dir.join(opennow_core::push::PUSH_CONFIG_FILE);
|
||||
let c_path = std::ffi::CString::new(path.as_os_str().as_bytes()).unwrap();
|
||||
assert_eq!(unsafe { libc::mkfifo(c_path.as_ptr(), 0o600) }, 0);
|
||||
let (sender, receiver) = std::sync::mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
let _ = sender.send(desired_push(&dir, &scope(), "device").is_none());
|
||||
});
|
||||
assert!(
|
||||
receiver
|
||||
.recv_timeout(Duration::from_secs(2))
|
||||
.expect("a non-regular override entry must not block reconciliation"),
|
||||
"a non-regular override must suppress the bundled default without a writer"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn registry_respects_an_explicitly_disabled_override() {
|
||||
let dir = empty_dir("disabled");
|
||||
std::fs::write(
|
||||
dir.join(opennow_core::push::PUSH_CONFIG_FILE),
|
||||
json!({"enabled": false}).to_string(),
|
||||
)
|
||||
.unwrap();
|
||||
let mut harness = harness(dir, true);
|
||||
harness.registry.reconcile().unwrap();
|
||||
assert!(
|
||||
harness.registry.owner.is_none(),
|
||||
"an explicit disable must not fall back to the bundled default"
|
||||
);
|
||||
assert_eq!(harness.factories.load(Ordering::SeqCst), 0);
|
||||
harness.registry.stop();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn registry_binds_the_bundled_default_to_the_authenticated_provider() {
|
||||
let mut harness = harness(empty_dir("provider-change"), true);
|
||||
harness.registry.reconcile().unwrap();
|
||||
assert_eq!(
|
||||
harness
|
||||
.registry
|
||||
.owner
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.config()
|
||||
.0
|
||||
.provider_id,
|
||||
"nvidia"
|
||||
);
|
||||
let mut switched = scope();
|
||||
switched.provider_id = "alliance".into();
|
||||
switched.generation = 4;
|
||||
*harness.scope.lock().unwrap() = Some(switched);
|
||||
harness.registry.reconcile().unwrap();
|
||||
let config = harness.registry.owner.as_ref().unwrap().config().0;
|
||||
assert_eq!(config.provider_id, "alliance");
|
||||
assert_eq!(
|
||||
harness.factories.load(Ordering::SeqCst),
|
||||
1,
|
||||
"a provider change must reuse the single owner"
|
||||
);
|
||||
harness.connect.release();
|
||||
harness.registry.stop();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn registry_reuses_one_owner_across_logout_and_login_cycles() {
|
||||
let mut harness = harness(config_dir("cycles"), true);
|
||||
harness.registry.reconcile().unwrap();
|
||||
let deadline = Instant::now() + Duration::from_secs(2);
|
||||
while harness.connects.load(Ordering::SeqCst) == 0 && Instant::now() < deadline {
|
||||
std::thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
assert!(
|
||||
harness.connects.load(Ordering::SeqCst) >= 1,
|
||||
"the worker must connect"
|
||||
);
|
||||
assert_eq!(harness.factories.load(Ordering::SeqCst), 1);
|
||||
for _ in 0..12 {
|
||||
*harness.scope.lock().unwrap() = None;
|
||||
harness.registry.reconcile().unwrap();
|
||||
*harness.scope.lock().unwrap() = Some(scope());
|
||||
harness.registry.reconcile().unwrap();
|
||||
}
|
||||
assert_eq!(
|
||||
harness.factories.load(Ordering::SeqCst),
|
||||
1,
|
||||
"a logout/login cycle must not create a second owner"
|
||||
);
|
||||
assert!(workers(&harness) <= 1);
|
||||
assert!(harness.maximum.load(Ordering::SeqCst) <= 1);
|
||||
harness.connect.release();
|
||||
let deadline = Instant::now() + Duration::from_secs(3);
|
||||
while workers(&harness) != 0 && Instant::now() < deadline {
|
||||
std::thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
harness.registry.stop();
|
||||
assert_eq!(harness.active.load(Ordering::SeqCst), 0);
|
||||
assert!(harness.maximum.load(Ordering::SeqCst) <= 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn registry_never_registers_a_new_provider_with_the_old_config() {
|
||||
let dir = config_dir("switch");
|
||||
let mut harness = harness(dir.clone(), true);
|
||||
harness.registry.reconcile().unwrap();
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
while harness.connects.load(Ordering::SeqCst) == 0 && Instant::now() < deadline {
|
||||
std::thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
assert!(
|
||||
harness.connects.load(Ordering::SeqCst) >= 1,
|
||||
"the nvidia session connects only after its registration finishes"
|
||||
);
|
||||
assert_eq!(
|
||||
harness.tokens.lock().unwrap().as_slice(),
|
||||
&[scope()],
|
||||
"the nvidia account registers"
|
||||
);
|
||||
let http_before = harness.http_calls.load(Ordering::SeqCst);
|
||||
assert_eq!(
|
||||
http_before, 5,
|
||||
"a completed nvidia registration runs every registration request"
|
||||
);
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
while !harness.registry.owner.as_ref().unwrap().is_running() && Instant::now() < deadline {
|
||||
std::thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
assert!(
|
||||
harness.registry.owner.as_ref().unwrap().is_running(),
|
||||
"the nvidia session is live before the provider switches"
|
||||
);
|
||||
let mut switched = scope();
|
||||
switched.provider_id = "alliance".into();
|
||||
switched.generation = 4;
|
||||
*harness.scope.lock().unwrap() = Some(switched.clone());
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
while harness.registry.owner.as_ref().unwrap().is_running() && Instant::now() < deadline {
|
||||
std::thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
assert!(
|
||||
!harness.registry.owner.as_ref().unwrap().is_running(),
|
||||
"the retired scope must end the nvidia session"
|
||||
);
|
||||
assert!(
|
||||
!harness
|
||||
.tokens
|
||||
.lock()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.any(|seen| seen.provider_id == "alliance"),
|
||||
"the old provider config must never request the new account's token"
|
||||
);
|
||||
assert_eq!(
|
||||
harness.http_calls.load(Ordering::SeqCst),
|
||||
http_before,
|
||||
"the old provider config must not run registration HTTP for the new provider"
|
||||
);
|
||||
assert_eq!(harness.factories.load(Ordering::SeqCst), 1);
|
||||
*harness.scope.lock().unwrap() = None;
|
||||
harness.registry.reconcile().unwrap();
|
||||
harness.connect.release();
|
||||
harness.registry.stop();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn registry_stops_a_blocked_registration_before_later_stages() {
|
||||
let mut harness = harness_with(config_dir("blocked-registration"), true, true);
|
||||
harness.registry.reconcile().unwrap();
|
||||
let deadline = Instant::now() + Duration::from_secs(2);
|
||||
while harness.http_calls.load(Ordering::SeqCst) == 0 && Instant::now() < deadline {
|
||||
std::thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
assert_eq!(
|
||||
harness.http_calls.load(Ordering::SeqCst),
|
||||
1,
|
||||
"the first stage must start"
|
||||
);
|
||||
*harness.scope.lock().unwrap() = None;
|
||||
harness.registry.reconcile().unwrap();
|
||||
harness.connect.release();
|
||||
let deadline = Instant::now() + Duration::from_secs(3);
|
||||
while workers(&harness) != 0 && Instant::now() < deadline {
|
||||
std::thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
harness.registry.stop();
|
||||
assert_eq!(
|
||||
harness.http_calls.load(Ordering::SeqCst),
|
||||
1,
|
||||
"no later registration stage may run after the scope retired"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn registry_sink_emits_the_documented_event_envelope() {
|
||||
let (output, rx) = std::sync::mpsc::channel();
|
||||
let gfn = Arc::new(GfnService::new(std::env::temp_dir()).unwrap());
|
||||
let registry = PushRegistry::new(gfn, output, std::env::temp_dir());
|
||||
let sink: PushSink = {
|
||||
let output = registry.output.clone();
|
||||
Arc::new(move |event, generation| {
|
||||
let _ = output.send(json!({
|
||||
"type": "event",
|
||||
"name": PUSH_EVENT_NAME,
|
||||
"payload": push_event_payload(&event, generation),
|
||||
}));
|
||||
})
|
||||
};
|
||||
sink(
|
||||
PushEvent::FavoritesChanged {
|
||||
changed_ids: vec!["app-1".into()],
|
||||
},
|
||||
11,
|
||||
);
|
||||
let message = rx.recv().unwrap();
|
||||
assert_eq!(message["type"], "event");
|
||||
assert_eq!(message["name"], "account.push.changed");
|
||||
assert_eq!(message["payload"]["generation"], 11);
|
||||
assert_eq!(message["payload"]["kind"], "favorites");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,523 @@
|
||||
use crate::gfn::ServiceError;
|
||||
use reqwest::blocking::Client;
|
||||
use serde::Serialize;
|
||||
use serde_json::{Map, Value, json};
|
||||
use std::collections::BTreeMap;
|
||||
use std::io::Read;
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
const QUEUE_URL: &str = "https://api.printedwaste.com/gfn/queue/";
|
||||
const MAPPING_URL: &str = "https://remote.printedwaste.com/config/GFN_SERVERID_TO_REGION_MAPPING";
|
||||
const MAX_BODY: usize = 256 * 1024;
|
||||
const MAX_ZONES: usize = 128;
|
||||
const PROBE_WORKERS: usize = 32;
|
||||
const MAX_AGE_SECONDS: u64 = 15 * 60;
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct Location {
|
||||
zone_id: String,
|
||||
title: String,
|
||||
region: String,
|
||||
queue_position: u64,
|
||||
eta_ms: Option<u64>,
|
||||
last_updated: u64,
|
||||
ping_ms: Option<u64>,
|
||||
streaming_base_url: String,
|
||||
alternate_count: usize,
|
||||
}
|
||||
|
||||
fn error(message: impl Into<String>) -> ServiceError {
|
||||
ServiceError {
|
||||
code: "queue_servers_failed",
|
||||
message: message.into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn zone_url(zone: &str) -> Option<String> {
|
||||
if !(9..=14).contains(&zone.len()) {
|
||||
return None;
|
||||
}
|
||||
let parts = zone.split('-').collect::<Vec<_>>();
|
||||
if parts.len() != 3
|
||||
|| parts[0] != "NP"
|
||||
|| !(3..=8).contains(&parts[1].len())
|
||||
|| !parts[1]
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_uppercase() || byte.is_ascii_digit())
|
||||
|| !parts[1].bytes().next()?.is_ascii_uppercase()
|
||||
|| parts[2].len() != 2
|
||||
|| !parts[2].bytes().all(|byte| byte.is_ascii_digit())
|
||||
{
|
||||
return None;
|
||||
}
|
||||
Some(format!(
|
||||
"https://{}.cloudmatchbeta.nvidiagrid.net/",
|
||||
zone.to_ascii_lowercase()
|
||||
))
|
||||
}
|
||||
|
||||
fn client(url: &str) -> Result<Client, ServiceError> {
|
||||
let parsed = url::Url::parse(url).map_err(|_| error("Queue endpoint is invalid"))?;
|
||||
let host = parsed
|
||||
.host_str()
|
||||
.ok_or_else(|| error("Queue endpoint has no host"))?;
|
||||
let port = parsed
|
||||
.port_or_known_default()
|
||||
.ok_or_else(|| error("Queue endpoint has no port"))?;
|
||||
let addresses = crate::network::resolve_bounded(host.to_owned(), port)
|
||||
.filter(|addresses| !addresses.is_empty())
|
||||
.ok_or_else(|| error("Queue endpoint could not be resolved"))?;
|
||||
crate::requests::check()?;
|
||||
Client::builder()
|
||||
.timeout(Duration::from_secs(5))
|
||||
.connect_timeout(Duration::from_secs(5))
|
||||
.resolve_to_addrs(host, &addresses)
|
||||
.redirect(reqwest::redirect::Policy::none())
|
||||
.pool_max_idle_per_host(0)
|
||||
.no_proxy()
|
||||
.user_agent("opennow-core")
|
||||
.build()
|
||||
.map_err(|_| error("Could not create the queue HTTP client"))
|
||||
}
|
||||
|
||||
fn fetch(url: &str) -> Result<Value, ServiceError> {
|
||||
crate::requests::check()?;
|
||||
let client = client(url)?;
|
||||
let mut response = client
|
||||
.get(url)
|
||||
.header("Accept", "application/json")
|
||||
.send()
|
||||
.map_err(|_| error("Queue data request failed"))?;
|
||||
crate::requests::check()?;
|
||||
if !response.status().is_success() {
|
||||
return Err(error(format!(
|
||||
"Queue data returned HTTP {}",
|
||||
response.status().as_u16()
|
||||
)));
|
||||
}
|
||||
if response
|
||||
.content_length()
|
||||
.is_some_and(|size| size > MAX_BODY as u64)
|
||||
{
|
||||
return Err(error("Queue data response is too large"));
|
||||
}
|
||||
let mut bytes = Vec::new();
|
||||
let mut chunk = [0_u8; 8192];
|
||||
loop {
|
||||
crate::requests::check()?;
|
||||
let count = response
|
||||
.read(&mut chunk)
|
||||
.map_err(|_| error("Could not read queue data"))?;
|
||||
if count == 0 {
|
||||
break;
|
||||
}
|
||||
if bytes.len() + count > MAX_BODY {
|
||||
return Err(error("Queue data response is too large"));
|
||||
}
|
||||
bytes.extend_from_slice(&chunk[..count]);
|
||||
}
|
||||
crate::requests::check()?;
|
||||
serde_json::from_slice(&bytes).map_err(|_| error("Queue data is not valid JSON"))
|
||||
}
|
||||
|
||||
fn payload_data(payload: &Value) -> Result<&Map<String, Value>, ServiceError> {
|
||||
if payload["status"].as_bool() != Some(true) {
|
||||
return Err(error("Queue data did not report success"));
|
||||
}
|
||||
let data = payload["data"]
|
||||
.as_object()
|
||||
.ok_or_else(|| error("Queue data is missing its data object"))?;
|
||||
if data.len() > 256 {
|
||||
return Err(error("Queue data contains too many entries"));
|
||||
}
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
fn text(value: &Value) -> Option<&str> {
|
||||
value
|
||||
.as_str()
|
||||
.map(str::trim)
|
||||
.filter(|text| !text.is_empty() && text.len() <= 128 && !text.chars().any(char::is_control))
|
||||
}
|
||||
|
||||
fn continent(region: &str) -> &str {
|
||||
match region {
|
||||
"US" => "North America",
|
||||
"CA" => "Canada",
|
||||
"EU" => "Europe",
|
||||
"JP" => "Japan",
|
||||
"KR" => "South Korea",
|
||||
"THAI" => "Southeast Asia",
|
||||
"MY" => "Malaysia",
|
||||
_ => region,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_zones(queue: &Value, mapping: &Value, now: u64) -> Result<Vec<Location>, ServiceError> {
|
||||
let data = payload_data(queue)?;
|
||||
let mapping = payload_data(mapping).ok();
|
||||
let mut zones = Vec::new();
|
||||
for (zone_id, raw) in data {
|
||||
let Some(streaming_base_url) = zone_url(zone_id) else {
|
||||
continue;
|
||||
};
|
||||
let entry = mapping
|
||||
.and_then(|mapping| mapping.get(zone_id))
|
||||
.unwrap_or(&Value::Null);
|
||||
if entry["nuked"].as_bool() == Some(true) {
|
||||
continue;
|
||||
}
|
||||
let (Some(queue_position), Some(last_updated), Some(region)) = (
|
||||
raw["QueuePosition"].as_u64(),
|
||||
raw["Last Updated"].as_u64(),
|
||||
text(&raw["Region"]),
|
||||
) else {
|
||||
continue;
|
||||
};
|
||||
if last_updated == 0
|
||||
|| last_updated > now.saturating_add(60)
|
||||
|| now.saturating_sub(last_updated) > MAX_AGE_SECONDS
|
||||
{
|
||||
continue;
|
||||
}
|
||||
zones.push(Location {
|
||||
zone_id: zone_id.clone(),
|
||||
title: text(&entry["title"]).unwrap_or(zone_id).to_owned(),
|
||||
region: text(&entry["region"])
|
||||
.unwrap_or_else(|| continent(region))
|
||||
.to_owned(),
|
||||
queue_position,
|
||||
eta_ms: raw["eta"].as_u64(),
|
||||
last_updated,
|
||||
ping_ms: None,
|
||||
streaming_base_url,
|
||||
alternate_count: 0,
|
||||
});
|
||||
}
|
||||
if zones.len() > MAX_ZONES {
|
||||
return Err(error("Queue data contains too many usable zones"));
|
||||
}
|
||||
if zones.is_empty() {
|
||||
return Err(error("Queue data has no fresh usable NVIDIA zones"));
|
||||
}
|
||||
Ok(zones)
|
||||
}
|
||||
|
||||
fn score(zone: &Location, max_ping: u64, max_queue: u64) -> f64 {
|
||||
0.75 * zone.ping_ms.unwrap_or(max_ping) as f64 / max_ping as f64
|
||||
+ 0.25 * zone.queue_position as f64 / max_queue as f64
|
||||
}
|
||||
|
||||
fn best(zones: &[Location]) -> usize {
|
||||
let measured = zones.iter().any(|zone| zone.ping_ms.is_some());
|
||||
let pool = zones
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, zone)| !measured || zone.ping_ms.is_some())
|
||||
.collect::<Vec<_>>();
|
||||
let max_ping = pool
|
||||
.iter()
|
||||
.filter_map(|(_, zone)| zone.ping_ms)
|
||||
.max()
|
||||
.unwrap_or(1)
|
||||
.max(1);
|
||||
let max_queue = pool
|
||||
.iter()
|
||||
.map(|(_, zone)| zone.queue_position)
|
||||
.max()
|
||||
.unwrap_or(1)
|
||||
.max(1);
|
||||
let selected = pool
|
||||
.iter()
|
||||
.min_by(|(_, a), (_, b)| {
|
||||
score(a, max_ping, max_queue)
|
||||
.total_cmp(&score(b, max_ping, max_queue))
|
||||
.then_with(|| a.ping_ms.cmp(&b.ping_ms))
|
||||
.then_with(|| a.queue_position.cmp(&b.queue_position))
|
||||
.then_with(|| a.zone_id.cmp(&b.zone_id))
|
||||
})
|
||||
.map(|(index, _)| *index)
|
||||
.unwrap_or(0);
|
||||
if zones[selected].ping_ms.is_some_and(|ping| ping > 100) {
|
||||
return pool
|
||||
.iter()
|
||||
.min_by_key(|(_, zone)| (zone.ping_ms, zone.queue_position, &zone.zone_id))
|
||||
.map(|(index, _)| *index)
|
||||
.unwrap_or(selected);
|
||||
}
|
||||
selected
|
||||
}
|
||||
|
||||
fn locations(zones: Vec<Location>) -> Value {
|
||||
let mut groups = BTreeMap::<(String, String), Vec<Location>>::new();
|
||||
for zone in zones {
|
||||
groups
|
||||
.entry((zone.region.clone(), zone.title.clone()))
|
||||
.or_default()
|
||||
.push(zone);
|
||||
}
|
||||
let mut locations = groups
|
||||
.into_values()
|
||||
.map(|group| {
|
||||
let mut primary = group[best(&group)].clone();
|
||||
primary.alternate_count = group.len() - 1;
|
||||
primary
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let recommended = (!locations.is_empty()).then(|| locations[best(&locations)].zone_id.clone());
|
||||
locations.sort_by(|a, b| {
|
||||
(Some(&b.zone_id) == recommended.as_ref())
|
||||
.cmp(&(Some(&a.zone_id) == recommended.as_ref()))
|
||||
.then_with(|| a.region.cmp(&b.region))
|
||||
.then_with(|| a.title.cmp(&b.title))
|
||||
});
|
||||
json!({"locations":locations,"recommendedZoneId":recommended})
|
||||
}
|
||||
|
||||
pub fn list() -> Result<Value, ServiceError> {
|
||||
crate::requests::check()?;
|
||||
let cancellation = crate::requests::current();
|
||||
let (queue, mapping) = std::thread::scope(|scope| {
|
||||
let mapping =
|
||||
scope.spawn(|| crate::requests::scope(cancellation.clone(), || fetch(MAPPING_URL)));
|
||||
let queue = fetch(QUEUE_URL);
|
||||
(
|
||||
queue,
|
||||
mapping
|
||||
.join()
|
||||
.unwrap_or_else(|_| Err(error("Queue mapping worker failed"))),
|
||||
)
|
||||
});
|
||||
crate::requests::check()?;
|
||||
let now = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map_err(|_| error("System clock is invalid"))?
|
||||
.as_secs();
|
||||
let mut zones = parse_zones(&queue?, &mapping.unwrap_or(Value::Null), now)?;
|
||||
for batch in zones.chunks_mut(PROBE_WORKERS) {
|
||||
crate::requests::check()?;
|
||||
std::thread::scope(|scope| {
|
||||
for zone in batch {
|
||||
let cancellation = cancellation.clone();
|
||||
scope.spawn(move || {
|
||||
zone.ping_ms = crate::requests::scope(cancellation, || {
|
||||
crate::network::measure_queue_region(&zone.streaming_base_url)
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
crate::requests::check()?;
|
||||
Ok(locations(zones))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::io::Write;
|
||||
use std::net::TcpListener;
|
||||
|
||||
fn queue() -> Value {
|
||||
json!({"status":true,"data":{
|
||||
"NP-NEW9-01":{"QueuePosition":3,"Last Updated":10000,"Region":"US","eta":60000},
|
||||
"NP-NEW9-02":{"QueuePosition":10,"Last Updated":10000,"Region":"US"}
|
||||
}})
|
||||
}
|
||||
|
||||
fn options() -> Vec<Location> {
|
||||
parse_zones(&queue(), &Value::Null, 10000).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strict_zone_identity_allows_unknown_locations_not_host_injection_or_alliance() {
|
||||
for zone in ["NP-NEW9-01", "NP-LAX-03", "NP-SJC6-04"] {
|
||||
assert!(zone_url(zone).is_some());
|
||||
}
|
||||
for zone in [
|
||||
"NPA-GKR-SEL-01",
|
||||
"np-lax-03",
|
||||
"NP-LAX-3",
|
||||
"NP-LAX-003",
|
||||
"NP-LAX-03.evil",
|
||||
"NP-LAX-03/path",
|
||||
"NP-LAX-03@evil",
|
||||
"NP--03",
|
||||
"NP-123-01",
|
||||
"NP-LAX-03\n",
|
||||
"NP-LAX-03-EXTRA",
|
||||
] {
|
||||
assert!(zone_url(zone).is_none(), "{zone}");
|
||||
}
|
||||
assert_eq!(
|
||||
zone_url("NP-LAX-03").unwrap(),
|
||||
"https://np-lax-03.cloudmatchbeta.nvidiagrid.net/"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn queue_errors_are_explicit_and_mapping_failure_preserves_raw_ids() {
|
||||
for payload in [
|
||||
Value::Null,
|
||||
json!({"status":false,"data":{}}),
|
||||
json!({"status":true}),
|
||||
json!({"status":true,"data":[]}),
|
||||
json!({"status":true,"data":{}}),
|
||||
] {
|
||||
assert!(parse_zones(&payload, &Value::Null, 10000).is_err());
|
||||
}
|
||||
for mapping in [
|
||||
Value::Null,
|
||||
json!({"status":false}),
|
||||
json!({"status":true,"data":{}}),
|
||||
] {
|
||||
let zones = parse_zones(&queue(), &mapping, 10000).unwrap();
|
||||
assert_eq!(zones[0].title, "NP-NEW9-01");
|
||||
assert_eq!(zones[0].region, "North America");
|
||||
assert_eq!(zones[0].eta_ms, Some(60000));
|
||||
assert_eq!(zones[1].eta_ms, None);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oversized_zone_sets_fail_before_any_probe_work() {
|
||||
let mut data = Map::new();
|
||||
for index in 0..=MAX_ZONES {
|
||||
data.insert(
|
||||
format!("NP-LOC{index}-01"),
|
||||
json!({"QueuePosition":1,"Last Updated":10000,"Region":"US"}),
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
parse_zones(&json!({"status":true,"data":data}), &Value::Null, 10000)
|
||||
.unwrap_err()
|
||||
.message,
|
||||
"Queue data contains too many usable zones"
|
||||
);
|
||||
for index in 0..257 {
|
||||
data.insert(index.to_string(), Value::Null);
|
||||
}
|
||||
assert_eq!(
|
||||
payload_data(&json!({"status":true,"data":data}))
|
||||
.unwrap_err()
|
||||
.message,
|
||||
"Queue data contains too many entries"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nuked_stale_future_and_malformed_rows_are_not_selectable() {
|
||||
let mut payload = queue();
|
||||
for (id, updated, position) in [
|
||||
("NP-OLD-01", 9099, json!(0)),
|
||||
("NP-FUT-01", 10061, json!(0)),
|
||||
("NP-BAD-01", 10000, json!(-1)),
|
||||
("NP-BAD-02", 10000, json!(1.5)),
|
||||
("NP-BAD-03", 10000, json!("1")),
|
||||
("NPA-BAD-01", 10000, json!(1)),
|
||||
] {
|
||||
payload["data"][id] =
|
||||
json!({"QueuePosition":position,"Last Updated":updated,"Region":"US"});
|
||||
}
|
||||
let mapping = json!({"status":true,"data":{"NP-NEW9-02":{"nuked":true}}});
|
||||
let zones = parse_zones(&payload, &mapping, 10000).unwrap();
|
||||
assert_eq!(zones.len(), 1);
|
||||
assert_eq!(zones[0].zone_id, "NP-NEW9-01");
|
||||
assert!(parse_zones(&payload, &mapping, 20000).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recommendation_weights_latency_and_queue_and_guards_high_latency() {
|
||||
let mut zones = options();
|
||||
zones[0].ping_ms = Some(20);
|
||||
zones[0].queue_position = 100;
|
||||
zones[1].ping_ms = Some(25);
|
||||
zones[1].queue_position = 0;
|
||||
assert_eq!(best(&zones), 1);
|
||||
zones[1].ping_ms = Some(80);
|
||||
assert_eq!(best(&zones), 0);
|
||||
zones[0].ping_ms = Some(110);
|
||||
zones[1].ping_ms = Some(120);
|
||||
assert_eq!(best(&zones), 0);
|
||||
zones[0].ping_ms = None;
|
||||
assert_eq!(best(&zones), 1);
|
||||
zones[1].ping_ms = None;
|
||||
assert_eq!(best(&zones), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn grouped_locations_use_measured_primary_and_recommend_returned_id() {
|
||||
let mapping = json!({"status":true,"data":{
|
||||
"NP-NEW9-01":{"title":" New location ","region":" US West "},
|
||||
"NP-NEW9-02":{"title":"New location","region":"US West"}
|
||||
}});
|
||||
let mut zones = parse_zones(&queue(), &mapping, 10000).unwrap();
|
||||
zones[1].ping_ms = Some(20);
|
||||
let result = locations(zones);
|
||||
assert_eq!(result["locations"].as_array().unwrap().len(), 1);
|
||||
assert_eq!(result["locations"][0]["title"], "New location");
|
||||
assert_eq!(result["locations"][0]["alternateCount"], 1);
|
||||
assert_eq!(result["locations"][0]["zoneId"], "NP-NEW9-02");
|
||||
assert_eq!(
|
||||
result["recommendedZoneId"],
|
||||
result["locations"][0]["zoneId"]
|
||||
);
|
||||
}
|
||||
|
||||
fn fetch_response(response: Vec<u8>) -> Result<Value, ServiceError> {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let url = format!("http://{}/", listener.local_addr().unwrap());
|
||||
let worker = std::thread::spawn(move || {
|
||||
let (mut stream, _) = listener.accept().unwrap();
|
||||
stream
|
||||
.set_read_timeout(Some(Duration::from_secs(2)))
|
||||
.unwrap();
|
||||
let mut request = [0; 8192];
|
||||
let count = stream.read(&mut request).unwrap();
|
||||
let request = String::from_utf8_lossy(&request[..count]).to_ascii_lowercase();
|
||||
assert!(!request.contains("authorization:"));
|
||||
assert!(!request.contains("cookie:"));
|
||||
let _ = stream.write_all(&response);
|
||||
});
|
||||
let result = fetch(&url);
|
||||
worker.join().unwrap();
|
||||
result
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn http_rejects_errors_redirects_invalid_json_and_oversized_streamed_bodies() {
|
||||
for response in [
|
||||
"HTTP/1.1 503 Unavailable\r\nContent-Length: 0\r\n\r\n",
|
||||
"HTTP/1.1 302 Found\r\nLocation: http://127.0.0.1:1/\r\nContent-Length: 0\r\n\r\n",
|
||||
"HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\n!",
|
||||
"HTTP/1.1 200 OK\r\nContent-Length: 999999999\r\n\r\n",
|
||||
] {
|
||||
assert!(fetch_response(response.as_bytes().to_vec()).is_err());
|
||||
}
|
||||
let mut response = b"HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n".to_vec();
|
||||
response.resize(response.len() + MAX_BODY + 1, b' ');
|
||||
assert_eq!(
|
||||
fetch_response(response).unwrap_err().message,
|
||||
"Queue data response is too large"
|
||||
);
|
||||
assert_eq!(
|
||||
fetch_response(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\n{}".to_vec()).unwrap(),
|
||||
json!({})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cancelled_queue_request_never_starts_fetches_or_probes() {
|
||||
let requests = std::sync::Arc::new(crate::requests::Requests::default());
|
||||
let permit = requests.admit("queue", "queue.servers.list").unwrap();
|
||||
requests.cancel("queue");
|
||||
crate::requests::scope(permit.token.clone(), || {
|
||||
assert_eq!(list().unwrap_err().code, "cancelled");
|
||||
assert_eq!(fetch("http://127.0.0.1:1").unwrap_err().code, "cancelled");
|
||||
assert_eq!(
|
||||
crate::network::measure_queue_region("http://127.0.0.1:1"),
|
||||
None
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
+529
@@ -0,0 +1,529 @@
|
||||
use flate2::{Compression, write::GzEncoder};
|
||||
use reqwest::blocking::{Client, multipart};
|
||||
use serde_json::{Value, json};
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Duration;
|
||||
|
||||
pub const DEFAULT_REPORTS_API: &str = "https://opennow-reports-production.up.railway.app";
|
||||
const MAX_REPORT_JSON_BYTES: usize = 64 * 1024;
|
||||
const MAX_LOG_TEXT_BYTES: u64 = 32 * 1024 * 1024;
|
||||
const MAX_LOG_GZIP_BYTES: usize = 5 * 1024 * 1024;
|
||||
const MAX_PENDING_REPORTS: usize = 5;
|
||||
const MAX_PENDING_AGE_MS: u128 = 7 * 24 * 60 * 60 * 1000;
|
||||
|
||||
pub fn api_base_from_env() -> String {
|
||||
std::env::var("OPENNOW_REPORTS_API")
|
||||
.ok()
|
||||
.map(|value| value.trim().trim_end_matches('/').to_owned())
|
||||
.filter(|value| value.starts_with("https://") || value.starts_with("http://"))
|
||||
.unwrap_or_else(|| DEFAULT_REPORTS_API.to_owned())
|
||||
}
|
||||
|
||||
pub fn valid_install_id(value: &str) -> bool {
|
||||
value.len() == 32 && value.bytes().all(|value| value.is_ascii_hexdigit())
|
||||
}
|
||||
|
||||
pub fn required_text(
|
||||
value: &Value,
|
||||
minimum: usize,
|
||||
maximum: usize,
|
||||
label: &str,
|
||||
) -> Result<String, String> {
|
||||
let value = value.as_str().map(str::trim).unwrap_or_default();
|
||||
if !(minimum..=maximum).contains(&value.chars().count()) {
|
||||
return Err(format!(
|
||||
"{label} must be between {minimum} and {maximum} characters"
|
||||
));
|
||||
}
|
||||
Ok(value.to_owned())
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Accepted {
|
||||
pub report_id: String,
|
||||
pub issue_id: Option<String>,
|
||||
pub issue_status: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Failure {
|
||||
pub message: String,
|
||||
pub retryable: bool,
|
||||
}
|
||||
|
||||
pub struct Document<'a> {
|
||||
pub install_id: &'a str,
|
||||
pub account: &'a Value,
|
||||
pub activity: Value,
|
||||
pub app: Value,
|
||||
}
|
||||
|
||||
impl Document<'_> {
|
||||
pub fn automatic(self, trigger: Value) -> Value {
|
||||
self.compose(true, trigger)
|
||||
}
|
||||
|
||||
pub fn manual(self, title: &str, description: &str) -> Value {
|
||||
self.compose(false, json!({"title":title, "description":description}))
|
||||
}
|
||||
|
||||
fn compose(self, automatic: bool, detail: Value) -> Value {
|
||||
let mut report = json!({
|
||||
"schemaVersion": 1,
|
||||
"automatic": automatic,
|
||||
"installId": self.install_id,
|
||||
"account": if self.account.is_object() { self.account.clone() } else { Value::Null },
|
||||
"activity": self.activity,
|
||||
"app": self.app
|
||||
});
|
||||
report[if automatic { "trigger" } else { "manual" }] = detail;
|
||||
report
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gzip_log(path: &Path) -> Option<Vec<u8>> {
|
||||
if path
|
||||
.metadata()
|
||||
.map_or(true, |metadata| metadata.len() > MAX_LOG_TEXT_BYTES)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let text = fs::read(path).ok()?;
|
||||
let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
|
||||
encoder.write_all(&text).ok()?;
|
||||
encoder
|
||||
.finish()
|
||||
.ok()
|
||||
.filter(|bytes| bytes.len() <= MAX_LOG_GZIP_BYTES)
|
||||
}
|
||||
|
||||
pub struct ReportsClient {
|
||||
client: Client,
|
||||
endpoint: String,
|
||||
pending: PathBuf,
|
||||
}
|
||||
|
||||
impl ReportsClient {
|
||||
pub fn new(base: &str, data_dir: &Path) -> Result<Self, String> {
|
||||
Ok(Self {
|
||||
client: Client::builder()
|
||||
.connect_timeout(Duration::from_secs(10))
|
||||
.timeout(Duration::from_secs(60))
|
||||
.build()
|
||||
.map_err(|error| error.to_string())?,
|
||||
endpoint: format!("{base}/v1/reports"),
|
||||
pending: data_dir.join("reports").join("pending"),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn submit(&self, report: &Value, log: Option<&[u8]>) -> Result<Accepted, Failure> {
|
||||
let body = report.to_string();
|
||||
if body.len() > MAX_REPORT_JSON_BYTES {
|
||||
return Err(Failure {
|
||||
message: "The bug report is too large to send.".to_owned(),
|
||||
retryable: false,
|
||||
});
|
||||
}
|
||||
let mut form = multipart::Form::new().part(
|
||||
"report",
|
||||
multipart::Part::text(body)
|
||||
.mime_str("application/json")
|
||||
.map_err(|error| Failure {
|
||||
message: error.to_string(),
|
||||
retryable: false,
|
||||
})?,
|
||||
);
|
||||
if let Some(log) = log {
|
||||
form = form.part(
|
||||
"log",
|
||||
multipart::Part::bytes(log.to_vec())
|
||||
.file_name("opennow-diagnostics.log.gz")
|
||||
.mime_str("application/gzip")
|
||||
.map_err(|error| Failure {
|
||||
message: error.to_string(),
|
||||
retryable: false,
|
||||
})?,
|
||||
);
|
||||
}
|
||||
let response = self
|
||||
.client
|
||||
.post(&self.endpoint)
|
||||
.multipart(form)
|
||||
.send()
|
||||
.map_err(|_| Failure {
|
||||
message: "The reporting service is unavailable right now.".to_owned(),
|
||||
retryable: true,
|
||||
})?;
|
||||
let status = response.status().as_u16();
|
||||
let body = response
|
||||
.text()
|
||||
.ok()
|
||||
.and_then(|body| serde_json::from_str::<Value>(&body).ok())
|
||||
.unwrap_or(Value::Null);
|
||||
if (200..300).contains(&status) {
|
||||
let text = |key: &str| body[key].as_str().map(|value| bounded(value, 64));
|
||||
return Ok(Accepted {
|
||||
report_id: text("reportId").unwrap_or_default(),
|
||||
issue_id: text("issueId"),
|
||||
issue_status: text("issueStatus"),
|
||||
});
|
||||
}
|
||||
Err(Failure {
|
||||
message: body["error"]["message"]
|
||||
.as_str()
|
||||
.map(|message| bounded(message, 320))
|
||||
.filter(|message| !message.is_empty())
|
||||
.unwrap_or_else(|| match status {
|
||||
429 => "Too many bug reports were sent. Try again later.".to_owned(),
|
||||
_ => format!("Bug report upload failed (HTTP {status})."),
|
||||
}),
|
||||
retryable: body["error"]["retryable"]
|
||||
.as_bool()
|
||||
.unwrap_or(status == 408 || status == 429 || status >= 500),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn spool(&self, report: &Value, log: Option<&[u8]>, now_ms: u128) -> std::io::Result<()> {
|
||||
fs::create_dir_all(&self.pending)?;
|
||||
let name = format!("{now_ms:013}-{:08x}", rand::random::<u32>());
|
||||
if let Some(log) = log {
|
||||
fs::write(self.pending.join(format!("{name}.log.gz")), log)?;
|
||||
}
|
||||
let path = self.pending.join(format!("{name}.json"));
|
||||
let temporary = path.with_extension("json.tmp");
|
||||
fs::write(&temporary, report.to_string())?;
|
||||
fs::rename(&temporary, &path)?;
|
||||
self.prune(now_ms);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn clear_pending(&self) {
|
||||
let _ = fs::remove_dir_all(&self.pending);
|
||||
}
|
||||
|
||||
pub fn retry_pending(&self, now_ms: u128) -> Vec<(Value, Accepted)> {
|
||||
let mut sent = Vec::new();
|
||||
for name in self.prune(now_ms) {
|
||||
let path = self.pending.join(format!("{name}.json"));
|
||||
let Some(report) = fs::read_to_string(&path)
|
||||
.ok()
|
||||
.and_then(|text| serde_json::from_str::<Value>(&text).ok())
|
||||
else {
|
||||
self.remove(&name);
|
||||
continue;
|
||||
};
|
||||
let log = fs::read(self.pending.join(format!("{name}.log.gz"))).ok();
|
||||
match self.submit(&report, log.as_deref()) {
|
||||
Ok(accepted) => {
|
||||
self.remove(&name);
|
||||
sent.push((report, accepted));
|
||||
}
|
||||
Err(failure) if failure.retryable => break,
|
||||
Err(_) => self.remove(&name),
|
||||
}
|
||||
}
|
||||
sent
|
||||
}
|
||||
|
||||
fn prune(&self, now_ms: u128) -> Vec<String> {
|
||||
let Ok(entries) = fs::read_dir(&self.pending) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let files = entries
|
||||
.filter_map(Result::ok)
|
||||
.filter_map(|entry| entry.file_name().into_string().ok())
|
||||
.collect::<Vec<_>>();
|
||||
let mut reports = files
|
||||
.iter()
|
||||
.filter_map(|file| file.strip_suffix(".json"))
|
||||
.map(ToOwned::to_owned)
|
||||
.collect::<Vec<_>>();
|
||||
reports.sort();
|
||||
let fresh = |name: &String| {
|
||||
name.split('-')
|
||||
.next()
|
||||
.and_then(|stamp| stamp.parse::<u128>().ok())
|
||||
.is_some_and(|stamp| now_ms.saturating_sub(stamp) <= MAX_PENDING_AGE_MS)
|
||||
};
|
||||
let excess = reports.len().saturating_sub(MAX_PENDING_REPORTS);
|
||||
let (kept, dropped): (Vec<_>, Vec<_>) = reports
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.partition(|(index, name)| *index >= excess && fresh(name));
|
||||
for (_, name) in dropped {
|
||||
self.remove(&name);
|
||||
}
|
||||
let kept = kept.into_iter().map(|(_, name)| name).collect::<Vec<_>>();
|
||||
for file in &files {
|
||||
let orphan = file
|
||||
.strip_suffix(".log.gz")
|
||||
.or_else(|| file.strip_suffix(".json.tmp"))
|
||||
.is_some_and(|name| !kept.iter().any(|kept| kept == name));
|
||||
if orphan {
|
||||
let _ = fs::remove_file(self.pending.join(file));
|
||||
}
|
||||
}
|
||||
kept
|
||||
}
|
||||
|
||||
fn remove(&self, name: &str) {
|
||||
let _ = fs::remove_file(self.pending.join(format!("{name}.json")));
|
||||
let _ = fs::remove_file(self.pending.join(format!("{name}.log.gz")));
|
||||
}
|
||||
}
|
||||
|
||||
fn bounded(value: &str, limit: usize) -> String {
|
||||
value
|
||||
.split_whitespace()
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
.chars()
|
||||
.take(limit)
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use flate2::read::GzDecoder;
|
||||
use std::io::{BufRead, BufReader, Read};
|
||||
use std::net::TcpListener;
|
||||
use std::thread;
|
||||
|
||||
const INSTALL: &str = "0123456789abcdef0123456789abcdef";
|
||||
|
||||
fn server(replies: Vec<(u16, Value)>) -> (String, thread::JoinHandle<Vec<Vec<u8>>>) {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let base = format!("http://{}", listener.local_addr().unwrap());
|
||||
let worker = thread::spawn(move || {
|
||||
let mut bodies = Vec::new();
|
||||
for (status, reply) in replies {
|
||||
let (mut stream, _) = listener.accept().unwrap();
|
||||
stream
|
||||
.set_read_timeout(Some(Duration::from_secs(5)))
|
||||
.unwrap();
|
||||
let mut reader = BufReader::new(&stream);
|
||||
let mut head = String::new();
|
||||
let mut length = 0;
|
||||
loop {
|
||||
let mut line = String::new();
|
||||
assert!(reader.read_line(&mut line).unwrap() > 0);
|
||||
if line == "\r\n" {
|
||||
break;
|
||||
}
|
||||
if let Some(value) = line.to_ascii_lowercase().strip_prefix("content-length:") {
|
||||
length = value.trim().parse().unwrap();
|
||||
}
|
||||
head.push_str(&line);
|
||||
}
|
||||
let mut body = vec![0; length];
|
||||
reader.read_exact(&mut body).unwrap();
|
||||
let mut request = head.into_bytes();
|
||||
request.extend_from_slice(b"\r\n");
|
||||
request.extend_from_slice(&body);
|
||||
bodies.push(request);
|
||||
let reply = reply.to_string();
|
||||
write!(stream, "HTTP/1.1 {status} Fixture\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{reply}", reply.len()).unwrap();
|
||||
}
|
||||
bodies
|
||||
});
|
||||
(base, worker)
|
||||
}
|
||||
|
||||
fn report() -> Value {
|
||||
Document {
|
||||
install_id: INSTALL,
|
||||
account: &json!({"userId":"sub-1", "providerIdpId":"idp", "providerCode":"NVIDIA",
|
||||
"providerName":"NVIDIA", "alliancePartner":false, "reporter":"Zortos",
|
||||
"reporterBasis":"username", "membershipTier":"ULTIMATE"}),
|
||||
activity: json!({"currentGame":{"title":"Portal 2","appId":"100"}, "recentGames":[]}),
|
||||
app: json!({"version":"1.0.3", "os":"linux", "arch":"x86_64", "gpu":null,
|
||||
"decoderBackend":"vaapi", "channel":"stable"}),
|
||||
}
|
||||
.automatic(
|
||||
json!({"kind":"frame_drops", "code":"sustained_frame_drops", "message":"",
|
||||
"metrics":{"droppedFrames":412.0}}),
|
||||
)
|
||||
}
|
||||
|
||||
fn find(haystack: &[u8], needle: &[u8]) -> Option<usize> {
|
||||
haystack
|
||||
.windows(needle.len())
|
||||
.position(|window| window == needle)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn manual_text_is_trimmed_and_bounded() {
|
||||
assert!(required_text(&json!("short"), 8, 120, "Bug report title").is_err());
|
||||
assert_eq!(
|
||||
required_text(&json!(" A valid title "), 8, 120, "Bug report title").unwrap(),
|
||||
"A valid title"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn documents_follow_schema_v1_and_carry_only_one_detail_section() {
|
||||
let automatic = report();
|
||||
assert_eq!(automatic["schemaVersion"], 1);
|
||||
assert_eq!(automatic["automatic"], true);
|
||||
assert!(automatic.get("manual").is_none());
|
||||
assert_eq!(automatic["trigger"]["code"], "sustained_frame_drops");
|
||||
let manual = Document {
|
||||
install_id: INSTALL,
|
||||
account: &Value::Null,
|
||||
activity: json!({"currentGame":null, "recentGames":[]}),
|
||||
app: json!({}),
|
||||
}
|
||||
.manual("Feedback: idea", "Add a dark mode please");
|
||||
assert_eq!(manual["automatic"], false);
|
||||
assert_eq!(manual["account"], Value::Null);
|
||||
assert!(manual.get("trigger").is_none());
|
||||
assert_eq!(manual["manual"]["title"], "Feedback: idea");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reports_upload_as_multipart_with_json_and_a_gzip_log() {
|
||||
let (base, worker) = server(vec![(
|
||||
201,
|
||||
json!({"reportId":"br-7F3A21", "issueId":"iss_1", "issueStatus":"investigating"}),
|
||||
)]);
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let client = ReportsClient::new(&base, directory.path()).unwrap();
|
||||
let log_path = directory.path().join("log.txt");
|
||||
fs::write(&log_path, "diagnostics line\n".repeat(64)).unwrap();
|
||||
let log = gzip_log(&log_path).unwrap();
|
||||
let accepted = client.submit(&report(), Some(&log)).unwrap();
|
||||
assert_eq!(
|
||||
accepted,
|
||||
Accepted {
|
||||
report_id: "br-7F3A21".to_owned(),
|
||||
issue_id: Some("iss_1".to_owned()),
|
||||
issue_status: Some("investigating".to_owned()),
|
||||
}
|
||||
);
|
||||
let request = worker.join().unwrap().pop().unwrap();
|
||||
let text = String::from_utf8_lossy(&request);
|
||||
assert!(text.starts_with("POST /v1/reports HTTP/1.1"));
|
||||
assert!(
|
||||
text.to_ascii_lowercase()
|
||||
.contains("content-type: multipart/form-data; boundary=")
|
||||
);
|
||||
assert!(text.contains("Content-Disposition: form-data; name=\"report\""));
|
||||
assert!(text.contains("Content-Type: application/json"));
|
||||
assert!(text.contains(&report().to_string()));
|
||||
assert!(text.contains(
|
||||
"Content-Disposition: form-data; name=\"log\"; filename=\"opennow-diagnostics.log.gz\""
|
||||
));
|
||||
let start = find(&request, &[0x1f, 0x8b]).expect("gzip payload");
|
||||
let mut decoded = String::new();
|
||||
GzDecoder::new(&request[start..])
|
||||
.read_to_string(&mut decoded)
|
||||
.unwrap();
|
||||
assert!(decoded.starts_with("diagnostics line\n"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_errors_report_their_retryability() {
|
||||
let (base, worker) = server(vec![
|
||||
(
|
||||
400,
|
||||
json!({"error":{"code":"schema","message":" bad schema ","retryable":false}}),
|
||||
),
|
||||
(503, Value::Null),
|
||||
(
|
||||
429,
|
||||
json!({"error":{"code":"rate_limited","message":"Slow down","retryable":true}}),
|
||||
),
|
||||
]);
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let client = ReportsClient::new(&base, directory.path()).unwrap();
|
||||
assert_eq!(
|
||||
client.submit(&report(), None).unwrap_err(),
|
||||
Failure {
|
||||
message: "bad schema".to_owned(),
|
||||
retryable: false
|
||||
}
|
||||
);
|
||||
assert!(client.submit(&report(), None).unwrap_err().retryable);
|
||||
assert!(client.submit(&report(), None).unwrap_err().retryable);
|
||||
worker.join().unwrap();
|
||||
let unreachable = ReportsClient::new("http://127.0.0.1:9", directory.path()).unwrap();
|
||||
assert!(unreachable.submit(&report(), None).unwrap_err().retryable);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pending_reports_keep_the_newest_five_within_seven_days() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let client = ReportsClient::new("http://127.0.0.1:9", directory.path()).unwrap();
|
||||
let day = 24 * 60 * 60 * 1000;
|
||||
let now = 100 * day;
|
||||
client
|
||||
.spool(&json!({"n":0}), Some(b"old"), now - 8 * day)
|
||||
.unwrap();
|
||||
for index in 1..=6 {
|
||||
client
|
||||
.spool(&json!({"n":index}), None, now - 6 * day + index)
|
||||
.unwrap();
|
||||
}
|
||||
let kept = client.prune(now);
|
||||
assert_eq!(kept.len(), 5);
|
||||
let numbers = kept
|
||||
.iter()
|
||||
.map(|name| {
|
||||
let text = fs::read_to_string(client.pending.join(format!("{name}.json"))).unwrap();
|
||||
serde_json::from_str::<Value>(&text).unwrap()["n"]
|
||||
.as_i64()
|
||||
.unwrap()
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(numbers, vec![2, 3, 4, 5, 6]);
|
||||
let gz = fs::read_dir(&client.pending)
|
||||
.unwrap()
|
||||
.filter(|entry| {
|
||||
entry
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.file_name()
|
||||
.to_string_lossy()
|
||||
.ends_with(".log.gz")
|
||||
})
|
||||
.count();
|
||||
assert_eq!(gz, 0);
|
||||
assert!(client.prune(now + 8 * day).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pending_reports_retry_until_the_service_is_unavailable() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let (base, worker) = server(vec![
|
||||
(201, json!({"reportId":"br-000001"})),
|
||||
(
|
||||
400,
|
||||
json!({"error":{"code":"schema","message":"bad","retryable":false}}),
|
||||
),
|
||||
(503, Value::Null),
|
||||
]);
|
||||
let client = ReportsClient::new(&base, directory.path()).unwrap();
|
||||
let now = 10_000_000;
|
||||
for index in 0..4 {
|
||||
client
|
||||
.spool(&json!({"n":index}), Some(b"log"), now + index)
|
||||
.unwrap();
|
||||
}
|
||||
let sent = client.retry_pending(now + 10);
|
||||
worker.join().unwrap();
|
||||
assert_eq!(sent.len(), 1);
|
||||
assert_eq!(sent[0].0["n"], 0);
|
||||
assert_eq!(sent[0].1.report_id, "br-000001");
|
||||
let remaining = client.prune(now + 10);
|
||||
assert_eq!(remaining.len(), 2);
|
||||
assert!(
|
||||
client
|
||||
.pending
|
||||
.join(format!("{}.log.gz", remaining[0]))
|
||||
.is_file()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
//! Bounded RPC ownership, cooperative cancellation, and allocation receipts.
|
||||
use crate::gfn::ServiceError;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Condvar, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
const MAX_ACTIVE: usize = 8;
|
||||
const MAX_BACKGROUND: usize = 4;
|
||||
|
||||
#[derive(Default)]
|
||||
struct RequestState {
|
||||
cancelled: AtomicBool,
|
||||
accepted: Mutex<bool>,
|
||||
wake: Condvar,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct Cancellation(Arc<RequestState>);
|
||||
impl Cancellation {
|
||||
pub fn commit<T>(
|
||||
&self,
|
||||
work: impl FnOnce() -> Result<T, ServiceError>,
|
||||
) -> Result<T, ServiceError> {
|
||||
let _commit = self.0.accepted.lock().expect("request commit poisoned");
|
||||
self.check()?;
|
||||
work()
|
||||
}
|
||||
pub fn cancelled(&self) -> bool {
|
||||
self.0.cancelled.load(Ordering::Acquire)
|
||||
}
|
||||
|
||||
pub fn await_acceptance(&self, timeout: Duration) -> bool {
|
||||
let accepted = self.0.accepted.lock().expect("request receipt poisoned");
|
||||
let (accepted, _) = self
|
||||
.0
|
||||
.wake
|
||||
.wait_timeout_while(accepted, timeout, |accepted| {
|
||||
!*accepted && !self.cancelled()
|
||||
})
|
||||
.expect("request receipt poisoned");
|
||||
*accepted && !self.cancelled()
|
||||
}
|
||||
pub fn check(&self) -> Result<(), ServiceError> {
|
||||
if self.cancelled() {
|
||||
Err(ServiceError {
|
||||
code: "cancelled",
|
||||
message: "Request cancelled".into(),
|
||||
})
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Requests(Mutex<HashMap<String, (bool, Cancellation)>>);
|
||||
impl Requests {
|
||||
pub fn admit(self: &Arc<Self>, id: &str, method: &str) -> Option<Permit> {
|
||||
let background = method.starts_with("catalog.")
|
||||
|| method.starts_with("artwork.")
|
||||
|| method == "network.regions.ping"
|
||||
|| method == "queue.servers.list";
|
||||
let mut active = self.0.lock().expect("request state poisoned");
|
||||
if active.contains_key(id)
|
||||
|| active.len() >= MAX_ACTIVE
|
||||
|| (background && active.values().filter(|(bg, _)| *bg).count() >= MAX_BACKGROUND)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let token = Cancellation::default();
|
||||
active.insert(id.into(), (background, token.clone()));
|
||||
Some(Permit {
|
||||
owner: self.clone(),
|
||||
id: id.into(),
|
||||
token,
|
||||
})
|
||||
}
|
||||
pub fn cancel(&self, id: &str) {
|
||||
if let Some((_, token)) = self.0.lock().expect("request state poisoned").get(id) {
|
||||
let _accepted = token.0.accepted.lock().expect("request receipt poisoned");
|
||||
token.0.cancelled.store(true, Ordering::Release);
|
||||
token.0.wake.notify_all();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn acknowledge(&self, id: &str) {
|
||||
if let Some((_, token)) = self.0.lock().expect("request state poisoned").get(id) {
|
||||
*token.0.accepted.lock().expect("request receipt poisoned") = true;
|
||||
token.0.wake.notify_all();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Permit {
|
||||
owner: Arc<Requests>,
|
||||
id: String,
|
||||
pub token: Cancellation,
|
||||
}
|
||||
impl Drop for Permit {
|
||||
fn drop(&mut self) {
|
||||
self.owner
|
||||
.0
|
||||
.lock()
|
||||
.expect("request state poisoned")
|
||||
.remove(&self.id);
|
||||
}
|
||||
}
|
||||
|
||||
thread_local! { static CURRENT: RefCell<Cancellation> = RefCell::default(); }
|
||||
pub fn current() -> Cancellation {
|
||||
CURRENT.with(|token| token.borrow().clone())
|
||||
}
|
||||
pub fn check() -> Result<(), ServiceError> {
|
||||
current().check()
|
||||
}
|
||||
|
||||
// A request worker is synchronous. Scoped child threads explicitly clone the
|
||||
// token; cache/download workers intentionally have independent lifetimes.
|
||||
pub fn scope<T>(token: Cancellation, work: impl FnOnce() -> T) -> T {
|
||||
struct Restore(Cancellation);
|
||||
impl Drop for Restore {
|
||||
fn drop(&mut self) {
|
||||
CURRENT.with(|token| *token.borrow_mut() = self.0.clone());
|
||||
}
|
||||
}
|
||||
let _restore = Restore(CURRENT.with(|current| current.replace(token)));
|
||||
work()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn auth_commit_fence_rejects_prior_cancellation_and_finishes_entered_commit() {
|
||||
let requests = Arc::new(Requests::default());
|
||||
let permit = requests.admit("login", "auth.device.complete").unwrap();
|
||||
requests.cancel("login");
|
||||
assert!(
|
||||
permit
|
||||
.token
|
||||
.commit(|| -> Result<(), ServiceError> { panic!("cancelled commit ran") })
|
||||
.is_err()
|
||||
);
|
||||
let permit = requests.admit("second", "auth.device.complete").unwrap();
|
||||
let (entered_tx, entered_rx) = std::sync::mpsc::channel();
|
||||
let (release_tx, release_rx) = std::sync::mpsc::channel();
|
||||
let token = permit.token.clone();
|
||||
std::thread::scope(|scope| {
|
||||
let worker = scope.spawn(move || {
|
||||
token.commit(|| {
|
||||
entered_tx.send(()).unwrap();
|
||||
release_rx.recv_timeout(Duration::from_secs(5)).unwrap();
|
||||
Ok("committed")
|
||||
})
|
||||
});
|
||||
entered_rx.recv_timeout(Duration::from_secs(5)).unwrap();
|
||||
let cancellation = scope.spawn(|| requests.cancel("second"));
|
||||
release_tx.send(()).unwrap();
|
||||
assert_eq!(worker.join().unwrap().unwrap(), "committed");
|
||||
cancellation.join().unwrap();
|
||||
});
|
||||
assert!(permit.token.cancelled());
|
||||
}
|
||||
#[test]
|
||||
fn allocation_receipt_requires_acceptance_and_cancellation_wins() {
|
||||
let requests = Arc::new(Requests::default());
|
||||
let permit = requests.admit("create", "session.create").unwrap();
|
||||
assert!(!permit.token.await_acceptance(Duration::ZERO));
|
||||
requests.acknowledge("unknown");
|
||||
assert!(!permit.token.await_acceptance(Duration::ZERO));
|
||||
requests.acknowledge("create");
|
||||
assert!(permit.token.await_acceptance(Duration::ZERO));
|
||||
requests.cancel("create");
|
||||
assert!(!permit.token.await_acceptance(Duration::ZERO));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cancellation_wakes_pending_allocation_receipt() {
|
||||
let requests = Arc::new(Requests::default());
|
||||
let permit = requests.admit("create", "session.create").unwrap();
|
||||
std::thread::scope(|scope| {
|
||||
let token = permit.token.clone();
|
||||
let worker = scope.spawn(move || token.await_acceptance(Duration::from_secs(10)));
|
||||
requests.cancel("create");
|
||||
assert!(!worker.join().unwrap());
|
||||
});
|
||||
}
|
||||
#[test]
|
||||
fn background_load_reserves_control_capacity_until_workers_really_exit() {
|
||||
let requests = Arc::new(Requests::default());
|
||||
let mut permits = Vec::new();
|
||||
for id in 0..4 {
|
||||
permits.push(
|
||||
requests
|
||||
.admit(&id.to_string(), "catalog.store.local")
|
||||
.unwrap(),
|
||||
);
|
||||
}
|
||||
requests.cancel("0");
|
||||
assert!(permits[0].token.cancelled());
|
||||
assert!(requests.admit("more", "catalog.store.local").is_none());
|
||||
for id in 4..8 {
|
||||
permits.push(requests.admit(&id.to_string(), "session.poll").unwrap());
|
||||
}
|
||||
assert!(requests.admit("overflow", "session.stop").is_none());
|
||||
permits.clear();
|
||||
assert!(requests.admit("new", "catalog.store.local").is_some());
|
||||
}
|
||||
#[test]
|
||||
fn unknown_cancels_do_not_accumulate_and_scopes_restore() {
|
||||
let requests = Arc::new(Requests::default());
|
||||
for id in 0..10000 {
|
||||
requests.cancel(&id.to_string());
|
||||
}
|
||||
assert!(requests.0.lock().unwrap().is_empty());
|
||||
let permit = requests.admit("work", "catalog.store.local").unwrap();
|
||||
assert!(requests.admit("work", "session.poll").is_none());
|
||||
requests.cancel("work");
|
||||
scope(permit.token.clone(), || {
|
||||
assert_eq!(check().unwrap_err().code, "cancelled")
|
||||
});
|
||||
assert!(check().is_ok());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
use crate::gfn::ServiceError;
|
||||
use serde_json::json;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
const FRESHNESS: Duration = Duration::from_secs(5 * 60);
|
||||
const FAILURE_BACKOFF: Duration = Duration::from_secs(30);
|
||||
|
||||
struct Entry {
|
||||
result: Result<String, ServiceError>,
|
||||
expires: Instant,
|
||||
}
|
||||
|
||||
type ScopeEntry = Arc<Mutex<Option<Entry>>>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ServerVpcCache(Mutex<HashMap<[u8; 32], ScopeEntry>>);
|
||||
|
||||
impl ServerVpcCache {
|
||||
pub fn resolve(
|
||||
&self,
|
||||
provider: &str,
|
||||
account: &str,
|
||||
token: &str,
|
||||
fetch: impl FnOnce() -> Result<Option<String>, ServiceError>,
|
||||
) -> Result<String, ServiceError> {
|
||||
let scope = Sha256::digest(json!([provider, account, token]).to_string().as_bytes()).into();
|
||||
let slot = {
|
||||
let mut entries = crate::store_requests::lock(&self.0)?;
|
||||
if !entries.contains_key(&scope) && entries.len() >= 16 {
|
||||
entries.retain(|_, entry| Arc::strong_count(entry) > 1);
|
||||
if entries.len() >= 16 {
|
||||
return Err(ServiceError {
|
||||
code: "routing_busy",
|
||||
message: "Too many provider lookups are in progress".into(),
|
||||
});
|
||||
}
|
||||
}
|
||||
entries.entry(scope).or_default().clone()
|
||||
};
|
||||
let mut cached = crate::store_requests::lock(&slot)?;
|
||||
if let Some(entry) = cached
|
||||
.as_ref()
|
||||
.filter(|entry| entry.expires > Instant::now())
|
||||
{
|
||||
return entry.result.clone();
|
||||
}
|
||||
let result = fetch().and_then(|value| {
|
||||
value
|
||||
.filter(|value| !value.trim().is_empty())
|
||||
.ok_or_else(|| ServiceError {
|
||||
code: "invalid_upstream_response",
|
||||
message: "Server info did not include a verified VPC".into(),
|
||||
})
|
||||
});
|
||||
crate::requests::check()?;
|
||||
if result.as_ref().is_err_and(|error| {
|
||||
matches!(
|
||||
error.code,
|
||||
"http_unauthorized" | "rate_limited" | "cancelled" | "stale_account"
|
||||
)
|
||||
}) {
|
||||
return result;
|
||||
}
|
||||
let freshness = if result.is_ok() {
|
||||
FRESHNESS
|
||||
} else {
|
||||
FAILURE_BACKOFF
|
||||
};
|
||||
*cached = Some(Entry {
|
||||
result: result.clone(),
|
||||
expires: Instant::now() + freshness,
|
||||
});
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::{
|
||||
Arc, Barrier,
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
};
|
||||
|
||||
fn only_entry(cache: &ServerVpcCache) -> Arc<Mutex<Option<Entry>>> {
|
||||
cache.0.lock().unwrap().values().next().unwrap().clone()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unrelated_provider_lookup_does_not_wait_for_an_inflight_scope() {
|
||||
let cache = ServerVpcCache::default();
|
||||
let (entered_tx, entered_rx) = std::sync::mpsc::channel();
|
||||
let (release_tx, release_rx) = std::sync::mpsc::channel();
|
||||
std::thread::scope(|threads| {
|
||||
let first = threads.spawn(|| {
|
||||
cache.resolve("provider-a", "account-a", "token-a", move || {
|
||||
entered_tx.send(()).unwrap();
|
||||
release_rx.recv_timeout(Duration::from_secs(5)).unwrap();
|
||||
Ok(Some("vpc-a".into()))
|
||||
})
|
||||
});
|
||||
entered_rx.recv_timeout(Duration::from_secs(5)).unwrap();
|
||||
assert_eq!(
|
||||
cache
|
||||
.resolve("provider-b", "account-b", "token-b", || Ok(Some(
|
||||
"vpc-b".into()
|
||||
)))
|
||||
.unwrap(),
|
||||
"vpc-b"
|
||||
);
|
||||
release_tx.send(()).unwrap();
|
||||
assert_eq!(first.join().unwrap().unwrap(), "vpc-a");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn concurrent_lookups_share_one_fetch_and_reuse_it_for_five_minutes() {
|
||||
let cache = ServerVpcCache::default();
|
||||
let ready = Barrier::new(4);
|
||||
let calls = AtomicUsize::new(0);
|
||||
let started = Instant::now();
|
||||
std::thread::scope(|threads| {
|
||||
for _ in 0..4 {
|
||||
let cache = &cache;
|
||||
let ready = &ready;
|
||||
let calls = &calls;
|
||||
threads.spawn(move || {
|
||||
ready.wait();
|
||||
let result = cache
|
||||
.resolve("provider", "account", "token", || {
|
||||
calls.fetch_add(1, Ordering::SeqCst);
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
Ok(Some("region-a".into()))
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(result, "region-a");
|
||||
});
|
||||
}
|
||||
});
|
||||
assert_eq!(calls.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(
|
||||
cache
|
||||
.resolve("provider", "account", "token", || panic!(
|
||||
"fresh metadata refetched"
|
||||
))
|
||||
.unwrap(),
|
||||
"region-a"
|
||||
);
|
||||
let slot = only_entry(&cache);
|
||||
let mut cached = slot.lock().unwrap();
|
||||
let entry = cached.as_mut().unwrap();
|
||||
assert!(entry.expires >= started + FRESHNESS);
|
||||
assert!(entry.expires <= Instant::now() + FRESHNESS);
|
||||
entry.expires = Instant::now() - Duration::from_secs(1);
|
||||
drop(cached);
|
||||
assert_eq!(
|
||||
cache
|
||||
.resolve("provider", "account", "token", || Ok(Some(
|
||||
"region-b".into()
|
||||
)))
|
||||
.unwrap(),
|
||||
"region-b"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn providers_accounts_and_refreshed_tokens_do_not_share_metadata() {
|
||||
let cache = ServerVpcCache::default();
|
||||
for (index, (provider, account, token)) in [
|
||||
("provider-a", "account-a", "token-a"),
|
||||
("provider-b", "account-a", "token-a"),
|
||||
("provider-b", "account-b", "token-a"),
|
||||
("provider-b", "account-b", "token-b"),
|
||||
]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
let expected = format!("region-{index}");
|
||||
assert_eq!(
|
||||
cache
|
||||
.resolve(provider, account, token, || Ok(Some(expected.clone())))
|
||||
.unwrap(),
|
||||
expected
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failures_preserve_known_metadata_with_a_short_retry_backoff() {
|
||||
let cache = ServerVpcCache::default();
|
||||
assert_eq!(
|
||||
cache.resolve("p", "a", "t", || Ok(None)).unwrap_err().code,
|
||||
"invalid_upstream_response"
|
||||
);
|
||||
assert!(
|
||||
only_entry(&cache)
|
||||
.lock()
|
||||
.unwrap()
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.expires
|
||||
.duration_since(Instant::now())
|
||||
<= FAILURE_BACKOFF
|
||||
);
|
||||
cache
|
||||
.resolve("p", "a", "t", || {
|
||||
panic!("failed lookup retried immediately")
|
||||
})
|
||||
.unwrap_err();
|
||||
only_entry(&cache).lock().unwrap().as_mut().unwrap().expires =
|
||||
Instant::now() - Duration::from_secs(1);
|
||||
assert_eq!(
|
||||
cache
|
||||
.resolve("p", "a", "t", || Ok(Some("known".into())))
|
||||
.unwrap(),
|
||||
"known"
|
||||
);
|
||||
only_entry(&cache).lock().unwrap().as_mut().unwrap().expires =
|
||||
Instant::now() - Duration::from_secs(1);
|
||||
assert_eq!(
|
||||
cache.resolve("p", "a", "t", || Ok(None)).unwrap_err().code,
|
||||
"invalid_upstream_response"
|
||||
);
|
||||
assert_eq!(
|
||||
cache
|
||||
.resolve("other", "a", "t", || Ok(None))
|
||||
.unwrap_err()
|
||||
.code,
|
||||
"invalid_upstream_response"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rate_limit_errors_are_not_cached_as_successful_metadata() {
|
||||
let cache = ServerVpcCache::default();
|
||||
let error = cache
|
||||
.resolve("p", "a", "t", || {
|
||||
Err(ServiceError {
|
||||
code: "rate_limited",
|
||||
message: "Try later".into(),
|
||||
})
|
||||
})
|
||||
.unwrap_err();
|
||||
assert_eq!(error.code, "rate_limited");
|
||||
assert!(only_entry(&cache).lock().unwrap().is_none());
|
||||
assert_eq!(
|
||||
cache
|
||||
.resolve("p", "a", "t", || Ok(Some("recovered".into())))
|
||||
.unwrap(),
|
||||
"recovered"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cancelled_lookups_neither_wait_for_the_cache_nor_publish_results() {
|
||||
let cache = ServerVpcCache::default();
|
||||
let requests = Arc::new(crate::requests::Requests::default());
|
||||
let permit = requests.admit("lookup", "catalog.store.local").unwrap();
|
||||
crate::requests::scope(permit.token.clone(), || {
|
||||
assert_eq!(
|
||||
cache
|
||||
.resolve("p", "a", "t", || {
|
||||
requests.cancel("lookup");
|
||||
Ok(Some("cancelled".into()))
|
||||
})
|
||||
.unwrap_err()
|
||||
.code,
|
||||
"cancelled"
|
||||
);
|
||||
let held = cache.0.lock().unwrap();
|
||||
assert!(held.values().all(|entry| entry.lock().unwrap().is_none()));
|
||||
assert_eq!(
|
||||
cache
|
||||
.resolve("p", "a", "t", || panic!("cancelled fetch started"))
|
||||
.unwrap_err()
|
||||
.code,
|
||||
"cancelled"
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
+3467
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,676 @@
|
||||
//! Bounded, account/provider-scoped Store responses persisted across core restarts.
|
||||
//! Only successful protocol-sized results are cached; credentials never enter files.
|
||||
use crate::{gfn::ServiceError, store_catalog_page::RESULT_BUDGET};
|
||||
use serde_json::Value;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::collections::HashMap;
|
||||
use std::fs::{self, File, OpenOptions};
|
||||
use std::io::{Read, Write};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, Mutex, Weak};
|
||||
|
||||
const MAX_BYTES: u64 = 64 * 1024 * 1024;
|
||||
const MAX_ENTRIES: usize = 512;
|
||||
const PAGE_TTL_MS: u64 = 15 * 60 * 1000;
|
||||
|
||||
type FetchLocks = HashMap<(PathBuf, u64), Weak<Mutex<()>>>;
|
||||
|
||||
pub struct CachePolicy {
|
||||
pub ttl_ms: u64,
|
||||
pub allow_stale: bool,
|
||||
}
|
||||
|
||||
pub struct StoreCache {
|
||||
root: PathBuf,
|
||||
index: Mutex<Option<(String, crate::store_index::StoreIndex, u64)>>,
|
||||
// IO is serialized, but network work never holds the lock. Invalidation
|
||||
// advances the epoch so an older in-flight fetch cannot refill cleared data.
|
||||
epoch: Mutex<u64>,
|
||||
fetches: Mutex<FetchLocks>,
|
||||
pub requests: crate::store_requests::StoreRequests,
|
||||
}
|
||||
|
||||
fn digest(value: &Value) -> String {
|
||||
format!("{:x}", Sha256::digest(value.to_string().as_bytes()))
|
||||
}
|
||||
|
||||
impl StoreCache {
|
||||
pub fn new(data_dir: PathBuf) -> Self {
|
||||
Self {
|
||||
root: data_dir.join("store-cache-v2"),
|
||||
index: Mutex::new(None),
|
||||
epoch: Mutex::new(0),
|
||||
fetches: Mutex::new(HashMap::new()),
|
||||
requests: crate::store_requests::StoreRequests::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_or_fetch(
|
||||
&self,
|
||||
scope: &Value,
|
||||
key: &Value,
|
||||
refresh: bool,
|
||||
fetch: impl FnOnce() -> Result<Value, ServiceError>,
|
||||
) -> Result<Value, ServiceError> {
|
||||
self.load_with_policy(
|
||||
scope,
|
||||
key,
|
||||
refresh,
|
||||
CachePolicy {
|
||||
ttl_ms: PAGE_TTL_MS,
|
||||
allow_stale: false,
|
||||
},
|
||||
fetch,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn catalog_revision(&self) -> u64 {
|
||||
let path = self.root.join("revision");
|
||||
let file = match File::open(&path) {
|
||||
Ok(file) => file,
|
||||
Err(error)
|
||||
if error.kind() == std::io::ErrorKind::NotFound
|
||||
&& fs::symlink_metadata(&path)
|
||||
.is_err_and(|error| error.kind() == std::io::ErrorKind::NotFound) =>
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
Err(_) => return now_ms(),
|
||||
};
|
||||
let mut bytes = Vec::new();
|
||||
if file.take(32).read_to_end(&mut bytes).is_err() {
|
||||
return now_ms();
|
||||
}
|
||||
serde_json::from_slice::<u64>(&bytes)
|
||||
.ok()
|
||||
.filter(|revision| *revision < (1_u64 << 52))
|
||||
.unwrap_or_else(now_ms)
|
||||
}
|
||||
|
||||
pub fn invalidate(&self, revision: u64) -> Result<(), ServiceError> {
|
||||
let mut epoch = self.epoch.lock().expect("Store cache poisoned");
|
||||
*epoch = epoch.wrapping_add(1);
|
||||
*self.index.lock().expect("Store index poisoned") = None;
|
||||
self.write(
|
||||
&self.root.join("revision"),
|
||||
&serde_json::json!(revision.max(self.catalog_revision())),
|
||||
)
|
||||
.map_err(|_| ServiceError {
|
||||
code: "catalog_cache_invalidation_failed",
|
||||
message: "The catalog cache revision could not be saved. Retry the refresh.".into(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn invalidate_key(&self, scope: &Value, key: &Value) {
|
||||
let mut epoch = self.epoch.lock().expect("Store cache poisoned");
|
||||
*epoch = epoch.wrapping_add(1);
|
||||
let _ = fs::remove_file(
|
||||
self.root
|
||||
.join(format!("{}-{}.json", digest(scope), digest(key))),
|
||||
);
|
||||
*self.index.lock().expect("Store index poisoned") = None;
|
||||
}
|
||||
|
||||
pub fn load_with_policy(
|
||||
&self,
|
||||
scope: &Value,
|
||||
key: &Value,
|
||||
refresh: bool,
|
||||
policy: CachePolicy,
|
||||
fetch: impl FnOnce() -> Result<Value, ServiceError>,
|
||||
) -> Result<Value, ServiceError> {
|
||||
crate::requests::check()?;
|
||||
let CachePolicy {
|
||||
ttl_ms,
|
||||
allow_stale,
|
||||
} = policy;
|
||||
let prefix = format!("{}-", digest(scope));
|
||||
let path = self.root.join(format!("{prefix}{}.json", digest(key)));
|
||||
let prior_fetch = if refresh && allow_stale {
|
||||
self.read(&path).map(|value| value["fetchedAt"].clone())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let epoch = {
|
||||
let mut epoch = self.epoch.lock().expect("Store cache poisoned");
|
||||
if refresh && !allow_stale {
|
||||
*self.index.lock().expect("Store index poisoned") = None;
|
||||
*epoch = epoch.wrapping_add(1);
|
||||
// Only this cache's generated, flat response files are targets.
|
||||
if let Ok(entries) = fs::read_dir(&self.root) {
|
||||
for entry in entries.flatten() {
|
||||
let name = entry.file_name().to_string_lossy().into_owned();
|
||||
if name.starts_with(&prefix) && name.ends_with(".json") {
|
||||
let _ = fs::remove_file(entry.path());
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if !refresh && let Some(mut value) = self.read_fresh(&path) {
|
||||
value["cacheHit"] = Value::Bool(true);
|
||||
return Ok(value);
|
||||
}
|
||||
*epoch
|
||||
};
|
||||
let fetch_lock = {
|
||||
let mut fetches = self.fetches.lock().expect("Store fetches poisoned");
|
||||
fetches.retain(|_, lock| lock.strong_count() > 0);
|
||||
let entry = fetches.entry((path.clone(), epoch)).or_default();
|
||||
match entry.upgrade() {
|
||||
Some(lock) => lock,
|
||||
None => {
|
||||
let lock = Arc::new(Mutex::new(()));
|
||||
*entry = Arc::downgrade(&lock);
|
||||
lock
|
||||
}
|
||||
}
|
||||
};
|
||||
let _fetch = crate::store_requests::lock(&fetch_lock)?;
|
||||
if let Some(mut value) = self.read_fresh(&path)
|
||||
&& (!refresh || (allow_stale && prior_fetch.as_ref() != Some(&value["fetchedAt"])))
|
||||
{
|
||||
value["cacheHit"] = Value::Bool(true);
|
||||
return Ok(value);
|
||||
}
|
||||
let mut value = match fetch() {
|
||||
Ok(value) => value,
|
||||
Err(error)
|
||||
if allow_stale
|
||||
&& !matches!(
|
||||
error.code,
|
||||
"cancelled" | "stale_account" | "http_unauthorized"
|
||||
) =>
|
||||
{
|
||||
let mut value = self.read(&path).unwrap_or_else(|| serde_json::json!({}));
|
||||
value["status"] = serde_json::json!(if value["fetchedAt"].is_number() {
|
||||
"stale"
|
||||
} else {
|
||||
"error"
|
||||
});
|
||||
value["freshness"] = value["status"].clone();
|
||||
value["error"] = serde_json::json!({"code":error.code,"message":error.message});
|
||||
return Ok(value);
|
||||
}
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
crate::requests::check()?;
|
||||
value["cacheHit"] = Value::Bool(false);
|
||||
let fetched_at = now_ms();
|
||||
value["fetchedAt"] = serde_json::json!(fetched_at);
|
||||
value["expiresAt"] = serde_json::json!(fetched_at.saturating_add(ttl_ms));
|
||||
value["freshness"] = serde_json::json!("fresh");
|
||||
value["status"] = serde_json::json!("success");
|
||||
value["error"] = Value::Null;
|
||||
let value = crate::store_catalog_page::bounded_result(value)?;
|
||||
let current = self.epoch.lock().expect("Store cache poisoned");
|
||||
if epoch == *current {
|
||||
// Cache failures must not turn a successful catalog fetch into an
|
||||
// unavailable Store (read-only disk, low space, interrupted writes).
|
||||
if self.write(&path, &value).is_err() {
|
||||
eprintln!("store cache: response could not be persisted");
|
||||
}
|
||||
*self.index.lock().expect("Store index poisoned") = None;
|
||||
}
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
pub fn local_query(&self, scope: &Value, params: &Value) -> Result<Value, ServiceError> {
|
||||
crate::requests::check()?;
|
||||
let scope_key = digest(scope);
|
||||
let read_key = |key: &Value| {
|
||||
self.read_fresh(&self.root.join(format!("{scope_key}-{}.json", digest(key))))
|
||||
};
|
||||
let mut cached = self.index.lock().expect("Store index poisoned");
|
||||
if params["refresh"] == true
|
||||
|| cached
|
||||
.as_ref()
|
||||
.is_none_or(|(key, _, expires)| key != &scope_key || *expires <= now_ms())
|
||||
{
|
||||
let mut index = crate::store_index::StoreIndex::default();
|
||||
let mut expires = u64::MAX;
|
||||
let mut cursor = String::new();
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
for _ in 0..100 {
|
||||
crate::requests::check()?;
|
||||
let key = serde_json::json!(["page", 100, cursor, ""]);
|
||||
let Some(page) = read_key(&key) else {
|
||||
break;
|
||||
};
|
||||
expires = expires.min(page["expiresAt"].as_u64().unwrap_or(0));
|
||||
for (at, game) in page["games"].as_array().into_iter().flatten().enumerate() {
|
||||
index.add(game, key.clone(), format!("/games/{at}"), None);
|
||||
}
|
||||
index.pages += 1;
|
||||
if page["hasNextPage"] == false {
|
||||
index.complete = true;
|
||||
break;
|
||||
}
|
||||
cursor = page["nextCursor"].as_str().unwrap_or("").to_owned();
|
||||
index.next_upstream = cursor.clone();
|
||||
if cursor.is_empty() || !seen.insert(cursor.clone()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let key = serde_json::json!(["presentation", "panels"]);
|
||||
if let Some(panels) = read_key(&key) {
|
||||
expires = expires.min(panels["expiresAt"].as_u64().unwrap_or(0));
|
||||
index.add_panels(&panels, &key);
|
||||
}
|
||||
*cached = Some((scope_key.clone(), index, expires));
|
||||
}
|
||||
let (_, index, _) = cached.as_ref().expect("Store index initialized");
|
||||
if index.pages == 0 {
|
||||
return Err(ServiceError {
|
||||
code: "store_cache_missing",
|
||||
message: "No saved Store catalog yet. Load a catalog page first.".into(),
|
||||
});
|
||||
}
|
||||
index.query(params, read_key)
|
||||
}
|
||||
|
||||
fn read(&self, path: &PathBuf) -> Option<Value> {
|
||||
let file = File::open(path).ok()?;
|
||||
if !file.metadata().ok()?.is_file() {
|
||||
return None;
|
||||
}
|
||||
let mut bytes = Vec::new();
|
||||
file.take(RESULT_BUDGET as u64 + 1)
|
||||
.read_to_end(&mut bytes)
|
||||
.ok()?;
|
||||
if bytes.len() > RESULT_BUDGET {
|
||||
return None;
|
||||
}
|
||||
let value: Value = serde_json::from_slice(&bytes).ok()?;
|
||||
// Versioned directory + bounded structural validation before crossing IPC.
|
||||
let page = value["games"].is_array()
|
||||
&& value["hasNextPage"].is_boolean()
|
||||
&& value["nextCursor"].is_string();
|
||||
let section = matches!(
|
||||
value["section"].as_str(),
|
||||
Some("panels" | "marquee" | "filters")
|
||||
) && value["items"].is_array();
|
||||
let metadata = value["metadataKind"].is_string();
|
||||
(page || section || metadata).then_some(value)
|
||||
}
|
||||
|
||||
fn read_fresh(&self, path: &PathBuf) -> Option<Value> {
|
||||
let value = self.read(path)?;
|
||||
(value["expiresAt"].as_u64()? > now_ms()).then_some(value)
|
||||
}
|
||||
|
||||
fn write(&self, path: &PathBuf, value: &Value) -> std::io::Result<()> {
|
||||
fs::create_dir_all(&self.root)?;
|
||||
let bytes = serde_json::to_vec(value)?;
|
||||
let temp = path.with_extension(format!(
|
||||
"{}-{}.tmp",
|
||||
std::process::id(),
|
||||
rand::random::<u64>()
|
||||
));
|
||||
let result = (|| {
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.open(&temp)?;
|
||||
file.write_all(&bytes)?;
|
||||
file.sync_all()?;
|
||||
drop(file);
|
||||
fs::rename(&temp, path)?;
|
||||
self.prune();
|
||||
Ok(())
|
||||
})();
|
||||
if result.is_err() {
|
||||
let _ = fs::remove_file(&temp);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn prune(&self) {
|
||||
let Ok(entries) = fs::read_dir(&self.root) else {
|
||||
return;
|
||||
};
|
||||
let mut files: Vec<_> = entries
|
||||
.flatten()
|
||||
.filter_map(|entry| {
|
||||
let name = entry.file_name().to_string_lossy().into_owned();
|
||||
if !(name.ends_with(".json") || name.ends_with(".tmp")) {
|
||||
return None;
|
||||
}
|
||||
let meta = entry.metadata().ok()?;
|
||||
meta.is_file()
|
||||
.then_some((entry.path(), meta.len(), meta.modified().ok()))
|
||||
})
|
||||
.collect();
|
||||
files.sort_by_key(|entry| entry.2);
|
||||
let mut bytes: u64 = files.iter().map(|entry| entry.1).sum();
|
||||
let mut count = files.len();
|
||||
for (path, size, _) in files {
|
||||
if count <= MAX_ENTRIES && bytes <= MAX_BYTES {
|
||||
break;
|
||||
}
|
||||
if fs::remove_file(path).is_ok() {
|
||||
count -= 1;
|
||||
bytes = bytes.saturating_sub(size);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn now_ms() -> u64 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_millis() as u64
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
fn cache() -> StoreCache {
|
||||
StoreCache::new(
|
||||
std::env::temp_dir().join(format!("opennow-store-test-{}", rand::random::<u64>())),
|
||||
)
|
||||
}
|
||||
fn page(id: &str) -> Value {
|
||||
json!({"games":[{"id":id}],"hasNextPage":false,"nextCursor":""})
|
||||
}
|
||||
#[test]
|
||||
fn restart_reads_disk_without_calling_the_fetcher() {
|
||||
let cache = cache();
|
||||
let scope = json!(["account-a", "provider-a", "direct", "en_US"]);
|
||||
let key = json!(["page", 100, "", ""]);
|
||||
assert_eq!(
|
||||
cache
|
||||
.load_or_fetch(&scope, &key, false, || Ok(page("saved")))
|
||||
.unwrap()["cacheHit"],
|
||||
false
|
||||
);
|
||||
let restarted = StoreCache::new(cache.root.parent().unwrap().to_path_buf());
|
||||
let hit = restarted
|
||||
.load_or_fetch(&scope, &key, false, || panic!("network called on restart"))
|
||||
.unwrap();
|
||||
assert_eq!(hit["games"][0]["id"], "saved");
|
||||
assert_eq!(hit["cacheHit"], true);
|
||||
fs::remove_dir_all(cache.root.parent().unwrap()).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expired_disk_pages_require_network_and_metadata_refreshes_coalesce() {
|
||||
let cache = cache();
|
||||
let scope = json!("scope");
|
||||
let key = json!("page");
|
||||
cache
|
||||
.load_or_fetch(&scope, &key, false, || Ok(page("old")))
|
||||
.unwrap();
|
||||
let path = cache
|
||||
.root
|
||||
.join(format!("{}-{}.json", digest(&scope), digest(&key)));
|
||||
let mut expired = cache.read(&path).unwrap();
|
||||
expired["expiresAt"] = json!(0);
|
||||
cache.write(&path, &expired).unwrap();
|
||||
let refreshed = cache
|
||||
.load_or_fetch(&scope, &key, false, || Ok(page("new")))
|
||||
.unwrap();
|
||||
assert_eq!(refreshed["cacheHit"], false);
|
||||
assert_eq!(refreshed["games"][0]["id"], "new");
|
||||
let calls = std::sync::atomic::AtomicUsize::new(0);
|
||||
let ready = std::sync::Barrier::new(4);
|
||||
std::thread::scope(|threads| {
|
||||
let mut workers = Vec::new();
|
||||
for _ in 0..4 {
|
||||
workers.push(threads.spawn(|| {
|
||||
ready.wait();
|
||||
cache
|
||||
.load_with_policy(
|
||||
&scope,
|
||||
&key,
|
||||
true,
|
||||
CachePolicy {
|
||||
ttl_ms: 1000,
|
||||
allow_stale: true,
|
||||
},
|
||||
|| {
|
||||
calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
||||
std::thread::sleep(std::time::Duration::from_millis(100));
|
||||
Ok(page("coalesced"))
|
||||
},
|
||||
)
|
||||
.unwrap()
|
||||
}));
|
||||
}
|
||||
for worker in workers {
|
||||
assert_eq!(worker.join().unwrap()["games"][0]["id"], "coalesced");
|
||||
}
|
||||
});
|
||||
assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);
|
||||
fs::remove_dir_all(cache.root.parent().unwrap()).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn catalog_invalidation_survives_restart_and_out_of_order_writers() {
|
||||
let cache = cache();
|
||||
cache.invalidate(2).unwrap();
|
||||
cache.invalidate(1).unwrap();
|
||||
let restarted = StoreCache::new(cache.root.parent().unwrap().to_path_buf());
|
||||
assert_eq!(restarted.catalog_revision(), 2);
|
||||
fs::write(cache.root.join("revision"), b"corrupt").unwrap();
|
||||
assert!(restarted.catalog_revision() > 2);
|
||||
fs::remove_dir_all(cache.root.parent().unwrap()).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn absent_revision_starts_in_the_initial_namespace() {
|
||||
let cache = cache();
|
||||
assert_eq!(cache.catalog_revision(), 0);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn unreadable_revision_cannot_reuse_pre_sync_pages_or_erase_reference_metadata() {
|
||||
let cache = cache();
|
||||
let page_key = json!(["page", 100, "", ""]);
|
||||
cache
|
||||
.load_or_fetch(&json!(["account", 0]), &page_key, false, || {
|
||||
Ok(page("pre-sync"))
|
||||
})
|
||||
.unwrap();
|
||||
let reference_scope = json!(["definitions", "account"]);
|
||||
let reference_key = json!(["genres"]);
|
||||
cache
|
||||
.load_or_fetch(&reference_scope, &reference_key, false, || {
|
||||
Ok(json!({"metadataKind":"genres","items":[{"genre":"ACTION","label":"Action"}]}))
|
||||
})
|
||||
.unwrap();
|
||||
cache.invalidate(1).unwrap();
|
||||
let revision_path = cache.root.join("revision");
|
||||
fs::remove_file(&revision_path).unwrap();
|
||||
std::os::unix::fs::symlink("revision", &revision_path).unwrap();
|
||||
assert!(File::open(&revision_path).is_err());
|
||||
|
||||
let restarted = StoreCache::new(cache.root.parent().unwrap().to_path_buf());
|
||||
let revision = restarted.catalog_revision();
|
||||
assert_ne!(revision, 0);
|
||||
let result = restarted
|
||||
.load_or_fetch(&json!(["account", revision]), &page_key, false, || {
|
||||
Ok(page("fresh-network"))
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(result["games"][0]["id"], "fresh-network");
|
||||
assert_eq!(result["cacheHit"], false);
|
||||
let reference = restarted
|
||||
.load_or_fetch(&reference_scope, &reference_key, false, || {
|
||||
panic!("reference metadata was invalidated")
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(reference["items"][0]["genre"], "ACTION");
|
||||
fs::remove_dir_all(cache.root.parent().unwrap()).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn simultaneous_cache_misses_fetch_each_page_only_once() {
|
||||
let cache = cache();
|
||||
let ready = std::sync::Barrier::new(4);
|
||||
let calls = std::sync::atomic::AtomicUsize::new(0);
|
||||
std::thread::scope(|threads| {
|
||||
let mut workers = Vec::new();
|
||||
for _ in 0..4 {
|
||||
workers.push(threads.spawn(|| {
|
||||
ready.wait();
|
||||
cache
|
||||
.load_or_fetch(&json!("a"), &json!("first"), false, || {
|
||||
calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
||||
std::thread::sleep(std::time::Duration::from_millis(100));
|
||||
Ok(page("shared"))
|
||||
})
|
||||
.unwrap()
|
||||
}));
|
||||
}
|
||||
let results: Vec<_> = workers
|
||||
.into_iter()
|
||||
.map(|worker| worker.join().unwrap())
|
||||
.collect();
|
||||
assert_eq!(
|
||||
results
|
||||
.iter()
|
||||
.filter(|value| value["cacheHit"] == false)
|
||||
.count(),
|
||||
1
|
||||
);
|
||||
assert!(
|
||||
results
|
||||
.iter()
|
||||
.all(|value| value["games"][0]["id"] == "shared")
|
||||
);
|
||||
});
|
||||
assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);
|
||||
fs::remove_dir_all(cache.root.parent().unwrap()).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_fetches_leave_saved_pages_available() {
|
||||
let cache = cache();
|
||||
cache
|
||||
.load_or_fetch(&json!("a"), &json!("saved"), false, || Ok(page("saved")))
|
||||
.unwrap();
|
||||
assert!(
|
||||
cache
|
||||
.load_or_fetch(&json!("a"), &json!("missing"), false, || {
|
||||
Err(ServiceError {
|
||||
code: "rate_limited",
|
||||
message: "Try later".into(),
|
||||
})
|
||||
})
|
||||
.is_err()
|
||||
);
|
||||
let result = cache
|
||||
.load_or_fetch(&json!("a"), &json!("saved"), false, || {
|
||||
panic!("cached page fetched again")
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(result["cacheHit"], true);
|
||||
fs::remove_dir_all(cache.root.parent().unwrap()).unwrap();
|
||||
}
|
||||
#[test]
|
||||
fn accounts_providers_queries_and_cursors_do_not_share_results() {
|
||||
let cache = cache();
|
||||
for scope in [
|
||||
json!(["a", "nvidia"]),
|
||||
json!(["b", "nvidia"]),
|
||||
json!(["a", "alliance"]),
|
||||
] {
|
||||
for key in [json!(["", ""]), json!(["next", ""]), json!(["", "search"])] {
|
||||
let result = cache
|
||||
.load_or_fetch(&scope, &key, false, || Ok(page("new")))
|
||||
.unwrap();
|
||||
assert_eq!(result["cacheHit"], false);
|
||||
}
|
||||
}
|
||||
fs::remove_dir_all(cache.root.parent().unwrap()).unwrap();
|
||||
}
|
||||
#[test]
|
||||
fn refresh_invalidates_all_pages_and_chrome_only_for_this_account() {
|
||||
let cache = cache();
|
||||
for scope in [json!("a"), json!("b")] {
|
||||
for key in [json!("first"), json!("next"), json!("chrome")] {
|
||||
cache
|
||||
.load_or_fetch(&scope, &key, false, || Ok(page("old")))
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
cache
|
||||
.load_or_fetch(&json!("a"), &json!("first"), true, || Ok(page("fresh")))
|
||||
.unwrap();
|
||||
for key in [json!("next"), json!("chrome")] {
|
||||
assert_eq!(
|
||||
cache
|
||||
.load_or_fetch(&json!("a"), &key, false, || Ok(page("new")))
|
||||
.unwrap()["cacheHit"],
|
||||
false
|
||||
);
|
||||
assert_eq!(
|
||||
cache
|
||||
.load_or_fetch(&json!("b"), &key, false, || panic!())
|
||||
.unwrap()["cacheHit"],
|
||||
true
|
||||
);
|
||||
}
|
||||
fs::remove_dir_all(cache.root.parent().unwrap()).unwrap();
|
||||
}
|
||||
#[test]
|
||||
fn failures_corruption_and_oversized_files_are_cache_misses() {
|
||||
let cache = cache();
|
||||
let scope = json!("a");
|
||||
let key = json!("first");
|
||||
assert!(
|
||||
cache
|
||||
.load_or_fetch(&scope, &key, false, || Err(ServiceError {
|
||||
code: "network_error",
|
||||
message: "offline".into()
|
||||
}))
|
||||
.is_err()
|
||||
);
|
||||
cache
|
||||
.load_or_fetch(&scope, &key, false, || Ok(page("new")))
|
||||
.unwrap();
|
||||
let path = cache
|
||||
.root
|
||||
.join(format!("{}-{}.json", digest(&scope), digest(&key)));
|
||||
for bytes in [b"broken json".to_vec(), vec![b' '; RESULT_BUDGET + 1]] {
|
||||
fs::write(&path, bytes).unwrap();
|
||||
assert_eq!(
|
||||
cache
|
||||
.load_or_fetch(&scope, &key, false, || Ok(page("recovered")))
|
||||
.unwrap()["cacheHit"],
|
||||
false
|
||||
);
|
||||
}
|
||||
fs::remove_dir_all(cache.root.parent().unwrap()).unwrap();
|
||||
}
|
||||
#[test]
|
||||
fn obsolete_fetch_cannot_repopulate_after_refresh() {
|
||||
let cache = cache();
|
||||
cache
|
||||
.load_or_fetch(&json!("a"), &json!("old"), false, || {
|
||||
cache.load_or_fetch(&json!("a"), &json!("first"), true, || Ok(page("fresh")))?;
|
||||
Ok(page("obsolete"))
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
cache
|
||||
.load_or_fetch(&json!("a"), &json!("old"), false, || Ok(page("new")))
|
||||
.unwrap()["cacheHit"],
|
||||
false
|
||||
);
|
||||
fs::remove_dir_all(cache.root.parent().unwrap()).unwrap();
|
||||
}
|
||||
#[test]
|
||||
fn disk_entry_count_is_bounded() {
|
||||
let cache = cache();
|
||||
fs::create_dir_all(&cache.root).unwrap();
|
||||
for index in 0..MAX_ENTRIES + 3 {
|
||||
fs::write(cache.root.join(format!("{index}.json")), b"{}").unwrap();
|
||||
}
|
||||
cache.prune();
|
||||
assert_eq!(fs::read_dir(&cache.root).unwrap().count(), MAX_ENTRIES);
|
||||
fs::remove_dir_all(cache.root.parent().unwrap()).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,246 @@
|
||||
//! One Store RPC contains one complete upstream page, never an entire catalog.
|
||||
use crate::gfn::ServiceError;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
// Reserve ample room for the protocol envelope within the unchanged 1 MiB limit.
|
||||
pub const RESULT_BUDGET: usize = 768 * 1024;
|
||||
const MAX_CURSOR_BYTES: usize = 4096;
|
||||
|
||||
pub struct PageRequest {
|
||||
pub limit: usize,
|
||||
pub cursor: String,
|
||||
pub search: String,
|
||||
}
|
||||
|
||||
impl PageRequest {
|
||||
pub fn parse(params: &Value) -> Result<Self, ServiceError> {
|
||||
let text = |key: &str, max: usize| -> Result<String, ServiceError> {
|
||||
let value = match params.get(key) {
|
||||
None => "",
|
||||
Some(Value::String(value)) => value,
|
||||
_ => {
|
||||
return Err(error(
|
||||
"invalid_params",
|
||||
"Store cursor and search must be strings",
|
||||
));
|
||||
}
|
||||
};
|
||||
if value.len() > max {
|
||||
return Err(error(
|
||||
"invalid_params",
|
||||
"Store cursor or search exceeds its size limit",
|
||||
));
|
||||
}
|
||||
Ok(value.to_owned())
|
||||
};
|
||||
Ok(Self {
|
||||
limit: params["limit"].as_u64().unwrap_or(100).clamp(1, 100) as usize,
|
||||
cursor: text("cursor", MAX_CURSOR_BYTES)?,
|
||||
search: text("searchQuery", 512)?.trim().to_owned(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn error(code: &'static str, message: &str) -> ServiceError {
|
||||
ServiceError {
|
||||
code,
|
||||
message: message.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
fn encoded_size(value: &Value) -> Result<usize, ServiceError> {
|
||||
serde_json::to_vec(value)
|
||||
.map(|bytes| bytes.len())
|
||||
.map_err(|_| {
|
||||
error(
|
||||
"invalid_upstream_response",
|
||||
"Store result could not be encoded",
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn bounded_result(value: Value) -> Result<Value, ServiceError> {
|
||||
if encoded_size(&value)? > RESULT_BUDGET {
|
||||
return Err(error(
|
||||
"catalog_response_too_large",
|
||||
"Store response exceeds the safe page size",
|
||||
));
|
||||
}
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
pub fn fetch_bounded_page(
|
||||
limit: usize,
|
||||
mut fetch: impl FnMut(usize) -> Result<Value, ServiceError>,
|
||||
) -> Result<Value, ServiceError> {
|
||||
let mut count = limit.clamp(1, 100);
|
||||
loop {
|
||||
crate::requests::check()?;
|
||||
let page = match fetch(count) {
|
||||
Err(error) if error.code == "catalog_response_too_large" && count > 1 => {
|
||||
count = (count / 2).max(1);
|
||||
continue;
|
||||
}
|
||||
result => result?,
|
||||
};
|
||||
crate::requests::check()?;
|
||||
if encoded_size(&page)? <= RESULT_BUDGET {
|
||||
return Ok(page);
|
||||
}
|
||||
if count == 1 {
|
||||
return Err(error(
|
||||
"catalog_response_too_large",
|
||||
"A Store game exceeds the safe page size",
|
||||
));
|
||||
}
|
||||
count = (count / 2).max(1);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn page_result(
|
||||
cursor: &str,
|
||||
games: Vec<Value>,
|
||||
info: &Value,
|
||||
fetched_at: u64,
|
||||
) -> Result<Value, ServiceError> {
|
||||
let has_more = info["hasNextPage"].as_bool().ok_or_else(|| {
|
||||
error(
|
||||
"invalid_upstream_response",
|
||||
"Store response has no pagination state",
|
||||
)
|
||||
})?;
|
||||
let next = info["endCursor"].as_str().unwrap_or("");
|
||||
if has_more && (next.is_empty() || next == cursor || next.len() > MAX_CURSOR_BYTES) {
|
||||
return Err(error(
|
||||
"invalid_upstream_response",
|
||||
"Store pagination did not advance",
|
||||
));
|
||||
}
|
||||
Ok(json!({
|
||||
"count":games.len(), "totalCount":info["totalCount"].as_u64(),
|
||||
"games":games, "hasNextPage":has_more, "nextCursor":if has_more {next} else {""},
|
||||
"source":"store-browse", "fetchedAt":fetched_at
|
||||
}))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn normal_sized_pages_keep_the_full_hundred_game_batch() {
|
||||
let request = PageRequest::parse(&json!({"limit": 100})).unwrap();
|
||||
let mut calls = 0;
|
||||
let page = fetch_bounded_page(request.limit, |count| {
|
||||
calls += 1;
|
||||
assert_eq!(count, 100);
|
||||
page_result(
|
||||
"",
|
||||
(0..count).map(|id| json!({"id": id})).collect(),
|
||||
&json!({"hasNextPage": true, "endCursor": "after-100", "totalCount": 2500}),
|
||||
0,
|
||||
)
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(calls, 1);
|
||||
assert_eq!(page["count"], 100);
|
||||
assert_eq!(page["nextCursor"], "after-100");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cancelled_fetch_does_not_retry_an_oversized_page() {
|
||||
let requests = std::sync::Arc::new(crate::requests::Requests::default());
|
||||
let permit = requests.admit("page", "catalog.store.browse").unwrap();
|
||||
let mut calls = 0;
|
||||
let result = crate::requests::scope(permit.token.clone(), || {
|
||||
fetch_bounded_page(100, |_| {
|
||||
calls += 1;
|
||||
requests.cancel("page");
|
||||
Ok(json!({"items":["x".repeat(RESULT_BUDGET)]}))
|
||||
})
|
||||
});
|
||||
assert_eq!(result.unwrap_err().code, "cancelled");
|
||||
assert_eq!(calls, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn page_requests_are_bounded_and_preserve_opaque_cursors() {
|
||||
let request = PageRequest::parse(
|
||||
&json!({"limit":2500,"cursor":"opaque+/=cursor","searchQuery":" game "}),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(request.limit, 100);
|
||||
assert_eq!(request.cursor, "opaque+/=cursor");
|
||||
assert_eq!(request.search, "game");
|
||||
assert!(PageRequest::parse(&json!({"cursor":"x".repeat(4097)})).is_err());
|
||||
assert!(PageRequest::parse(&json!({"searchQuery":5})).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oversized_pages_refetch_smaller_without_skipping_items() {
|
||||
let mut requested = Vec::new();
|
||||
let page = fetch_bounded_page(100, |count| {
|
||||
requested.push(count);
|
||||
let games = (0..count).map(|i| json!({"id":i,"title":"界\"".repeat(4000)})).collect();
|
||||
page_result("start", games, &json!({"hasNextPage":true,"endCursor":format!("offset-{count}"),"totalCount":2500}), 0)
|
||||
}).unwrap();
|
||||
assert!(requested.len() > 1);
|
||||
let count = *requested.last().unwrap();
|
||||
assert_eq!(page["count"], count);
|
||||
assert_eq!(page["nextCursor"], format!("offset-{count}"));
|
||||
let framed = json!({"type":"response","id":"999999","ok":true,"result":page});
|
||||
assert!(serde_json::to_vec(&framed).unwrap().len() < 1024 * 1024);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unpageable_records_and_chrome_return_scoped_errors() {
|
||||
let huge = json!({"items":["x".repeat(RESULT_BUDGET)]});
|
||||
assert_eq!(
|
||||
bounded_result(huge.clone()).unwrap_err().code,
|
||||
"catalog_response_too_large"
|
||||
);
|
||||
let mut attempts = 0;
|
||||
let result = fetch_bounded_page(100, |_| {
|
||||
attempts += 1;
|
||||
Ok(huge.clone())
|
||||
});
|
||||
assert_eq!(result.unwrap_err().code, "catalog_response_too_large");
|
||||
assert_eq!(attempts, 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_final_pages_and_invalid_continuations_are_distinct() {
|
||||
let end = page_result(
|
||||
"last",
|
||||
vec![],
|
||||
&json!({"hasNextPage":false,"totalCount":12}),
|
||||
0,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(end["hasNextPage"], false);
|
||||
assert_eq!(end["nextCursor"], "");
|
||||
for next in ["", "last"] {
|
||||
assert!(
|
||||
page_result(
|
||||
"last",
|
||||
vec![],
|
||||
&json!({"hasNextPage":true,"endCursor":next}),
|
||||
0
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
assert!(page_result("last", vec![], &json!({}), 0).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn network_errors_do_not_trigger_size_retries() {
|
||||
let mut attempts = 0;
|
||||
let result = fetch_bounded_page(100, |_| {
|
||||
attempts += 1;
|
||||
Err(error("upstream_error", "HTTP 503"))
|
||||
});
|
||||
assert_eq!(result.unwrap_err().code, "upstream_error");
|
||||
assert_eq!(attempts, 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,469 @@
|
||||
//! Compact local title/facet index. Full game records stay in the page cache;
|
||||
//! only the selected page is materialized for Qt or the command palette.
|
||||
use crate::{gfn::ServiceError, store_catalog_page};
|
||||
use serde_json::{Value, json};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::collections::{BTreeSet, HashMap, HashSet};
|
||||
|
||||
pub struct Entry {
|
||||
id: String,
|
||||
title: String,
|
||||
words: Vec<String>,
|
||||
acronym: String,
|
||||
owned: bool,
|
||||
genres: Vec<String>,
|
||||
stores: Vec<String>,
|
||||
categories: HashSet<String>,
|
||||
key: Value,
|
||||
pointer: String,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct StoreIndex {
|
||||
entries: Vec<Entry>,
|
||||
identities: HashMap<String, usize>,
|
||||
pub categories: Vec<Value>,
|
||||
pub complete: bool,
|
||||
pub next_upstream: String,
|
||||
pub pages: usize,
|
||||
}
|
||||
|
||||
fn normalized(text: &str) -> String {
|
||||
text.to_lowercase()
|
||||
.chars()
|
||||
.map(|c| if c.is_alphanumeric() { c } else { ' ' })
|
||||
.collect::<String>()
|
||||
.split_whitespace()
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
}
|
||||
fn strings(value: &Value) -> Vec<String> {
|
||||
value
|
||||
.as_array()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter_map(Value::as_str)
|
||||
.map(str::to_owned)
|
||||
.collect()
|
||||
}
|
||||
fn error(message: &str) -> ServiceError {
|
||||
ServiceError {
|
||||
code: "invalid_params",
|
||||
message: message.into(),
|
||||
}
|
||||
}
|
||||
|
||||
impl StoreIndex {
|
||||
pub fn add(&mut self, game: &Value, key: Value, pointer: String, category: Option<&str>) {
|
||||
let id = game["id"].as_str().unwrap_or("");
|
||||
if id.is_empty() {
|
||||
return;
|
||||
}
|
||||
if let Some(&at) = self.identities.get(id) {
|
||||
if let Some(category) = category {
|
||||
self.entries[at].categories.insert(category.into());
|
||||
}
|
||||
return;
|
||||
}
|
||||
if self.entries.len() >= 12000 {
|
||||
return;
|
||||
}
|
||||
let title = normalized(game["title"].as_str().unwrap_or(""));
|
||||
let words: Vec<String> = title.split_whitespace().map(str::to_owned).collect();
|
||||
let acronym = words
|
||||
.iter()
|
||||
.filter_map(|word| word.chars().next())
|
||||
.collect();
|
||||
let mut categories: HashSet<String> =
|
||||
strings(&game["storeCategories"]).into_iter().collect();
|
||||
if let Some(category) = category {
|
||||
categories.insert(category.into());
|
||||
}
|
||||
if game["freeToPlay"] == true
|
||||
|| game["storeFree"] == true
|
||||
|| game["playType"]
|
||||
.as_str()
|
||||
.unwrap_or("")
|
||||
.to_uppercase()
|
||||
.contains("FREE")
|
||||
{
|
||||
categories.insert("free".into());
|
||||
}
|
||||
let state = normalized(game["playabilityState"].as_str().unwrap_or(""));
|
||||
if state.contains("coming") || state.contains("unreleased") {
|
||||
categories.insert("coming".into());
|
||||
}
|
||||
if strings(&game["nvidiaTech"])
|
||||
.iter()
|
||||
.any(|tech| tech.to_lowercase().contains("rtx"))
|
||||
{
|
||||
categories.insert("rtx".into());
|
||||
}
|
||||
let mut stores = strings(&game["availableStores"]);
|
||||
for variant in game["variants"].as_array().into_iter().flatten() {
|
||||
if let Some(store) = variant["store"].as_str() {
|
||||
if !stores.iter().any(|s| s == store) {
|
||||
stores.push(store.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
self.identities.insert(id.into(), self.entries.len());
|
||||
self.entries.push(Entry {
|
||||
id: id.into(),
|
||||
title,
|
||||
words,
|
||||
acronym,
|
||||
owned: game["isInLibrary"] == true || game["storeOwned"] == true,
|
||||
genres: strings(&game["genres"]),
|
||||
stores,
|
||||
categories,
|
||||
key,
|
||||
pointer,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn add_panels(&mut self, panels: &Value, key: &Value) {
|
||||
for (p, panel) in panels["items"].as_array().into_iter().flatten().enumerate() {
|
||||
for (s, section) in panel["sections"]
|
||||
.as_array()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.enumerate()
|
||||
{
|
||||
let category = format!("shelf:{p}:{s}");
|
||||
let title = section["title"].as_str().unwrap_or("Games");
|
||||
let lower = title.to_lowercase();
|
||||
let alias = if lower.contains("thursday")
|
||||
|| lower.contains("new")
|
||||
|| lower.contains("recent")
|
||||
{
|
||||
Some("new")
|
||||
} else if lower.contains("rtx") {
|
||||
Some("rtx")
|
||||
} else if lower.contains("free") {
|
||||
Some("free")
|
||||
} else if lower.contains("coming") {
|
||||
Some("coming")
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let games = section["games"]
|
||||
.as_array()
|
||||
.map(Vec::as_slice)
|
||||
.unwrap_or(&[]);
|
||||
for (g, game) in games.iter().enumerate() {
|
||||
let pointer = format!("/items/{p}/sections/{s}/games/{g}");
|
||||
self.add(game, key.clone(), pointer.clone(), Some(&category));
|
||||
if let Some(alias) = alias {
|
||||
self.add(game, key.clone(), pointer, Some(alias));
|
||||
}
|
||||
}
|
||||
if !games.is_empty() {
|
||||
self.categories
|
||||
.push(json!({"id":category,"label":title,"count":games.len()}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn query(
|
||||
&self,
|
||||
params: &Value,
|
||||
mut read: impl FnMut(&Value) -> Option<Value>,
|
||||
) -> Result<Value, ServiceError> {
|
||||
let page = store_catalog_page::PageRequest::parse(params)?;
|
||||
let query = normalized(&page.search);
|
||||
let tokens: Vec<_> = query.split_whitespace().take(8).collect();
|
||||
let filter = |name: &str| -> Result<String, ServiceError> {
|
||||
match params.get(name) {
|
||||
None => Ok(String::new()),
|
||||
Some(Value::String(s)) if s.len() <= 256 => Ok(s.to_owned()),
|
||||
_ => Err(error("Invalid local Store filter")),
|
||||
}
|
||||
};
|
||||
let genre = filter("genre")?;
|
||||
let store = filter("store")?;
|
||||
let category = filter("categoryId")?;
|
||||
let fingerprint = format!(
|
||||
"{:x}",
|
||||
Sha256::digest(
|
||||
json!([query, genre, store, category])
|
||||
.to_string()
|
||||
.as_bytes()
|
||||
)
|
||||
);
|
||||
let prefix = format!("local:{}:", &fingerprint[..16]);
|
||||
let offset = if page.cursor.is_empty() {
|
||||
0
|
||||
} else {
|
||||
page.cursor
|
||||
.strip_prefix(&prefix)
|
||||
.and_then(|s| s.split(':').next())
|
||||
.and_then(|s| s.parse::<usize>().ok())
|
||||
.filter(|n| *n <= 12000)
|
||||
.ok_or_else(|| error("Local Store cursor does not match its search and filters"))?
|
||||
};
|
||||
let mut matches: Vec<_> = self
|
||||
.entries
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(at, entry)| {
|
||||
if !genre.is_empty() && !entry.genres.iter().any(|g| g.eq_ignore_ascii_case(&genre))
|
||||
{
|
||||
return None;
|
||||
}
|
||||
if !store.is_empty() && !entry.stores.iter().any(|s| s.eq_ignore_ascii_case(&store))
|
||||
{
|
||||
return None;
|
||||
}
|
||||
if !category.is_empty()
|
||||
&& category != "featured"
|
||||
&& category != "all"
|
||||
&& !entry.categories.contains(&category)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
score(entry, &query, &tokens).map(|score| (at, score))
|
||||
})
|
||||
.collect();
|
||||
if !query.is_empty() {
|
||||
matches.sort_by_key(|&(at, score)| (std::cmp::Reverse(score), at));
|
||||
}
|
||||
let genres: BTreeSet<_> = self
|
||||
.entries
|
||||
.iter()
|
||||
.flat_map(|e| e.genres.iter().cloned())
|
||||
.collect();
|
||||
let stores: BTreeSet<_> = self
|
||||
.entries
|
||||
.iter()
|
||||
.flat_map(|e| e.stores.iter().cloned())
|
||||
.collect();
|
||||
let facets = json!({"genres":genres,"stores":stores,"categories":self.categories});
|
||||
let mut loaded = HashMap::new();
|
||||
let selected: Vec<Value> = matches
|
||||
.iter()
|
||||
.skip(offset)
|
||||
.take(page.limit.min(60))
|
||||
.map(|&(at, _)| {
|
||||
let entry = &self.entries[at];
|
||||
let key = entry.key.to_string();
|
||||
let document = loaded.entry(key).or_insert_with(|| read(&entry.key));
|
||||
document
|
||||
.as_ref()
|
||||
.and_then(|doc| doc.pointer(&entry.pointer))
|
||||
.filter(|game| game["id"] == entry.id)
|
||||
.cloned()
|
||||
.ok_or_else(|| ServiceError {
|
||||
code: "store_cache_changed",
|
||||
message: "Saved Store data changed. Refresh the results.".into(),
|
||||
})
|
||||
})
|
||||
.collect::<Result<_, _>>()?;
|
||||
store_catalog_page::fetch_bounded_page(page.limit.min(60), |limit| {
|
||||
let count = selected.len().min(limit);
|
||||
let next = offset + count;
|
||||
let more = next < matches.len() || !self.complete;
|
||||
Ok(
|
||||
json!({"games":&selected[..count],"count":count,"totalCount":matches.len(),"catalogTotalCount":self.entries.len(),
|
||||
"hasNextPage":more,"nextCursor":if more {format!("{prefix}{next}:{}",self.pages)} else {String::new()},
|
||||
"source":"store-local","cacheHit":true,"cacheComplete":self.complete,
|
||||
"localHasNextPage":next < matches.len(),
|
||||
"upstreamCoverage":if self.complete {"complete-traversal"} else {"partial"},
|
||||
"facetsSource":"local-cache",
|
||||
"upstreamCursor":self.next_upstream,
|
||||
"facets":if offset == 0 {facets.clone()} else {Value::Null}}),
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn score(entry: &Entry, query: &str, tokens: &[&str]) -> Option<u32> {
|
||||
if query.is_empty() {
|
||||
return Some(0);
|
||||
}
|
||||
if entry.title == query {
|
||||
return Some(12000 + if entry.owned { 300 } else { 0 });
|
||||
}
|
||||
if entry.acronym == query.replace(' ', "") {
|
||||
return Some(11000 + if entry.owned { 300 } else { 0 });
|
||||
}
|
||||
let mut score = if entry.title.starts_with(query) {
|
||||
9000
|
||||
} else if entry.title.contains(query) {
|
||||
7000
|
||||
} else {
|
||||
4000
|
||||
};
|
||||
for token in tokens {
|
||||
let best = entry
|
||||
.words
|
||||
.iter()
|
||||
.filter_map(|word| {
|
||||
if word == token {
|
||||
Some(300)
|
||||
} else if word.starts_with(token) {
|
||||
Some(200)
|
||||
} else if token.len() >= 3 && word.contains(token) {
|
||||
Some(100)
|
||||
} else if token.len() >= 4 && one_typo(word, token) {
|
||||
Some(30)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.max()?;
|
||||
score += best;
|
||||
}
|
||||
Some(score + if entry.owned { 300 } else { 0 })
|
||||
}
|
||||
|
||||
// Bounded edit-distance-one matcher, including adjacent transpositions. It
|
||||
// tolerates common typing errors without broad substring/subsequence noise.
|
||||
fn one_typo(a: &str, b: &str) -> bool {
|
||||
let a: Vec<_> = a.chars().take(64).collect();
|
||||
let b: Vec<_> = b.chars().take(64).collect();
|
||||
if a.len().abs_diff(b.len()) > 1 {
|
||||
return false;
|
||||
}
|
||||
let first = a
|
||||
.iter()
|
||||
.zip(&b)
|
||||
.position(|(a, b)| a != b)
|
||||
.unwrap_or(a.len().min(b.len()));
|
||||
if first == a.len().min(b.len()) {
|
||||
return true;
|
||||
}
|
||||
if a.len() == b.len() {
|
||||
a[first + 1..] == b[first + 1..]
|
||||
|| (first + 1 < a.len()
|
||||
&& a[first] == b[first + 1]
|
||||
&& a[first + 1] == b[first]
|
||||
&& a[first + 2..] == b[first + 2..])
|
||||
} else if a.len() > b.len() {
|
||||
a[first + 1..] == b[first..]
|
||||
} else {
|
||||
a[first..] == b[first + 1..]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
fn fixture() -> (StoreIndex, Value) {
|
||||
let games = json!({"games":[{"id":"1","title":"Counter-Strike 2","genres":["ACTION"],"availableStores":["STEAM"]},
|
||||
{"id":"2","title":"Fortnite","genres":["ACTION"],"availableStores":["EPIC"]},
|
||||
{"id":"3","title":"Fortnite Festival","genres":["MUSIC"],"availableStores":["EPIC"]}]});
|
||||
let mut index = StoreIndex {
|
||||
complete: true,
|
||||
..StoreIndex::default()
|
||||
};
|
||||
for (i, g) in games["games"].as_array().unwrap().iter().enumerate() {
|
||||
index.add(g, json!("page"), format!("/games/{i}"), None);
|
||||
}
|
||||
(index, games)
|
||||
}
|
||||
#[test]
|
||||
fn ranked_search_handles_titles_acronyms_word_order_and_typos() {
|
||||
let (index, games) = fixture();
|
||||
for (query, id) in [
|
||||
("fortnite", "2"),
|
||||
("fortn", "2"),
|
||||
("fortntie", "2"),
|
||||
("cs2", "1"),
|
||||
("2 counter", "1"),
|
||||
] {
|
||||
let result = index
|
||||
.query(&json!({"searchQuery":query}), |_| Some(games.clone()))
|
||||
.unwrap();
|
||||
assert_eq!(result["games"][0]["id"], id, "{query}");
|
||||
}
|
||||
assert_eq!(
|
||||
index
|
||||
.query(&json!({"searchQuery":"zzzz"}), |_| panic!())
|
||||
.unwrap()["count"],
|
||||
0
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn paging_is_bounded_and_facets_cover_unloaded_games() {
|
||||
let (index, games) = fixture();
|
||||
let first = index
|
||||
.query(&json!({"limit":1}), |_| Some(games.clone()))
|
||||
.unwrap();
|
||||
assert_eq!(first["count"], 1);
|
||||
assert_eq!(first["facets"]["genres"].as_array().unwrap().len(), 2);
|
||||
let next = index
|
||||
.query(&json!({"limit":1,"cursor":first["nextCursor"]}), |_| {
|
||||
Some(games.clone())
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(next["games"][0]["id"], "2");
|
||||
assert!(
|
||||
index
|
||||
.query(
|
||||
&json!({"searchQuery":"other","cursor":first["nextCursor"]}),
|
||||
|_| Some(games.clone())
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(
|
||||
index
|
||||
.query(&json!({"store":"EPIC","genre":"MUSIC"}), |_| Some(
|
||||
games.clone()
|
||||
))
|
||||
.unwrap()["games"][0]["id"],
|
||||
"3"
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn only_selected_pages_are_read() {
|
||||
let (index, games) = fixture();
|
||||
let mut reads = 0;
|
||||
index
|
||||
.query(&json!({"limit":2}), |_| {
|
||||
reads += 1;
|
||||
Some(games.clone())
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(reads, 1);
|
||||
assert!(!one_typo("fortnite", "fzzzzzzz"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn panel_categories_include_games_outside_the_first_page() {
|
||||
let (mut index, games) = fixture();
|
||||
let panels = json!({"items":[{"sections":[{"title":"GFN Thursday",
|
||||
"games":[games["games"][2].clone(), {"id":"4","title":"Coming Game"}]}]}]});
|
||||
index.add_panels(&panels, &json!("panels"));
|
||||
let result = index
|
||||
.query(&json!({"categoryId":"shelf:0:0"}), |key| {
|
||||
Some(if key == "panels" {
|
||||
panels.clone()
|
||||
} else {
|
||||
games.clone()
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(result["count"], 2);
|
||||
assert_eq!(result["games"][1]["id"], "4");
|
||||
assert_eq!(result["facets"]["categories"][0]["label"], "GFN Thursday");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn owned_game_breaks_ambiguous_acronym_ties() {
|
||||
let games = json!({"games":[{"id":"1","title":"Clouds & Sheep 2"},
|
||||
{"id":"2","title":"Counter-Strike 2","isInLibrary":true}]});
|
||||
let mut index = StoreIndex {
|
||||
complete: true,
|
||||
..StoreIndex::default()
|
||||
};
|
||||
for (i, game) in games["games"].as_array().unwrap().iter().enumerate() {
|
||||
index.add(game, json!("page"), format!("/games/{i}"), None);
|
||||
}
|
||||
let result = index
|
||||
.query(&json!({"searchQuery":"cs2"}), |_| Some(games.clone()))
|
||||
.unwrap();
|
||||
assert_eq!(result["games"][0]["id"], "2");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
use crate::gfn::ServiceError;
|
||||
use reqwest::blocking::{RequestBuilder, Response};
|
||||
use std::sync::{Mutex, MutexGuard, TryLockError};
|
||||
use std::time::{Duration, Instant, SystemTime};
|
||||
|
||||
const REQUEST_INTERVAL: Duration = Duration::from_millis(50);
|
||||
const DEFAULT_COOLDOWN: Duration = Duration::from_secs(60);
|
||||
|
||||
pub fn lock<T>(mutex: &Mutex<T>) -> Result<MutexGuard<'_, T>, ServiceError> {
|
||||
loop {
|
||||
crate::requests::check()?;
|
||||
match mutex.try_lock() {
|
||||
Ok(guard) => return Ok(guard),
|
||||
Err(TryLockError::WouldBlock) => std::thread::sleep(REQUEST_INTERVAL),
|
||||
Err(TryLockError::Poisoned(_)) => panic!("Store request state poisoned"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Schedule {
|
||||
next_request: Option<Instant>,
|
||||
cooldown: Option<Instant>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct StoreRequests(Mutex<Schedule>);
|
||||
|
||||
fn rate_limited(remaining: Duration) -> ServiceError {
|
||||
ServiceError {
|
||||
code: "rate_limited",
|
||||
message: format!(
|
||||
"The Store is rate limited. Try again in {} seconds.",
|
||||
remaining.as_secs().saturating_add(1)
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
impl StoreRequests {
|
||||
pub fn send(&self, request: RequestBuilder, context: &str) -> Result<Response, ServiceError> {
|
||||
self.send_mapped(request, |error| ServiceError::network(context, error))
|
||||
}
|
||||
|
||||
pub fn send_mapped(
|
||||
&self,
|
||||
request: RequestBuilder,
|
||||
network: impl FnOnce(reqwest::Error) -> ServiceError,
|
||||
) -> Result<Response, ServiceError> {
|
||||
let mut schedule = lock(&self.0)?;
|
||||
if let Some(remaining) = schedule
|
||||
.cooldown
|
||||
.and_then(|until| until.checked_duration_since(Instant::now()))
|
||||
{
|
||||
return Err(rate_limited(remaining));
|
||||
}
|
||||
while let Some(remaining) = schedule
|
||||
.next_request
|
||||
.and_then(|until| until.checked_duration_since(Instant::now()))
|
||||
{
|
||||
crate::requests::check()?;
|
||||
drop(schedule);
|
||||
std::thread::sleep(remaining.min(REQUEST_INTERVAL));
|
||||
schedule = lock(&self.0)?;
|
||||
}
|
||||
if let Some(remaining) = schedule
|
||||
.cooldown
|
||||
.and_then(|until| until.checked_duration_since(Instant::now()))
|
||||
{
|
||||
return Err(rate_limited(remaining));
|
||||
}
|
||||
crate::requests::check()?;
|
||||
schedule.next_request = Some(Instant::now() + REQUEST_INTERVAL);
|
||||
drop(schedule);
|
||||
let response = request.send();
|
||||
let mut schedule = lock(&self.0)?;
|
||||
schedule.next_request = Some(Instant::now() + REQUEST_INTERVAL);
|
||||
let response = response.map_err(network)?;
|
||||
if response.status() == reqwest::StatusCode::TOO_MANY_REQUESTS {
|
||||
let delay = response
|
||||
.headers()
|
||||
.get(reqwest::header::RETRY_AFTER)
|
||||
.and_then(|value| value.to_str().ok())
|
||||
.and_then(|value| {
|
||||
value
|
||||
.parse::<u64>()
|
||||
.ok()
|
||||
.map(Duration::from_secs)
|
||||
.or_else(|| {
|
||||
httpdate::parse_http_date(value)
|
||||
.ok()
|
||||
.and_then(|date| date.duration_since(SystemTime::now()).ok())
|
||||
})
|
||||
})
|
||||
.unwrap_or(DEFAULT_COOLDOWN)
|
||||
.max(REQUEST_INTERVAL);
|
||||
let now = Instant::now();
|
||||
schedule.cooldown = Some(now.checked_add(delay).unwrap_or(now + DEFAULT_COOLDOWN));
|
||||
return Err(rate_limited(delay));
|
||||
}
|
||||
Ok(response)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
use std::net::TcpListener;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[test]
|
||||
fn in_flight_network_does_not_hold_the_scheduler_lock() {
|
||||
let (entered_tx, entered_rx) = std::sync::mpsc::channel();
|
||||
let (release_tx, release_rx) = std::sync::mpsc::channel();
|
||||
let (url, worker) =
|
||||
crate::gfn::tests::mock_requests(vec![(200, serde_json::json!({}))], move |_, _| {
|
||||
entered_tx.send(()).unwrap();
|
||||
release_rx.recv_timeout(Duration::from_secs(5)).unwrap();
|
||||
});
|
||||
let requests = StoreRequests::default();
|
||||
std::thread::scope(|threads| {
|
||||
let pending = threads
|
||||
.spawn(|| requests.send(reqwest::blocking::Client::new().get(url), "fixture"));
|
||||
entered_rx.recv_timeout(Duration::from_secs(5)).unwrap();
|
||||
let unlocked = requests.0.try_lock().is_ok();
|
||||
release_tx.send(()).unwrap();
|
||||
assert!(unlocked);
|
||||
assert!(pending.join().unwrap().is_ok());
|
||||
});
|
||||
worker.join().unwrap();
|
||||
}
|
||||
|
||||
fn server(responses: Vec<String>) -> (String, std::thread::JoinHandle<Vec<Instant>>) {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let url = format!("http://{}", listener.local_addr().unwrap());
|
||||
let worker = std::thread::spawn(move || {
|
||||
let mut arrivals = Vec::new();
|
||||
for response in responses {
|
||||
let (mut stream, _) = listener.accept().unwrap();
|
||||
stream
|
||||
.set_read_timeout(Some(Duration::from_secs(5)))
|
||||
.unwrap();
|
||||
let mut reader = BufReader::new(&mut stream);
|
||||
let mut request = Vec::new();
|
||||
while !request.ends_with(b"\r\n\r\n") {
|
||||
assert!(reader.read_until(b'\n', &mut request).unwrap() > 0);
|
||||
assert!(request.len() <= 4096);
|
||||
}
|
||||
arrivals.push(Instant::now());
|
||||
stream.write_all(response.as_bytes()).unwrap();
|
||||
}
|
||||
arrivals
|
||||
});
|
||||
(url, worker)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn concurrent_store_requests_are_paced() {
|
||||
let (url, server) = server(
|
||||
vec!["HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".into(); 4],
|
||||
);
|
||||
let requests = Arc::new(StoreRequests::default());
|
||||
let client = reqwest::blocking::Client::builder()
|
||||
.no_proxy()
|
||||
.build()
|
||||
.unwrap();
|
||||
std::thread::scope(|scope| {
|
||||
for _ in 0..4 {
|
||||
scope.spawn(|| requests.send(client.get(&url), "test").unwrap());
|
||||
}
|
||||
});
|
||||
let arrivals = server.join().unwrap();
|
||||
assert!(
|
||||
arrivals
|
||||
.windows(2)
|
||||
.all(|pair| pair[1].duration_since(pair[0]) >= REQUEST_INTERVAL)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rate_limits_stop_subsequent_requests_including_retries() {
|
||||
for header in [
|
||||
"Retry-After: 120\r\n".to_owned(),
|
||||
format!(
|
||||
"Retry-After: {}\r\n",
|
||||
httpdate::fmt_http_date(SystemTime::now() + Duration::from_secs(120))
|
||||
),
|
||||
"Retry-After: invalid\r\n".to_owned(),
|
||||
String::new(),
|
||||
] {
|
||||
let (url, server) = server(vec![format!(
|
||||
"HTTP/1.1 429 Too Many Requests\r\n{header}Content-Length: 0\r\nConnection: close\r\n\r\n"
|
||||
)]);
|
||||
let requests = StoreRequests::default();
|
||||
let client = reqwest::blocking::Client::builder()
|
||||
.no_proxy()
|
||||
.build()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
requests.send(client.get(&url), "test").unwrap_err().code,
|
||||
"rate_limited"
|
||||
);
|
||||
server.join().unwrap();
|
||||
for _ in 0..100 {
|
||||
assert_eq!(
|
||||
requests.send(client.get(&url), "test").unwrap_err().code,
|
||||
"rate_limited"
|
||||
);
|
||||
}
|
||||
let remaining = requests
|
||||
.0
|
||||
.lock()
|
||||
.unwrap()
|
||||
.cooldown
|
||||
.unwrap()
|
||||
.duration_since(Instant::now());
|
||||
assert!(
|
||||
remaining
|
||||
>= Duration::from_secs(if header.contains("120") || header.contains("GMT") {
|
||||
118
|
||||
} else {
|
||||
58
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cooldown_expires_and_cancelled_waiters_never_send() {
|
||||
let (url, server) = server(vec![
|
||||
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".into(),
|
||||
]);
|
||||
let requests = StoreRequests::default();
|
||||
requests.0.lock().unwrap().cooldown = Some(Instant::now() - Duration::from_secs(1));
|
||||
let client = reqwest::blocking::Client::builder()
|
||||
.no_proxy()
|
||||
.build()
|
||||
.unwrap();
|
||||
requests.send(client.get(&url), "test").unwrap();
|
||||
server.join().unwrap();
|
||||
let active = Arc::new(crate::requests::Requests::default());
|
||||
let permit = active.admit("cancelled", "catalog.store.local").unwrap();
|
||||
active.cancel("cancelled");
|
||||
let _held = requests.0.lock().unwrap();
|
||||
crate::requests::scope(permit.token.clone(), || {
|
||||
assert_eq!(
|
||||
requests.send(client.get(&url), "test").unwrap_err().code,
|
||||
"cancelled"
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
+4313
File diff suppressed because it is too large.
Load diff
+278
@@ -0,0 +1,278 @@
|
||||
use regex::Regex;
|
||||
use reqwest::blocking::Client;
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Value, json};
|
||||
use std::collections::HashSet;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use url::Url;
|
||||
|
||||
const CONTRIBUTORS_URL: &str =
|
||||
"https://api.github.com/repos/OpenCloudGaming/OpenNOW/contributors?per_page=100";
|
||||
const SUPPORTERS_URL: &str = "https://github.com/sponsors/zortos293";
|
||||
const MAXIMUM_RESPONSE_BYTES: u64 = 4 * 1024 * 1024;
|
||||
|
||||
pub struct ThanksService {
|
||||
client: Client,
|
||||
}
|
||||
|
||||
impl ThanksService {
|
||||
pub fn new() -> Result<Self, String> {
|
||||
Ok(Self {
|
||||
client: Client::builder()
|
||||
.connect_timeout(Duration::from_secs(8))
|
||||
.timeout(Duration::from_secs(8))
|
||||
.pool_idle_timeout(Duration::from_secs(30))
|
||||
.build()
|
||||
.map_err(|error| error.to_string())?,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn data(&self) -> Value {
|
||||
let (contributors, supporters) = thread::scope(|scope| {
|
||||
let contributors = scope.spawn(|| self.contributors());
|
||||
let supporters = scope.spawn(|| self.supporters());
|
||||
(
|
||||
contributors
|
||||
.join()
|
||||
.unwrap_or_else(|_| Err("Contributor request failed".to_owned())),
|
||||
supporters
|
||||
.join()
|
||||
.unwrap_or_else(|_| Err("Supporter request failed".to_owned())),
|
||||
)
|
||||
});
|
||||
let mut result = json!({
|
||||
"contributors":contributors.as_ref().cloned().unwrap_or_default(),
|
||||
"supporters":supporters.as_ref().cloned().unwrap_or_else(|_| custom_supporters())
|
||||
});
|
||||
if let Err(error) = contributors {
|
||||
result["contributorsError"] = json!(error);
|
||||
}
|
||||
if let Err(error) = supporters {
|
||||
result["supportersError"] = json!(error);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn contributors(&self) -> Result<Vec<Value>, String> {
|
||||
let response = self
|
||||
.client
|
||||
.get(CONTRIBUTORS_URL)
|
||||
.header("Accept", "application/vnd.github+json")
|
||||
.header("User-Agent", "OpenNOW-DesktopClient")
|
||||
.send()
|
||||
.map_err(|_| "Unable to load contributors right now".to_owned())?;
|
||||
if !response.status().is_success()
|
||||
|| response.content_length().unwrap_or(0) > MAXIMUM_RESPONSE_BYTES
|
||||
{
|
||||
return Err(format!(
|
||||
"GitHub contributors request failed ({})",
|
||||
response.status().as_u16()
|
||||
));
|
||||
}
|
||||
let mut values = response
|
||||
.json::<Vec<Contributor>>()
|
||||
.map_err(|_| "GitHub contributors response was invalid".to_owned())?
|
||||
.into_iter()
|
||||
.filter_map(normalize_contributor)
|
||||
.collect::<Vec<_>>();
|
||||
values.sort_by(|left, right| {
|
||||
right["contributions"]
|
||||
.as_u64()
|
||||
.cmp(&left["contributions"].as_u64())
|
||||
.then_with(|| left["login"].as_str().cmp(&right["login"].as_str()))
|
||||
});
|
||||
Ok(values)
|
||||
}
|
||||
|
||||
fn supporters(&self) -> Result<Vec<Value>, String> {
|
||||
let response = self
|
||||
.client
|
||||
.get(SUPPORTERS_URL)
|
||||
.header("Accept", "text/html,application/xhtml+xml")
|
||||
.header("User-Agent", "OpenNOW-DesktopClient")
|
||||
.send()
|
||||
.map_err(|_| "Unable to load supporters right now".to_owned())?;
|
||||
if !response.status().is_success()
|
||||
|| response.content_length().unwrap_or(0) > MAXIMUM_RESPONSE_BYTES
|
||||
{
|
||||
return Err(format!(
|
||||
"GitHub sponsors request failed ({})",
|
||||
response.status().as_u16()
|
||||
));
|
||||
}
|
||||
let html = response
|
||||
.text()
|
||||
.map_err(|_| "GitHub sponsors response was invalid".to_owned())?;
|
||||
let mut output = custom_supporters();
|
||||
output.extend(parse_supporters(&html));
|
||||
let mut seen = HashSet::new();
|
||||
output.retain(|value| {
|
||||
let key = value["profileUrl"]
|
||||
.as_str()
|
||||
.map(|value| format!("profile:{}", value.to_ascii_lowercase()))
|
||||
.unwrap_or_else(|| {
|
||||
format!(
|
||||
"name:{}|private:{}",
|
||||
value["name"]
|
||||
.as_str()
|
||||
.unwrap_or_default()
|
||||
.to_ascii_lowercase(),
|
||||
value["isPrivate"].as_bool().unwrap_or(false)
|
||||
)
|
||||
});
|
||||
seen.insert(key)
|
||||
});
|
||||
if output.is_empty() {
|
||||
return Err("No public supporters were found on GitHub Sponsors".to_owned());
|
||||
}
|
||||
Ok(output)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Contributor {
|
||||
login: Option<String>,
|
||||
avatar_url: Option<String>,
|
||||
html_url: Option<String>,
|
||||
contributions: Option<u64>,
|
||||
#[serde(rename = "type")]
|
||||
kind: Option<String>,
|
||||
}
|
||||
|
||||
fn normalize_contributor(value: Contributor) -> Option<Value> {
|
||||
let login = value.login?.trim().to_owned();
|
||||
let avatar = trusted_github_url(&value.avatar_url?)?;
|
||||
let profile = trusted_github_url(&value.html_url?)?;
|
||||
let excluded = login.ends_with("[bot]")
|
||||
|| value.kind.as_deref() == Some("Bot")
|
||||
|| ["copilot", "claude", "cappy"]
|
||||
.iter()
|
||||
.any(|needle| login.to_ascii_lowercase().contains(needle));
|
||||
if login.is_empty() || excluded {
|
||||
return None;
|
||||
}
|
||||
Some(json!({
|
||||
"login":login,
|
||||
"avatarUrl":avatar,
|
||||
"profileUrl":profile,
|
||||
"contributions":value.contributions.unwrap_or(0)
|
||||
}))
|
||||
}
|
||||
|
||||
fn parse_supporters(html: &str) -> Vec<Value> {
|
||||
let entry = Regex::new(r#"(?is)<div class="d-flex mb-1 mr-1"[^>]*>(.*?)</div>"#)
|
||||
.expect("supporter entry regex");
|
||||
let image = Regex::new(r#"(?is)<img[^>]+(?:alt|aria-label)="([^"]*)"[^>]*src="([^"]+)""#)
|
||||
.expect("supporter image regex");
|
||||
let href = Regex::new(r#"(?is)<a[^>]+href="([^"]+)""#).expect("supporter href regex");
|
||||
let mut values = Vec::new();
|
||||
for capture in entry.captures_iter(html).take(100) {
|
||||
let body = capture
|
||||
.get(1)
|
||||
.map(|value| value.as_str())
|
||||
.unwrap_or_default();
|
||||
let private = body.to_ascii_lowercase().contains("private sponsor");
|
||||
let image_capture = image.captures(body);
|
||||
let name = if private {
|
||||
"Private".to_owned()
|
||||
} else {
|
||||
image_capture
|
||||
.as_ref()
|
||||
.and_then(|capture| capture.get(1))
|
||||
.map(|value| {
|
||||
decode_html(value.as_str())
|
||||
.trim_start_matches('@')
|
||||
.trim()
|
||||
.to_owned()
|
||||
})
|
||||
.filter(|value| !value.is_empty())
|
||||
.unwrap_or_else(|| "Private".to_owned())
|
||||
};
|
||||
let avatar = image_capture
|
||||
.as_ref()
|
||||
.and_then(|capture| capture.get(2))
|
||||
.and_then(|value| normalize_github_url(value.as_str()));
|
||||
let profile = (!private)
|
||||
.then(|| href.captures(body))
|
||||
.flatten()
|
||||
.and_then(|capture| capture.get(1))
|
||||
.and_then(|value| normalize_github_url(value.as_str()));
|
||||
values.push(json!({
|
||||
"name":name,
|
||||
"avatarUrl":avatar,
|
||||
"profileUrl":profile,
|
||||
"isPrivate":private || profile.is_none(),
|
||||
"source":if private { "private" } else { "github" }
|
||||
}));
|
||||
}
|
||||
values
|
||||
}
|
||||
|
||||
fn custom_supporters() -> Vec<Value> {
|
||||
vec![json!({
|
||||
"name":"DarkevilPT",
|
||||
"avatarUrl":"https://github.com/DarkevilPT.png?size=96",
|
||||
"profileUrl":"https://github.com/DarkevilPT",
|
||||
"isPrivate":false,
|
||||
"source":"custom"
|
||||
})]
|
||||
}
|
||||
|
||||
fn normalize_github_url(value: &str) -> Option<String> {
|
||||
let value = decode_html(value.trim());
|
||||
let absolute = if value.starts_with("//") {
|
||||
format!("https:{value}")
|
||||
} else if value.starts_with('/') {
|
||||
format!("https://github.com{value}")
|
||||
} else {
|
||||
value
|
||||
};
|
||||
trusted_github_url(&absolute)
|
||||
}
|
||||
|
||||
fn trusted_github_url(value: &str) -> Option<String> {
|
||||
let parsed = Url::parse(value).ok()?;
|
||||
let host = parsed.host_str()?.to_ascii_lowercase();
|
||||
(parsed.scheme() == "https"
|
||||
&& matches!(
|
||||
host.as_str(),
|
||||
"github.com" | "avatars.githubusercontent.com" | "user-images.githubusercontent.com"
|
||||
))
|
||||
.then(|| parsed.to_string())
|
||||
}
|
||||
|
||||
fn decode_html(value: &str) -> String {
|
||||
value
|
||||
.replace("&", "&")
|
||||
.replace(""", "\"")
|
||||
.replace("'", "'")
|
||||
.replace("<", "<")
|
||||
.replace(">", ">")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn contributor_and_supporter_inputs_are_bounded_and_trusted() {
|
||||
assert!(
|
||||
normalize_contributor(Contributor {
|
||||
login: Some("dependabot[bot]".to_owned()),
|
||||
avatar_url: Some("https://avatars.githubusercontent.com/u/1".to_owned()),
|
||||
html_url: Some("https://github.com/apps/dependabot".to_owned()),
|
||||
contributions: Some(3),
|
||||
kind: Some("Bot".to_owned()),
|
||||
})
|
||||
.is_none()
|
||||
);
|
||||
assert!(trusted_github_url("https://evil.example/avatar.png").is_none());
|
||||
let supporters = parse_supporters(
|
||||
r#"<div class="d-flex mb-1 mr-1"><a href="/player"><img alt="@player" src="https://avatars.githubusercontent.com/u/2"></a></div>"#,
|
||||
);
|
||||
assert_eq!(supporters.len(), 1);
|
||||
assert_eq!(supporters[0]["name"], "player");
|
||||
assert_eq!(supporters[0]["profileUrl"], "https://github.com/player");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
use std::collections::HashSet;
|
||||
use std::fs::{self, File, OpenOptions};
|
||||
use std::io::{Read, Write};
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
|
||||
const MAX_ENTRIES: usize = 100_000;
|
||||
const MAX_EXPANDED_BYTES: u64 = 12 * 1024 * 1024 * 1024;
|
||||
|
||||
pub(super) fn extract_zip(
|
||||
package: &Path,
|
||||
destination: &Path,
|
||||
allow_links: bool,
|
||||
) -> Result<(), String> {
|
||||
fs::create_dir(destination).map_err(|error| error.to_string())?;
|
||||
let result = extract(package, destination, allow_links);
|
||||
if result.is_err() {
|
||||
let _ = fs::remove_dir_all(destination);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn extract(package: &Path, destination: &Path, allow_links: bool) -> Result<(), String> {
|
||||
let mut archive = zip::ZipArchive::new(File::open(package).map_err(|error| error.to_string())?)
|
||||
.map_err(|error| format!("Invalid update ZIP: {error}"))?;
|
||||
if archive.len() > MAX_ENTRIES {
|
||||
return Err("Update archive has too many entries".to_owned());
|
||||
}
|
||||
let mut paths = HashSet::new();
|
||||
let mut expanded = 0u64;
|
||||
let mut links = Vec::new();
|
||||
for index in 0..archive.len() {
|
||||
let mut entry = archive.by_index(index).map_err(|error| error.to_string())?;
|
||||
let relative = archive_path(entry.name())?;
|
||||
if !paths.insert(relative.to_string_lossy().to_lowercase()) {
|
||||
return Err("Update archive contains duplicate paths".to_owned());
|
||||
}
|
||||
expanded = expanded
|
||||
.checked_add(entry.size())
|
||||
.ok_or("Update archive size overflow")?;
|
||||
if expanded > MAX_EXPANDED_BYTES || entry.size() > super::verification::MAXIMUM_UPDATE_BYTES
|
||||
{
|
||||
return Err("Expanded update exceeds its size limit".to_owned());
|
||||
}
|
||||
let mode = entry.unix_mode().unwrap_or(0o100644);
|
||||
let file_type = mode & 0o170000;
|
||||
let output = destination.join(&relative);
|
||||
if file_type == 0o120000 {
|
||||
if !allow_links || entry.size() > 4096 {
|
||||
return Err("Unsupported update archive symlink".to_owned());
|
||||
}
|
||||
let mut target = String::new();
|
||||
entry
|
||||
.by_ref()
|
||||
.take(4097)
|
||||
.read_to_string(&mut target)
|
||||
.map_err(|error| error.to_string())?;
|
||||
validate_link(&relative, &target)?;
|
||||
links.push((output, PathBuf::from(target)));
|
||||
continue;
|
||||
}
|
||||
if !matches!(file_type, 0 | 0o040000 | 0o100000) {
|
||||
return Err("Update archive contains a special file".to_owned());
|
||||
}
|
||||
if entry.is_dir() {
|
||||
fs::create_dir_all(&output).map_err(|error| error.to_string())?;
|
||||
continue;
|
||||
}
|
||||
fs::create_dir_all(output.parent().ok_or("Invalid archive path")?)
|
||||
.map_err(|error| error.to_string())?;
|
||||
let expected = entry.size();
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.open(&output)
|
||||
.map_err(|error| error.to_string())?;
|
||||
let copied = std::io::copy(&mut entry.by_ref().take(expected + 1), &mut file)
|
||||
.map_err(|error| error.to_string())?;
|
||||
if copied != expected {
|
||||
return Err("Update ZIP entry has an invalid expanded size".to_owned());
|
||||
}
|
||||
file.flush()
|
||||
.and_then(|_| file.sync_all())
|
||||
.map_err(|error| error.to_string())?;
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
fs::set_permissions(
|
||||
&output,
|
||||
fs::Permissions::from_mode(if mode & 0o111 != 0 { 0o755 } else { 0o644 }),
|
||||
)
|
||||
.map_err(|error| error.to_string())?;
|
||||
}
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
if !links.is_empty() {
|
||||
return Err("Update symlinks are unsupported on this platform".to_owned());
|
||||
}
|
||||
#[cfg(unix)]
|
||||
for (path, target) in &links {
|
||||
fs::create_dir_all(path.parent().ok_or("Invalid symlink path")?)
|
||||
.map_err(|error| error.to_string())?;
|
||||
std::os::unix::fs::symlink(target, path).map_err(|error| error.to_string())?;
|
||||
}
|
||||
let root = fs::canonicalize(destination).map_err(|error| error.to_string())?;
|
||||
for (path, _) in links {
|
||||
if !fs::canonicalize(path)
|
||||
.map_err(|error| format!("Invalid update symlink: {error}"))?
|
||||
.starts_with(&root)
|
||||
{
|
||||
return Err("Update symlink escapes the package".to_owned());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn archive_path(value: &str) -> Result<PathBuf, String> {
|
||||
if value.is_empty() || value.contains(['\\', ':', '\0']) || value.starts_with('/') {
|
||||
return Err("Unsafe update archive path".to_owned());
|
||||
}
|
||||
let value = value.trim_end_matches('/');
|
||||
for part in value.split('/') {
|
||||
let stem = part
|
||||
.split('.')
|
||||
.next()
|
||||
.unwrap_or_default()
|
||||
.to_ascii_uppercase();
|
||||
if part.is_empty()
|
||||
|| part == "."
|
||||
|| part == ".."
|
||||
|| part.ends_with(['.', ' '])
|
||||
|| matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL")
|
||||
|| (stem.len() == 4
|
||||
&& (stem.starts_with("COM") || stem.starts_with("LPT"))
|
||||
&& stem.as_bytes()[3].is_ascii_digit())
|
||||
{
|
||||
return Err("Unsafe update archive component".to_owned());
|
||||
}
|
||||
}
|
||||
let path = PathBuf::from(value);
|
||||
if !path
|
||||
.components()
|
||||
.all(|part| matches!(part, Component::Normal(_)))
|
||||
{
|
||||
return Err("Unsafe update archive path".to_owned());
|
||||
}
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
fn validate_link(path: &Path, target: &str) -> Result<(), String> {
|
||||
if target.is_empty() || target.contains(['\\', ':', '\0']) || Path::new(target).is_absolute() {
|
||||
return Err("Unsafe update symlink target".to_owned());
|
||||
}
|
||||
let mut depth = path
|
||||
.parent()
|
||||
.map(|parent| parent.components().count())
|
||||
.unwrap_or(0);
|
||||
for part in Path::new(target).components() {
|
||||
match part {
|
||||
Component::ParentDir => {
|
||||
depth = depth
|
||||
.checked_sub(1)
|
||||
.ok_or("Update symlink escapes the package")?;
|
||||
}
|
||||
Component::Normal(_) => depth += 1,
|
||||
Component::CurDir => (),
|
||||
_ => return Err("Unsafe update symlink target".to_owned()),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
use std::path::Path;
|
||||
|
||||
pub(super) const MAX_COPY_ENTRIES: u64 = 100_000;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub(super) enum CopyPolicy {
|
||||
Bundle,
|
||||
UserData,
|
||||
}
|
||||
|
||||
pub(super) struct CopyBudget {
|
||||
remaining_entries: u64,
|
||||
remaining_bytes: u64,
|
||||
}
|
||||
|
||||
impl CopyBudget {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
remaining_entries: MAX_COPY_ENTRIES,
|
||||
remaining_bytes: 12 * 1024 * 1024 * 1024,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn with_limits(entries: u64, bytes: u64) -> Self {
|
||||
Self {
|
||||
remaining_entries: entries,
|
||||
remaining_bytes: bytes,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn consume_entry(&mut self) -> Result<(), String> {
|
||||
self.remaining_entries = self
|
||||
.remaining_entries
|
||||
.checked_sub(1)
|
||||
.ok_or("Preserved data exceeds its aggregate entry limit")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn copy_file(
|
||||
&mut self,
|
||||
source: &Path,
|
||||
destination: &Path,
|
||||
policy: CopyPolicy,
|
||||
) -> Result<(), String> {
|
||||
let copied = super::copy_synced_bounded(
|
||||
source,
|
||||
destination,
|
||||
self.remaining_bytes
|
||||
.min(super::verification::MAXIMUM_UPDATE_BYTES),
|
||||
policy,
|
||||
)?;
|
||||
self.remaining_bytes -= copied;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn run(command: &mut std::process::Command) -> Result<(), String> {
|
||||
use std::process::Stdio;
|
||||
use std::time::{Duration, Instant};
|
||||
let mut child = command
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
.map_err(|error| error.to_string())?;
|
||||
let start = Instant::now();
|
||||
loop {
|
||||
if let Some(status) = child.try_wait().map_err(|error| error.to_string())? {
|
||||
return if status.success() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("macOS package operation failed".to_owned())
|
||||
};
|
||||
}
|
||||
if start.elapsed() >= Duration::from_secs(120) {
|
||||
let _ = child.kill();
|
||||
let _ = child.wait();
|
||||
return Err("macOS package operation exceeded its deadline".to_owned());
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn extract_dmg(package: &Path, destination: &Path) -> Result<(), String> {
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
use std::process::Command;
|
||||
let mount = destination.with_extension("mount");
|
||||
std::fs::create_dir(&mount).map_err(|error| error.to_string())?;
|
||||
let attached = run(Command::new("/usr/bin/hdiutil")
|
||||
.args([
|
||||
"attach",
|
||||
"-readonly",
|
||||
"-nobrowse",
|
||||
"-noautoopen",
|
||||
"-mountpoint",
|
||||
])
|
||||
.arg(&mount)
|
||||
.arg(package));
|
||||
let result = attached.and_then(|_| {
|
||||
let apps: Vec<_> = std::fs::read_dir(&mount)
|
||||
.map_err(|error| error.to_string())?
|
||||
.filter_map(|entry| entry.ok().map(|entry| entry.path()))
|
||||
.filter(|path| {
|
||||
path.extension().is_some_and(|value| value == "app") && path.is_dir()
|
||||
})
|
||||
.collect();
|
||||
if apps.len() != 1 || apps[0].file_name().is_none_or(|name| name != "OpenNOW.app") {
|
||||
return Err("DMG must contain exactly one OpenNOW.app bundle".to_owned());
|
||||
}
|
||||
std::fs::create_dir(destination).map_err(|error| error.to_string())?;
|
||||
copy_tree(
|
||||
&apps[0],
|
||||
&destination.join("OpenNOW.app"),
|
||||
&mut CopyBudget::new(),
|
||||
CopyPolicy::Bundle,
|
||||
)
|
||||
});
|
||||
let detached =
|
||||
run(Command::new("/usr/bin/hdiutil").arg("detach").arg(&mount)).or_else(|_| {
|
||||
run(Command::new("/usr/bin/hdiutil")
|
||||
.args(["detach", "-force"])
|
||||
.arg(&mount))
|
||||
});
|
||||
if detached.is_ok() {
|
||||
let _ = std::fs::remove_dir(&mount);
|
||||
}
|
||||
result.and(detached)
|
||||
}
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
{
|
||||
let _ = (package, destination);
|
||||
Err("DMG updates require macOS".to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn copy_tree(
|
||||
source: &Path,
|
||||
destination: &Path,
|
||||
budget: &mut CopyBudget,
|
||||
policy: CopyPolicy,
|
||||
) -> Result<(), String> {
|
||||
use std::fs;
|
||||
budget.consume_entry()?;
|
||||
let source = fs::canonicalize(source).map_err(|error| error.to_string())?;
|
||||
if policy == CopyPolicy::UserData {
|
||||
super::security::create_private_directory(destination)?;
|
||||
} else {
|
||||
fs::create_dir(destination).map_err(|error| error.to_string())?;
|
||||
}
|
||||
let result = (|| {
|
||||
let mut pending = vec![(source.clone(), destination.to_path_buf())];
|
||||
let mut links = Vec::new();
|
||||
let mut directory_permissions = Vec::new();
|
||||
while let Some((from, to)) = pending.pop() {
|
||||
if policy == CopyPolicy::UserData {
|
||||
directory_permissions.push((from.clone(), to.clone()));
|
||||
}
|
||||
for entry in fs::read_dir(&from).map_err(|error| error.to_string())? {
|
||||
let entry = entry.map_err(|error| error.to_string())?;
|
||||
budget.consume_entry()?;
|
||||
let metadata =
|
||||
fs::symlink_metadata(entry.path()).map_err(|error| error.to_string())?;
|
||||
let path = to.join(entry.file_name());
|
||||
if metadata.is_symlink() {
|
||||
let resolved =
|
||||
fs::canonicalize(entry.path()).map_err(|error| error.to_string())?;
|
||||
if !resolved.starts_with(&source) {
|
||||
return Err("Application bundle symlink escapes its root".to_owned());
|
||||
}
|
||||
let target = fs::read_link(entry.path()).map_err(|error| error.to_string())?;
|
||||
if target.is_absolute() {
|
||||
return Err("Application bundle contains an absolute symlink".to_owned());
|
||||
}
|
||||
links.push((path, target));
|
||||
} else if metadata.is_dir() {
|
||||
if policy == CopyPolicy::UserData {
|
||||
super::security::create_private_directory(&path)?;
|
||||
} else {
|
||||
fs::create_dir(&path).map_err(|error| error.to_string())?;
|
||||
}
|
||||
pending.push((entry.path(), path));
|
||||
} else if metadata.is_file() {
|
||||
budget.copy_file(&entry.path(), &path, policy)?;
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
if policy == CopyPolicy::Bundle {
|
||||
fs::set_permissions(
|
||||
&path,
|
||||
fs::Permissions::from_mode(
|
||||
if metadata.permissions().mode() & 0o111 != 0 {
|
||||
0o755
|
||||
} else {
|
||||
0o644
|
||||
},
|
||||
),
|
||||
)
|
||||
.map_err(|error| error.to_string())?;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err("Application bundle contains a special file".to_owned());
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
if !links.is_empty() {
|
||||
return Err("Bundle symlinks require macOS or Unix".to_owned());
|
||||
}
|
||||
#[cfg(unix)]
|
||||
for (path, target) in links {
|
||||
std::os::unix::fs::symlink(&target, &path).map_err(|error| error.to_string())?;
|
||||
}
|
||||
for (source, destination) in directory_permissions.into_iter().rev() {
|
||||
super::security::preserve_permissions(&source, &destination)?;
|
||||
}
|
||||
Ok(())
|
||||
})();
|
||||
if result.is_err() {
|
||||
let _ = fs::remove_dir_all(destination);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
pub(super) fn verify_bundle(bundle: &Path, installed_bundle: &Path) -> Result<(), String> {
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
use std::process::Command;
|
||||
run(Command::new("/usr/bin/codesign")
|
||||
.args(["--verify", "--deep", "--strict"])
|
||||
.arg(bundle))?;
|
||||
let identity = |path: &Path| -> Result<String, String> {
|
||||
let output = super::command_output(
|
||||
Command::new("/usr/bin/codesign")
|
||||
.args(["-dv", "--verbose=4"])
|
||||
.arg(path),
|
||||
std::time::Duration::from_secs(120),
|
||||
)?;
|
||||
if !output.status.success() || output.stderr.len() > 64 * 1024 {
|
||||
return Err("Cannot read application signing identity".to_owned());
|
||||
}
|
||||
let value = String::from_utf8_lossy(&output.stderr);
|
||||
Ok(value
|
||||
.lines()
|
||||
.filter(|line| {
|
||||
line.starts_with("TeamIdentifier=") || line.starts_with("Identifier=")
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n"))
|
||||
};
|
||||
let old = identity(installed_bundle)?;
|
||||
if old.is_empty() || old != identity(bundle)? {
|
||||
return Err(
|
||||
"Updated app bundle signing identity does not match the installation".to_owned(),
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
{
|
||||
let _ = (bundle, installed_bundle);
|
||||
Err("macOS bundle verification requires macOS".to_owned())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,490 @@
|
||||
use super::*;
|
||||
use base64::Engine as _;
|
||||
use ed25519_dalek::{Signer, SigningKey};
|
||||
use sha2::{Digest, Sha256};
|
||||
use tempfile::TempDir;
|
||||
|
||||
struct OwnedChild(Child);
|
||||
|
||||
impl Drop for OwnedChild {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.0.kill();
|
||||
let _ = self.0.wait();
|
||||
}
|
||||
}
|
||||
|
||||
struct Fixture {
|
||||
_temporary: TempDir,
|
||||
plan: Plan,
|
||||
directory: PathBuf,
|
||||
original: Vec<u8>,
|
||||
candidate: Vec<u8>,
|
||||
application: OwnedChild,
|
||||
core: OwnedChild,
|
||||
}
|
||||
|
||||
impl Drop for Fixture {
|
||||
fn drop(&mut self) {
|
||||
let _ = fs::write(self.directory.join("exit-fixture"), b"exit");
|
||||
if let Ok(ack) = read_json::<Acknowledgement>(&self.directory.join("ack.json"), 64 * 1024) {
|
||||
let start = Instant::now();
|
||||
while ack.application.is_running().unwrap_or(false)
|
||||
&& start.elapsed() < Duration::from_secs(10)
|
||||
{
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
}
|
||||
let _ = self.application.0.kill();
|
||||
let _ = self.application.0.wait();
|
||||
let _ = self.core.0.kill();
|
||||
let _ = self.core.0.wait();
|
||||
}
|
||||
}
|
||||
|
||||
fn fixture_path(name: &str) -> PathBuf {
|
||||
fs::canonicalize(std::env::var_os(name).unwrap_or_else(|| {
|
||||
panic!("Run opennow-qt/tests/run_update_helper_integration.py; missing {name}")
|
||||
}))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn command_ok(command: &mut Command) {
|
||||
let output = command.output().unwrap();
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
}
|
||||
|
||||
fn sign(package: &Path) {
|
||||
let seed: [u8; 32] = fs::read(fixture_path("OPENNOW_TEST_UPDATE_SEED_FILE"))
|
||||
.unwrap()
|
||||
.try_into()
|
||||
.unwrap();
|
||||
let key = SigningKey::from_bytes(&seed);
|
||||
assert_eq!(
|
||||
verification::embedded_update_key().unwrap(),
|
||||
key.verifying_key(),
|
||||
"Test key must be compiled into the helper; runtime key overrides are forbidden"
|
||||
);
|
||||
let bytes = fs::read(package).unwrap();
|
||||
let mut manifest = verification::UpdateManifest {
|
||||
schema_version: 1,
|
||||
version: "1.2.3".to_owned(),
|
||||
asset: package.file_name().unwrap().to_str().unwrap().to_owned(),
|
||||
size: bytes.len() as u64,
|
||||
sha256: format!("{:x}", Sha256::digest(&bytes)),
|
||||
signature: String::new(),
|
||||
};
|
||||
manifest.signature = base64::engine::general_purpose::STANDARD.encode(
|
||||
key.sign(verification::signature_payload(&manifest).as_bytes())
|
||||
.to_bytes(),
|
||||
);
|
||||
atomic_json(&manifest_path(package).unwrap(), &manifest).unwrap();
|
||||
}
|
||||
|
||||
fn stage_executable(source: &Path, target: &Path) {
|
||||
fs::create_dir_all(target.parent().unwrap()).unwrap();
|
||||
fs::copy(source, target).unwrap();
|
||||
make_executable(target).unwrap();
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn sign_bundle(root: &Path, identifier: &str, version: &str) {
|
||||
fs::write(root.join("Contents/Info.plist"), format!(r#"<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict><key>CFBundleIdentifier</key><string>{identifier}</string><key>CFBundleExecutable</key><string>OpenNOW</string><key>CFBundlePackageType</key><string>APPL</string><key>CFBundleShortVersionString</key><string>{version}</string><key>CFBundleVersion</key><string>{version}</string></dict></plist>"#)).unwrap();
|
||||
command_ok(
|
||||
Command::new("/usr/bin/codesign")
|
||||
.args([
|
||||
"--force",
|
||||
"--deep",
|
||||
"--sign",
|
||||
"-",
|
||||
"--identifier",
|
||||
identifier,
|
||||
])
|
||||
.arg(root),
|
||||
);
|
||||
command_ok(
|
||||
Command::new("/usr/bin/codesign")
|
||||
.args(["--verify", "--deep", "--strict"])
|
||||
.arg(root),
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn zip_tree(root: &Path, package: &Path) {
|
||||
let mut zip = zip::ZipWriter::new(File::create(package).unwrap());
|
||||
let mut pending = vec![root.to_path_buf()];
|
||||
while let Some(directory) = pending.pop() {
|
||||
for entry in fs::read_dir(directory).unwrap() {
|
||||
let path = entry.unwrap().path();
|
||||
if path.is_dir() {
|
||||
pending.push(path);
|
||||
continue;
|
||||
}
|
||||
let name = path
|
||||
.strip_prefix(root)
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.replace('\\', "/");
|
||||
zip.start_file(
|
||||
name,
|
||||
zip::write::SimpleFileOptions::default()
|
||||
.compression_method(zip::CompressionMethod::Stored),
|
||||
)
|
||||
.unwrap();
|
||||
zip.write_all(&fs::read(path).unwrap()).unwrap();
|
||||
}
|
||||
}
|
||||
zip.finish().unwrap();
|
||||
}
|
||||
|
||||
fn fixture(wrong_identity: bool) -> Fixture {
|
||||
let temporary = TempDir::new().unwrap();
|
||||
let root = fs::canonicalize(temporary.path()).unwrap();
|
||||
let directory = root.join(".opennow-update-integration");
|
||||
fs::create_dir(&directory).unwrap();
|
||||
let previous = fixture_path("OPENNOW_TEST_UPDATE_PREVIOUS");
|
||||
let candidate = fixture_path("OPENNOW_TEST_UPDATE_CANDIDATE");
|
||||
#[cfg(any(windows, target_os = "macos"))]
|
||||
let helper = fixture_path("OPENNOW_TEST_UPDATE_HELPER");
|
||||
#[cfg(target_os = "linux")]
|
||||
let (kind, target, application, package, candidate_bytes) = {
|
||||
let target = root.join("OpenNOW.AppImage");
|
||||
stage_executable(&previous, &target);
|
||||
let package = directory.join("OpenNOW-1.2.3.AppImage");
|
||||
let mut bytes = fs::read(&candidate).unwrap();
|
||||
assert_eq!(&bytes[..4], b"\x7fELF");
|
||||
bytes[8..11].copy_from_slice(b"AI\x02");
|
||||
fs::write(&package, &bytes).unwrap();
|
||||
(
|
||||
InstallKind::AppImage,
|
||||
target.clone(),
|
||||
target,
|
||||
package,
|
||||
bytes,
|
||||
)
|
||||
};
|
||||
#[cfg(any(windows, target_os = "macos"))]
|
||||
let (kind, target, application, package, candidate_bytes) = {
|
||||
#[cfg(windows)]
|
||||
let (kind, target, relative, package) = (
|
||||
InstallKind::WindowsPortable,
|
||||
root.join("installed"),
|
||||
PathBuf::from("bin/OpenNOW.exe"),
|
||||
directory.join("OpenNOW-1.2.3.zip"),
|
||||
);
|
||||
#[cfg(target_os = "macos")]
|
||||
let (kind, target, relative, package) = (
|
||||
InstallKind::MacBundle,
|
||||
root.join("OpenNOW.app"),
|
||||
PathBuf::from("Contents/MacOS/OpenNOW"),
|
||||
directory.join("OpenNOW-1.2.3.dmg"),
|
||||
);
|
||||
let application = target.join(&relative);
|
||||
stage_executable(&previous, &application);
|
||||
let image_root = root.join("image-root");
|
||||
#[cfg(target_os = "macos")]
|
||||
let payload = image_root.join("OpenNOW.app");
|
||||
#[cfg(windows)]
|
||||
let payload = image_root.clone();
|
||||
let payload_application = payload.join(&relative);
|
||||
stage_executable(&candidate, &payload_application);
|
||||
let suffix = if cfg!(windows) { ".exe" } else { "" };
|
||||
for (name, source) in [
|
||||
(format!("opennow-core{suffix}"), &previous),
|
||||
(format!("opennow-update-helper{suffix}"), &helper),
|
||||
] {
|
||||
stage_executable(source, &application.parent().unwrap().join(&name));
|
||||
stage_executable(source, &payload_application.parent().unwrap().join(&name));
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
if wrong_identity {
|
||||
fs::rename(
|
||||
&payload_application,
|
||||
payload_application.with_file_name("OtherNOW.exe"),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
zip_tree(&payload, &package);
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
sign_bundle(&target, "org.opennow.integration", "1.0.0");
|
||||
sign_bundle(
|
||||
&payload,
|
||||
if wrong_identity {
|
||||
"org.opennow.unrelated"
|
||||
} else {
|
||||
"org.opennow.integration"
|
||||
},
|
||||
"1.2.3",
|
||||
);
|
||||
command_ok(
|
||||
Command::new("/usr/bin/hdiutil")
|
||||
.args([
|
||||
"create",
|
||||
"-quiet",
|
||||
"-format",
|
||||
"UDZO",
|
||||
"-volname",
|
||||
"OpenNOW integration",
|
||||
"-srcfolder",
|
||||
])
|
||||
.arg(&image_root)
|
||||
.arg(&package),
|
||||
);
|
||||
}
|
||||
let bytes = if payload_application.exists() {
|
||||
fs::read(&payload_application).unwrap()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
(kind, target, application, package, bytes)
|
||||
};
|
||||
sign(&package);
|
||||
let target = fs::canonicalize(target).unwrap();
|
||||
let application_path = fs::canonicalize(application).unwrap();
|
||||
let original = fs::read(&application_path).unwrap();
|
||||
let data_dir = if cfg!(windows) {
|
||||
target.join("user-data")
|
||||
} else {
|
||||
root.join("settings")
|
||||
};
|
||||
fs::create_dir(&data_dir).unwrap();
|
||||
fs::write(data_dir.join("preferences.json"), b"{\"preserved\":true}").unwrap();
|
||||
let application = OwnedChild(
|
||||
Command::new(&application_path)
|
||||
.arg("--hold")
|
||||
.spawn()
|
||||
.unwrap(),
|
||||
);
|
||||
let core = OwnedChild(Command::new(&previous).arg("--hold").spawn().unwrap());
|
||||
let plan = Plan {
|
||||
schema_version: 1,
|
||||
version: if cfg!(target_os = "linux") && wrong_identity {
|
||||
"1.2.4"
|
||||
} else {
|
||||
"1.2.3"
|
||||
}
|
||||
.to_owned(),
|
||||
kind,
|
||||
package,
|
||||
target,
|
||||
application_executable: application_path,
|
||||
data_dir: fs::canonicalize(data_dir).unwrap(),
|
||||
processes: vec![
|
||||
ProcessIdentity::capture(application.0.id()).unwrap(),
|
||||
ProcessIdentity::capture(core.0.id()).unwrap(),
|
||||
],
|
||||
nonce: "a".repeat(64),
|
||||
managed_identity: None,
|
||||
};
|
||||
atomic_json(&directory.join("plan.json"), &plan).unwrap();
|
||||
write_outcome(
|
||||
&directory,
|
||||
&plan,
|
||||
OutcomeStatus::Prepared,
|
||||
"Integration transaction prepared",
|
||||
None,
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
Fixture {
|
||||
_temporary: temporary,
|
||||
plan,
|
||||
directory,
|
||||
original,
|
||||
candidate: candidate_bytes,
|
||||
application,
|
||||
core,
|
||||
}
|
||||
}
|
||||
|
||||
fn wait_for_status(fixture: &Fixture, helper: &mut OwnedChild, status: OutcomeStatus) {
|
||||
let start = Instant::now();
|
||||
loop {
|
||||
if read_outcome(&fixture.directory.join("outcome.json"))
|
||||
.unwrap()
|
||||
.is_some_and(|outcome| outcome.status == status)
|
||||
{
|
||||
return;
|
||||
}
|
||||
assert!(
|
||||
helper.0.try_wait().unwrap().is_none(),
|
||||
"Helper exited before {status:?}"
|
||||
);
|
||||
assert!(
|
||||
start.elapsed() < Duration::from_secs(150),
|
||||
"Helper never reached {status:?}"
|
||||
);
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_after_exit(fixture: &mut Fixture) -> std::process::ExitStatus {
|
||||
let mut command = Command::new(fixture_path("OPENNOW_TEST_UPDATE_HELPER"));
|
||||
#[cfg(target_os = "linux")]
|
||||
command
|
||||
.env("APPDIR", "/tmp/opennow-old-mount")
|
||||
.env("APPIMAGE", &fixture.plan.target)
|
||||
.env("ARGV0", "/tmp/opennow-old-mount/AppRun");
|
||||
let mut helper = OwnedChild(
|
||||
command
|
||||
.arg("--apply")
|
||||
.arg(fixture.directory.join("plan.json"))
|
||||
.spawn()
|
||||
.unwrap(),
|
||||
);
|
||||
wait_for_status(fixture, &mut helper, OutcomeStatus::WaitingForExit);
|
||||
assert_eq!(
|
||||
fs::read(&fixture.plan.application_executable).unwrap(),
|
||||
fixture.original
|
||||
);
|
||||
assert!(!fixture.directory.join("ack.json").exists());
|
||||
fixture.application.0.kill().unwrap();
|
||||
fixture.application.0.wait().unwrap();
|
||||
fixture.core.0.kill().unwrap();
|
||||
fixture.core.0.wait().unwrap();
|
||||
let start = Instant::now();
|
||||
loop {
|
||||
if let Some(status) = helper.0.try_wait().unwrap() {
|
||||
return status;
|
||||
}
|
||||
assert!(
|
||||
start.elapsed() < Duration::from_secs(120),
|
||||
"Helper did not finish replacement"
|
||||
);
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "Run opennow-qt/tests/run_update_helper_integration.py with an ephemeral pinned test key"]
|
||||
fn signed_native_package_replaces_and_candidate_acknowledges_its_own_restart() {
|
||||
let mut fixture = fixture(false);
|
||||
assert!(apply_after_exit(&mut fixture).success());
|
||||
let outcome = read_outcome(&fixture.directory.join("outcome.json"))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(outcome.status, OutcomeStatus::Completed);
|
||||
assert_eq!(outcome.installed_version.as_deref(), Some("1.2.3"));
|
||||
assert_eq!(
|
||||
fs::read(&fixture.plan.application_executable).unwrap(),
|
||||
fixture.candidate
|
||||
);
|
||||
assert_ne!(fixture.candidate, fixture.original);
|
||||
let ack: Acknowledgement = read_json(&fixture.directory.join("ack.json"), 64 * 1024).unwrap();
|
||||
assert_eq!(ack.version, "1.2.3");
|
||||
assert_eq!(ack.nonce, fixture.plan.nonce);
|
||||
assert_ne!(ack.application.pid, std::process::id());
|
||||
assert_ne!(ack.application.pid, fixture.plan.processes[0].pid);
|
||||
assert_eq!(
|
||||
ack.application.executable,
|
||||
fixture.plan.application_executable
|
||||
);
|
||||
assert!(ack.application.is_running().unwrap());
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
let environment = fs::read(format!("/proc/{}/environ", ack.application.pid)).unwrap();
|
||||
let environment = String::from_utf8(environment).unwrap();
|
||||
assert!(!environment.contains("/tmp/opennow-old-mount"));
|
||||
}
|
||||
assert_eq!(
|
||||
fs::read_to_string(fixture.directory.join("candidate-started")).unwrap(),
|
||||
ack.application.pid.to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
fs::read(fixture.plan.data_dir.join("preferences.json")).unwrap(),
|
||||
b"{\"preserved\":true}"
|
||||
);
|
||||
assert!(!fixture.directory.join("previous").exists());
|
||||
assert!(!fixture.plan.package.exists());
|
||||
#[cfg(target_os = "macos")]
|
||||
assert!(!fixture.directory.join("payload.mount").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "Run opennow-qt/tests/run_update_helper_integration.py with an ephemeral pinned test key"]
|
||||
fn signed_native_package_with_failed_startup_rolls_back_and_restarts_previous() {
|
||||
let mut fixture = fixture(false);
|
||||
fs::write(fixture.directory.join("reject-startup"), b"reject").unwrap();
|
||||
assert!(!apply_after_exit(&mut fixture).success());
|
||||
assert_eq!(
|
||||
read_outcome(&fixture.directory.join("outcome.json"))
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.status,
|
||||
OutcomeStatus::RolledBack
|
||||
);
|
||||
assert_eq!(
|
||||
fs::read(&fixture.plan.application_executable).unwrap(),
|
||||
fixture.original
|
||||
);
|
||||
assert!(!fixture.directory.join("ack.json").exists());
|
||||
assert!(fixture.directory.join("candidate-started").exists());
|
||||
let start = Instant::now();
|
||||
while !fixture.plan.data_dir.join("previous-restarted").exists() {
|
||||
assert!(
|
||||
start.elapsed() < Duration::from_secs(10),
|
||||
"Previous application did not restart after rollback"
|
||||
);
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
assert_eq!(
|
||||
fs::read(fixture.plan.data_dir.join("previous-restarted")).unwrap(),
|
||||
b"1.0.0"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "Run opennow-qt/tests/run_update_helper_integration.py with an ephemeral pinned test key"]
|
||||
fn tampered_native_package_never_replaces_or_restarts() {
|
||||
let fixture = fixture(false);
|
||||
let mut bytes = fs::read(&fixture.plan.package).unwrap();
|
||||
let last = bytes.len() - 1;
|
||||
bytes[last] ^= 1;
|
||||
fs::write(&fixture.plan.package, bytes).unwrap();
|
||||
assert!(
|
||||
run_helper(&fixture.directory.join("plan.json"))
|
||||
.unwrap_err()
|
||||
.contains("SHA256")
|
||||
);
|
||||
assert_eq!(
|
||||
fs::read(&fixture.plan.application_executable).unwrap(),
|
||||
fixture.original
|
||||
);
|
||||
assert!(fixture.plan.processes[0].is_running().unwrap());
|
||||
assert!(!fixture.directory.join("candidate-started").exists());
|
||||
assert_eq!(
|
||||
read_outcome(&fixture.directory.join("outcome.json"))
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.status,
|
||||
OutcomeStatus::Failed
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "Run opennow-qt/tests/run_update_helper_integration.py with an ephemeral pinned test key"]
|
||||
fn signed_wrong_package_or_bundle_identity_never_replaces() {
|
||||
let fixture = fixture(true);
|
||||
assert!(run_helper(&fixture.directory.join("plan.json")).is_err());
|
||||
assert_eq!(
|
||||
fs::read(&fixture.plan.application_executable).unwrap(),
|
||||
fixture.original
|
||||
);
|
||||
assert!(fixture.plan.processes[0].is_running().unwrap());
|
||||
assert!(!fixture.directory.join("candidate-started").exists());
|
||||
assert_eq!(
|
||||
read_outcome(&fixture.directory.join("outcome.json"))
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.status,
|
||||
OutcomeStatus::Failed
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,756 @@
|
||||
use super::{InstallKind, OutcomeStatus, Plan, write_outcome};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::Path;
|
||||
use std::process::{Command, Stdio};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(super) struct Identity {
|
||||
package: String,
|
||||
version: String,
|
||||
architecture: String,
|
||||
installed_product: String,
|
||||
}
|
||||
|
||||
pub(super) fn prepare(
|
||||
kind: InstallKind,
|
||||
package: &Path,
|
||||
target: &Path,
|
||||
version: &str,
|
||||
) -> Result<Option<Identity>, String> {
|
||||
match kind {
|
||||
InstallKind::DebianPackage => deb_identity(package, target, version).map(Some),
|
||||
InstallKind::WindowsMsi => msi_identity(package, target, version).map(Some),
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn verify_identity(
|
||||
kind: InstallKind,
|
||||
package: &Path,
|
||||
target: &Path,
|
||||
version: &str,
|
||||
expected: Option<&Identity>,
|
||||
) -> Result<(), String> {
|
||||
if prepare(kind, package, target, version)?.as_ref() != expected {
|
||||
return Err("Native package identity changed after preparation".to_owned());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn output(command: &mut Command) -> Result<String, String> {
|
||||
let result = super::command_output(command, Duration::from_secs(120))?;
|
||||
if !result.status.success() {
|
||||
return Err("Native package identity query failed".to_owned());
|
||||
}
|
||||
if result.stdout.len() > 64 * 1024 {
|
||||
return Err("Native package query exceeded its limit".to_owned());
|
||||
}
|
||||
String::from_utf8(result.stdout)
|
||||
.map(|value| value.trim().to_owned())
|
||||
.map_err(|error| error.to_string())
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn deb_identity(package: &Path, target: &Path, version: &str) -> Result<Identity, String> {
|
||||
if !Path::new("/usr/bin/pkexec").is_file() {
|
||||
return Err(
|
||||
"DEB updates require PolicyKit's pkexec and an interactive authorization agent"
|
||||
.to_owned(),
|
||||
);
|
||||
}
|
||||
let field = |name| {
|
||||
output(
|
||||
Command::new("/usr/bin/dpkg-deb")
|
||||
.arg("--field")
|
||||
.arg(package)
|
||||
.arg(name),
|
||||
)
|
||||
};
|
||||
let name = field("Package")?;
|
||||
let architecture = field("Architecture")?;
|
||||
let expected_arch = if cfg!(target_arch = "x86_64") {
|
||||
"amd64"
|
||||
} else if cfg!(target_arch = "aarch64") {
|
||||
"arm64"
|
||||
} else {
|
||||
return Err("Unsupported DEB architecture".to_owned());
|
||||
};
|
||||
let version = version
|
||||
.trim_start_matches('v')
|
||||
.replace("-nightly.", "~nightly.")
|
||||
.replace("-supporter.", "~supporter.");
|
||||
if name != "opennow" || architecture != expected_arch || field("Version")? != version {
|
||||
return Err(
|
||||
"DEB package identity does not match OpenNOW, architecture, or signed version"
|
||||
.to_owned(),
|
||||
);
|
||||
}
|
||||
let files = deb_file_owners(target)?;
|
||||
if !deb_owns_file(&files, target) {
|
||||
return Err(
|
||||
"Running application is not owned by the installed OpenNOW DEB package".to_owned(),
|
||||
);
|
||||
}
|
||||
let installed = output(Command::new("/usr/bin/dpkg-query").args([
|
||||
"--show",
|
||||
"--showformat=${db:Status-Status}\t${Architecture}",
|
||||
"opennow",
|
||||
]))?;
|
||||
if installed != format!("installed\t{expected_arch}") {
|
||||
return Err("Installed OpenNOW DEB is not configured for this architecture".to_owned());
|
||||
}
|
||||
Ok(Identity {
|
||||
package: name,
|
||||
version,
|
||||
architecture,
|
||||
installed_product: "opennow".to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn deb_file_owners(target: &Path) -> Result<String, String> {
|
||||
let name = target.file_name().ok_or("Application has no file name")?;
|
||||
let mut pattern = std::ffi::OsString::from("*");
|
||||
pattern.push(name);
|
||||
output(
|
||||
Command::new("/usr/bin/dpkg-query")
|
||||
.arg("--search")
|
||||
.arg(pattern),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn deb_owns_file(files: &str, target: &Path) -> bool {
|
||||
let Ok(target) = super::canonical_file(target) else {
|
||||
return false;
|
||||
};
|
||||
files.lines().any(|line| {
|
||||
line.strip_prefix("opennow: ").is_some_and(|file| {
|
||||
Path::new(file).is_absolute()
|
||||
&& super::canonical_file(Path::new(file)).is_ok_and(|path| path == target)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
fn deb_identity(_: &Path, _: &Path, _: &str) -> Result<Identity, String> {
|
||||
Err("DEB updates require Linux".to_owned())
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
mod windows {
|
||||
use super::*;
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
use windows_sys::Win32::System::ApplicationInstallationAndServicing::*;
|
||||
pub(super) fn wide(value: impl AsRef<std::ffi::OsStr>) -> Vec<u16> {
|
||||
value.as_ref().encode_wide().chain(Some(0)).collect()
|
||||
}
|
||||
|
||||
pub(super) fn argument_path(path: &Path) -> Result<String, String> {
|
||||
let value = path
|
||||
.to_str()
|
||||
.ok_or("Windows Installer requires a Unicode installation path")?;
|
||||
if let Some(unc) = value.strip_prefix("\\\\?\\UNC\\") {
|
||||
return Ok(format!("\\\\{unc}"));
|
||||
}
|
||||
Ok(value.strip_prefix("\\\\?\\").unwrap_or(value).to_owned())
|
||||
}
|
||||
|
||||
pub(super) fn install_root_argument(target: &Path) -> Result<String, String> {
|
||||
let path = argument_path(target)?;
|
||||
if path.contains('"') || path.contains('\n') || path.contains('\r') {
|
||||
return Err(
|
||||
"Windows Installer requires an installation path without quotes".to_owned(),
|
||||
);
|
||||
}
|
||||
if path.is_empty() || path.len() > 32767 {
|
||||
return Err("Windows Installer requires a bounded installation path".to_owned());
|
||||
}
|
||||
Ok(format!("INSTALL_ROOT=\"{path}\""))
|
||||
}
|
||||
|
||||
struct Handle(u32);
|
||||
impl Drop for Handle {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
MsiCloseHandle(self.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn property(package: &Path, name: &str) -> Result<String, String> {
|
||||
let mut database = 0;
|
||||
if unsafe { MsiOpenDatabaseW(wide(package).as_ptr(), std::ptr::null(), &mut database) } != 0
|
||||
{
|
||||
return Err("Cannot read signed MSI database".to_owned());
|
||||
}
|
||||
let database = Handle(database);
|
||||
let mut view = 0;
|
||||
let query = format!("SELECT `Value` FROM `Property` WHERE `Property`='{name}'");
|
||||
if unsafe { MsiDatabaseOpenViewW(database.0, wide(query).as_ptr(), &mut view) } != 0 {
|
||||
return Err("Cannot query MSI identity".to_owned());
|
||||
}
|
||||
let view = Handle(view);
|
||||
if unsafe { MsiViewExecute(view.0, 0) } != 0 {
|
||||
return Err("Cannot execute MSI identity query".to_owned());
|
||||
}
|
||||
let mut record = 0;
|
||||
if unsafe { MsiViewFetch(view.0, &mut record) } != 0 {
|
||||
return Err(format!("MSI identity property {name} is missing"));
|
||||
}
|
||||
let record = Handle(record);
|
||||
let mut buffer = vec![0u16; 32768];
|
||||
let mut size = buffer.len() as u32;
|
||||
if unsafe { MsiRecordGetStringW(record.0, 1, buffer.as_mut_ptr(), &mut size) } != 0 {
|
||||
return Err("Cannot read MSI identity property".to_owned());
|
||||
}
|
||||
String::from_utf16(&buffer[..size as usize]).map_err(|error| error.to_string())
|
||||
}
|
||||
|
||||
pub(super) fn registered(product: &str, property: &str) -> Result<String, String> {
|
||||
let mut buffer = vec![0u16; 32768];
|
||||
let mut size = buffer.len() as u32;
|
||||
if unsafe {
|
||||
MsiGetProductInfoW(
|
||||
wide(product).as_ptr(),
|
||||
wide(property).as_ptr(),
|
||||
buffer.as_mut_ptr(),
|
||||
&mut size,
|
||||
)
|
||||
} != 0
|
||||
{
|
||||
return Err("Cannot read registered MSI installation identity".to_owned());
|
||||
}
|
||||
String::from_utf16(&buffer[..size as usize]).map_err(|error| error.to_string())
|
||||
}
|
||||
|
||||
fn architecture(package: &Path) -> Result<String, String> {
|
||||
let mut summary = 0;
|
||||
if unsafe { MsiGetSummaryInformationW(0, wide(package).as_ptr(), 0, &mut summary) } != 0 {
|
||||
return Err("Cannot read MSI architecture".to_owned());
|
||||
}
|
||||
let summary = Handle(summary);
|
||||
let mut buffer = [0u16; 1024];
|
||||
let mut size = buffer.len() as u32;
|
||||
let mut kind = 0;
|
||||
let mut integer = 0;
|
||||
let mut time = windows_sys::Win32::Foundation::FILETIME {
|
||||
dwLowDateTime: 0,
|
||||
dwHighDateTime: 0,
|
||||
};
|
||||
if unsafe {
|
||||
MsiSummaryInfoGetPropertyW(
|
||||
summary.0,
|
||||
7,
|
||||
&mut kind,
|
||||
&mut integer,
|
||||
&mut time,
|
||||
buffer.as_mut_ptr(),
|
||||
&mut size,
|
||||
)
|
||||
} != 0
|
||||
{
|
||||
return Err("Cannot read MSI architecture template".to_owned());
|
||||
}
|
||||
Ok(String::from_utf16(&buffer[..size as usize])
|
||||
.map_err(|error| error.to_string())?
|
||||
.split(';')
|
||||
.next()
|
||||
.unwrap_or_default()
|
||||
.to_owned())
|
||||
}
|
||||
|
||||
pub(super) fn family(version: &str) -> Result<&'static str, String> {
|
||||
let version = semver::Version::parse(version.trim_start_matches('v'))
|
||||
.map_err(|error| error.to_string())?;
|
||||
if version.pre.is_empty() {
|
||||
Ok("{6E81F7AE-B19D-4E87-A94A-2B2F01EBF762}")
|
||||
} else if version.pre.as_str().starts_with("nightly.") {
|
||||
Ok("{9661F4F8-656C-4B64-9035-01B04F4822B1}")
|
||||
} else if version.pre.as_str().starts_with("supporter.") {
|
||||
Ok("{B3AF8A40-5F44-445A-AD99-CECE00593601}")
|
||||
} else {
|
||||
Err("Unsupported MSI release channel".to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn installed_at(target: &Path, upgrade: &str) -> Result<Option<String>, String> {
|
||||
for index in 0..256 {
|
||||
let mut product = [0u16; 39];
|
||||
let status = unsafe {
|
||||
MsiEnumRelatedProductsW(wide(upgrade).as_ptr(), 0, index, product.as_mut_ptr())
|
||||
};
|
||||
if status == 259 {
|
||||
return Ok(None);
|
||||
}
|
||||
if status != 0 {
|
||||
return Err("Cannot enumerate registered MSI products".to_owned());
|
||||
}
|
||||
let product = String::from_utf16(&product[..38]).map_err(|error| error.to_string())?;
|
||||
let location = registered(&product, "InstallLocation")?;
|
||||
if location.is_empty() {
|
||||
return Err("An OpenNOW MSI registration has no installation location; repair it before updating".to_owned());
|
||||
}
|
||||
let location = std::fs::canonicalize(location).map_err(|_| "An OpenNOW MSI registration points to an unavailable installation; repair it before updating")?;
|
||||
if location == target {
|
||||
return Ok(Some(product));
|
||||
}
|
||||
}
|
||||
Err("Too many registered MSI products".to_owned())
|
||||
}
|
||||
|
||||
pub(super) fn identity(
|
||||
package: &Path,
|
||||
target: &Path,
|
||||
version: &str,
|
||||
) -> Result<Identity, String> {
|
||||
let upgrade = family(version)?;
|
||||
let parsed = semver::Version::parse(version.trim_start_matches('v'))
|
||||
.map_err(|error| error.to_string())?;
|
||||
let (name, expected_version) = if parsed.pre.is_empty() {
|
||||
(
|
||||
"OpenNOW",
|
||||
format!("{}.{}.{}", parsed.major, parsed.minor, parsed.patch),
|
||||
)
|
||||
} else {
|
||||
let parts: Vec<_> = parsed.pre.as_str().split('.').collect();
|
||||
if parts.len() != 3 {
|
||||
return Err("Invalid MSI channel version".to_owned());
|
||||
}
|
||||
let run: u32 = parts[1].parse().map_err(|_| "Invalid MSI release run")?;
|
||||
let attempt: u32 = parts[2]
|
||||
.parse()
|
||||
.map_err(|_| "Invalid MSI release attempt")?;
|
||||
if !(1..=65535).contains(&run) || !(1..=65535).contains(&attempt) {
|
||||
return Err("MSI release sequence is outside its supported range".to_owned());
|
||||
}
|
||||
(
|
||||
if parts[0] == "nightly" {
|
||||
"OpenNOW Nightly"
|
||||
} else {
|
||||
"OpenNOW Supporter"
|
||||
},
|
||||
format!("{}.{}.{}", run / 256, run % 256, attempt),
|
||||
)
|
||||
};
|
||||
if !property(package, "UpgradeCode")?.eq_ignore_ascii_case(upgrade)
|
||||
|| property(package, "ProductName")? != name
|
||||
|| property(package, "Manufacturer")? != "OpenCloudGaming"
|
||||
{
|
||||
return Err("MSI product family or channel does not match OpenNOW".to_owned());
|
||||
}
|
||||
let arch = architecture(package)?;
|
||||
let expected_arch = if cfg!(target_arch = "aarch64") {
|
||||
"Arm64"
|
||||
} else {
|
||||
"x64"
|
||||
};
|
||||
if !arch.eq_ignore_ascii_case(expected_arch) {
|
||||
return Err("MSI architecture does not match this installation".to_owned());
|
||||
}
|
||||
let installed_product = installed_at(target, upgrade)?
|
||||
.ok_or("No matching Windows Installer registration owns this application")?;
|
||||
let cached = registered(&installed_product, "LocalPackage")?;
|
||||
if architecture(Path::new(&cached))? != arch {
|
||||
return Err("MSI update architecture differs from the installed product".to_owned());
|
||||
}
|
||||
let numeric = property(package, "ProductVersion")?;
|
||||
if numeric != expected_version {
|
||||
return Err("MSI product version does not match the signed release version".to_owned());
|
||||
}
|
||||
Ok(Identity {
|
||||
package: property(package, "ProductCode")?,
|
||||
version: numeric,
|
||||
architecture: arch,
|
||||
installed_product,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn msi_identity(package: &Path, target: &Path, version: &str) -> Result<Identity, String> {
|
||||
windows::identity(package, target, version)
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
fn msi_identity(_: &Path, _: &Path, _: &str) -> Result<Identity, String> {
|
||||
Err("MSI updates require Windows".to_owned())
|
||||
}
|
||||
|
||||
pub(super) fn windows_managed(target: &Path) -> Result<bool, String> {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
for version in ["1.0.0", "1.0.0-nightly.1.1", "1.0.0-supporter.1.1"] {
|
||||
if windows::installed_at(target, windows::family(version)?)?.is_some() {
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
let _ = target;
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
fn msi_install_root_argument(target: &Path) -> Result<String, String> {
|
||||
let value = target
|
||||
.to_str()
|
||||
.ok_or("Windows Installer requires a Unicode installation path")?;
|
||||
let path = if let Some(unc) = value.strip_prefix("\\\\?\\UNC\\") {
|
||||
format!("\\\\{unc}")
|
||||
} else {
|
||||
value.strip_prefix("\\\\?\\").unwrap_or(value).to_owned()
|
||||
};
|
||||
if path.contains('"') || path.contains('\n') || path.contains('\r') {
|
||||
return Err("Windows Installer requires an installation path without quotes".to_owned());
|
||||
}
|
||||
if path.is_empty() || path.len() > 32767 {
|
||||
return Err("Windows Installer requires a bounded installation path".to_owned());
|
||||
}
|
||||
Ok(format!("INSTALL_ROOT=\"{path}\""))
|
||||
}
|
||||
|
||||
pub(super) fn install(plan: &Plan, directory: &Path) -> Result<(), String> {
|
||||
let identity = plan
|
||||
.managed_identity
|
||||
.as_ref()
|
||||
.ok_or("Missing prepared managed-package identity")?;
|
||||
if matches!(
|
||||
plan.kind,
|
||||
InstallKind::WindowsMsi | InstallKind::DebianPackage
|
||||
) {
|
||||
super::recovery::record_install_boot(directory)?;
|
||||
}
|
||||
let mut command = match plan.kind {
|
||||
InstallKind::DebianPackage => {
|
||||
let mut command = Command::new("/usr/bin/pkexec");
|
||||
command
|
||||
.args(["/usr/bin/dpkg", "--install"])
|
||||
.arg(&plan.package);
|
||||
command
|
||||
}
|
||||
InstallKind::WindowsMsi => {
|
||||
// apply() refuses this kind before install(), so in-app updates do not
|
||||
// reach msiexec. The command stays while the WiX publish job still ships
|
||||
// an MSI for manual install.
|
||||
let system =
|
||||
std::env::var_os("SystemRoot").ok_or("Windows system directory is unavailable")?;
|
||||
let mut command = Command::new(Path::new(&system).join("System32/msiexec.exe"));
|
||||
command.arg("/i");
|
||||
#[cfg(windows)]
|
||||
{
|
||||
let package = windows::argument_path(&plan.package)?;
|
||||
if package.contains('"') {
|
||||
return Err(
|
||||
"Windows Installer requires a package path without quotes".to_owned()
|
||||
);
|
||||
}
|
||||
command.arg(package);
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
command.arg(&plan.package);
|
||||
command.args(["/passive", "/norestart", "REBOOT=ReallySuppress"]);
|
||||
// msiexec parses the raw command line for PROPERTY=value tokens. A Rust
|
||||
// Command::arg containing spaces would be quoted as a whole
|
||||
// ("INSTALL_ROOT=C:\Program Files\..."), which msiexec rejects with 1639
|
||||
// and a help dialog. Emit INSTALL_ROOT="..." with quotes only around the
|
||||
// value, matching the installer contract test.
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::os::windows::process::CommandExt;
|
||||
command.raw_arg(windows::install_root_argument(&plan.target)?);
|
||||
let log = windows::argument_path(&directory.join("msiexec.log"))?;
|
||||
if log.contains('"') {
|
||||
return Err("Windows Installer requires a log path without quotes".to_owned());
|
||||
}
|
||||
command.arg("/l*v");
|
||||
command.arg(log);
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
command.arg(msi_install_root_argument(&plan.target)?);
|
||||
command.arg("/l*v");
|
||||
command.arg(directory.join("msiexec.log"));
|
||||
}
|
||||
command
|
||||
}
|
||||
_ => return Err("Not a managed package update".to_owned()),
|
||||
};
|
||||
let mut child = command
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
.map_err(|error| format!("Cannot start native package manager: {error}"))?;
|
||||
let started = Instant::now();
|
||||
let code = loop {
|
||||
if let Some(status) = child.try_wait().map_err(|error| error.to_string())? {
|
||||
break status.code().unwrap_or(-1);
|
||||
}
|
||||
if started.elapsed() > Duration::from_secs(15 * 60) {
|
||||
write_outcome(
|
||||
directory,
|
||||
plan,
|
||||
OutcomeStatus::ManagedPending,
|
||||
"Native package manager is still running; installation completion is not confirmed. Do not start another update.",
|
||||
None,
|
||||
super::ProcessIdentity::capture(child.id()).ok(),
|
||||
)?;
|
||||
return Err("Native installer exceeded the monitoring deadline; it was not killed or reported as successful".to_owned());
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(250));
|
||||
};
|
||||
if plan.kind == InstallKind::WindowsMsi && matches!(code, 3010 | 1641) {
|
||||
write_outcome(
|
||||
directory,
|
||||
plan,
|
||||
OutcomeStatus::RebootRequired,
|
||||
"Windows Installer requires a system restart; application startup is not yet confirmed",
|
||||
Some(identity.version.clone()),
|
||||
None,
|
||||
)?;
|
||||
return Ok(());
|
||||
}
|
||||
if code != 0 {
|
||||
let mut message = installer_failure_message(plan.kind, code);
|
||||
if plan.kind == InstallKind::WindowsMsi {
|
||||
let log = directory.join("msiexec.log");
|
||||
if log.is_file() {
|
||||
message.push_str(&format!("; installer log: {}", log.display()));
|
||||
}
|
||||
}
|
||||
if prepare(plan.kind, &plan.package, &plan.target, &plan.version).is_ok()
|
||||
&& super::canonical_file(&plan.application_executable).is_ok()
|
||||
{
|
||||
match super::restart_previous(plan) {
|
||||
Ok(_) => message.push_str("; the surviving registered application was restarted"),
|
||||
Err(error) => message.push_str(&format!(
|
||||
"; the surviving application could not restart: {error}"
|
||||
)),
|
||||
}
|
||||
} else {
|
||||
message
|
||||
.push_str("; a usable registered installation could not be confirmed for restart");
|
||||
}
|
||||
write_outcome(directory, plan, OutcomeStatus::Failed, &message, None, None)?;
|
||||
return Err(message);
|
||||
}
|
||||
verify_installed(plan.kind, identity, &plan.target)?;
|
||||
if let Err(error) = super::restart_and_acknowledge(plan, directory, Duration::from_secs(90)) {
|
||||
let message = format!(
|
||||
"Native package manager installed {}; application restart failed: {error}. No filesystem rollback was attempted for a managed installation.",
|
||||
identity.version
|
||||
);
|
||||
write_outcome(
|
||||
directory,
|
||||
plan,
|
||||
OutcomeStatus::Failed,
|
||||
&message,
|
||||
Some(identity.version.clone()),
|
||||
None,
|
||||
)?;
|
||||
return Err(message);
|
||||
}
|
||||
write_outcome(
|
||||
directory,
|
||||
plan,
|
||||
OutcomeStatus::Completed,
|
||||
"Native package manager confirmed installation and the updated application acknowledged healthy startup",
|
||||
Some(plan.version.clone()),
|
||||
None,
|
||||
)?;
|
||||
super::cleanup_completed(plan, directory)
|
||||
}
|
||||
|
||||
fn installer_failure_message(kind: InstallKind, code: i32) -> String {
|
||||
let reason = match (kind, code) {
|
||||
(InstallKind::DebianPackage, 126) => {
|
||||
"Update authorization was cancelled. Try installing the update again and approve the authorization prompt"
|
||||
}
|
||||
(InstallKind::DebianPackage, 127) => {
|
||||
"Update authorization failed. Ensure a PolicyKit authentication agent is running, then try again and approve the authorization prompt"
|
||||
}
|
||||
(InstallKind::WindowsMsi, 1602) => {
|
||||
"Windows Installer was cancelled. Try installing the update again and complete the installer prompts"
|
||||
}
|
||||
(InstallKind::WindowsMsi, 1639) => {
|
||||
"Windows Installer rejected the update command line. Try installing the update again; the installer log beside the update outcome has details"
|
||||
}
|
||||
_ => "Native package manager failed",
|
||||
};
|
||||
format!("{reason} (exit code {code}); update completion is not confirmed")
|
||||
}
|
||||
|
||||
pub(super) fn verify_installed(
|
||||
kind: InstallKind,
|
||||
identity: &Identity,
|
||||
target: &Path,
|
||||
) -> Result<(), String> {
|
||||
let _ = target;
|
||||
match kind {
|
||||
InstallKind::DebianPackage => {
|
||||
let value = super::command_output(
|
||||
Command::new("/usr/bin/dpkg-query").args([
|
||||
"--show",
|
||||
"--showformat=${db:Status-Status}\t${Version}\t${Architecture}",
|
||||
&identity.package,
|
||||
]),
|
||||
Duration::from_secs(5),
|
||||
)?;
|
||||
if !value.status.success()
|
||||
|| value.stdout
|
||||
!= format!("installed\t{}\t{}", identity.version, identity.architecture)
|
||||
.as_bytes()
|
||||
{
|
||||
return Err(
|
||||
"The OpenNOW DEB is not configured at the expected version and architecture."
|
||||
.to_owned(),
|
||||
);
|
||||
}
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
let files = deb_file_owners(target)?;
|
||||
if !deb_owns_file(&files, target) {
|
||||
return Err("Installed OpenNOW DEB does not own the application".to_owned());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
InstallKind::WindowsMsi => {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
if windows::registered(&identity.package, "VersionString")? != identity.version {
|
||||
return Err(
|
||||
"Windows Installer registration does not confirm the installed version"
|
||||
.to_owned(),
|
||||
);
|
||||
}
|
||||
if std::fs::canonicalize(windows::registered(&identity.package, "InstallLocation")?)
|
||||
.map_err(|error| error.to_string())?
|
||||
!= target
|
||||
{
|
||||
return Err(
|
||||
"Windows Installer registered an unexpected installation location"
|
||||
.to_owned(),
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
Err("MSI updates require Windows".to_owned())
|
||||
}
|
||||
_ => Err("Not a managed package".to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn installer_failures_explain_authorization_and_cancellation_without_claiming_success() {
|
||||
for (kind, code, explanation) in [
|
||||
(
|
||||
InstallKind::DebianPackage,
|
||||
126,
|
||||
"authorization was cancelled",
|
||||
),
|
||||
(
|
||||
InstallKind::DebianPackage,
|
||||
127,
|
||||
"PolicyKit authentication agent",
|
||||
),
|
||||
(
|
||||
InstallKind::WindowsMsi,
|
||||
1602,
|
||||
"Windows Installer was cancelled",
|
||||
),
|
||||
(
|
||||
InstallKind::WindowsMsi,
|
||||
1639,
|
||||
"rejected the update command line",
|
||||
),
|
||||
] {
|
||||
let message = installer_failure_message(kind, code);
|
||||
assert!(message.contains(explanation), "{message}");
|
||||
assert!(message.contains(&format!("exit code {code}")), "{message}");
|
||||
assert!(message.ends_with("update completion is not confirmed"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn installer_failure_codes_are_interpreted_only_for_their_package_manager() {
|
||||
for (kind, code) in [
|
||||
(InstallKind::DebianPackage, 1602),
|
||||
(InstallKind::DebianPackage, 1639),
|
||||
(InstallKind::WindowsMsi, 126),
|
||||
(InstallKind::WindowsMsi, 127),
|
||||
(InstallKind::DebianPackage, 1),
|
||||
(InstallKind::WindowsMsi, 1603),
|
||||
] {
|
||||
assert_eq!(
|
||||
installer_failure_message(kind, code),
|
||||
format!(
|
||||
"Native package manager failed (exit code {code}); update completion is not confirmed"
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn msi_install_root_quotes_only_the_value_for_msiexec() {
|
||||
#[cfg(windows)]
|
||||
let argument =
|
||||
windows::install_root_argument(Path::new(r"C:\Program Files\OpenNOW Nightly")).unwrap();
|
||||
#[cfg(not(windows))]
|
||||
let argument =
|
||||
msi_install_root_argument(Path::new(r"C:\Program Files\OpenNOW Nightly")).unwrap();
|
||||
assert_eq!(
|
||||
argument,
|
||||
r#"INSTALL_ROOT="C:\Program Files\OpenNOW Nightly""#
|
||||
);
|
||||
#[cfg(windows)]
|
||||
let stripped = windows::install_root_argument(Path::new(r"\\?\C:\OpenNOW")).unwrap();
|
||||
#[cfg(not(windows))]
|
||||
let stripped = msi_install_root_argument(Path::new(r"\\?\C:\OpenNOW")).unwrap();
|
||||
assert_eq!(stripped, r#"INSTALL_ROOT="C:\OpenNOW""#);
|
||||
#[cfg(windows)]
|
||||
assert!(windows::install_root_argument(Path::new("C:\\evil\"quote")).is_err());
|
||||
#[cfg(not(windows))]
|
||||
assert!(msi_install_root_argument(Path::new("C:\\evil\"quote")).is_err());
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
#[test]
|
||||
fn deb_ownership_matches_canonical_directories_but_rejects_file_symlinks() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let actual = directory.path().join("usr/bin");
|
||||
std::fs::create_dir_all(&actual).unwrap();
|
||||
let target = actual.join("opennow-qt");
|
||||
std::fs::write(&target, b"application").unwrap();
|
||||
let alias = directory.path().join("bin");
|
||||
std::os::unix::fs::symlink(&actual, &alias).unwrap();
|
||||
let registered = alias.join("opennow-qt");
|
||||
let owners = format!("opennow: {}", registered.display());
|
||||
assert!(deb_owns_file(&owners, &target));
|
||||
let unrelated = directory.path().join("unrelated");
|
||||
std::fs::write(&unrelated, b"other application").unwrap();
|
||||
assert!(!deb_owns_file(&owners, &unrelated));
|
||||
let link = actual.join("opennow-link");
|
||||
std::os::unix::fs::symlink(&unrelated, &link).unwrap();
|
||||
assert!(!deb_owns_file(
|
||||
&format!("opennow: {}", link.display()),
|
||||
&unrelated
|
||||
));
|
||||
assert!(!deb_owns_file(
|
||||
&format!("opennow: {}", unrelated.display()),
|
||||
&link
|
||||
));
|
||||
assert!(!deb_owns_file(
|
||||
&format!("other-package: {}", target.display()),
|
||||
&target
|
||||
));
|
||||
assert!(!deb_owns_file("opennow: relative/path", &target));
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,544 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
pub(super) struct OwnedApplication {
|
||||
pub child: std::process::Child,
|
||||
pub identity: ProcessIdentity,
|
||||
#[cfg(windows)]
|
||||
job: Job,
|
||||
}
|
||||
|
||||
impl OwnedApplication {
|
||||
pub fn start(command: &mut std::process::Command) -> Result<Self, super::RestartFailure> {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::process::CommandExt;
|
||||
command.process_group(0);
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::os::windows::process::CommandExt;
|
||||
command.creation_flags(0x00000004);
|
||||
}
|
||||
let mut child = command.spawn().map_err(|error| {
|
||||
super::RestartFailure::RollbackSafe(format!(
|
||||
"Updated application could not start: {error}"
|
||||
))
|
||||
})?;
|
||||
#[cfg(windows)]
|
||||
let job = match Job::assign(&child) {
|
||||
Ok(job) => job,
|
||||
Err(error) => {
|
||||
return Err(failed_start(&mut child, error));
|
||||
}
|
||||
};
|
||||
let identity = match ProcessIdentity::capture(child.id()) {
|
||||
Ok(identity) => identity,
|
||||
Err(error) => {
|
||||
return Err(failed_start(&mut child, error));
|
||||
}
|
||||
};
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::os::windows::io::AsRawHandle;
|
||||
#[link(name = "ntdll")]
|
||||
unsafe extern "system" {
|
||||
fn NtResumeProcess(process: windows_sys::Win32::Foundation::HANDLE) -> i32;
|
||||
}
|
||||
if unsafe { NtResumeProcess(child.as_raw_handle()) } != 0 {
|
||||
return Err(failed_start(
|
||||
&mut child,
|
||||
"Cannot resume the owned updated application".to_owned(),
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(Self {
|
||||
child,
|
||||
identity,
|
||||
#[cfg(windows)]
|
||||
job,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn owns(&self, process: &ProcessIdentity) -> Result<bool, String> {
|
||||
if !process.is_running()? || process.started < self.identity.started {
|
||||
return Ok(false);
|
||||
}
|
||||
#[cfg(unix)]
|
||||
{
|
||||
Ok(process.group == self.identity.group)
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
self.job.contains(process.pid)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stop(&mut self) -> Result<(), String> {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
if capture(self.identity.pid)?.is_some_and(|current| {
|
||||
current.started != self.identity.started || current.boot_id != self.identity.boot_id
|
||||
}) {
|
||||
return Err(
|
||||
"Updated application process identity was reused; refusing group shutdown"
|
||||
.to_owned(),
|
||||
);
|
||||
}
|
||||
if unsafe { libc::kill(-(self.identity.group as i32), libc::SIGKILL) } < 0 {
|
||||
let error = std::io::Error::last_os_error();
|
||||
if error.raw_os_error() != Some(libc::ESRCH) {
|
||||
return Err(error.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
self.job.stop()?;
|
||||
let start = Instant::now();
|
||||
loop {
|
||||
let exited = self
|
||||
.child
|
||||
.try_wait()
|
||||
.map_err(|error| error.to_string())?
|
||||
.is_some();
|
||||
#[cfg(unix)]
|
||||
let descendants = group_is_running(self.identity.group)?;
|
||||
#[cfg(windows)]
|
||||
let descendants = self.job.active()?;
|
||||
if exited && !descendants {
|
||||
return Ok(());
|
||||
}
|
||||
if start.elapsed() > Duration::from_secs(10) {
|
||||
return Err(
|
||||
"Could not stop all updated application processes before rollback".to_owned(),
|
||||
);
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn failed_start(
|
||||
child: &mut std::process::Child,
|
||||
error: String,
|
||||
) -> super::RestartFailure {
|
||||
let cleanup = (|| {
|
||||
#[cfg(unix)]
|
||||
if unsafe { libc::kill(-(child.id() as i32), libc::SIGKILL) } < 0 {
|
||||
let error = std::io::Error::last_os_error();
|
||||
if error.raw_os_error() != Some(libc::ESRCH) {
|
||||
return Err(error.to_string());
|
||||
}
|
||||
}
|
||||
if let (Err(error), None) = (
|
||||
child.kill(),
|
||||
child.try_wait().map_err(|error| error.to_string())?,
|
||||
) {
|
||||
return Err(error.to_string());
|
||||
}
|
||||
let start = Instant::now();
|
||||
loop {
|
||||
let exited = child
|
||||
.try_wait()
|
||||
.map_err(|error| error.to_string())?
|
||||
.is_some();
|
||||
#[cfg(unix)]
|
||||
let descendants = group_is_running(child.id())?;
|
||||
#[cfg(windows)]
|
||||
let descendants = false;
|
||||
if exited && !descendants {
|
||||
return Ok(());
|
||||
}
|
||||
if start.elapsed() >= Duration::from_secs(10) {
|
||||
return Err(
|
||||
"Failed startup process tree did not stop before the deadline".to_owned(),
|
||||
);
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
})();
|
||||
match cleanup {
|
||||
Ok(()) => super::RestartFailure::RollbackSafe(error),
|
||||
Err(cleanup_error) => super::RestartFailure::ProcessesMayBeRunning(format!(
|
||||
"{error}; startup cleanup failed: {cleanup_error}"
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn group_is_running(group: u32) -> Result<bool, String> {
|
||||
for entry in std::fs::read_dir("/proc").map_err(|error| error.to_string())? {
|
||||
let entry = entry.map_err(|error| error.to_string())?;
|
||||
if !entry
|
||||
.file_name()
|
||||
.to_string_lossy()
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_digit())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let Ok(stat) = std::fs::read_to_string(entry.path().join("stat")) else {
|
||||
continue;
|
||||
};
|
||||
let Some((_, fields)) = stat.rsplit_once(')') else {
|
||||
continue;
|
||||
};
|
||||
let fields: Vec<_> = fields.split_whitespace().collect();
|
||||
if fields.first() != Some(&"Z")
|
||||
&& fields.get(2).and_then(|value| value.parse::<u32>().ok()) == Some(group)
|
||||
{
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn group_is_running(group: u32) -> Result<bool, String> {
|
||||
unsafe extern "C" {
|
||||
fn proc_listpids(kind: u32, info: u32, buffer: *mut std::ffi::c_void, size: i32) -> i32;
|
||||
}
|
||||
let mut pids = vec![0u32; 100_000];
|
||||
let length =
|
||||
unsafe { proc_listpids(2, group, pids.as_mut_ptr().cast(), (pids.len() * 4) as i32) };
|
||||
if length < 0 || length as usize >= pids.len() * 4 {
|
||||
return Err("Cannot bound the updated application's process group".to_owned());
|
||||
}
|
||||
for pid in pids
|
||||
.into_iter()
|
||||
.take(length as usize / 4)
|
||||
.filter(|pid| *pid != 0)
|
||||
{
|
||||
if capture(pid)?.is_some() {
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
struct Job(windows_sys::Win32::Foundation::HANDLE);
|
||||
|
||||
#[cfg(windows)]
|
||||
impl Drop for Job {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
windows_sys::Win32::Foundation::CloseHandle(self.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
impl Job {
|
||||
fn assign(child: &std::process::Child) -> Result<Self, String> {
|
||||
use std::os::windows::io::AsRawHandle;
|
||||
use windows_sys::Win32::System::JobObjects::*;
|
||||
let handle = unsafe { CreateJobObjectW(std::ptr::null(), std::ptr::null()) };
|
||||
if handle.is_null() {
|
||||
return Err(std::io::Error::last_os_error().to_string());
|
||||
}
|
||||
let job = Self(handle);
|
||||
if unsafe { AssignProcessToJobObject(handle, child.as_raw_handle()) } == 0 {
|
||||
return Err(std::io::Error::last_os_error().to_string());
|
||||
}
|
||||
Ok(job)
|
||||
}
|
||||
fn contains(&self, pid: u32) -> Result<bool, String> {
|
||||
use windows_sys::Win32::System::{JobObjects::*, Threading::*};
|
||||
let process = unsafe { OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid) };
|
||||
if process.is_null() {
|
||||
return Err(std::io::Error::last_os_error().to_string());
|
||||
}
|
||||
let mut member = 0;
|
||||
let result = unsafe { IsProcessInJob(process, self.0, &mut member) };
|
||||
unsafe {
|
||||
windows_sys::Win32::Foundation::CloseHandle(process);
|
||||
}
|
||||
if result == 0 {
|
||||
return Err(std::io::Error::last_os_error().to_string());
|
||||
}
|
||||
Ok(member != 0)
|
||||
}
|
||||
fn stop(&self) -> Result<(), String> {
|
||||
if unsafe { windows_sys::Win32::System::JobObjects::TerminateJobObject(self.0, 1) } == 0 {
|
||||
return Err(std::io::Error::last_os_error().to_string());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn active(&self) -> Result<bool, String> {
|
||||
use windows_sys::Win32::System::JobObjects::*;
|
||||
let mut info: JOBOBJECT_BASIC_ACCOUNTING_INFORMATION = unsafe { std::mem::zeroed() };
|
||||
if unsafe {
|
||||
QueryInformationJobObject(
|
||||
self.0,
|
||||
JobObjectBasicAccountingInformation,
|
||||
(&mut info as *mut JOBOBJECT_BASIC_ACCOUNTING_INFORMATION).cast(),
|
||||
std::mem::size_of_val(&info) as u32,
|
||||
std::ptr::null_mut(),
|
||||
)
|
||||
} == 0
|
||||
{
|
||||
return Err(std::io::Error::last_os_error().to_string());
|
||||
}
|
||||
Ok(info.ActiveProcesses != 0)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct ProcessIdentity {
|
||||
pub pid: u32,
|
||||
pub started: u64,
|
||||
pub executable: PathBuf,
|
||||
pub group: u32,
|
||||
pub boot_id: String,
|
||||
}
|
||||
|
||||
impl ProcessIdentity {
|
||||
pub fn capture(pid: u32) -> Result<Self, String> {
|
||||
capture(pid)?.ok_or_else(|| format!("Process {pid} is not running"))
|
||||
}
|
||||
|
||||
pub fn matches_executable(&self, executable: &Path) -> Result<(), String> {
|
||||
if std::fs::canonicalize(executable).map_err(|error| error.to_string())? != self.executable
|
||||
{
|
||||
return Err("Application process does not match its trusted executable".to_owned());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn is_running(&self) -> Result<bool, String> {
|
||||
Ok(capture(self.pid)?.is_some_and(|current| {
|
||||
current.started == self.started && current.boot_id == self.boot_id
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn wait_for_exit(
|
||||
processes: &[ProcessIdentity],
|
||||
timeout: Duration,
|
||||
) -> Result<(), String> {
|
||||
let start = Instant::now();
|
||||
loop {
|
||||
let mut running = false;
|
||||
for process in processes {
|
||||
running |= process.is_running()?;
|
||||
}
|
||||
if !running {
|
||||
return Ok(());
|
||||
}
|
||||
if start.elapsed() >= timeout {
|
||||
return Err(
|
||||
"Timed out waiting for the original application and core to exit".to_owned(),
|
||||
);
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn owned_tree_is_running(identity: &ProcessIdentity) -> Result<bool, String> {
|
||||
#[cfg(target_os = "linux")]
|
||||
if std::fs::read_to_string("/proc/sys/kernel/random/boot_id")
|
||||
.map_err(|error| error.to_string())?
|
||||
.trim()
|
||||
!= identity.boot_id
|
||||
{
|
||||
return Ok(false);
|
||||
}
|
||||
#[cfg(unix)]
|
||||
{
|
||||
group_is_running(identity.group)
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
identity.is_running()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn capture(pid: u32) -> Result<Option<ProcessIdentity>, String> {
|
||||
let path = PathBuf::from(format!("/proc/{pid}"));
|
||||
let stat = match std::fs::read_to_string(path.join("stat")) {
|
||||
Ok(value) => value,
|
||||
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
|
||||
Err(error) => return Err(error.to_string()),
|
||||
};
|
||||
let fields: Vec<_> = stat
|
||||
.rsplit_once(')')
|
||||
.ok_or("Invalid process identity")?
|
||||
.1
|
||||
.split_whitespace()
|
||||
.collect();
|
||||
if fields.first() == Some(&"Z") {
|
||||
return Ok(None);
|
||||
}
|
||||
let started = fields
|
||||
.get(19)
|
||||
.ok_or("Missing process start identity")?
|
||||
.parse()
|
||||
.map_err(|_| "Invalid process start identity")?;
|
||||
let executable = match std::fs::read_link(path.join("exe")) {
|
||||
Ok(path) => path,
|
||||
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
|
||||
Err(error) => return Err(error.to_string()),
|
||||
};
|
||||
Ok(Some(ProcessIdentity {
|
||||
pid,
|
||||
started,
|
||||
executable,
|
||||
group: fields
|
||||
.get(2)
|
||||
.ok_or("Missing process group")?
|
||||
.parse()
|
||||
.map_err(|_| "Invalid process group")?,
|
||||
boot_id: std::fs::read_to_string("/proc/sys/kernel/random/boot_id")
|
||||
.map_err(|error| error.to_string())?
|
||||
.trim()
|
||||
.to_owned(),
|
||||
}))
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn capture(pid: u32) -> Result<Option<ProcessIdentity>, String> {
|
||||
#[repr(C)]
|
||||
#[derive(Default)]
|
||||
struct BsdInfo {
|
||||
flags: u32,
|
||||
status: u32,
|
||||
xstatus: u32,
|
||||
pid: u32,
|
||||
ppid: u32,
|
||||
uid: u32,
|
||||
gid: u32,
|
||||
ruid: u32,
|
||||
rgid: u32,
|
||||
svuid: u32,
|
||||
svgid: u32,
|
||||
rfu_1: u32,
|
||||
comm: [u8; 16],
|
||||
name: [u8; 32],
|
||||
nfiles: u32,
|
||||
pgid: u32,
|
||||
pjobc: u32,
|
||||
e_tdev: u32,
|
||||
e_tpgid: u32,
|
||||
nice: i32,
|
||||
start_tvsec: u64,
|
||||
start_tvusec: u64,
|
||||
}
|
||||
unsafe extern "C" {
|
||||
fn proc_pidinfo(
|
||||
pid: i32,
|
||||
flavor: i32,
|
||||
arg: u64,
|
||||
buffer: *mut std::ffi::c_void,
|
||||
size: i32,
|
||||
) -> i32;
|
||||
fn proc_pidpath(pid: i32, buffer: *mut std::ffi::c_void, size: u32) -> i32;
|
||||
}
|
||||
let mut info = BsdInfo::default();
|
||||
let length = unsafe {
|
||||
proc_pidinfo(
|
||||
pid as i32,
|
||||
3,
|
||||
0,
|
||||
(&mut info as *mut BsdInfo).cast(),
|
||||
std::mem::size_of::<BsdInfo>() as i32,
|
||||
)
|
||||
};
|
||||
if length == 0 {
|
||||
let error = std::io::Error::last_os_error();
|
||||
if error.raw_os_error() == Some(libc::ESRCH) {
|
||||
return Ok(None);
|
||||
}
|
||||
return Err(error.to_string());
|
||||
}
|
||||
if length as usize != std::mem::size_of::<BsdInfo>() {
|
||||
return Err("Incomplete macOS process identity".to_owned());
|
||||
}
|
||||
if info.status == 5 {
|
||||
return Ok(None);
|
||||
}
|
||||
let mut path = [0u8; 4096];
|
||||
let length = unsafe { proc_pidpath(pid as i32, path.as_mut_ptr().cast(), path.len() as u32) };
|
||||
if length <= 0 {
|
||||
let error = std::io::Error::last_os_error();
|
||||
if error.raw_os_error() == Some(libc::ESRCH) {
|
||||
return Ok(None);
|
||||
}
|
||||
return Err(error.to_string());
|
||||
}
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
let end = path
|
||||
.iter()
|
||||
.position(|byte| *byte == 0)
|
||||
.unwrap_or(length as usize);
|
||||
let executable = std::fs::canonicalize(Path::new(std::ffi::OsStr::from_bytes(&path[..end])))
|
||||
.map_err(|error| error.to_string())?;
|
||||
Ok(Some(ProcessIdentity {
|
||||
pid,
|
||||
started: info.start_tvsec * 1_000_000 + info.start_tvusec,
|
||||
executable,
|
||||
group: info.pgid,
|
||||
boot_id: String::new(),
|
||||
}))
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn capture(pid: u32) -> Result<Option<ProcessIdentity>, String> {
|
||||
use std::os::windows::ffi::OsStringExt;
|
||||
use windows_sys::Win32::Foundation::{CloseHandle, ERROR_INVALID_PARAMETER, FILETIME};
|
||||
use windows_sys::Win32::System::Threading::{
|
||||
GetExitCodeProcess, GetProcessTimes, OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION,
|
||||
QueryFullProcessImageNameW,
|
||||
};
|
||||
let handle = unsafe { OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid) };
|
||||
if handle.is_null() {
|
||||
let error = std::io::Error::last_os_error();
|
||||
if error.raw_os_error() == Some(ERROR_INVALID_PARAMETER as i32) {
|
||||
return Ok(None);
|
||||
}
|
||||
return Err(error.to_string());
|
||||
}
|
||||
let result = (|| {
|
||||
let mut exit = 0;
|
||||
if unsafe { GetExitCodeProcess(handle, &mut exit) } == 0 {
|
||||
return Err(std::io::Error::last_os_error().to_string());
|
||||
}
|
||||
if exit != 259 {
|
||||
return Ok(None);
|
||||
}
|
||||
let mut creation = FILETIME {
|
||||
dwLowDateTime: 0,
|
||||
dwHighDateTime: 0,
|
||||
};
|
||||
let mut end = creation;
|
||||
let mut kernel = creation;
|
||||
let mut user = creation;
|
||||
if unsafe { GetProcessTimes(handle, &mut creation, &mut end, &mut kernel, &mut user) } == 0
|
||||
{
|
||||
return Err(std::io::Error::last_os_error().to_string());
|
||||
}
|
||||
let mut buffer = vec![0u16; 32768];
|
||||
let mut length = buffer.len() as u32;
|
||||
if unsafe { QueryFullProcessImageNameW(handle, 0, buffer.as_mut_ptr(), &mut length) } == 0 {
|
||||
return Err(std::io::Error::last_os_error().to_string());
|
||||
}
|
||||
let executable = std::fs::canonicalize(PathBuf::from(std::ffi::OsString::from_wide(
|
||||
&buffer[..length as usize],
|
||||
)))
|
||||
.map_err(|error| error.to_string())?;
|
||||
Ok(Some(ProcessIdentity {
|
||||
pid,
|
||||
started: (creation.dwHighDateTime as u64) << 32 | creation.dwLowDateTime as u64,
|
||||
executable,
|
||||
group: 0,
|
||||
boot_id: String::new(),
|
||||
}))
|
||||
})();
|
||||
unsafe {
|
||||
CloseHandle(handle);
|
||||
}
|
||||
result
|
||||
}
|
||||
@@ -0,0 +1,313 @@
|
||||
use super::{InstallKind, OutcomeStatus, Plan, PreparedUpdate, UpdateOutcome, managed};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ManagedRecovery {
|
||||
Pending(String),
|
||||
RebootRequired(String),
|
||||
Finished(UpdateOutcome),
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
struct InstallBoot {
|
||||
schema_version: u32,
|
||||
boot_id: String,
|
||||
}
|
||||
|
||||
pub(super) fn record_install_boot(directory: &Path) -> Result<(), String> {
|
||||
super::atomic_json(
|
||||
&directory.join("install-boot.json"),
|
||||
&InstallBoot {
|
||||
schema_version: 1,
|
||||
boot_id: current_boot_id()?,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub fn recover_managed_update(
|
||||
prepared: &PreparedUpdate,
|
||||
outcome: &UpdateOutcome,
|
||||
running_version: &str,
|
||||
) -> Result<ManagedRecovery, String> {
|
||||
match super::helper_is_running(prepared) {
|
||||
Ok(true) => {
|
||||
return Ok(ManagedRecovery::Pending(
|
||||
"The update helper is still finishing the native installation.".to_owned(),
|
||||
));
|
||||
}
|
||||
Err(error) => {
|
||||
return Ok(ManagedRecovery::Pending(format!(
|
||||
"Could not confirm that the update helper has finished: {error}"
|
||||
)));
|
||||
}
|
||||
Ok(false) => (),
|
||||
}
|
||||
let directory = prepared
|
||||
.plan_path
|
||||
.parent()
|
||||
.ok_or("Missing update transaction directory.")?;
|
||||
let unknown_installer = match &outcome.restarted_process {
|
||||
Some(installer) => match installer.is_running() {
|
||||
Ok(true) => return Ok(ManagedRecovery::Pending(
|
||||
"The native package manager is still running; update completion is not confirmed."
|
||||
.to_owned(),
|
||||
)),
|
||||
Err(error) => Some(error),
|
||||
Ok(false) => None,
|
||||
},
|
||||
None if outcome.status == OutcomeStatus::ManagedPending => {
|
||||
Some("The native installer process identity was not recorded.".to_owned())
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
if let Some(reason) = unknown_installer {
|
||||
match reboot_completed(directory) {
|
||||
Ok(true) => (),
|
||||
Ok(false) => {
|
||||
return Ok(ManagedRecovery::Pending(format!(
|
||||
"Native installer completion cannot be confirmed: {reason} Restart your system before starting another update or session."
|
||||
)));
|
||||
}
|
||||
Err(error) => {
|
||||
return Ok(ManagedRecovery::Pending(format!(
|
||||
"Native installer completion cannot be confirmed: {reason} The restart boundary also could not be verified: {error}"
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
let plan: Plan = super::read_json(&prepared.plan_path, 64 * 1024)?;
|
||||
if plan.schema_version != 1
|
||||
|| plan.version != prepared.version
|
||||
|| outcome.version != prepared.version
|
||||
|| !matches!(
|
||||
plan.kind,
|
||||
InstallKind::WindowsMsi | InstallKind::DebianPackage
|
||||
)
|
||||
|| !matches!(
|
||||
outcome.status,
|
||||
OutcomeStatus::ManagedPending | OutcomeStatus::RebootRequired
|
||||
)
|
||||
|| (outcome.status == OutcomeStatus::RebootRequired && plan.kind != InstallKind::WindowsMsi)
|
||||
{
|
||||
return Err("Persisted managed update identity does not match its outcome.".to_owned());
|
||||
}
|
||||
let identity = plan
|
||||
.managed_identity
|
||||
.as_ref()
|
||||
.ok_or("Missing persisted native package identity.")?;
|
||||
let reboot_completed = if outcome.status == OutcomeStatus::RebootRequired {
|
||||
match reboot_completed(directory) {
|
||||
Ok(value) => value,
|
||||
Err(error) => {
|
||||
return Ok(ManagedRecovery::RebootRequired(format!(
|
||||
"Windows Installer requires a restart; reboot completion could not be verified: {error}"
|
||||
)));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
true
|
||||
};
|
||||
let recovery = classify_recovery(outcome, running_version, reboot_completed, || {
|
||||
managed::verify_installed(plan.kind, identity, &plan.target)
|
||||
});
|
||||
if let ManagedRecovery::Finished(result) = &recovery {
|
||||
super::atomic_json(&prepared.outcome_path, result)?;
|
||||
}
|
||||
Ok(recovery)
|
||||
}
|
||||
|
||||
fn classify_recovery(
|
||||
outcome: &UpdateOutcome,
|
||||
running_version: &str,
|
||||
reboot_completed: bool,
|
||||
verify_installed: impl FnOnce() -> Result<(), String>,
|
||||
) -> ManagedRecovery {
|
||||
if outcome.status == OutcomeStatus::RebootRequired && !reboot_completed {
|
||||
return ManagedRecovery::RebootRequired("Windows Installer requires a system restart before all updated files can be confirmed. Sessions remain available, but restart before checking for another update.".to_owned());
|
||||
}
|
||||
let mut result = outcome.clone();
|
||||
result.restarted_process = None;
|
||||
result.installed_version = None;
|
||||
if let Err(error) = verify_installed() {
|
||||
result.status = OutcomeStatus::Failed;
|
||||
result.message = format!(
|
||||
"The native installer has finished, but the expected installed package could not be verified: {error}"
|
||||
);
|
||||
} else if running_version.trim_start_matches('v') != outcome.version.trim_start_matches('v') {
|
||||
result.status = OutcomeStatus::Failed;
|
||||
result.message = format!(
|
||||
"The native package is installed, but OpenNOW is running {running_version} instead of {}. Restart OpenNOW before retrying the update.",
|
||||
outcome.version
|
||||
);
|
||||
} else {
|
||||
result.status = OutcomeStatus::Completed;
|
||||
result.installed_version = Some(outcome.version.clone());
|
||||
result.message = format!(
|
||||
"Native package identity and running OpenNOW {} were verified after installation.",
|
||||
outcome.version
|
||||
);
|
||||
}
|
||||
ManagedRecovery::Finished(result)
|
||||
}
|
||||
|
||||
fn reboot_completed(directory: &Path) -> Result<bool, String> {
|
||||
let path = directory.join("install-boot.json");
|
||||
if !path.try_exists().map_err(|error| error.to_string())? {
|
||||
record_install_boot(directory)?;
|
||||
return Ok(false);
|
||||
}
|
||||
recorded_boot_changed(directory)
|
||||
}
|
||||
|
||||
fn recorded_boot_changed(directory: &Path) -> Result<bool, String> {
|
||||
let previous: InstallBoot = super::read_json(&directory.join("install-boot.json"), 1024)?;
|
||||
if previous.schema_version != 1 || previous.boot_id.is_empty() {
|
||||
return Err("The recorded installation boot identity is invalid.".to_owned());
|
||||
}
|
||||
Ok(previous.boot_id != current_boot_id()?)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn current_boot_id() -> Result<String, String> {
|
||||
use windows_sys::Wdk::System::Threading::{
|
||||
NtQueryInformationProcess, ProcessTelemetryIdInformation,
|
||||
};
|
||||
use windows_sys::Win32::System::Threading::GetCurrentProcess;
|
||||
let mut returned = 0u32;
|
||||
unsafe {
|
||||
NtQueryInformationProcess(
|
||||
GetCurrentProcess(),
|
||||
ProcessTelemetryIdInformation,
|
||||
std::ptr::null_mut(),
|
||||
0,
|
||||
&mut returned,
|
||||
);
|
||||
}
|
||||
if !(64..=1024 * 1024).contains(&returned) {
|
||||
return Err("Windows did not provide bounded process telemetry.".to_owned());
|
||||
}
|
||||
let mut information = vec![0u64; (returned as usize).div_ceil(8)];
|
||||
let status = unsafe {
|
||||
NtQueryInformationProcess(
|
||||
GetCurrentProcess(),
|
||||
ProcessTelemetryIdInformation,
|
||||
information.as_mut_ptr().cast(),
|
||||
(information.len() * 8) as u32,
|
||||
&mut returned,
|
||||
)
|
||||
};
|
||||
let header_size = information[0] as u32;
|
||||
let process_id = (information[0] >> 32) as u32;
|
||||
let boot_id = (information[7] >> 32) as u32;
|
||||
if status < 0
|
||||
|| returned < 64
|
||||
|| header_size < 64
|
||||
|| header_size > returned
|
||||
|| process_id != std::process::id()
|
||||
{
|
||||
return Err("Windows did not provide a valid boot sequence number.".to_owned());
|
||||
}
|
||||
Ok(format!("windows-boot-sequence:{boot_id}"))
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn current_boot_id() -> Result<String, String> {
|
||||
let value = std::fs::read_to_string("/proc/sys/kernel/random/boot_id")
|
||||
.map_err(|error| error.to_string())?;
|
||||
let value = value.trim();
|
||||
if value.len() != 36
|
||||
|| !value
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_hexdigit() || byte == b'-')
|
||||
{
|
||||
return Err("The operating system boot identifier is invalid.".to_owned());
|
||||
}
|
||||
Ok(value.to_owned())
|
||||
}
|
||||
|
||||
#[cfg(not(any(windows, target_os = "linux")))]
|
||||
fn current_boot_id() -> Result<String, String> {
|
||||
Err("Managed reboot recovery is not supported on this platform.".to_owned())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn outcome(status: OutcomeStatus) -> UpdateOutcome {
|
||||
UpdateOutcome {
|
||||
schema_version: 1,
|
||||
version: "1.2.3".to_owned(),
|
||||
status,
|
||||
message: "Native installation is pending".to_owned(),
|
||||
installed_version: None,
|
||||
restarted_process: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recovery_requires_native_identity_and_the_running_version() {
|
||||
for status in [OutcomeStatus::ManagedPending, OutcomeStatus::RebootRequired] {
|
||||
for (version, installed, expected) in [
|
||||
("1.2.3", Ok(()), OutcomeStatus::Completed),
|
||||
("1.2.2", Ok(()), OutcomeStatus::Failed),
|
||||
(
|
||||
"1.2.3",
|
||||
Err("Native registration does not match".to_owned()),
|
||||
OutcomeStatus::Failed,
|
||||
),
|
||||
] {
|
||||
let ManagedRecovery::Finished(result) =
|
||||
classify_recovery(&outcome(status), version, true, || installed)
|
||||
else {
|
||||
panic!("Finished installer must resolve to an outcome")
|
||||
};
|
||||
assert_eq!(result.status, expected);
|
||||
assert_eq!(
|
||||
result.installed_version.is_some(),
|
||||
expected == OutcomeStatus::Completed
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reboot_required_never_claims_success_before_a_proven_reboot() {
|
||||
assert!(matches!(
|
||||
classify_recovery(
|
||||
&outcome(OutcomeStatus::RebootRequired),
|
||||
"1.2.3",
|
||||
false,
|
||||
|| panic!("Native package verification must not run before reboot")
|
||||
),
|
||||
ManagedRecovery::RebootRequired(_)
|
||||
));
|
||||
}
|
||||
|
||||
#[cfg(any(windows, target_os = "linux"))]
|
||||
#[test]
|
||||
fn legacy_reboot_marker_requires_a_subsequent_boot() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
assert!(!reboot_completed(directory.path()).unwrap());
|
||||
assert!(!reboot_completed(directory.path()).unwrap());
|
||||
super::super::atomic_json(
|
||||
&directory.path().join("install-boot.json"),
|
||||
&InstallBoot {
|
||||
schema_version: 1,
|
||||
boot_id: "a-previous-boot".to_owned(),
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
assert!(reboot_completed(directory.path()).unwrap());
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn windows_boot_sequence_is_available_and_stable() {
|
||||
let first = current_boot_id().unwrap();
|
||||
assert!(first.starts_with("windows-boot-sequence:"));
|
||||
assert_eq!(first, current_boot_id().unwrap());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,355 @@
|
||||
use std::path::Path;
|
||||
|
||||
pub(super) fn create_private_directory(path: &Path) -> Result<(), String> {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::DirBuilderExt;
|
||||
let mut builder = std::fs::DirBuilder::new();
|
||||
builder.mode(0o700);
|
||||
builder.create(path).map_err(|error| error.to_string())
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
windows::create_private(path)
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn preserve_permissions(source: &Path, destination: &Path) -> Result<(), String> {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
let metadata = std::fs::symlink_metadata(source).map_err(|error| error.to_string())?;
|
||||
if metadata.is_symlink() {
|
||||
return Err("Cannot preserve permissions through a user-data symlink".to_owned());
|
||||
}
|
||||
std::os::unix::fs::chown(destination, Some(metadata.uid()), Some(metadata.gid()))
|
||||
.map_err(|error| format!("Cannot preserve user-data ownership: {error}"))?;
|
||||
std::fs::set_permissions(destination, metadata.permissions())
|
||||
.map_err(|error| format!("Cannot preserve user-data permissions: {error}"))?;
|
||||
preserve_unix_acl(source, destination)
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
windows::preserve(source, destination)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn preserve_unix_acl(source: &Path, destination: &Path) -> Result<(), String> {
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
let source =
|
||||
std::ffi::CString::new(source.as_os_str().as_bytes()).map_err(|error| error.to_string())?;
|
||||
let destination = std::ffi::CString::new(destination.as_os_str().as_bytes())
|
||||
.map_err(|error| error.to_string())?;
|
||||
for name in [c"system.posix_acl_access", c"system.posix_acl_default"] {
|
||||
let size =
|
||||
unsafe { libc::getxattr(source.as_ptr(), name.as_ptr(), std::ptr::null_mut(), 0) };
|
||||
if size < 0 {
|
||||
let error = std::io::Error::last_os_error();
|
||||
if !matches!(error.raw_os_error(), Some(libc::ENODATA | libc::ENOTSUP)) {
|
||||
return Err(format!("Cannot inspect user-data ACL: {error}"));
|
||||
}
|
||||
if unsafe { libc::removexattr(destination.as_ptr(), name.as_ptr()) } < 0 {
|
||||
let error = std::io::Error::last_os_error();
|
||||
if !matches!(error.raw_os_error(), Some(libc::ENODATA | libc::ENOTSUP)) {
|
||||
return Err(format!("Cannot remove inherited user-data ACL: {error}"));
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if size > 64 * 1024 {
|
||||
return Err("User-data ACL exceeds its size limit".to_owned());
|
||||
}
|
||||
let mut acl = vec![0u8; size as usize];
|
||||
let read = unsafe {
|
||||
libc::getxattr(
|
||||
source.as_ptr(),
|
||||
name.as_ptr(),
|
||||
acl.as_mut_ptr().cast(),
|
||||
acl.len(),
|
||||
)
|
||||
};
|
||||
if read != size {
|
||||
return Err("User-data ACL changed while it was being copied".to_owned());
|
||||
}
|
||||
if unsafe {
|
||||
libc::setxattr(
|
||||
destination.as_ptr(),
|
||||
name.as_ptr(),
|
||||
acl.as_ptr().cast(),
|
||||
acl.len(),
|
||||
0,
|
||||
)
|
||||
} < 0
|
||||
{
|
||||
return Err(format!(
|
||||
"Cannot preserve user-data ACL: {}",
|
||||
std::io::Error::last_os_error()
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn preserve_unix_acl(source: &Path, destination: &Path) -> Result<(), String> {
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
unsafe extern "C" {
|
||||
fn copyfile(
|
||||
source: *const libc::c_char,
|
||||
destination: *const libc::c_char,
|
||||
state: *mut libc::c_void,
|
||||
flags: u32,
|
||||
) -> i32;
|
||||
}
|
||||
let source =
|
||||
std::ffi::CString::new(source.as_os_str().as_bytes()).map_err(|error| error.to_string())?;
|
||||
let destination = std::ffi::CString::new(destination.as_os_str().as_bytes())
|
||||
.map_err(|error| error.to_string())?;
|
||||
if unsafe {
|
||||
copyfile(
|
||||
source.as_ptr(),
|
||||
destination.as_ptr(),
|
||||
std::ptr::null_mut(),
|
||||
1,
|
||||
)
|
||||
} != 0
|
||||
{
|
||||
return Err(format!(
|
||||
"Cannot preserve user-data ACL: {}",
|
||||
std::io::Error::last_os_error()
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
mod windows {
|
||||
use super::*;
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
use windows_sys::Win32::Foundation::LocalFree;
|
||||
use windows_sys::Win32::Security::Authorization::*;
|
||||
use windows_sys::Win32::Security::*;
|
||||
|
||||
struct Descriptor(PSECURITY_DESCRIPTOR);
|
||||
impl Drop for Descriptor {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
LocalFree(self.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn wide(path: &Path) -> Vec<u16> {
|
||||
path.as_os_str().encode_wide().chain(Some(0)).collect()
|
||||
}
|
||||
|
||||
pub(super) fn create_private(path: &Path) -> Result<(), String> {
|
||||
use std::os::windows::fs::OpenOptionsExt;
|
||||
use std::os::windows::io::AsRawHandle;
|
||||
use windows_sys::Win32::Storage::FileSystem::{
|
||||
FILE_FLAG_BACKUP_SEMANTICS, GetVolumeInformationByHandleW,
|
||||
};
|
||||
let parent = std::fs::OpenOptions::new()
|
||||
.read(true)
|
||||
.custom_flags(FILE_FLAG_BACKUP_SEMANTICS)
|
||||
.open(
|
||||
path.parent()
|
||||
.ok_or("Private update directory has no parent")?,
|
||||
)
|
||||
.map_err(|error| error.to_string())?;
|
||||
let mut flags = 0;
|
||||
if unsafe {
|
||||
GetVolumeInformationByHandleW(
|
||||
parent.as_raw_handle(),
|
||||
std::ptr::null_mut(),
|
||||
0,
|
||||
std::ptr::null_mut(),
|
||||
std::ptr::null_mut(),
|
||||
&mut flags,
|
||||
std::ptr::null_mut(),
|
||||
0,
|
||||
)
|
||||
} == 0
|
||||
{
|
||||
return Err(format!(
|
||||
"Cannot verify update volume ACL support: {}",
|
||||
std::io::Error::last_os_error()
|
||||
));
|
||||
}
|
||||
require_persistent_acls(flags)?;
|
||||
let text: Vec<_> = "D:P(A;OICI;FA;;;OW)"
|
||||
.encode_utf16()
|
||||
.chain(Some(0))
|
||||
.collect();
|
||||
let mut descriptor = std::ptr::null_mut();
|
||||
if unsafe {
|
||||
ConvertStringSecurityDescriptorToSecurityDescriptorW(
|
||||
text.as_ptr(),
|
||||
1,
|
||||
&mut descriptor,
|
||||
std::ptr::null_mut(),
|
||||
)
|
||||
} == 0
|
||||
{
|
||||
return Err(std::io::Error::last_os_error().to_string());
|
||||
}
|
||||
let descriptor = Descriptor(descriptor);
|
||||
let attributes = SECURITY_ATTRIBUTES {
|
||||
nLength: std::mem::size_of::<SECURITY_ATTRIBUTES>() as u32,
|
||||
lpSecurityDescriptor: descriptor.0,
|
||||
bInheritHandle: 0,
|
||||
};
|
||||
if unsafe {
|
||||
windows_sys::Win32::Storage::FileSystem::CreateDirectoryW(
|
||||
wide(path).as_ptr(),
|
||||
&attributes,
|
||||
)
|
||||
} == 0
|
||||
{
|
||||
return Err(format!(
|
||||
"Cannot create private update directory: {}",
|
||||
std::io::Error::last_os_error()
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn require_persistent_acls(flags: u32) -> Result<(), String> {
|
||||
if flags & windows_sys::Win32::System::SystemServices::FILE_PERSISTENT_ACLS == 0 {
|
||||
return Err("Update staging requires a filesystem with persistent ACLs; FAT and exFAT cannot protect the transaction".to_owned());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn preserve(source: &Path, destination: &Path) -> Result<(), String> {
|
||||
let mut owner = std::ptr::null_mut();
|
||||
let mut group = std::ptr::null_mut();
|
||||
let mut acl = std::ptr::null_mut();
|
||||
let mut descriptor = std::ptr::null_mut();
|
||||
let info =
|
||||
OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
|
||||
let code = unsafe {
|
||||
GetNamedSecurityInfoW(
|
||||
wide(source).as_ptr(),
|
||||
SE_FILE_OBJECT,
|
||||
info,
|
||||
&mut owner,
|
||||
&mut group,
|
||||
&mut acl,
|
||||
std::ptr::null_mut(),
|
||||
&mut descriptor,
|
||||
)
|
||||
};
|
||||
if code != 0 {
|
||||
return Err(format!(
|
||||
"Cannot inspect user-data security descriptor: Windows error {code}"
|
||||
));
|
||||
}
|
||||
let _descriptor = Descriptor(descriptor);
|
||||
let code = unsafe {
|
||||
SetNamedSecurityInfoW(
|
||||
wide(destination).as_ptr(),
|
||||
SE_FILE_OBJECT,
|
||||
info | PROTECTED_DACL_SECURITY_INFORMATION,
|
||||
owner,
|
||||
group,
|
||||
acl,
|
||||
std::ptr::null_mut(),
|
||||
)
|
||||
};
|
||||
if code != 0 {
|
||||
return Err(format!(
|
||||
"Cannot preserve the original effective user-data DACL and ownership: Windows error {code}"
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn private_staging_requires_persistent_acl_support() {
|
||||
assert!(require_persistent_acls(0).is_err());
|
||||
assert!(
|
||||
require_persistent_acls(
|
||||
!windows_sys::Win32::System::SystemServices::FILE_PERSISTENT_ACLS
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
require_persistent_acls(
|
||||
windows_sys::Win32::System::SystemServices::FILE_PERSISTENT_ACLS
|
||||
)
|
||||
.is_ok()
|
||||
);
|
||||
}
|
||||
|
||||
fn dacl(path: &Path) -> (Vec<Vec<u8>>, bool) {
|
||||
let mut acl = std::ptr::null_mut();
|
||||
let mut descriptor = std::ptr::null_mut();
|
||||
assert_eq!(
|
||||
unsafe {
|
||||
GetNamedSecurityInfoW(
|
||||
wide(path).as_ptr(),
|
||||
SE_FILE_OBJECT,
|
||||
DACL_SECURITY_INFORMATION,
|
||||
std::ptr::null_mut(),
|
||||
std::ptr::null_mut(),
|
||||
&mut acl,
|
||||
std::ptr::null_mut(),
|
||||
&mut descriptor,
|
||||
)
|
||||
},
|
||||
0
|
||||
);
|
||||
let descriptor = Descriptor(descriptor);
|
||||
let mut control = 0;
|
||||
let mut revision = 0;
|
||||
assert_ne!(
|
||||
unsafe { GetSecurityDescriptorControl(descriptor.0, &mut control, &mut revision) },
|
||||
0
|
||||
);
|
||||
assert!(!acl.is_null());
|
||||
let mut entries = Vec::new();
|
||||
for index in 0..unsafe { (*acl).AceCount } {
|
||||
let mut ace = std::ptr::null_mut();
|
||||
assert_ne!(unsafe { GetAce(acl, index as u32, &mut ace) }, 0);
|
||||
let header = ace.cast::<ACE_HEADER>();
|
||||
let mut bytes = unsafe {
|
||||
std::slice::from_raw_parts(ace.cast::<u8>(), (*header).AceSize as usize)
|
||||
}
|
||||
.to_vec();
|
||||
bytes[1] &= !(INHERITED_ACE as u8);
|
||||
entries.push(bytes);
|
||||
}
|
||||
(entries, control & SE_DACL_PROTECTED != 0)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn private_transactions_and_preserved_profiles_keep_effective_dacls() {
|
||||
let directory = tempfile::TempDir::new().unwrap();
|
||||
let profile = directory.path().join("profile");
|
||||
create_private(&profile).unwrap();
|
||||
std::fs::write(profile.join("account.json"), b"private fixture data").unwrap();
|
||||
let root_before = dacl(&profile);
|
||||
let file_before = dacl(&profile.join("account.json"));
|
||||
assert!(root_before.1);
|
||||
let destination = directory.path().join("preserved");
|
||||
crate::update_apply::copy_user_data(
|
||||
&profile,
|
||||
&destination,
|
||||
&mut crate::update_apply::bundle::CopyBudget::new(),
|
||||
)
|
||||
.unwrap();
|
||||
let root_after = dacl(&destination);
|
||||
let file_after = dacl(&destination.join("account.json"));
|
||||
assert_eq!(root_before.0, root_after.0);
|
||||
assert_eq!(file_before.0, file_after.0);
|
||||
assert!(root_after.1);
|
||||
assert!(file_after.1);
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,134 @@
|
||||
use base64::Engine as _;
|
||||
use base64::engine::general_purpose::STANDARD as BASE64;
|
||||
use ed25519_dalek::{Signature, VerifyingKey};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::path::Path;
|
||||
|
||||
pub const MAXIMUM_MANIFEST_BYTES: u64 = 64 * 1024;
|
||||
pub const MAXIMUM_UPDATE_BYTES: u64 = 4 * 1024 * 1024 * 1024;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct UpdateManifest {
|
||||
pub schema_version: u32,
|
||||
pub version: String,
|
||||
pub asset: String,
|
||||
pub size: u64,
|
||||
pub sha256: String,
|
||||
pub signature: String,
|
||||
}
|
||||
|
||||
pub fn embedded_update_key() -> Result<VerifyingKey, String> {
|
||||
let encoded = option_env!("OPENNOW_UPDATE_ED25519_PUBLIC_KEY")
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.ok_or("This build has no pinned update signing key")?;
|
||||
decode_verifying_key(encoded)
|
||||
}
|
||||
|
||||
pub fn decode_verifying_key(encoded: &str) -> Result<VerifyingKey, String> {
|
||||
let bytes: [u8; 32] = BASE64
|
||||
.decode(encoded)
|
||||
.map_err(|_| "Pinned update signing key is not valid base64")?
|
||||
.try_into()
|
||||
.map_err(|_| "Pinned update signing key has the wrong length")?;
|
||||
VerifyingKey::from_bytes(&bytes).map_err(|_| "Pinned update signing key is invalid".to_owned())
|
||||
}
|
||||
|
||||
pub fn signature_payload(manifest: &UpdateManifest) -> String {
|
||||
format!(
|
||||
"OpenNOW update manifest v1\nversion={}\nasset={}\nsize={}\nsha256={}\n",
|
||||
manifest.version.trim_start_matches('v'),
|
||||
manifest.asset,
|
||||
manifest.size,
|
||||
manifest.sha256.to_ascii_lowercase()
|
||||
)
|
||||
}
|
||||
|
||||
pub fn verify_manifest(manifest: &UpdateManifest, key: &VerifyingKey) -> Result<(), String> {
|
||||
if manifest.schema_version != 1
|
||||
|| semver::Version::parse(
|
||||
manifest
|
||||
.version
|
||||
.strip_prefix('v')
|
||||
.unwrap_or(&manifest.version),
|
||||
)
|
||||
.is_err()
|
||||
|| !safe_asset_name(&manifest.asset)
|
||||
|| manifest.size == 0
|
||||
|| manifest.size > MAXIMUM_UPDATE_BYTES
|
||||
|| manifest.sha256.len() != 64
|
||||
|| !manifest.sha256.bytes().all(|byte| byte.is_ascii_hexdigit())
|
||||
{
|
||||
return Err("Signed update metadata fields are invalid".to_owned());
|
||||
}
|
||||
let bytes = BASE64
|
||||
.decode(&manifest.signature)
|
||||
.map_err(|_| "Update signature is not valid base64")?;
|
||||
let signature =
|
||||
Signature::from_slice(&bytes).map_err(|_| "Update signature has the wrong length")?;
|
||||
key.verify_strict(signature_payload(manifest).as_bytes(), &signature)
|
||||
.map_err(|_| "Update manifest signature is invalid".to_owned())
|
||||
}
|
||||
|
||||
pub fn safe_asset_name(value: &str) -> bool {
|
||||
!value.is_empty()
|
||||
&& value.len() <= 240
|
||||
&& !value.contains("..")
|
||||
&& value
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'-' | b'_'))
|
||||
}
|
||||
|
||||
pub fn verify_signed_manifest(bytes: &[u8]) -> Result<UpdateManifest, String> {
|
||||
if bytes.len() as u64 > MAXIMUM_MANIFEST_BYTES {
|
||||
return Err("Update manifest exceeds its size limit".to_owned());
|
||||
}
|
||||
let manifest = serde_json::from_slice(bytes)
|
||||
.map_err(|error| format!("Invalid update manifest: {error}"))?;
|
||||
verify_manifest(&manifest, &embedded_update_key()?)?;
|
||||
Ok(manifest)
|
||||
}
|
||||
|
||||
pub fn verify_package(path: &Path, manifest: &UpdateManifest) -> Result<(), String> {
|
||||
if path.file_name().and_then(|name| name.to_str()) != Some(manifest.asset.as_str()) {
|
||||
return Err("Update package name does not match its signed manifest".to_owned());
|
||||
}
|
||||
let metadata = std::fs::symlink_metadata(path).map_err(|error| error.to_string())?;
|
||||
if !metadata.is_file() || metadata.len() != manifest.size {
|
||||
return Err(
|
||||
"Update package size or file type does not match its signed manifest".to_owned(),
|
||||
);
|
||||
}
|
||||
let mut file = File::open(path).map_err(|error| error.to_string())?;
|
||||
let (size, hash) = hash_reader(&mut file, MAXIMUM_UPDATE_BYTES)?;
|
||||
if size != manifest.size || !hash.eq_ignore_ascii_case(&manifest.sha256) {
|
||||
return Err("Update package SHA256 does not match its signed manifest".to_owned());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn hash_reader(reader: &mut impl Read, maximum: u64) -> Result<(u64, String), String> {
|
||||
let mut hasher = Sha256::new();
|
||||
let mut total = 0u64;
|
||||
let mut buffer = [0u8; 64 * 1024];
|
||||
loop {
|
||||
let length = reader
|
||||
.read(&mut buffer)
|
||||
.map_err(|error| error.to_string())?;
|
||||
if length == 0 {
|
||||
break;
|
||||
}
|
||||
total = total
|
||||
.checked_add(length as u64)
|
||||
.ok_or("Update size overflow")?;
|
||||
if total > maximum {
|
||||
return Err("Update data exceeds its size limit".to_owned());
|
||||
}
|
||||
hasher.update(&buffer[..length]);
|
||||
}
|
||||
Ok((total, format!("{:x}", hasher.finalize())))
|
||||
}
|
||||
+2009
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,40 @@
|
||||
pub const APPLICATION_VERSION: &str = match option_env!("OPENNOW_BUILD_VERSION") {
|
||||
Some(version) => version,
|
||||
None => env!("CARGO_PKG_VERSION"),
|
||||
};
|
||||
|
||||
pub fn update_channel(version: &str) -> &'static str {
|
||||
if semver::Version::parse(version)
|
||||
.is_ok_and(|version| version.pre.as_str().split('.').next() == Some("nightly"))
|
||||
{
|
||||
"nightly"
|
||||
} else {
|
||||
"stable"
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn application_version_is_semver() {
|
||||
semver::Version::parse(APPLICATION_VERSION).expect("application version must be semver");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_channel_follows_build_identity() {
|
||||
assert_eq!(update_channel("1.0.0-nightly.123.2"), "nightly");
|
||||
for version in ["1.0.0", "1.0.0-beta.1", "1.0.0+nightly", "invalid"] {
|
||||
assert_eq!(update_channel(version), "stable");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nightly_version_preserves_run_and_attempt() {
|
||||
let version = semver::Version::parse("1.0.0-nightly.123456.2").unwrap();
|
||||
assert_eq!((version.major, version.minor, version.patch), (1, 0, 0));
|
||||
assert_eq!(version.pre.as_str(), "nightly.123456.2");
|
||||
assert_eq!(version.to_string(), "1.0.0-nightly.123456.2");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,390 @@
|
||||
use serde_json::{Value, json};
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
use std::process::{Command, Stdio};
|
||||
use std::sync::mpsc;
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
fn hello(version: u32) -> Value {
|
||||
let unique = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos();
|
||||
let directory = std::env::temp_dir().join(format!("opennow-protocol-{version}-{unique}"));
|
||||
let mut child = Command::new(env!("CARGO_BIN_EXE_opennow-core"))
|
||||
.arg("--data-dir")
|
||||
.arg(&directory)
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
writeln!(
|
||||
child.stdin.as_mut().unwrap(),
|
||||
"{}",
|
||||
json!({
|
||||
"type":"request","id":"hello","method":"core.hello",
|
||||
"params":{"protocolVersion":version,"shell":"qt"}
|
||||
})
|
||||
)
|
||||
.unwrap();
|
||||
let stdout = child.stdout.take().unwrap();
|
||||
let (sender, receiver) = mpsc::channel();
|
||||
let reader = std::thread::spawn(move || {
|
||||
let mut line = String::new();
|
||||
let result = BufReader::new(stdout).read_line(&mut line).map(|_| line);
|
||||
sender.send(result).unwrap();
|
||||
});
|
||||
let response = receiver.recv_timeout(Duration::from_secs(10));
|
||||
let _ = child.kill();
|
||||
child.wait().unwrap();
|
||||
reader.join().unwrap();
|
||||
if directory.exists() {
|
||||
std::fs::remove_dir_all(directory).unwrap();
|
||||
}
|
||||
serde_json::from_str(&response.expect("core handshake timed out").unwrap()).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn protocol_three_shells_are_rejected_before_the_paged_library_contract() {
|
||||
let response = hello(3);
|
||||
assert_eq!(response["ok"], false);
|
||||
assert_eq!(response["error"]["code"], "incompatible_protocol");
|
||||
assert!(response.get("result").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn protocol_four_shells_receive_the_paged_library_capabilities() {
|
||||
let response = hello(4);
|
||||
assert_eq!(response["ok"], false);
|
||||
assert_eq!(response["error"]["code"], "incompatible_protocol");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn protocol_five_shells_receive_the_paged_library_capabilities() {
|
||||
let response = hello(5);
|
||||
assert_eq!(response["ok"], true);
|
||||
assert_eq!(response["result"]["protocolVersion"], 5);
|
||||
let capabilities = response["result"]["capabilities"].as_array().unwrap();
|
||||
for capability in [
|
||||
"catalog.libraryPages.v1",
|
||||
"catalog.metadata.v1",
|
||||
"account.syncObservation.v1",
|
||||
"account.pushInvalidation.v1",
|
||||
"catalog.languages.v1",
|
||||
"queue.servers.v1",
|
||||
"auth.accountPicker.v1",
|
||||
"session.terminalCleanup.v1",
|
||||
] {
|
||||
assert!(capabilities.contains(&json!(capability)));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn writable_cores_exclusively_own_the_resolved_profile_until_exit() {
|
||||
let unique = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos();
|
||||
let directory = std::env::temp_dir().join(format!("opennow-profile-lock-{unique}"));
|
||||
let other_directory = directory.join("other-profile");
|
||||
let mut first = Command::new(env!("CARGO_BIN_EXE_opennow-core"))
|
||||
.args(["--data-dir", directory.to_str().unwrap()])
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
writeln!(first.stdin.as_mut().unwrap(), "{}", json!({
|
||||
"type":"request","id":"owner","method":"core.hello","params":{"protocolVersion":5,"shell":"qt"}
|
||||
})).unwrap();
|
||||
let stdout = first.stdout.take().unwrap();
|
||||
let (sender, receiver) = mpsc::channel();
|
||||
let reader = std::thread::spawn(move || {
|
||||
let mut line = String::new();
|
||||
let _ = sender.send(BufReader::new(stdout).read_line(&mut line).map(|_| line));
|
||||
});
|
||||
let ready = receiver.recv_timeout(Duration::from_secs(10));
|
||||
let launch = |path: &std::path::Path, graphics: bool| {
|
||||
let mut command = Command::new(env!("CARGO_BIN_EXE_opennow-core"));
|
||||
command.arg("--data-dir").arg(path).stdin(Stdio::null());
|
||||
if graphics {
|
||||
command.arg("--graphics-preferences");
|
||||
}
|
||||
command.output().unwrap()
|
||||
};
|
||||
let second = launch(&directory, false);
|
||||
let independent = launch(&other_directory, false);
|
||||
let read_only = launch(&directory, true);
|
||||
first.kill().unwrap();
|
||||
first.wait().unwrap();
|
||||
reader.join().unwrap();
|
||||
let recovered = launch(&directory, false);
|
||||
std::fs::remove_dir_all(&directory).unwrap();
|
||||
let hello: Value =
|
||||
serde_json::from_str(&ready.expect("owner handshake timed out").unwrap()).unwrap();
|
||||
assert_eq!(hello["ok"], true);
|
||||
assert!(
|
||||
!second.status.success(),
|
||||
"a second writable core admitted the same profile"
|
||||
);
|
||||
assert!(String::from_utf8_lossy(&second.stderr).contains("data directory is already in use"));
|
||||
assert!(
|
||||
independent.status.success(),
|
||||
"a distinct profile was blocked"
|
||||
);
|
||||
assert!(
|
||||
read_only.status.success(),
|
||||
"read-only graphics preferences were blocked"
|
||||
);
|
||||
assert!(
|
||||
recovered.status.success(),
|
||||
"process exit left a stale profile lock"
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(all(target_os = "linux", target_env = "gnu"))]
|
||||
mod pending_profile_owner {
|
||||
use super::*;
|
||||
use fs2::FileExt;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::{self, Read};
|
||||
use std::os::fd::{AsRawFd, FromRawFd};
|
||||
use std::os::unix::process::CommandExt;
|
||||
use std::process::Child;
|
||||
use std::thread::{self, JoinHandle};
|
||||
use std::time::Instant;
|
||||
|
||||
struct ReapedChild {
|
||||
child: Child,
|
||||
reader: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl Drop for ReapedChild {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.child.kill();
|
||||
let _ = self.child.wait();
|
||||
if let Some(reader) = self.reader.take() {
|
||||
let _ = reader.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn pipe() -> (File, File) {
|
||||
let mut descriptors = [-1; 2];
|
||||
assert_eq!(
|
||||
unsafe { libc::pipe2(descriptors.as_mut_ptr(), libc::O_CLOEXEC) },
|
||||
0
|
||||
);
|
||||
unsafe {
|
||||
(
|
||||
File::from_raw_fd(descriptors[0]),
|
||||
File::from_raw_fd(descriptors[1]),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn signal(reader: &mut File, expected: u8) {
|
||||
let mut descriptor = libc::pollfd {
|
||||
fd: reader.as_raw_fd(),
|
||||
events: libc::POLLIN,
|
||||
revents: 0,
|
||||
};
|
||||
assert_eq!(
|
||||
unsafe { libc::poll(&mut descriptor, 1, 10_000) },
|
||||
1,
|
||||
"fixture signal timed out"
|
||||
);
|
||||
let mut byte = [0];
|
||||
reader.read_exact(&mut byte).unwrap();
|
||||
assert_eq!(byte[0], expected);
|
||||
}
|
||||
|
||||
fn response(receiver: &mpsc::Receiver<io::Result<String>>, id: &str) -> Value {
|
||||
let deadline = Instant::now() + Duration::from_secs(10);
|
||||
loop {
|
||||
let line = receiver
|
||||
.recv_timeout(deadline.saturating_duration_since(Instant::now()))
|
||||
.expect("core response timed out")
|
||||
.unwrap();
|
||||
let value: Value = serde_json::from_str(&line).unwrap();
|
||||
if value["id"] == id {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pending_mutable_worker_keeps_profile_locked_after_main_loop_error() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let fixture = directory.path().join("profile-fsync-gate.so");
|
||||
let source = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/fixtures/profile_fsync_gate.c");
|
||||
let compiled = Command::new("cc")
|
||||
.args(["-shared", "-fPIC", "-Wall", "-Wextra", "-Werror"])
|
||||
.arg(source)
|
||||
.args(["-o"])
|
||||
.arg(&fixture)
|
||||
.arg("-ldl")
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(
|
||||
compiled.status.success(),
|
||||
"fsync fixture compilation failed"
|
||||
);
|
||||
let armed = directory.path().join("armed");
|
||||
let (mut ready_read, ready_write) = pipe();
|
||||
let (mut error_read, error_write) = pipe();
|
||||
let (release_read, mut release_write) = pipe();
|
||||
let (_stderr_read, mut stderr_write) = pipe();
|
||||
let inherited = [
|
||||
ready_write.as_raw_fd(),
|
||||
error_write.as_raw_fd(),
|
||||
release_read.as_raw_fd(),
|
||||
];
|
||||
let executable = std::env::var_os("OPENNOW_PROFILE_TEST_CORE")
|
||||
.unwrap_or_else(|| env!("CARGO_BIN_EXE_opennow-core").into());
|
||||
let mut command = Command::new(&executable);
|
||||
command
|
||||
.arg("--data-dir")
|
||||
.arg(directory.path())
|
||||
.env("LD_PRELOAD", fixture)
|
||||
.env("PROFILE_FSYNC_ARMED", &armed)
|
||||
.env("PROFILE_FSYNC_READY_FD", inherited[0].to_string())
|
||||
.env("PROFILE_ERROR_FD", inherited[1].to_string())
|
||||
.env("PROFILE_FSYNC_RELEASE_FD", inherited[2].to_string())
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::from(stderr_write.try_clone().unwrap()));
|
||||
unsafe {
|
||||
command.pre_exec(move || {
|
||||
for fd in inherited {
|
||||
if libc::fcntl(fd, libc::F_SETFD, 0) < 0 {
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
let mut first = ReapedChild {
|
||||
child: command.spawn().unwrap(),
|
||||
reader: None,
|
||||
};
|
||||
drop((ready_write, error_write, release_read));
|
||||
let stdout = first.child.stdout.take().unwrap();
|
||||
let (sender, receiver) = mpsc::channel();
|
||||
first.reader = Some(thread::spawn(move || {
|
||||
for line in BufReader::new(stdout).lines() {
|
||||
if sender.send(line).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}));
|
||||
writeln!(
|
||||
first.child.stdin.as_mut().unwrap(),
|
||||
"{}",
|
||||
json!({
|
||||
"type":"request", "id":"owner", "method":"core.hello",
|
||||
"params":{"protocolVersion":5,"shell":"qt"}
|
||||
})
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(response(&receiver, "owner")["ok"], true);
|
||||
|
||||
let fd = stderr_write.as_raw_fd();
|
||||
let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) };
|
||||
assert!(flags >= 0);
|
||||
assert_eq!(
|
||||
unsafe { libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK) },
|
||||
0
|
||||
);
|
||||
loop {
|
||||
match stderr_write.write(&[b'x'; 4096]) {
|
||||
Ok(count) => assert!(count > 0),
|
||||
Err(error) if error.kind() == io::ErrorKind::WouldBlock => break,
|
||||
Err(error) => panic!("could not fill fixture stderr pipe: {error}"),
|
||||
}
|
||||
}
|
||||
assert_eq!(unsafe { libc::fcntl(fd, libc::F_SETFL, flags) }, 0);
|
||||
File::create(&armed).unwrap();
|
||||
writeln!(
|
||||
first.child.stdin.as_mut().unwrap(),
|
||||
"{}",
|
||||
json!({
|
||||
"type":"request", "id":"width", "method":"settings.set",
|
||||
"params":{"key":"windowWidth","value":1234}
|
||||
})
|
||||
)
|
||||
.unwrap();
|
||||
signal(&mut ready_read, b's');
|
||||
writeln!(first.child.stdin.as_mut().unwrap(), "invalid-json").unwrap();
|
||||
signal(&mut error_read, b'e');
|
||||
assert!(first.child.try_wait().unwrap().is_none());
|
||||
|
||||
let mut second = ReapedChild {
|
||||
child: Command::new(&executable)
|
||||
.arg("--data-dir")
|
||||
.arg(directory.path())
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.unwrap(),
|
||||
reader: None,
|
||||
};
|
||||
let deadline = Instant::now() + Duration::from_secs(10);
|
||||
let status = loop {
|
||||
if let Some(status) = second.child.try_wait().unwrap() {
|
||||
break status;
|
||||
}
|
||||
assert!(Instant::now() < deadline, "second core did not exit");
|
||||
thread::sleep(Duration::from_millis(5));
|
||||
};
|
||||
let mut error = String::new();
|
||||
second
|
||||
.child
|
||||
.stderr
|
||||
.take()
|
||||
.unwrap()
|
||||
.read_to_string(&mut error)
|
||||
.unwrap();
|
||||
assert!(
|
||||
!status.success(),
|
||||
"a second writable core admitted a profile while a pending worker survived main-loop failure"
|
||||
);
|
||||
assert!(error.contains("data directory is already in use"));
|
||||
|
||||
release_write.write_all(b"r").unwrap();
|
||||
let saved = response(&receiver, "width");
|
||||
assert_eq!(saved["ok"], true);
|
||||
assert_eq!(saved["result"]["value"], 1234);
|
||||
let deadline = Instant::now() + Duration::from_secs(10);
|
||||
loop {
|
||||
let tasks = std::fs::read_dir(format!("/proc/{}/task", first.child.id())).unwrap();
|
||||
let rpc_running = tasks.filter_map(Result::ok).any(|entry| {
|
||||
std::fs::read_to_string(entry.path().join("comm"))
|
||||
.is_ok_and(|name| name.starts_with("opennow-rpc-"))
|
||||
});
|
||||
if !rpc_running {
|
||||
break;
|
||||
}
|
||||
assert!(Instant::now() < deadline, "RPC worker did not finish");
|
||||
thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
let lock = OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.open(directory.path().join("core.lock"))
|
||||
.unwrap();
|
||||
assert!(first.child.try_wait().unwrap().is_none());
|
||||
let error = lock
|
||||
.try_lock_exclusive()
|
||||
.expect_err("live core process released profile ownership after RPC teardown");
|
||||
assert_eq!(
|
||||
error.raw_os_error(),
|
||||
fs2::lock_contended_error().raw_os_error()
|
||||
);
|
||||
drop(first);
|
||||
lock.try_lock_exclusive()
|
||||
.expect("terminated core retained profile ownership");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/uio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static void report_main_error(int fd, const void *data, size_t length)
|
||||
{
|
||||
static int reported;
|
||||
if (fd != STDERR_FILENO || !memmem(data, length, "opennow-core:", 13)) return;
|
||||
const char *signal_fd = getenv("PROFILE_ERROR_FD");
|
||||
if (!signal_fd || __atomic_exchange_n(&reported, 1, __ATOMIC_SEQ_CST)) return;
|
||||
ssize_t (*real_write)(int, const void *, size_t) = dlsym(RTLD_NEXT, "write");
|
||||
const char signal = 'e';
|
||||
real_write(atoi(signal_fd), &signal, 1);
|
||||
}
|
||||
|
||||
ssize_t write(int fd, const void *data, size_t length)
|
||||
{
|
||||
ssize_t (*real_write)(int, const void *, size_t) = dlsym(RTLD_NEXT, "write");
|
||||
report_main_error(fd, data, length);
|
||||
return real_write(fd, data, length);
|
||||
}
|
||||
|
||||
ssize_t writev(int fd, const struct iovec *vectors, int count)
|
||||
{
|
||||
ssize_t (*real_writev)(int, const struct iovec *, int) = dlsym(RTLD_NEXT, "writev");
|
||||
for (int i = 0; i < count; ++i)
|
||||
report_main_error(fd, vectors[i].iov_base, vectors[i].iov_len);
|
||||
return real_writev(fd, vectors, count);
|
||||
}
|
||||
|
||||
int fsync(int fd)
|
||||
{
|
||||
static int entered;
|
||||
int (*real_fsync)(int) = dlsym(RTLD_NEXT, "fsync");
|
||||
int result = real_fsync(fd);
|
||||
int saved_errno = errno;
|
||||
const char *armed = getenv("PROFILE_FSYNC_ARMED");
|
||||
char link[64], path[4096];
|
||||
snprintf(link, sizeof(link), "/proc/self/fd/%d", fd);
|
||||
ssize_t length = readlink(link, path, sizeof(path) - 1);
|
||||
if (length > 0) path[length] = 0;
|
||||
if (armed && access(armed, F_OK) == 0 && length > 0
|
||||
&& strstr(path, "/settings.json.tmp")
|
||||
&& !__atomic_exchange_n(&entered, 1, __ATOMIC_SEQ_CST)) {
|
||||
const char *ready_fd = getenv("PROFILE_FSYNC_READY_FD");
|
||||
const char *release_fd = getenv("PROFILE_FSYNC_RELEASE_FD");
|
||||
if (ready_fd && release_fd) {
|
||||
const char signal = 's';
|
||||
write(atoi(ready_fd), &signal, 1);
|
||||
char release;
|
||||
while (read(atoi(release_fd), &release, 1) < 0 && errno == EINTR) {}
|
||||
}
|
||||
}
|
||||
errno = saved_errno;
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
use serde_json::json;
|
||||
use std::fs;
|
||||
use std::process::Command;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
#[test]
|
||||
fn graphics_preferences_cli_prints_one_exact_json_line_without_mutating_settings() {
|
||||
let unique = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos();
|
||||
let directory = std::env::temp_dir().join(format!("opennow-graphics-cli-{unique}"));
|
||||
let path = directory.join("settings.json");
|
||||
fs::create_dir_all(&directory).unwrap();
|
||||
let persisted = serde_json::to_vec(&json!({
|
||||
"windowsGpuDeviceId":"gpu-ü-1",
|
||||
"mouseAcceleration":true,
|
||||
"futureSecret":"do-not-print"
|
||||
}))
|
||||
.unwrap();
|
||||
fs::write(&path, &persisted).unwrap();
|
||||
|
||||
let mut explicit = Command::new(env!("CARGO_BIN_EXE_opennow-core"));
|
||||
explicit
|
||||
.arg("--graphics-preferences")
|
||||
.arg("--data-dir")
|
||||
.arg(&directory);
|
||||
let mut environment = Command::new(env!("CARGO_BIN_EXE_opennow-core"));
|
||||
environment
|
||||
.arg("--graphics-preferences")
|
||||
.env("OPENNOW_DATA_DIR", &directory);
|
||||
|
||||
for mut command in [explicit, environment] {
|
||||
let output = command.output().unwrap();
|
||||
assert!(output.status.success());
|
||||
assert_eq!(
|
||||
output.stdout,
|
||||
"{\"version\":1,\"windowsGpuDeviceId\":\"gpu-ü-1\"}\n".as_bytes()
|
||||
);
|
||||
assert!(output.stderr.is_empty());
|
||||
}
|
||||
assert_eq!(fs::read(&path).unwrap(), persisted);
|
||||
assert!(!directory.join("settings.json.bak").exists());
|
||||
assert!(!directory.join("settings.json.tmp").exists());
|
||||
assert!(!directory.join("settings.json.corrupt").exists());
|
||||
assert_eq!(fs::read_dir(&directory).unwrap().count(), 1);
|
||||
fs::remove_dir_all(directory).unwrap();
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
use serde_json::Value;
|
||||
use std::collections::HashSet;
|
||||
|
||||
const MANIFEST: &str = include_str!("../contracts/legacy-open-now-api.json");
|
||||
const SCHEMA: &str = include_str!("../contracts/legacy-open-now-api.schema.json");
|
||||
const CORE_DISPATCH: &str = include_str!("../src/main.rs");
|
||||
|
||||
#[test]
|
||||
fn legacy_api_manifest_is_complete_unique_and_fixture_backed() {
|
||||
let manifest: Value = serde_json::from_str(MANIFEST).expect("contract manifest must be JSON");
|
||||
let schema: Value = serde_json::from_str(SCHEMA).expect("contract schema must be JSON");
|
||||
assert_eq!(manifest["version"], 1);
|
||||
assert_eq!(schema["properties"]["operations"]["minItems"], 109);
|
||||
let expected_columns = [
|
||||
"legacy",
|
||||
"kind",
|
||||
"requestSchema",
|
||||
"resultSchema",
|
||||
"owner",
|
||||
"replacement",
|
||||
"status",
|
||||
"requestFixture",
|
||||
"resultFixture",
|
||||
];
|
||||
assert_eq!(
|
||||
manifest["columns"].as_array().expect("columns"),
|
||||
&expected_columns.map(Value::from)
|
||||
);
|
||||
|
||||
let operations = manifest["operations"].as_array().expect("operations array");
|
||||
assert_eq!(
|
||||
operations.len(),
|
||||
109,
|
||||
"OpenNowApi surface changed without a contract update"
|
||||
);
|
||||
let mut names = HashSet::new();
|
||||
for operation in operations {
|
||||
let fields = operation.as_array().expect("operation tuple");
|
||||
assert_eq!(fields.len(), 9);
|
||||
let name = fields[0].as_str().expect("legacy name");
|
||||
assert!(names.insert(name), "duplicate legacy operation {name}");
|
||||
assert!(matches!(
|
||||
fields[1].as_str(),
|
||||
Some("request" | "send" | "event")
|
||||
));
|
||||
assert!(
|
||||
!fields[2].as_str().unwrap_or_default().is_empty(),
|
||||
"{name} request schema"
|
||||
);
|
||||
assert!(
|
||||
!fields[3].as_str().unwrap_or_default().is_empty(),
|
||||
"{name} result schema"
|
||||
);
|
||||
assert!(matches!(
|
||||
fields[4].as_str(),
|
||||
Some("core" | "qt" | "streamer")
|
||||
));
|
||||
assert!(
|
||||
!fields[5].as_str().unwrap_or_default().is_empty(),
|
||||
"{name} replacement"
|
||||
);
|
||||
assert!(matches!(
|
||||
fields[6].as_str(),
|
||||
Some("ported" | "superseded" | "pending")
|
||||
));
|
||||
assert!(fields.get(7).is_some(), "{name} request fixture");
|
||||
assert!(fields.get(8).is_some(), "{name} result fixture");
|
||||
let serialized = serde_json::to_string(operation).expect("fixture must serialize");
|
||||
for secret in ["access_token", "refresh_token", "client_token", "password"] {
|
||||
assert!(
|
||||
!serialized.to_ascii_lowercase().contains(secret),
|
||||
"{name} fixture leaks {secret}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_api_manifest_has_no_pending_implementation() {
|
||||
let manifest: Value = serde_json::from_str(MANIFEST).expect("contract manifest must be JSON");
|
||||
for operation in manifest["operations"].as_array().expect("operations array") {
|
||||
let fields = operation.as_array().expect("operation tuple");
|
||||
assert_ne!(
|
||||
fields[6].as_str(),
|
||||
Some("pending"),
|
||||
"{} still has no Qt/Rust parity disposition",
|
||||
fields[0].as_str().unwrap_or("unnamed operation")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_ported_core_rpc_is_present_in_the_dispatcher() {
|
||||
let manifest: Value = serde_json::from_str(MANIFEST).unwrap();
|
||||
for operation in manifest["operations"].as_array().unwrap() {
|
||||
let fields = operation.as_array().unwrap();
|
||||
if fields[4] != "core" || fields[6] != "ported" {
|
||||
continue;
|
||||
}
|
||||
let replacement = fields[5].as_str().unwrap();
|
||||
for method in replacement.split(" + ") {
|
||||
if !method.contains('.') || method.contains(' ') {
|
||||
continue;
|
||||
}
|
||||
assert!(
|
||||
CORE_DISPATCH.contains(&format!("\"{method}\"")),
|
||||
"{} claims missing core method {method}",
|
||||
fields[0]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
use serde_json::{Value, json};
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
use std::process::{Child, ChildStdin, Command, Stdio};
|
||||
use std::sync::mpsc;
|
||||
use std::time::Duration;
|
||||
use tempfile::TempDir;
|
||||
|
||||
struct Session {
|
||||
child: Child,
|
||||
stdin: ChildStdin,
|
||||
lines: mpsc::Receiver<String>,
|
||||
_directory: TempDir,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
fn start(environment: &[(&str, &str)]) -> Self {
|
||||
let directory = TempDir::new().unwrap();
|
||||
let mut command = Command::new(env!("CARGO_BIN_EXE_opennow-core"));
|
||||
command
|
||||
.arg("--data-dir")
|
||||
.arg(directory.path())
|
||||
.env_remove("OPENNOW_PICTURES_DIR")
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::inherit());
|
||||
for (name, value) in environment {
|
||||
command.env(name, value);
|
||||
}
|
||||
let mut child = command.spawn().unwrap();
|
||||
let stdin = child.stdin.take().unwrap();
|
||||
let stdout = child.stdout.take().unwrap();
|
||||
let (sender, lines) = mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
for line in BufReader::new(stdout).lines() {
|
||||
let Ok(line) = line else { break };
|
||||
if sender.send(line).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
Self {
|
||||
child,
|
||||
stdin,
|
||||
lines,
|
||||
_directory: directory,
|
||||
}
|
||||
}
|
||||
|
||||
fn request(&mut self, id: &str, method: &str, params: Value) -> Value {
|
||||
writeln!(
|
||||
self.stdin,
|
||||
"{}",
|
||||
json!({"type":"request","id":id,"method":method,"params":params})
|
||||
)
|
||||
.unwrap();
|
||||
loop {
|
||||
let line = self
|
||||
.lines
|
||||
.recv_timeout(Duration::from_secs(10))
|
||||
.expect("core response timed out");
|
||||
let value: Value = serde_json::from_str(&line).unwrap();
|
||||
if value["type"] == "response" && value["id"] == id {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handshake(&mut self) {
|
||||
let response = self.request(
|
||||
"hello",
|
||||
"core.hello",
|
||||
json!({"protocolVersion":5,"shell":"qt"}),
|
||||
);
|
||||
assert_eq!(response["ok"], true);
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Session {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.child.kill();
|
||||
let _ = self.child.wait();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_pictures_override_keeps_unrelated_startup_working_while_media_is_unavailable() {
|
||||
let mut session = Session::start(&[("OPENNOW_PICTURES_DIR", "")]);
|
||||
session.handshake();
|
||||
let media = session.request("media", "media.root.get", json!({}));
|
||||
assert_eq!(media["ok"], false);
|
||||
assert_eq!(media["error"]["code"], "media_unavailable");
|
||||
let unrelated = session.request("thanks", "thanks.data.get", json!({}));
|
||||
assert_eq!(unrelated["ok"], true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_pictures_override_is_published_by_the_running_core() {
|
||||
let directory = TempDir::new().unwrap();
|
||||
{
|
||||
let mut session =
|
||||
Session::start(&[("OPENNOW_PICTURES_DIR", directory.path().to_str().unwrap())]);
|
||||
session.handshake();
|
||||
let media = session.request("media", "media.root.get", json!({}));
|
||||
assert_eq!(media["ok"], true);
|
||||
assert_eq!(
|
||||
media["result"]["path"],
|
||||
directory.path().join("OpenNOW").to_string_lossy().as_ref()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn absent_pictures_override_keeps_the_platform_fallback() {
|
||||
let home = TempDir::new().unwrap();
|
||||
{
|
||||
let mut session = Session::start(&[
|
||||
("HOME", home.path().to_str().unwrap()),
|
||||
("USERPROFILE", home.path().to_str().unwrap()),
|
||||
]);
|
||||
session.handshake();
|
||||
let media = session.request("media", "media.root.get", json!({}));
|
||||
assert_eq!(media["ok"], true);
|
||||
assert_eq!(
|
||||
media["result"]["path"],
|
||||
home.path()
|
||||
.join("Pictures")
|
||||
.join("OpenNOW")
|
||||
.to_string_lossy()
|
||||
.as_ref()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
use serde_json::{Value, json};
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
use std::process::{Command, Stdio};
|
||||
use std::sync::mpsc;
|
||||
use std::time::Duration;
|
||||
|
||||
#[test]
|
||||
fn settings_events_precede_acknowledgements_and_other_events_keep_their_order() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let mut child = Command::new(env!("CARGO_BIN_EXE_opennow-core"))
|
||||
.args(["--data-dir", directory.path().to_str().unwrap()])
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
let stdout = child.stdout.take().unwrap();
|
||||
let (sender, receiver) = mpsc::channel();
|
||||
let reader = std::thread::spawn(move || {
|
||||
for line in BufReader::new(stdout).lines() {
|
||||
let Ok(line) = line else { break };
|
||||
if sender
|
||||
.send(serde_json::from_str::<Value>(&line).unwrap())
|
||||
.is_err()
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
||||
let mut serial = 0;
|
||||
let mut request = |method: &str, params: Value| {
|
||||
serial += 1;
|
||||
let id = serial.to_string();
|
||||
writeln!(
|
||||
child.stdin.as_mut().unwrap(),
|
||||
"{}",
|
||||
json!({"type":"request","id":id,"method":method,"params":params})
|
||||
)
|
||||
.unwrap();
|
||||
id
|
||||
};
|
||||
let next = || {
|
||||
receiver
|
||||
.recv_timeout(Duration::from_secs(5))
|
||||
.expect("core response timed out")
|
||||
};
|
||||
for (key, value, changes) in [
|
||||
(
|
||||
"webrtcCompatibilityMode",
|
||||
json!("on"),
|
||||
Some(json!({"allianceWebrtcCompatibility":true})),
|
||||
),
|
||||
(
|
||||
"webrtcCompatibilityMode",
|
||||
json!("off"),
|
||||
Some(json!({"allianceWebrtcCompatibility":false})),
|
||||
),
|
||||
(
|
||||
"webrtcCompatibilityMode",
|
||||
json!("auto"),
|
||||
Some(json!({"allianceWebrtcCompatibility":false})),
|
||||
),
|
||||
(
|
||||
"allianceWebrtcCompatibility",
|
||||
json!(true),
|
||||
Some(json!({"webrtcCompatibilityMode":"on"})),
|
||||
),
|
||||
(
|
||||
"allianceWebrtcCompatibility",
|
||||
json!(false),
|
||||
Some(json!({"webrtcCompatibilityMode":"off"})),
|
||||
),
|
||||
("gameLanguage", json!("es_419"), None),
|
||||
("gameLanguage", json!("zh_Hant_TW"), None),
|
||||
("launchInConsoleMode", json!(true), None),
|
||||
(
|
||||
"launchInConsoleMode",
|
||||
json!(false),
|
||||
Some(json!({"switchToConsoleOnPad":false})),
|
||||
),
|
||||
(
|
||||
"themePack",
|
||||
json!("bone"),
|
||||
Some(json!({"appTheme":"light","themeAccentOverride":false})),
|
||||
),
|
||||
(
|
||||
"microphoneMode",
|
||||
json!("voice-activity"),
|
||||
Some(json!({"microphoneDeviceId":""})),
|
||||
),
|
||||
] {
|
||||
let id = request("settings.set", json!({"key":key,"value":value}));
|
||||
let event = next();
|
||||
assert_eq!(
|
||||
event["type"], "event",
|
||||
"a queued next write must not start before this event"
|
||||
);
|
||||
assert_eq!(event["name"], "settings.changed");
|
||||
assert_eq!(event["payload"]["key"], key);
|
||||
assert_eq!(event["payload"]["value"], value);
|
||||
if let Some(changes) = changes {
|
||||
assert_eq!(event["payload"]["changes"], changes);
|
||||
}
|
||||
let response = next();
|
||||
assert_eq!(response["id"], id);
|
||||
assert_eq!(response["ok"], true);
|
||||
assert_eq!(response["result"], event["payload"]);
|
||||
}
|
||||
let id = request("settings.set", json!({"key":"gameLanguage","value":"auto"}));
|
||||
let rejected = next();
|
||||
assert_eq!(rejected["id"], id);
|
||||
assert_eq!(rejected["ok"], false);
|
||||
let id = request("settings.reset", json!({}));
|
||||
let response = next();
|
||||
assert_eq!(response["id"], id);
|
||||
assert_eq!(response["ok"], true);
|
||||
assert_eq!(next()["name"], "settings.reset");
|
||||
}));
|
||||
let _ = child.kill();
|
||||
child.wait().unwrap();
|
||||
reader.join().unwrap();
|
||||
if let Err(error) = result {
|
||||
std::panic::resume_unwind(error);
|
||||
}
|
||||
}
|
||||
+3176
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,75 @@
|
||||
[workspace]
|
||||
members = [
|
||||
"crates/opennow-awdl-helper",
|
||||
"crates/opennow-streamer",
|
||||
"crates/opennow-streamer-core",
|
||||
"crates/opennow-streamer-ffi",
|
||||
"crates/opennow-streamer-hid",
|
||||
"crates/opennow-streamer-platform",
|
||||
"crates/opennow-streamer-platform-linux",
|
||||
"crates/opennow-streamer-platform-macos",
|
||||
"crates/opennow-streamer-platform-windows",
|
||||
"crates/opennow-streamer-protocol",
|
||||
"crates/opennow-streamer-transport",
|
||||
]
|
||||
default-members = ["crates/opennow-streamer"]
|
||||
exclude = ["vendor/cros-codecs-extended"]
|
||||
resolver = "2"
|
||||
|
||||
[workspace.package]
|
||||
version = "0.2.0"
|
||||
edition = "2024"
|
||||
license = "MIT"
|
||||
rust-version = "1.85"
|
||||
|
||||
[workspace.dependencies]
|
||||
aes = "0.8"
|
||||
aes-gcm = "0.10"
|
||||
audiopus_sys = { version = "0.2.2", features = ["static"] }
|
||||
base64 = "0.22"
|
||||
ctr = "0.9"
|
||||
crc32fast = "1"
|
||||
getrandom = "0.3"
|
||||
ghash = "0.5"
|
||||
hmac = "0.12"
|
||||
image = { version = "0.25", default-features = false, features = ["bmp", "ico", "png"] }
|
||||
mtu = "0.3"
|
||||
openh264 = { version = "0.9.8", default-features = false, features = ["source"] }
|
||||
oxideav-core = "0.1.34"
|
||||
oxideav-mkv = "0.0.9"
|
||||
opus = "0.3.1"
|
||||
# Pin rust-sdl2 after its bundled SDL2 update. SDL 2.26.4 from the crates.io
|
||||
# release does not compile against current PipeWire headers with modern GCC.
|
||||
#
|
||||
# This is upstream adeb978d plus a cherry-pick of upstream #1512, which Windows
|
||||
# needs because clang types C enums as `int` for MSVC and `unsigned` for Linux
|
||||
# and `set_mod_state` only accepted the Linux form. The fix cannot be taken by
|
||||
# moving the pin forward: the commit before it rewrote `Texture<'a>` to
|
||||
# `Texture<'_>` inside the `unsafe_textures` block, where `Texture` has no
|
||||
# lifetime, so every revision from there through master fails to build with the
|
||||
# feature we rely on. Drop this fork once upstream repairs `unsafe_textures`.
|
||||
sdl2 = { git = "https://github.com/zortos293/rust-sdl2.git", rev = "8c812c4a0087eaf664a11a844634200cebe9afb8", default-features = false, features = ["bundled", "raw-window-handle", "static-link", "unsafe_textures"] }
|
||||
scuffle-av1 = "0.1.4"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
sha1 = "0.10"
|
||||
socket2 = { version = "0.5", features = ["all"] }
|
||||
str0m = { version = "0.23", default-features = false }
|
||||
subtle = "2"
|
||||
thiserror = "2"
|
||||
rustls = { version = "0.23", default-features = false, features = ["std", "ring", "tls12"] }
|
||||
tungstenite = { version = "0.28", default-features = false, features = ["handshake", "rustls-tls-native-roots"] }
|
||||
|
||||
[profile.release]
|
||||
lto = "thin"
|
||||
codegen-units = 1
|
||||
strip = true
|
||||
|
||||
[patch.crates-io]
|
||||
str0m = { path = "vendor/str0m" }
|
||||
# Always compile the vendored Opus sources for the distributable streamer.
|
||||
audiopus_sys = { path = "vendor/audiopus-sys" }
|
||||
# Upstream does not expose an NVDEC-only bundled build. This patch keeps CUDA
|
||||
# and nvcuvid as driver-loaded interfaces without requiring the CUDA toolkit.
|
||||
ffmpeg-sys-next = { path = "vendor/ffmpeg-sys-next" }
|
||||
cros-codecs-extended = { path = "vendor/cros-codecs-extended" }
|
||||
+315
@@ -0,0 +1,315 @@
|
||||
# OpenNOW native streamer
|
||||
|
||||
This workspace implements the native GeForce NOW NVST runtime. It owns RTSPS negotiation, the dedicated Mjolnir SRTP video socket, the NVST ICE/DTLS/SCTP bundle used for audio, microphone, RTCP and input, bounded media queues, platform decode/audio output, source-stream Matroska recording and GPU frame publication. It does not implement the browser WebRTC offer/answer or trickle-ICE protocol.
|
||||
|
||||
The Qt application loads `opennow-streamer-ffi` as an in-process shared library. Qt supplies one complete CloudMatch session context; the embedded engine reserves its bundle and Mjolnir sockets and performs OPTIONS, DESCRIBE, SETUP, ANNOUNCE, PLAY, keepalive and TEARDOWN. The runtime does not load or redistribute NVIDIA client libraries.
|
||||
|
||||
## Crates
|
||||
|
||||
### Hardware selection in the Qt app
|
||||
|
||||
Stream settings default to `nativeVideoBackend: auto`. On Windows the embedded presenter
|
||||
currently uses D3D11; its picker offers an explicit DX11 override and shows DX12/Vulkan as
|
||||
unavailable. Standalone D3D12 support does **not** imply embedded D3D12 texture interop.
|
||||
Unsupported forced backends fail explicitly; they never silently select a different API.
|
||||
On Linux supported decoder overrides are passed per session, without changing process-global
|
||||
environment variables. Qt's compositor still uses the platform-native presentation API.
|
||||
|
||||
Windows codec detection requires both a usable Media Foundation transform and a matching
|
||||
DXVA decoder profile/configuration on the probed GPU. Decoder startup repeats the check on
|
||||
Qt's adopted device with the actual codec, dimensions, bit depth and chroma. Installing an
|
||||
AV1 codec package on a GPU without AV1 hardware support no longer makes it hardware-capable.
|
||||
The core resolves Auto against the embedded capability report before CloudMatch allocation.
|
||||
This is capability selection, not an automatic reconnect or codec change inside a live stream.
|
||||
|
||||
GPU import/shader failures are reported to the shell and Qt log. To diagnose an individual
|
||||
black-screen report, collect `diagnostics/native-streamer.log` plus the Qt log and distinguish
|
||||
an entirely black application window from a stream-only black video surface.
|
||||
|
||||
### Workspace components
|
||||
|
||||
- `opennow-streamer-protocol`: versioned local command and session DTOs.
|
||||
- `opennow-streamer-core`: NVST lifecycle, command routing, media feedback and recording.
|
||||
- `opennow-streamer-transport`: Mjolnir SRTP plus the NVST-required ICE/DTLS/SCTP, RTCP and input implementation.
|
||||
- `opennow-streamer-platform`: bounded media queues, decode/audio output, recording and GPU-frame publication.
|
||||
- `opennow-streamer-platform-{windows,macos,linux}`: platform decoders and native GPU texture producers.
|
||||
- `opennow-streamer-ffi`: bounded C ABI used in process by Qt.
|
||||
- `opennow-streamer`: a development JSON-lines host for the same engine; Qt packages do not include it.
|
||||
|
||||
## Qt GPU integration
|
||||
|
||||
The Qt path does not create an SDL video window or a child streamer process. `NativeStreamRuntime` owns the embedded Rust handle, and `StreamVideoItem` drives the GPU-only FFI from Qt's render thread:
|
||||
|
||||
1. Qt lends the current QRhi native graphics objects to the runtime.
|
||||
2. Platform decode publishes a native GPU frame into a one-frame, drop-stale mailbox.
|
||||
3. Qt acquires the latest opaque frame token and asks Rust to record conversion and synchronization into the same QRhi command buffer that will render it.
|
||||
4. Qt imports the returned RGBA8 native texture and samples it in the scene graph, so QML overlays compose above video normally.
|
||||
5. Qt releases the token after GPU use and shuts down the scene-graph binding on the render thread before QRhi teardown.
|
||||
|
||||
The FFI exposes no CPU image, encoded-frame callback, swap chain, window or Qt object. See `crates/opennow-streamer-ffi/README.md` for ownership and threading details.
|
||||
|
||||
### Local playback mute
|
||||
|
||||
Protocol 7 accepts `{"type":"setAudioMuted","id":"audio-mute-1","muted":true}`
|
||||
and returns the correlated `ok` response. `muted` must be a boolean; omitting it
|
||||
returns `missing-muted`. Set it to `false` to restore speaker playback.
|
||||
|
||||
The mute state belongs to the media runtime, defaults to false, and can be set
|
||||
before starting a session. It survives stops, reconnects, backend fallback, and
|
||||
audio device recovery within that runtime. Creating a new runtime resets it, so
|
||||
the shell must resend its desired state when its native runtime becomes ready.
|
||||
The FFI ABI is unchanged.
|
||||
|
||||
Mute replaces samples with silence only at SDL, ALSA/PipeWire, CoreAudio, and
|
||||
WASAPI playback boundaries. Playback queues keep draining; transport, Opus decode,
|
||||
recording, replay capture, microphone capture, and video remain active. Samples
|
||||
already handed to the operating system or device may finish playing during its
|
||||
existing bounded output latency; mute does not stop or flush those devices.
|
||||
|
||||
### Audio output selection
|
||||
|
||||
Protocol 5 accepts the additive request `{"type":"audioDevices","id":"audio-1"}`
|
||||
and returns `{"type":"audioDevices","id":"audio-1","devices":[{"id":"...","name":"..."}]}`.
|
||||
Failures return a correlated `error` with code `audio-devices-unavailable`; an empty
|
||||
successful list is not an enumeration failure. Queries run on the existing native
|
||||
host worker with a two-second response timeout and at most one outstanding query.
|
||||
Lists exceeding 512 KiB return an error rather than exceeding Qt's callback bound.
|
||||
Queries neither open a playback device
|
||||
nor pause, recreate, or stop the active session.
|
||||
|
||||
The optional session-context `settings.audioOutputDevice` string is an opaque ID
|
||||
from this response. An absent or empty string uses the system default and preserves
|
||||
existing backend fallback behavior. Nonempty IDs are used only when the next session
|
||||
starts, must contain no NUL and be at most 1024 UTF-8 bytes, and must identify exactly
|
||||
one available output. Missing or ambiguous fixed outputs fail explicitly rather than
|
||||
opening the default. Clients persist IDs, never list positions, device indices, or
|
||||
display names. Duplicate identities are excluded from enumeration.
|
||||
|
||||
The Qt embedded backends use their actual playback identities:
|
||||
|
||||
- Windows uses exact SDL2 playback device names. SDL playback and enumeration share
|
||||
the existing embedded host worker; Qt does not create a second audio owner.
|
||||
- Linux uses `pipewire:<node.name>` or `alsa:<PCM hint name>`. PipeWire enumeration
|
||||
requires `pw-dump` and is bounded to 1.5 seconds and 4 MiB; playback uses `pw-cat`.
|
||||
ALSA enumeration uses the playback PCM hints from the same dynamically loaded
|
||||
libasound backend. Default-routing aliases are excluded from fixed ALSA choices.
|
||||
Fixed outputs disable cross-backend fallback; PipeWire streams also set
|
||||
`node.dont-fallback` and `node.dont-reconnect`.
|
||||
- macOS uses `coreaudio:<device UID>` and passes that UID through native backend
|
||||
startup and audio recovery rather than persisting an AudioDeviceID.
|
||||
|
||||
The development standalone host uses SDL identities for SDL playback and CoreAudio
|
||||
identities for native macOS playback. Standalone Windows native WASAPI output does
|
||||
not support fixed SDL device selection; use the Qt embedded path instead.
|
||||
The startup selection guarantee does not promise that the operating system will
|
||||
never reroute an already-playing stream after device loss.
|
||||
|
||||
This extension does not change protocol or FFI ABI versions: old clients omit the
|
||||
setting and keep default behavior, and new clients must tolerate `unknown-command`
|
||||
from older runtimes that do not implement enumeration.
|
||||
|
||||
## Packaging
|
||||
|
||||
The Qt CMake build always compiles `opennow-streamer-ffi` with Cargo's release profile and links the resulting shared library to `opennow-qt`. CPack installs that library beside the Qt executable and `opennow-core`; there is no separate streamer executable, helper application or native video window in the Qt package. Linux packages enable the bundled FFmpeg fallback while optional GPU driver interfaces remain dynamically discovered.
|
||||
|
||||
The standalone `opennow-streamer` binary remains a development host and is not evidence for the packaged Qt presentation path.
|
||||
|
||||
### Embedded latency and resource bounds
|
||||
|
||||
- Windows decoder workers wait for actionable input or a bounded 1 ms output
|
||||
poll; queued frames do not wake a worker that has no decoder input credits.
|
||||
Closing the queue interrupts either wait immediately.
|
||||
- Windows conversion targets are allocated lazily for the QRhi slots actually
|
||||
used, with an eight-slot upper bound. Format changes discard the old slot set.
|
||||
- Stereo SDL playout retains the 120 ms overflow bound. A backlog above 60 ms
|
||||
lasting 200 ms enables allocation-free catch-up resampling (at most 2% faster)
|
||||
until the queued tail reaches 40 ms. Normal playback is bit-exact; clearing
|
||||
the buffer resets recovery. This controls the application queue, not latency
|
||||
inside the OS audio device, and the temporary correction can slightly alter pitch.
|
||||
- Qt samples the converted surface directly in its scene pass; it does not
|
||||
allocate a second video-sized intermediate render target. Native conversion
|
||||
and Qt drawing remain on the same graphics command stream.
|
||||
|
||||
## Checks
|
||||
|
||||
### Microphone upstream
|
||||
|
||||
Microphone capture defaults off. The `voice-activity` settings value is exposed as
|
||||
**Open microphone**: it sends continuous audio, not a voice-activity detector or
|
||||
push-to-talk implementation. Select it before starting a session; NVST does not
|
||||
renegotiate microphone support during a stream. Capture uses the system default
|
||||
input device. A nonempty `microphoneDeviceId` is rejected rather than silently
|
||||
capturing a different device.
|
||||
|
||||
The native RTSP path requires the server's DESCRIBE to advertise
|
||||
`x-nv-general.rtcMicOnNativeBundle:1`. Only then, and with microphone opt-in,
|
||||
ANNOUNCE includes that flag and `x-nv-mic.micSsrcConfig.senderSsrc:1`. The existing
|
||||
str0m bundle sends the negotiated microphone Opus payload type on SSRC 1,
|
||||
independently of downlink audio. Handoffs without a microphone payload type retain
|
||||
PT 111. When the handoff supplies a RED payload type and a nonzero redundancy
|
||||
level, the sender wraps Opus in RFC 2198 RED with up to three previous frames.
|
||||
History is cleared on mute/unmute, and unrepresentable offsets, block lengths,
|
||||
or packets above the payload budget reduce redundancy rather than dropping the
|
||||
primary frame. This follows the native bundle findings documented in OpenNOW-Mac's
|
||||
`docs/StreamTransportArchitecture.md` (reference revision `88a09bd68598651b367aa4744e7528da9c074d28`).
|
||||
Legacy RTSP/UDP microphone carriage is not implemented.
|
||||
|
||||
Capture produces mono 48 kHz PCM and an off-callback encoder produces 10 ms Opus
|
||||
frames at 16 kbps. The frame size must match the server's `mic.frameSize:10`: the
|
||||
cloud PC's microphone stays silent when it receives 20 ms frames. PCM, encoded audio, and transport queues each hold at most five
|
||||
frames and drop old data under load. Mute closes capture and clears pending audio;
|
||||
unmute preserves the RTP clock and transport sequence lifetime. Capture or uplink
|
||||
failure disables only the microphone, not game audio/video. Session termination
|
||||
also closes capture. Queue drops are reported in microphone diagnostics without
|
||||
logging audio contents.
|
||||
|
||||
Embedded capture devices are owned by the existing embedded runtime host, sharing
|
||||
SDL ownership with Windows playback and output-device enumeration. Engine keeps
|
||||
only the microphone session's shared control; the uplink worker receives encoded
|
||||
packets without owning SDL. Mute pauses hardware synchronously, and the host
|
||||
reclaims stopped capture on its next bounded poll. The host sleeps without a
|
||||
polling timer when no capture is active. Output enumeration and input-pause
|
||||
commands never open, mute, or restart capture. The actual audio-device-aware
|
||||
stream-start entry point resets the microphone clock; mute/unmute does not.
|
||||
When SDL reports an active capture device as disconnected, the same host closes
|
||||
it and tries the default endpoint again at 250 ms intervals for up to one second.
|
||||
PCM, encoded, and in-flight encoder generations are flushed during reopening.
|
||||
The original sample-clock origin is retained. Reopening failure disables only
|
||||
the mic. Disabled capture never opens or retries a device.
|
||||
|
||||
### Timestamp audio jitter buffer
|
||||
|
||||
Authenticated NVST audio RTP is reordered by sender timestamp and sequence before
|
||||
RED recovery and decoding. The network buffer starts at 45 ms and increases its
|
||||
target by 20 ms for a new late-arrival burst after playout starts, capped at 200 ms.
|
||||
It bounds timestamp span to 200 ms, packet count to 80, and individual payload
|
||||
size to the existing eight-redundant-block RED receive limit. Duplicates and stale
|
||||
originals are discarded; source changes reset the timeline. `audio-jitter` logs
|
||||
report the target, queue depth, reordering, stale/overflow/invalid drops, source
|
||||
resets, and underflow observations once a second without audio contents.
|
||||
|
||||
The Windows GFN logs establish an 80 ms initial target, a dynamic 45 ms floor,
|
||||
the ceiling, and the growth value, but not the complete target-reduction
|
||||
predicate. OpenNOW deliberately starts at that 45 ms floor rather than imposing
|
||||
80 ms permanently without a verified downward adaptation path. After N distinct
|
||||
underflow bursts, the target is `min(45 + 20*N, 200)` ms until a source reset.
|
||||
Downward adaptation remains deferred. This adds 45 ms before decode on a clean
|
||||
network, rising only on late bursts; the existing PCM capacity and roughly 40 ms
|
||||
catch-up target are unchanged. These are separate queues, so the addition is
|
||||
45 ms, not merely 5 ms compared with the PCM catch-up target. This is not a total
|
||||
end-to-end latency cap. OS endpoint buffering is outside the network buffer's
|
||||
bound.
|
||||
|
||||
The local JSON protocol is version 7; the C ABI is unchanged. `hello` exposes
|
||||
`supportsMicrophone` for runtime capture support, while the `start` response
|
||||
reports the negotiated session capability. `microphone-set` requires a boolean
|
||||
`enabled`; `microphone-toggle` toggles the current capture state. Both require an
|
||||
active, opted-in, negotiated session. `microphone-state` events contain `state`
|
||||
(`disabled`, `muted`, `ready`, `unavailable`, or `error`), `enabled`, and an optional
|
||||
`message`. `ready` confirms local capture/queueing, not that a remote game has
|
||||
selected the microphone.
|
||||
|
||||
The `start` command accepts an optional boolean `microphoneEnabled`. Setting it
|
||||
to false reserves negotiated microphone support but does not open capture; Qt
|
||||
uses this to retain mute during same-session recovery. It cannot override a
|
||||
disabled microphone mode or a server that does not support bundle capture.
|
||||
|
||||
Live release acceptance must confirm remote voice reception and uninterrupted
|
||||
game audio, then mute/unmute, disconnect the input device, end/restart the session,
|
||||
and deny OS permission. Use headphones: acoustic echo cancellation and noise
|
||||
suppression are not provided by this capture path.
|
||||
|
||||
### Embedded session diagnostics
|
||||
|
||||
Protocol 5 telemetry includes optional `jitterMs` (RTP interarrival jitter on
|
||||
the video 90 kHz clock) and `packetLossPercent` (authenticated RTP reception loss
|
||||
over the latest sampled interval, normally one second). Loss sampling requires
|
||||
new traffic and an interval between one and three seconds. The first sample
|
||||
establishes a baseline and returns null. A longer gap, stream change, or counter
|
||||
reset starts a new baseline. Late recovery cannot produce negative loss. Sampling
|
||||
does not change cumulative RTCP loss or advance RTCP report intervals. Qt forwards
|
||||
measured values without converting nulls into zeros.
|
||||
|
||||
Qt's shared connection-health state requires at least 0.5% loss sustained for two
|
||||
seconds before showing an unstable header or popup. A fresh sample below 0.1%
|
||||
restores healthy status and dismisses the loss popup. Missing or invalid samples,
|
||||
or five seconds without a fresh measurement, clear health to unknown. Historical
|
||||
video frame and audio packet drop totals remain unchanged. Loss popups keep their
|
||||
four-second lifetime and thirty-second cooldown across local overlay changes.
|
||||
|
||||
The optional `pingMs` field measures network round-trip time on the active session. It prefers
|
||||
the nominated ICE candidate pair's measured RTT, then authenticated STUN/NATT replies
|
||||
on the dedicated video socket or control/audio bundle. RTSPS keepalive response times
|
||||
are not used: they include application-level request handling and can overstate network latency.
|
||||
STUN replies must match an outstanding transaction, the peer address, fingerprint, and
|
||||
message integrity. Each receiver tracks at most 64 probes. ICE statistics only refresh
|
||||
the sample when the pair's response count changes; rereading old statistics cannot keep
|
||||
an old measurement alive.
|
||||
|
||||
The bundle remembers up to 64 emitted ICE transactions and forwards each authenticated
|
||||
success response to the ICE library only once. Delayed duplicate replies must not overwrite
|
||||
the original completion time and turn the age of an old probe into the displayed network RTT.
|
||||
This does not cap genuine network latency or filter first replies based on their timing.
|
||||
|
||||
Session liveness follows [OpenNOW-Mac's live-tested keepalive method](https://github.com/OpenCloudGaming/OpenNOW-Mac/blob/90627114383501dd18ef165baa005d9ea603fdf3/GFN/NVST/Rtsp/NvstRtspConnection.swift#L161-L234):
|
||||
send `GET_PARAMETER` with the RTSP Session header every two seconds over the existing
|
||||
RTSPS WebSocket, and match the response. A `551 Option Not Supported` response
|
||||
still completes the keepalive. Some seats do not answer STUN/ICE probes and close the
|
||||
connection on client WebSocket pings, so those pings are replaced by session-scoped
|
||||
RTSP keepalives. Server-initiated WebSocket pings are still answered with pongs.
|
||||
|
||||
Measurements expire after five seconds, and new sessions start without a sample.
|
||||
Missing or expired ping is emitted as null, including on seats that only answer RTSPS
|
||||
keepalives. No discovery-server ping, remote-clock
|
||||
assumptions, stale handshake timing, or synthesized ICE replies supply this metric.
|
||||
Ping is network RTT, not control-request, decode, one-way video, or input-to-display latency. Decode duration
|
||||
and end-to-end latency remain unavailable without a measurement source; Qt hides those
|
||||
two metrics when unavailable in the panel, compact bar, and copied statistics.
|
||||
|
||||
Queue discard events retain `type: "log"`, `event: "queue-dropped"`, `media`, and
|
||||
the per-source delta `count`. The additive `unit` field is `frames` for video
|
||||
decode/presentation sources, `samples` for `audio-output`, `packets` for compressed
|
||||
audio or queued PCM blocks, and `items` for unknown sources. `audio-output` also
|
||||
includes `sampleRate: 48000` and `channels: 2`; its interleaved sample count converts
|
||||
to milliseconds as `count * 1000 / (sampleRate * channels)`. Audio packet/block
|
||||
duration is unknown and must not be inferred from its count. Never sum unlike
|
||||
units or treat these queue-stage counters as unique lost video frames or network
|
||||
packet loss.
|
||||
|
||||
Pending deltas flush every second, including when no further drops occur. Full
|
||||
event queues retain pending counts for retry. Shutdown joins the media producers
|
||||
and drains their remaining drop feedback before final best-effort delivery.
|
||||
Final summaries never wait for event-queue capacity: undeliverable deltas are
|
||||
written directly to the native log with `delivery=event-queue-unavailable`, then
|
||||
removed. These deltas will be missing from callback/UI totals under saturation.
|
||||
Each delivered or directly logged final delta is removed, so it is never counted twice.
|
||||
Native-to-Qt file logs retain every delivered per-source delta and its unit,
|
||||
independently of periodic telemetry logging. Routine progress/file telemetry and
|
||||
repeated keyframe-attempt logs use ten-second intervals; errors, warnings and
|
||||
recovery transitions remain visible. Callback telemetry and wire feedback/retry
|
||||
cadences are unchanged.
|
||||
|
||||
During Windows decoder recreation, the worker retains one pending recovery
|
||||
keyframe outside the bounded input queue. Already-queued descendants remain in
|
||||
FIFO order instead of being silently cleared. This adds at most one owned access
|
||||
unit while the replacement MFT waits for input credits; it adds no presenter or
|
||||
CPU pixel-copy path. Regression coverage verifies reference-chain ordering.
|
||||
|
||||
```sh
|
||||
cargo fmt --manifest-path native/opennow-streamer/Cargo.toml --all -- --check
|
||||
cargo clippy --manifest-path native/opennow-streamer/Cargo.toml --workspace --all-targets -- -D warnings
|
||||
cargo test --manifest-path native/opennow-streamer/Cargo.toml --workspace
|
||||
```
|
||||
|
||||
Controlled output tests can also run without physical audio hardware:
|
||||
|
||||
```sh
|
||||
SDL_AUDIODRIVER=dummy cargo test --manifest-path native/opennow-streamer/Cargo.toml -p opennow-streamer-platform selected_sdl_output -- --ignored
|
||||
ALSA_CONFIG_PATH="$PWD/native/opennow-streamer/crates/opennow-streamer-platform-linux/tests/fixtures/audio-null.conf" cargo test --manifest-path native/opennow-streamer/Cargo.toml -p opennow-streamer-platform-linux selected_alsa_output -- --ignored
|
||||
```
|
||||
|
||||
The ignored `selected_pipewire_output` test requires an isolated PipeWire server
|
||||
and session manager with an `Audio/Sink` node named `opennow_test_output`. Point
|
||||
`XDG_RUNTIME_DIR` at that server's private runtime directory when running the test.
|
||||
|
||||
Release validation must additionally run authorized live sessions on Windows, Linux/X11, Linux/Wayland, Intel macOS and Apple Silicon to validate NVST interoperability, native GPU import, audio, input, recovery and device-loss behavior.
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
[package]
|
||||
name = "opennow-streamer-core"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
rust-version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
base64.workspace = true
|
||||
getrandom.workspace = true
|
||||
opennow-streamer-platform = { path = "../opennow-streamer-platform" }
|
||||
opennow-streamer-hid = { path = "../opennow-streamer-hid" }
|
||||
opennow-streamer-protocol = { path = "../opennow-streamer-protocol" }
|
||||
opennow-streamer-transport = { path = "../opennow-streamer-transport" }
|
||||
rustls.workspace = true
|
||||
serde_json.workspace = true
|
||||
tungstenite.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
openh264.workspace = true
|
||||
str0m.workspace = true
|
||||
opennow-streamer-platform = { path = "../opennow-streamer-platform", features = ["test-runtime"] }
|
||||
rcgen = { version = "0.14", default-features = false, features = ["crypto", "ring"] }
|
||||
Vendored
+707
@@ -0,0 +1,707 @@
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use opennow_streamer_platform::DecodeTimingsReport;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct DecodeProgressPolicy {
|
||||
pub stall: Duration,
|
||||
pub keyframe_grace: Duration,
|
||||
pub recovery_grace: Duration,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
|
||||
pub enum DecodeProgressStage {
|
||||
#[default]
|
||||
Tracking,
|
||||
KeyframePending,
|
||||
RecoveryRequired,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum DecodeProgressEvent {
|
||||
KeyframeRequested {
|
||||
idle_for: Duration,
|
||||
in_flight: usize,
|
||||
},
|
||||
RecoveryNeeded {
|
||||
idle_for: Duration,
|
||||
in_flight: usize,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub(crate) struct DecodeProgressWatchdog {
|
||||
stage: DecodeProgressStage,
|
||||
stage_started_at: Option<Instant>,
|
||||
decoder_epoch: Option<u64>,
|
||||
unsubmitted_since: Option<Instant>,
|
||||
}
|
||||
|
||||
impl DecodeProgressWatchdog {
|
||||
pub(crate) fn stage(&self) -> DecodeProgressStage {
|
||||
self.stage
|
||||
}
|
||||
|
||||
pub(crate) fn poll(
|
||||
&mut self,
|
||||
timings: &DecodeTimingsReport,
|
||||
upstream_stalled: bool,
|
||||
last_assembled_at: Option<Instant>,
|
||||
now: Instant,
|
||||
policy: DecodeProgressPolicy,
|
||||
) -> Option<DecodeProgressEvent> {
|
||||
if self.decoder_epoch != Some(timings.epoch) {
|
||||
self.decoder_epoch = Some(timings.epoch);
|
||||
self.clear_episode();
|
||||
self.unsubmitted_since = None;
|
||||
}
|
||||
if self.stage == DecodeProgressStage::RecoveryRequired {
|
||||
if !timings.last_output_at.is_some_and(|output| {
|
||||
self.stage_started_at
|
||||
.is_some_and(|started| output > started)
|
||||
}) {
|
||||
return None;
|
||||
}
|
||||
self.clear_episode();
|
||||
self.unsubmitted_since = None;
|
||||
}
|
||||
if upstream_stalled {
|
||||
self.clear_episode();
|
||||
self.unsubmitted_since = None;
|
||||
return None;
|
||||
}
|
||||
let reference = if timings.in_flight == 0 {
|
||||
if !last_assembled_at.is_some_and(|assembled| {
|
||||
now.saturating_duration_since(assembled) < policy.stall
|
||||
&& timings
|
||||
.last_output_at
|
||||
.is_none_or(|output| assembled > output)
|
||||
}) {
|
||||
self.clear_episode();
|
||||
self.unsubmitted_since = None;
|
||||
return None;
|
||||
}
|
||||
let first = *self.unsubmitted_since.get_or_insert(now);
|
||||
if timings.last_output_at.is_some_and(|output| output >= first) {
|
||||
self.clear_episode();
|
||||
self.unsubmitted_since = None;
|
||||
return None;
|
||||
}
|
||||
first
|
||||
} else {
|
||||
self.unsubmitted_since = None;
|
||||
progress_reference(timings)?
|
||||
};
|
||||
let idle_for = now.saturating_duration_since(reference);
|
||||
if idle_for < policy.stall {
|
||||
self.clear_episode();
|
||||
return None;
|
||||
}
|
||||
match self.stage {
|
||||
DecodeProgressStage::Tracking => {
|
||||
self.stage = DecodeProgressStage::KeyframePending;
|
||||
self.stage_started_at = Some(now);
|
||||
Some(DecodeProgressEvent::KeyframeRequested {
|
||||
idle_for,
|
||||
in_flight: timings.in_flight,
|
||||
})
|
||||
}
|
||||
DecodeProgressStage::KeyframePending => {
|
||||
let grace_started = self.stage_started_at.unwrap_or(now);
|
||||
if now.saturating_duration_since(grace_started) < policy.keyframe_grace {
|
||||
return None;
|
||||
}
|
||||
self.stage = DecodeProgressStage::RecoveryRequired;
|
||||
self.stage_started_at = Some(now);
|
||||
Some(DecodeProgressEvent::RecoveryNeeded {
|
||||
idle_for,
|
||||
in_flight: timings.in_flight,
|
||||
})
|
||||
}
|
||||
DecodeProgressStage::RecoveryRequired => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn clear_episode(&mut self) {
|
||||
self.stage = DecodeProgressStage::Tracking;
|
||||
self.stage_started_at = None;
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn progress_reference(timings: &DecodeTimingsReport) -> Option<Instant> {
|
||||
let this_epoch_output = timings
|
||||
.last_output_at
|
||||
.filter(|at| timings.epoch_started_at.is_none_or(|floor| *at >= floor));
|
||||
match (this_epoch_output, timings.oldest_in_flight_at) {
|
||||
(Some(last), Some(oldest)) => Some(last.max(oldest)),
|
||||
(Some(last), None) => Some(last),
|
||||
(None, Some(oldest)) => Some(
|
||||
timings
|
||||
.epoch_started_at
|
||||
.map_or(oldest, |floor| oldest.max(floor)),
|
||||
),
|
||||
(None, None) => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn timings(epoch: u64, epoch_started_at: Option<Instant>) -> DecodeTimingsReport {
|
||||
DecodeTimingsReport {
|
||||
call: None,
|
||||
residence: None,
|
||||
call_window_samples: 0,
|
||||
residence_window_samples: 0,
|
||||
submissions_total: 0,
|
||||
outputs_total: 0,
|
||||
output_calls_total: 0,
|
||||
last_submission_at: None,
|
||||
last_output_at: None,
|
||||
in_flight: 0,
|
||||
oldest_in_flight_at: None,
|
||||
epoch,
|
||||
epoch_started_at,
|
||||
unmatched_outputs: 0,
|
||||
unmatched_submissions: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn policy() -> DecodeProgressPolicy {
|
||||
DecodeProgressPolicy {
|
||||
stall: Duration::from_secs(8),
|
||||
keyframe_grace: Duration::from_secs(4),
|
||||
recovery_grace: Duration::from_secs(8),
|
||||
}
|
||||
}
|
||||
|
||||
fn single_pending_submission(base: Instant) -> DecodeTimingsReport {
|
||||
let mut report = timings(1, Some(base));
|
||||
report.submissions_total = 1;
|
||||
report.in_flight = 1;
|
||||
report.oldest_in_flight_at = Some(base);
|
||||
report.last_submission_at = Some(base);
|
||||
report
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_outstanding_work_never_reports_a_decode_stall() {
|
||||
let mut watchdog = DecodeProgressWatchdog::default();
|
||||
let base = Instant::now();
|
||||
let report = timings(1, Some(base));
|
||||
assert_eq!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
None,
|
||||
base + Duration::from_secs(60),
|
||||
policy()
|
||||
),
|
||||
None
|
||||
);
|
||||
assert_eq!(watchdog.stage(), DecodeProgressStage::Tracking);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arriving_video_without_decoder_submissions_requests_recovery() {
|
||||
let mut watchdog = DecodeProgressWatchdog::default();
|
||||
let base = Instant::now();
|
||||
let mut report = timings(1, Some(base));
|
||||
report.last_output_at = Some(base);
|
||||
assert_eq!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
Some(base + Duration::from_secs(1)),
|
||||
base + Duration::from_secs(1),
|
||||
policy()
|
||||
),
|
||||
None
|
||||
);
|
||||
for second in 2..9 {
|
||||
assert_eq!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
Some(base + Duration::from_secs(second)),
|
||||
base + Duration::from_secs(second),
|
||||
policy()
|
||||
),
|
||||
None
|
||||
);
|
||||
}
|
||||
assert!(matches!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
Some(base + Duration::from_secs(9)),
|
||||
base + Duration::from_secs(9),
|
||||
policy()
|
||||
),
|
||||
Some(DecodeProgressEvent::KeyframeRequested { in_flight: 0, .. })
|
||||
));
|
||||
assert!(matches!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
Some(base + Duration::from_secs(13)),
|
||||
base + Duration::from_secs(13),
|
||||
policy()
|
||||
),
|
||||
Some(DecodeProgressEvent::RecoveryNeeded { in_flight: 0, .. })
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stale_assembly_and_resumed_output_do_not_trigger_recovery() {
|
||||
let mut watchdog = DecodeProgressWatchdog::default();
|
||||
let base = Instant::now();
|
||||
let mut report = timings(1, Some(base));
|
||||
report.last_output_at = Some(base);
|
||||
assert_eq!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
Some(base + Duration::from_secs(1)),
|
||||
base + Duration::from_secs(1),
|
||||
policy()
|
||||
),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
Some(base + Duration::from_secs(1)),
|
||||
base + Duration::from_secs(10),
|
||||
policy()
|
||||
),
|
||||
None
|
||||
);
|
||||
report.last_output_at = Some(base + Duration::from_secs(11));
|
||||
assert_eq!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
Some(base + Duration::from_secs(12)),
|
||||
base + Duration::from_secs(12),
|
||||
policy()
|
||||
),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
Some(base + Duration::from_secs(19)),
|
||||
base + Duration::from_secs(19),
|
||||
policy()
|
||||
),
|
||||
None
|
||||
);
|
||||
assert_eq!(watchdog.stage(), DecodeProgressStage::Tracking);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn upstream_stall_restarts_the_unsubmitted_frame_grace_period() {
|
||||
let mut watchdog = DecodeProgressWatchdog::default();
|
||||
let base = Instant::now();
|
||||
let report = timings(1, Some(base));
|
||||
assert_eq!(
|
||||
watchdog.poll(&report, false, Some(base), base, policy()),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
true,
|
||||
Some(base + Duration::from_secs(7)),
|
||||
base + Duration::from_secs(7),
|
||||
policy()
|
||||
),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
Some(base + Duration::from_secs(10)),
|
||||
base + Duration::from_secs(10),
|
||||
policy()
|
||||
),
|
||||
None
|
||||
);
|
||||
assert!(matches!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
Some(base + Duration::from_secs(18)),
|
||||
base + Duration::from_secs(18),
|
||||
policy()
|
||||
),
|
||||
Some(DecodeProgressEvent::KeyframeRequested { in_flight: 0, .. })
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_pending_submission_with_a_stale_assembly_timestamp_still_escalates() {
|
||||
let mut watchdog = DecodeProgressWatchdog::default();
|
||||
let base = Instant::now();
|
||||
let report = single_pending_submission(base);
|
||||
assert_eq!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
None,
|
||||
base + Duration::from_secs(7),
|
||||
policy()
|
||||
),
|
||||
None
|
||||
);
|
||||
assert!(matches!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
None,
|
||||
base + Duration::from_secs(8),
|
||||
policy()
|
||||
),
|
||||
Some(DecodeProgressEvent::KeyframeRequested { in_flight: 1, .. })
|
||||
));
|
||||
assert!(matches!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
None,
|
||||
base + Duration::from_secs(12),
|
||||
policy()
|
||||
),
|
||||
Some(DecodeProgressEvent::RecoveryNeeded { in_flight: 1, .. })
|
||||
));
|
||||
assert_eq!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
None,
|
||||
base + Duration::from_secs(60),
|
||||
policy()
|
||||
),
|
||||
None
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fresh_output_clears_a_pending_keyframe_episode() {
|
||||
let mut watchdog = DecodeProgressWatchdog::default();
|
||||
let base = Instant::now();
|
||||
let mut report = single_pending_submission(base);
|
||||
assert!(matches!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
None,
|
||||
base + Duration::from_secs(8),
|
||||
policy()
|
||||
),
|
||||
Some(DecodeProgressEvent::KeyframeRequested { .. })
|
||||
));
|
||||
report.last_output_at = Some(base + Duration::from_secs(9));
|
||||
report.oldest_in_flight_at = Some(base + Duration::from_secs(9));
|
||||
assert_eq!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
None,
|
||||
base + Duration::from_secs(9),
|
||||
policy()
|
||||
),
|
||||
None
|
||||
);
|
||||
assert_eq!(watchdog.stage(), DecodeProgressStage::Tracking);
|
||||
for second in 10..40 {
|
||||
report.last_output_at = Some(base + Duration::from_secs(second));
|
||||
report.oldest_in_flight_at = Some(base + Duration::from_secs(second));
|
||||
assert_eq!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
None,
|
||||
base + Duration::from_secs(second),
|
||||
policy()
|
||||
),
|
||||
None
|
||||
);
|
||||
}
|
||||
assert_eq!(watchdog.stage(), DecodeProgressStage::Tracking);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_work_clears_a_pending_episode_and_later_work_starts_fresh() {
|
||||
let mut watchdog = DecodeProgressWatchdog::default();
|
||||
let base = Instant::now();
|
||||
let mut report = single_pending_submission(base);
|
||||
assert!(matches!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
None,
|
||||
base + Duration::from_secs(8),
|
||||
policy()
|
||||
),
|
||||
Some(DecodeProgressEvent::KeyframeRequested { .. })
|
||||
));
|
||||
report.in_flight = 0;
|
||||
report.oldest_in_flight_at = None;
|
||||
assert_eq!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
None,
|
||||
base + Duration::from_secs(9),
|
||||
policy()
|
||||
),
|
||||
None
|
||||
);
|
||||
assert_eq!(watchdog.stage(), DecodeProgressStage::Tracking);
|
||||
|
||||
let later = base + Duration::from_secs(100);
|
||||
report.in_flight = 1;
|
||||
report.oldest_in_flight_at = Some(later);
|
||||
assert_eq!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
None,
|
||||
later + Duration::from_secs(1),
|
||||
policy()
|
||||
),
|
||||
None
|
||||
);
|
||||
assert_eq!(watchdog.stage(), DecodeProgressStage::Tracking);
|
||||
assert!(matches!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
None,
|
||||
later + Duration::from_secs(8),
|
||||
policy()
|
||||
),
|
||||
Some(DecodeProgressEvent::KeyframeRequested { .. })
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn upstream_ownership_suppresses_until_the_transport_stage_resolves() {
|
||||
let mut watchdog = DecodeProgressWatchdog::default();
|
||||
let base = Instant::now();
|
||||
let report = single_pending_submission(base);
|
||||
assert_eq!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
true,
|
||||
None,
|
||||
base + Duration::from_secs(30),
|
||||
policy()
|
||||
),
|
||||
None
|
||||
);
|
||||
assert_eq!(watchdog.stage(), DecodeProgressStage::Tracking);
|
||||
assert!(matches!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
None,
|
||||
base + Duration::from_secs(31),
|
||||
policy()
|
||||
),
|
||||
Some(DecodeProgressEvent::KeyframeRequested { .. })
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_outputs_before_the_epoch_do_not_mask_a_stall() {
|
||||
let mut watchdog = DecodeProgressWatchdog::default();
|
||||
let base = Instant::now();
|
||||
let mut report = timings(2, Some(base + Duration::from_secs(10)));
|
||||
report.last_output_at = Some(base);
|
||||
report.in_flight = 1;
|
||||
report.oldest_in_flight_at = Some(base + Duration::from_secs(10));
|
||||
assert!(matches!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
None,
|
||||
base + Duration::from_secs(19),
|
||||
policy()
|
||||
),
|
||||
Some(DecodeProgressEvent::KeyframeRequested { .. })
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decoder_epoch_change_resets_a_terminal_episode() {
|
||||
let mut watchdog = DecodeProgressWatchdog::default();
|
||||
let base = Instant::now();
|
||||
let report = single_pending_submission(base);
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
None,
|
||||
base + Duration::from_secs(8),
|
||||
policy(),
|
||||
);
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
None,
|
||||
base + Duration::from_secs(12),
|
||||
policy(),
|
||||
);
|
||||
assert_eq!(watchdog.stage(), DecodeProgressStage::RecoveryRequired);
|
||||
|
||||
let mut restarted = timings(2, Some(base + Duration::from_secs(20)));
|
||||
restarted.in_flight = 1;
|
||||
restarted.oldest_in_flight_at = Some(base + Duration::from_secs(20));
|
||||
assert_eq!(
|
||||
watchdog.poll(
|
||||
&restarted,
|
||||
false,
|
||||
None,
|
||||
base + Duration::from_secs(27),
|
||||
policy()
|
||||
),
|
||||
None
|
||||
);
|
||||
assert!(matches!(
|
||||
watchdog.poll(
|
||||
&restarted,
|
||||
false,
|
||||
None,
|
||||
base + Duration::from_secs(28),
|
||||
policy()
|
||||
),
|
||||
Some(DecodeProgressEvent::KeyframeRequested { .. })
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn same_epoch_output_rearms_recovery_for_a_later_stall() {
|
||||
let mut watchdog = DecodeProgressWatchdog::default();
|
||||
let base = Instant::now();
|
||||
let mut report = single_pending_submission(base);
|
||||
assert!(matches!(
|
||||
watchdog.poll(&report, false, None, base + policy().stall, policy()),
|
||||
Some(DecodeProgressEvent::KeyframeRequested { .. })
|
||||
));
|
||||
let recovery_at = base + policy().stall + policy().keyframe_grace;
|
||||
assert!(matches!(
|
||||
watchdog.poll(&report, false, None, recovery_at, policy()),
|
||||
Some(DecodeProgressEvent::RecoveryNeeded { .. })
|
||||
));
|
||||
report.last_output_at = Some(recovery_at - Duration::from_secs(1));
|
||||
assert_eq!(
|
||||
watchdog.poll(&report, false, None, recovery_at, policy()),
|
||||
None
|
||||
);
|
||||
assert_eq!(watchdog.stage(), DecodeProgressStage::RecoveryRequired);
|
||||
|
||||
let resumed_at = recovery_at + Duration::from_secs(1);
|
||||
report.last_output_at = Some(resumed_at);
|
||||
report.outputs_total = 1;
|
||||
report.in_flight = 0;
|
||||
report.oldest_in_flight_at = None;
|
||||
assert_eq!(
|
||||
watchdog.poll(&report, false, None, resumed_at, policy()),
|
||||
None
|
||||
);
|
||||
assert_eq!(watchdog.stage(), DecodeProgressStage::Tracking);
|
||||
|
||||
report.in_flight = 1;
|
||||
report.oldest_in_flight_at = Some(resumed_at);
|
||||
let stalled_at = resumed_at + policy().stall;
|
||||
assert!(matches!(
|
||||
watchdog.poll(&report, false, None, stalled_at, policy()),
|
||||
Some(DecodeProgressEvent::KeyframeRequested { .. })
|
||||
));
|
||||
assert!(matches!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
None,
|
||||
stalled_at + policy().keyframe_grace,
|
||||
policy()
|
||||
),
|
||||
Some(DecodeProgressEvent::RecoveryNeeded { .. })
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn outstanding_work_changes_do_not_rearm_without_recovered_output() {
|
||||
let mut watchdog = DecodeProgressWatchdog::default();
|
||||
let base = Instant::now();
|
||||
let mut report = single_pending_submission(base);
|
||||
watchdog.poll(&report, false, None, base + policy().stall, policy());
|
||||
let recovery_at = base + policy().stall + policy().keyframe_grace;
|
||||
assert!(matches!(
|
||||
watchdog.poll(&report, false, None, recovery_at, policy()),
|
||||
Some(DecodeProgressEvent::RecoveryNeeded { .. })
|
||||
));
|
||||
for second in 1..30 {
|
||||
let now = recovery_at + Duration::from_secs(second);
|
||||
report.in_flight = usize::from(second % 2 != 0);
|
||||
report.oldest_in_flight_at = (report.in_flight != 0).then_some(now);
|
||||
assert_eq!(
|
||||
watchdog.poll(&report, false, Some(now), now, policy()),
|
||||
None
|
||||
);
|
||||
assert_eq!(watchdog.stage(), DecodeProgressStage::RecoveryRequired);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recovering_epoch_does_not_escalate_from_a_prior_epoch_output() {
|
||||
let mut watchdog = DecodeProgressWatchdog::default();
|
||||
let base = Instant::now();
|
||||
let mut report = single_pending_submission(base);
|
||||
report.last_output_at = Some(base - Duration::from_secs(30));
|
||||
report.epoch = 4;
|
||||
report.epoch_started_at = Some(base);
|
||||
assert_eq!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
None,
|
||||
base + Duration::from_secs(1),
|
||||
policy()
|
||||
),
|
||||
None
|
||||
);
|
||||
assert_eq!(watchdog.stage(), DecodeProgressStage::Tracking);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn advancing_decode_output_keeps_the_watchdog_tracking() {
|
||||
let mut watchdog = DecodeProgressWatchdog::default();
|
||||
let base = Instant::now();
|
||||
let mut report = timings(1, Some(base));
|
||||
report.in_flight = 2;
|
||||
report.oldest_in_flight_at = Some(base);
|
||||
report.last_output_at = Some(base);
|
||||
for second in 1..20 {
|
||||
report.last_output_at = Some(base + Duration::from_secs(second));
|
||||
report.oldest_in_flight_at = Some(base + Duration::from_secs(second));
|
||||
assert_eq!(
|
||||
watchdog.poll(
|
||||
&report,
|
||||
false,
|
||||
None,
|
||||
base + Duration::from_secs(second + 1),
|
||||
policy()
|
||||
),
|
||||
None
|
||||
);
|
||||
}
|
||||
assert_eq!(watchdog.stage(), DecodeProgressStage::Tracking);
|
||||
}
|
||||
}
|
||||
+6858
File diff suppressed because it is too large.
Load diff
+262
@@ -0,0 +1,262 @@
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread::{self, JoinHandle};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use opennow_streamer_platform::{MediaRuntime, MicrophoneReceiver, MicrophoneSession};
|
||||
use opennow_streamer_protocol::event;
|
||||
use opennow_streamer_transport::NvstUdpReceiverControl;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::{EventSender, Lifecycle, State, lock_lifecycle};
|
||||
|
||||
pub(super) struct MicrophoneController {
|
||||
runtime: MediaRuntime,
|
||||
device_id: String,
|
||||
events: EventSender,
|
||||
lifecycle: Arc<Mutex<Lifecycle>>,
|
||||
generation: u64,
|
||||
enabled: Arc<AtomicBool>,
|
||||
transport: NvstUdpReceiverControl,
|
||||
capture: Option<MicrophoneSession>,
|
||||
worker: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl MicrophoneController {
|
||||
pub fn new(
|
||||
runtime: MediaRuntime,
|
||||
transport: NvstUdpReceiverControl,
|
||||
device_id: String,
|
||||
events: EventSender,
|
||||
lifecycle: Arc<Mutex<Lifecycle>>,
|
||||
generation: u64,
|
||||
) -> Self {
|
||||
Self {
|
||||
runtime,
|
||||
device_id,
|
||||
events,
|
||||
lifecycle,
|
||||
generation,
|
||||
enabled: Arc::new(AtomicBool::new(false)),
|
||||
transport,
|
||||
capture: None,
|
||||
worker: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn enabled(&self) -> bool {
|
||||
self.enabled.load(Ordering::Acquire)
|
||||
}
|
||||
|
||||
pub fn set_enabled(&mut self, enabled: bool) -> Result<(), String> {
|
||||
if enabled && self.enabled() {
|
||||
return Ok(());
|
||||
}
|
||||
self.stop_capture();
|
||||
if !enabled {
|
||||
publish(
|
||||
&self.events,
|
||||
&self.lifecycle,
|
||||
self.generation,
|
||||
"muted",
|
||||
false,
|
||||
None,
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
let result = self.start_capture();
|
||||
if let Err(message) = &result {
|
||||
self.stop_capture();
|
||||
publish(
|
||||
&self.events,
|
||||
&self.lifecycle,
|
||||
self.generation,
|
||||
"error",
|
||||
false,
|
||||
Some(message),
|
||||
);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn start_capture(&mut self) -> Result<(), String> {
|
||||
let capture = self.runtime.start_microphone(&self.device_id)?;
|
||||
let lifecycle = lock_lifecycle(&self.lifecycle);
|
||||
if lifecycle.generation != self.generation || lifecycle.state != State::Connected {
|
||||
return Err("Microphone session ended during capture setup".to_owned());
|
||||
}
|
||||
self.transport
|
||||
.set_microphone_enabled(true)
|
||||
.map_err(|error| error.to_string())?;
|
||||
self.enabled.store(true, Ordering::Release);
|
||||
let receiver = capture.receiver();
|
||||
self.capture = Some(capture);
|
||||
let pump = MicrophonePump {
|
||||
transport: self.transport.clone(),
|
||||
events: self.events.clone(),
|
||||
lifecycle: self.lifecycle.clone(),
|
||||
generation: self.generation,
|
||||
enabled: self.enabled.clone(),
|
||||
receiver,
|
||||
};
|
||||
self.worker = Some(
|
||||
thread::Builder::new()
|
||||
.name("opennow-microphone-uplink".to_owned())
|
||||
.spawn(move || pump.run())
|
||||
.map_err(|error| format!("Microphone worker could not start: {error}"))?,
|
||||
);
|
||||
let _ = self.events.send(event(
|
||||
"microphone-state",
|
||||
json!({"state":"ready", "enabled":true}),
|
||||
));
|
||||
drop(lifecycle);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn stop_capture(&mut self) {
|
||||
self.enabled.store(false, Ordering::Release);
|
||||
let _ = self.transport.set_microphone_enabled(false);
|
||||
if let Some(mut capture) = self.capture.take() {
|
||||
capture.stop();
|
||||
}
|
||||
if let Some(worker) = self.worker.take() {
|
||||
let _ = worker.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for MicrophoneController {
|
||||
fn drop(&mut self) {
|
||||
self.stop_capture();
|
||||
}
|
||||
}
|
||||
|
||||
fn publish(
|
||||
events: &EventSender,
|
||||
lifecycle: &Mutex<Lifecycle>,
|
||||
generation: u64,
|
||||
state: &str,
|
||||
enabled: bool,
|
||||
message: Option<&str>,
|
||||
) {
|
||||
let lifecycle = lock_lifecycle(lifecycle);
|
||||
if lifecycle.generation == generation && lifecycle.state == State::Connected {
|
||||
let _ = events.send(event(
|
||||
"microphone-state",
|
||||
json!({"state":state, "enabled":enabled, "message":message}),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
struct MicrophonePump {
|
||||
transport: NvstUdpReceiverControl,
|
||||
events: EventSender,
|
||||
lifecycle: Arc<Mutex<Lifecycle>>,
|
||||
generation: u64,
|
||||
enabled: Arc<AtomicBool>,
|
||||
receiver: MicrophoneReceiver,
|
||||
}
|
||||
|
||||
impl MicrophonePump {
|
||||
fn active(&self) -> bool {
|
||||
let lifecycle = lock_lifecycle(&self.lifecycle);
|
||||
self.enabled.load(Ordering::Acquire)
|
||||
&& lifecycle.generation == self.generation
|
||||
&& lifecycle.state == State::Connected
|
||||
}
|
||||
|
||||
fn run(self) {
|
||||
let mut last_drops = (0, 0, 0);
|
||||
let mut last_report = Instant::now();
|
||||
while self.active() {
|
||||
let mut failure = None;
|
||||
for _ in 0..5 {
|
||||
match self.receiver.try_recv() {
|
||||
Ok(Some(frame)) => {
|
||||
if let Err(error) = self
|
||||
.transport
|
||||
.send_microphone_opus(frame.opus, frame.timestamp)
|
||||
{
|
||||
failure = Some(error.to_string());
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(None) => break,
|
||||
Err(error) => {
|
||||
failure = Some(error);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
let status = self.receiver.status();
|
||||
if !status.enabled && failure.is_none() {
|
||||
failure = Some(
|
||||
status
|
||||
.error
|
||||
.unwrap_or_else(|| "Microphone capture stopped".to_owned()),
|
||||
);
|
||||
}
|
||||
if let Some(message) = failure {
|
||||
if self.enabled.swap(false, Ordering::AcqRel) {
|
||||
publish(
|
||||
&self.events,
|
||||
&self.lifecycle,
|
||||
self.generation,
|
||||
"error",
|
||||
false,
|
||||
Some(&message),
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if last_report.elapsed() >= Duration::from_secs(1) {
|
||||
let drops = (
|
||||
status.dropped_pcm_frames,
|
||||
status.dropped_encoded_frames,
|
||||
self.transport.microphone_dropped_frames(),
|
||||
);
|
||||
if drops != last_drops {
|
||||
opennow_streamer_protocol::log::log_line(
|
||||
"WARN",
|
||||
"microphone",
|
||||
&format!(
|
||||
"dropped_pcm={} dropped_encoded={} dropped_transport={}",
|
||||
drops.0, drops.1, drops.2
|
||||
),
|
||||
);
|
||||
last_drops = drops;
|
||||
}
|
||||
last_report = Instant::now();
|
||||
}
|
||||
thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
self.enabled.store(false, Ordering::Release);
|
||||
self.receiver.stop();
|
||||
let _ = self.transport.set_microphone_enabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn microphone_status_cannot_escape_its_session_generation() {
|
||||
let (sender, receiver) = std::sync::mpsc::channel();
|
||||
let events = EventSender::unbounded(sender);
|
||||
let lifecycle = Mutex::new(Lifecycle {
|
||||
state: State::Connected,
|
||||
context: None,
|
||||
generation: 2,
|
||||
});
|
||||
publish(&events, &lifecycle, 1, "ready", true, None);
|
||||
assert!(receiver.try_recv().is_err());
|
||||
publish(&events, &lifecycle, 2, "ready", true, None);
|
||||
let value = receiver.try_recv().unwrap();
|
||||
assert_eq!(value["type"], "microphone-state");
|
||||
assert_eq!(value["enabled"], true);
|
||||
lock_lifecycle(&lifecycle).state = State::Idle;
|
||||
publish(&events, &lifecycle, 2, "error", false, Some("device lost"));
|
||||
assert!(receiver.try_recv().is_err());
|
||||
}
|
||||
}
|
||||
+3921
File diff suppressed because it is too large.
Load diff
Vendored
+34
@@ -0,0 +1,34 @@
|
||||
use opennow_streamer_platform::MediaStreamConfig;
|
||||
|
||||
/// NVST ANNOUNCE color encoding, matching the official wire format.
|
||||
///
|
||||
/// A vendor capture of a `10bit_420` session carries `bitDepth:10
|
||||
/// chromaFormat:1`: depth is literal (8 or 10) and chroma follows
|
||||
/// `chroma_format_idc` (1 = 4:2:0, 3 = 4:4:4). Both lines are always sent
|
||||
/// explicitly; the seat never sees a lone `bitDepth` line. `dynamicRangeMode`
|
||||
/// is sent only for HDR (`1`); SDR omits it, matching the captured baseline.
|
||||
///
|
||||
/// The internal 0/1 chroma enum from the client's app-to-NVST conversion must
|
||||
/// not reach the wire: emitting it (or suppressing the chroma line for 4:2:0)
|
||||
/// leaves seats unable to initialize the encoder, which surfaces as an
|
||||
/// accepted session that never delivers video.
|
||||
pub(super) fn announce_color_lines(stream: MediaStreamConfig) -> Vec<String> {
|
||||
let mut lines = vec![
|
||||
format!(
|
||||
"a=x-nv-video[0].bitDepth:{}",
|
||||
stream.color_quality.bit_depth()
|
||||
),
|
||||
format!(
|
||||
"a=x-nv-video[0].chromaFormat:{}",
|
||||
if stream.color_quality.is_444() { 3 } else { 1 }
|
||||
),
|
||||
];
|
||||
if stream.hdr {
|
||||
lines.push("a=x-nv-video[0].dynamicRangeMode:1".to_owned());
|
||||
}
|
||||
lines
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "nvst_rtsp_color_tests.rs"]
|
||||
mod tests;
|
||||
Vendored
+77
@@ -0,0 +1,77 @@
|
||||
use super::*;
|
||||
use opennow_streamer_platform::{MediaColorQuality, MediaVideoCodec};
|
||||
|
||||
fn stream(color_quality: MediaColorQuality, hdr: bool) -> MediaStreamConfig {
|
||||
MediaStreamConfig {
|
||||
codec: MediaVideoCodec::H265,
|
||||
color_quality,
|
||||
hdr,
|
||||
width: 2560,
|
||||
height: 1440,
|
||||
fps: 120,
|
||||
..MediaStreamConfig::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wire_values_use_literal_depth_and_chroma_format_idc() {
|
||||
for (quality, depth, chroma) in [
|
||||
(MediaColorQuality::EightBit420, 8, 1),
|
||||
(MediaColorQuality::EightBit444, 8, 3),
|
||||
(MediaColorQuality::TenBit420, 10, 1),
|
||||
(MediaColorQuality::TenBit444, 10, 3),
|
||||
] {
|
||||
assert_eq!(
|
||||
announce_color_lines(stream(quality, false)),
|
||||
vec![
|
||||
format!("a=x-nv-video[0].bitDepth:{depth}"),
|
||||
format!("a=x-nv-video[0].chromaFormat:{chroma}"),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dynamic_range_mode_is_hdr_only() {
|
||||
assert_eq!(
|
||||
announce_color_lines(stream(MediaColorQuality::TenBit420, true)),
|
||||
vec![
|
||||
"a=x-nv-video[0].bitDepth:10",
|
||||
"a=x-nv-video[0].chromaFormat:1",
|
||||
"a=x-nv-video[0].dynamicRangeMode:1",
|
||||
]
|
||||
);
|
||||
assert_eq!(
|
||||
announce_color_lines(stream(MediaColorQuality::TenBit444, true)),
|
||||
vec![
|
||||
"a=x-nv-video[0].bitDepth:10",
|
||||
"a=x-nv-video[0].chromaFormat:3",
|
||||
"a=x-nv-video[0].dynamicRangeMode:1",
|
||||
]
|
||||
);
|
||||
for quality in [
|
||||
MediaColorQuality::EightBit420,
|
||||
MediaColorQuality::EightBit444,
|
||||
MediaColorQuality::TenBit420,
|
||||
MediaColorQuality::TenBit444,
|
||||
] {
|
||||
assert!(
|
||||
announce_color_lines(stream(quality, false))
|
||||
.iter()
|
||||
.all(|line| !line.contains("dynamicRangeMode")),
|
||||
"{quality:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ten_bit_420_matches_the_vendor_capture() {
|
||||
// The only vendor-observed 10-bit 4:2:0 value on the wire.
|
||||
assert_eq!(
|
||||
announce_color_lines(stream(MediaColorQuality::TenBit420, false)),
|
||||
vec![
|
||||
"a=x-nv-video[0].bitDepth:10",
|
||||
"a=x-nv-video[0].chromaFormat:1",
|
||||
]
|
||||
);
|
||||
}
|
||||
+330
@@ -0,0 +1,330 @@
|
||||
use super::*;
|
||||
use std::net::TcpListener;
|
||||
|
||||
fn socket_pair() -> (RtspClient, WebSocket<TcpStream>) {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let server = thread::spawn(move || {
|
||||
let (stream, _) = listener.accept().unwrap();
|
||||
stream
|
||||
.set_read_timeout(Some(Duration::from_secs(6)))
|
||||
.unwrap();
|
||||
stream
|
||||
.set_write_timeout(Some(Duration::from_secs(1)))
|
||||
.unwrap();
|
||||
tungstenite::accept(stream).unwrap()
|
||||
});
|
||||
let stream = TcpStream::connect(address).unwrap();
|
||||
stream.set_nodelay(true).unwrap();
|
||||
let (socket, _) = tungstenite::client(
|
||||
format!("ws://{address}/rtsp"),
|
||||
MaybeTlsStream::Plain(stream),
|
||||
)
|
||||
.unwrap();
|
||||
(
|
||||
RtspClient {
|
||||
socket,
|
||||
cseq: 7,
|
||||
buffer: String::new(),
|
||||
},
|
||||
server.join().unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
fn active_session(client: RtspClient, ping: &NvstControlPing) -> ActiveNvstRtspSession {
|
||||
ActiveNvstRtspSession::spawn(client, ping.clone()).unwrap()
|
||||
}
|
||||
|
||||
fn server_ping(server: &mut WebSocket<TcpStream>) {
|
||||
server.send(Message::Ping(vec![1, 2, 3].into())).unwrap();
|
||||
assert_eq!(server.read().unwrap(), Message::Pong(vec![1, 2, 3].into()));
|
||||
}
|
||||
|
||||
fn client_ping(server: &mut WebSocket<TcpStream>) -> Vec<u8> {
|
||||
let message = server.read().unwrap();
|
||||
let Message::Ping(bytes) = message else {
|
||||
panic!("expected WebSocket ping, received {message:?}");
|
||||
};
|
||||
assert_eq!(bytes.len(), 8);
|
||||
bytes.to_vec()
|
||||
}
|
||||
|
||||
fn wait_until(mut condition: impl FnMut() -> bool) {
|
||||
let deadline = Instant::now() + Duration::from_secs(1);
|
||||
while !condition() {
|
||||
assert!(Instant::now() < deadline, "worker did not respond promptly");
|
||||
thread::sleep(Duration::from_millis(2));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn describe_accepts_large_text_binary_and_split_responses() {
|
||||
for (binary, chunk_size) in [
|
||||
(false, usize::MAX),
|
||||
(true, usize::MAX),
|
||||
(false, 16 * 1024),
|
||||
(true, 16 * 1024),
|
||||
] {
|
||||
let (mut client, mut server) = socket_pair();
|
||||
let body = "a=x-nv-test:description\r\n".repeat(12_000);
|
||||
assert!(body.len() > MAX_CONTROL_RESPONSE_BYTES);
|
||||
let expected_body = body.clone();
|
||||
let sender = thread::spawn(move || {
|
||||
let Message::Text(request) = server.read().unwrap() else {
|
||||
panic!("expected DESCRIBE request");
|
||||
};
|
||||
assert!(request.starts_with("DESCRIBE "));
|
||||
assert!(request.contains("\r\nCSeq: 8\r\n"));
|
||||
let response = format!(
|
||||
"RTSP/1.0 200 OK\r\nCSeq: 8\r\nContent-Length: {}\r\n\r\n{body}",
|
||||
body.len()
|
||||
);
|
||||
for chunk in response.as_bytes().chunks(chunk_size) {
|
||||
let message = if binary {
|
||||
Message::Binary(chunk.to_vec().into())
|
||||
} else {
|
||||
Message::Text(String::from_utf8(chunk.to_vec()).unwrap().into())
|
||||
};
|
||||
server.send(message).unwrap();
|
||||
}
|
||||
});
|
||||
let response = client
|
||||
.request("DESCRIBE", "rtsps://seat.nvidiagrid.net:322", &[], "")
|
||||
.unwrap();
|
||||
assert_eq!(response.status, 200);
|
||||
assert_eq!(response.body, expected_body);
|
||||
assert!(client.buffer.is_empty());
|
||||
sender.join().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn request_rejects_unbounded_partial_response() {
|
||||
let (mut client, mut server) = socket_pair();
|
||||
let sender = thread::spawn(move || {
|
||||
server.read().unwrap();
|
||||
server
|
||||
.send(Message::Text("x".repeat(MAX_REQUEST_RESPONSE_BYTES).into()))
|
||||
.unwrap();
|
||||
server.send(Message::Text("x".into())).unwrap();
|
||||
});
|
||||
let error = client
|
||||
.request("DESCRIBE", "rtsps://seat.nvidiagrid.net:322", &[], "")
|
||||
.err()
|
||||
.unwrap();
|
||||
assert_eq!(error.code, "nvst-rtsp-failed");
|
||||
assert!(
|
||||
error
|
||||
.message
|
||||
.contains("DESCRIBE response exceeds request buffer limit")
|
||||
);
|
||||
sender.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn request_bounds_websocket_message_size() {
|
||||
let (mut client, mut server) = socket_pair();
|
||||
let sender = thread::spawn(move || {
|
||||
server.read().unwrap();
|
||||
let _ = server.send(Message::Text(
|
||||
"x".repeat(MAX_REQUEST_RESPONSE_BYTES + 1).into(),
|
||||
));
|
||||
});
|
||||
let error = client
|
||||
.request("DESCRIBE", "rtsps://seat.nvidiagrid.net:322", &[], "")
|
||||
.err()
|
||||
.unwrap();
|
||||
assert_eq!(error.code, "nvst-rtsp-failed");
|
||||
assert!(error.message.contains("Space limit exceeded"));
|
||||
assert!(client.buffer.is_empty());
|
||||
sender.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rtsp_parser_accepts_response_at_request_size_limit() {
|
||||
let prefix = "RTSP/1.0 200 OK\r\nCSeq: 8\r\nContent-Length: ";
|
||||
let body_length = MAX_REQUEST_RESPONSE_BYTES
|
||||
- prefix.len()
|
||||
- MAX_REQUEST_RESPONSE_BYTES.to_string().len()
|
||||
- 4;
|
||||
let body = "x".repeat(body_length);
|
||||
let mut buffer = format!("{prefix}{body_length}\r\n\r\n{body}");
|
||||
assert_eq!(buffer.len(), MAX_REQUEST_RESPONSE_BYTES);
|
||||
let response = take_rtsp_response(&mut buffer, 8).unwrap().unwrap();
|
||||
assert_eq!(response.body, body);
|
||||
assert!(buffer.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rtsp_parser_bounds_declared_response_size_before_receiving_body() {
|
||||
let mut oversized = format!(
|
||||
"RTSP/1.0 200 OK\r\nCSeq: 8\r\nContent-Length: {MAX_REQUEST_RESPONSE_BYTES}\r\n\r\n"
|
||||
);
|
||||
let error = take_rtsp_response(&mut oversized, 8).err().unwrap();
|
||||
assert!(
|
||||
error
|
||||
.message
|
||||
.contains("response exceeds request buffer limit")
|
||||
);
|
||||
|
||||
let mut bounded = "RTSP/1.0 200 OK\r\nCSeq: 8\r\nContent-Length: 262144\r\n\r\n".to_owned();
|
||||
assert!(take_rtsp_response(&mut bounded, 8).unwrap().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn control_ping_parser_rejects_overflowing_and_invalid_body_boundaries() {
|
||||
let mut overflowing = format!(
|
||||
"RTSP/1.0 551 Response\r\nCSeq: 8\r\nContent-Length: {}\r\n\r\n",
|
||||
usize::MAX
|
||||
);
|
||||
assert!(take_rtsp_response(&mut overflowing, 8).is_err());
|
||||
let mut invalid = "RTSP/1.0 551 Response\r\nCSeq: 8\r\nContent-Length: 1\r\n\r\né".to_owned();
|
||||
assert!(take_rtsp_response(&mut invalid, 8).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn control_ping_samples_are_shared_expiring_and_clearable() {
|
||||
let ping = NvstControlPing::default();
|
||||
let clone = ping.clone();
|
||||
let sent = Instant::now();
|
||||
let received = sent + Duration::from_micros(4_750);
|
||||
assert_eq!(ping.ping_ms(sent), None);
|
||||
ping.record(sent, received);
|
||||
assert_eq!(clone.ping_ms(received), Some(4.75));
|
||||
assert_eq!(clone.ping_ms(sent), None);
|
||||
assert_eq!(ping.ping_ms(received + Duration::from_secs(4)), Some(4.75));
|
||||
assert_eq!(ping.ping_ms(received + CONTROL_PING_EXPIRY), None);
|
||||
assert_eq!(clone.ping_ms(received), None);
|
||||
ping.record(sent, received);
|
||||
clone.clear();
|
||||
assert_eq!(ping.ping_ms(received), None);
|
||||
ping.record(sent, sent);
|
||||
assert_eq!(ping.ping_ms(sent), Some(0.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn live_control_ping_uses_websocket_without_rtsp_requests() {
|
||||
let (client, mut server) = socket_pair();
|
||||
let ping = NvstControlPing::default();
|
||||
let mut active = active_session(client, &ping);
|
||||
server_ping(&mut server);
|
||||
let payload = client_ping(&mut server);
|
||||
server.send(Message::Pong(vec![0; 8].into())).unwrap();
|
||||
assert_eq!(ping.ping_ms(Instant::now()), None);
|
||||
server.send(Message::Pong(payload.into())).unwrap();
|
||||
wait_until(|| ping.ping_ms(Instant::now()).is_some());
|
||||
assert!(ping.ping_ms(Instant::now()).unwrap() < 1000.0);
|
||||
server_ping(&mut server);
|
||||
server.close(None).unwrap();
|
||||
wait_until(|| active.worker.as_ref().unwrap().is_finished());
|
||||
assert_eq!(ping.ping_ms(Instant::now()), None);
|
||||
let shutdown_started = Instant::now();
|
||||
active.shutdown();
|
||||
assert!(shutdown_started.elapsed() < Duration::from_secs(1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn live_control_ping_times_out_without_accepting_late_pongs() {
|
||||
let (client, mut server) = socket_pair();
|
||||
let ping = NvstControlPing::default();
|
||||
let mut active = active_session(client, &ping);
|
||||
let first = client_ping(&mut server);
|
||||
server.send(Message::Pong(first.into())).unwrap();
|
||||
wait_until(|| ping.ping_ms(Instant::now()).is_some());
|
||||
let missing = client_ping(&mut server);
|
||||
server.send(Message::Pong(vec![0; 8].into())).unwrap();
|
||||
let next = client_ping(&mut server);
|
||||
server.send(Message::Pong(vec![0; 8].into())).unwrap();
|
||||
assert_eq!(
|
||||
u64::from_be_bytes(next.clone().try_into().unwrap()),
|
||||
u64::from_be_bytes(missing.clone().try_into().unwrap()) + 1
|
||||
);
|
||||
assert_eq!(ping.ping_ms(Instant::now()), None);
|
||||
server.send(Message::Pong(missing.into())).unwrap();
|
||||
server_ping(&mut server);
|
||||
assert_eq!(ping.ping_ms(Instant::now()), None);
|
||||
server.send(Message::Pong(next.into())).unwrap();
|
||||
wait_until(|| ping.ping_ms(Instant::now()).is_some());
|
||||
drop(server);
|
||||
wait_until(|| active.worker.as_ref().unwrap().is_finished());
|
||||
assert_eq!(ping.ping_ms(Instant::now()), None);
|
||||
active.shutdown();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unanswered_websocket_ping_does_not_end_the_session() {
|
||||
let (client, mut server) = socket_pair();
|
||||
let ping = NvstControlPing::default();
|
||||
let mut active = active_session(client, &ping);
|
||||
let first = client_ping(&mut server);
|
||||
server.send(Message::Pong(vec![0; 8].into())).unwrap();
|
||||
let second = client_ping(&mut server);
|
||||
server.send(Message::Pong(vec![0; 8].into())).unwrap();
|
||||
assert_ne!(first, second);
|
||||
assert_eq!(ping.ping_ms(Instant::now()), None);
|
||||
assert!(!active.worker.as_ref().unwrap().is_finished());
|
||||
let shutdown = thread::spawn(move || active.shutdown());
|
||||
assert!(matches!(server.read().unwrap(), Message::Close(_)));
|
||||
shutdown.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn live_control_ping_shutdown_closes_without_rtsp_teardown() {
|
||||
let (client, mut server) = socket_pair();
|
||||
let ping = NvstControlPing::default();
|
||||
let mut active = active_session(client, &ping);
|
||||
client_ping(&mut server);
|
||||
let shutdown_started = Instant::now();
|
||||
let shutdown = thread::spawn(move || active.shutdown());
|
||||
assert!(matches!(server.read().unwrap(), Message::Close(_)));
|
||||
shutdown.join().unwrap();
|
||||
assert!(shutdown_started.elapsed() < Duration::from_secs(1));
|
||||
assert_eq!(ping.ping_ms(Instant::now()), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn live_control_ping_shutdown_does_not_wait_for_pending_pong() {
|
||||
let (client, mut server) = socket_pair();
|
||||
let ping = NvstControlPing::default();
|
||||
let mut active = active_session(client, &ping);
|
||||
client_ping(&mut server);
|
||||
let shutdown_started = Instant::now();
|
||||
let shutdown = thread::spawn(move || active.shutdown());
|
||||
assert!(matches!(server.read().unwrap(), Message::Close(_)));
|
||||
assert_eq!(ping.ping_ms(Instant::now()), None);
|
||||
shutdown.join().unwrap();
|
||||
assert!(shutdown_started.elapsed() < Duration::from_secs(1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn live_control_ping_shutdown_clears_sample() {
|
||||
let (client, mut server) = socket_pair();
|
||||
let ping = NvstControlPing::default();
|
||||
let mut active = active_session(client, &ping);
|
||||
let payload = client_ping(&mut server);
|
||||
server.send(Message::Pong(payload.into())).unwrap();
|
||||
wait_until(|| ping.ping_ms(Instant::now()).is_some());
|
||||
let shutdown_started = Instant::now();
|
||||
let shutdown = thread::spawn(move || active.shutdown());
|
||||
assert!(matches!(server.read().unwrap(), Message::Close(_)));
|
||||
assert_eq!(ping.ping_ms(Instant::now()), None);
|
||||
shutdown.join().unwrap();
|
||||
assert!(shutdown_started.elapsed() < Duration::from_secs(1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn live_control_ping_rejects_unbounded_partial_response() {
|
||||
let (client, mut server) = socket_pair();
|
||||
let ping = NvstControlPing::default();
|
||||
let mut active = active_session(client, &ping);
|
||||
let payload = client_ping(&mut server);
|
||||
server.send(Message::Pong(payload.into())).unwrap();
|
||||
wait_until(|| ping.ping_ms(Instant::now()).is_some());
|
||||
server
|
||||
.send(Message::Text("x".repeat(MAX_CONTROL_RESPONSE_BYTES).into()))
|
||||
.unwrap();
|
||||
server.send(Message::Text("x".into())).unwrap();
|
||||
wait_until(|| active.worker.as_ref().unwrap().is_finished());
|
||||
assert_eq!(ping.ping_ms(Instant::now()), None);
|
||||
active.shutdown();
|
||||
}
|
||||
Vendored
+295
@@ -0,0 +1,295 @@
|
||||
use super::{NvstRtspError, sdp_attribute};
|
||||
use opennow_streamer_transport::NvstRecoveryPolicy;
|
||||
use serde_json::{Value, json};
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
fn ignore_offer(name: &str, value: &str) {
|
||||
opennow_streamer_protocol::log::log_line(
|
||||
"WARN",
|
||||
"transport",
|
||||
&format!("ignored-server-offer name={name} value={value}"),
|
||||
);
|
||||
}
|
||||
|
||||
pub(super) fn offered_bool(sdp: &str, name: &str) -> Option<bool> {
|
||||
let value = sdp_attribute(sdp, name)?;
|
||||
match value.as_str() {
|
||||
"0" => Some(false),
|
||||
"1" => Some(true),
|
||||
_ => {
|
||||
ignore_offer(name, &value);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn offered_u32(sdp: &str, name: &str) -> Option<u32> {
|
||||
let value = sdp_attribute(sdp, name)?;
|
||||
value
|
||||
.parse()
|
||||
.inspect_err(|_| ignore_offer(name, &value))
|
||||
.ok()
|
||||
}
|
||||
|
||||
fn offered_in(sdp: &str, name: &str, range: RangeInclusive<u32>) -> Option<u32> {
|
||||
let value = offered_u32(sdp, name)?;
|
||||
if range.contains(&value) {
|
||||
return Some(value);
|
||||
}
|
||||
ignore_offer(name, &value.to_string());
|
||||
None
|
||||
}
|
||||
|
||||
pub(super) fn recovery_policy(sdp: &str) -> NvstRecoveryPolicy {
|
||||
NvstRecoveryPolicy {
|
||||
ref_pic_invalidation: offered_bool(sdp, "video[0].refPicInvalidation"),
|
||||
stream_recovery_type: offered_u32(sdp, "video[0].streamRecoveryType"),
|
||||
frames_with_invalid_ref_threshold: offered_u32(
|
||||
sdp,
|
||||
"video[0].framesWithInvalidRefThreshold",
|
||||
),
|
||||
enable_recovery_mode: offered_bool(sdp, "general.enableRecoveryMode"),
|
||||
recovery_mode_trigger_threshold_ms: offered_u32(
|
||||
sdp,
|
||||
"general.recoveryModeTriggerThresholdMs",
|
||||
),
|
||||
enable_intra_refresh_in_recovery_mode: offered_bool(
|
||||
sdp,
|
||||
"general.enableIntraRefreshInRecoveryMode",
|
||||
),
|
||||
encoder_ltr_feature_setting: offered_u32(sdp, "video[0].encoderLtrFeatureSetting"),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn negotiated_announce(
|
||||
announce: &str,
|
||||
describe: &str,
|
||||
handoff: &mut Value,
|
||||
) -> Result<String, NvstRtspError> {
|
||||
let mut values = serde_json::Map::new();
|
||||
for (name, minimum, maximum) in [
|
||||
("vqos[0].fec.enable", 0, 1),
|
||||
("vqos[0].fec.rateDropWindow", 0, u32::MAX),
|
||||
("vqos[0].fec.minRequiredFecPackets", 0, u32::MAX),
|
||||
("vqos[0].fec.repairPercent", 0, 100),
|
||||
("vqos[0].fec.repairMinPercent", 0, 100),
|
||||
("vqos[0].fec.repairMaxPercent", 0, 100),
|
||||
("vqos[0].bllFec.enable", 0, 1),
|
||||
("packetPacing.version", 3, 3),
|
||||
("packetPacing.mode", 1, 1),
|
||||
("packetPacing.numGroups", 1, u32::MAX),
|
||||
("packetPacing.maxDelayUs", 0, u32::MAX),
|
||||
("packetPacing.minNumPacketsFrame", 0, u32::MAX),
|
||||
("packetPacing.minNumPacketsPerGroup", 0, u32::MAX),
|
||||
("packetPacing.enableAccurateSleep", 0, 1),
|
||||
("packetPacing.enableSmoothTransition", 0, 1),
|
||||
("packetPacing.allowFpsBasedToggle", 0, 1),
|
||||
] {
|
||||
let value = offered_in(describe, name, minimum..=maximum)
|
||||
.or(offered_u32(announce, name))
|
||||
.unwrap();
|
||||
values.insert(name.to_owned(), json!(value));
|
||||
}
|
||||
let mut min = values["vqos[0].fec.repairMinPercent"].as_u64().unwrap();
|
||||
let mut max = values["vqos[0].fec.repairMaxPercent"].as_u64().unwrap();
|
||||
if min > max {
|
||||
ignore_offer("vqos[0].fec.repairMinPercent", &format!("{min}>{max}"));
|
||||
min = u64::from(offered_u32(announce, "vqos[0].fec.repairMinPercent").unwrap());
|
||||
max = u64::from(offered_u32(announce, "vqos[0].fec.repairMaxPercent").unwrap());
|
||||
values["vqos[0].fec.repairMinPercent"] = json!(min);
|
||||
values["vqos[0].fec.repairMaxPercent"] = json!(max);
|
||||
}
|
||||
let repair = values["vqos[0].fec.repairPercent"].as_u64().unwrap();
|
||||
values["vqos[0].fec.repairPercent"] = json!(repair.clamp(min, max));
|
||||
let ceiling = offered_u32(announce, "vqos[0].bw.maximumBitrateKbps").unwrap();
|
||||
let initial = offered_in(describe, "video[0].initialBitrateKbps", 1..=u32::MAX)
|
||||
.unwrap_or(ceiling)
|
||||
.min(ceiling);
|
||||
let peak = offered_u32(describe, "video[0].initialPeakBitrateKbps")
|
||||
.unwrap_or(ceiling)
|
||||
.clamp(initial, ceiling);
|
||||
values.insert("video[0].initialBitrateKbps".to_owned(), json!(initial));
|
||||
values.insert("video[0].initialPeakBitrateKbps".to_owned(), json!(peak));
|
||||
values.insert("vqos[0].bw.maximumBitrateKbps".to_owned(), json!(ceiling));
|
||||
opennow_streamer_protocol::log::log_line(
|
||||
"INFO",
|
||||
"transport",
|
||||
&format!("negotiated-encoding={}", Value::Object(values.clone())),
|
||||
);
|
||||
let mut result = String::with_capacity(announce.len());
|
||||
for line in announce.split_inclusive("\r\n") {
|
||||
let attribute = line
|
||||
.strip_prefix("a=x-nv-")
|
||||
.and_then(|line| line.split_once(':'));
|
||||
if let Some((name, _)) = attribute
|
||||
&& let Some(value) = values.get(name)
|
||||
{
|
||||
result.push_str(&format!("a=x-nv-{name}:{value}\r\n"));
|
||||
} else {
|
||||
result.push_str(line);
|
||||
}
|
||||
}
|
||||
handoff["framePacingFeedbackMode"] =
|
||||
json!(super::video_qos_offers(describe).pacing_feedback_mode(&result)?);
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub(super) fn microphone_handoff(sdp: &str, handoff: &mut Value) {
|
||||
let enabled = offered_bool(sdp, "aqos.enableRedundancyForMic").unwrap_or(false);
|
||||
let level = if enabled {
|
||||
offered_in(sdp, "aqos.redundancyLevelForMic", 0..=3).unwrap_or(0)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
handoff["microphoneRedundancyLevel"] = json!(level);
|
||||
let mut in_mic = false;
|
||||
let mut payloads = Vec::new();
|
||||
for line in sdp
|
||||
.split("||")
|
||||
.next()
|
||||
.unwrap_or_default()
|
||||
.split(";;")
|
||||
.next()
|
||||
.unwrap_or_default()
|
||||
.lines()
|
||||
.map(str::trim)
|
||||
{
|
||||
if let Some(media) = line.strip_prefix("m=") {
|
||||
let mut parts = media.split_whitespace();
|
||||
in_mic = parts
|
||||
.next()
|
||||
.is_some_and(|kind| kind.eq_ignore_ascii_case("mic"));
|
||||
payloads = if in_mic {
|
||||
parts.skip(2).collect()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
} else if in_mic
|
||||
&& let Some(mapping) = line.strip_prefix("a=rtpmap:")
|
||||
&& let Some((payload, encoding)) = mapping.split_once(char::is_whitespace)
|
||||
{
|
||||
if !payloads.contains(&payload) {
|
||||
continue;
|
||||
}
|
||||
let Some(codec) = encoding.split('/').next() else {
|
||||
continue;
|
||||
};
|
||||
let field = if codec.eq_ignore_ascii_case("opus") {
|
||||
"microphoneOpusPayloadType"
|
||||
} else if codec.eq_ignore_ascii_case("red") {
|
||||
"microphoneRedPayloadType"
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
let Some(payload) = payload.parse::<u8>().ok().filter(|value| *value <= 127) else {
|
||||
ignore_offer(field, payload);
|
||||
continue;
|
||||
};
|
||||
handoff[field] = json!(payload);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn recovery_describe_is_typed_and_does_not_guess_defaults() {
|
||||
let policy = recovery_policy(
|
||||
"a=x-nv-video[0].refPicInvalidation:1\r\na=x-nv-video[0].streamRecoveryType:2\r\na=x-nv-video[0].framesWithInvalidRefThreshold:4\r\na=x-nv-general.enableRecoveryMode:1\r\na=x-nv-general.recoveryModeTriggerThresholdMs:6000\r\na=x-nv-general.enableIntraRefreshInRecoveryMode:1\r\na=x-nv-video[0].encoderLtrFeatureSetting:2\r\n",
|
||||
);
|
||||
assert_eq!(policy.ref_pic_invalidation, Some(true));
|
||||
assert_eq!(policy.stream_recovery_type, Some(2));
|
||||
assert_eq!(policy.frames_with_invalid_ref_threshold, Some(4));
|
||||
assert_eq!(policy.recovery_mode_trigger_threshold_ms, Some(6000));
|
||||
assert_eq!(policy.encoder_ltr_feature_setting, Some(2));
|
||||
assert_eq!(recovery_policy(""), NvstRecoveryPolicy::default());
|
||||
assert_eq!(
|
||||
recovery_policy(
|
||||
"a=x-nv-video[0].refPicInvalidation:2\r\na=x-nv-video[0].framesWithInvalidRefThreshold:-1\r\n"
|
||||
),
|
||||
NvstRecoveryPolicy::default()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mic_handoff_uses_only_its_offered_payloads_and_red_depth() {
|
||||
let mut handoff = json!({});
|
||||
microphone_handoff(
|
||||
"a=x-nv-aqos.enableRedundancyForMic:1\r\na=x-nv-aqos.redundancyLevelForMic:3\r\nm=audio 5000 RTP/AVP 111 112\r\na=rtpmap:111 opus/48000/2\r\na=rtpmap:112 red/48000/2\r\nm=mic 5002 RTP/AVP 113 114\r\na=rtpmap:113 opus/48000/1\r\na=rtpmap:114 red/48000/1\r\na=rtpmap:115 red/48000/1\r\n",
|
||||
&mut handoff,
|
||||
);
|
||||
assert_eq!(
|
||||
handoff,
|
||||
json!({"microphoneOpusPayloadType":113,"microphoneRedPayloadType":114,"microphoneRedundancyLevel":3})
|
||||
);
|
||||
for sdp in [
|
||||
"",
|
||||
"a=x-nv-aqos.enableRedundancyForMic:0\r\na=x-nv-aqos.redundancyLevelForMic:3\r\nm=audio 5000 RTP/AVP 111\r\na=rtpmap:111 opus/48000/2\r\n",
|
||||
] {
|
||||
let mut handoff = json!({});
|
||||
microphone_handoff(sdp, &mut handoff);
|
||||
assert_eq!(handoff, json!({"microphoneRedundancyLevel":0}));
|
||||
}
|
||||
for sdp in [
|
||||
"m=mic 5002 RTP/AVP 128\r\na=rtpmap:128 opus/48000/1\r\n",
|
||||
"a=x-nv-aqos.enableRedundancyForMic:1\r\na=x-nv-aqos.redundancyLevelForMic:4",
|
||||
] {
|
||||
let mut handoff = json!({});
|
||||
microphone_handoff(sdp, &mut handoff);
|
||||
assert_eq!(handoff, json!({"microphoneRedundancyLevel":0}));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mic_describe_handoff_reaches_the_live_transport_parser() {
|
||||
use opennow_streamer_transport::{NvstConfigError, NvstVideoConfig};
|
||||
|
||||
let mut handoff = json!({
|
||||
"clientUdpPort":49005,"videoPeerIp":"192.0.2.1","videoPeerPort":5004,
|
||||
"srtpAesKeyHex":"01".repeat(32),"srtpSaltHex":"02".repeat(12),"codec":"H264",
|
||||
"microphoneOnBundle":true,"pingVersion":6,
|
||||
"localIceUsernameFragment":"loc1","localIcePassword":"local-password-value-01",
|
||||
"remoteIceUsernameFragment":"remote01","remoteIcePassword":"remote-password-value-01",
|
||||
"remoteDtlsFingerprint":"AA:BB"
|
||||
});
|
||||
microphone_handoff(
|
||||
"a=x-nv-aqos.enableRedundancyForMic:1\r\na=x-nv-aqos.redundancyLevelForMic:3\r\nm=mic 5002 RTP/AVP 113 114\r\na=rtpmap:113 opus/48000/1\r\na=rtpmap:114 red/48000/1\r\n",
|
||||
&mut handoff,
|
||||
);
|
||||
assert_eq!(handoff["microphoneOpusPayloadType"], 113);
|
||||
assert_eq!(handoff["microphoneRedPayloadType"], 114);
|
||||
assert_eq!(handoff["microphoneRedundancyLevel"], 3);
|
||||
assert!(
|
||||
NvstVideoConfig::from_legacy_handoff(&handoff, None)
|
||||
.unwrap()
|
||||
.microphone_available()
|
||||
);
|
||||
|
||||
for (field, invalid) in [
|
||||
("microphoneOpusPayloadType", 78),
|
||||
("microphoneRedPayloadType", 128),
|
||||
("microphoneRedPayloadType", 113),
|
||||
("microphoneRedundancyLevel", 4),
|
||||
] {
|
||||
let mut invalid_handoff = handoff.clone();
|
||||
invalid_handoff[field] = json!(invalid);
|
||||
assert!(
|
||||
matches!(NvstVideoConfig::from_legacy_handoff(&invalid_handoff, None),
|
||||
Err(NvstConfigError::OutOfRange {field: rejected}) if rejected == field),
|
||||
"the live transport parser must consume and validate {field}"
|
||||
);
|
||||
}
|
||||
microphone_handoff(
|
||||
"a=x-nv-aqos.enableRedundancyForMic:0\r\na=x-nv-aqos.redundancyLevelForMic:3\r\n",
|
||||
&mut handoff,
|
||||
);
|
||||
assert_eq!(handoff["microphoneRedundancyLevel"], 0);
|
||||
assert!(
|
||||
NvstVideoConfig::from_legacy_handoff(&handoff, None)
|
||||
.unwrap()
|
||||
.microphone_available()
|
||||
);
|
||||
}
|
||||
}
|
||||
Vendored
+441
@@ -0,0 +1,441 @@
|
||||
use super::*;
|
||||
use std::net::TcpListener;
|
||||
|
||||
const TARGET: &str = "rtsps://seat.nvidiagrid.net:322";
|
||||
const VALID_PEER: &str = "Transport: unicast;source=192.0.2.10;X-GS-ServerPort=5004-5005\r\nX-Nv-Ping: 6\r\nX-Nv-Ping-Payload: 00ff\r\n";
|
||||
const EMPTY_TRANSPORT_URIS: [&str; 4] = [
|
||||
"streamid=video/0/0",
|
||||
"streamid=video/0",
|
||||
"rtsps://seat.nvidiagrid.net:322/streamid=video/0/0",
|
||||
"rtsps://seat.nvidiagrid.net:322/streamid=video/0",
|
||||
];
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct Reply {
|
||||
uri: &'static str,
|
||||
transport: &'static str,
|
||||
status: u16,
|
||||
headers: &'static str,
|
||||
}
|
||||
|
||||
fn scripted_setup(replies: Vec<Reply>) -> Result<VideoSetup, NvstRtspError> {
|
||||
scripted_setup_with_retry(replies, 0, Duration::ZERO, None)
|
||||
}
|
||||
|
||||
fn scripted_setup_with_retry(
|
||||
replies: Vec<Reply>,
|
||||
max_peer_retries: u32,
|
||||
peer_retry_delay: Duration,
|
||||
bundle_video_peer: Option<(String, u16, u16)>,
|
||||
) -> Result<VideoSetup, NvstRtspError> {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let stream = TcpStream::connect(address).unwrap();
|
||||
let (server_stream, _) = listener.accept().unwrap();
|
||||
for socket in [&stream, &server_stream] {
|
||||
socket
|
||||
.set_read_timeout(Some(Duration::from_secs(2)))
|
||||
.unwrap();
|
||||
socket
|
||||
.set_write_timeout(Some(Duration::from_secs(2)))
|
||||
.unwrap();
|
||||
}
|
||||
let server = thread::spawn(move || {
|
||||
let mut socket = tungstenite::accept(server_stream).unwrap();
|
||||
for (index, reply) in replies.iter().enumerate() {
|
||||
let Message::Text(request) = socket.read().unwrap() else {
|
||||
panic!("expected a SETUP request");
|
||||
};
|
||||
assert!(
|
||||
request.starts_with(&format!("SETUP {} RTSP/1.0\r\n", reply.uri)),
|
||||
"{request}"
|
||||
);
|
||||
assert!(request.contains(&format!("\r\nTransport: {}\r\n", reply.transport)));
|
||||
assert!(request.contains("\r\nSession: rtsp-session\r\n"));
|
||||
assert!(request.contains("\r\nx-nv-sessionid: nv-session\r\n"));
|
||||
assert!(request.contains("\r\nx-nv-ping: 6\r\n"));
|
||||
let cseq = index + 3;
|
||||
assert!(request.contains(&format!("\r\nCSeq: {cseq}\r\n")));
|
||||
socket
|
||||
.send(Message::Text(
|
||||
format!(
|
||||
"RTSP/1.0 {} Response\r\nCSeq: {cseq}\r\n{}Content-Length: 0\r\n\r\n",
|
||||
reply.status, reply.headers
|
||||
)
|
||||
.into(),
|
||||
))
|
||||
.unwrap();
|
||||
}
|
||||
});
|
||||
let (socket, _) = tungstenite::client(
|
||||
format!("ws://{address}/rtsp"),
|
||||
MaybeTlsStream::Plain(stream),
|
||||
)
|
||||
.unwrap();
|
||||
let mut client = RtspClient {
|
||||
socket,
|
||||
cseq: 2,
|
||||
buffer: String::new(),
|
||||
};
|
||||
let result = client.setup_video_with_retry(
|
||||
"streamid=video/0",
|
||||
TARGET,
|
||||
&[
|
||||
("Session", "rtsp-session".to_owned()),
|
||||
("x-nv-sessionid", "nv-session".to_owned()),
|
||||
("x-nv-ping", "6".to_owned()),
|
||||
],
|
||||
49005,
|
||||
max_peer_retries,
|
||||
peer_retry_delay,
|
||||
bundle_video_peer,
|
||||
);
|
||||
drop(client);
|
||||
server.join().unwrap();
|
||||
result
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn video_setup_keeps_the_official_first_attempt_and_its_metadata() {
|
||||
let setup = scripted_setup(vec![Reply {
|
||||
uri: EMPTY_TRANSPORT_URIS[0],
|
||||
transport: "",
|
||||
status: 200,
|
||||
headers: VALID_PEER,
|
||||
}])
|
||||
.unwrap();
|
||||
assert_eq!(setup.peer, ("192.0.2.10".to_owned(), 5004, 5005));
|
||||
assert!(!setup.bundle_fallback);
|
||||
assert_eq!(
|
||||
header_value(&setup.response, "x-nv-ping-payload"),
|
||||
Some("00ff")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn video_setup_stops_at_first_peerless_success_to_protect_announce() {
|
||||
// Live alliance rigs accept the first SETUP but poison the session once
|
||||
// further forms are tried (later rounds degrade to pure 400s and ANNOUNCE
|
||||
// is then rejected), while a single SETUP followed by ANNOUNCE succeeds.
|
||||
// The scripted server holds exactly one reply: any extra request fails it.
|
||||
let error = match scripted_setup(vec![Reply {
|
||||
uri: EMPTY_TRANSPORT_URIS[0],
|
||||
transport: "",
|
||||
status: 200,
|
||||
headers: "",
|
||||
}]) {
|
||||
Ok(_) => panic!("a peerless 200 must not yield a peer"),
|
||||
Err(error) => error,
|
||||
};
|
||||
assert_eq!(error.code, "missing-video-peer");
|
||||
assert!(error.message.contains("peerless 200"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn video_setup_tries_absolute_forms_after_uri_rejections() {
|
||||
let replies = EMPTY_TRANSPORT_URIS
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, uri)| Reply {
|
||||
uri,
|
||||
transport: "",
|
||||
status: if index == 3 { 200 } else { 404 },
|
||||
headers: if index == 3 { VALID_PEER } else { "" },
|
||||
})
|
||||
.collect();
|
||||
let setup = scripted_setup(replies).unwrap();
|
||||
assert_eq!(setup.peer.1, 5004);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn video_setup_tries_client_udp_transport_after_all_empty_transport_forms() {
|
||||
let mut replies: Vec<_> = EMPTY_TRANSPORT_URIS
|
||||
.iter()
|
||||
.map(|uri| Reply {
|
||||
uri,
|
||||
transport: "",
|
||||
status: 461,
|
||||
headers: "",
|
||||
})
|
||||
.collect();
|
||||
replies.extend(
|
||||
EMPTY_TRANSPORT_URIS
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, uri)| Reply {
|
||||
uri,
|
||||
transport: "unicast;X-GS-ClientPort=49005-49006",
|
||||
status: if index == 3 { 200 } else { 400 },
|
||||
headers: if index == 3 { VALID_PEER } else { "" },
|
||||
}),
|
||||
);
|
||||
let setup = scripted_setup(replies).unwrap();
|
||||
assert_eq!(setup.peer.1, 5004);
|
||||
assert_eq!(
|
||||
header_value(&setup.response, "x-nv-ping-payload"),
|
||||
Some("00ff")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn video_setup_never_invents_a_peer_when_success_omits_transport() {
|
||||
// Single peerless 200: the sweep stops immediately (one scripted reply)
|
||||
// instead of trying further forms that would poison strict sessions.
|
||||
let error = match scripted_setup(vec![Reply {
|
||||
uri: EMPTY_TRANSPORT_URIS[0],
|
||||
transport: "",
|
||||
status: 200,
|
||||
headers: "",
|
||||
}]) {
|
||||
Ok(_) => panic!("SETUP without a peer must not succeed"),
|
||||
Err(error) => error,
|
||||
};
|
||||
assert_eq!(error.code, "missing-video-peer");
|
||||
assert!(error.message.contains("peerless 200"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn video_setup_rejects_partial_and_invalid_transport_metadata() {
|
||||
// Present-but-unusable Transport (missing/invalid source or ports) also
|
||||
// stops the sweep: no live rig has ever yielded a peer on a later form
|
||||
// after a peerless 200, while extra SETUPs poison strict sessions.
|
||||
for headers in [
|
||||
"Transport: unicast;X-GS-ServerPort=5004\r\n",
|
||||
"Transport: unicast;source=192.0.2.10\r\n",
|
||||
"Transport: unicast;source=not-an-ip;X-GS-ServerPort=5004\r\n",
|
||||
] {
|
||||
let error = match scripted_setup(vec![Reply {
|
||||
uri: EMPTY_TRANSPORT_URIS[0],
|
||||
transport: "",
|
||||
status: 200,
|
||||
headers,
|
||||
}]) {
|
||||
Ok(_) => panic!("unusable Transport must not yield a peer"),
|
||||
Err(error) => error,
|
||||
};
|
||||
assert_eq!(error.code, "missing-video-peer");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn video_setup_stops_on_auth_session_and_server_errors() {
|
||||
for status in [401, 403, 454, 455, 500, 503] {
|
||||
let result = scripted_setup(vec![Reply {
|
||||
uri: EMPTY_TRANSPORT_URIS[0],
|
||||
transport: "",
|
||||
status,
|
||||
headers: "",
|
||||
}]);
|
||||
let error = match result {
|
||||
Ok(_) => panic!("a fatal SETUP response must not succeed"),
|
||||
Err(error) => error,
|
||||
};
|
||||
assert_eq!(error.code, "nvst-rtsp-failed");
|
||||
assert!(error.message.contains(&status.to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn video_setup_candidates_match_mac_order_without_duplicates() {
|
||||
assert_eq!(
|
||||
video_setup_candidates("streamid=video/0", TARGET),
|
||||
EMPTY_TRANSPORT_URIS
|
||||
);
|
||||
assert_eq!(
|
||||
video_setup_candidates("streamid=video/0/0", TARGET),
|
||||
vec![
|
||||
"streamid=video/0/0",
|
||||
"rtsps://seat.nvidiagrid.net:322/streamid=video/0/0",
|
||||
]
|
||||
);
|
||||
assert_eq!(
|
||||
video_setup_candidates("/streamid=video/0", TARGET),
|
||||
vec![
|
||||
"/streamid=video/0",
|
||||
"rtsps://seat.nvidiagrid.net:322/streamid=video/0",
|
||||
]
|
||||
);
|
||||
for absolute in [
|
||||
"rtsp://seat.nvidiagrid.net:322/video",
|
||||
"rtsps://seat.nvidiagrid.net:322/video",
|
||||
] {
|
||||
assert_eq!(video_setup_candidates(absolute, TARGET), vec![absolute]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rtsp_request_deadline_bounds_a_partial_response() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let stream = TcpStream::connect(address).unwrap();
|
||||
let (server_stream, _) = listener.accept().unwrap();
|
||||
for socket in [&stream, &server_stream] {
|
||||
socket
|
||||
.set_read_timeout(Some(Duration::from_secs(5)))
|
||||
.unwrap();
|
||||
socket
|
||||
.set_write_timeout(Some(Duration::from_secs(5)))
|
||||
.unwrap();
|
||||
}
|
||||
let (release, released) = mpsc::channel();
|
||||
let server = thread::spawn(move || {
|
||||
let mut socket = tungstenite::accept(server_stream).unwrap();
|
||||
assert!(matches!(socket.read().unwrap(), Message::Text(_)));
|
||||
socket
|
||||
.send(Message::Text("RTSP/1.0 200 OK\r\nCSeq: 1\r\n".into()))
|
||||
.unwrap();
|
||||
let _ = released.recv_timeout(Duration::from_secs(2));
|
||||
});
|
||||
let (socket, _) = tungstenite::client(
|
||||
format!("ws://{address}/rtsp"),
|
||||
MaybeTlsStream::Plain(stream),
|
||||
)
|
||||
.unwrap();
|
||||
let mut client = RtspClient {
|
||||
socket,
|
||||
cseq: 0,
|
||||
buffer: String::new(),
|
||||
};
|
||||
let start = Instant::now();
|
||||
let result = client.request_with_timeout(
|
||||
"SETUP",
|
||||
"streamid=video/0/0",
|
||||
&[],
|
||||
"",
|
||||
Duration::from_millis(80),
|
||||
);
|
||||
let elapsed = start.elapsed();
|
||||
let _ = release.send(());
|
||||
server.join().unwrap();
|
||||
let error = match result {
|
||||
Ok(_) => panic!("an incomplete RTSP response must time out"),
|
||||
Err(error) => error,
|
||||
};
|
||||
assert_eq!(error.code, "nvst-rtsp-timeout");
|
||||
assert!(
|
||||
elapsed < Duration::from_secs(1),
|
||||
"deadline exceeded: {elapsed:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn video_setup_resweeps_when_successes_omit_a_peer_until_the_rig_is_ready() {
|
||||
// Round 1 draws one peerless 200 and stops at it; round 2 re-sends the
|
||||
// same form and the rig has a peer by then. Mirrors a late-starting
|
||||
// encoder without spraying further forms that poison strict sessions.
|
||||
let replies = vec![
|
||||
Reply {
|
||||
uri: EMPTY_TRANSPORT_URIS[0],
|
||||
transport: "",
|
||||
status: 200,
|
||||
headers: "",
|
||||
},
|
||||
Reply {
|
||||
uri: EMPTY_TRANSPORT_URIS[0],
|
||||
transport: "",
|
||||
status: 200,
|
||||
headers: VALID_PEER,
|
||||
},
|
||||
];
|
||||
let setup = scripted_setup_with_retry(replies, 3, Duration::ZERO, None).unwrap();
|
||||
assert_eq!(setup.peer, ("192.0.2.10".to_owned(), 5004, 5005));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn video_setup_peer_retry_stays_bounded_and_keeps_the_terminal_code() {
|
||||
// Every round draws one peerless 200 and stops there: retries exhaust,
|
||||
// then the original missing-video-peer error (not a timeout, not a new
|
||||
// code) is returned.
|
||||
let replies = vec![
|
||||
Reply {
|
||||
uri: EMPTY_TRANSPORT_URIS[0],
|
||||
transport: "",
|
||||
status: 200,
|
||||
headers: "",
|
||||
};
|
||||
3
|
||||
];
|
||||
let error = match scripted_setup_with_retry(replies, 2, Duration::ZERO, None) {
|
||||
Ok(_) => panic!("SETUP without a peer must not succeed"),
|
||||
Err(error) => error,
|
||||
};
|
||||
assert_eq!(error.code, "missing-video-peer");
|
||||
assert!(error.message.contains("peerless 200"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn video_setup_never_retries_pure_rejections() {
|
||||
// No 200 seen at all: the forms are wrong for this server, so fail fast
|
||||
// without burning retry rounds.
|
||||
let replies: Vec<_> = ["", "unicast;X-GS-ClientPort=49005-49006"]
|
||||
.iter()
|
||||
.flat_map(|transport| {
|
||||
EMPTY_TRANSPORT_URIS.iter().map(move |uri| Reply {
|
||||
uri,
|
||||
transport,
|
||||
status: 400,
|
||||
headers: "",
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let error = match scripted_setup_with_retry(replies, 3, Duration::ZERO, None) {
|
||||
Ok(_) => panic!("rejected SETUP forms must not succeed"),
|
||||
Err(error) => error,
|
||||
};
|
||||
assert_eq!(error.code, "nvst-rtsp-failed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn video_setup_degraded_retry_round_keeps_missing_peer_code() {
|
||||
// Round 1 draws a peerless 200 and stops, so a retry is scheduled; round
|
||||
// 2 then degrades to pure 400s (repeat SETUPs rejected). The terminal code
|
||||
// must stay missing-video-peer so callers can try the bundle peer instead
|
||||
// of treating it as wrong forms. Round 2 sweeps all forms because it drew
|
||||
// no 200 at all.
|
||||
let mut replies = vec![Reply {
|
||||
uri: EMPTY_TRANSPORT_URIS[0],
|
||||
transport: "",
|
||||
status: 200,
|
||||
headers: "",
|
||||
}];
|
||||
replies.extend(
|
||||
["", "unicast;X-GS-ClientPort=49005-49006"]
|
||||
.iter()
|
||||
.flat_map(|transport| {
|
||||
EMPTY_TRANSPORT_URIS.iter().map(move |uri| Reply {
|
||||
uri,
|
||||
transport,
|
||||
status: 400,
|
||||
headers: "",
|
||||
})
|
||||
}),
|
||||
);
|
||||
let error = match scripted_setup_with_retry(replies, 1, Duration::ZERO, None) {
|
||||
Ok(_) => panic!("peerless SETUP must not succeed"),
|
||||
Err(error) => error,
|
||||
};
|
||||
assert_eq!(error.code, "missing-video-peer");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn video_setup_uses_bundle_peer_on_first_peerless_success_without_retry() {
|
||||
let peerless = vec![Reply {
|
||||
uri: EMPTY_TRANSPORT_URIS[0],
|
||||
transport: "",
|
||||
status: 200,
|
||||
headers: "Transport: \r\nX-Nv-Ping: 6\r\nX-Nv-Ping-Payload: 00ff\r\n",
|
||||
}];
|
||||
let setup = scripted_setup_with_retry(
|
||||
peerless,
|
||||
3,
|
||||
Duration::ZERO,
|
||||
Some(("192.0.2.99".to_owned(), 13749, 13749)),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(setup.peer, ("192.0.2.99".to_owned(), 13749, 13749));
|
||||
assert!(setup.bundle_fallback);
|
||||
assert_eq!(
|
||||
header_value(&setup.response, "x-nv-ping-payload"),
|
||||
Some("00ff")
|
||||
);
|
||||
}
|
||||
Loaded 100 of 763 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user