feat: add address source tracking (source_meta field) (#794)

- Add source_meta field to address table for tracking creation source
- Web: records client IP address (with fallback to 'web:unknown')
- Telegram: records 'tg:{userId}'
- Admin: records 'admin'
- Add database migration with field existence check
- Add frontend display in admin Account page
- Backward compatible: fallback if field doesn't exist

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Dream Hunter
2025-12-28 13:44:52 +08:00
committed by GitHub
parent 499f65078b
commit 3b3968f3b4
11 changed files with 99 additions and 16 deletions

View File

@@ -123,6 +123,7 @@ export const newAddress = async (
addressPrefix = null,
checkAllowDomains = true,
enableCheckNameRegex = true,
sourceMeta = null,
}: {
name: string, domain: string | undefined | null,
enablePrefix: boolean,
@@ -130,6 +131,7 @@ export const newAddress = async (
addressPrefix?: string | undefined | null,
checkAllowDomains?: boolean,
enableCheckNameRegex?: boolean,
sourceMeta?: string | undefined | null,
}
): Promise<{ address: string, jwt: string, password?: string | null }> => {
// trim whitespace and remove special characters
@@ -180,19 +182,30 @@ export const newAddress = async (
// create address
name = name + "@" + domain;
try {
const { success } = await c.env.DB.prepare(
`INSERT INTO address(name) VALUES(?)`
).bind(name).run();
if (!success) {
// Try insert with source_meta field first
const result = await c.env.DB.prepare(
`INSERT INTO address(name, source_meta) VALUES(?, ?)`
).bind(name, sourceMeta).run();
if (!result.success) {
throw new Error("Failed to create address")
}
await updateAddressUpdatedAt(c, name);
} catch (e) {
const message = (e as Error).message;
if (message && message.includes("UNIQUE")) {
// Fallback: source_meta field may not exist, try without it
if (message && message.includes("source_meta")) {
const result = await c.env.DB.prepare(
`INSERT INTO address(name) VALUES(?)`
).bind(name).run();
if (!result.success) {
throw new Error("Failed to create address")
}
await updateAddressUpdatedAt(c, name);
} else if (message && message.includes("UNIQUE")) {
throw new Error("Address already exists")
} else {
throw new Error("Failed to create address")
}
throw new Error("Failed to create address")
}
const address_id = await c.env.DB.prepare(
`SELECT id FROM address where name = ?`