fix(bridge): synchronize reconciled login state
Route recovered login state through the hook client before dependent services start. Add regression coverage to keep packet actions available after reconciliation. Fixes #286.
This commit is contained in:
3 files changed
+71
-5
No files matched your search
@@ -507,7 +507,14 @@ export class HookSession extends EventEmitter {
|
|||||||
this.log.info('login reconciled via active probe: PID=%d UIN=%s', this.pid, info.uin);
|
this.log.info('login reconciled via active probe: PID=%d UIN=%s', this.pid, info.uin);
|
||||||
// isRealUin guarantees a pure non-empty digit string, so BigInt() here
|
// isRealUin guarantees a pure non-empty digit string, so BigInt() here
|
||||||
// cannot throw (keep that contract if isRealUin's regex is ever changed).
|
// cannot throw (keep that contract if isRealUin's regex is ever changed).
|
||||||
this.handleLoginState({ loggedIn: true, uin: info.uin, uinNumber: BigInt(info.uin) });
|
// Reconcile through QqHookClient instead of mutating HookSession alone:
|
||||||
|
// packet sending and login waiters must observe the same state before
|
||||||
|
// the login event starts Bridge / OneBot initialization.
|
||||||
|
this.client!.reconcileLoginState({
|
||||||
|
loggedIn: true,
|
||||||
|
uin: info.uin,
|
||||||
|
uinNumber: BigInt(info.uin),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
this.probing = false;
|
this.probing = false;
|
||||||
|
|||||||
@@ -374,6 +374,19 @@ export class QqHookClient extends EventEmitter {
|
|||||||
return { ...this.loginState };
|
return { ...this.loginState };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reconcile a login edge that the native hook could not replay after the
|
||||||
|
* pipe connected. The client remains the single source of truth so packet
|
||||||
|
* sending, login waiters, and HookSession all observe the same transition.
|
||||||
|
*/
|
||||||
|
reconcileLoginState(state: QqHookLoginState): void {
|
||||||
|
this.applyLoginState({
|
||||||
|
loggedIn: state.loggedIn,
|
||||||
|
uin: state.uin || state.uinNumber.toString(),
|
||||||
|
uinNumber: state.uinNumber,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async waitForLogin({ timeoutMs = 0 } = {}): Promise<QqHookLoginState> {
|
async waitForLogin({ timeoutMs = 0 } = {}): Promise<QqHookLoginState> {
|
||||||
await this.connect();
|
await this.connect();
|
||||||
if (this.loginState.loggedIn) {
|
if (this.loginState.loggedIn) {
|
||||||
@@ -750,8 +763,11 @@ export class QqHookClient extends EventEmitter {
|
|||||||
const loggedIn = flaggedLoggedIn || statusLoggedIn;
|
const loggedIn = flaggedLoggedIn || statusLoggedIn;
|
||||||
const uinNumber = BigInt(frame.value0);
|
const uinNumber = BigInt(frame.value0);
|
||||||
const uin = frame.msg || uinNumber.toString();
|
const uin = frame.msg || uinNumber.toString();
|
||||||
|
this.applyLoginState({ loggedIn, uin, uinNumber });
|
||||||
|
}
|
||||||
|
|
||||||
|
private applyLoginState(next: QqHookLoginState): void {
|
||||||
const previous = this.loginState;
|
const previous = this.loginState;
|
||||||
const next = { loggedIn, uin, uinNumber };
|
|
||||||
this.loginState = next;
|
this.loginState = next;
|
||||||
this.emit('loginState', next);
|
this.emit('loginState', next);
|
||||||
if (previous.loggedIn !== next.loggedIn || previous.uin !== next.uin) {
|
if (previous.loggedIn !== next.loggedIn || previous.uin !== next.uin) {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { HookSession } from '../src/hook-session';
|
|||||||
import type { ManualMapHandle } from '../src/injector';
|
import type { ManualMapHandle } from '../src/injector';
|
||||||
import type { QqHookClient, QqHookPacket } from '../src/qq-hook-client';
|
import type { QqHookClient, QqHookPacket } from '../src/qq-hook-client';
|
||||||
import type { QqPortLoginInfo } from '../src/qq-port-probe';
|
import type { QqPortLoginInfo } from '../src/qq-port-probe';
|
||||||
|
import type { PacketSender } from '@snowluma/common/packet-sender';
|
||||||
import type { PacketSink } from '@snowluma/common/protocol-types';
|
import type { PacketSink } from '@snowluma/common/protocol-types';
|
||||||
|
|
||||||
const DUMMY_HANDLE: ManualMapHandle = { base: 0n, entry: 0n, exceptionTable: 0n, size: 0 };
|
const DUMMY_HANDLE: ManualMapHandle = { base: 0n, entry: 0n, exceptionTable: 0n, size: 0 };
|
||||||
@@ -14,6 +15,7 @@ class FakeClient extends EventEmitter {
|
|||||||
isClosed = false;
|
isClosed = false;
|
||||||
isLoggedIn = false;
|
isLoggedIn = false;
|
||||||
shouldFailConnect = false;
|
shouldFailConnect = false;
|
||||||
|
readonly sentPackets: Array<{ serviceCmd: string; body: Buffer }> = [];
|
||||||
private loginState = { loggedIn: false, uin: '0', uinNumber: 0n };
|
private loginState = { loggedIn: false, uin: '0', uinNumber: 0n };
|
||||||
|
|
||||||
async connectAll(_opts: { recv: boolean }): Promise<void> {
|
async connectAll(_opts: { recv: boolean }): Promise<void> {
|
||||||
@@ -21,6 +23,20 @@ class FakeClient extends EventEmitter {
|
|||||||
if (this.isClosed) throw new Error('client is closed');
|
if (this.isClosed) throw new Error('client is closed');
|
||||||
}
|
}
|
||||||
getLoginState() { return { ...this.loginState }; }
|
getLoginState() { return { ...this.loginState }; }
|
||||||
|
reconcileLoginState(state: { loggedIn: boolean; uin: string; uinNumber: bigint }): void {
|
||||||
|
this.isLoggedIn = state.loggedIn;
|
||||||
|
this.loginState = { ...state };
|
||||||
|
this.emit('loginState', { ...this.loginState });
|
||||||
|
}
|
||||||
|
async send(serviceCmd: string, body: Buffer) {
|
||||||
|
this.sentPackets.push({ serviceCmd, body: Buffer.from(body) });
|
||||||
|
return {
|
||||||
|
requestId: this.sentPackets.length,
|
||||||
|
error: 0,
|
||||||
|
message: '',
|
||||||
|
body: Buffer.from('reply'),
|
||||||
|
};
|
||||||
|
}
|
||||||
close(): void {
|
close(): void {
|
||||||
if (this.isClosed) return;
|
if (this.isClosed) return;
|
||||||
this.isClosed = true;
|
this.isClosed = true;
|
||||||
@@ -28,9 +44,7 @@ class FakeClient extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fireLogin(uin: string): void {
|
fireLogin(uin: string): void {
|
||||||
this.isLoggedIn = true;
|
this.reconcileLoginState({ loggedIn: true, uin, uinNumber: BigInt(uin) });
|
||||||
this.loginState = { loggedIn: true, uin, uinNumber: BigInt(uin) };
|
|
||||||
this.emit('loginState', { ...this.loginState });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
firePacket(packet: QqHookPacket): void {
|
firePacket(packet: QqHookPacket): void {
|
||||||
@@ -541,6 +555,35 @@ describe('HookSession — packet forwarding', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('HookSession — login reconcile (Docker auto-login safety net)', () => {
|
describe('HookSession — login reconcile (Docker auto-login safety net)', () => {
|
||||||
|
it('keeps the packet sender usable when login is reconciled by the active probe', async () => {
|
||||||
|
const ctx = makeSession({
|
||||||
|
pipeLive: true,
|
||||||
|
probeLogin: async () => ({ port: 4301, uin: '10001', loggedIn: true }),
|
||||||
|
});
|
||||||
|
let sender: PacketSender | undefined;
|
||||||
|
ctx.session.on('login', (_uin: string, nextSender: PacketSender) => {
|
||||||
|
sender = nextSender;
|
||||||
|
});
|
||||||
|
|
||||||
|
await ctx.session.load();
|
||||||
|
ctx.session.onPipeUp();
|
||||||
|
await flush();
|
||||||
|
await flush();
|
||||||
|
|
||||||
|
expect(sender).toBeDefined();
|
||||||
|
const result = await sender!.sendPacket('Test.LoginReady', Buffer.from('request'));
|
||||||
|
|
||||||
|
expect(result).toMatchObject({
|
||||||
|
success: true,
|
||||||
|
gotResponse: true,
|
||||||
|
errorCode: 0,
|
||||||
|
responseData: Buffer.from('reply'),
|
||||||
|
});
|
||||||
|
expect(ctx.currentClient().sentPackets).toEqual([
|
||||||
|
{ serviceCmd: 'Test.LoginReady', body: Buffer.from('request') },
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
it('reconciles login via the active probe when the pushed frame is missed', async () => {
|
it('reconciles login via the active probe when the pushed frame is missed', async () => {
|
||||||
// Probe reports logged-in; the FakeClient never fires a loginState frame
|
// Probe reports logged-in; the FakeClient never fires a loginState frame
|
||||||
// (the missed-edge Docker case). The immediate reconcile probe must detect
|
// (the missed-edge Docker case). The immediate reconcile probe must detect
|
||||||
|
|||||||
Reference in new issue
Block a user