diff options
58 files changed, 392 insertions, 491 deletions
diff --git a/deno/base/config.ts b/deno/base/config.ts index 8fce1d8..a5f5d86 100644 --- a/deno/base/config.ts +++ b/deno/base/config.ts @@ -1,4 +1,4 @@ -import { camelCaseToKebabCase } from "./text.ts"; +import { camelCaseToKebabCase } from "./lib.ts"; export interface ConfigDefinitionItem { readonly description: string; diff --git a/deno/base/deno.json b/deno/base/deno.json index 2c2d550..dabc02a 100644 --- a/deno/base/deno.json +++ b/deno/base/deno.json @@ -2,10 +2,9 @@ "name": "@crupest/base", "version": "0.1.0", "exports": { + ".": "./lib.ts", "./config": "./config.ts", "./cron": "./cron.ts", - "./date": "./date.ts", - "./text": "./text.ts", "./log": "./log.ts" } } diff --git a/deno/base/date.ts b/deno/base/lib.ts index e65691e..a5e4a6a 100644 --- a/deno/base/date.ts +++ b/deno/base/lib.ts @@ -1,3 +1,7 @@ +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 diff --git a/deno/base/log.ts b/deno/base/log.ts index cc71dfa..940f569 100644 --- a/deno/base/log.ts +++ b/deno/base/log.ts @@ -1,66 +1,17 @@ import { join } from "@std/path"; -import { toFileNameString } from "./date.ts"; - -export type LogLevel = "error" | "warn" | "info"; - -export interface LogOptions { - level?: LogLevel; - cause?: unknown; -} +import { toFileNameString } from "./lib.ts"; export interface ExternalLogStream extends Disposable { stream: WritableStream; } -export class Logger { - #defaultLevel = "info" as const; - #externalLogDir?: string; - - get externalLogDir() { - return this.#externalLogDir; - } - - set externalLogDir(value: string | undefined) { - this.#externalLogDir = value; - if (value != null) { - Deno.mkdirSync(value, { - recursive: true, - }); - } - } - - write(message: string, options?: LogOptions): void { - const logFunction = console[options?.level ?? this.#defaultLevel]; - if (options?.cause != null) { - logFunction.call(console, message, options.cause); - } else { - logFunction.call(console, message); - } - } - - info(message: string) { - this.write(message, { level: "info" }); - } - - tagInfo(tag: string, message: string) { - this.info(tag + " " + message); - } - - warn(message: string) { - this.write(message, { level: "warn" }); - } +export class LogFileProvider { + #directory: string; - tagWarn(tag: string, message: string) { - this.warn(tag + " " + message); - } - - error(message: string, cause?: unknown) { - this.write(message, { level: "info", cause }); - } - - tagError(tag: string, message: string, cause?: unknown) { - this.error(tag + " " + message, cause); + constructor(directory: string) { + this.#directory = directory; + Deno.mkdirSync(directory, { recursive: true }); } async createExternalLogStream( @@ -72,12 +23,9 @@ export class Logger { if (name.includes("/")) { throw new Error(`External log stream's name (${name}) contains '/'.`); } - if (this.#externalLogDir == null) { - throw new Error("External log directory is not set."); - } const logPath = join( - this.#externalLogDir, + this.#directory, options?.noTime === true ? name : `${name}-${toFileNameString(new Date())}`, diff --git a/deno/base/text.ts b/deno/base/text.ts deleted file mode 100644 index f3e4020..0000000 --- a/deno/base/text.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function camelCaseToKebabCase(str: string): string { - return str.replace(/[A-Z]/g, (m) => "-" + m.toLowerCase()); -} diff --git a/deno/deno.json b/deno/deno.json index f4859d1..53cdf7a 100644 --- a/deno/deno.json +++ b/deno/deno.json @@ -1,7 +1,7 @@ { - "workspace": ["./base", "./mail-relay", "./tools" ], + "workspace": ["./base", "./mail-relay", "./tools"], "tasks": { - "compile:mail-relay": "deno task --cwd=mail-relay compile", + "compile:mail-relay": "deno task --cwd=mail-relay compile" }, "imports": { "@std/collections": "jsr:@std/collections@^1.1.1", @@ -13,7 +13,7 @@ "@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" + "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 0fc543b..871a9ae 100644 --- a/deno/deno.lock +++ b/deno/deno.lock @@ -1326,8 +1326,7 @@ }, "tools": { "dependencies": [ - "npm:mustache@^4.2.0", - "npm:yargs@18" + "npm:mustache@^4.2.0" ] } } diff --git a/deno/mail-relay/app.ts b/deno/mail-relay/app.ts index d96fa1d..eeffc12 100644 --- a/deno/mail-relay/app.ts +++ b/deno/mail-relay/app.ts @@ -1,7 +1,7 @@ import { Hono } from "hono"; import { logger as honoLogger } from "hono/logger"; -import { Logger } from "@crupest/base/log"; +import { LogFileProvider } from "@crupest/base/log"; import { AliasRecipientMailHook, @@ -13,7 +13,7 @@ import { DovecotMailDeliverer } from "./dovecot.ts"; import { DumbSmtpServer } from "./dumb-smtp-server.ts"; export function createInbound( - logger: Logger, + logFileProvider: LogFileProvider, { fallback, mailDomain, @@ -26,7 +26,7 @@ export function createInbound( ldaPath: string; }, ) { - const deliverer = new DovecotMailDeliverer(logger, ldaPath); + const deliverer = new DovecotMailDeliverer(logFileProvider, ldaPath); deliverer.preHooks.push( new RecipientFromHeadersHook(mailDomain), new FallbackRecipientHook(new Set(fallback)), @@ -35,15 +35,11 @@ export function createInbound( return deliverer; } -export function createHono( - logger: Logger, - outbound: MailDeliverer, - inbound: MailDeliverer, -) { +export function createHono(outbound: MailDeliverer, inbound: MailDeliverer) { const hono = new Hono(); hono.onError((err, c) => { - logger.error("Hono handler throws an error.", err); + console.error("Hono handler throws an error.", err); return c.json({ msg: "Server error, check its log." }, 500); }); hono.use(honoLogger()); @@ -66,11 +62,11 @@ export function createHono( return hono; } -export function createSmtp(logger: Logger, outbound: MailDeliverer) { - return new DumbSmtpServer(logger, outbound); +export function createSmtp(outbound: MailDeliverer) { + return new DumbSmtpServer(outbound); } -export async function sendMail(logger: Logger, port: number) { +export async function sendMail(port: number) { const decoder = new TextDecoder(); let text = ""; for await (const chunk of Deno.stdin.readable) { @@ -81,9 +77,8 @@ export async function sendMail(logger: Logger, port: number) { method: "post", body: text, }); - logger.write(Deno.inspect(res), { level: res.ok ? "info" : "error" }); - logger.write(Deno.inspect(await res.text()), { - level: res.ok ? "info" : "error", - }); + 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 index 05d93cd..cb275ae 100644 --- a/deno/mail-relay/aws/app.ts +++ b/deno/mail-relay/aws/app.ts @@ -6,7 +6,7 @@ import { FetchHttpHandler } from "@smithy/fetch-http-handler"; // @ts-types="npm:@types/yargs" import yargs from "yargs"; -import { Logger } from "@crupest/base/log"; +import { LogFileProvider } from "@crupest/base/log"; import { ConfigDefinition, ConfigProvider } from "@crupest/base/config"; import { CronTask } from "@crupest/base/cron"; @@ -18,7 +18,7 @@ import { } from "./mail.ts"; import { AwsMailDeliverer } from "./deliver.ts"; import { AwsMailFetcher, AwsS3MailConsumer } from "./fetch.ts"; -import { createInbound, createHono, sendMail, createSmtp } from "../app.ts"; +import { createHono, createInbound, createSmtp, sendMail } from "../app.ts"; const PREFIX = "crupest-mail-server"; const CONFIG_DEFINITIONS = { @@ -94,17 +94,16 @@ function createAwsOptions({ } function createOutbound( - logger: Logger, awsOptions: ReturnType<typeof createAwsOptions>, db: DbService, ) { - const deliverer = new AwsMailDeliverer(logger, awsOptions); + 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(), + db.addMessageIdMap({ message_id: original, aws_message_id: aws }).then() ), ); return deliverer; @@ -156,80 +155,70 @@ function createCron(fetcher: AwsMailFetcher, consumer: AwsS3MailConsumer) { function createBaseServices() { const config = new ConfigProvider(PREFIX, CONFIG_DEFINITIONS); Deno.mkdirSync(config.get("dataPath"), { recursive: true }); - const logger = new Logger(); - logger.externalLogDir = join(config.get("dataPath"), "log"); - return { config, logger }; + const logFileProvider = new LogFileProvider( + join(config.get("dataPath"), "log"), + ); + return { config, logFileProvider }; } function createAwsFetchOnlyServices() { - const { config, logger } = createBaseServices(); + 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( - logger, - awsOptions, - config.get("awsMailBucket"), - ); - return { config, logger, awsOptions, fetcher }; + const fetcher = new AwsMailFetcher(awsOptions, config.get("awsMailBucket")); + + return { ...services, awsOptions, fetcher }; } function createAwsRecycleOnlyServices() { - const { config, logger, awsOptions, fetcher } = createAwsFetchOnlyServices(); + const services = createAwsFetchOnlyServices(); + const { config, logFileProvider } = services; - const inbound = createInbound(logger, { + 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 { config, logger, awsOptions, fetcher, inbound, recycler }; + return { ...services, inbound, recycler }; } function createAwsServices() { - const { config, logger, inbound, awsOptions, fetcher, recycler } = - createAwsRecycleOnlyServices(); + const services = createAwsRecycleOnlyServices(); + const { config, awsOptions } = services; + const dbService = new DbService(join(config.get("dataPath"), "db.sqlite")); - const outbound = createOutbound(logger, awsOptions, dbService); + const outbound = createOutbound(awsOptions, dbService); - return { - config, - logger, - inbound, - dbService, - awsOptions, - fetcher, - recycler, - outbound, - }; + return { ...services, dbService, outbound }; } function createServerServices() { const services = createAwsServices(); - const { logger, config, outbound, inbound, fetcher } = services; - const smtp = createSmtp(logger, outbound); + const { config, outbound, inbound, fetcher } = services; - const hono = createHono(logger, outbound, inbound); + 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 fetcher.consumeS3Mail( + s3Key, + (rawMail, _) => + inbound.deliver({ mail: new Mail(rawMail), recipients }).then(), ); }, }); - return { - ...services, - smtp, - hono, - }; + return { ...services, smtp, hono }; } function serve(cron: boolean = false) { @@ -252,11 +241,11 @@ function serve(cron: boolean = false) { } async function listLives() { - const { logger, fetcher } = createAwsFetchOnlyServices(); + const { fetcher } = createAwsFetchOnlyServices(); const liveMails = await fetcher.listLiveMails(); - logger.info(`Total ${liveMails.length}:`); + console.info(`Total ${liveMails.length}:`); if (liveMails.length !== 0) { - logger.info(liveMails.join("\n")); + console.info(liveMails.join("\n")); } } @@ -272,8 +261,8 @@ if (import.meta.main) { command: "sendmail", describe: "send mail via this server's endpoint", handler: async (_argv) => { - const { logger, config } = createBaseServices(); - await sendMail(logger, config.getInt("httpPort")); + const { config } = createBaseServices(); + await sendMail(config.getInt("httpPort")); }, }) .command({ diff --git a/deno/mail-relay/aws/deliver.ts b/deno/mail-relay/aws/deliver.ts index 9950e37..4dd4b3a 100644 --- a/deno/mail-relay/aws/deliver.ts +++ b/deno/mail-relay/aws/deliver.ts @@ -1,12 +1,9 @@ -// spellchecker:words sesv2 amazonses - import { SendEmailCommand, SESv2Client, SESv2ClientConfig, } from "@aws-sdk/client-sesv2"; -import { Logger } from "@crupest/base/log"; import { Mail, MailDeliverContext, SyncMailDeliverer } from "../mail.ts"; declare module "../mail.ts" { @@ -17,13 +14,11 @@ declare module "../mail.ts" { export class AwsMailDeliverer extends SyncMailDeliverer { readonly name = "aws"; - readonly #logger; readonly #aws; readonly #ses; - constructor(logger: Logger, aws: SESv2ClientConfig) { - super(logger); - this.#logger = logger; + constructor(aws: SESv2ClientConfig) { + super(); this.#aws = aws; this.#ses = new SESv2Client(aws); } @@ -32,7 +27,7 @@ export class AwsMailDeliverer extends SyncMailDeliverer { mail: Mail, context: MailDeliverContext, ): Promise<void> { - this.#logger.info("Begin to call aws send-email api..."); + console.info("Begin to call aws send-email api..."); try { const sendCommand = new SendEmailCommand({ @@ -43,14 +38,16 @@ export class AwsMailDeliverer extends SyncMailDeliverer { const res = await this.#ses.send(sendCommand); if (res.MessageId == null) { - this.#logger.warn("Aws send-email returns no message id."); + console.warn("Aws send-email returns no message id."); } else { - context.result.awsMessageId = `${res.MessageId}@${this.#aws.region}.amazonses.com`; + 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}.`, + message: + `Successfully called aws send-email, message id ${context.result.awsMessageId}.`, }); } catch (cause) { context.result.recipients.set("*", { diff --git a/deno/mail-relay/aws/fetch.ts b/deno/mail-relay/aws/fetch.ts index ef1ba5f..9278e63 100644 --- a/deno/mail-relay/aws/fetch.ts +++ b/deno/mail-relay/aws/fetch.ts @@ -7,8 +7,7 @@ import { S3ClientConfig, } from "@aws-sdk/client-s3"; -import { toFileNameString } from "@crupest/base/date"; -import { Logger } from "@crupest/base/log"; +import { toFileNameString } from "@crupest/base"; import { Mail } from "../mail.ts"; @@ -42,18 +41,16 @@ export type AwsS3MailConsumer = ( export class AwsMailFetcher { readonly #livePrefix = "mail/live/"; readonly #archivePrefix = "mail/archive/"; - readonly #logger; readonly #s3; readonly #bucket; - constructor(logger: Logger, aws: S3ClientConfig, bucket: string) { - this.#logger = logger; + constructor(aws: S3ClientConfig, bucket: string) { this.#s3 = new S3Client(aws); this.#bucket = bucket; } async listLiveMails(): Promise<string[]> { - this.#logger.info("Begin to retrieve live mails."); + console.info("Begin to retrieve live mails."); const listCommand = new ListObjectsV2Command({ Bucket: this.#bucket, @@ -62,16 +59,14 @@ export class AwsMailFetcher { const res = await this.#s3.send(listCommand); if (res.Contents == null) { - this.#logger.warn("Listing live mails in S3 returns null Content."); + console.warn("Listing live mails in S3 returns null Content."); return []; } const result: string[] = []; for (const object of res.Contents) { if (object.Key == null) { - this.#logger.warn( - "Listing live mails in S3 returns an object with no Key.", - ); + console.warn("Listing live mails in S3 returns an object with no Key."); continue; } @@ -83,9 +78,9 @@ export class AwsMailFetcher { } async consumeS3Mail(s3Key: string, consumer: AwsS3MailConsumer) { - this.#logger.info(`Begin to consume s3 mail ${s3Key} ...`); + console.info(`Begin to consume s3 mail ${s3Key} ...`); - this.#logger.info(`Fetching s3 mail ${s3Key}...`); + console.info(`Fetching s3 mail ${s3Key}...`); const mailPath = `${this.#livePrefix}${s3Key}`; const command = new GetObjectCommand({ Bucket: this.#bucket, @@ -98,32 +93,33 @@ export class AwsMailFetcher { } const rawMail = await res.Body.transformToString(); - this.#logger.info(`Done fetching s3 mail ${s3Key}.`); + console.info(`Done fetching s3 mail ${s3Key}.`); - this.#logger.info(`Calling consumer...`); + console.info(`Calling consumer...`); await consumer(rawMail, s3Key); - this.#logger.info(`Done consuming s3 mail ${s3Key}.`); + console.info(`Done consuming s3 mail ${s3Key}.`); const date = new Mail(rawMail) - .startSimpleParse(this.#logger) + .startSimpleParse() .sections() .headers() .date(); - const dateString = - date != null ? toFileNameString(date, true) : "invalid-date"; + const dateString = date != null + ? toFileNameString(date, true) + : "invalid-date"; const newPath = `${this.#archivePrefix}${dateString}/${s3Key}`; - this.#logger.info(`Archiving s3 mail ${s3Key} to ${newPath}...`); + console.info(`Archiving s3 mail ${s3Key} to ${newPath}...`); await s3MoveObject(this.#s3, this.#bucket, mailPath, newPath); - this.#logger.info(`Done archiving s3 mail ${s3Key}.`); + console.info(`Done archiving s3 mail ${s3Key}.`); - this.#logger.info(`Done consuming s3 mail ${s3Key}.`); + console.info(`Done consuming s3 mail ${s3Key}.`); } async recycleLiveMails(consumer: AwsS3MailConsumer) { - this.#logger.info("Begin to recycle live mails..."); + console.info("Begin to recycle live mails..."); const mails = await this.listLiveMails(); - this.#logger.info(`Found ${mails.length} live mails`); + 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 index d2cfad1..cc05d23 100644 --- a/deno/mail-relay/aws/mail.ts +++ b/deno/mail-relay/aws/mail.ts @@ -8,17 +8,17 @@ export class AwsMailMessageIdRewriteHook implements MailDeliverHook { } async callback(context: MailDeliverContext): Promise<void> { - context.logger.info("Rewrite message ids..."); + console.info("Rewrite message ids..."); const addresses = context.mail.simpleFindAllAddresses(); - context.logger.info(`Addresses found in mail: ${addresses.join(", ")}.`); + 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) { - context.logger.info(`Rewrite ${address} to ${awsMessageId}.`); + console.info(`Rewrite ${address} to ${awsMessageId}.`); context.mail.raw = context.mail.raw.replaceAll(address, awsMessageId); } } - context.logger.info("Done rewrite message ids."); + console.info("Done rewrite message ids."); } } @@ -30,24 +30,20 @@ export class AwsMailMessageIdSaveHook implements MailDeliverHook { } async callback(context: MailDeliverContext): Promise<void> { - context.logger.info("Save aws message ids..."); + console.info("Save aws message ids..."); const messageId = context.mail - .startSimpleParse(context.logger) + .startSimpleParse() .sections() .headers() .messageId(); if (messageId == null) { - context.logger.info( - "Original mail does not have message id. Skip saving.", - ); + console.info("Original mail does not have message id. Skip saving."); return; } if (context.result.awsMessageId != null) { - context.logger.info( - `Saving ${messageId} => ${context.result.awsMessageId}.`, - ); + console.info(`Saving ${messageId} => ${context.result.awsMessageId}.`); await this.#record(messageId, context.result.awsMessageId); } - context.logger.info("Done save message ids."); + console.info("Done save message ids."); } } diff --git a/deno/mail-relay/db.ts b/deno/mail-relay/db.ts index 807ecf6..062700b 100644 --- a/deno/mail-relay/db.ts +++ b/deno/mail-relay/db.ts @@ -1,5 +1,3 @@ -// spellchecker: words kysely insertable updateable introspector - import { Generated, Insertable, diff --git a/deno/mail-relay/dovecot.ts b/deno/mail-relay/dovecot.ts index 124a82b..bace225 100644 --- a/deno/mail-relay/dovecot.ts +++ b/deno/mail-relay/dovecot.ts @@ -1,15 +1,17 @@ import { basename } from "@std/path"; -import { Logger } from "@crupest/base/log"; +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(logger: Logger, ldaPath: string) { - super(logger); + constructor(logFileProvider: LogFileProvider, ldaPath: string) { + super(); + this.#logFileProvider = logFileProvider; this.#ldaPath = ldaPath; } @@ -29,12 +31,12 @@ export class DovecotMailDeliverer extends MailDeliverer { return; } - this.logger.info(`Deliver to dovecot users: ${recipients.join(", ")}.`); + console.info(`Deliver to dovecot users: ${recipients.join(", ")}.`); for (const recipient of recipients) { try { const commandArgs = ["-d", recipient]; - this.logger.info(`Run ${ldaBinName} ${commandArgs.join(" ")}...`); + console.info(`Run ${ldaBinName} ${commandArgs.join(" ")}...`); const ldaCommand = new Deno.Command(ldaPath, { args: commandArgs, @@ -44,8 +46,10 @@ export class DovecotMailDeliverer extends MailDeliverer { }); const ldaProcess = ldaCommand.spawn(); - using logFiles = - await this.logger.createExternalLogStreamsForProgram(ldaBinName); + using logFiles = await this.#logFileProvider + .createExternalLogStreamsForProgram( + ldaBinName, + ); ldaProcess.stdout.pipeTo(logFiles.stdout); ldaProcess.stderr.pipeTo(logFiles.stderr); @@ -90,6 +94,6 @@ export class DovecotMailDeliverer extends MailDeliverer { } } - this.logger.info("Done handling all recipients."); + console.info("Done handling all recipients."); } } diff --git a/deno/mail-relay/dumb-smtp-server.ts b/deno/mail-relay/dumb-smtp-server.ts index 1a1090a..ac7069c 100644 --- a/deno/mail-relay/dumb-smtp-server.ts +++ b/deno/mail-relay/dumb-smtp-server.ts @@ -1,4 +1,3 @@ -import { Logger } from "@crupest/base/log"; import { MailDeliverer } from "./mail.ts"; const CRLF = "\r\n"; @@ -20,26 +19,24 @@ function createResponses(host: string, port: number | string) { const LOG_TAG = "[dumb-smtp]"; export class DumbSmtpServer { - #logger; #deliverer; #responses: ReturnType<typeof createResponses> = createResponses( "invalid", "invalid", ); - constructor(logger: Logger, deliverer: MailDeliverer) { - this.#logger = logger; + constructor(deliverer: MailDeliverer) { this.#deliverer = deliverer; } async #handleConnection(conn: Deno.Conn) { using disposeStack = new DisposableStack(); disposeStack.defer(() => { - this.#logger.tagInfo(LOG_TAG, "Close session's tcp connection."); + console.info(LOG_TAG, "Close session's tcp connection."); conn.close(); }); - this.#logger.tagInfo(LOG_TAG, "New session's tcp connection established."); + console.info(LOG_TAG, "New session's tcp connection established."); const writer = conn.writable.getWriter(); disposeStack.defer(() => writer.releaseLock()); @@ -49,7 +46,7 @@ export class DumbSmtpServer { const [decoder, encoder] = [new TextDecoder(), new TextEncoder()]; const decode = (data: Uint8Array) => decoder.decode(data); const send = async (s: string) => { - this.#logger.tagInfo(LOG_TAG, "Send line: " + s); + console.info(LOG_TAG, "Send line: " + s); await writer.write(encoder.encode(s + CRLF)); }; @@ -72,7 +69,7 @@ export class DumbSmtpServer { buffer = buffer.slice(eolPos + CRLF.length); if (rawMail == null) { - this.#logger.tagInfo(LOG_TAG, "Received line: " + line); + console.info(LOG_TAG, "Received line: " + line); const upperLine = line.toUpperCase(); if (upperLine.startsWith("EHLO") || upperLine.startsWith("HELO")) { await send(this.#responses["EHLO"]); @@ -82,32 +79,26 @@ export class DumbSmtpServer { await send(this.#responses["RCPT"]); } else if (upperLine === "DATA") { await send(this.#responses["DATA"]); - this.#logger.tagInfo(LOG_TAG, "Begin to receive mail data..."); + console.info(LOG_TAG, "Begin to receive mail data..."); rawMail = ""; } else if (upperLine === "QUIT") { await send(this.#responses["QUIT"]); return; } else { - this.#logger.tagWarn( - LOG_TAG, - "Unrecognized command from client: " + line, - ); + console.warn(LOG_TAG, "Unrecognized command from client: " + line); await send(this.#responses["INVALID"]); return; } } else { if (line === ".") { try { - this.#logger.tagInfo( - LOG_TAG, - "Mail data Received, begin to relay...", - ); + 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; - this.#logger.tagInfo(LOG_TAG, "Relay succeeded."); + console.info(LOG_TAG, "Relay succeeded."); } catch (err) { - this.#logger.tagError(LOG_TAG, "Relay failed.", err); + console.error(LOG_TAG, "Relay failed.", err); await send("554 5.3.0 Error: check server log"); return; } @@ -123,7 +114,7 @@ export class DumbSmtpServer { async serve(options: { hostname: string; port: number }) { const listener = Deno.listen(options); this.#responses = createResponses(options.hostname, options.port); - this.#logger.tagInfo( + console.info( LOG_TAG, `Dumb SMTP server starts to listen on ${this.#responses.serverName}.`, ); @@ -132,11 +123,7 @@ export class DumbSmtpServer { try { await this.#handleConnection(conn); } catch (cause) { - this.#logger.tagError( - LOG_TAG, - "Tcp connection throws an error.", - 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 index 09cf8eb..cd0c38d 100644 --- a/deno/mail-relay/mail.test.ts +++ b/deno/mail-relay/mail.test.ts @@ -1,8 +1,6 @@ import { describe, it } from "@std/testing/bdd"; import { expect, fn } from "@std/expect"; -import { Logger } from "@crupest/base/log"; - import { Mail, MailDeliverContext, MailDeliverer } from "./mail.ts"; const mockDate = "Fri, 02 May 2025 08:33:02 +0000"; @@ -89,7 +87,7 @@ describe("Mail", () => { }), ]).toEqual( [...mockToAddresses, mockCcAddress].filter((a) => - a.endsWith("example.com"), + a.endsWith("example.com") ), ); }); @@ -119,7 +117,7 @@ describe("MailDeliverer", () => { return Promise.resolve(); }) as MailDeliverer["doDeliver"]; } - const mockDeliverer = new MockMailDeliverer(new Logger()); + const mockDeliverer = new MockMailDeliverer(); it("deliver success", async () => { await mockDeliverer.deliverRaw(mockMailStr); diff --git a/deno/mail-relay/mail.ts b/deno/mail-relay/mail.ts index 12d5972..d6dfe65 100644 --- a/deno/mail-relay/mail.ts +++ b/deno/mail-relay/mail.ts @@ -2,19 +2,10 @@ import { encodeBase64 } from "@std/encoding/base64"; import { parse } from "@std/csv/parse"; import emailAddresses from "email-addresses"; -import { Logger } from "@crupest/base/log"; - class MailSimpleParseError extends Error {} class MailSimpleParsedHeaders { - #logger; - - constructor( - logger: Logger | undefined, - public fields: [key: string, value: string][], - ) { - this.#logger = logger; - } + constructor(public fields: [key: string, value: string][]) {} getFirst(fieldKey: string): string | undefined { for (const [key, value] of this.fields) { @@ -31,9 +22,7 @@ class MailSimpleParsedHeaders { if (match != null) { return match[1]; } else { - this.#logger?.warn( - "Invalid message-id header of mail: " + messageIdField, - ); + console.warn("Invalid message-id header of mail: " + messageIdField); return undefined; } } @@ -44,7 +33,7 @@ class MailSimpleParsedHeaders { const date = new Date(dateField); if (invalidToUndefined && isNaN(date.getTime())) { - this.#logger?.warn(`Invalid date string (${dateField}) found in header.`); + console.warn(`Invalid date string (${dateField}) found in header.`); return undefined; } return date; @@ -76,11 +65,7 @@ class MailSimpleParsedSections { eol: string; sep: string; - #logger; - - constructor(logger: Logger | undefined, raw: string) { - this.#logger = logger; - + constructor(raw: string) { const twoEolMatch = raw.match(/(\r?\n)(\r?\n)/); if (twoEolMatch == null) { throw new MailSimpleParseError( @@ -91,7 +76,7 @@ class MailSimpleParsedSections { const [eol, sep] = [twoEolMatch[1], twoEolMatch[2]]; if (eol !== sep) { - logger?.warn("Different EOLs (\\r\\n, \\n) found."); + console.warn("Different EOLs (\\r\\n, \\n) found."); } this.header = raw.slice(0, twoEolMatch.index!); @@ -131,7 +116,7 @@ class MailSimpleParsedSections { handleField(); - return new MailSimpleParsedHeaders(this.#logger, headers); + return new MailSimpleParsedHeaders(headers); } } @@ -147,8 +132,8 @@ export class Mail { return encodeBase64(this.raw); } - startSimpleParse(logger?: Logger) { - return { sections: () => new MailSimpleParsedSections(logger, this.raw) }; + startSimpleParse() { + return { sections: () => new MailSimpleParsedSections(this.raw) }; } simpleFindAllAddresses(): string[] { @@ -195,10 +180,7 @@ export class MailDeliverContext { readonly recipients: Set<string> = new Set(); readonly result; - constructor( - public readonly logger: Logger, - public mail: Mail, - ) { + constructor(public mail: Mail) { this.result = new MailDeliverResult(this.mail); } } @@ -212,8 +194,6 @@ export abstract class MailDeliverer { preHooks: MailDeliverHook[] = []; postHooks: MailDeliverHook[] = []; - constructor(protected readonly logger: Logger) {} - protected abstract doDeliver( mail: Mail, context: MailDeliverContext, @@ -227,9 +207,9 @@ export abstract class MailDeliverer { mail: Mail; recipients?: string[]; }): Promise<MailDeliverResult> { - this.logger.info(`Begin to deliver mail via ${this.name}...`); + console.info(`Begin to deliver mail via ${this.name}...`); - const context = new MailDeliverContext(this.logger, options.mail); + const context = new MailDeliverContext(options.mail); options.recipients?.forEach((r) => context.recipients.add(r)); for (const hook of this.preHooks) { @@ -242,7 +222,8 @@ export abstract class MailDeliverer { await hook.callback(context); } - context.logger.info("Deliver result:\n" + Deno.inspect(context.result)); + console.info("Deliver result:"); + console.info(context.result); if (context.result.hasError()) { throw new Error("Mail failed to deliver."); @@ -259,7 +240,7 @@ export abstract class SyncMailDeliverer extends MailDeliverer { mail: Mail; recipients?: string[]; }): Promise<MailDeliverResult> { - this.logger.info( + console.info( "The mail deliverer is sync. Wait for last delivering done...", ); await this.#last; @@ -277,12 +258,12 @@ export class RecipientFromHeadersHook implements MailDeliverHook { callback(context: MailDeliverContext) { if (context.recipients.size !== 0) { - context.logger.warn( + console.warn( "Recipients are already filled. Won't set them with ones in headers.", ); } else { context.mail - .startSimpleParse(context.logger) + .startSimpleParse() .sections() .headers() .recipients({ @@ -290,7 +271,7 @@ export class RecipientFromHeadersHook implements MailDeliverHook { }) .forEach((r) => context.recipients.add(r)); - context.logger.info( + console.info( "Recipients found from mail headers: " + [...context.recipients].join(", "), ); @@ -304,7 +285,7 @@ export class FallbackRecipientHook implements MailDeliverHook { callback(context: MailDeliverContext) { if (context.recipients.size === 0) { - context.logger.info( + console.info( "No recipients, fill with fallback: " + [...this.fallback].join(", "), ); this.fallback.forEach((a) => context.recipients.add(a)); @@ -320,10 +301,10 @@ export class AliasRecipientMailHook implements MailDeliverHook { this.#aliasFile = aliasFile; } - async #parseAliasFile(logger: Logger): Promise<Map<string, string>> { + async #parseAliasFile(): Promise<Map<string, string>> { const result = new Map(); if ((await Deno.stat(this.#aliasFile)).isFile) { - logger.info(`Found recipients alias file: ${this.#aliasFile}.`); + 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) { @@ -334,11 +315,11 @@ export class AliasRecipientMailHook implements MailDeliverHook { } async callback(context: MailDeliverContext) { - const aliases = await this.#parseAliasFile(context.logger); + const aliases = await this.#parseAliasFile(); for (const recipient of [...context.recipients]) { const realRecipients = aliases.get(recipient); if (realRecipients != null) { - context.logger.info( + console.info( `Recipient alias resolved: ${recipient} => ${realRecipients}.`, ); context.recipients.delete(recipient); diff --git a/deno/tools/deno.json b/deno/tools/deno.json index 1b2cf32..355046a 100644 --- a/deno/tools/deno.json +++ b/deno/tools/deno.json @@ -3,6 +3,6 @@ "tasks": { }, "imports": { - "mustache": "npm:mustache@^4.2.0", + "mustache": "npm:mustache@^4.2.0" } } diff --git a/deno/tools/generate-geosite-rules.ts b/deno/tools/generate-geosite-rules.ts index c59d34f..bfa53ba 100644 --- a/deno/tools/generate-geosite-rules.ts +++ b/deno/tools/generate-geosite-rules.ts @@ -1,7 +1,8 @@ -const PROXY_NAME = "node-select" -const ATTR = "cn" +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 URL = + "https://github.com/v2fly/domain-list-community/archive/refs/heads/master.zip"; const SITES = [ "github", "google", @@ -39,9 +40,9 @@ const SITES = [ "ieee", "sci-hub", "libgen", -] +]; -const prefixes = ["include", "domain", "keyword", "full", "regexp"] as const +const prefixes = ["include", "domain", "keyword", "full", "regexp"] as const; interface Rule { kind: (typeof prefixes)[number]; @@ -52,20 +53,20 @@ interface Rule { 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"; + 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()), + }; } - 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 @@ -76,10 +77,10 @@ function parseLine(line: string): Rule { .map((l) => parseLine(l)); } - const visited = [] as string[] - const rules = [] as Rule[] + const visited = [] as string[]; + const rules = [] as Rule[]; - function add(name :string) { + function add(name: string) { const text = provider(name); for (const rule of parse(text)) { if (rule.kind === "include") { @@ -100,25 +101,25 @@ function parseLine(line: string): Rule { add(start); } - return rules + 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", + domain: "DOMAIN-SUFFIX", + full: "DOMAIN", + keyword: "DOMAIN-KEYWORD", + regexp: "DOMAIN-REGEX", } as const; if (rule.kind === "include") { - throw new Error("Include rule not parsed.") + throw new Error("Include rule not parsed."); } - return `${prefixMap[rule.kind]},${rule.value}` + return `${prefixMap[rule.kind]},${rule.value}`; } function toLines(rules: Rule[]) { - return rules.map(r => toLine(r)).join("\n") + return rules.map((r) => toLine(r)).join("\n"); } const has: Rule[] = []; @@ -128,7 +129,6 @@ function toNewFormat(rules: Rule[], attr: string): [string, string] { return [toLines(has), toLines(notHas)]; } - if (import.meta.main) { const tmpDir = Deno.makeTempDirSync({ prefix: "geosite-rules-" }); console.log("Work dir is ", tmpDir); @@ -150,12 +150,11 @@ if (import.meta.main) { 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) + 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-vm.ts b/deno/tools/manage-vm.ts index a1388b1..bb985ce 100644 --- a/deno/tools/manage-vm.ts +++ b/deno/tools/manage-vm.ts @@ -1,9 +1,8 @@ -import os from "node:os" +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"], @@ -13,9 +12,7 @@ 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, - )) { + for (const [name, aliases] of Object.entries(arches as ArchAliasMap)) { if (aliases.includes(generalName)) return name as Arch; } throw Error("Unknown architecture name."); diff --git a/deno/tools/template.ts b/deno/tools/template.ts index 0b043a1..1b67eb8 100644 --- a/deno/tools/template.ts +++ b/deno/tools/template.ts @@ -73,7 +73,9 @@ export class TemplateDir { generate(vars: Record<string, string>, generatedDir?: string) { console.log( - `Generating, template dir: ${this.dir}, generated dir: ${generatedDir ?? "[dry-run]"}:`, + `Generating, template dir: ${this.dir}, generated dir: ${ + generatedDir ?? "[dry-run]" + }:`, ); const undefinedVars = this.allNeededVars().filter((v) => !(v in vars)); diff --git a/dictionary.txt b/dictionary.txt index 03a5f54..e2894d9 100644 --- a/dictionary.txt +++ b/dictionary.txt @@ -4,40 +4,68 @@ Yuqian Yang fxxking -# self-hosted services +# general +aarch64 +esmtp +healthcheck + 2fauth -rspamd certbot roundcube roundcubemail +gerrit gohugoio +pwsh +rclone + +kmod +btrfs +chroot +nproc +zstd +cpio +pacman +fontconfig -# general -catppuccin -macchiato -cheatsheet -aarch64 -pythonpath -gerrit -esmtp -tini -healthcheck nspawn +tini containerd buildx -fontconfig +qcow2 +hostfwd + +# languages +pythonpath +denoland +kysely +insertable + +ustc +sourceware +sesv2 +amazonses + +geodata +geoip +geosite +vmess +vnext + +catppuccin +macchiato # vim/nvim nvim neovide vimruntime - +termguicolors autobrief autopairs bashls bufhidden bufnr clangd +denols devicons exepath gitsigns @@ -45,16 +73,6 @@ lspconfig lualine luasnip -# unix -cpio -kmod -nproc -sourceware -zstd -btrfs -pacman -rclone - # hurd gnumach settrans @@ -69,7 +87,6 @@ dquilt buildpackage quiltrc nocheck -chroot indep confdir createchroot @@ -77,10 +94,3 @@ sbuild sbuildrc schroot -# misc -geodata -geoip -geosite -vmess -vnext -ustc diff --git a/store/config/mihomo/config.yaml b/store/config/mihomo/config.yaml index cd2c3a3..c455409 100644 --- a/store/config/mihomo/config.yaml +++ b/store/config/mihomo/config.yaml @@ -26,56 +26,61 @@ dns: ipv6: true default-nameserver: - 223.5.5.5 + - 119.29.29.29 nameserver: +# - 9.9.9.11 +# - tls://1.1.1.1 + - https://doh.pub/dns-query - https://dns.alidns.com/dns-query - 223.5.5.5 - - 8.8.8.8 - - 1.1.1.1 + - 119.29.29.29 + +sniffer: + enable: true + sniff: + HTTP: + ports: [80] + TLS: + ports: [443] + QUIC: + ports: [443] + skip-domain: + - "Mijia Cloud" + +rule-providers: + cn: + type: file + path: has-rule + behavior: classical + format: text + + non-cn: + type: file + path: not-has-rule + behavior: classical + format: text + + need: + type: file + path: need-rule + behavior: classical + format: text rules: - - GEOSITE,github,node-select - - GEOSITE,google,node-select - - GEOSITE,youtube,node-select - - GEOSITE,twitter,node-select - - GEOSITE,facebook,node-select - - GEOSITE,discord,node-select - - GEOSITE,reddit,node-select - - GEOSITE,twitch,node-select - - GEOSITE,quora,node-select - - GEOSITE,telegram,node-select - - GEOSITE,imgur,node-select - - GEOSITE,stackexchange,node-select - - GEOSITE,onedrive,node-select - - GEOSITE,duckduckgo,node-select - - GEOSITE,wikimedia,node-select - - GEOSITE,gitbook,node-select - - GEOSITE,gitlab,node-select - - GEOSITE,creativecommons,node-select - - GEOSITE,archive,node-select - - GEOSITE,matrix,node-select - - GEOSITE,tor,node-select - - GEOSITE,python,node-select - - GEOSITE,ruby,node-select - - GEOSITE,rust,node-select - - GEOSITE,nodejs,node-select - - GEOSITE,npmjs,node-select - - GEOSITE,qt,node-select - - GEOSITE,docker,node-select - - GEOSITE,v2ray,node-select - - GEOSITE,homebrew,node-select - - GEOSITE,bootstrap,node-select - - GEOSITE,heroku,node-select - - GEOSITE,vercel,node-select - - GEOSITE,ieee,node-select - - GEOSITE,sci-hub,node-select - - GEOSITE,libgen,node-select + - RULE-SET,cn,DIRECT + - RULE-SET,non-cn,node-select + - RULE-SET,need,node-select +# - NOT,((GEOIP,CN)),node-select - DOMAIN-SUFFIX,gnu.org,node-select - DOMAIN-SUFFIX,nongnu.org,node-select - DOMAIN-SUFFIX,ietf.org,node-select - - DOMAIN-SUFFIX,packagist.org,node-select - DOMAIN-SUFFIX,metacubex.one,node-select - DOMAIN-SUFFIX,winehq.org,node-select - - DOMAIN-SUFFIX,postfix.org,node-select + - DOMAIN-SUFFIX,freedesktop.org,node-select + - DOMAIN-SUFFIX,eff.org,node-select + - DOMAIN-SUFFIX,typescriptlang.org,node-select + - DOMAIN-SUFFIX,arxiv.org,node-select +# - MATCH,node-select - MATCH,DIRECT proxy-groups: diff --git a/store/config/mihomo/need-rule b/store/config/mihomo/need-rule new file mode 100644 index 0000000..7ffcf49 --- /dev/null +++ b/store/config/mihomo/need-rule @@ -0,0 +1,4 @@ +IP-CIDR,185.199.108.153/32 +IP-CIDR,185.199.109.153/32 +IP-CIDR,185.199.110.153/32 +IP-CIDR,185.199.111.153/32
\ No newline at end of file diff --git a/store/config/nvim/lazy-lock.json b/store/config/nvim/lazy-lock.json index f323937..4f6c2b5 100644 --- a/store/config/nvim/lazy-lock.json +++ b/store/config/nvim/lazy-lock.json @@ -3,16 +3,15 @@ "cmp-buffer": { "branch": "main", "commit": "b74fab3656eea9de20a9b8116afa3cfc4ec09657" }, "cmp-nvim-lsp": { "branch": "main", "commit": "a8912b88ce488f411177fc8aed358b04dc246d7b" }, "cmp-path": { "branch": "main", "commit": "c6635aae33a50d6010bf1aa756ac2398a2d54c32" }, - "conform.nvim": { "branch": "master", "commit": "0e93e0d12d2f7ebdea9e3e444dfaff0050cefbe6" }, - "gitsigns.nvim": { "branch": "main", "commit": "d0f90ef51d4be86b824b012ec52ed715b5622e51" }, + "gitsigns.nvim": { "branch": "main", "commit": "731b581428ec6c1ccb451b95190ebbc6d7006db7" }, "lazy.nvim": { "branch": "main", "commit": "6c3bda4aca61a13a9c63f1c1d1b16b9d3be90d7a" }, - "lualine.nvim": { "branch": "master", "commit": "0c6cca9f2c63dadeb9225c45bc92bb95a151d4af" }, + "lualine.nvim": { "branch": "master", "commit": "a94fc68960665e54408fe37dcf573193c4ce82c9" }, "neo-tree.nvim": { "branch": "v3.x", "commit": "f481de16a0eb59c985abac8985e3f2e2f75b4875" }, - "nui.nvim": { "branch": "main", "commit": "7cd18e73cfbd70e1546931b7268b3eebaeff9391" }, + "nui.nvim": { "branch": "main", "commit": "de740991c12411b663994b2860f1a4fd0937c130" }, "nvim-autopairs": { "branch": "master", "commit": "4d74e75913832866aa7de35e4202463ddf6efd1b" }, "nvim-cmp": { "branch": "main", "commit": "b5311ab3ed9c846b585c0c15b7559be131ec4be9" }, - "nvim-lint": { "branch": "master", "commit": "cc26ae6a620298bb3f33b0e0681f99a10ae57781" }, - "nvim-lspconfig": { "branch": "master", "commit": "a182334ba933e58240c2c45e6ae2d9c7ae313e00" }, + "nvim-lint": { "branch": "master", "commit": "2b0039b8be9583704591a13129c600891ac2c596" }, + "nvim-lspconfig": { "branch": "master", "commit": "7ad4a11cc5742774877c529fcfb2702f7caf75e4" }, "nvim-treesitter": { "branch": "master", "commit": "42fc28ba918343ebfd5565147a42a26580579482" }, "nvim-web-devicons": { "branch": "master", "commit": "1fb58cca9aebbc4fd32b086cb413548ce132c127" }, "plenary.nvim": { "branch": "master", "commit": "857c5ac632080dba10aae49dba902ce3abf91b35" }, diff --git a/store/config/nvim/lua/plugins.lua b/store/config/nvim/lua/plugins.lua index 8458575..85de362 100644 --- a/store/config/nvim/lua/plugins.lua +++ b/store/config/nvim/lua/plugins.lua @@ -37,5 +37,4 @@ return { { "hrsh7th/cmp-path" }, { "windwp/nvim-autopairs" }, { "mfussenegger/nvim-lint" }, - { 'stevearc/conform.nvim' } } diff --git a/store/config/nvim/lua/setup/init.lua b/store/config/nvim/lua/setup/init.lua index ec8c8d4..bbce01c 100644 --- a/store/config/nvim/lua/setup/init.lua +++ b/store/config/nvim/lua/setup/init.lua @@ -1,5 +1,3 @@ --- spellchecker: words termguicolors - local function close_float() local wins = vim.api.nvim_list_wins() for _, v in ipairs(wins) do diff --git a/store/config/nvim/lua/setup/lsp.lua b/store/config/nvim/lua/setup/lsp.lua index a11ad34..4216f1c 100644 --- a/store/config/nvim/lua/setup/lsp.lua +++ b/store/config/nvim/lua/setup/lsp.lua @@ -1,5 +1,3 @@ --- spellchecker: words denols luals - vim.lsp.config("*", { capabilities = vim.tbl_extend("force", vim.lsp.protocol.make_client_capabilities(), @@ -24,15 +22,23 @@ local function setup_clangd() vim.lsp.config("clangd", { cmd = { clangd } }) - vim.api.nvim_create_autocmd('LspAttach', { + vim.api.nvim_create_autocmd("LspAttach", { callback = function(ev) if client_name_is(ev, "clangd") then - vim.keymap.set('n', 'grs', "<cmd>ClangdSwitchSourceHeader<cr>", { + vim.keymap.set("n", "grs", "<cmd>ClangdSwitchSourceHeader<cr>", { buffer = ev.buf }) end end }) + + vim.api.nvim_create_autocmd("LspDetach", { + callback = function(ev) + if client_name_is(ev, "clangd") then + vim.keymap.del("n", "grs", { buffer = ev.buf }) + end + end + }) end local function setup_lua_ls() @@ -57,6 +63,11 @@ local function setup_lua_ls() }) end +function vim.crupest.no_range_format() + print("Lsp doesn't support range formatting. Use gqa to format the whole document.") + return 0 +end + local function setup_denols() vim.lsp.config("denols", { root_dir = function(bufnr, on_dir) @@ -69,10 +80,22 @@ local function setup_denols() end, }) - vim.api.nvim_create_autocmd('LspAttach', { + vim.api.nvim_create_autocmd("LspAttach", { callback = function(ev) if client_name_is(ev, "denols") then - vim.o.formatexpr = "v:lua.require'conform'.formatexpr()" + vim.api.nvim_set_option_value( + "formatexpr", + "v:lua.vim.crupest.no_range_format()", + { buf = ev.buf } + ) + end + end + }) + + vim.api.nvim_create_autocmd("LspDetach", { + callback = function(ev) + if client_name_is(ev, "denols") then + vim.api.nvim_set_option_value("formatexpr", "", { buf = ev.buf }) end end }) @@ -80,6 +103,18 @@ end local function setup() + vim.api.nvim_create_autocmd("LspAttach", { + callback = function(ev) + vim.keymap.set("n", "gqa", vim.lsp.buf.format, { buffer = ev.buf }) + end + }) + + vim.api.nvim_create_autocmd("LspDetach", { + callback = function(ev) + vim.keymap.del("n", "gqa", { buffer = ev.buf }) + end + }) + setup_clangd() setup_lua_ls() setup_denols() diff --git a/store/config/nvim/lua/setup/plugins/conform.lua b/store/config/nvim/lua/setup/plugins/conform.lua deleted file mode 100644 index 57b74a9..0000000 --- a/store/config/nvim/lua/setup/plugins/conform.lua +++ /dev/null @@ -1,17 +0,0 @@ -local function setup() - require("conform").setup({ - formatters_by_ft = { - javascript = { "prettierd", "prettier", stop_after_first = true }, - typescript = { "prettierd", "prettier", stop_after_first = true }, - javascriptreact = { "prettierd", "prettier", stop_after_first = true }, - typescriptreact = { "prettierd", "prettier", stop_after_first = true }, - }, - default_format_opts = { - lsp_format = "fallback", - }, - }) -end - -return { - setup = setup -} diff --git a/store/config/nvim/lua/setup/plugins/init.lua b/store/config/nvim/lua/setup/plugins/init.lua index 88eca4f..8f1346b 100644 --- a/store/config/nvim/lua/setup/plugins/init.lua +++ b/store/config/nvim/lua/setup/plugins/init.lua @@ -15,7 +15,6 @@ local function setup() require("setup.plugins.tree-sitter").setup() require("setup.plugins.lint").setup() - require("setup.plugins.conform").setup() require("setup.plugins.cmp").setup() require("nvim-autopairs").setup {} end diff --git a/store/config/nvim/lua/setup/plugins/lint.lua b/store/config/nvim/lua/setup/plugins/lint.lua index b33db22..d03f539 100644 --- a/store/config/nvim/lua/setup/plugins/lint.lua +++ b/store/config/nvim/lua/setup/plugins/lint.lua @@ -1,4 +1,4 @@ ---- spellchecker: words markdownlintrc +--- spellchecker: ignore markdownlintrc ---@alias CruLinter { name: string, config_patterns: string[], filetypes: string[] | nil, fast: boolean } diff --git a/store/config/nvim/lua/setup/win.lua b/store/config/nvim/lua/setup/win.lua index 90e168a..9aa979d 100644 --- a/store/config/nvim/lua/setup/win.lua +++ b/store/config/nvim/lua/setup/win.lua @@ -1,4 +1,3 @@ --- spellchecker: words pwsh -- spellchecker: ignore shellcmdflag shellredir shellpipe shellquote shellxquote local function setup() vim.cmd([[ diff --git a/www/assets/res/css/base.css b/www/assets/res/css/base.css index 06fcb4b..4449c40 100644 --- a/www/assets/res/css/base.css +++ b/www/assets/res/css/base.css @@ -39,7 +39,7 @@ table { border-collapse: collapse; &, :is(td,th) { - padding: 0.4em; + padding: 0.2em 0.4em; border: 1px solid var(--table-border-color); } } diff --git a/www/assets/res/css/todos.css b/www/assets/res/css/todos.css index 7802812..f9aa23b 100644 --- a/www/assets/res/css/todos.css +++ b/www/assets/res/css/todos.css @@ -1,24 +1,17 @@ -.todo { - h3::before { - font-family: monospace; +h3.todo { + &::before { + font-size: small; } - &.working h3::before { - content: "* "; + &.working::before { + content: "(working) "; } - &.done h3::before { - content: "✓ "; + &.done::before { + content: "(done) "; } - &.give-up { - &, a:link, a:visited { - color: grey; - } - - h3:before { - content: "orz ✖ "; - } + &.give-up::before { + content: "(give up) "; } } - diff --git a/www/config/_default/hugo.yaml b/www/config/_default/hugo.yaml index b913177..289b0b4 100644 --- a/www/config/_default/hugo.yaml +++ b/www/config/_default/hugo.yaml @@ -18,6 +18,10 @@ frontmatter: markup: goldmark: + parser: + attribute: + block: true + title: true extensions: table: true highlight: diff --git a/www/content/notes/_index.md b/www/content/notes/_index.md index 3c736ed..3f96f73 100644 --- a/www/content/notes/_index.md +++ b/www/content/notes/_index.md @@ -1,5 +1,16 @@ --- title: "Notes" -params: - recursive: true +date: 2025-06-14T21:24:00+08:00 +lastmod: 2025-06-14T21:24:00+08:00 +layout: single --- + +- [Cheat Sheet](/notes/cheat-sheet) + +- [Hurd](/notes/hurd) + + - [Cheat Sheet](/notes/hurd/cheat-sheet) + + - [Todos](/notes/hurd/todos) + + - [Useful Links](/notes/hurd/links) diff --git a/www/content/notes/cheat-sheet.md b/www/content/notes/cheat-sheet.md index 56bc92a..2f30140 100644 --- a/www/content/notes/cheat-sheet.md +++ b/www/content/notes/cheat-sheet.md @@ -4,11 +4,8 @@ date: 2025-04-01T23:09:53+08:00 lastmod: 2025-06-12T01:09:39+08:00 --- -{{< mono >}} - goto: [Hurd Cheat Sheet (in a separated page)](/notes/hurd/cheat-sheet) - -{{< /mono >}} +{class="mono"} ## GRUB @@ -44,7 +41,7 @@ docker run -it --rm -v "./data/git/user-info:/user-info" httpd htpasswd /user-in A complete command is `[prefix] [docker (based on challenge kind)] [command] [challenge] [domains] [test] [misc]` | part | for | segment | -| --- | --- | --- | +| :-: | :-: | --- | | prefix | * | `docker run -it --rm --name certbot -v "./data/certbot/certs:/etc/letsencrypt" -v "./data/certbot/data:/var/lib/letsencrypt"` | | docker | challenge standalone | `-p "0.0.0.0:80:80"` | | docker | challenge nginx | `-v "./data/certbot/webroot:/var/www/certbot"` | diff --git a/www/content/notes/hurd/_index.md b/www/content/notes/hurd/_index.md new file mode 100644 index 0000000..8faf70b --- /dev/null +++ b/www/content/notes/hurd/_index.md @@ -0,0 +1,15 @@ +--- +title: "Hurd" +date: 2025-03-03T15:34:41+08:00 +lastmod: 2025-06-12T01:09:39+08:00 +layout: single +--- + +This is the gateway page for various notes about +[GNU/Hurd](https://www.gnu.org/software/hurd/) written by me. + +- [Cheat Sheet](/notes/hurd/cheat-sheet) + +- [Todos](/notes/hurd/todos) + +- [Useful Links](/notes/hurd/links) diff --git a/www/content/notes/hurd/cheat-sheet.md b/www/content/notes/hurd/cheat-sheet.md index f48e943..6fe5ccd 100644 --- a/www/content/notes/hurd/cheat-sheet.md +++ b/www/content/notes/hurd/cheat-sheet.md @@ -1,7 +1,7 @@ --- title: "Hurd Cheat Sheet" date: 2025-06-12T00:59:16+08:00 -lastmod: 2025-06-12T00:59:16+08:00 +lastmod: 2025-06-14T20:34:06+08:00 --- ## Mirrors @@ -45,6 +45,9 @@ boot on. QEMU cli arguments `-machine q35` enables AHCI and SATA, and is **required for official x86_64 image to boot**. As for i386, I haven't checked now. +There is [a Deno script](https://github.com/crupest/crupest/blob/dev/deno/tools/manage-vm.ts) +written by me to help define and build QEMU cli arguments of VMs. + ## Inside Hurd Configure/Setup network diff --git a/www/content/notes/hurd.md b/www/content/notes/hurd/links.md index aeb9b15..1e966d4 100644 --- a/www/content/notes/hurd.md +++ b/www/content/notes/hurd/links.md @@ -1,19 +1,11 @@ --- -title: "Hurd" -date: 2025-03-03T15:34:41+08:00 -lastmod: 2025-06-12T01:09:39+08:00 +title: "Hurd Useful Links" +date: 2025-06-14T20:34:06+08:00 +lastmod: 2025-06-14T20:34:06+08:00 --- -{{< mono >}} - -goto: [Cheat Sheet](/notes/hurd/cheat-sheet) | [Todos](/notes/hurd/todos) - -{{< /mono >}} - ## links -{{< mono >}} - | name | link | | --- | --- | | kernel-list-archive | <https://lists.gnu.org/archive/html/bug-hurd/> | @@ -22,12 +14,8 @@ goto: [Cheat Sheet](/notes/hurd/cheat-sheet) | [Todos](/notes/hurd/todos) | kernel-home | <https://www.gnu.org/software/hurd/index.html> | | debian-home | <https://www.debian.org/ports/hurd/> | -{{< /mono >}} - refs: -{{< mono >}} - | name | link | | --- | --- | | c | <https://en.cppreference.com/w/c> | @@ -36,20 +24,14 @@ refs: | posix 2008 | <https://pubs.opengroup.org/onlinepubs/9699919799.2008edition/> | | glibc | <https://sourceware.org/glibc/manual/2.41/html_mono/libc.html> | -{{< /mono >}} - ## mailing lists / irc -{{< mono >}} - | name | address | | --- | --- | | hurd | <bug-hurd@gnu.org> | | debian | <debian-hurd@lists.debian.org> | | irc | librechat #hurd | -{{< /mono >}} - ## *_MAX patch See [this](posts/c-func-ext.md) diff --git a/www/content/notes/hurd/todos.md b/www/content/notes/hurd/todos.md index 8fe068b..2dbded3 100644 --- a/www/content/notes/hurd/todos.md +++ b/www/content/notes/hurd/todos.md @@ -1,7 +1,7 @@ --- title: "Hurd Todos" date: 2025-03-03T21:22:35+08:00 -lastmod: 2025-03-03T23:28:46+08:00 +lastmod: 2025-06-14T20:34:06+08:00 params: css: - todos @@ -9,7 +9,11 @@ params: ## Porting -{{< todo name=pam state=give-up >}} +### hurd-fs4 {class="todo working"} + +<https://salsa.debian.org/rust-team/debcargo-conf/-/merge_requests/872> + +### pam {class="todo give-up"} {{< link-group >}} git @@ -23,9 +27,7 @@ mail <https://lists.debian.org/debian-hurd/2025/02/msg00018.html> {{< /link-group >}} -{{< /todo >}} - -{{< todo name=abseil state=working >}} +### abseil {class="todo working"} {{< link-group >}} git @@ -40,9 +42,7 @@ mail <https://lists.debian.org/debian-hurd/2025/02/msg00035.html> {{< /link-group >}} -{{< /todo >}} - -{{< todo name=libgav1 state=done >}} +### libgav1 {class="todo done"} {{< link-group >}} git @@ -56,5 +56,3 @@ misc mail: <https://lists.debian.org/debian-hurd/2025/02/msg00016.html> gerrit: <https://chromium-review.googlesource.com/c/codecs/libgav1/+/6239812> {{< /link-group >}} - -{{< /todo >}} diff --git a/www/content/notes/todos.md b/www/content/notes/todos.md deleted file mode 100644 index 1625362..0000000 --- a/www/content/notes/todos.md +++ /dev/null @@ -1,7 +0,0 @@ ---- -title: "Todos" -date: 2025-03-03T15:34:53+08:00 -lastmod: 2025-03-03T23:28:46+08:00 ---- - -[Hurd](/notes/hurd/todos) diff --git a/www/layouts/partials/css-res.html b/www/layouts/_partials/css-res.html index 6fabf67..6fabf67 100644 --- a/www/layouts/partials/css-res.html +++ b/www/layouts/_partials/css-res.html diff --git a/www/layouts/partials/css.html b/www/layouts/_partials/css.html index 12d3353..12d3353 100644 --- a/www/layouts/partials/css.html +++ b/www/layouts/_partials/css.html diff --git a/www/layouts/partials/date.html b/www/layouts/_partials/date.html index 9769e4e..9769e4e 100644 --- a/www/layouts/partials/date.html +++ b/www/layouts/_partials/date.html diff --git a/www/layouts/partials/highlight.html b/www/layouts/_partials/highlight.html index 28c510e..28c510e 100644 --- a/www/layouts/partials/highlight.html +++ b/www/layouts/_partials/highlight.html diff --git a/www/layouts/partials/js.html b/www/layouts/_partials/js.html index 16dafa4..16dafa4 100644 --- a/www/layouts/partials/js.html +++ b/www/layouts/_partials/js.html diff --git a/www/layouts/partials/nav.html b/www/layouts/_partials/nav.html index 42c9ad1..42c9ad1 100644 --- a/www/layouts/partials/nav.html +++ b/www/layouts/_partials/nav.html diff --git a/www/layouts/partials/preview/article.html b/www/layouts/_partials/preview/article.html index 6245434..6245434 100644 --- a/www/layouts/partials/preview/article.html +++ b/www/layouts/_partials/preview/article.html diff --git a/www/layouts/partials/preview/post.html b/www/layouts/_partials/preview/post.html index f0c6fb5..f0c6fb5 100644 --- a/www/layouts/partials/preview/post.html +++ b/www/layouts/_partials/preview/post.html diff --git a/www/layouts/partials/preview/posts.html b/www/layouts/_partials/preview/posts.html index f2cb640..f2cb640 100644 --- a/www/layouts/partials/preview/posts.html +++ b/www/layouts/_partials/preview/posts.html diff --git a/www/layouts/shortcodes/link-group.html b/www/layouts/_shortcodes/link-group.html index b16c2bc..b16c2bc 100644 --- a/www/layouts/shortcodes/link-group.html +++ b/www/layouts/_shortcodes/link-group.html diff --git a/www/layouts/_default/baseof.html b/www/layouts/baseof.html index 6d00be5..6d00be5 100644 --- a/www/layouts/_default/baseof.html +++ b/www/layouts/baseof.html diff --git a/www/layouts/index.html b/www/layouts/home.html index af3e11d..3cfc455 100644 --- a/www/layouts/index.html +++ b/www/layouts/home.html @@ -33,12 +33,12 @@ goto: <ul> <li><a href="{{ absURL "/git/" }}">git</a></li> + {{ with .GetPage "/notes" }} + <li><a href="{{ .RelPermalink }}">notes</a></li> + {{ end }} {{ with .GetPage "/notes/hurd" }} <li><a href="{{ .RelPermalink }}">hurd</a></li> {{ end }} - {{ with .GetPage "/notes/todos" }} - <li><a href="{{ .RelPermalink }}">todos</a></li> - {{ end }} {{ with .GetPage "/notes/cheat-sheet" }} <li><a href="{{ .RelPermalink }}">cheat-sheet</a></li> {{ end }} diff --git a/www/layouts/_default/list.html b/www/layouts/list.html index 5bb0b5e..be33d10 100644 --- a/www/layouts/_default/list.html +++ b/www/layouts/list.html @@ -5,13 +5,9 @@ {{ define "content" }} {{ partial "nav.html" . }} <h1>{{ .Title }}</h1> - {{ $pages := .RegularPages }} - {{ if .Param "recursive" }} - {{ $pages = .RegularPagesRecursive }} - {{ end }} {{ partial "preview/posts.html" (dict "h" "h3" - "pages" $pages + "pages" .RegularPages ) }} {{ end }} diff --git a/www/layouts/shortcodes/mono.html b/www/layouts/shortcodes/mono.html deleted file mode 100644 index ab183a5..0000000 --- a/www/layouts/shortcodes/mono.html +++ /dev/null @@ -1,3 +0,0 @@ -<div class="mono-container"> - {{ .Inner | .Page.RenderString }} -</div> diff --git a/www/layouts/shortcodes/todo.html b/www/layouts/shortcodes/todo.html deleted file mode 100644 index b4fc680..0000000 --- a/www/layouts/shortcodes/todo.html +++ /dev/null @@ -1,5 +0,0 @@ -<section class="todo {{ .Get "state" }}"> - <h3>{{ .Get "name" }}</h3> - {{ .Inner }} -</section> - diff --git a/www/layouts/_default/single.html b/www/layouts/single.html index cd0e9c5..cd0e9c5 100644 --- a/www/layouts/_default/single.html +++ b/www/layouts/single.html |