Verbindungsdaten schneller: ein KeyHelp-Aufruf statt vier

Postfach und Domains kommen aus /clients/{id}/resources (wie die Liste),
parallel zum Hostnamen, der 1 Stunde zwischengespeichert wird (~1,2 s → ~0,25 s).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Claude (KI) 2026-10-16 11:44:23 +02:00
parent 6a7b5446d5
commit fbc4ececd0
2 changed files with 19 additions and 7 deletions

View file

@ -150,6 +150,15 @@ export function createKeyHelpConnector(deps: KeyHelpDeps = {}): Connector {
return rows.filter((r) => String(r.id_user) === cid).map((r) => ({ id: String(r.id), kind: 'dirprotect' as const, name: String(r.path ?? ''), state: Number(r.status ?? 1) === 0 ? 'disabled' as const : 'active' as const,
details: { username: r.username ?? null, realm: r.auth_name ?? null, exceptions: Array.isArray(r.exceptions) ? r.exceptions : [] } }));
}
const hostOk = (h: unknown) => (typeof h === 'string' && DOMAIN_RE.test(h.trim()) ? h.trim().toLowerCase() : undefined);
const hostCache = new Map<string, { host: string; at: number }>();
/** Mailserver für die Verbindungsdaten: Einstellung mailServer, sonst Hostname des Panels (1 Stunde zwischengespeichert), sonst Host der Panel-Adresse. */
async function mailHost(ctx: ConnectorContext): Promise<string> {
const fixed = hostOk(ctx.config.mailServer); if (fixed) return fixed;
const key = base(ctx); const hit = hostCache.get(key); if (hit && now().getTime() - hit.at < 3_600_000) return hit.host;
try { const h = hostOk((await call<{ meta?: { hostname?: string } }>(ctx, 'GET', '/server'))?.meta?.hostname); if (h) { hostCache.set(key, { host: h, at: now().getTime() }); return h; } } catch { /* Ersatz unten */ }
return new URL(key).hostname;
}
const children: ChildAccess = {
async kinds(ctx, parentRef) {
const c = await getClient(ctx, parentRef); const p = c.permissions ?? {};
@ -213,16 +222,17 @@ export function createKeyHelpConnector(deps: KeyHelpDeps = {}): Connector {
*/
async connectionInfo(ctx, req) {
if (req.kind !== 'email') throw new ConnectorError('UNSUPPORTED', 'Verbindungsdaten gibt es nur für Postfächer');
const row = owned(await optional(call<Row>(ctx, 'GET', `/emails/${encodeURIComponent(str(req.id, 'id', 40))}`)), req.parentRef);
const eid = str(req.id, 'id', 40); const cid = req.parentRef;
// Ein Aufruf für Postfach + Domains des Kunden, parallel zum (zwischengespeicherten) Hostnamen.
const [res, host] = await Promise.all([call<Record<string, Row[]>>(ctx, 'GET', `/clients/${encodeURIComponent(cid)}/resources`), mailHost(ctx)]);
const mine = (rows: unknown) => (Array.isArray(rows) ? (rows as Row[]).filter((r) => String(r.id_user) === cid || r.id_user === undefined) : []);
const row = mine(res?.emails).find((r) => String(r.id) === eid); if (!row) throw new ConnectorError('NOT_FOUND');
const ascii = String(row.email ?? ''); const utf8 = String(row.email_utf8 || ascii);
if (!ascii.includes('@')) throw new ConnectorError('INVALID_RESPONSE', 'Postfach ohne Adresse');
let dom = owned(await optional(call<Row>(ctx, 'GET', `/domains/name/${encodeURIComponent(ascii.split('@')[1]!)}`)), req.parentRef);
for (let i = 0; i < 5 && Number(dom.id_parent_domain) > 0; i++) { const p = await optional(call<Row>(ctx, 'GET', `/domains/${encodeURIComponent(String(dom.id_parent_domain))}`)); if (!p || String(p.id_user) !== req.parentRef) break; dom = p; }
const doms = mine(res?.domains); const at = ascii.split('@')[1]!.toLowerCase();
let dom = doms.find((d) => String(d.domain ?? '').toLowerCase() === at); if (!dom) throw new ConnectorError('NOT_FOUND');
for (let i = 0; i < 5 && Number(dom.id_parent_domain) > 0; i++) { const p = doms.find((d) => String(d.id) === String(dom!.id_parent_domain)); if (!p) break; dom = p; }
const main = String(dom.domain ?? '').toLowerCase(); if (!DOMAIN_RE.test(main)) throw new ConnectorError('INVALID_RESPONSE', 'Hauptdomain ungültig');
const hostOk = (h: unknown) => (typeof h === 'string' && DOMAIN_RE.test(h.trim()) ? h.trim().toLowerCase() : undefined);
let host = hostOk(ctx.config.mailServer);
if (!host) { try { host = hostOk((await call<{ meta?: { hostname?: string } }>(ctx, 'GET', '/server'))?.meta?.hostname); } catch { /* Ersatz unten */ } }
host ??= new URL(base(ctx)).hostname;
const alternativeHosts = String(ctx.config.mailServerAlternatives ?? '').split(/[\s,]+/).map(hostOk).filter((h): h is string => !!h && h !== host);
const profile = (protocol: 'imap' | 'pop3') => `http://autoconfig-apple.${main}/setup.mobileconfig?email=${encodeURIComponent(utf8)}&protocol=${protocol}`;
return {

View file

@ -209,6 +209,8 @@ describe('KeyHelp-Connector: Verbindungsdaten für Postfächer', () => {
f.state.domains.push({ id: 12, id_user: 1, id_parent_domain: 10, domain: 'shop.alpha.example.test', domain_utf8: 'shop.alpha.example.test', status: 1, is_subdomain: true, security: {} });
f.state.emails.push({ id: 32, id_user: 1, email: 'xn--mller-kva@shop.alpha.example.test', email_utf8: 'müller@shop.alpha.example.test', status: 1, aliases: [], forwardings: [], password_hash: 'GEHEIM' });
const i = await ch.connectionInfo!(ctx(), { parentRef: '1', kind: 'email', id: '30' });
await ch.connectionInfo!(ctx(), { parentRef: '1', kind: 'email', id: '30' }); // zweiter Aufruf: Hostname aus dem Zwischenspeicher
expect(f.calls.map((c) => c.path)).toEqual(['/clients/1/resources', '/server', '/clients/1/resources']);
expect(i.host).toBe('kh.test'); expect(i.usernames).toEqual(['info@alpha.example.test']);
expect(i.ports.map((p) => `${p.protocol}:${p.port}:${p.security}`)).toEqual(['IMAP:993:tls', 'POP3:995:tls', 'SMTP:465:tls', 'IMAP:143:starttls', 'POP3:110:starttls', 'SMTP:587:starttls']);
expect(i.profiles[0]!.url).toBe('http://autoconfig-apple.alpha.example.test/setup.mobileconfig?email=info%40alpha.example.test&protocol=imap');