aboutsummaryrefslogtreecommitdiff
path: root/deno/mail-relay/aws
diff options
context:
space:
mode:
Diffstat (limited to 'deno/mail-relay/aws')
-rw-r--r--deno/mail-relay/aws/app.ts297
-rw-r--r--deno/mail-relay/aws/deliver.ts60
-rw-r--r--deno/mail-relay/aws/fetch.ts127
-rw-r--r--deno/mail-relay/aws/mail.ts49
4 files changed, 0 insertions, 533 deletions
diff --git a/deno/mail-relay/aws/app.ts b/deno/mail-relay/aws/app.ts
deleted file mode 100644
index cb275ae..0000000
--- a/deno/mail-relay/aws/app.ts
+++ /dev/null
@@ -1,297 +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 { 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
deleted file mode 100644
index 4dd4b3a..0000000
--- a/deno/mail-relay/aws/deliver.ts
+++ /dev/null
@@ -1,60 +0,0 @@
-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
deleted file mode 100644
index 9278e63..0000000
--- a/deno/mail-relay/aws/fetch.ts
+++ /dev/null
@@ -1,127 +0,0 @@
-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
deleted file mode 100644
index cc05d23..0000000
--- a/deno/mail-relay/aws/mail.ts
+++ /dev/null
@@ -1,49 +0,0 @@
-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.");
- }
-}