/* eslint-disable no-console */ import Inject, { RegisterSingleton, ServiceProvider } from "@entity-access/entity-access/dist/di/di.js"; import { cacheFor, ICacheContainer, ObjectCache } from "../../common/cacheFor.js"; import MessagingService from "../services/message-events/MessagingService.js"; import SocialMailContext from "../model/SocialMailContext.js"; import { globalEnv } from "../../common/globalEnv.js"; import CryptoService from "../services/CryptoService.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; @RegisterSingleton export default class VirtualHost implements ICacheContainer { cache: ObjectCache; @Inject ms: MessagingService; constructor() { setTimeout(() => { this.ms.refreshDns.on(({ serverID }) => { this.cache?.clear("findEmailHost"); this.cache?.clear("findHost"); }).catch(console.error); }, 1); } @cacheFor() async findHost(host: string, retry = 1) { using scope = ServiceProvider.createScope(this); const db = scope.resolve(SocialMailContext); if(globalEnv.ips.includes(host)) { host = globalEnv.host; } host = host.trim().toLocaleLowerCase(); if (host === globalEnv.host) { // ensure server is created... return await this.ensureRootServer(db); } // console.log(`Looking up host ${host} for ${globalEnv.wildcardDomain}`); if (host.endsWith(".root." + globalEnv.wildcardDomain)) { // this is server id... const { groups } = /^(?\d{1,16})\.root\./i.exec(host) ?? {}; const sID = groups?.id as any; if (sID) { const s = await db.virtualServers.where({ sID}, (x, p) => x.serverID === p.sID).first(); if (s) { return { server: s, host: `*.root.${globalEnv.wildcardDomain}` }; } } } { // this is server id... const { groups } = /^(?\d{1,16})\-(?[^\.]{1,400})\.sites\.(?.*)$/i.exec(host) ?? {}; const sID = groups?.sID as any; if (sID) { const s = await db.virtualServers.where({ sID }, (x, p) => x.serverID === p.sID).first(); if (s) { // verify that host exists.. const env = JSON.parse(s.environmentVariables) ?? {}; return { server: s, host: `*.sites.${env.SOCIAL_MAIL_WILDCARD_DOMAIN || globalEnv.wildcardDomain}` }; } } } // find existing virtual server const vs = await db.virtualServers.where({ host }, (x, p) => x.host === p.host) .first(); if (vs) { return { server: vs, host }; } // for everything else... lets try the dns const types = ["A", "AAAA", "CNAME"]; const hostedRecord = await db.cachedDnsRecords.where({ host, types }, (x, p) => x.fqdn === p.host && Sql.in(x.type,p.types) ) .include((x) => x.virtualServer) .first(); if (!hostedRecord) { // if (retry) { // // we need to sync every server... // // this needs to be done with a lock // const dnsSync = ServiceProvider.resolve(this, DnsSyncService); // await dnsSync.syncServers(); // return this.findHost(host, 0); // } console.warn(`Host ${host} not hosted on system`); return await this.ensureRootServer(db); } return { server: hostedRecord?.virtualServer, host }; } @cacheFor() async findEmailHost(host: string, retry = 1) { using scope = ServiceProvider.createScope(this); const db = scope.resolve(SocialMailContext); const hostedRecord = await db.cachedDnsRecords.where({ host }, (x, p) => (x.fqdn === p.host ) && x.type === "MX" ) .include((x) => x.virtualServer) .first(); if (!hostedRecord) { // if (retry) { // // we need to sync every server... // // this needs to be done with a lock // const dnsSync = ServiceProvider.resolve(this, DnsSyncService); // await dnsSync.syncServers(); // return this.findEmailHost(host, 0); // } console.warn(`Host "${host}" not hosted on system`); return { server: null, host: globalEnv.host }; } return { server: hostedRecord?.virtualServer, host }; } async ensureRootServer(db: SocialMailContext) { const host = globalEnv.host; let server = await db.virtualServers .where({ servername: host }, (x, p) => x.host === p.servername) .first(); if (server) { return { server, host }; } const cryptoService = ServiceProvider.resolve(this, CryptoService); const { privateKey, publicKey } = await cryptoService.generateKey(); server = await db.virtualServers.statements.selectOrInsert({ host, bucketName: "", database: "", serverName: "Root", forwardPrivateKey: privateKey, forwardPublicKey: publicKey }, { host }); return { server, host }; } }