diff options
Diffstat (limited to 'deno')
-rw-r--r-- | deno/.gitignore | 3 | ||||
-rw-r--r-- | deno/base/config.ts | 93 | ||||
-rw-r--r-- | deno/base/cron.ts | 43 | ||||
-rw-r--r-- | deno/base/deno.json | 10 | ||||
-rw-r--r-- | deno/base/lib.ts | 10 | ||||
-rw-r--r-- | deno/base/log.ts | 60 | ||||
-rw-r--r-- | deno/deno.json | 19 | ||||
-rw-r--r-- | deno/deno.lock | 1334 | ||||
-rw-r--r-- | deno/mail-relay/app.ts | 84 | ||||
-rw-r--r-- | deno/mail-relay/aws/app.ts | 297 | ||||
-rw-r--r-- | deno/mail-relay/aws/deliver.ts | 60 | ||||
-rw-r--r-- | deno/mail-relay/aws/fetch.ts | 127 | ||||
-rw-r--r-- | deno/mail-relay/aws/mail.ts | 49 | ||||
-rw-r--r-- | deno/mail-relay/db.test.ts | 23 | ||||
-rw-r--r-- | deno/mail-relay/db.ts | 146 | ||||
-rw-r--r-- | deno/mail-relay/deno.json | 18 | ||||
-rw-r--r-- | deno/mail-relay/dovecot.ts | 99 | ||||
-rw-r--r-- | deno/mail-relay/dumb-smtp-server.ts | 130 | ||||
-rw-r--r-- | deno/mail-relay/mail.test.ts | 126 | ||||
-rw-r--r-- | deno/mail-relay/mail.ts | 330 | ||||
-rw-r--r-- | deno/tools/deno.json | 8 | ||||
-rw-r--r-- | deno/tools/generate-geosite-rules.ts | 160 | ||||
-rw-r--r-- | deno/tools/manage-service.ts | 42 | ||||
-rw-r--r-- | deno/tools/manage-vm.ts | 144 | ||||
-rw-r--r-- | deno/tools/template.ts | 124 |
25 files changed, 3539 insertions, 0 deletions
diff --git a/deno/.gitignore b/deno/.gitignore new file mode 100644 index 0000000..327aef0 --- /dev/null +++ b/deno/.gitignore @@ -0,0 +1,3 @@ +out +.env.local +db.sqlite diff --git a/deno/base/config.ts b/deno/base/config.ts new file mode 100644 index 0000000..a5f5d86 --- /dev/null +++ b/deno/base/config.ts @@ -0,0 +1,93 @@ +import { camelCaseToKebabCase } from "./lib.ts"; + +export interface ConfigDefinitionItem { + readonly description: string; + readonly default?: string; + readonly secret?: boolean; +} + +interface ConfigMapItem extends ConfigDefinitionItem { + readonly env: string; + value?: string; +} + +export type ConfigDefinition<K extends string = string> = Record< + K, + ConfigDefinitionItem +>; +type ConfigMap<K extends string = string> = Record<K, ConfigMapItem>; + +export class ConfigProvider<K extends string> { + readonly #prefix: string; + readonly #map: ConfigMap<K>; + + constructor(prefix: string, ...definitions: Partial<ConfigDefinition<K>>[]) { + this.#prefix = prefix; + + const map: ConfigMap = {}; + for (const definition of definitions) { + for (const [key, def] of Object.entries(definition as ConfigDefinition)) { + map[key] = { + ...def, + env: `${this.#prefix}-${camelCaseToKebabCase(key as string)}` + .replaceAll("-", "_") + .toUpperCase(), + }; + } + } + this.#map = map as ConfigMap<K>; + } + + resolveFromEnv(options?: { keys?: K[] }) { + const keys = options?.keys ?? Object.keys(this.#map); + for (const key of keys) { + const { env, description, default: _default } = this.#map[key as K]; + const value = Deno.env.get(env) ?? _default; + if (value == null) { + throw new Error(`Required env ${env} (${description}) is not set.`); + } + this.#map[key as K].value = value; + } + } + + get(key: K): string { + if (!(key in this.#map)) { + throw new Error(`Unknown config key ${key as string}.`); + } + if (this.#map[key].value == null) { + this.resolveFromEnv({ keys: [key] }); + } + return this.#map[key].value!; + } + + set(key: K, value: string) { + if (!(key in this.#map)) { + throw new Error(`Unknown config key ${key as string}.`); + } + this.#map[key].value = value; + } + + getInt(key: K): number { + return Number(this.get(key)); + } + + getList(key: K, separator: string = ","): string[] { + const value = this.get(key); + if (value.length === 0) return []; + return value.split(separator); + } + + [Symbol.for("Deno.customInspect")]() { + const getValueString = (item: ConfigMapItem): string => { + if (item.value == null) return "(unresolved)"; + if (item.secret === true) return "***"; + return item.value; + }; + + return Object.entries(this.#map as ConfigMap) + .map( + ([key, item]) => `${key} [env: ${item.env}]: ${getValueString(item)}`, + ) + .join("\n"); + } +} diff --git a/deno/base/cron.ts b/deno/base/cron.ts new file mode 100644 index 0000000..bf0a0be --- /dev/null +++ b/deno/base/cron.ts @@ -0,0 +1,43 @@ +export type CronCallback = (task: CronTask) => Promise<void>; + +export interface CronTaskConfig { + readonly name: string; + readonly interval: number; + readonly callback: CronCallback; + readonly startNow?: boolean; +} + +export class CronTask { + #timerTag: number | null = null; + + constructor(public readonly config: CronTaskConfig) { + if (config.interval <= 0) { + throw new Error("Cron task interval must be positive."); + } + + if (config.startNow === true) { + this.start(); + } + } + + get running(): boolean { + return this.#timerTag != null; + } + + start() { + if (this.#timerTag == null) { + this.#timerTag = setInterval( + this.config.callback, + this.config.interval, + this, + ); + } + } + + stop() { + if (this.#timerTag != null) { + clearInterval(this.#timerTag); + this.#timerTag = null; + } + } +} diff --git a/deno/base/deno.json b/deno/base/deno.json new file mode 100644 index 0000000..dabc02a --- /dev/null +++ b/deno/base/deno.json @@ -0,0 +1,10 @@ +{ + "name": "@crupest/base", + "version": "0.1.0", + "exports": { + ".": "./lib.ts", + "./config": "./config.ts", + "./cron": "./cron.ts", + "./log": "./log.ts" + } +} diff --git a/deno/base/lib.ts b/deno/base/lib.ts new file mode 100644 index 0000000..a5e4a6a --- /dev/null +++ b/deno/base/lib.ts @@ -0,0 +1,10 @@ +export function camelCaseToKebabCase(str: string): string { + return str.replace(/[A-Z]/g, (m) => "-" + m.toLowerCase()); +} + +export function toFileNameString(date: Date, dateOnly?: boolean): string { + const str = date.toISOString(); + return dateOnly === true + ? str.slice(0, str.indexOf("T")) + : str.replaceAll(/:|\./g, "-"); +} diff --git a/deno/base/log.ts b/deno/base/log.ts new file mode 100644 index 0000000..940f569 --- /dev/null +++ b/deno/base/log.ts @@ -0,0 +1,60 @@ +import { join } from "@std/path"; + +import { toFileNameString } from "./lib.ts"; + +export interface ExternalLogStream extends Disposable { + stream: WritableStream; +} + +export class LogFileProvider { + #directory: string; + + constructor(directory: string) { + this.#directory = directory; + Deno.mkdirSync(directory, { recursive: true }); + } + + async createExternalLogStream( + name: string, + options?: { + noTime?: boolean; + }, + ): Promise<ExternalLogStream> { + if (name.includes("/")) { + throw new Error(`External log stream's name (${name}) contains '/'.`); + } + + const logPath = join( + this.#directory, + options?.noTime === true + ? name + : `${name}-${toFileNameString(new Date())}`, + ); + + const file = await Deno.open(logPath, { + read: false, + write: true, + append: true, + create: true, + }); + return { + stream: file.writable, + [Symbol.dispose]: file[Symbol.dispose].bind(file), + }; + } + + async createExternalLogStreamsForProgram( + program: string, + ): Promise<{ stdout: WritableStream; stderr: WritableStream } & Disposable> { + const stdout = await this.createExternalLogStream(`${program}-stdout`); + const stderr = await this.createExternalLogStream(`${program}-stderr`); + return { + stdout: stdout.stream, + stderr: stderr.stream, + [Symbol.dispose]: () => { + stdout[Symbol.dispose](); + stderr[Symbol.dispose](); + }, + }; + } +} diff --git a/deno/deno.json b/deno/deno.json new file mode 100644 index 0000000..53cdf7a --- /dev/null +++ b/deno/deno.json @@ -0,0 +1,19 @@ +{ + "workspace": ["./base", "./mail-relay", "./tools"], + "tasks": { + "compile:mail-relay": "deno task --cwd=mail-relay compile" + }, + "imports": { + "@std/collections": "jsr:@std/collections@^1.1.1", + "@std/csv": "jsr:@std/csv@^1.0.6", + "@std/encoding": "jsr:@std/encoding@^1.0.10", + "@std/expect": "jsr:@std/expect@^1.0.16", + "@std/io": "jsr:@std/io@^0.225.2", + "@std/path": "jsr:@std/path@^1.1.0", + "@std/testing": "jsr:@std/testing@^1.0.13", + "@std/dotenv": "jsr:@std/dotenv@^0.225.5", + "@std/fs": "jsr:@std/fs@^1.0.18", + "yargs": "npm:yargs@^18.0.0", + "@types/yargs": "npm:@types/yargs@^17.0.33" + } +} diff --git a/deno/deno.lock b/deno/deno.lock new file mode 100644 index 0000000..871a9ae --- /dev/null +++ b/deno/deno.lock @@ -0,0 +1,1334 @@ +{ + "version": "5", + "specifiers": { + "jsr:@db/sqlite@0.12": "0.12.0", + "jsr:@denosaurs/plug@1": "1.1.0", + "jsr:@std/assert@0.217": "0.217.0", + "jsr:@std/assert@^1.0.13": "1.0.13", + "jsr:@std/async@^1.0.13": "1.0.13", + "jsr:@std/bytes@^1.0.5": "1.0.6", + "jsr:@std/collections@^1.1.1": "1.1.1", + "jsr:@std/csv@^1.0.6": "1.0.6", + "jsr:@std/data-structures@^1.0.8": "1.0.8", + "jsr:@std/dotenv@~0.225.5": "0.225.5", + "jsr:@std/encoding@1": "1.0.10", + "jsr:@std/encoding@^1.0.10": "1.0.10", + "jsr:@std/expect@^1.0.16": "1.0.16", + "jsr:@std/fmt@1": "1.0.8", + "jsr:@std/fs@1": "1.0.17", + "jsr:@std/fs@^1.0.17": "1.0.17", + "jsr:@std/fs@^1.0.18": "1.0.18", + "jsr:@std/internal@^1.0.6": "1.0.7", + "jsr:@std/internal@^1.0.7": "1.0.7", + "jsr:@std/internal@^1.0.8": "1.0.8", + "jsr:@std/io@~0.225.2": "0.225.2", + "jsr:@std/path@0.217": "0.217.0", + "jsr:@std/path@1": "1.1.0", + "jsr:@std/path@^1.0.9": "1.1.0", + "jsr:@std/path@^1.1.0": "1.1.0", + "jsr:@std/streams@^1.0.9": "1.0.9", + "jsr:@std/testing@^1.0.13": "1.0.13", + "npm:@aws-sdk/client-s3@^3.821.0": "3.824.0", + "npm:@aws-sdk/client-sesv2@^3.821.0": "3.824.0", + "npm:@hono/zod-validator@0.7": "0.7.0_hono@4.7.11_zod@3.25.51", + "npm:@smithy/fetch-http-handler@^5.0.4": "5.0.4", + "npm:@types/lodash@*": "4.17.17", + "npm:@types/mustache@*": "4.2.6", + "npm:@types/node@*": "22.15.15", + "npm:@types/yargs@*": "17.0.33", + "npm:@types/yargs@^17.0.33": "17.0.33", + "npm:email-addresses@5": "5.0.0", + "npm:hono@^4.7.11": "4.7.11", + "npm:kysely@~0.28.2": "0.28.2", + "npm:mustache@^4.2.0": "4.2.0", + "npm:yargs@18": "18.0.0", + "npm:zod@^3.25.48": "3.25.51" + }, + "jsr": { + "@db/sqlite@0.12.0": { + "integrity": "dd1ef7f621ad50fc1e073a1c3609c4470bd51edc0994139c5bf9851de7a6d85f", + "dependencies": [ + "jsr:@denosaurs/plug", + "jsr:@std/path@0.217" + ] + }, + "@denosaurs/plug@1.1.0": { + "integrity": "eb2f0b7546c7bca2000d8b0282c54d50d91cf6d75cb26a80df25a6de8c4bc044", + "dependencies": [ + "jsr:@std/encoding@1", + "jsr:@std/fmt", + "jsr:@std/fs@1", + "jsr:@std/path@1" + ] + }, + "@std/assert@0.217.0": { + "integrity": "c98e279362ca6982d5285c3b89517b757c1e3477ee9f14eb2fdf80a45aaa9642" + }, + "@std/assert@1.0.13": { + "integrity": "ae0d31e41919b12c656c742b22522c32fb26ed0cba32975cb0de2a273cb68b29", + "dependencies": [ + "jsr:@std/internal@^1.0.6" + ] + }, + "@std/async@1.0.13": { + "integrity": "1d76ca5d324aef249908f7f7fe0d39aaf53198e5420604a59ab5c035adc97c96" + }, + "@std/bytes@1.0.6": { + "integrity": "f6ac6adbd8ccd99314045f5703e23af0a68d7f7e58364b47d2c7f408aeb5820a" + }, + "@std/collections@1.1.1": { + "integrity": "eff6443fbd9d5a6697018fb39c5d13d5f662f0045f21392d640693d0008ab2af" + }, + "@std/csv@1.0.6": { + "integrity": "52ef0e62799a0028d278fa04762f17f9bd263fad9a8e7f98c14fbd371d62d9fd", + "dependencies": [ + "jsr:@std/streams" + ] + }, + "@std/data-structures@1.0.8": { + "integrity": "2fb7219247e044c8fcd51341788547575653c82ae2c759ff209e0263ba7d9b66" + }, + "@std/dotenv@0.225.5": { + "integrity": "9ce6f9d0ec3311f74a32535aa1b8c62ed88b1ab91b7f0815797d77a6f60c922f" + }, + "@std/encoding@1.0.10": { + "integrity": "8783c6384a2d13abd5e9e87a7ae0520a30e9f56aeeaa3bdf910a3eaaf5c811a1" + }, + "@std/expect@1.0.16": { + "integrity": "ceeef6dda21f256a5f0f083fcc0eaca175428b523359a9b1d9b3a1df11cc7391", + "dependencies": [ + "jsr:@std/assert@^1.0.13", + "jsr:@std/internal@^1.0.7" + ] + }, + "@std/fmt@1.0.8": { + "integrity": "71e1fc498787e4434d213647a6e43e794af4fd393ef8f52062246e06f7e372b7" + }, + "@std/fs@1.0.17": { + "integrity": "1c00c632677c1158988ef7a004cb16137f870aafdb8163b9dce86ec652f3952b", + "dependencies": [ + "jsr:@std/path@^1.0.9" + ] + }, + "@std/fs@1.0.18": { + "integrity": "24bcad99eab1af4fde75e05da6e9ed0e0dce5edb71b7e34baacf86ffe3969f3a", + "dependencies": [ + "jsr:@std/path@^1.1.0" + ] + }, + "@std/internal@1.0.7": { + "integrity": "39eeb5265190a7bc5d5591c9ff019490bd1f2c3907c044a11b0d545796158a0f" + }, + "@std/internal@1.0.8": { + "integrity": "fc66e846d8d38a47cffd274d80d2ca3f0de71040f855783724bb6b87f60891f5" + }, + "@std/io@0.225.2": { + "integrity": "3c740cd4ee4c082e6cfc86458f47e2ab7cb353dc6234d5e9b1f91a2de5f4d6c7", + "dependencies": [ + "jsr:@std/bytes" + ] + }, + "@std/path@0.217.0": { + "integrity": "1217cc25534bca9a2f672d7fe7c6f356e4027df400c0e85c0ef3e4343bc67d11", + "dependencies": [ + "jsr:@std/assert@0.217" + ] + }, + "@std/path@1.1.0": { + "integrity": "ddc94f8e3c275627281cbc23341df6b8bcc874d70374f75fec2533521e3d6886" + }, + "@std/streams@1.0.9": { + "integrity": "a9d26b1988cdd7aa7b1f4b51e1c36c1557f3f252880fa6cc5b9f37078b1a5035", + "dependencies": [ + "jsr:@std/bytes" + ] + }, + "@std/testing@1.0.13": { + "integrity": "74418be16f627dfe996937ab0ffbdbda9c1f35534b78724658d981492f121e71", + "dependencies": [ + "jsr:@std/assert@^1.0.13", + "jsr:@std/async", + "jsr:@std/data-structures", + "jsr:@std/fs@^1.0.17", + "jsr:@std/internal@^1.0.8", + "jsr:@std/path@^1.1.0" + ] + } + }, + "npm": { + "@aws-crypto/crc32@5.2.0": { + "integrity": "sha512-nLbCWqQNgUiwwtFsen1AdzAtvuLRsQS8rYgMuxCrdKf9kOssamGLuPwyTY9wyYblNr9+1XM8v6zoDTPPSIeANg==", + "dependencies": [ + "@aws-crypto/util", + "@aws-sdk/types", + "tslib" + ] + }, + "@aws-crypto/crc32c@5.2.0": { + "integrity": "sha512-+iWb8qaHLYKrNvGRbiYRHSdKRWhto5XlZUEBwDjYNf+ly5SVYG6zEoYIdxvf5R3zyeP16w4PLBn3rH1xc74Rag==", + "dependencies": [ + "@aws-crypto/util", + "@aws-sdk/types", + "tslib" + ] + }, + "@aws-crypto/sha1-browser@5.2.0": { + "integrity": "sha512-OH6lveCFfcDjX4dbAvCFSYUjJZjDr/3XJ3xHtjn3Oj5b9RjojQo8npoLeA/bNwkOkrSQ0wgrHzXk4tDRxGKJeg==", + "dependencies": [ + "@aws-crypto/supports-web-crypto", + "@aws-crypto/util", + "@aws-sdk/types", + "@aws-sdk/util-locate-window", + "@smithy/util-utf8@2.3.0", + "tslib" + ] + }, + "@aws-crypto/sha256-browser@5.2.0": { + "integrity": "sha512-AXfN/lGotSQwu6HNcEsIASo7kWXZ5HYWvfOmSNKDsEqC4OashTp8alTmaz+F7TC2L083SFv5RdB+qU3Vs1kZqw==", + "dependencies": [ + "@aws-crypto/sha256-js", + "@aws-crypto/supports-web-crypto", + "@aws-crypto/util", + "@aws-sdk/types", + "@aws-sdk/util-locate-window", + "@smithy/util-utf8@2.3.0", + "tslib" + ] + }, + "@aws-crypto/sha256-js@5.2.0": { + "integrity": "sha512-FFQQyu7edu4ufvIZ+OadFpHHOt+eSTBaYaki44c+akjg7qZg9oOQeLlk77F6tSYqjDAFClrHJk9tMf0HdVyOvA==", + "dependencies": [ + "@aws-crypto/util", + "@aws-sdk/types", + "tslib" + ] + }, + "@aws-crypto/supports-web-crypto@5.2.0": { + "integrity": "sha512-iAvUotm021kM33eCdNfwIN//F77/IADDSs58i+MDaOqFrVjZo9bAal0NK7HurRuWLLpF1iLX7gbWrjHjeo+YFg==", + "dependencies": [ + "tslib" + ] + }, + "@aws-crypto/util@5.2.0": { + "integrity": "sha512-4RkU9EsI6ZpBve5fseQlGNUWKMa1RLPQ1dnjnQoe07ldfIzcsGb5hC5W0Dm7u423KWzawlrpbjXBrXCEv9zazQ==", + "dependencies": [ + "@aws-sdk/types", + "@smithy/util-utf8@2.3.0", + "tslib" + ] + }, + "@aws-sdk/client-s3@3.824.0": { + "integrity": "sha512-7neTQIdSVP/F4RTWG5T87LDpB955iQD6lxg9nJ00fdkIPczDcRtAEXow44NjF4fEdpQ1A9jokUtBSVE+GMXZ/A==", + "dependencies": [ + "@aws-crypto/sha1-browser", + "@aws-crypto/sha256-browser", + "@aws-crypto/sha256-js", + "@aws-sdk/core", + "@aws-sdk/credential-provider-node", + "@aws-sdk/middleware-bucket-endpoint", + "@aws-sdk/middleware-expect-continue", + "@aws-sdk/middleware-flexible-checksums", + "@aws-sdk/middleware-host-header", + "@aws-sdk/middleware-location-constraint", + "@aws-sdk/middleware-logger", + "@aws-sdk/middleware-recursion-detection", + "@aws-sdk/middleware-sdk-s3", + "@aws-sdk/middleware-ssec", + "@aws-sdk/middleware-user-agent", + "@aws-sdk/region-config-resolver", + "@aws-sdk/signature-v4-multi-region", + "@aws-sdk/types", + "@aws-sdk/util-endpoints", + "@aws-sdk/util-user-agent-browser", + "@aws-sdk/util-user-agent-node", + "@aws-sdk/xml-builder", + "@smithy/config-resolver", + "@smithy/core", + "@smithy/eventstream-serde-browser", + "@smithy/eventstream-serde-config-resolver", + "@smithy/eventstream-serde-node", + "@smithy/fetch-http-handler", + "@smithy/hash-blob-browser", + "@smithy/hash-node", + "@smithy/hash-stream-node", + "@smithy/invalid-dependency", + "@smithy/md5-js", + "@smithy/middleware-content-length", + "@smithy/middleware-endpoint", + "@smithy/middleware-retry", + "@smithy/middleware-serde", + "@smithy/middleware-stack", + "@smithy/node-config-provider", + "@smithy/node-http-handler", + "@smithy/protocol-http", + "@smithy/smithy-client", + "@smithy/types", + "@smithy/url-parser", + "@smithy/util-base64", + "@smithy/util-body-length-browser", + "@smithy/util-body-length-node", + "@smithy/util-defaults-mode-browser", + "@smithy/util-defaults-mode-node", + "@smithy/util-endpoints", + "@smithy/util-middleware", + "@smithy/util-retry", + "@smithy/util-stream", + "@smithy/util-utf8@4.0.0", + "@smithy/util-waiter", + "tslib" + ] + }, + "@aws-sdk/client-sesv2@3.824.0": { + "integrity": "sha512-WRssgE34ZTO0It5knqDsjp42CwyAC0RnPLHI1f8lOZIpAZjtTUFWuptKriDoycDXaGPkRcu1dcvrYqUskBXn1A==", + "dependencies": [ + "@aws-crypto/sha256-browser", + "@aws-crypto/sha256-js", + "@aws-sdk/core", + "@aws-sdk/credential-provider-node", + "@aws-sdk/middleware-host-header", + "@aws-sdk/middleware-logger", + "@aws-sdk/middleware-recursion-detection", + "@aws-sdk/middleware-user-agent", + "@aws-sdk/region-config-resolver", + "@aws-sdk/signature-v4-multi-region", + "@aws-sdk/types", + "@aws-sdk/util-endpoints", + "@aws-sdk/util-user-agent-browser", + "@aws-sdk/util-user-agent-node", + "@smithy/config-resolver", + "@smithy/core", + "@smithy/fetch-http-handler", + "@smithy/hash-node", + "@smithy/invalid-dependency", + "@smithy/middleware-content-length", + "@smithy/middleware-endpoint", + "@smithy/middleware-retry", + "@smithy/middleware-serde", + "@smithy/middleware-stack", + "@smithy/node-config-provider", + "@smithy/node-http-handler", + "@smithy/protocol-http", + "@smithy/smithy-client", + "@smithy/types", + "@smithy/url-parser", + "@smithy/util-base64", + "@smithy/util-body-length-browser", + "@smithy/util-body-length-node", + "@smithy/util-defaults-mode-browser", + "@smithy/util-defaults-mode-node", + "@smithy/util-endpoints", + "@smithy/util-middleware", + "@smithy/util-retry", + "@smithy/util-utf8@4.0.0", + "tslib" + ] + }, + "@aws-sdk/client-sso@3.823.0": { + "integrity": "sha512-dBWdsbyGw8rPfdCsZySNtTOGQK4EZ8lxB/CneSQWRBPHgQ+Ys88NXxImO8xfWO7Itt1eh8O7UDTZ9+smcvw2pw==", + "dependencies": [ + "@aws-crypto/sha256-browser", + "@aws-crypto/sha256-js", + "@aws-sdk/core", + "@aws-sdk/middleware-host-header", + "@aws-sdk/middleware-logger", + "@aws-sdk/middleware-recursion-detection", + "@aws-sdk/middleware-user-agent", + "@aws-sdk/region-config-resolver", + "@aws-sdk/types", + "@aws-sdk/util-endpoints", + "@aws-sdk/util-user-agent-browser", + "@aws-sdk/util-user-agent-node", + "@smithy/config-resolver", + "@smithy/core", + "@smithy/fetch-http-handler", + "@smithy/hash-node", + "@smithy/invalid-dependency", + "@smithy/middleware-content-length", + "@smithy/middleware-endpoint", + "@smithy/middleware-retry", + "@smithy/middleware-serde", + "@smithy/middleware-stack", + "@smithy/node-config-provider", + "@smithy/node-http-handler", + "@smithy/protocol-http", + "@smithy/smithy-client", + "@smithy/types", + "@smithy/url-parser", + "@smithy/util-base64", + "@smithy/util-body-length-browser", + "@smithy/util-body-length-node", + "@smithy/util-defaults-mode-browser", + "@smithy/util-defaults-mode-node", + "@smithy/util-endpoints", + "@smithy/util-middleware", + "@smithy/util-retry", + "@smithy/util-utf8@4.0.0", + "tslib" + ] + }, + "@aws-sdk/core@3.823.0": { + "integrity": "sha512-1Cf4w8J7wYexz0KU3zpaikHvldGXQEjFldHOhm0SBGRy7qfYNXecfJAamccF7RdgLxKGgkv5Pl9zX/Z/DcW9zg==", + "dependencies": [ + "@aws-sdk/types", + "@aws-sdk/xml-builder", + "@smithy/core", + "@smithy/node-config-provider", + "@smithy/property-provider", + "@smithy/protocol-http", + "@smithy/signature-v4", + "@smithy/smithy-client", + "@smithy/types", + "@smithy/util-base64", + "@smithy/util-body-length-browser", + "@smithy/util-middleware", + "@smithy/util-utf8@4.0.0", + "fast-xml-parser", + "tslib" + ] + }, + "@aws-sdk/credential-provider-env@3.823.0": { + "integrity": "sha512-AIrLLwumObge+U1klN4j5ToIozI+gE9NosENRyHe0GIIZgTLOG/8jxrMFVYFeNHs7RUtjDTxxewislhFyGxJ/w==", + "dependencies": [ + "@aws-sdk/core", + "@aws-sdk/types", + "@smithy/property-provider", + "@smithy/types", + "tslib" + ] + }, + "@aws-sdk/credential-provider-http@3.823.0": { + "integrity": "sha512-u4DXvB/J/o2bcvP1JP6n3ch7V3/NngmiJFPsM0hKUyRlLuWM37HEDEdjPRs3/uL/soTxrEhWKTA9//YVkvzI0w==", + "dependencies": [ + "@aws-sdk/core", + "@aws-sdk/types", + "@smithy/fetch-http-handler", + "@smithy/node-http-handler", + "@smithy/property-provider", + "@smithy/protocol-http", + "@smithy/smithy-client", + "@smithy/types", + "@smithy/util-stream", + "tslib" + ] + }, + "@aws-sdk/credential-provider-ini@3.823.0": { + "integrity": "sha512-C0o63qviK5yFvjH9zKWAnCUBkssJoQ1A1XAHe0IAQkurzoNBSmu9oVemqwnKKHA4H6QrmusaEERfL00yohIkJA==", + "dependencies": [ + "@aws-sdk/core", + "@aws-sdk/credential-provider-env", + "@aws-sdk/credential-provider-http", + "@aws-sdk/credential-provider-process", + "@aws-sdk/credential-provider-sso", + "@aws-sdk/credential-provider-web-identity", + "@aws-sdk/nested-clients", + "@aws-sdk/types", + "@smithy/credential-provider-imds", + "@smithy/property-provider", + "@smithy/shared-ini-file-loader", + "@smithy/types", + "tslib" + ] + }, + "@aws-sdk/credential-provider-node@3.823.0": { + "integrity": "sha512-nfSxXVuZ+2GJDpVFlflNfh55Yb4BtDsXLGNssXF5YU6UgSPsi8j2YkaE92Jv2s7dlUK07l0vRpLyPuXMaGeiRQ==", + "dependencies": [ + "@aws-sdk/credential-provider-env", + "@aws-sdk/credential-provider-http", + "@aws-sdk/credential-provider-ini", + "@aws-sdk/credential-provider-process", + "@aws-sdk/credential-provider-sso", + "@aws-sdk/credential-provider-web-identity", + "@aws-sdk/types", + "@smithy/credential-provider-imds", + "@smithy/property-provider", + "@smithy/shared-ini-file-loader", + "@smithy/types", + "tslib" + ] + }, + "@aws-sdk/credential-provider-process@3.823.0": { + "integrity": "sha512-U/A10/7zu2FbMFFVpIw95y0TZf+oYyrhZTBn9eL8zgWcrYRqxrxdqtPj/zMrfIfyIvQUhuJSENN4dx4tfpCMWQ==", + "dependencies": [ + "@aws-sdk/core", + "@aws-sdk/types", + "@smithy/property-provider", + "@smithy/shared-ini-file-loader", + "@smithy/types", + "tslib" + ] + }, + "@aws-sdk/credential-provider-sso@3.823.0": { + "integrity": "sha512-ff8IM80Wqz1V7VVMaMUqO2iR417jggfGWLPl8j2l7uCgwpEyop1ZZl5CFVYEwSupRBtwp+VlW1gTCk7ke56MUw==", + "dependencies": [ + "@aws-sdk/client-sso", + "@aws-sdk/core", + "@aws-sdk/token-providers", + "@aws-sdk/types", + "@smithy/property-provider", + "@smithy/shared-ini-file-loader", + "@smithy/types", + "tslib" + ] + }, + "@aws-sdk/credential-provider-web-identity@3.823.0": { + "integrity": "sha512-lzoZdJMQq9w7i4lXVka30cVBe/dZoUDZST8Xz/soEd73gg7RTKgG+0szL4xFWgdBDgcJDWLfZfJzlbyIVyAyOA==", + "dependencies": [ + "@aws-sdk/core", + "@aws-sdk/nested-clients", + "@aws-sdk/types", + "@smithy/property-provider", + "@smithy/types", + "tslib" + ] + }, + "@aws-sdk/middleware-bucket-endpoint@3.821.0": { + "integrity": "sha512-cebgeytKlWOgGczLo3BPvNY9XlzAzGZQANSysgJ2/8PSldmUpXRIF+GKPXDVhXeInWYHIfB8zZi3RqrPoXcNYQ==", + "dependencies": [ + "@aws-sdk/types", + "@aws-sdk/util-arn-parser", + "@smithy/node-config-provider", + "@smithy/protocol-http", + "@smithy/types", + "@smithy/util-config-provider", + "tslib" + ] + }, + "@aws-sdk/middleware-expect-continue@3.821.0": { + "integrity": "sha512-zAOoSZKe1njOrtynvK6ZORU57YGv5I7KP4+rwOvUN3ZhJbQ7QPf8gKtFUCYAPRMegaXCKF/ADPtDZBAmM+zZ9g==", + "dependencies": [ + "@aws-sdk/types", + "@smithy/protocol-http", + "@smithy/types", + "tslib" + ] + }, + "@aws-sdk/middleware-flexible-checksums@3.823.0": { + "integrity": "sha512-Elt6G1ryEEdkrppqbyJON0o2x4x9xKknimJtMLdfG1b4YfO9X+UB31pk4R2SHvMYfrJ+p8DE2jRAhvV4g/dwIQ==", + "dependencies": [ + "@aws-crypto/crc32", + "@aws-crypto/crc32c", + "@aws-crypto/util", + "@aws-sdk/core", + "@aws-sdk/types", + "@smithy/is-array-buffer@4.0.0", + "@smithy/node-config-provider", + "@smithy/protocol-http", + "@smithy/types", + "@smithy/util-middleware", + "@smithy/util-stream", + "@smithy/util-utf8@4.0.0", + "tslib" + ] + }, + "@aws-sdk/middleware-host-header@3.821.0": { + "integrity": "sha512-xSMR+sopSeWGx5/4pAGhhfMvGBHioVBbqGvDs6pG64xfNwM5vq5s5v6D04e2i+uSTj4qGa71dLUs5I0UzAK3sw==", + "dependencies": [ + "@aws-sdk/types", + "@smithy/protocol-http", + "@smithy/types", + "tslib" + ] + }, + "@aws-sdk/middleware-location-constraint@3.821.0": { + "integrity": "sha512-sKrm80k0t3R0on8aA/WhWFoMaAl4yvdk+riotmMElLUpcMcRXAd1+600uFVrxJqZdbrKQ0mjX0PjT68DlkYXLg==", + "dependencies": [ + "@aws-sdk/types", + "@smithy/types", + "tslib" + ] + }, + "@aws-sdk/middleware-logger@3.821.0": { + "integrity": "sha512-0cvI0ipf2tGx7fXYEEN5fBeZDz2RnHyb9xftSgUsEq7NBxjV0yTZfLJw6Za5rjE6snC80dRN8+bTNR1tuG89zA==", + "dependencies": [ + "@aws-sdk/types", + "@smithy/types", + "tslib" + ] + }, + "@aws-sdk/middleware-recursion-detection@3.821.0": { + "integrity": "sha512-efmaifbhBoqKG3bAoEfDdcM8hn1psF+4qa7ykWuYmfmah59JBeqHLfz5W9m9JoTwoKPkFcVLWZxnyZzAnVBOIg==", + "dependencies": [ + "@aws-sdk/types", + "@smithy/protocol-http", + "@smithy/types", + "tslib" + ] + }, + "@aws-sdk/middleware-sdk-s3@3.823.0": { + "integrity": "sha512-UV755wt2HDru8PbxLn2S0Fvwgdn9mYamexn31Q6wyUGQ6rkpjKNEzL+oNDGQQmDQAOcQO+nLubKFsCwtBM02fQ==", + "dependencies": [ + "@aws-sdk/core", + "@aws-sdk/types", + "@aws-sdk/util-arn-parser", + "@smithy/core", + "@smithy/node-config-provider", + "@smithy/protocol-http", + "@smithy/signature-v4", + "@smithy/smithy-client", + "@smithy/types", + "@smithy/util-config-provider", + "@smithy/util-middleware", + "@smithy/util-stream", + "@smithy/util-utf8@4.0.0", + "tslib" + ] + }, + "@aws-sdk/middleware-ssec@3.821.0": { + "integrity": "sha512-YYi1Hhr2AYiU/24cQc8HIB+SWbQo6FBkMYojVuz/zgrtkFmALxENGF/21OPg7f/QWd+eadZJRxCjmRwh5F2Cxg==", + "dependencies": [ + "@aws-sdk/types", + "@smithy/types", + "tslib" + ] + }, + "@aws-sdk/middleware-user-agent@3.823.0": { + "integrity": "sha512-TKRQK09ld1LrIPExC9rIDpqnMsWcv+eq8ABKFHVo8mDLTSuWx/IiQ4eCh9T5zDuEZcLY4nNYCSzXKqw6XKcMCA==", + "dependencies": [ + "@aws-sdk/core", + "@aws-sdk/types", + "@aws-sdk/util-endpoints", + "@smithy/core", + "@smithy/protocol-http", + "@smithy/types", + "tslib" + ] + }, + "@aws-sdk/nested-clients@3.823.0": { + "integrity": "sha512-/BcyOBubrJnd2gxlbbmNJR1w0Z3OVN/UE8Yz20e+ou+Mijjv7EbtVwmWvio1e3ZjphwdA8tVfPYZKwXmrvHKmQ==", + "dependencies": [ + "@aws-crypto/sha256-browser", + "@aws-crypto/sha256-js", + "@aws-sdk/core", + "@aws-sdk/middleware-host-header", + "@aws-sdk/middleware-logger", + "@aws-sdk/middleware-recursion-detection", + "@aws-sdk/middleware-user-agent", + "@aws-sdk/region-config-resolver", + "@aws-sdk/types", + "@aws-sdk/util-endpoints", + "@aws-sdk/util-user-agent-browser", + "@aws-sdk/util-user-agent-node", + "@smithy/config-resolver", + "@smithy/core", + "@smithy/fetch-http-handler", + "@smithy/hash-node", + "@smithy/invalid-dependency", + "@smithy/middleware-content-length", + "@smithy/middleware-endpoint", + "@smithy/middleware-retry", + "@smithy/middleware-serde", + "@smithy/middleware-stack", + "@smithy/node-config-provider", + "@smithy/node-http-handler", + "@smithy/protocol-http", + "@smithy/smithy-client", + "@smithy/types", + "@smithy/url-parser", + "@smithy/util-base64", + "@smithy/util-body-length-browser", + "@smithy/util-body-length-node", + "@smithy/util-defaults-mode-browser", + "@smithy/util-defaults-mode-node", + "@smithy/util-endpoints", + "@smithy/util-middleware", + "@smithy/util-retry", + "@smithy/util-utf8@4.0.0", + "tslib" + ] + }, + "@aws-sdk/region-config-resolver@3.821.0": { + "integrity": "sha512-t8og+lRCIIy5nlId0bScNpCkif8sc0LhmtaKsbm0ZPm3sCa/WhCbSZibjbZ28FNjVCV+p0D9RYZx0VDDbtWyjw==", + "dependencies": [ + "@aws-sdk/types", + "@smithy/node-config-provider", + "@smithy/types", + "@smithy/util-config-provider", + "@smithy/util-middleware", + "tslib" + ] + }, + "@aws-sdk/signature-v4-multi-region@3.824.0": { + "integrity": "sha512-HBjuWeN6Z1pvJjUvGXdMNLwEypKKB4km6zXj9jsbOOwP8NTL6J5rY+JmlX/mfBTmvzmI0kMu2bxlQ4ME2CIRbA==", + "dependencies": [ + "@aws-sdk/middleware-sdk-s3", + "@aws-sdk/types", + "@smithy/protocol-http", + "@smithy/signature-v4", + "@smithy/types", + "tslib" + ] + }, + "@aws-sdk/token-providers@3.823.0": { + "integrity": "sha512-vz6onCb/+g4y+owxGGPMEMdN789dTfBOgz/c9pFv0f01840w9Rrt46l+gjQlnXnx+0KG6wNeBIVhFdbCfV3HyQ==", + "dependencies": [ + "@aws-sdk/core", + "@aws-sdk/nested-clients", + "@aws-sdk/types", + "@smithy/property-provider", + "@smithy/shared-ini-file-loader", + "@smithy/types", + "tslib" + ] + }, + "@aws-sdk/types@3.821.0": { + "integrity": "sha512-Znroqdai1a90TlxGaJ+FK1lwC0fHpo97Xjsp5UKGR5JODYm7f9+/fF17ebO1KdoBr/Rm0UIFiF5VmI8ts9F1eA==", + "dependencies": [ + "@smithy/types", + "tslib" + ] + }, + "@aws-sdk/util-arn-parser@3.804.0": { + "integrity": "sha512-wmBJqn1DRXnZu3b4EkE6CWnoWMo1ZMvlfkqU5zPz67xx1GMaXlDCchFvKAXMjk4jn/L1O3tKnoFDNsoLV1kgNQ==", + "dependencies": [ + "tslib" + ] + }, + "@aws-sdk/util-endpoints@3.821.0": { + "integrity": "sha512-Uknt/zUZnLE76zaAAPEayOeF5/4IZ2puTFXvcSCWHsi9m3tqbb9UozlnlVqvCZLCRWfQryZQoG2W4XSS3qgk5A==", + "dependencies": [ + "@aws-sdk/types", + "@smithy/types", + "@smithy/util-endpoints", + "tslib" + ] + }, + "@aws-sdk/util-locate-window@3.804.0": { + "integrity": "sha512-zVoRfpmBVPodYlnMjgVjfGoEZagyRF5IPn3Uo6ZvOZp24chnW/FRstH7ESDHDDRga4z3V+ElUQHKpFDXWyBW5A==", + "dependencies": [ + "tslib" + ] + }, + "@aws-sdk/util-user-agent-browser@3.821.0": { + "integrity": "sha512-irWZHyM0Jr1xhC+38OuZ7JB6OXMLPZlj48thElpsO1ZSLRkLZx5+I7VV6k3sp2yZ7BYbKz/G2ojSv4wdm7XTLw==", + "dependencies": [ + "@aws-sdk/types", + "@smithy/types", + "bowser", + "tslib" + ] + }, + "@aws-sdk/util-user-agent-node@3.823.0": { + "integrity": "sha512-WvNeRz7HV3JLBVGTXW4Qr5QvvWY0vtggH5jW/NqHFH+ZEliVQaUIJ/HNLMpMoCSiu/DlpQAyAjRZXAptJ0oqbw==", + "dependencies": [ + "@aws-sdk/middleware-user-agent", + "@aws-sdk/types", + "@smithy/node-config-provider", + "@smithy/types", + "tslib" + ] + }, + "@aws-sdk/xml-builder@3.821.0": { + "integrity": "sha512-DIIotRnefVL6DiaHtO6/21DhJ4JZnnIwdNbpwiAhdt/AVbttcE4yw925gsjur0OGv5BTYXQXU3YnANBYnZjuQA==", + "dependencies": [ + "@smithy/types", + "tslib" + ] + }, + "@hono/zod-validator@0.7.0_hono@4.7.11_zod@3.25.51": { + "integrity": "sha512-qe2ZE6sHFE98dcUrbYMtS3bAV8hqcCOflykvZga2S7XhmNSZzT+dIz4OuMILsjLHkJw9JMn912/dB7dQOmuPvg==", + "dependencies": [ + "hono", + "zod" + ] + }, + "@smithy/abort-controller@4.0.4": { + "integrity": "sha512-gJnEjZMvigPDQWHrW3oPrFhQtkrgqBkyjj3pCIdF3A5M6vsZODG93KNlfJprv6bp4245bdT32fsHK4kkH3KYDA==", + "dependencies": [ + "@smithy/types", + "tslib" + ] + }, + "@smithy/chunked-blob-reader-native@4.0.0": { + "integrity": "sha512-R9wM2yPmfEMsUmlMlIgSzOyICs0x9uu7UTHoccMyt7BWw8shcGM8HqB355+BZCPBcySvbTYMs62EgEQkNxz2ig==", + "dependencies": [ + "@smithy/util-base64", + "tslib" + ] + }, + "@smithy/chunked-blob-reader@5.0.0": { + "integrity": "sha512-+sKqDBQqb036hh4NPaUiEkYFkTUGYzRsn3EuFhyfQfMy6oGHEUJDurLP9Ufb5dasr/XiAmPNMr6wa9afjQB+Gw==", + "dependencies": [ + "tslib" + ] + }, + "@smithy/config-resolver@4.1.4": { + "integrity": "sha512-prmU+rDddxHOH0oNcwemL+SwnzcG65sBF2yXRO7aeXIn/xTlq2pX7JLVbkBnVLowHLg4/OL4+jBmv9hVrVGS+w==", + "dependencies": [ + "@smithy/node-config-provider", + "@smithy/types", + "@smithy/util-config-provider", + "@smithy/util-middleware", + "tslib" + ] + }, + "@smithy/core@3.5.3": { + "integrity": "sha512-xa5byV9fEguZNofCclv6v9ra0FYh5FATQW/da7FQUVTic94DfrN/NvmKZjrMyzbpqfot9ZjBaO8U1UeTbmSLuA==", + "dependencies": [ + "@smithy/middleware-serde", + "@smithy/protocol-http", + "@smithy/types", + "@smithy/util-base64", + "@smithy/util-body-length-browser", + "@smithy/util-middleware", + "@smithy/util-stream", + "@smithy/util-utf8@4.0.0", + "tslib" + ] + }, + "@smithy/credential-provider-imds@4.0.6": { + "integrity": "sha512-hKMWcANhUiNbCJouYkZ9V3+/Qf9pteR1dnwgdyzR09R4ODEYx8BbUysHwRSyex4rZ9zapddZhLFTnT4ZijR4pw==", + "dependencies": [ + "@smithy/node-config-provider", + "@smithy/property-provider", + "@smithy/types", + "@smithy/url-parser", + "tslib" + ] + }, + "@smithy/eventstream-codec@4.0.4": { + "integrity": "sha512-7XoWfZqWb/QoR/rAU4VSi0mWnO2vu9/ltS6JZ5ZSZv0eovLVfDfu0/AX4ub33RsJTOth3TiFWSHS5YdztvFnig==", + "dependencies": [ + "@aws-crypto/crc32", + "@smithy/types", + "@smithy/util-hex-encoding", + "tslib" + ] + }, + "@smithy/eventstream-serde-browser@4.0.4": { + "integrity": "sha512-3fb/9SYaYqbpy/z/H3yIi0bYKyAa89y6xPmIqwr2vQiUT2St+avRt8UKwsWt9fEdEasc5d/V+QjrviRaX1JRFA==", + "dependencies": [ + "@smithy/eventstream-serde-universal", + "@smithy/types", + "tslib" + ] + }, + "@smithy/eventstream-serde-config-resolver@4.1.2": { + "integrity": "sha512-JGtambizrWP50xHgbzZI04IWU7LdI0nh/wGbqH3sJesYToMi2j/DcoElqyOcqEIG/D4tNyxgRuaqBXWE3zOFhQ==", + "dependencies": [ + "@smithy/types", + "tslib" + ] + }, + "@smithy/eventstream-serde-node@4.0.4": { + "integrity": "sha512-RD6UwNZ5zISpOWPuhVgRz60GkSIp0dy1fuZmj4RYmqLVRtejFqQ16WmfYDdoSoAjlp1LX+FnZo+/hkdmyyGZ1w==", + "dependencies": [ + "@smithy/eventstream-serde-universal", + "@smithy/types", + "tslib" + ] + }, + "@smithy/eventstream-serde-universal@4.0.4": { + "integrity": "sha512-UeJpOmLGhq1SLox79QWw/0n2PFX+oPRE1ZyRMxPIaFEfCqWaqpB7BU9C8kpPOGEhLF7AwEqfFbtwNxGy4ReENA==", + "dependencies": [ + "@smithy/eventstream-codec", + "@smithy/types", + "tslib" + ] + }, + "@smithy/fetch-http-handler@5.0.4": { + "integrity": "sha512-AMtBR5pHppYMVD7z7G+OlHHAcgAN7v0kVKEpHuTO4Gb199Gowh0taYi9oDStFeUhetkeP55JLSVlTW1n9rFtUw==", + "dependencies": [ + "@smithy/protocol-http", + "@smithy/querystring-builder", + "@smithy/types", + "@smithy/util-base64", + "tslib" + ] + }, + "@smithy/hash-blob-browser@4.0.4": { + "integrity": "sha512-WszRiACJiQV3QG6XMV44i5YWlkrlsM5Yxgz4jvsksuu7LDXA6wAtypfPajtNTadzpJy3KyJPoWehYpmZGKUFIQ==", + "dependencies": [ + "@smithy/chunked-blob-reader", + "@smithy/chunked-blob-reader-native", + "@smithy/types", + "tslib" + ] + }, + "@smithy/hash-node@4.0.4": { + "integrity": "sha512-qnbTPUhCVnCgBp4z4BUJUhOEkVwxiEi1cyFM+Zj6o+aY8OFGxUQleKWq8ltgp3dujuhXojIvJWdoqpm6dVO3lQ==", + "dependencies": [ + "@smithy/types", + "@smithy/util-buffer-from@4.0.0", + "@smithy/util-utf8@4.0.0", + "tslib" + ] + }, + "@smithy/hash-stream-node@4.0.4": { + "integrity": "sha512-wHo0d8GXyVmpmMh/qOR0R7Y46/G1y6OR8U+bSTB4ppEzRxd1xVAQ9xOE9hOc0bSjhz0ujCPAbfNLkLrpa6cevg==", + "dependencies": [ + "@smithy/types", + "@smithy/util-utf8@4.0.0", + "tslib" + ] + }, + "@smithy/invalid-dependency@4.0.4": { + "integrity": "sha512-bNYMi7WKTJHu0gn26wg8OscncTt1t2b8KcsZxvOv56XA6cyXtOAAAaNP7+m45xfppXfOatXF3Sb1MNsLUgVLTw==", + "dependencies": [ + "@smithy/types", + "tslib" + ] + }, + "@smithy/is-array-buffer@2.2.0": { + "integrity": "sha512-GGP3O9QFD24uGeAXYUjwSTXARoqpZykHadOmA8G5vfJPK0/DC67qa//0qvqrJzL1xc8WQWX7/yc7fwudjPHPhA==", + "dependencies": [ + "tslib" + ] + }, + "@smithy/is-array-buffer@4.0.0": { + "integrity": "sha512-saYhF8ZZNoJDTvJBEWgeBccCg+yvp1CX+ed12yORU3NilJScfc6gfch2oVb4QgxZrGUx3/ZJlb+c/dJbyupxlw==", + "dependencies": [ + "tslib" + ] + }, + "@smithy/md5-js@4.0.4": { + "integrity": "sha512-uGLBVqcOwrLvGh/v/jw423yWHq/ofUGK1W31M2TNspLQbUV1Va0F5kTxtirkoHawODAZcjXTSGi7JwbnPcDPJg==", + "dependencies": [ + "@smithy/types", + "@smithy/util-utf8@4.0.0", + "tslib" + ] + }, + "@smithy/middleware-content-length@4.0.4": { + "integrity": "sha512-F7gDyfI2BB1Kc+4M6rpuOLne5LOcEknH1n6UQB69qv+HucXBR1rkzXBnQTB2q46sFy1PM/zuSJOB532yc8bg3w==", + "dependencies": [ + "@smithy/protocol-http", + "@smithy/types", + "tslib" + ] + }, + "@smithy/middleware-endpoint@4.1.11": { + "integrity": "sha512-zDogwtRLzKl58lVS8wPcARevFZNBOOqnmzWWxVe9XiaXU2CADFjvJ9XfNibgkOWs08sxLuSr81NrpY4mgp9OwQ==", + "dependencies": [ + "@smithy/core", + "@smithy/middleware-serde", + "@smithy/node-config-provider", + "@smithy/shared-ini-file-loader", + "@smithy/types", + "@smithy/url-parser", + "@smithy/util-middleware", + "tslib" + ] + }, + "@smithy/middleware-retry@4.1.12": { + "integrity": "sha512-wvIH70c4e91NtRxdaLZF+mbLZ/HcC6yg7ySKUiufL6ESp6zJUSnJucZ309AvG9nqCFHSRB5I6T3Ez1Q9wCh0Ww==", + "dependencies": [ + "@smithy/node-config-provider", + "@smithy/protocol-http", + "@smithy/service-error-classification", + "@smithy/smithy-client", + "@smithy/types", + "@smithy/util-middleware", + "@smithy/util-retry", + "tslib", + "uuid" + ] + }, + "@smithy/middleware-serde@4.0.8": { + "integrity": "sha512-iSSl7HJoJaGyMIoNn2B7czghOVwJ9nD7TMvLhMWeSB5vt0TnEYyRRqPJu/TqW76WScaNvYYB8nRoiBHR9S1Ddw==", + "dependencies": [ + "@smithy/protocol-http", + "@smithy/types", + "tslib" + ] + }, + "@smithy/middleware-stack@4.0.4": { + "integrity": "sha512-kagK5ggDrBUCCzI93ft6DjteNSfY8Ulr83UtySog/h09lTIOAJ/xUSObutanlPT0nhoHAkpmW9V5K8oPyLh+QA==", + "dependencies": [ + "@smithy/types", + "tslib" + ] + }, + "@smithy/node-config-provider@4.1.3": { + "integrity": "sha512-HGHQr2s59qaU1lrVH6MbLlmOBxadtzTsoO4c+bF5asdgVik3I8o7JIOzoeqWc5MjVa+vD36/LWE0iXKpNqooRw==", + "dependencies": [ + "@smithy/property-provider", + "@smithy/shared-ini-file-loader", + "@smithy/types", + "tslib" + ] + }, + "@smithy/node-http-handler@4.0.6": { + "integrity": "sha512-NqbmSz7AW2rvw4kXhKGrYTiJVDHnMsFnX4i+/FzcZAfbOBauPYs2ekuECkSbtqaxETLLTu9Rl/ex6+I2BKErPA==", + "dependencies": [ + "@smithy/abort-controller", + "@smithy/protocol-http", + "@smithy/querystring-builder", + "@smithy/types", + "tslib" + ] + }, + "@smithy/property-provider@4.0.4": { + "integrity": "sha512-qHJ2sSgu4FqF4U/5UUp4DhXNmdTrgmoAai6oQiM+c5RZ/sbDwJ12qxB1M6FnP+Tn/ggkPZf9ccn4jqKSINaquw==", + "dependencies": [ + "@smithy/types", + "tslib" + ] + }, + "@smithy/protocol-http@5.1.2": { + "integrity": "sha512-rOG5cNLBXovxIrICSBm95dLqzfvxjEmuZx4KK3hWwPFHGdW3lxY0fZNXfv2zebfRO7sJZ5pKJYHScsqopeIWtQ==", + "dependencies": [ + "@smithy/types", + "tslib" + ] + }, + "@smithy/querystring-builder@4.0.4": { + "integrity": "sha512-SwREZcDnEYoh9tLNgMbpop+UTGq44Hl9tdj3rf+yeLcfH7+J8OXEBaMc2kDxtyRHu8BhSg9ADEx0gFHvpJgU8w==", + "dependencies": [ + "@smithy/types", + "@smithy/util-uri-escape", + "tslib" + ] + }, + "@smithy/querystring-parser@4.0.4": { + "integrity": "sha512-6yZf53i/qB8gRHH/l2ZwUG5xgkPgQF15/KxH0DdXMDHjesA9MeZje/853ifkSY0x4m5S+dfDZ+c4x439PF0M2w==", + "dependencies": [ + "@smithy/types", + "tslib" + ] + }, + "@smithy/service-error-classification@4.0.5": { + "integrity": "sha512-LvcfhrnCBvCmTee81pRlh1F39yTS/+kYleVeLCwNtkY8wtGg8V/ca9rbZZvYIl8OjlMtL6KIjaiL/lgVqHD2nA==", + "dependencies": [ + "@smithy/types" + ] + }, + "@smithy/shared-ini-file-loader@4.0.4": { + "integrity": "sha512-63X0260LoFBjrHifPDs+nM9tV0VMkOTl4JRMYNuKh/f5PauSjowTfvF3LogfkWdcPoxsA9UjqEOgjeYIbhb7Nw==", + "dependencies": [ + "@smithy/types", + "tslib" + ] + }, + "@smithy/signature-v4@5.1.2": { + "integrity": "sha512-d3+U/VpX7a60seHziWnVZOHuEgJlclufjkS6zhXvxcJgkJq4UWdH5eOBLzHRMx6gXjsdT9h6lfpmLzbrdupHgQ==", + "dependencies": [ + "@smithy/is-array-buffer@4.0.0", + "@smithy/protocol-http", + "@smithy/types", + "@smithy/util-hex-encoding", + "@smithy/util-middleware", + "@smithy/util-uri-escape", + "@smithy/util-utf8@4.0.0", + "tslib" + ] + }, + "@smithy/smithy-client@4.4.3": { + "integrity": "sha512-xxzNYgA0HD6ETCe5QJubsxP0hQH3QK3kbpJz3QrosBCuIWyEXLR/CO5hFb2OeawEKUxMNhz3a1nuJNN2np2RMA==", + "dependencies": [ + "@smithy/core", + "@smithy/middleware-endpoint", + "@smithy/middleware-stack", + "@smithy/protocol-http", + "@smithy/types", + "@smithy/util-stream", + "tslib" + ] + }, + "@smithy/types@4.3.1": { + "integrity": "sha512-UqKOQBL2x6+HWl3P+3QqFD4ncKq0I8Nuz9QItGv5WuKuMHuuwlhvqcZCoXGfc+P1QmfJE7VieykoYYmrOoFJxA==", + "dependencies": [ + "tslib" + ] + }, + "@smithy/url-parser@4.0.4": { + "integrity": "sha512-eMkc144MuN7B0TDA4U2fKs+BqczVbk3W+qIvcoCY6D1JY3hnAdCuhCZODC+GAeaxj0p6Jroz4+XMUn3PCxQQeQ==", + "dependencies": [ + "@smithy/querystring-parser", + "@smithy/types", + "tslib" + ] + }, + "@smithy/util-base64@4.0.0": { + "integrity": "sha512-CvHfCmO2mchox9kjrtzoHkWHxjHZzaFojLc8quxXY7WAAMAg43nuxwv95tATVgQFNDwd4M9S1qFzj40Ul41Kmg==", + "dependencies": [ + "@smithy/util-buffer-from@4.0.0", + "@smithy/util-utf8@4.0.0", + "tslib" + ] + }, + "@smithy/util-body-length-browser@4.0.0": { + "integrity": "sha512-sNi3DL0/k64/LO3A256M+m3CDdG6V7WKWHdAiBBMUN8S3hK3aMPhwnPik2A/a2ONN+9doY9UxaLfgqsIRg69QA==", + "dependencies": [ + "tslib" + ] + }, + "@smithy/util-body-length-node@4.0.0": { + "integrity": "sha512-q0iDP3VsZzqJyje8xJWEJCNIu3lktUGVoSy1KB0UWym2CL1siV3artm+u1DFYTLejpsrdGyCSWBdGNjJzfDPjg==", + "dependencies": [ + "tslib" + ] + }, + "@smithy/util-buffer-from@2.2.0": { + "integrity": "sha512-IJdWBbTcMQ6DA0gdNhh/BwrLkDR+ADW5Kr1aZmd4k3DIF6ezMV4R2NIAmT08wQJ3yUK82thHWmC/TnK/wpMMIA==", + "dependencies": [ + "@smithy/is-array-buffer@2.2.0", + "tslib" + ] + }, + "@smithy/util-buffer-from@4.0.0": { + "integrity": "sha512-9TOQ7781sZvddgO8nxueKi3+yGvkY35kotA0Y6BWRajAv8jjmigQ1sBwz0UX47pQMYXJPahSKEKYFgt+rXdcug==", + "dependencies": [ + "@smithy/is-array-buffer@4.0.0", + "tslib" + ] + }, + "@smithy/util-config-provider@4.0.0": { + "integrity": "sha512-L1RBVzLyfE8OXH+1hsJ8p+acNUSirQnWQ6/EgpchV88G6zGBTDPdXiiExei6Z1wR2RxYvxY/XLw6AMNCCt8H3w==", + "dependencies": [ + "tslib" + ] + }, + "@smithy/util-defaults-mode-browser@4.0.19": { + "integrity": "sha512-mvLMh87xSmQrV5XqnUYEPoiFFeEGYeAKIDDKdhE2ahqitm8OHM3aSvhqL6rrK6wm1brIk90JhxDf5lf2hbrLbQ==", + "dependencies": [ + "@smithy/property-provider", + "@smithy/smithy-client", + "@smithy/types", + "bowser", + "tslib" + ] + }, + "@smithy/util-defaults-mode-node@4.0.19": { + "integrity": "sha512-8tYnx+LUfj6m+zkUUIrIQJxPM1xVxfRBvoGHua7R/i6qAxOMjqR6CpEpDwKoIs1o0+hOjGvkKE23CafKL0vJ9w==", + "dependencies": [ + "@smithy/config-resolver", + "@smithy/credential-provider-imds", + "@smithy/node-config-provider", + "@smithy/property-provider", + "@smithy/smithy-client", + "@smithy/types", + "tslib" + ] + }, + "@smithy/util-endpoints@3.0.6": { + "integrity": "sha512-YARl3tFL3WgPuLzljRUnrS2ngLiUtkwhQtj8PAL13XZSyUiNLQxwG3fBBq3QXFqGFUXepIN73pINp3y8c2nBmA==", + "dependencies": [ + "@smithy/node-config-provider", + "@smithy/types", + "tslib" + ] + }, + "@smithy/util-hex-encoding@4.0.0": { + "integrity": "sha512-Yk5mLhHtfIgW2W2WQZWSg5kuMZCVbvhFmC7rV4IO2QqnZdbEFPmQnCcGMAX2z/8Qj3B9hYYNjZOhWym+RwhePw==", + "dependencies": [ + "tslib" + ] + }, + "@smithy/util-middleware@4.0.4": { + "integrity": "sha512-9MLKmkBmf4PRb0ONJikCbCwORACcil6gUWojwARCClT7RmLzF04hUR4WdRprIXal7XVyrddadYNfp2eF3nrvtQ==", + "dependencies": [ + "@smithy/types", + "tslib" + ] + }, + "@smithy/util-retry@4.0.5": { + "integrity": "sha512-V7MSjVDTlEt/plmOFBn1762Dyu5uqMrV2Pl2X0dYk4XvWfdWJNe9Bs5Bzb56wkCuiWjSfClVMGcsuKrGj7S/yg==", + "dependencies": [ + "@smithy/service-error-classification", + "@smithy/types", + "tslib" + ] + }, + "@smithy/util-stream@4.2.2": { + "integrity": "sha512-aI+GLi7MJoVxg24/3J1ipwLoYzgkB4kUfogZfnslcYlynj3xsQ0e7vk4TnTro9hhsS5PvX1mwmkRqqHQjwcU7w==", + "dependencies": [ + "@smithy/fetch-http-handler", + "@smithy/node-http-handler", + "@smithy/types", + "@smithy/util-base64", + "@smithy/util-buffer-from@4.0.0", + "@smithy/util-hex-encoding", + "@smithy/util-utf8@4.0.0", + "tslib" + ] + }, + "@smithy/util-uri-escape@4.0.0": { + "integrity": "sha512-77yfbCbQMtgtTylO9itEAdpPXSog3ZxMe09AEhm0dU0NLTalV70ghDZFR+Nfi1C60jnJoh/Re4090/DuZh2Omg==", + "dependencies": [ + "tslib" + ] + }, + "@smithy/util-utf8@2.3.0": { + "integrity": "sha512-R8Rdn8Hy72KKcebgLiv8jQcQkXoLMOGGv5uI1/k0l+snqkOzQ1R0ChUBCxWMlBsFMekWjq0wRudIweFs7sKT5A==", + "dependencies": [ + "@smithy/util-buffer-from@2.2.0", + "tslib" + ] + }, + "@smithy/util-utf8@4.0.0": { + "integrity": "sha512-b+zebfKCfRdgNJDknHCob3O7FpeYQN6ZG6YLExMcasDHsCXlsXCEuiPZeLnJLpwa5dvPetGlnGCiMHuLwGvFow==", + "dependencies": [ + "@smithy/util-buffer-from@4.0.0", + "tslib" + ] + }, + "@smithy/util-waiter@4.0.5": { + "integrity": "sha512-4QvC49HTteI1gfemu0I1syWovJgPvGn7CVUoN9ZFkdvr/cCFkrEL7qNCdx/2eICqDWEGnnr68oMdSIPCLAriSQ==", + "dependencies": [ + "@smithy/abort-controller", + "@smithy/types", + "tslib" + ] + }, + "@types/lodash@4.17.17": { + "integrity": "sha512-RRVJ+J3J+WmyOTqnz3PiBLA501eKwXl2noseKOrNo/6+XEHjTAxO4xHvxQB6QuNm+s4WRbn6rSiap8+EA+ykFQ==" + }, + "@types/mustache@4.2.6": { + "integrity": "sha512-t+8/QWTAhOFlrF1IVZqKnMRJi84EgkIK5Kh0p2JV4OLywUvCwJPFxbJAl7XAow7DVIHsF+xW9f1MVzg0L6Szjw==" + }, + "@types/node@22.15.15": { + "integrity": "sha512-R5muMcZob3/Jjchn5LcO8jdKwSCbzqmPB6ruBxMcf9kbxtniZHP327s6C37iOfuw8mbKK3cAQa7sEl7afLrQ8A==", + "dependencies": [ + "undici-types" + ] + }, + "@types/yargs-parser@21.0.3": { + "integrity": "sha512-I4q9QU9MQv4oEOz4tAHJtNz1cwuLxn2F3xcc2iV5WdqLPpUnj30aUuxt1mAxYTG+oe8CZMV/+6rU4S4gRDzqtQ==" + }, + "@types/yargs@17.0.33": { + "integrity": "sha512-WpxBCKWPLr4xSsHgz511rFJAM+wS28w2zEO1QDNY5zM/S8ok70NNfztH0xwhqKyaK0OHCbN98LDAZuy1ctxDkA==", + "dependencies": [ + "@types/yargs-parser" + ] + }, + "ansi-regex@6.1.0": { + "integrity": "sha512-7HSX4QQb4CspciLpVFwyRe79O3xsIZDDLER21kERQ71oaPodF8jL725AgJMFAYbooIqolJoRLuM81SpeUkpkvA==" + }, + "ansi-styles@6.2.1": { + "integrity": "sha512-bN798gFfQX+viw3R7yrGWRqnrN2oRkEkUjjl4JNn4E8GxxbjtG3FbrEIIY3l8/hrwUwIeCZvi4QuOTP4MErVug==" + }, + "bowser@2.11.0": { + "integrity": "sha512-AlcaJBi/pqqJBIQ8U9Mcpc9i8Aqxn88Skv5d+xBX006BY5u8N3mGLHa5Lgppa7L/HfwgwLgZ6NYs+Ag6uUmJRA==" + }, + "cliui@9.0.1": { + "integrity": "sha512-k7ndgKhwoQveBL+/1tqGJYNz097I7WOvwbmmU2AR5+magtbjPWQTS1C5vzGkBC8Ym8UWRzfKUzUUqFLypY4Q+w==", + "dependencies": [ + "string-width", + "strip-ansi", + "wrap-ansi" + ] + }, + "email-addresses@5.0.0": { + "integrity": "sha512-4OIPYlA6JXqtVn8zpHpGiI7vE6EQOAg16aGnDMIAlZVinnoZ8208tW1hAbjWydgN/4PLTT9q+O1K6AH/vALJGw==" + }, + "emoji-regex@10.4.0": { + "integrity": "sha512-EC+0oUMY1Rqm4O6LLrgjtYDvcVYTy7chDnM4Q7030tP4Kwj3u/pR6gP9ygnp2CJMK5Gq+9Q2oqmrFJAz01DXjw==" + }, + "escalade@3.2.0": { + "integrity": "sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA==" + }, + "fast-xml-parser@4.4.1": { + "integrity": "sha512-xkjOecfnKGkSsOwtZ5Pz7Us/T6mrbPQrq0nh+aCO5V9nk5NLWmasAHumTKjiPJPWANe+kAZ84Jc8ooJkzZ88Sw==", + "dependencies": [ + "strnum" + ], + "bin": true + }, + "get-caller-file@2.0.5": { + "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==" + }, + "get-east-asian-width@1.3.0": { + "integrity": "sha512-vpeMIQKxczTD/0s2CdEWHcb0eeJe6TFjxb+J5xgX7hScxqrGuyjmv4c1D4A/gelKfyox0gJJwIHF+fLjeaM8kQ==" + }, + "hono@4.7.11": { + "integrity": "sha512-rv0JMwC0KALbbmwJDEnxvQCeJh+xbS3KEWW5PC9cMJ08Ur9xgatI0HmtgYZfOdOSOeYsp5LO2cOhdI8cLEbDEQ==" + }, + "kysely@0.28.2": { + "integrity": "sha512-4YAVLoF0Sf0UTqlhgQMFU9iQECdah7n+13ANkiuVfRvlK+uI0Etbgd7bVP36dKlG+NXWbhGua8vnGt+sdhvT7A==" + }, + "mustache@4.2.0": { + "integrity": "sha512-71ippSywq5Yb7/tVYyGbkBggbU8H3u5Rz56fH60jGFgr8uHwxs+aSKeqmluIVzM0m0kB7xQjKS6qPfd0b2ZoqQ==", + "bin": true + }, + "string-width@7.2.0": { + "integrity": "sha512-tsaTIkKW9b4N+AEj+SVA+WhJzV7/zMhcSu78mLKWSk7cXMOSHsBKFWUs0fWwq8QyK3MgJBQRX6Gbi4kYbdvGkQ==", + "dependencies": [ + "emoji-regex", + "get-east-asian-width", + "strip-ansi" + ] + }, + "strip-ansi@7.1.0": { + "integrity": "sha512-iq6eVVI64nQQTRYq2KtEg2d2uU7LElhTJwsH4YzIHZshxlgZms/wIc4VoDQTlG/IvVIrBKG06CrZnp0qv7hkcQ==", + "dependencies": [ + "ansi-regex" + ] + }, + "strnum@1.1.2": { + "integrity": "sha512-vrN+B7DBIoTTZjnPNewwhx6cBA/H+IS7rfW68n7XxC1y7uoiGQBxaKzqucGUgavX15dJgiGztLJ8vxuEzwqBdA==" + }, + "tslib@2.8.1": { + "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==" + }, + "undici-types@6.21.0": { + "integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==" + }, + "uuid@9.0.1": { + "integrity": "sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==", + "bin": true + }, + "wrap-ansi@9.0.0": { + "integrity": "sha512-G8ura3S+3Z2G+mkgNRq8dqaFZAuxfsxpBB8OCTGRTCtp+l/v9nbFNmCUP1BZMts3G1142MsZfn6eeUKrr4PD1Q==", + "dependencies": [ + "ansi-styles", + "string-width", + "strip-ansi" + ] + }, + "y18n@5.0.8": { + "integrity": "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==" + }, + "yargs-parser@22.0.0": { + "integrity": "sha512-rwu/ClNdSMpkSrUb+d6BRsSkLUq1fmfsY6TOpYzTwvwkg1/NRG85KBy3kq++A8LKQwX6lsu+aWad+2khvuXrqw==" + }, + "yargs@18.0.0": { + "integrity": "sha512-4UEqdc2RYGHZc7Doyqkrqiln3p9X2DZVxaGbwhn2pi7MrRagKaOcIKe8L3OxYcbhXLgLFUS3zAYuQjKBQgmuNg==", + "dependencies": [ + "cliui", + "escalade", + "get-caller-file", + "string-width", + "y18n", + "yargs-parser" + ] + }, + "zod@3.25.51": { + "integrity": "sha512-TQSnBldh+XSGL+opiSIq0575wvDPqu09AqWe1F7JhUMKY+M91/aGlK4MhpVNO7MgYfHcVCB1ffwAUTJzllKJqg==" + } + }, + "workspace": { + "dependencies": [ + "jsr:@std/collections@^1.1.1", + "jsr:@std/csv@^1.0.6", + "jsr:@std/dotenv@~0.225.5", + "jsr:@std/encoding@^1.0.10", + "jsr:@std/expect@^1.0.16", + "jsr:@std/fs@^1.0.18", + "jsr:@std/io@~0.225.2", + "jsr:@std/path@^1.1.0", + "jsr:@std/testing@^1.0.13", + "npm:@types/yargs@^17.0.33", + "npm:yargs@18" + ], + "members": { + "mail-relay": { + "dependencies": [ + "jsr:@db/sqlite@0.12", + "npm:@aws-sdk/client-s3@^3.821.0", + "npm:@aws-sdk/client-sesv2@^3.821.0", + "npm:@hono/zod-validator@0.7", + "npm:@smithy/fetch-http-handler@^5.0.4", + "npm:email-addresses@5", + "npm:hono@^4.7.11", + "npm:kysely@~0.28.2", + "npm:zod@^3.25.48" + ] + }, + "tools": { + "dependencies": [ + "npm:mustache@^4.2.0" + ] + } + } + } +} diff --git a/deno/mail-relay/app.ts b/deno/mail-relay/app.ts new file mode 100644 index 0000000..eeffc12 --- /dev/null +++ b/deno/mail-relay/app.ts @@ -0,0 +1,84 @@ +import { Hono } from "hono"; +import { logger as honoLogger } from "hono/logger"; + +import { LogFileProvider } from "@crupest/base/log"; + +import { + AliasRecipientMailHook, + FallbackRecipientHook, + MailDeliverer, + RecipientFromHeadersHook, +} from "./mail.ts"; +import { DovecotMailDeliverer } from "./dovecot.ts"; +import { DumbSmtpServer } from "./dumb-smtp-server.ts"; + +export function createInbound( + logFileProvider: LogFileProvider, + { + fallback, + mailDomain, + aliasFile, + ldaPath, + }: { + fallback: string[]; + mailDomain: string; + aliasFile: string; + ldaPath: string; + }, +) { + const deliverer = new DovecotMailDeliverer(logFileProvider, ldaPath); + deliverer.preHooks.push( + new RecipientFromHeadersHook(mailDomain), + new FallbackRecipientHook(new Set(fallback)), + new AliasRecipientMailHook(aliasFile), + ); + return deliverer; +} + +export function createHono(outbound: MailDeliverer, inbound: MailDeliverer) { + const hono = new Hono(); + + hono.onError((err, c) => { + console.error("Hono handler throws an error.", err); + return c.json({ msg: "Server error, check its log." }, 500); + }); + hono.use(honoLogger()); + hono.post("/send/raw", async (context) => { + const body = await context.req.text(); + if (body.trim().length === 0) { + return context.json({ msg: "Can't send an empty mail." }, 400); + } else { + const result = await outbound.deliverRaw(body); + return context.json({ + awsMessageId: result.awsMessageId, + }); + } + }); + hono.post("/receive/raw", async (context) => { + await inbound.deliverRaw(await context.req.text()); + return context.json({ msg: "Done!" }); + }); + + return hono; +} + +export function createSmtp(outbound: MailDeliverer) { + return new DumbSmtpServer(outbound); +} + +export async function sendMail(port: number) { + const decoder = new TextDecoder(); + let text = ""; + for await (const chunk of Deno.stdin.readable) { + text += decoder.decode(chunk); + } + + const res = await fetch(`http://127.0.0.1:${port}/send/raw`, { + method: "post", + body: text, + }); + const fn = res.ok ? "info" : "error"; + console[fn](res); + console[fn](await res.text()); + if (!res.ok) Deno.exit(-1); +} diff --git a/deno/mail-relay/aws/app.ts b/deno/mail-relay/aws/app.ts new file mode 100644 index 0000000..cb275ae --- /dev/null +++ b/deno/mail-relay/aws/app.ts @@ -0,0 +1,297 @@ +import { join } from "@std/path"; +import { z } from "zod"; +import { Hono } from "hono"; +import { zValidator } from "@hono/zod-validator"; +import { FetchHttpHandler } from "@smithy/fetch-http-handler"; +// @ts-types="npm:@types/yargs" +import yargs from "yargs"; + +import { LogFileProvider } from "@crupest/base/log"; +import { ConfigDefinition, ConfigProvider } from "@crupest/base/config"; +import { CronTask } from "@crupest/base/cron"; + +import { DbService } from "../db.ts"; +import { Mail } from "../mail.ts"; +import { + AwsMailMessageIdRewriteHook, + AwsMailMessageIdSaveHook, +} from "./mail.ts"; +import { AwsMailDeliverer } from "./deliver.ts"; +import { AwsMailFetcher, AwsS3MailConsumer } from "./fetch.ts"; +import { createHono, createInbound, createSmtp, sendMail } from "../app.ts"; + +const PREFIX = "crupest-mail-server"; +const CONFIG_DEFINITIONS = { + dataPath: { + description: "Path to save app persistent data.", + default: ".", + }, + mailDomain: { + description: + "The part after `@` of an address. Used to determine local recipients.", + }, + httpHost: { + description: "Listening address for http server.", + default: "0.0.0.0", + }, + httpPort: { description: "Listening port for http server.", default: "2345" }, + smtpHost: { + description: "Listening address for dumb smtp server.", + default: "127.0.0.1", + }, + smtpPort: { + description: "Listening port for dumb smtp server.", + default: "2346", + }, + ldaPath: { + description: "full path of lda executable", + default: "/dovecot/libexec/dovecot/dovecot-lda", + }, + inboundFallback: { + description: "comma separated addresses used as fallback recipients", + default: "", + }, + awsInboundPath: { + description: "(random set) path for aws sns", + }, + awsInboundKey: { + description: "(random set) http header Authorization for aws sns", + }, + awsRegion: { + description: "aws region", + }, + awsUser: { + description: "aws access key id", + }, + awsPassword: { + description: "aws secret access key", + secret: true, + }, + awsMailBucket: { + description: "aws s3 bucket saving raw mails", + secret: true, + }, +} as const satisfies ConfigDefinition; + +function createAwsOptions({ + user, + password, + region, +}: { + user: string; + password: string; + region: string; +}) { + return { + credentials: () => + Promise.resolve({ + accessKeyId: user, + secretAccessKey: password, + }), + requestHandler: new FetchHttpHandler(), + region, + }; +} + +function createOutbound( + awsOptions: ReturnType<typeof createAwsOptions>, + db: DbService, +) { + const deliverer = new AwsMailDeliverer(awsOptions); + deliverer.preHooks.push( + new AwsMailMessageIdRewriteHook(db.messageIdToAws.bind(db)), + ); + deliverer.postHooks.push( + new AwsMailMessageIdSaveHook((original, aws) => + db.addMessageIdMap({ message_id: original, aws_message_id: aws }).then() + ), + ); + return deliverer; +} + +function setupAwsHono( + hono: Hono, + options: { + path: string; + auth: string; + callback: (s3Key: string, recipients?: string[]) => Promise<void>; + }, +) { + hono.post( + `/${options.path}`, + async (ctx, next) => { + const auth = ctx.req.header("Authorization"); + if (auth !== options.auth) { + return ctx.json({ msg: "Bad auth!" }, 403); + } + await next(); + }, + zValidator( + "json", + z.object({ + key: z.string(), + recipients: z.optional(z.array(z.string())), + }), + ), + async (ctx) => { + const { key, recipients } = ctx.req.valid("json"); + await options.callback(key, recipients); + return ctx.json({ msg: "Done!" }); + }, + ); +} + +function createCron(fetcher: AwsMailFetcher, consumer: AwsS3MailConsumer) { + return new CronTask({ + name: "live-mail-recycler", + interval: 6 * 3600 * 1000, + callback: () => { + return fetcher.recycleLiveMails(consumer); + }, + startNow: true, + }); +} + +function createBaseServices() { + const config = new ConfigProvider(PREFIX, CONFIG_DEFINITIONS); + Deno.mkdirSync(config.get("dataPath"), { recursive: true }); + const logFileProvider = new LogFileProvider( + join(config.get("dataPath"), "log"), + ); + return { config, logFileProvider }; +} + +function createAwsFetchOnlyServices() { + const services = createBaseServices(); + const { config } = services; + + const awsOptions = createAwsOptions({ + user: config.get("awsUser"), + password: config.get("awsPassword"), + region: config.get("awsRegion"), + }); + const fetcher = new AwsMailFetcher(awsOptions, config.get("awsMailBucket")); + + return { ...services, awsOptions, fetcher }; +} + +function createAwsRecycleOnlyServices() { + const services = createAwsFetchOnlyServices(); + const { config, logFileProvider } = services; + + const inbound = createInbound(logFileProvider, { + fallback: config.getList("inboundFallback"), + ldaPath: config.get("ldaPath"), + aliasFile: join(config.get("dataPath"), "aliases.csv"), + mailDomain: config.get("mailDomain"), + }); + const recycler = (rawMail: string, _: unknown): Promise<void> => + inbound.deliver({ mail: new Mail(rawMail) }).then(); + + return { ...services, inbound, recycler }; +} +function createAwsServices() { + const services = createAwsRecycleOnlyServices(); + const { config, awsOptions } = services; + + const dbService = new DbService(join(config.get("dataPath"), "db.sqlite")); + const outbound = createOutbound(awsOptions, dbService); + + return { ...services, dbService, outbound }; +} + +function createServerServices() { + const services = createAwsServices(); + const { config, outbound, inbound, fetcher } = services; + + const smtp = createSmtp(outbound); + const hono = createHono(outbound, inbound); + setupAwsHono(hono, { + path: config.get("awsInboundPath"), + auth: config.get("awsInboundKey"), + callback: (s3Key, recipients) => { + return fetcher.consumeS3Mail( + s3Key, + (rawMail, _) => + inbound.deliver({ mail: new Mail(rawMail), recipients }).then(), + ); + }, + }); + + return { ...services, smtp, hono }; +} + +function serve(cron: boolean = false) { + const { config, fetcher, recycler, smtp, hono } = createServerServices(); + smtp.serve({ + hostname: config.get("smtpHost"), + port: config.getInt("smtpPort"), + }); + Deno.serve( + { + hostname: config.get("httpHost"), + port: config.getInt("httpPort"), + }, + hono.fetch, + ); + + if (cron) { + createCron(fetcher, recycler); + } +} + +async function listLives() { + const { fetcher } = createAwsFetchOnlyServices(); + const liveMails = await fetcher.listLiveMails(); + console.info(`Total ${liveMails.length}:`); + if (liveMails.length !== 0) { + console.info(liveMails.join("\n")); + } +} + +async function recycleLives() { + const { fetcher, recycler } = createAwsRecycleOnlyServices(); + await fetcher.recycleLiveMails(recycler); +} + +if (import.meta.main) { + await yargs(Deno.args) + .scriptName("mail-relay") + .command({ + command: "sendmail", + describe: "send mail via this server's endpoint", + handler: async (_argv) => { + const { config } = createBaseServices(); + await sendMail(config.getInt("httpPort")); + }, + }) + .command({ + command: "live", + describe: "work with live mails", + builder: (builder) => { + return builder + .command({ + command: "list", + describe: "list live mails", + handler: listLives, + }) + .command({ + command: "recycle", + describe: "recycle all live mails", + handler: recycleLives, + }) + .demandCommand(1, "One command must be specified."); + }, + handler: () => {}, + }) + .command({ + command: "serve", + describe: "start the http and smtp servers", + builder: (builder) => builder.option("real", { type: "boolean" }), + handler: (argv) => serve(argv.real), + }) + .demandCommand(1, "One command must be specified.") + .help() + .strict() + .parse(); +} diff --git a/deno/mail-relay/aws/deliver.ts b/deno/mail-relay/aws/deliver.ts new file mode 100644 index 0000000..4dd4b3a --- /dev/null +++ b/deno/mail-relay/aws/deliver.ts @@ -0,0 +1,60 @@ +import { + SendEmailCommand, + SESv2Client, + SESv2ClientConfig, +} from "@aws-sdk/client-sesv2"; + +import { Mail, MailDeliverContext, SyncMailDeliverer } from "../mail.ts"; + +declare module "../mail.ts" { + interface MailDeliverResult { + awsMessageId?: string; + } +} + +export class AwsMailDeliverer extends SyncMailDeliverer { + readonly name = "aws"; + readonly #aws; + readonly #ses; + + constructor(aws: SESv2ClientConfig) { + super(); + this.#aws = aws; + this.#ses = new SESv2Client(aws); + } + + protected override async doDeliver( + mail: Mail, + context: MailDeliverContext, + ): Promise<void> { + console.info("Begin to call aws send-email api..."); + + try { + const sendCommand = new SendEmailCommand({ + Content: { + Raw: { Data: mail.toUtf8Bytes() }, + }, + }); + + const res = await this.#ses.send(sendCommand); + if (res.MessageId == null) { + console.warn("Aws send-email returns no message id."); + } else { + context.result.awsMessageId = + `${res.MessageId}@${this.#aws.region}.amazonses.com`; + } + + context.result.recipients.set("*", { + kind: "done", + message: + `Successfully called aws send-email, message id ${context.result.awsMessageId}.`, + }); + } catch (cause) { + context.result.recipients.set("*", { + kind: "fail", + message: "An error was thrown when calling aws send-email." + cause, + cause, + }); + } + } +} diff --git a/deno/mail-relay/aws/fetch.ts b/deno/mail-relay/aws/fetch.ts new file mode 100644 index 0000000..9278e63 --- /dev/null +++ b/deno/mail-relay/aws/fetch.ts @@ -0,0 +1,127 @@ +import { + CopyObjectCommand, + DeleteObjectCommand, + GetObjectCommand, + ListObjectsV2Command, + S3Client, + S3ClientConfig, +} from "@aws-sdk/client-s3"; + +import { toFileNameString } from "@crupest/base"; + +import { Mail } from "../mail.ts"; + +async function s3MoveObject( + client: S3Client, + bucket: string, + path: string, + newPath: string, +): Promise<void> { + const copyCommand = new CopyObjectCommand({ + Bucket: bucket, + Key: newPath, + CopySource: `${bucket}/${path}`, + }); + await client.send(copyCommand); + + const deleteCommand = new DeleteObjectCommand({ + Bucket: bucket, + Key: path, + }); + await client.send(deleteCommand); +} + +const AWS_SES_S3_SETUP_TAG = "AMAZON_SES_SETUP_NOTIFICATION"; + +export type AwsS3MailConsumer = ( + rawMail: string, + s3Key: string, +) => Promise<void>; + +export class AwsMailFetcher { + readonly #livePrefix = "mail/live/"; + readonly #archivePrefix = "mail/archive/"; + readonly #s3; + readonly #bucket; + + constructor(aws: S3ClientConfig, bucket: string) { + this.#s3 = new S3Client(aws); + this.#bucket = bucket; + } + + async listLiveMails(): Promise<string[]> { + console.info("Begin to retrieve live mails."); + + const listCommand = new ListObjectsV2Command({ + Bucket: this.#bucket, + Prefix: this.#livePrefix, + }); + const res = await this.#s3.send(listCommand); + + if (res.Contents == null) { + console.warn("Listing live mails in S3 returns null Content."); + return []; + } + + const result: string[] = []; + for (const object of res.Contents) { + if (object.Key == null) { + console.warn("Listing live mails in S3 returns an object with no Key."); + continue; + } + + if (object.Key.endsWith(AWS_SES_S3_SETUP_TAG)) continue; + + result.push(object.Key.slice(this.#livePrefix.length)); + } + return result; + } + + async consumeS3Mail(s3Key: string, consumer: AwsS3MailConsumer) { + console.info(`Begin to consume s3 mail ${s3Key} ...`); + + console.info(`Fetching s3 mail ${s3Key}...`); + const mailPath = `${this.#livePrefix}${s3Key}`; + const command = new GetObjectCommand({ + Bucket: this.#bucket, + Key: mailPath, + }); + const res = await this.#s3.send(command); + + if (res.Body == null) { + throw new Error("S3 mail returns a null body."); + } + + const rawMail = await res.Body.transformToString(); + console.info(`Done fetching s3 mail ${s3Key}.`); + + console.info(`Calling consumer...`); + await consumer(rawMail, s3Key); + console.info(`Done consuming s3 mail ${s3Key}.`); + + const date = new Mail(rawMail) + .startSimpleParse() + .sections() + .headers() + .date(); + const dateString = date != null + ? toFileNameString(date, true) + : "invalid-date"; + const newPath = `${this.#archivePrefix}${dateString}/${s3Key}`; + + console.info(`Archiving s3 mail ${s3Key} to ${newPath}...`); + await s3MoveObject(this.#s3, this.#bucket, mailPath, newPath); + console.info(`Done archiving s3 mail ${s3Key}.`); + + console.info(`Done consuming s3 mail ${s3Key}.`); + } + + async recycleLiveMails(consumer: AwsS3MailConsumer) { + console.info("Begin to recycle live mails..."); + const mails = await this.listLiveMails(); + console.info(`Found ${mails.length} live mails`); + for (const s3Key of mails) { + await this.consumeS3Mail(s3Key, consumer); + } + } +} diff --git a/deno/mail-relay/aws/mail.ts b/deno/mail-relay/aws/mail.ts new file mode 100644 index 0000000..cc05d23 --- /dev/null +++ b/deno/mail-relay/aws/mail.ts @@ -0,0 +1,49 @@ +import { MailDeliverContext, MailDeliverHook } from "../mail.ts"; + +export class AwsMailMessageIdRewriteHook implements MailDeliverHook { + readonly #lookup; + + constructor(lookup: (origin: string) => Promise<string | null>) { + this.#lookup = lookup; + } + + async callback(context: MailDeliverContext): Promise<void> { + console.info("Rewrite message ids..."); + const addresses = context.mail.simpleFindAllAddresses(); + console.info(`Addresses found in mail: ${addresses.join(", ")}.`); + for (const address of addresses) { + const awsMessageId = await this.#lookup(address); + if (awsMessageId != null && awsMessageId.length !== 0) { + console.info(`Rewrite ${address} to ${awsMessageId}.`); + context.mail.raw = context.mail.raw.replaceAll(address, awsMessageId); + } + } + console.info("Done rewrite message ids."); + } +} + +export class AwsMailMessageIdSaveHook implements MailDeliverHook { + readonly #record; + + constructor(record: (original: string, aws: string) => Promise<void>) { + this.#record = record; + } + + async callback(context: MailDeliverContext): Promise<void> { + console.info("Save aws message ids..."); + const messageId = context.mail + .startSimpleParse() + .sections() + .headers() + .messageId(); + if (messageId == null) { + console.info("Original mail does not have message id. Skip saving."); + return; + } + if (context.result.awsMessageId != null) { + console.info(`Saving ${messageId} => ${context.result.awsMessageId}.`); + await this.#record(messageId, context.result.awsMessageId); + } + console.info("Done save message ids."); + } +} diff --git a/deno/mail-relay/db.test.ts b/deno/mail-relay/db.test.ts new file mode 100644 index 0000000..60035c4 --- /dev/null +++ b/deno/mail-relay/db.test.ts @@ -0,0 +1,23 @@ +import { describe, it } from "@std/testing/bdd"; +import { expect } from "@std/expect/expect"; + +import { DbService } from "./db.ts"; + +describe("DbService", () => { + const mockRow = { + message_id: "mock-message-id@mock.mock", + aws_message_id: "mock-aws-message-id@mock.mock", + }; + + it("works", async () => { + const db = new DbService(":memory:"); + await db.migrate(); + await db.addMessageIdMap(mockRow); + expect(await db.messageIdToAws(mockRow.message_id)).toBe( + mockRow.aws_message_id, + ); + expect(await db.messageIdFromAws(mockRow.aws_message_id)).toBe( + mockRow.message_id, + ); + }); +}); diff --git a/deno/mail-relay/db.ts b/deno/mail-relay/db.ts new file mode 100644 index 0000000..062700b --- /dev/null +++ b/deno/mail-relay/db.ts @@ -0,0 +1,146 @@ +import { + Generated, + Insertable, + Kysely, + Migration, + Migrator, + SqliteDatabase, + SqliteDialect, + SqliteStatement, +} from "kysely"; +import * as sqlite from "@db/sqlite"; + +class SqliteStatementAdapter implements SqliteStatement { + constructor(public readonly stmt: sqlite.Statement) {} + + get reader(): boolean { + return this.stmt.columnNames().length >= 1; + } + + all(parameters: readonly unknown[]): unknown[] { + return this.stmt.all(...(parameters as sqlite.BindValue[])); + } + + iterate(parameters: readonly unknown[]): IterableIterator<unknown> { + return this.stmt.iter(...(parameters as sqlite.BindValue[])); + } + + run(parameters: readonly unknown[]): { + changes: number | bigint; + lastInsertRowid: number | bigint; + } { + const { db } = this.stmt; + const totalChangesBefore = db.totalChanges; + const changes = this.stmt.run(...(parameters as sqlite.BindValue[])); + return { + changes: totalChangesBefore === db.totalChanges ? 0 : changes, + lastInsertRowid: db.lastInsertRowId, + }; + } +} + +class SqliteDatabaseAdapter implements SqliteDatabase { + constructor(public readonly db: sqlite.Database) {} + + prepare(sql: string): SqliteStatementAdapter { + return new SqliteStatementAdapter(this.db.prepare(sql)); + } + + close(): void { + this.db.close(); + } +} + +export class DbError extends Error {} + +interface AwsMessageIdMapTable { + id: Generated<number>; + message_id: string; + aws_message_id: string; +} + +interface Database { + aws_message_id_map: AwsMessageIdMapTable; +} + +const migrations: Record<string, Migration> = { + "0001-init": { + // deno-lint-ignore no-explicit-any + async up(db: Kysely<any>): Promise<void> { + await db.schema + .createTable("aws_message_id_map") + .addColumn("id", "integer", (col) => col.primaryKey().autoIncrement()) + .addColumn("message_id", "text", (col) => col.notNull().unique()) + .addColumn("aws_message_id", "text", (col) => col.notNull().unique()) + .execute(); + + for (const column of ["message_id", "aws_message_id"]) { + await db.schema + .createIndex(`aws_message_id_map_${column}`) + .on("aws_message_id_map") + .column(column) + .execute(); + } + }, + + // deno-lint-ignore no-explicit-any + async down(db: Kysely<any>): Promise<void> { + await db.schema.dropTable("aws_message_id_map").execute(); + }, + }, +}; + +export class DbService { + #db; + #kysely; + #migrator; + + constructor(public readonly path: string) { + this.#db = new sqlite.Database(path); + this.#kysely = new Kysely<Database>({ + dialect: new SqliteDialect({ + database: new SqliteDatabaseAdapter(this.#db), + }), + }); + this.#migrator = new Migrator({ + db: this.#kysely, + provider: { + getMigrations(): Promise<Record<string, Migration>> { + return Promise.resolve(migrations); + }, + }, + }); + } + + async migrate(): Promise<void> { + await this.#migrator.migrateToLatest(); + } + + async addMessageIdMap( + mail: Insertable<AwsMessageIdMapTable>, + ): Promise<number> { + const inserted = await this.#kysely + .insertInto("aws_message_id_map") + .values(mail) + .executeTakeFirstOrThrow(); + return Number(inserted.insertId!); + } + + async messageIdToAws(messageId: string): Promise<string | null> { + const row = await this.#kysely + .selectFrom("aws_message_id_map") + .where("message_id", "=", messageId) + .select("aws_message_id") + .executeTakeFirst(); + return row?.aws_message_id ?? null; + } + + async messageIdFromAws(awsMessageId: string): Promise<string | null> { + const row = await this.#kysely + .selectFrom("aws_message_id_map") + .where("aws_message_id", "=", awsMessageId) + .select("message_id") + .executeTakeFirst(); + return row?.message_id ?? null; + } +} diff --git a/deno/mail-relay/deno.json b/deno/mail-relay/deno.json new file mode 100644 index 0000000..9105747 --- /dev/null +++ b/deno/mail-relay/deno.json @@ -0,0 +1,18 @@ +{ + "version": "0.1.0", + "tasks": { + "run": "deno run -A aws/app.ts", + "compile": "deno compile -o out/crupest-relay -A aws/app.ts" + }, + "imports": { + "@aws-sdk/client-s3": "npm:@aws-sdk/client-s3@^3.821.0", + "@aws-sdk/client-sesv2": "npm:@aws-sdk/client-sesv2@^3.821.0", + "@db/sqlite": "jsr:@db/sqlite@^0.12.0", + "@hono/zod-validator": "npm:@hono/zod-validator@^0.7.0", + "@smithy/fetch-http-handler": "npm:@smithy/fetch-http-handler@^5.0.4", + "email-addresses": "npm:email-addresses@^5.0.0", + "hono": "npm:hono@^4.7.11", + "kysely": "npm:kysely@^0.28.2", + "zod": "npm:zod@^3.25.48" + } +} diff --git a/deno/mail-relay/dovecot.ts b/deno/mail-relay/dovecot.ts new file mode 100644 index 0000000..bace225 --- /dev/null +++ b/deno/mail-relay/dovecot.ts @@ -0,0 +1,99 @@ +import { basename } from "@std/path"; + +import { LogFileProvider } from "@crupest/base/log"; + +import { Mail, MailDeliverContext, MailDeliverer } from "./mail.ts"; + +export class DovecotMailDeliverer extends MailDeliverer { + readonly name = "dovecot"; + readonly #logFileProvider; + readonly #ldaPath; + + constructor(logFileProvider: LogFileProvider, ldaPath: string) { + super(); + this.#logFileProvider = logFileProvider; + this.#ldaPath = ldaPath; + } + + protected override async doDeliver( + mail: Mail, + context: MailDeliverContext, + ): Promise<void> { + const ldaPath = this.#ldaPath; + const ldaBinName = basename(ldaPath); + const utf8Stream = mail.toUtf8Bytes(); + + const recipients = [...context.recipients]; + + if (recipients.length === 0) { + context.result.message = + "Failed to deliver to dovecot, no recipients are specified."; + return; + } + + console.info(`Deliver to dovecot users: ${recipients.join(", ")}.`); + + for (const recipient of recipients) { + try { + const commandArgs = ["-d", recipient]; + console.info(`Run ${ldaBinName} ${commandArgs.join(" ")}...`); + + const ldaCommand = new Deno.Command(ldaPath, { + args: commandArgs, + stdin: "piped", + stdout: "piped", + stderr: "piped", + }); + + const ldaProcess = ldaCommand.spawn(); + using logFiles = await this.#logFileProvider + .createExternalLogStreamsForProgram( + ldaBinName, + ); + ldaProcess.stdout.pipeTo(logFiles.stdout); + ldaProcess.stderr.pipeTo(logFiles.stderr); + + const stdinWriter = ldaProcess.stdin.getWriter(); + await stdinWriter.write(utf8Stream); + await stdinWriter.close(); + + const status = await ldaProcess.status; + + if (status.success) { + context.result.recipients.set(recipient, { + kind: "done", + message: `${ldaBinName} exited with success.`, + }); + } else { + let message = `${ldaBinName} exited with error code ${status.code}`; + + if (status.signal != null) { + message += ` (signal ${status.signal})`; + } + + // https://doc.dovecot.org/main/core/man/dovecot-lda.1.html + switch (status.code) { + case 67: + message += ", recipient user not known"; + break; + case 75: + message += ", temporary error"; + break; + } + + message += "."; + + context.result.recipients.set(recipient, { kind: "fail", message }); + } + } catch (cause) { + context.result.recipients.set(recipient, { + kind: "fail", + message: "An error is thrown when running lda: " + cause, + cause, + }); + } + } + + console.info("Done handling all recipients."); + } +} diff --git a/deno/mail-relay/dumb-smtp-server.ts b/deno/mail-relay/dumb-smtp-server.ts new file mode 100644 index 0000000..ac7069c --- /dev/null +++ b/deno/mail-relay/dumb-smtp-server.ts @@ -0,0 +1,130 @@ +import { MailDeliverer } from "./mail.ts"; + +const CRLF = "\r\n"; + +function createResponses(host: string, port: number | string) { + const serverName = `[${host}]:${port}`; + return { + serverName, + READY: `220 ${serverName} SMTP Ready`, + EHLO: `250 ${serverName}`, + MAIL: "250 2.1.0 Sender OK", + RCPT: "250 2.1.5 Recipient OK", + DATA: "354 Start mail input; end with <CRLF>.<CRLF>", + QUIT: `211 2.0.0 ${serverName} closing connection`, + INVALID: "500 5.5.1 Error: command not recognized", + } as const; +} + +const LOG_TAG = "[dumb-smtp]"; + +export class DumbSmtpServer { + #deliverer; + #responses: ReturnType<typeof createResponses> = createResponses( + "invalid", + "invalid", + ); + + constructor(deliverer: MailDeliverer) { + this.#deliverer = deliverer; + } + + async #handleConnection(conn: Deno.Conn) { + using disposeStack = new DisposableStack(); + disposeStack.defer(() => { + console.info(LOG_TAG, "Close session's tcp connection."); + conn.close(); + }); + + console.info(LOG_TAG, "New session's tcp connection established."); + + const writer = conn.writable.getWriter(); + disposeStack.defer(() => writer.releaseLock()); + const reader = conn.readable.getReader(); + disposeStack.defer(() => reader.releaseLock()); + + const [decoder, encoder] = [new TextDecoder(), new TextEncoder()]; + const decode = (data: Uint8Array) => decoder.decode(data); + const send = async (s: string) => { + console.info(LOG_TAG, "Send line: " + s); + await writer.write(encoder.encode(s + CRLF)); + }; + + let buffer: string = ""; + let rawMail: string | null = null; + + await send(this.#responses["READY"]); + + while (true) { + const { value, done } = await reader.read(); + if (done) break; + + buffer += decode(value); + + while (true) { + const eolPos = buffer.indexOf(CRLF); + if (eolPos === -1) break; + + const line = buffer.slice(0, eolPos); + buffer = buffer.slice(eolPos + CRLF.length); + + if (rawMail == null) { + console.info(LOG_TAG, "Received line: " + line); + const upperLine = line.toUpperCase(); + if (upperLine.startsWith("EHLO") || upperLine.startsWith("HELO")) { + await send(this.#responses["EHLO"]); + } else if (upperLine.startsWith("MAIL FROM:")) { + await send(this.#responses["MAIL"]); + } else if (upperLine.startsWith("RCPT TO:")) { + await send(this.#responses["RCPT"]); + } else if (upperLine === "DATA") { + await send(this.#responses["DATA"]); + console.info(LOG_TAG, "Begin to receive mail data..."); + rawMail = ""; + } else if (upperLine === "QUIT") { + await send(this.#responses["QUIT"]); + return; + } else { + console.warn(LOG_TAG, "Unrecognized command from client: " + line); + await send(this.#responses["INVALID"]); + return; + } + } else { + if (line === ".") { + try { + console.info(LOG_TAG, "Mail data Received, begin to relay..."); + const { message } = await this.#deliverer.deliverRaw(rawMail); + await send(`250 2.6.0 ${message}`); + rawMail = null; + console.info(LOG_TAG, "Relay succeeded."); + } catch (err) { + console.error(LOG_TAG, "Relay failed.", err); + await send("554 5.3.0 Error: check server log"); + return; + } + } else { + const dataLine = line.startsWith("..") ? line.slice(1) : line; + rawMail += dataLine + CRLF; + } + } + } + } + } + + async serve(options: { hostname: string; port: number }) { + const listener = Deno.listen(options); + this.#responses = createResponses(options.hostname, options.port); + console.info( + LOG_TAG, + `Dumb SMTP server starts to listen on ${this.#responses.serverName}.`, + ); + + for await (const conn of listener) { + try { + await this.#handleConnection(conn); + } catch (cause) { + console.error(LOG_TAG, "Tcp connection throws an error.", cause); + } + } + } +} diff --git a/deno/mail-relay/mail.test.ts b/deno/mail-relay/mail.test.ts new file mode 100644 index 0000000..cd0c38d --- /dev/null +++ b/deno/mail-relay/mail.test.ts @@ -0,0 +1,126 @@ +import { describe, it } from "@std/testing/bdd"; +import { expect, fn } from "@std/expect"; + +import { Mail, MailDeliverContext, MailDeliverer } from "./mail.ts"; + +const mockDate = "Fri, 02 May 2025 08:33:02 +0000"; +const mockMessageId = "mock-message-id@from.mock"; +const mockMessageId2 = "mock-message-id-2@from.mock"; +const mockFromAddress = "mock@from.mock"; +const mockCcAddress = "mock@cc.mock"; +const mockBodyStr = `This is body content. +Line 2 ${mockMessageId2} + +Line 4`; +const mockHeaders = [ + ["Content-Disposition", "inline"], + ["Content-Transfer-Encoding", "quoted-printable"], + ["MIME-Version", "1.0"], + ["X-Mailer", "MIME-tools 5.509 (Entity 5.509)"], + ["Content-Type", "text/plain; charset=utf-8"], + ["From", `"Mock From" <${mockFromAddress}>`], + [ + "To", + `"John \\"Big\\" Doe" <john@example.com>, "Alice (Work)" <alice+work@example.com>, + undisclosed-recipients:;, "Group: Team" <team@company.com>, + "Escaped, Name" <escape@test.com>, just@email.com, + "Comment (This is valid)" <comment@domain.net>, + "Odd @Chars" <weird!#$%'*+-/=?^_\`{|}~@char-test.com>, + "Non-ASCII 用户" <user@例子.中国>, + admin@[192.168.1.1]`, + ], + ["CC", `Mock CC <${mockCcAddress}>`], + ["Subject", "A very long mock\n subject"], + ["Message-ID", `<${mockMessageId}>`], + ["Date", mockDate], +]; +const mockHeaderStr = mockHeaders.map((h) => h[0] + ": " + h[1]).join("\n"); +const mockMailStr = mockHeaderStr + "\n\n" + mockBodyStr; +const mockCrlfMailStr = mockMailStr.replaceAll("\n", "\r\n"); +const mockToAddresses = [ + "john@example.com", + "alice+work@example.com", + "team@company.com", + "escape@test.com", + "just@email.com", + "comment@domain.net", + "weird!#$%'*+-/=?^_`{|}~@char-test.com", + "user@例子.中国", + "admin@[192.168.1.1]", +]; + +describe("Mail", () => { + it("simple parse", () => { + const parsed = new Mail(mockMailStr).startSimpleParse().sections(); + expect(parsed.header).toEqual(mockHeaderStr); + expect(parsed.body).toEqual(mockBodyStr); + expect(parsed.sep).toBe("\n"); + expect(parsed.eol).toBe("\n"); + }); + + it("simple parse crlf", () => { + const parsed = new Mail(mockCrlfMailStr).startSimpleParse().sections(); + expect(parsed.sep).toBe("\r\n"); + expect(parsed.eol).toBe("\r\n"); + }); + + it("simple parse date", () => { + expect( + new Mail(mockMailStr).startSimpleParse().sections().headers().date(), + ).toEqual(new Date(mockDate)); + }); + + it("simple parse headers", () => { + expect( + new Mail(mockMailStr).startSimpleParse().sections().headers().fields, + ).toEqual(mockHeaders.map((h) => [h[0], " " + h[1].replaceAll("\n", "")])); + }); + + it("parse recipients", () => { + const mail = new Mail(mockMailStr); + expect([ + ...mail.startSimpleParse().sections().headers().recipients(), + ]).toEqual([...mockToAddresses, mockCcAddress]); + expect([ + ...mail.startSimpleParse().sections().headers().recipients({ + domain: "example.com", + }), + ]).toEqual( + [...mockToAddresses, mockCcAddress].filter((a) => + a.endsWith("example.com") + ), + ); + }); + + it("find all addresses", () => { + const mail = new Mail(mockMailStr); + expect(mail.simpleFindAllAddresses()).toEqual([ + "mock@from.mock", + "john@example.com", + "alice+work@example.com", + "team@company.com", + "escape@test.com", + "just@email.com", + "comment@domain.net", + "mock@cc.mock", + "mock-message-id@from.mock", + "mock-message-id-2@from.mock", + ]); + }); +}); + +describe("MailDeliverer", () => { + class MockMailDeliverer extends MailDeliverer { + name = "mock"; + override doDeliver = fn((_: Mail, ctx: MailDeliverContext) => { + ctx.result.recipients.set("*", { kind: "done", message: "success" }); + return Promise.resolve(); + }) as MailDeliverer["doDeliver"]; + } + const mockDeliverer = new MockMailDeliverer(); + + it("deliver success", async () => { + await mockDeliverer.deliverRaw(mockMailStr); + expect(mockDeliverer.doDeliver).toHaveBeenCalledTimes(1); + }); +}); diff --git a/deno/mail-relay/mail.ts b/deno/mail-relay/mail.ts new file mode 100644 index 0000000..d6dfe65 --- /dev/null +++ b/deno/mail-relay/mail.ts @@ -0,0 +1,330 @@ +import { encodeBase64 } from "@std/encoding/base64"; +import { parse } from "@std/csv/parse"; +import emailAddresses from "email-addresses"; + +class MailSimpleParseError extends Error {} + +class MailSimpleParsedHeaders { + constructor(public fields: [key: string, value: string][]) {} + + getFirst(fieldKey: string): string | undefined { + for (const [key, value] of this.fields) { + if (key.toLowerCase() === fieldKey.toLowerCase()) return value; + } + return undefined; + } + + messageId(): string | undefined { + const messageIdField = this.getFirst("message-id"); + if (messageIdField == null) return undefined; + + const match = messageIdField.match(/\<(.*?)\>/); + if (match != null) { + return match[1]; + } else { + console.warn("Invalid message-id header of mail: " + messageIdField); + return undefined; + } + } + + date(invalidToUndefined: boolean = true): Date | undefined { + const dateField = this.getFirst("date"); + if (dateField == null) return undefined; + + const date = new Date(dateField); + if (invalidToUndefined && isNaN(date.getTime())) { + console.warn(`Invalid date string (${dateField}) found in header.`); + return undefined; + } + return date; + } + + recipients(options?: { domain?: string; headers?: string[] }): Set<string> { + const domain = options?.domain; + const headers = options?.headers ?? ["to", "cc", "bcc", "x-original-to"]; + const recipients = new Set<string>(); + for (const [key, value] of this.fields) { + if (headers.includes(key.toLowerCase())) { + emailAddresses + .parseAddressList(value) + ?.flatMap((a) => (a.type === "mailbox" ? a : a.addresses)) + ?.forEach(({ address }) => { + if (domain == null || address.endsWith(domain)) { + recipients.add(address); + } + }); + } + } + return recipients; + } +} + +class MailSimpleParsedSections { + header: string; + body: string; + eol: string; + sep: string; + + constructor(raw: string) { + const twoEolMatch = raw.match(/(\r?\n)(\r?\n)/); + if (twoEolMatch == null) { + throw new MailSimpleParseError( + "No header/body section separator (2 successive EOLs) found.", + ); + } + + const [eol, sep] = [twoEolMatch[1], twoEolMatch[2]]; + + if (eol !== sep) { + console.warn("Different EOLs (\\r\\n, \\n) found."); + } + + this.header = raw.slice(0, twoEolMatch.index!); + this.body = raw.slice(twoEolMatch.index! + eol.length + sep.length); + this.eol = eol; + this.sep = sep; + } + + headers(): MailSimpleParsedHeaders { + const headers = [] as [key: string, value: string][]; + + let field: string | null = null; + let lineNumber = 1; + + const handleField = () => { + if (field == null) return; + const sepPos = field.indexOf(":"); + if (sepPos === -1) { + throw new MailSimpleParseError(`No ':' in the header line: ${field}`); + } + headers.push([field.slice(0, sepPos).trim(), field.slice(sepPos + 1)]); + field = null; + }; + + for (const line of this.header.trimEnd().split(/\r?\n|\r/)) { + if (line.match(/^\s/)) { + if (field == null) { + throw new MailSimpleParseError("Header section starts with a space."); + } + field += line; + } else { + handleField(); + field = line; + } + lineNumber += 1; + } + + handleField(); + + return new MailSimpleParsedHeaders(headers); + } +} + +export class Mail { + constructor(public raw: string) {} + + toUtf8Bytes(): Uint8Array { + const utf8Encoder = new TextEncoder(); + return utf8Encoder.encode(this.raw); + } + + toBase64(): string { + return encodeBase64(this.raw); + } + + startSimpleParse() { + return { sections: () => new MailSimpleParsedSections(this.raw) }; + } + + simpleFindAllAddresses(): string[] { + const re = /,?\<?([a-z0-9_'+\-\.]+\@[a-z0-9_'+\-\.]+)\>?,?/gi; + return [...this.raw.matchAll(re)].map((m) => m[1]); + } +} + +export type MailDeliverResultKind = "done" | "fail"; + +export interface MailDeliverRecipientResult { + kind: MailDeliverResultKind; + message: string; + cause?: unknown; +} + +export class MailDeliverResult { + message: string = ""; + recipients: Map<string, MailDeliverRecipientResult> = new Map(); + + constructor(public mail: Mail) {} + + hasError(): boolean { + return ( + this.recipients.size === 0 || + this.recipients.values().some((r) => r.kind !== "done") + ); + } + + [Symbol.for("Deno.customInspect")]() { + return [ + `message: ${this.message}`, + ...this.recipients + .entries() + .map( + ([recipient, result]) => + `${recipient} [${result.kind}]: ${result.message}`, + ), + ].join("\n"); + } +} + +export class MailDeliverContext { + readonly recipients: Set<string> = new Set(); + readonly result; + + constructor(public mail: Mail) { + this.result = new MailDeliverResult(this.mail); + } +} + +export interface MailDeliverHook { + callback(context: MailDeliverContext): Promise<void>; +} + +export abstract class MailDeliverer { + abstract readonly name: string; + preHooks: MailDeliverHook[] = []; + postHooks: MailDeliverHook[] = []; + + protected abstract doDeliver( + mail: Mail, + context: MailDeliverContext, + ): Promise<void>; + + async deliverRaw(rawMail: string) { + return await this.deliver({ mail: new Mail(rawMail) }); + } + + async deliver(options: { + mail: Mail; + recipients?: string[]; + }): Promise<MailDeliverResult> { + console.info(`Begin to deliver mail via ${this.name}...`); + + const context = new MailDeliverContext(options.mail); + options.recipients?.forEach((r) => context.recipients.add(r)); + + for (const hook of this.preHooks) { + await hook.callback(context); + } + + await this.doDeliver(context.mail, context); + + for (const hook of this.postHooks) { + await hook.callback(context); + } + + console.info("Deliver result:"); + console.info(context.result); + + if (context.result.hasError()) { + throw new Error("Mail failed to deliver."); + } + + return context.result; + } +} + +export abstract class SyncMailDeliverer extends MailDeliverer { + #last: Promise<void> = Promise.resolve(); + + override async deliver(options: { + mail: Mail; + recipients?: string[]; + }): Promise<MailDeliverResult> { + console.info( + "The mail deliverer is sync. Wait for last delivering done...", + ); + await this.#last; + const result = super.deliver(options); + this.#last = result.then( + () => {}, + () => {}, + ); + return result; + } +} + +export class RecipientFromHeadersHook implements MailDeliverHook { + constructor(public mailDomain: string) {} + + callback(context: MailDeliverContext) { + if (context.recipients.size !== 0) { + console.warn( + "Recipients are already filled. Won't set them with ones in headers.", + ); + } else { + context.mail + .startSimpleParse() + .sections() + .headers() + .recipients({ + domain: this.mailDomain, + }) + .forEach((r) => context.recipients.add(r)); + + console.info( + "Recipients found from mail headers: " + + [...context.recipients].join(", "), + ); + } + return Promise.resolve(); + } +} + +export class FallbackRecipientHook implements MailDeliverHook { + constructor(public fallback: Set<string> = new Set()) {} + + callback(context: MailDeliverContext) { + if (context.recipients.size === 0) { + console.info( + "No recipients, fill with fallback: " + [...this.fallback].join(", "), + ); + this.fallback.forEach((a) => context.recipients.add(a)); + } + return Promise.resolve(); + } +} + +export class AliasRecipientMailHook implements MailDeliverHook { + #aliasFile; + + constructor(aliasFile: string) { + this.#aliasFile = aliasFile; + } + + async #parseAliasFile(): Promise<Map<string, string>> { + const result = new Map(); + if ((await Deno.stat(this.#aliasFile)).isFile) { + console.info(`Found recipients alias file: ${this.#aliasFile}.`); + const text = await Deno.readTextFile(this.#aliasFile); + const csv = parse(text); + for (const [real, ...aliases] of csv) { + aliases.forEach((a) => result.set(a, real)); + } + } + return result; + } + + async callback(context: MailDeliverContext) { + const aliases = await this.#parseAliasFile(); + for (const recipient of [...context.recipients]) { + const realRecipients = aliases.get(recipient); + if (realRecipients != null) { + console.info( + `Recipient alias resolved: ${recipient} => ${realRecipients}.`, + ); + context.recipients.delete(recipient); + context.recipients.add(realRecipients); + } + } + } +} diff --git a/deno/tools/deno.json b/deno/tools/deno.json new file mode 100644 index 0000000..355046a --- /dev/null +++ b/deno/tools/deno.json @@ -0,0 +1,8 @@ +{ + "version": "0.1.0", + "tasks": { + }, + "imports": { + "mustache": "npm:mustache@^4.2.0" + } +} diff --git a/deno/tools/generate-geosite-rules.ts b/deno/tools/generate-geosite-rules.ts new file mode 100644 index 0000000..bfa53ba --- /dev/null +++ b/deno/tools/generate-geosite-rules.ts @@ -0,0 +1,160 @@ +const PROXY_NAME = "node-select"; +const ATTR = "cn"; +const REPO_NAME = "domain-list-community"; +const URL = + "https://github.com/v2fly/domain-list-community/archive/refs/heads/master.zip"; +const SITES = [ + "github", + "google", + "youtube", + "twitter", + "facebook", + "discord", + "reddit", + "twitch", + "quora", + "telegram", + "imgur", + "stackexchange", + "onedrive", + "duckduckgo", + "wikimedia", + "gitbook", + "gitlab", + "creativecommons", + "archive", + "matrix", + "tor", + "python", + "ruby", + "rust", + "nodejs", + "npmjs", + "qt", + "docker", + "v2ray", + "homebrew", + "bootstrap", + "heroku", + "vercel", + "ieee", + "sci-hub", + "libgen", +]; + +const prefixes = ["include", "domain", "keyword", "full", "regexp"] as const; + +interface Rule { + kind: (typeof prefixes)[number]; + value: string; + attrs: string[]; +} + +type FileProvider = (name: string) => string; + +function extract(starts: string[], provider: FileProvider): Rule[] { + function parseLine(line: string): Rule { + let kind = prefixes.find((p) => line.startsWith(p + ":")); + if (kind != null) { + line = line.slice(line.indexOf(":") + 1); + } else { + kind = "domain"; + } + const segs = line.split("@"); + return { + kind, + value: segs[0].trim(), + attrs: [...segs.slice(1)].map((s) => s.trim()), + }; + } + + function parse(text: string): Rule[] { + return text + .replaceAll("\c\n", "\n") + .split("\n") + .map((l) => l.trim()) + .filter((l) => l.length !== 0 && !l.startsWith("#")) + .map((l) => parseLine(l)); + } + + const visited = [] as string[]; + const rules = [] as Rule[]; + + function add(name: string) { + const text = provider(name); + for (const rule of parse(text)) { + if (rule.kind === "include") { + if (visited.includes(rule.value)) { + console.warn(`circular refs found: ${name} includes ${rule.value}.`); + continue; + } else { + visited.push(rule.value); + add(rule.value); + } + } else { + rules.push(rule); + } + } + } + + for (const start of starts) { + add(start); + } + + return rules; +} + +function toNewFormat(rules: Rule[], attr: string): [string, string] { + function toLine(rule: Rule) { + const prefixMap = { + domain: "DOMAIN-SUFFIX", + full: "DOMAIN", + keyword: "DOMAIN-KEYWORD", + regexp: "DOMAIN-REGEX", + } as const; + if (rule.kind === "include") { + throw new Error("Include rule not parsed."); + } + return `${prefixMap[rule.kind]},${rule.value}`; + } + + function toLines(rules: Rule[]) { + return rules.map((r) => toLine(r)).join("\n"); + } + + const has: Rule[] = []; + const notHas: Rule[] = []; + rules.forEach((r) => (r.attrs.includes(attr) ? has.push(r) : notHas.push(r))); + + return [toLines(has), toLines(notHas)]; +} + +if (import.meta.main) { + const tmpDir = Deno.makeTempDirSync({ prefix: "geosite-rules-" }); + console.log("Work dir is ", tmpDir); + const zipFilePath = tmpDir + "/repo.zip"; + const res = await fetch(URL); + if (!res.ok) { + throw new Error("Failed to download repo."); + } + Deno.writeFileSync(zipFilePath, await res.bytes()); + const unzip = new Deno.Command("unzip", { + args: ["-q", zipFilePath], + cwd: tmpDir, + }); + if (!(await unzip.spawn().status).success) { + throw new Error("Failed to unzip"); + } + + const dataDir = tmpDir + "/" + REPO_NAME + "-master/data"; + const provider = (name: string) => + Deno.readTextFileSync(dataDir + "/" + name); + + const rules = extract(SITES, provider); + const [has, notHas] = toNewFormat(rules, ATTR); + const hasFile = tmpDir + "/has-rule"; + const notHasFile = tmpDir + "/not-has-rule"; + console.log("Write result to: " + hasFile + " , " + notHasFile); + Deno.writeTextFileSync(hasFile, has); + Deno.writeTextFileSync(notHasFile, notHas); +} diff --git a/deno/tools/manage-service.ts b/deno/tools/manage-service.ts new file mode 100644 index 0000000..148f55a --- /dev/null +++ b/deno/tools/manage-service.ts @@ -0,0 +1,42 @@ +import { join } from "@std/path"; +// @ts-types="npm:@types/yargs" +import yargs from "yargs"; + +import { TemplateDir } from "./template.ts"; + +if (import.meta.main) { + await yargs(Deno.args) + .scriptName("manage-service") + .option("project-dir", { + type: "string", + }) + .demandOption("project-dir") + .command({ + command: "gen-tmpl", + describe: "generate files for templates", + builder: (builder) => { + return builder + .option("dry-run", { + type: "boolean", + default: true, + }) + .strict(); + }, + handler: (argv) => { + const { projectDir, dryRun } = argv; + new TemplateDir( + join(projectDir, "services/templates"), + ).generateWithVariableFiles( + [ + join(projectDir, "data/config"), + join(projectDir, "services/config.template"), + ], + dryRun ? undefined : join(projectDir, "services/generated"), + ); + }, + }) + .demandCommand(1, "One command must be specified.") + .help() + .strict() + .parse(); +} diff --git a/deno/tools/manage-vm.ts b/deno/tools/manage-vm.ts new file mode 100644 index 0000000..bb985ce --- /dev/null +++ b/deno/tools/manage-vm.ts @@ -0,0 +1,144 @@ +import os from "node:os"; +import { join } from "@std/path"; +// @ts-types="npm:@types/yargs" +import yargs from "yargs"; + +type ArchAliasMap = { [name: string]: string[] }; +const arches = { + x86_64: ["x86_64", "amd64"], + i386: ["i386", "x86", "i686"], +} as const satisfies ArchAliasMap; +type Arch = keyof typeof arches; +type GeneralArch = (typeof arches)[Arch][number]; + +function normalizeArch(generalName: GeneralArch): Arch { + for (const [name, aliases] of Object.entries(arches as ArchAliasMap)) { + if (aliases.includes(generalName)) return name as Arch; + } + throw Error("Unknown architecture name."); +} + +interface GeneralVmSetup { + name?: string[]; + arch: GeneralArch; + disk: string; + sshForwardPort: number; + kvm?: boolean; +} + +interface VmSetup { + arch: Arch; + disk: string; + sshForwardPort: number; + kvm: boolean; +} + +const MY_VMS: GeneralVmSetup[] = [ + { + name: ["hurd", ...arches.i386.map((a) => `hurd-${a}`)], + arch: "i386", + disk: join(os.homedir(), "vms/hurd-i386.qcow2"), + sshForwardPort: 3222, + }, + { + name: [...arches.x86_64.map((a) => `hurd-${a}`)], + arch: "x86_64", + disk: join(os.homedir(), "vms/hurd-x86_64.qcow2"), + sshForwardPort: 3223, + }, +]; + +function normalizeVmSetup(generalSetup: GeneralVmSetup): VmSetup { + const { arch, disk, sshForwardPort, kvm } = generalSetup; + return { + arch: normalizeArch(arch), + disk, + sshForwardPort, + kvm: kvm ?? Deno.build.os === "linux", + }; +} + +function resolveVmSetup( + name: string, + vms: GeneralVmSetup[], +): VmSetup | undefined { + const setup = vms.find((vm) => vm.name?.includes(name)); + return setup == null ? undefined : normalizeVmSetup(setup); +} + +const qemuBinPrefix = "qemu-system" as const; + +const qemuBinSuffix = { + x86_64: "x86_64", + i386: "x86_64", +} as const; + +function getQemuBin(arch: Arch): string { + return `${qemuBinPrefix}-${qemuBinSuffix[arch]}`; +} + +function getLinuxHostArgs(kvm: boolean): string[] { + return kvm ? ["-enable-kvm"] : []; +} + +function getMachineArgs(arch: Arch): string[] { + const is64 = arch === "x86_64"; + const machineArgs = is64 ? ["-machine", "q35"] : []; + const memory = is64 ? 8 : 4; + return [...machineArgs, "-m", `${memory}G`]; +} + +function getNetworkArgs(sshForwardPort: number): string[] { + return ["-net", "nic", "-net", `user,hostfwd=tcp::${sshForwardPort}-:22`]; +} + +function getDisplayArgs(): string[] { + return ["-vga", "vmware"]; +} + +function getDiskArgs(disk: string): string[] { + return ["-drive", `cache=writeback,file=${disk}`]; +} + +function createQemuArgs(setup: VmSetup): string[] { + const { arch, disk, sshForwardPort } = setup; + return [ + getQemuBin(arch), + ...getLinuxHostArgs(setup.kvm), + ...getMachineArgs(arch), + ...getDisplayArgs(), + ...getNetworkArgs(sshForwardPort), + ...getDiskArgs(disk), + ]; +} + +if (import.meta.main) { + await yargs(Deno.args) + .scriptName("manage-vm") + .command({ + command: "gen <name>", + describe: "generate cli command to run the vm", + builder: (builder) => { + return builder + .positional("name", { + describe: "name of the vm to run", + type: "string", + }) + .demandOption("name") + .strict(); + }, + handler: (argv) => { + const vm = resolveVmSetup(argv.name, MY_VMS); + if (vm == null) { + console.error(`No vm called ${argv.name} is found.`); + Deno.exit(-1); + } + const cli = createQemuArgs(vm); + console.log(`${cli.join(" ")}`); + }, + }) + .demandCommand(1, "One command must be specified.") + .help() + .strict() + .parse(); +} diff --git a/deno/tools/template.ts b/deno/tools/template.ts new file mode 100644 index 0000000..1b67eb8 --- /dev/null +++ b/deno/tools/template.ts @@ -0,0 +1,124 @@ +import { dirname, join, relative } from "@std/path"; +import { copySync, existsSync, walkSync } from "@std/fs"; +import { parse } from "@std/dotenv"; +import { distinct } from "@std/collections"; +// @ts-types="npm:@types/mustache" +import Mustache from "mustache"; + +Mustache.tags = ["@@", "@@"]; +Mustache.escape = (value) => String(value); + +function getVariableKeys(original: string): string[] { + return distinct( + Mustache.parse(original) + .filter(function (v) { + return v[0] === "name"; + }) + .map(function (v) { + return v[1]; + }), + ); +} + +export function loadVariables(files: string[]): Record<string, string> { + const vars: Record<string, string> = {}; + for (const file of files) { + const text = Deno.readTextFileSync(file); + for (const [key, valueText] of Object.entries(parse(text))) { + getVariableKeys(valueText).forEach((name) => { + if (!(name in vars)) { + throw new Error( + `Variable ${name} is not defined yet, perhaps due to typos or wrong order.`, + ); + } + }); + vars[key] = Mustache.render(valueText, vars); + } + } + return vars; +} + +const TEMPLATE_FILE_EXT = ".template"; + +export class TemplateDir { + templates: { path: string; ext: string; text: string; vars: string[] }[] = []; + plains: { path: string }[] = []; + + constructor(public dir: string) { + console.log("Scanning template dir:"); + Array.from( + walkSync(dir, { includeDirs: false, followSymlinks: true }), + ).forEach(({ path }) => { + path = relative(this.dir, path); + if (path.endsWith(TEMPLATE_FILE_EXT)) { + console.log(` (template) ${path}`); + const text = Deno.readTextFileSync(join(dir, path)); + this.templates.push({ + path, + ext: TEMPLATE_FILE_EXT, + text, + vars: getVariableKeys(text), + }); + } else { + console.log(` (plain) ${path}`); + this.plains.push({ path }); + } + }); + console.log("Done scanning template dir."); + } + + allNeededVars() { + return distinct(this.templates.flatMap((t) => t.vars)); + } + + generate(vars: Record<string, string>, generatedDir?: string) { + console.log( + `Generating, template dir: ${this.dir}, generated dir: ${ + generatedDir ?? "[dry-run]" + }:`, + ); + + const undefinedVars = this.allNeededVars().filter((v) => !(v in vars)); + if (undefinedVars.length !== 0) { + throw new Error( + `Needed variables are not defined: ${undefinedVars.join(", ")}`, + ); + } + + if (generatedDir != null) { + if (existsSync(generatedDir)) { + console.log(` delete old generated dir ${generatedDir}`); + Deno.removeSync(generatedDir, { recursive: true }); + } + + for (const file of this.plains) { + const [source, destination] = [ + join(this.dir, file.path), + join(generatedDir, file.path), + ]; + console.log(` copy ${source} to ${destination} ...`); + Deno.mkdirSync(dirname(destination), { recursive: true }); + copySync(source, destination); + } + for (const file of this.templates) { + const [source, destination] = [ + join(this.dir, file.path), + join(generatedDir, file.path.slice(0, -file.ext.length)), + ]; + console.log(` generate ${source} to ${destination} ...`); + const rendered = Mustache.render(file.text, vars); + Deno.mkdirSync(dirname(destination), { recursive: true }); + Deno.writeTextFileSync(destination, rendered); + } + } + console.log(`Done generating.`); + } + + generateWithVariableFiles(varFiles: string[], generatedDir?: string) { + console.log("Scanning defined vars:"); + const vars = loadVariables(varFiles); + Object.keys(vars).forEach((name) => console.log(` ${name}`)); + console.log("Done scanning defined vars."); + this.generate(vars, generatedDir); + } +} |