The rkey fallback endpoint is deployment infra (one self-hosted server serves
all accounts; fallback rkeys are fungible), so it doesn't belong in per-UIN
onebot_<uin>.json. Introduce config/snowluma.json — a generic home for small
all-accounts SnowLuma settings — and move rkey.fallbackServers there.
- New onebot/global-config.ts: load/save/normalize with section-merge + atomic
write; http(s)+host URL validation; default [] (off). Lives in onebot (not
core) because the onebot manager constructs instances and must not depend on
core.
- OneBotInstance takes globalSettings; manager loads it on session start and
exposes reloadGlobalSettings() to hot-push edits to every live instance via
applyGlobalSettings → RKeyCache.setFallbackServers.
- Remove rkey from per-UIN OneBotConfig + the WebUI per-UIN round-trip (never
released, so no migration). Register snowluma.json in the backup allowlist.
Reworks the per-UIN rkey from 12b0190. Phase 1 of the global-settings split.
188 lines
6.2 KiB
TypeScript
188 lines
6.2 KiB
TypeScript
import { createLogger } from '@snowluma/common/logger';
|
|
import type { BridgeInterface } from '@snowluma/core/bridge-interface';
|
|
import type { BridgeManager } from '@snowluma/core/manager';
|
|
import { loadOneBotConfig } from './config';
|
|
import { loadGlobalSettings } from './global-config';
|
|
import { OneBotInstance } from './instance';
|
|
import type { AdapterStatus } from './network';
|
|
|
|
const log = createLogger('OneBot');
|
|
const VERBOSE_WARMUP = process.env.SNOWLUMA_VERBOSE_WARMUP === '1';
|
|
|
|
/** Per-account OneBot connection health, surfaced to the WebUI dashboard. */
|
|
export interface AccountConnections {
|
|
uin: string;
|
|
nickname: string;
|
|
adapters: AdapterStatus[];
|
|
}
|
|
|
|
export class OneBotManager {
|
|
private readonly instances = new Map<string, OneBotInstance>();
|
|
|
|
bind(bridgeManager: BridgeManager): void {
|
|
bridgeManager.addSessionStartedListener((uin, bridge) => {
|
|
this.onSessionStarted(uin, bridge);
|
|
});
|
|
|
|
bridgeManager.addSessionClosedListener((uin) => {
|
|
this.onSessionClosed(uin);
|
|
});
|
|
}
|
|
|
|
getInstance(uin: string): OneBotInstance | null {
|
|
return this.instances.get(uin) ?? null;
|
|
}
|
|
|
|
getInstances(): OneBotInstance[] {
|
|
return [...this.instances.values()];
|
|
}
|
|
|
|
/** Live OneBot adapter status for every account, for the WebUI dashboard. */
|
|
getConnectionStatuses(): AccountConnections[] {
|
|
return this.getInstances().map((i) => ({
|
|
uin: i.uin,
|
|
nickname: i.nickname,
|
|
adapters: i.getConnectionStatuses(),
|
|
}));
|
|
}
|
|
|
|
reloadConfig(uin: string): boolean {
|
|
const instance = this.instances.get(uin);
|
|
if (!instance) return false;
|
|
|
|
const config = loadOneBotConfig(uin, { persistDefaults: true });
|
|
instance.reloadConfig(config);
|
|
log.info('configuration reloaded: UIN=%s', uin);
|
|
return true;
|
|
}
|
|
|
|
/** Re-read global (all-accounts) settings from config/snowluma.json and push
|
|
* them to every live instance. Called after the WebUI saves global config. */
|
|
reloadGlobalSettings(): void {
|
|
const globalSettings = loadGlobalSettings();
|
|
for (const instance of this.instances.values()) {
|
|
instance.applyGlobalSettings(globalSettings);
|
|
}
|
|
log.info('global settings reloaded for %d instance(s)', this.instances.size);
|
|
}
|
|
|
|
dispose(): void {
|
|
for (const instance of this.instances.values()) {
|
|
instance.dispose();
|
|
}
|
|
this.instances.clear();
|
|
}
|
|
|
|
private onSessionStarted(uin: string, bridge: BridgeInterface): void {
|
|
if (this.instances.has(uin)) return;
|
|
|
|
const config = loadOneBotConfig(uin, { persistDefaults: true });
|
|
const instance = new OneBotInstance(uin, bridge, config, loadGlobalSettings());
|
|
|
|
const activePid = bridge.activePid;
|
|
if (activePid !== null) {
|
|
instance.addPid(activePid);
|
|
}
|
|
if (!bridge.identity.nickname) bridge.identity.nickname = uin;
|
|
|
|
this.instances.set(uin, instance);
|
|
log.info('session started: UIN=%s', uin);
|
|
warmUpBridgeState(uin, bridge).catch((err) => {
|
|
log.warn('warmup error for UIN %s: %s', uin, err instanceof Error ? (err.stack ?? err.message) : String(err));
|
|
});
|
|
}
|
|
|
|
private onSessionClosed(uin: string): void {
|
|
const instance = this.instances.get(uin);
|
|
if (!instance) return;
|
|
|
|
instance.dispose();
|
|
this.instances.delete(uin);
|
|
log.info('session closed: UIN=%s', uin);
|
|
}
|
|
|
|
}
|
|
|
|
async function warmUpBridgeState(uin: string, bridge: BridgeInterface): Promise<void> {
|
|
const selfUin = parseInt(uin, 10) || 0;
|
|
let selfResolved = false;
|
|
|
|
// Step 1: Fetch friend list + derive self profile when QQ happens to
|
|
// include self in the response. Some accounts / versions omit self,
|
|
// which used to leave identity.nickname empty — see step 1b for the
|
|
// explicit fallback.
|
|
try {
|
|
const friends = await bridge.apis.contacts.fetchFriendList();
|
|
log.info('friends loaded: UIN=%s count=%d', uin, friends.length);
|
|
|
|
for (const f of friends) {
|
|
if (f.uin === selfUin) {
|
|
bridge.identity.setSelfProfile({
|
|
uin: f.uin, uid: f.uid,
|
|
nickname: f.nickname || uin,
|
|
remark: '', qid: '', sex: 'unknown', age: 0, sign: '', avatar: '', level: 0,
|
|
});
|
|
bridge.identity.nickname = f.nickname || uin;
|
|
log.debug('self info: UIN=%s uid=%s nickname=%s', uin, f.uid, f.nickname ?? '');
|
|
selfResolved = true;
|
|
break;
|
|
}
|
|
}
|
|
} catch (e) {
|
|
log.warn('failed to load friends for UIN %s: %s', uin, e instanceof Error ? e.message : String(e));
|
|
}
|
|
|
|
// Step 1b: friend-list path didn't resolve self → fetch user profile
|
|
// directly via OIDB 0xFE1_2 so multi-account WebUI shows a nickname
|
|
// for every injected session, not just the ones where QQ echoed self
|
|
// back in the friend list.
|
|
if (!selfResolved && selfUin > 0) {
|
|
try {
|
|
const profile = await bridge.apis.contacts.fetchUserProfile(selfUin);
|
|
bridge.identity.setSelfProfile(profile);
|
|
bridge.identity.nickname = profile.nickname || uin;
|
|
log.debug('self info via profile: UIN=%s uid=%s nickname=%s',
|
|
uin, profile.uid, profile.nickname);
|
|
} catch (e) {
|
|
log.warn('failed to load self profile for UIN %s: %s',
|
|
uin, e instanceof Error ? e.message : String(e));
|
|
}
|
|
}
|
|
|
|
// Step 2: Fetch group list
|
|
let groups: { groupId: number }[] = [];
|
|
try {
|
|
groups = await bridge.apis.contacts.fetchGroupList();
|
|
log.info('groups loaded: UIN=%s count=%d', uin, groups.length);
|
|
} catch (e) {
|
|
log.warn('failed to load groups for UIN %s: %s', uin, e instanceof Error ? e.message : String(e));
|
|
}
|
|
|
|
// Step 3: Fetch members for each group
|
|
let loadedGroupCount = 0;
|
|
let loadedMemberCount = 0;
|
|
let failedGroupCount = 0;
|
|
for (const g of groups) {
|
|
try {
|
|
const members = await bridge.apis.contacts.fetchGroupMemberList(g.groupId);
|
|
loadedGroupCount += 1;
|
|
loadedMemberCount += members.length;
|
|
if (VERBOSE_WARMUP) {
|
|
log.debug('members loaded: group=%d count=%d', g.groupId, members.length);
|
|
}
|
|
} catch (e) {
|
|
failedGroupCount += 1;
|
|
log.warn('failed to load members for group %d: %s', g.groupId, e instanceof Error ? e.message : String(e));
|
|
}
|
|
}
|
|
|
|
log.info(
|
|
'member warmup completed: UIN=%s groups=%d/%d members=%d failed=%d',
|
|
uin,
|
|
loadedGroupCount,
|
|
groups.length,
|
|
loadedMemberCount,
|
|
failedGroupCount,
|
|
);
|
|
}
|