aboutsummaryrefslogtreecommitdiff
path: root/deno/mail-relay/aws/app.ts
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-06-30 14:25:02 +0800
committerYuqian Yang <crupest@crupest.life>2025-06-30 14:25:02 +0800
commite18d101cae1dfcef29abd102d2908d429f4688d5 (patch)
tree49c0b1d1c237c674fe603db23d2e174acdea6979 /deno/mail-relay/aws/app.ts
parent66e2d76b75ed04ae8a43baefdb970f4cb89c5925 (diff)
downloadcrupest-e18d101cae1dfcef29abd102d2908d429f4688d5.tar.gz
crupest-e18d101cae1dfcef29abd102d2908d429f4688d5.tar.bz2
crupest-e18d101cae1dfcef29abd102d2908d429f4688d5.zip
mail: revert removing.
Diffstat (limited to 'deno/mail-relay/aws/app.ts')
-rw-r--r--deno/mail-relay/aws/app.ts315
1 files changed, 0 insertions, 315 deletions
diff --git a/deno/mail-relay/aws/app.ts b/deno/mail-relay/aws/app.ts
deleted file mode 100644
index e3f1114..0000000
--- a/deno/mail-relay/aws/app.ts
+++ /dev/null
@@ -1,315 +0,0 @@
-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 { ConfigDefinition, ConfigProvider } from "@crupest/base/config";
-import { CronTask } from "@crupest/base/cron";
-
-import { DbService } from "../db.ts";
-import { createHono, createInbound, createSmtp, sendMail } from "../app.ts";
-import { DovecotMailDeliverer } from "../dovecot.ts";
-import { MailDeliverer } from "../mail.ts";
-import {
- AwsMailMessageIdRewriteHook,
- AwsMailMessageIdSaveHook,
-} from "./mail.ts";
-import { AwsMailDeliverer } from "./deliver.ts";
-import { AwsMailFetcher, LiveMailNotFoundError } from "./fetch.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",
- },
- doveadmPath: {
- description: "full path of doveadm executable",
- default: "/dovecot/bin/doveadm",
- },
- 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,
- local?: DovecotMailDeliverer,
-) {
- const deliverer = new AwsMailDeliverer(awsOptions);
- deliverer.preHooks.push(
- new AwsMailMessageIdRewriteHook(db.messageIdToAws.bind(db)),
- );
- deliverer.postHooks.push(
- new AwsMailMessageIdSaveHook(
- async (original, aws, context) => {
- await db.addMessageIdMap({ message_id: original, aws_message_id: aws });
- void local?.saveNewSent(context.logTag, context.mail, original);
- },
- ),
- );
- return deliverer;
-}
-
-function setupAwsHono(
- hono: Hono,
- options: {
- path: string;
- auth: string;
- fetcher: AwsMailFetcher;
- deliverer: MailDeliverer;
- },
-) {
- let counter = 1;
-
- hono.post(
- `/${options.path}`,
- async (ctx, next) => {
- const auth = ctx.req.header("Authorization");
- if (auth !== options.auth) {
- return ctx.json({ message: "Bad auth!" }, 403);
- }
- await next();
- },
- zValidator(
- "json",
- z.object({
- key: z.string(),
- recipients: z.optional(z.array(z.string())),
- }),
- ),
- async (ctx) => {
- const { fetcher, deliverer } = options;
- const { key, recipients } = ctx.req.valid("json");
- try {
- await fetcher.deliverLiveMail(
- `[inbound ${counter++}]`,
- key,
- deliverer,
- recipients,
- );
- } catch (e) {
- if (e instanceof LiveMailNotFoundError) {
- return ctx.json({ message: e.message });
- }
- throw e;
- }
- return ctx.json({ message: "Done!" });
- },
- );
-}
-
-function createCron(fetcher: AwsMailFetcher, deliverer: MailDeliverer) {
- return new CronTask({
- name: "live-mail-recycler",
- interval: 6 * 3600 * 1000,
- callback: () => {
- return fetcher.recycleLiveMails(deliverer);
- },
- startNow: true,
- });
-}
-
-function createBaseServices() {
- const config = new ConfigProvider(PREFIX, CONFIG_DEFINITIONS);
- Deno.mkdirSync(config.get("dataPath"), { recursive: true });
- return { config };
-}
-
-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 } = services;
-
- const inbound = createInbound({
- fallback: config.getList("inboundFallback"),
- ldaPath: config.get("ldaPath"),
- doveadmPath: config.get("doveadmPath"),
- aliasFile: join(config.get("dataPath"), "aliases.csv"),
- mailDomain: config.get("mailDomain"),
- });
-
- return { ...services, inbound };
-}
-
-function createAwsServices() {
- const services = createAwsRecycleOnlyServices();
- const { config, awsOptions, inbound } = services;
-
- const dbService = new DbService(join(config.get("dataPath"), "db.sqlite"));
- const outbound = createOutbound(awsOptions, dbService, inbound);
-
- 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"),
- fetcher,
- deliverer: inbound,
- });
-
- return { ...services, smtp, hono };
-}
-
-function serve(cron: boolean = false) {
- const { config, fetcher, inbound, 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, inbound);
- }
-}
-
-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, inbound } = createAwsRecycleOnlyServices();
- await fetcher.recycleLiveMails(inbound);
-}
-
-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();
-}