aboutsummaryrefslogtreecommitdiff
path: root/deno
diff options
context:
space:
mode:
Diffstat (limited to 'deno')
-rw-r--r--deno/base/config.ts6
-rw-r--r--deno/base/deno.json2
-rw-r--r--deno/base/lib.ts24
-rw-r--r--deno/deno.json5
-rw-r--r--deno/deno.lock7
-rw-r--r--deno/mail-relay/aws/mail.ts59
-rw-r--r--deno/mail/app.ts (renamed from deno/mail-relay/app.ts)8
-rw-r--r--deno/mail/aws/app.ts (renamed from deno/mail-relay/aws/app.ts)78
-rw-r--r--deno/mail/aws/deliver.ts (renamed from deno/mail-relay/aws/deliver.ts)12
-rw-r--r--deno/mail/aws/fetch.ts (renamed from deno/mail-relay/aws/fetch.ts)43
-rw-r--r--deno/mail/db.test.ts (renamed from deno/mail-relay/db.test.ts)8
-rw-r--r--deno/mail/db.ts (renamed from deno/mail-relay/db.ts)36
-rw-r--r--deno/mail/deno.json (renamed from deno/mail-relay/deno.json)2
-rw-r--r--deno/mail/dovecot.ts (renamed from deno/mail-relay/dovecot.ts)14
-rw-r--r--deno/mail/dumb-smtp-server.ts (renamed from deno/mail-relay/dumb-smtp-server.ts)4
-rw-r--r--deno/mail/mail-parsing.ts (renamed from deno/mail-relay/mail-parsing.ts)0
-rw-r--r--deno/mail/mail.test.ts (renamed from deno/mail-relay/mail.test.ts)0
-rw-r--r--deno/mail/mail.ts (renamed from deno/mail-relay/mail.ts)80
-rw-r--r--deno/tools/geosite.ts (renamed from deno/tools/generate-geosite-rules.ts)7
-rw-r--r--deno/tools/main.ts14
-rw-r--r--deno/tools/manage-service.ts42
-rw-r--r--deno/tools/manage-vm.ts144
-rw-r--r--deno/tools/service.ts180
-rw-r--r--deno/tools/template.ts124
-rw-r--r--deno/tools/vm.ts225
-rw-r--r--deno/tools/yargs.ts12
26 files changed, 647 insertions, 489 deletions
diff --git a/deno/base/config.ts b/deno/base/config.ts
index a5f5d86..96cc869 100644
--- a/deno/base/config.ts
+++ b/deno/base/config.ts
@@ -1,4 +1,4 @@
-import { camelCaseToKebabCase } from "./lib.ts";
+import { StringUtils } from "./lib.ts";
export interface ConfigDefinitionItem {
readonly description: string;
@@ -29,7 +29,9 @@ export class ConfigProvider<K extends string> {
for (const [key, def] of Object.entries(definition as ConfigDefinition)) {
map[key] = {
...def,
- env: `${this.#prefix}-${camelCaseToKebabCase(key as string)}`
+ env: `${this.#prefix}-${
+ StringUtils.camelCaseToKebabCase(key as string)
+ }`
.replaceAll("-", "_")
.toUpperCase(),
};
diff --git a/deno/base/deno.json b/deno/base/deno.json
index 52baaa5..582f0f6 100644
--- a/deno/base/deno.json
+++ b/deno/base/deno.json
@@ -4,6 +4,6 @@
"exports": {
".": "./lib.ts",
"./config": "./config.ts",
- "./cron": "./cron.ts",
+ "./cron": "./cron.ts"
}
}
diff --git a/deno/base/lib.ts b/deno/base/lib.ts
index a5e4a6a..af75115 100644
--- a/deno/base/lib.ts
+++ b/deno/base/lib.ts
@@ -1,10 +1,30 @@
-export function camelCaseToKebabCase(str: string): string {
+function camelCaseToKebabCase(str: string): string {
return str.replace(/[A-Z]/g, (m) => "-" + m.toLowerCase());
}
-export function toFileNameString(date: Date, dateOnly?: boolean): string {
+function prependNonEmpty<T>(
+ object: T | null | undefined,
+ prefix: string = " ",
+): string {
+ if (object == null) return "";
+ const string = typeof object === "string" ? object : String(object);
+ return string.length === 0 ? "" : prefix + string;
+}
+
+export const StringUtils = Object.freeze({
+ camelCaseToKebabCase,
+ prependNonEmpty,
+});
+
+function toFileNameString(date: Date, dateOnly?: boolean): string {
const str = date.toISOString();
return dateOnly === true
? str.slice(0, str.indexOf("T"))
: str.replaceAll(/:|\./g, "-");
}
+
+export const DateUtils = Object.freeze(
+ {
+ toFileNameString,
+ } as const,
+);
diff --git a/deno/deno.json b/deno/deno.json
index 53cdf7a..286451e 100644
--- a/deno/deno.json
+++ b/deno/deno.json
@@ -1,7 +1,7 @@
{
- "workspace": ["./base", "./mail-relay", "./tools"],
+ "workspace": ["./base", "./mail", "./tools"],
"tasks": {
- "compile:mail-relay": "deno task --cwd=mail-relay compile"
+ "compile:mail": "deno task --cwd=mail compile"
},
"imports": {
"@std/collections": "jsr:@std/collections@^1.1.1",
@@ -11,7 +11,6 @@
"@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
index 871a9ae..bdc8c3f 100644
--- a/deno/deno.lock
+++ b/deno/deno.lock
@@ -10,7 +10,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",
@@ -88,9 +87,6 @@
"@std/data-structures@1.0.8": {
"integrity": "2fb7219247e044c8fcd51341788547575653c82ae2c759ff209e0263ba7d9b66"
},
- "@std/dotenv@0.225.5": {
- "integrity": "9ce6f9d0ec3311f74a32535aa1b8c62ed88b1ab91b7f0815797d77a6f60c922f"
- },
"@std/encoding@1.0.10": {
"integrity": "8783c6384a2d13abd5e9e87a7ae0520a30e9f56aeeaa3bdf910a3eaaf5c811a1"
},
@@ -1300,7 +1296,6 @@
"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",
@@ -1311,7 +1306,7 @@
"npm:yargs@18"
],
"members": {
- "mail-relay": {
+ "mail": {
"dependencies": [
"jsr:@db/sqlite@0.12",
"npm:@aws-sdk/client-s3@^3.821.0",
diff --git a/deno/mail-relay/aws/mail.ts b/deno/mail-relay/aws/mail.ts
deleted file mode 100644
index 26f3ea0..0000000
--- a/deno/mail-relay/aws/mail.ts
+++ /dev/null
@@ -1,59 +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> {
- const addresses = context.mail.simpleFindAllAddresses();
- for (const address of addresses) {
- const awsMessageId = await this.#lookup(address);
- if (awsMessageId != null && awsMessageId.length !== 0) {
- console.info(
- context.logTag,
- `Rewrite address-line string in mail: ${address} => ${awsMessageId}.`,
- );
- context.mail.raw = context.mail.raw.replaceAll(address, awsMessageId);
- }
- }
- }
-}
-
-export class AwsMailMessageIdSaveHook implements MailDeliverHook {
- readonly #record;
-
- constructor(
- record: (
- original: string,
- aws: string,
- context: MailDeliverContext,
- ) => Promise<void>,
- ) {
- this.#record = record;
- }
-
- async callback(context: MailDeliverContext): Promise<void> {
- const { messageId } = context.mail.parsed;
- if (messageId == null) {
- console.warn(
- context.logTag,
- "Original mail doesn't have message id, skip saving message id map.",
- );
- return;
- }
- if (context.result.awsMessageId != null) {
- console.info(
- context.logTag,
- `Save message id map: ${messageId} => ${context.result.awsMessageId}.`,
- );
- context.mail.raw = context.mail.raw.replaceAll(
- messageId,
- context.result.awsMessageId,
- );
- await this.#record(messageId, context.result.awsMessageId, context);
- }
- }
-}
diff --git a/deno/mail-relay/app.ts b/deno/mail/app.ts
index af328da..2a8c78a 100644
--- a/deno/mail-relay/app.ts
+++ b/deno/mail/app.ts
@@ -39,23 +39,23 @@ export function createHono(outbound: MailDeliverer, inbound: MailDeliverer) {
hono.onError((err, c) => {
console.error("Hono handler threw an uncaught error.", err);
- return c.json({ msg: "Server error, check its log." }, 500);
+ return c.json({ message: "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);
+ return context.json({ message: "Can't send an empty mail." }, 400);
} else {
const result = await outbound.deliverRaw(body);
return context.json({
- awsMessageId: result.awsMessageId,
+ newMessageId: result.newMessageId,
});
}
});
hono.post("/receive/raw", async (context) => {
await inbound.deliverRaw(await context.req.text());
- return context.json({ msg: "Done!" });
+ return context.json({ message: "Done!" });
});
return hono;
diff --git a/deno/mail-relay/aws/app.ts b/deno/mail/aws/app.ts
index c8a90c8..7e16488 100644
--- a/deno/mail-relay/aws/app.ts
+++ b/deno/mail/aws/app.ts
@@ -10,15 +10,12 @@ 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";
import { DovecotMailDeliverer } from "../dovecot.ts";
+import { MailDeliverer } from "../mail.ts";
+import { MessageIdRewriteHook, MessageIdSaveHook } from "../mail.ts";
+import { AwsMailDeliverer } from "./deliver.ts";
+import { AwsMailFetcher, LiveMailNotFoundError } from "./fetch.ts";
const PREFIX = "crupest-mail-server";
const CONFIG_DEFINITIONS = {
@@ -104,12 +101,12 @@ function createOutbound(
) {
const deliverer = new AwsMailDeliverer(awsOptions);
deliverer.preHooks.push(
- new AwsMailMessageIdRewriteHook(db.messageIdToAws.bind(db)),
+ new MessageIdRewriteHook(db.messageIdToNew.bind(db)),
);
deliverer.postHooks.push(
- new AwsMailMessageIdSaveHook(
- async (original, aws, context) => {
- await db.addMessageIdMap({ message_id: original, aws_message_id: aws });
+ new MessageIdSaveHook(
+ async (original, new_message_id, context) => {
+ await db.addMessageIdMap({ message_id: original, new_message_id });
void local?.saveNewSent(context.logTag, context.mail, original);
},
),
@@ -122,15 +119,18 @@ function setupAwsHono(
options: {
path: string;
auth: string;
- callback: (s3Key: string, recipients?: string[]) => Promise<void>;
+ 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({ msg: "Bad auth!" }, 403);
+ return ctx.json({ message: "Bad auth!" }, 403);
}
await next();
},
@@ -142,19 +142,32 @@ function setupAwsHono(
}),
),
async (ctx) => {
+ const { fetcher, deliverer } = options;
const { key, recipients } = ctx.req.valid("json");
- await options.callback(key, recipients);
- return ctx.json({ msg: "Done!" });
+ 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, consumer: AwsS3MailConsumer) {
+function createCron(fetcher: AwsMailFetcher, deliverer: MailDeliverer) {
return new CronTask({
name: "live-mail-recycler",
interval: 6 * 3600 * 1000,
callback: () => {
- return fetcher.recycleLiveMails(consumer);
+ return fetcher.recycleLiveMails(deliverer);
},
startNow: true,
});
@@ -191,10 +204,8 @@ function createAwsRecycleOnlyServices() {
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 };
+ return { ...services, inbound };
}
function createAwsServices() {
@@ -214,25 +225,22 @@ function createServerServices() {
const smtp = createSmtp(outbound);
const hono = createHono(outbound, inbound);
- let counter = 1;
setupAwsHono(hono, {
path: config.get("awsInboundPath"),
auth: config.get("awsInboundKey"),
- callback: (s3Key, recipients) => {
- return fetcher.consumeS3Mail(
- `[inbound ${counter++}]`,
- s3Key,
- (rawMail, _) =>
- inbound.deliver({ mail: new Mail(rawMail), recipients }).then(),
- );
- },
+ fetcher,
+ deliverer: inbound,
});
return { ...services, smtp, hono };
}
-function serve(cron: boolean = false) {
- const { config, fetcher, recycler, smtp, hono } = createServerServices();
+async function serve(cron: boolean = false) {
+ const { config, fetcher, inbound, smtp, dbService, hono } =
+ createServerServices();
+
+ await dbService.migrate();
+
smtp.serve({
hostname: config.get("smtpHost"),
port: config.getInt("smtpPort"),
@@ -246,7 +254,7 @@ function serve(cron: boolean = false) {
);
if (cron) {
- createCron(fetcher, recycler);
+ createCron(fetcher, inbound);
}
}
@@ -260,13 +268,13 @@ async function listLives() {
}
async function recycleLives() {
- const { fetcher, recycler } = createAwsRecycleOnlyServices();
- await fetcher.recycleLiveMails(recycler);
+ const { fetcher, inbound } = createAwsRecycleOnlyServices();
+ await fetcher.recycleLiveMails(inbound);
}
if (import.meta.main) {
await yargs(Deno.args)
- .scriptName("mail-relay")
+ .scriptName("mail")
.command({
command: "sendmail",
describe: "send mail via this server's endpoint",
diff --git a/deno/mail-relay/aws/deliver.ts b/deno/mail/aws/deliver.ts
index 0195369..37a871d 100644
--- a/deno/mail-relay/aws/deliver.ts
+++ b/deno/mail/aws/deliver.ts
@@ -6,12 +6,6 @@ import {
import { Mail, MailDeliverContext, MailDeliverer } from "../mail.ts";
-declare module "../mail.ts" {
- interface MailDeliverResult {
- awsMessageId?: string;
- }
-}
-
export class AwsMailDeliverer extends MailDeliverer {
readonly name = "aws";
readonly #aws;
@@ -42,12 +36,12 @@ export class AwsMailDeliverer extends MailDeliverer {
"AWS send-email returned null message id.",
);
} else {
- context.result.awsMessageId =
+ context.result.newMessageId =
`${res.MessageId}@${this.#aws.region}.amazonses.com`;
}
- context.result.smtpMessage =
- `AWS Message ID: ${context.result.awsMessageId}`;
+ context.result.messageForSmtp =
+ `AWS Message ID: ${context.result.newMessageId}`;
context.result.recipients.set("*", {
kind: "success",
message: `Succeeded to call aws send-email api.`,
diff --git a/deno/mail-relay/aws/fetch.ts b/deno/mail/aws/fetch.ts
index da8609f..2154972 100644
--- a/deno/mail-relay/aws/fetch.ts
+++ b/deno/mail/aws/fetch.ts
@@ -3,14 +3,17 @@ import {
DeleteObjectCommand,
GetObjectCommand,
ListObjectsV2Command,
- NoSuchBucket,
+ NoSuchKey,
S3Client,
S3ClientConfig,
} from "@aws-sdk/client-s3";
-import { toFileNameString } from "@crupest/base";
+import { DateUtils } from "@crupest/base";
import { Mail } from "../mail.ts";
+import { MailDeliverer } from "../mail.ts";
+
+export class LiveMailNotFoundError extends Error {}
async function s3MoveObject(
client: S3Client,
@@ -34,11 +37,6 @@ async function s3MoveObject(
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/";
@@ -76,12 +74,13 @@ export class AwsMailFetcher {
return result;
}
- async consumeS3Mail(
+ async deliverLiveMail(
logTag: string,
s3Key: string,
- consumer: AwsS3MailConsumer,
+ deliverer: MailDeliverer,
+ recipients?: string[],
) {
- console.info(logTag, `Fetching s3 mail ${s3Key}...`);
+ console.info(logTag, `Fetching live mail ${s3Key}...`);
const mailPath = `${this.#livePrefix}${s3Key}`;
const command = new GetObjectCommand({
Bucket: this.#bucket,
@@ -97,38 +96,40 @@ export class AwsMailFetcher {
}
rawMail = await res.Body.transformToString();
} catch (cause) {
- if (cause instanceof NoSuchBucket) {
- console.error(`S3 mail key ${s3Key} not found. Perhaps already consumed?`)
- return;
+ if (cause instanceof NoSuchKey) {
+ const message =
+ `Live mail ${s3Key} is not found. Perhaps already delivered?`;
+ console.error(message, cause);
+ throw new LiveMailNotFoundError(message);
}
throw cause;
}
- console.info(logTag, `Calling consumer...`);
- await consumer(rawMail, s3Key);
+ const mail = new Mail(rawMail);
+ await deliverer.deliver({ mail, recipients });
const { date } = new Mail(rawMail).parsed;
const dateString = date != null
- ? toFileNameString(date, true)
+ ? DateUtils.toFileNameString(date, true)
: "invalid-date";
const newPath = `${this.#archivePrefix}${dateString}/${s3Key}`;
- console.info(logTag, `Archiving s3 mail ${s3Key} to ${newPath}...`);
+ console.info(logTag, `Archiving live mail ${s3Key} to ${newPath}...`);
await s3MoveObject(this.#s3, this.#bucket, mailPath, newPath);
- console.info(logTag, `Done consuming s3 mail ${s3Key}.`);
+ console.info(logTag, `Done deliver live mail ${s3Key}.`);
}
- async recycleLiveMails(consumer: AwsS3MailConsumer) {
+ async recycleLiveMails(deliverer: MailDeliverer) {
console.info("Begin to recycle live mails...");
const mails = await this.listLiveMails();
console.info(`Found ${mails.length} live mails`);
let counter = 1;
for (const s3Key of mails) {
- await this.consumeS3Mail(
+ await this.deliverLiveMail(
`[${counter++}/${mails.length}]`,
s3Key,
- consumer,
+ deliverer,
);
}
}
diff --git a/deno/mail-relay/db.test.ts b/deno/mail/db.test.ts
index 60035c4..8a9ad27 100644
--- a/deno/mail-relay/db.test.ts
+++ b/deno/mail/db.test.ts
@@ -6,17 +6,17 @@ 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",
+ new_message_id: "mock-new-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.messageIdToNew(mockRow.message_id)).toBe(
+ mockRow.new_message_id,
);
- expect(await db.messageIdFromAws(mockRow.aws_message_id)).toBe(
+ expect(await db.messageIdFromNew(mockRow.new_message_id)).toBe(
mockRow.message_id,
);
});
diff --git a/deno/mail-relay/db.ts b/deno/mail/db.ts
index 062700b..e41f762 100644
--- a/deno/mail-relay/db.ts
+++ b/deno/mail/db.ts
@@ -53,14 +53,14 @@ class SqliteDatabaseAdapter implements SqliteDatabase {
export class DbError extends Error {}
-interface AwsMessageIdMapTable {
+interface MessageIdMapTable {
id: Generated<number>;
message_id: string;
- aws_message_id: string;
+ new_message_id: string;
}
interface Database {
- aws_message_id_map: AwsMessageIdMapTable;
+ message_id_map: MessageIdMapTable;
}
const migrations: Record<string, Migration> = {
@@ -68,16 +68,16 @@ const migrations: Record<string, Migration> = {
// deno-lint-ignore no-explicit-any
async up(db: Kysely<any>): Promise<void> {
await db.schema
- .createTable("aws_message_id_map")
+ .createTable("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())
+ .addColumn("new_message_id", "text", (col) => col.notNull().unique())
.execute();
- for (const column of ["message_id", "aws_message_id"]) {
+ for (const column of ["message_id", "new_message_id"]) {
await db.schema
- .createIndex(`aws_message_id_map_${column}`)
- .on("aws_message_id_map")
+ .createIndex(`message_id_map_${column}`)
+ .on("message_id_map")
.column(column)
.execute();
}
@@ -85,7 +85,7 @@ const migrations: Record<string, Migration> = {
// deno-lint-ignore no-explicit-any
async down(db: Kysely<any>): Promise<void> {
- await db.schema.dropTable("aws_message_id_map").execute();
+ await db.schema.dropTable("message_id_map").execute();
},
},
};
@@ -117,28 +117,28 @@ export class DbService {
}
async addMessageIdMap(
- mail: Insertable<AwsMessageIdMapTable>,
+ mail: Insertable<MessageIdMapTable>,
): Promise<number> {
const inserted = await this.#kysely
- .insertInto("aws_message_id_map")
+ .insertInto("message_id_map")
.values(mail)
.executeTakeFirstOrThrow();
return Number(inserted.insertId!);
}
- async messageIdToAws(messageId: string): Promise<string | null> {
+ async messageIdToNew(messageId: string): Promise<string | null> {
const row = await this.#kysely
- .selectFrom("aws_message_id_map")
+ .selectFrom("message_id_map")
.where("message_id", "=", messageId)
- .select("aws_message_id")
+ .select("new_message_id")
.executeTakeFirst();
- return row?.aws_message_id ?? null;
+ return row?.new_message_id ?? null;
}
- async messageIdFromAws(awsMessageId: string): Promise<string | null> {
+ async messageIdFromNew(newMessageId: string): Promise<string | null> {
const row = await this.#kysely
- .selectFrom("aws_message_id_map")
- .where("aws_message_id", "=", awsMessageId)
+ .selectFrom("message_id_map")
+ .where("new_message_id", "=", newMessageId)
.select("message_id")
.executeTakeFirst();
return row?.message_id ?? null;
diff --git a/deno/mail-relay/deno.json b/deno/mail/deno.json
index 9105747..86a8999 100644
--- a/deno/mail-relay/deno.json
+++ b/deno/mail/deno.json
@@ -2,7 +2,7 @@
"version": "0.1.0",
"tasks": {
"run": "deno run -A aws/app.ts",
- "compile": "deno compile -o out/crupest-relay -A aws/app.ts"
+ "compile": "deno compile -o out/crupest-mail -A aws/app.ts"
},
"imports": {
"@aws-sdk/client-s3": "npm:@aws-sdk/client-s3@^3.821.0",
diff --git a/deno/mail-relay/dovecot.ts b/deno/mail/dovecot.ts
index 4fe2f41..c0d56a2 100644
--- a/deno/mail-relay/dovecot.ts
+++ b/deno/mail/dovecot.ts
@@ -17,6 +17,7 @@ async function runCommand(
logTag: string;
args: string[];
stdin?: Uint8Array;
+ suppressStartLog?: boolean;
suppressResultLog?: boolean;
errorCodeMessageMap?: Map<number, string>;
},
@@ -24,7 +25,9 @@ async function runCommand(
const { logTag, args, stdin, suppressResultLog, errorCodeMessageMap } =
options;
- console.info(logTag, `Run external command ${bin} ${args.join(" ")}`);
+ if (options.suppressResultLog !== true) {
+ console.info(logTag, `Run external command ${bin} ${args.join(" ")}`);
+ }
try {
// Create and spawn process.
@@ -129,10 +132,13 @@ export class DovecotMailDeliverer extends MailDeliverer {
user: string,
mailbox: string,
messageId: string,
+ noLog?: boolean,
): Promise<void> {
await runCommand(this.#doveadmPath, {
logTag,
args: ["expunge", "-u", user, ...this.#queryArgs(mailbox, messageId)],
+ suppressStartLog: noLog,
+ suppressResultLog: noLog,
});
}
@@ -202,11 +208,11 @@ export class DovecotMailDeliverer extends MailDeliverer {
console.info(
logTag,
- "Schedule deletion of old mails at 15,30,60 seconds later.",
+ "Schedule deletion of old mails (no logging) at 5,15,30,60 seconds later.",
);
- [15, 30, 60].forEach((seconds) =>
+ [5, 15, 30, 60].forEach((seconds) =>
setTimeout(() => {
- void this.#deleteMail(logTag, from, "Sent", messageIdToDelete);
+ void this.#deleteMail(logTag, from, "Sent", messageIdToDelete, true);
}, 1000 * seconds)
);
}
diff --git a/deno/mail-relay/dumb-smtp-server.ts b/deno/mail/dumb-smtp-server.ts
index 70d5ec0..c3ebf5d 100644
--- a/deno/mail-relay/dumb-smtp-server.ts
+++ b/deno/mail/dumb-smtp-server.ts
@@ -91,8 +91,8 @@ export class DumbSmtpServer {
if (line === ".") {
try {
console.info(logTag, "Mail data received, begin to relay...");
- const { smtpMessage } = await this.#deliverer.deliverRaw(rawMail);
- await send(`250 2.6.0 ${smtpMessage}`);
+ const result = await this.#deliverer.deliverRaw(rawMail);
+ await send(`250 2.6.0 ${result.generateMessageForSmtp()}`);
rawMail = null;
} catch (err) {
console.error(logTag, "Relay failed.", err);
diff --git a/deno/mail-relay/mail-parsing.ts b/deno/mail/mail-parsing.ts
index 8e9697d..8e9697d 100644
--- a/deno/mail-relay/mail-parsing.ts
+++ b/deno/mail/mail-parsing.ts
diff --git a/deno/mail-relay/mail.test.ts b/deno/mail/mail.test.ts
index a8204be..a8204be 100644
--- a/deno/mail-relay/mail.test.ts
+++ b/deno/mail/mail.test.ts
diff --git a/deno/mail-relay/mail.ts b/deno/mail/mail.ts
index 94944b0..b88ce2b 100644
--- a/deno/mail-relay/mail.ts
+++ b/deno/mail/mail.ts
@@ -1,5 +1,8 @@
import { encodeBase64 } from "@std/encoding/base64";
import { parse } from "@std/csv/parse";
+
+import { StringUtils } from "@crupest/base";
+
import { simpleParseMail } from "./mail-parsing.ts";
export class Mail {
@@ -47,7 +50,9 @@ export interface MailDeliverRecipientResult {
export class MailDeliverResult {
message?: string;
- smtpMessage?: string;
+ messageForSmtp?: string;
+ newMessageId?: string;
+
recipients = new Map<string, MailDeliverRecipientResult>();
constructor(public mail: Mail) {}
@@ -58,15 +63,22 @@ export class MailDeliverResult {
generateLogMessage(prefix: string) {
const lines = [];
if (this.message != null) lines.push(`${prefix} message: ${this.message}`);
- if (this.smtpMessage != null) {
- lines.push(`${prefix} smtpMessage: ${this.smtpMessage}`);
+ if (this.messageForSmtp != null) {
+ lines.push(`${prefix} smtpMessage: ${this.messageForSmtp}`);
}
for (const [name, result] of this.recipients.entries()) {
- const { kind, message, cause } = result;
- lines.push(`${prefix} (${name}): ${kind} ${message} ${cause}`);
+ const { kind, message } = result;
+ lines.push(`${prefix} (${name}): ${kind} ${message}`);
}
return lines.join("\n");
}
+
+ generateMessageForSmtp(): string {
+ if (this.messageForSmtp != null) return this.messageForSmtp;
+ return `2.0.0 OK${
+ StringUtils.prependNonEmpty(this.newMessageId)
+ } Message accepted for delivery`;
+ }
}
export class MailDeliverContext {
@@ -232,3 +244,61 @@ export class AliasRecipientMailHook implements MailDeliverHook {
}
}
}
+
+export class MessageIdRewriteHook implements MailDeliverHook {
+ readonly #lookup;
+
+ constructor(lookup: (origin: string) => Promise<string | null>) {
+ this.#lookup = lookup;
+ }
+
+ async callback(context: MailDeliverContext): Promise<void> {
+ const addresses = context.mail.simpleFindAllAddresses();
+ for (const address of addresses) {
+ const newMessageId = await this.#lookup(address);
+ if (newMessageId != null && newMessageId.length !== 0) {
+ console.info(
+ context.logTag,
+ `Rewrite address-line string in mail: ${address} => ${newMessageId}.`,
+ );
+ context.mail.raw = context.mail.raw.replaceAll(address, newMessageId);
+ }
+ }
+ }
+}
+
+export class MessageIdSaveHook implements MailDeliverHook {
+ readonly #record;
+
+ constructor(
+ record: (
+ original: string,
+ newMessageId: string,
+ context: MailDeliverContext,
+ ) => Promise<void>,
+ ) {
+ this.#record = record;
+ }
+
+ async callback(context: MailDeliverContext): Promise<void> {
+ const { messageId } = context.mail.parsed;
+ if (messageId == null) {
+ console.warn(
+ context.logTag,
+ "Original mail doesn't have message id, skip saving message id map.",
+ );
+ return;
+ }
+ if (context.result.newMessageId != null) {
+ console.info(
+ context.logTag,
+ `Save message id map: ${messageId} => ${context.result.newMessageId}.`,
+ );
+ context.mail.raw = context.mail.raw.replaceAll(
+ messageId,
+ context.result.newMessageId,
+ );
+ await this.#record(messageId, context.result.newMessageId, context);
+ }
+ }
+}
diff --git a/deno/tools/generate-geosite-rules.ts b/deno/tools/geosite.ts
index bfa53ba..3aabec2 100644
--- a/deno/tools/generate-geosite-rules.ts
+++ b/deno/tools/geosite.ts
@@ -1,4 +1,3 @@
-const PROXY_NAME = "node-select";
const ATTR = "cn";
const REPO_NAME = "domain-list-community";
const URL =
@@ -152,8 +151,10 @@ if (import.meta.main) {
const rules = extract(SITES, provider);
const [has, notHas] = toNewFormat(rules, ATTR);
- const hasFile = tmpDir + "/has-rule";
- const notHasFile = tmpDir + "/not-has-rule";
+ const resultDir = tmpDir + "/result";
+ Deno.mkdirSync(resultDir);
+ const hasFile = resultDir + "/has-rule";
+ const notHasFile = resultDir + "/not-has-rule";
console.log("Write result to: " + hasFile + " , " + notHasFile);
Deno.writeTextFileSync(hasFile, has);
Deno.writeTextFileSync(notHasFile, notHas);
diff --git a/deno/tools/main.ts b/deno/tools/main.ts
new file mode 100644
index 0000000..897350c
--- /dev/null
+++ b/deno/tools/main.ts
@@ -0,0 +1,14 @@
+import yargs, { DEMAND_COMMAND_MESSAGE } from "./yargs.ts";
+import vm from "./vm.ts";
+import service from "./service.ts";
+
+if (import.meta.main) {
+ await yargs(Deno.args)
+ .scriptName("crupest")
+ .command(vm)
+ .command(service)
+ .demandCommand(1, DEMAND_COMMAND_MESSAGE)
+ .help()
+ .strict()
+ .parse();
+}
diff --git a/deno/tools/manage-service.ts b/deno/tools/manage-service.ts
deleted file mode 100644
index 148f55a..0000000
--- a/deno/tools/manage-service.ts
+++ /dev/null
@@ -1,42 +0,0 @@
-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
deleted file mode 100644
index bb985ce..0000000
--- a/deno/tools/manage-vm.ts
+++ /dev/null
@@ -1,144 +0,0 @@
-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/service.ts b/deno/tools/service.ts
new file mode 100644
index 0000000..bd4d22c
--- /dev/null
+++ b/deno/tools/service.ts
@@ -0,0 +1,180 @@
+import { dirname, join, relative } from "@std/path";
+import { copySync, existsSync, walkSync } from "@std/fs";
+import { distinct } from "@std/collections";
+// @ts-types="npm:@types/mustache"
+import Mustache from "mustache";
+
+import { defineYargsModule, DEMAND_COMMAND_MESSAGE } from "./yargs.ts";
+
+const MUSTACHE_RENDER_OPTIONS: Mustache.RenderOptions = {
+ tags: ["@@", "@@"],
+ escape: (value: unknown) => String(value),
+};
+
+function mustacheParse(template: string) {
+ return Mustache.parse(template, MUSTACHE_RENDER_OPTIONS.tags);
+}
+
+function mustacheRender(template: string, view: Record<string, string>) {
+ return Mustache.render(template, view, {}, MUSTACHE_RENDER_OPTIONS);
+}
+
+function getVariableKeysOfTemplate(template: string): string[] {
+ return distinct(
+ mustacheParse(template)
+ .filter((v) => v[0] === "name")
+ .map((v) => v[1]),
+ );
+}
+
+function loadTemplatedConfigFiles(
+ files: string[],
+): Record<string, string> {
+ console.log("Scan config files ...");
+ const config: Record<string, string> = {};
+ for (const file of files) {
+ console.log(` from file ${file}`);
+ const text = Deno.readTextFileSync(file);
+ let lineNumber = 0;
+ for (const rawLine of text.split("\n")) {
+ lineNumber++;
+ const line = rawLine.trim();
+ if (line.length === 0) continue;
+ if (line.startsWith("#")) continue;
+ const equalSymbolIndex = line.indexOf("=");
+ if (equalSymbolIndex === -1) {
+ throw new Error(`Line ${lineNumber} of ${file} is invalid.`);
+ }
+ const [key, valueText] = [
+ line.slice(0, equalSymbolIndex).trim(),
+ line.slice(equalSymbolIndex + 1).trim(),
+ ];
+ console.log(` (${key in config ? "override" : "new"}) ${key}`);
+ getVariableKeysOfTemplate(valueText).forEach((name) => {
+ if (!(name in config)) {
+ throw new Error(
+ `Variable ${name} is not defined yet, perhaps due to typos or wrong order.`,
+ );
+ }
+ });
+ config[key] = mustacheRender(valueText, config);
+ }
+ }
+ return config;
+}
+
+const TEMPLATE_FILE_EXT = ".template";
+
+class TemplateDir {
+ templates: { path: string; ext: string; text: string; vars: string[] }[] = [];
+ plains: { path: string }[] = [];
+
+ constructor(public dir: string) {
+ console.log(`Scan template dir ${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: getVariableKeysOfTemplate(text),
+ });
+ } else {
+ console.log(` (plain) ${path}`);
+ this.plains.push({ path });
+ }
+ });
+ }
+
+ allNeededVars() {
+ return distinct(this.templates.flatMap((t) => t.vars));
+ }
+
+ generate(vars: Record<string, string>, generatedDir?: string) {
+ console.log(
+ `Generate to 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`);
+ 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 ${file.path}`);
+ Deno.mkdirSync(dirname(destination), { recursive: true });
+ copySync(source, destination);
+ }
+ for (const file of this.templates) {
+ const path = file.path.slice(0, -file.ext.length);
+ const destination = join(generatedDir, path);
+ console.log(` generate ${path}`);
+ const rendered = mustacheRender(file.text, vars);
+ Deno.mkdirSync(dirname(destination), { recursive: true });
+ Deno.writeTextFileSync(destination, rendered);
+ }
+ }
+ }
+}
+
+export default defineYargsModule({
+ command: "service",
+ aliases: ["sv"],
+ describe: "Manage services.",
+ builder: (builder) => {
+ return builder
+ .option("project-dir", {
+ type: "string",
+ })
+ .demandOption("project-dir")
+ .command({
+ command: "gen-tmpl",
+ describe: "Generate files from templates",
+ builder: (builder) => {
+ return builder
+ .option("dry-run", {
+ type: "boolean",
+ default: true,
+ })
+ .strict();
+ },
+ handler: (argv) => {
+ const { projectDir, dryRun } = argv;
+
+ const config = loadTemplatedConfigFiles(
+ [
+ join(projectDir, "data/config"),
+ join(projectDir, "services/config.template"),
+ ],
+ );
+
+ new TemplateDir(
+ join(projectDir, "services/templates"),
+ ).generate(
+ config,
+ dryRun ? undefined : join(projectDir, "services/generated"),
+ );
+ console.log("Done!");
+ },
+ })
+ .demandCommand(1, DEMAND_COMMAND_MESSAGE);
+ },
+ handler: () => {},
+});
diff --git a/deno/tools/template.ts b/deno/tools/template.ts
deleted file mode 100644
index 1b67eb8..0000000
--- a/deno/tools/template.ts
+++ /dev/null
@@ -1,124 +0,0 @@
-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);
- }
-}
diff --git a/deno/tools/vm.ts b/deno/tools/vm.ts
new file mode 100644
index 0000000..b54c0d4
--- /dev/null
+++ b/deno/tools/vm.ts
@@ -0,0 +1,225 @@
+import os from "node:os";
+import { join } from "@std/path";
+import { defineYargsModule, DEMAND_COMMAND_MESSAGE } from "./yargs.ts";
+
+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;
+ cpuNumber?: number;
+ memory?: number;
+ disk: string;
+ usbTablet?: boolean;
+ sshForwardPort?: number;
+ tpm?: boolean;
+ kvm?: boolean;
+}
+
+interface VmSetup {
+ arch: Arch;
+ cpuNumber: number;
+ memory: number;
+ disk: string;
+ usbTablet: boolean;
+ sshForwardPort?: number;
+ tpm: boolean;
+ kvm: boolean;
+}
+
+const VM_DIR = join(os.homedir(), "vms");
+
+function getDiskFilePath(name: string): string {
+ return join(VM_DIR, `${name}.qcow2`);
+}
+
+const MY_VMS: GeneralVmSetup[] = [
+ {
+ name: ["hurd", ...arches.i386.map((a) => `hurd-${a}`)],
+ arch: "i386",
+ disk: getDiskFilePath("hurd-i386"),
+ sshForwardPort: 3222,
+ },
+ {
+ name: [...arches.x86_64.map((a) => `hurd-${a}`)],
+ arch: "x86_64",
+ disk: getDiskFilePath("hurd-x86_64"),
+ sshForwardPort: 3223,
+ },
+ {
+ name: ["win"],
+ arch: "x86_64",
+ cpuNumber: 4,
+ memory: 16,
+ disk: getDiskFilePath("win"),
+ usbTablet: true,
+ tpm: true,
+ },
+];
+
+function normalizeVmSetup(generalSetup: GeneralVmSetup): VmSetup {
+ const { arch, cpuNumber, memory, disk, usbTablet, sshForwardPort, tpm, kvm } =
+ generalSetup;
+
+ const normalizedArch = normalizeArch(arch);
+ const is64 = normalizedArch === "x86_64";
+
+ return {
+ arch: normalizedArch,
+ disk,
+ cpuNumber: cpuNumber ?? 1,
+ memory: memory ?? (is64 ? 8 : 4),
+ usbTablet: usbTablet ?? false,
+ sshForwardPort,
+ tpm: tpm ?? false,
+ 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(vm: VmSetup): string[] {
+ const is64 = vm.arch === "x86_64";
+ const machineArgs = is64 ? ["-machine", "q35"] : [];
+ return [...machineArgs, "-smp", String(vm.cpuNumber), "-m", `${vm.memory}G`];
+}
+
+function getDeviceArgs(vm: VmSetup): string[] {
+ const { usbTablet } = vm;
+ return usbTablet ? ["-usb", "-device", "usb-tablet"] : [];
+}
+
+function getNetworkArgs(sshForwardPort?: number): string[] {
+ const args = ["-net", "nic"];
+ if (sshForwardPort != null) {
+ args.push("-net", `user,hostfwd=tcp::${sshForwardPort}-:22`);
+ }
+ return args;
+}
+
+function getDisplayArgs(): string[] {
+ return ["-vga", "vmware"];
+}
+
+function getDiskArgs(disk: string): string[] {
+ return ["-drive", `cache=writeback,file=${disk}`];
+}
+
+function getTpmControlSocketPath(): string {
+ return join(VM_DIR, "tpm2/swtpm-sock");
+}
+
+function getTpmArgs(tpm: boolean): string[] {
+ if (!tpm) return [];
+ return [
+ "-chardev",
+ `socket,id=chrtpm,path=${getTpmControlSocketPath()}`,
+ "-tpmdev",
+ "emulator,id=tpm0,chardev=chrtpm",
+ "-device",
+ "tpm-tis,tpmdev=tpm0",
+ ];
+}
+
+function getTpmPreCommand(): string[] {
+ return [
+ "swtpm",
+ "socket",
+ "--tpm2",
+ "--tpmstate",
+ `dir=${join(VM_DIR, "tpm2")}`,
+ "--ctrl",
+ `type=unixio,path=${getTpmControlSocketPath()}`,
+ ];
+}
+
+function createPreCommands(setup: VmSetup): string[][] {
+ const { tpm } = setup;
+ const result = [];
+ if (tpm) result.push(getTpmPreCommand());
+ return result;
+}
+
+function createQemuArgs(setup: VmSetup): string[] {
+ const { arch, disk, sshForwardPort, tpm } = setup;
+ return [
+ getQemuBin(arch),
+ ...getLinuxHostArgs(setup.kvm),
+ ...getMachineArgs(setup),
+ ...getDeviceArgs(setup),
+ ...getDisplayArgs(),
+ ...getNetworkArgs(sshForwardPort),
+ ...getDiskArgs(disk),
+ ...getTpmArgs(tpm),
+ ];
+}
+
+const gen = defineYargsModule({
+ 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 preCommands = createPreCommands(vm);
+ const cli = createQemuArgs(vm);
+ for (const command of preCommands) {
+ console.log(`${command.join(" ")} &`);
+ }
+ console.log(`${cli.join(" ")}`);
+ },
+});
+
+export default defineYargsModule({
+ command: "vm",
+ describe: "Manage (qemu) virtual machines.",
+ builder: (builder) => {
+ return builder.command(gen).demandCommand(1, DEMAND_COMMAND_MESSAGE);
+ },
+ handler: () => {},
+});
diff --git a/deno/tools/yargs.ts b/deno/tools/yargs.ts
new file mode 100644
index 0000000..eaa7803
--- /dev/null
+++ b/deno/tools/yargs.ts
@@ -0,0 +1,12 @@
+// @ts-types="npm:@types/yargs"
+export { default } from "yargs";
+export * from "yargs";
+
+import { CommandModule } from "yargs";
+export function defineYargsModule<T, U>(
+ module: CommandModule<T, U>,
+): CommandModule<T, U> {
+ return module;
+}
+
+export const DEMAND_COMMAND_MESSAGE = "No command is specified";