From ac2c185d4cc072305c21fd8602bdbc01b7d2e392 Mon Sep 17 00:00:00 2001 From: N0VAN0M4LY <224130499+N0VAN0M4LY@users.noreply.github.com> Date: Tue, 24 Nov 2026 09:05:31 +0800 Subject: [PATCH] feat(accounts): persist EULA acceptance Persist the accepted EULA version before responding and return both agreement fields in the profile. Respond to matching heartbeats. --- .../src/char/actions/account-context.ts | 10 ++++ .../src/char/actions/action-table.ts | 2 + .../src/char/actions/agreement-actions.ts | 57 +++++++++++++++++++ .../src/char/actions/online-init-actions.ts | 10 +--- .../src/matching/matching-message.ts | 19 +++++++ .../src/matching/matching-service.ts | 12 +++- .../src/profile/player-profiles.ts | 8 +++ 7 files changed, 108 insertions(+), 10 deletions(-) create mode 100644 packages/local-service/src/char/actions/account-context.ts create mode 100644 packages/local-service/src/char/actions/agreement-actions.ts diff --git a/packages/local-service/src/char/actions/account-context.ts b/packages/local-service/src/char/actions/account-context.ts new file mode 100644 index 0000000..f0d494a --- /dev/null +++ b/packages/local-service/src/char/actions/account-context.ts @@ -0,0 +1,10 @@ +/** Resolving the player a char request is for (the uidhint header, implementation notes). */ +import type { Account, AccountStore } from '../../accounts/account-store.ts'; +import type { CharAnswer } from '../char-answers.ts'; +import type { CharActionContext } from '../char-action.ts'; + +/** Run the handler for the request's account; an unknown or missing user id is an authorisation error. */ +export function withAccount(context: CharActionContext, accounts: AccountStore, run: (account: Account) => CharAnswer): CharAnswer { + const account = context.userId === undefined ? undefined : accounts.byUserId(context.userId); + return account ? run(account) : { kind: 'error', code: 'AUTH_REQ_ERROR' }; +} diff --git a/packages/local-service/src/char/actions/action-table.ts b/packages/local-service/src/char/actions/action-table.ts index a33e78e..c4236f7 100644 --- a/packages/local-service/src/char/actions/action-table.ts +++ b/packages/local-service/src/char/actions/action-table.ts @@ -2,6 +2,7 @@ import type { AccountStore } from '../../accounts/account-store.ts'; import type { PlayerProfiles } from '../../profile/player-profiles.ts'; import type { CharActionHandler } from '../char-action.ts'; +import { acceptEula } from './agreement-actions.ts'; import { entitlementsFull, initialMeta, legacyUserData } from './online-init-actions.ts'; export interface ActionDependencies { @@ -14,5 +15,6 @@ export function actionTable(deps: ActionDependencies): ReadonlyMap withAccount(context, accounts, (account) => { + const version = intParam(context.payload, 'version'); + const type = intParam(context.payload, 'type'); + if (version === undefined || type !== EULA_AGREEMENT_TYPE) return { kind: 'error', code: 'BAD_PARAMS' }; + + const profile = profiles.update(account.userId, (saved) => { + setParam(saved, EULA_VERSION, { type: 'int', value: version }); + }); + return { kind: 'data', blk: acceptedVersions(profile) }; + }); +} + +/** Both versions, because the client overwrites both from the answer (a missing one reads as 0). */ +function acceptedVersions(profile: BlkBlock): BlkBlock { + const answer = emptyBlock(); + setParam(answer, EULA_VERSION, { type: 'int', value: intParam(profile, EULA_VERSION) ?? 0 }); + setParam(answer, NDA_VERSION, { type: 'int', value: intParam(profile, NDA_VERSION) ?? 0 }); + return answer; +} + +function intParam(block: BlkBlock | undefined, name: string): number | undefined { + const value = block ? findParam(block, name)?.value : undefined; + return value?.type === 'int' ? value.value : undefined; +} diff --git a/packages/local-service/src/char/actions/online-init-actions.ts b/packages/local-service/src/char/actions/online-init-actions.ts index bf89592..7a9fa19 100644 --- a/packages/local-service/src/char/actions/online-init-actions.ts +++ b/packages/local-service/src/char/actions/online-init-actions.ts @@ -3,11 +3,11 @@ *. */ import { emptyBlock } from '@wto/blk'; -import type { Account, AccountStore } from '../../accounts/account-store.ts'; +import type { AccountStore } from '../../accounts/account-store.ts'; import { clientProfile } from '../../profile/client-profile.ts'; import type { PlayerProfiles } from '../../profile/player-profiles.ts'; -import type { CharAnswer } from '../char-answers.ts'; -import type { CharActionContext, CharActionHandler } from '../char-action.ts'; +import type { CharActionHandler } from '../char-action.ts'; +import { withAccount } from './account-context.ts'; /** * cln_get_initial_meta: server-side metadata the client reads in GameSaveData's initial-meta @@ -47,7 +47,3 @@ function profileAnswer(profiles: PlayerProfiles, accounts: AccountStore): CharAc })); } -function withAccount(context: CharActionContext, accounts: AccountStore, run: (account: Account) => CharAnswer): CharAnswer { - const account = context.userId === undefined ? undefined : accounts.byUserId(context.userId); - return account ? run(account) : { kind: 'error', code: 'AUTH_REQ_ERROR' }; -} diff --git a/packages/local-service/src/matching/matching-message.ts b/packages/local-service/src/matching/matching-message.ts index ac4f184..7f9d82c 100644 --- a/packages/local-service/src/matching/matching-message.ts +++ b/packages/local-service/src/matching/matching-message.ts @@ -49,6 +49,25 @@ export interface MatchingRequest { readonly params: Record; } +/** + * Value of "type" in the client's keep-alive: about 30 s after the last traffic the client + * sends {"id", "type": 100} through the same request sender as named requests (0x44ea0e0, + * called at 0x44edf0b) with its own completion callback (0x44f21d0). The answer handler + * finds it by "id" like any request; status 0 logs "got CLN_PING response", and no answer + * within 15 s closes the connection ("CLN_PING got no response"). + */ +export const PING_TYPE = 100; + +/** The ping's id, or undefined when the message is not a keep-alive. */ +export function asPing(message: MatchingMessage): number | undefined { + const { id, type } = message.json; + return message.kind === MessageKind.Request && type === PING_TYPE && typeof id === 'number' ? id : undefined; +} + +export function pingAnswer(id: number): MatchingMessage { + return { kind: MessageKind.Answer, json: { id, type: PING_TYPE } }; +} + /** Undefined when the message is not a request the client expects an answer to. */ export function asRequest(message: MatchingMessage): MatchingRequest | undefined { if (message.kind !== MessageKind.Request) return undefined; diff --git a/packages/local-service/src/matching/matching-service.ts b/packages/local-service/src/matching/matching-service.ts index ccc91b9..99509ef 100644 --- a/packages/local-service/src/matching/matching-service.ts +++ b/packages/local-service/src/matching/matching-service.ts @@ -8,13 +8,13 @@ * * Every request gets an answer. A request with no handler yet is answered with success and * empty data and marked unhandled in the traffic log, so the client carries on and shows - * what it asks for next (the same policy as the char service). Messages that are not - * requests are only logged. + * what it asks for next (the same policy as the char service). Keep-alive pings are + * answered with their id (matching-message.ts). Other messages are only logged. */ import type { Socket } from 'node:net'; import type { TrafficLog } from '../traffic-log/traffic-log.ts'; import { encodePacket, PacketReader } from './matching-frame.ts'; -import { answerTo, asRequest, decodeMessage, encodeMessage, type MatchingMessage } from './matching-message.ts'; +import { answerTo, asPing, asRequest, decodeMessage, encodeMessage, pingAnswer, type MatchingMessage } from './matching-message.ts'; import { MATCHING_REQUESTS, type MatchingSessionControl } from './matching-requests.ts'; import { messageLogText } from './message-redaction.ts'; import { EnvelopeReader, sealEnvelope } from './session-crypto/encrypted-envelope.ts'; @@ -78,6 +78,12 @@ class MatchingSession { } #handle(message: MatchingMessage): void { + const pingId = asPing(message); + if (pingId !== undefined) { + this.#record('in', message, `ping #${pingId}`, false); + this.#send(pingAnswer(pingId)); + return; + } const request = asRequest(message); if (!request) { this.#record('in', message, 'not a request', false); diff --git a/packages/local-service/src/profile/player-profiles.ts b/packages/local-service/src/profile/player-profiles.ts index 42e7fa3..f48e395 100644 --- a/packages/local-service/src/profile/player-profiles.ts +++ b/packages/local-service/src/profile/player-profiles.ts @@ -23,4 +23,12 @@ export class PlayerProfiles { this.#saves.saveProfile(userId, created); return created; } + + /** Change the player's profile and write it to the save before returning it. */ + update(userId: string, change: (profile: BlkBlock) => void): BlkBlock { + const profile = this.profileOf(userId); + change(profile); + this.#saves.saveProfile(userId, profile); + return profile; + } }