import { createLogger, type Logger } from '@snowluma/common/logger'; import { pickDispatchJson, resolveReportOptions, type DispatchPayload, type EventReportOptions, } from '../event-filter'; import type { HttpClientNetwork, JsonObject } from '../types'; import { IOneBotNetworkAdapter, NetworkReloadType, type NetworkAdapterContext } from './adapter'; import { executeQuickOperation } from './quick-operation'; const moduleLog = createLogger('OneBot.POST'); const DEFAULT_TIMEOUT_MS = 5000; export class HttpPostAdapter extends IOneBotNetworkAdapter { private options: EventReportOptions; private signature_: string; private readonly log: Logger; constructor(name: string, config: HttpClientNetwork, ctx: NetworkAdapterContext) { super(name, config, ctx); this.options = resolveReportOptions(config); this.signature_ = bindingSignature(config); const uinNum = Number.parseInt(ctx.uin, 10); this.log = Number.isFinite(uinNum) && uinNum > 0 ? moduleLog.child({ uin: uinNum }) : moduleLog; } override get isActive(): boolean { return this.isEnabled; } open(): void { if (this.config.enabled === false) return; if (!this.config.url) return; this.isEnabled = true; } close(): void { this.isEnabled = false; } async reload(next: HttpClientNetwork): Promise { const wasEnabled = this.isEnabled; const willEnable = next.enabled !== false && !!next.url; this.config = structuredClone(next); this.options = resolveReportOptions(next); const newSig = bindingSignature(next); const sigChanged = newSig !== this.signature_; this.signature_ = newSig; if (sigChanged && wasEnabled) { this.close(); if (willEnable) { this.open(); return NetworkReloadType.Reopened; } return NetworkReloadType.Closed; } if (!wasEnabled && willEnable) { this.open(); return NetworkReloadType.Opened; } if (wasEnabled && !willEnable) { this.close(); return NetworkReloadType.Closed; } return NetworkReloadType.Normal; } onEvent(event: JsonObject, payload: DispatchPayload): void { if (!this.isEnabled) return; const json = pickDispatchJson(payload, this.options); if (json === null) return; void this.postEvent(json, event).catch((err) => { this.log.warn('[%s] postEvent threw: %s', this.name, err instanceof Error ? (err.stack ?? err.message) : String(err)); }); } private async postEvent(payload: string, event: JsonObject): Promise { if (!this.isEnabled) return; const headers: Record = { 'Content-Type': 'application/json', 'User-Agent': 'OneBot', 'X-Self-ID': this.ctx.uin, }; if (this.config.accessToken) { headers['X-Signature'] = await computeHmacSha1(this.config.accessToken, payload); } const timeoutMs = this.config.timeoutMs ?? DEFAULT_TIMEOUT_MS; try { const response = await fetch(this.config.url, { method: 'POST', headers, body: payload, signal: AbortSignal.timeout(timeoutMs), }); if (response.ok) { const contentType = response.headers.get('content-type') ?? ''; if (contentType.includes('application/json')) { const body = await response.text(); if (body.trim()) { await this.handleQuickOperation(event, body); } } } else { this.log.warn('[%s] POST %s returned %d', this.name, this.config.url, response.status); } } catch (error) { if (this.isEnabled) { this.log.warn('[%s] POST %s failed: %s', this.name, this.config.url, error instanceof Error ? error.message : String(error)); } } } private async handleQuickOperation(event: JsonObject, responseBody: string): Promise { try { const operation = JSON.parse(responseBody) as Record; if (!operation || typeof operation !== 'object') return; await executeQuickOperation(event, operation, this.ctx.api); } catch (error) { this.log.warn('[%s] quick operation failed: %s', this.name, error instanceof Error ? error.message : String(error)); } } } function bindingSignature(net: HttpClientNetwork): string { return `${net.url}#${net.accessToken ?? ''}#${net.timeoutMs ?? DEFAULT_TIMEOUT_MS}`; } async function computeHmacSha1(secret: string, payload: string): Promise { const { createHmac } = await import('crypto'); return 'sha1=' + createHmac('sha1', secret).update(payload).digest('hex'); }