feat: allow admin and user delete mail, sendbox, send access(only admin) (#329)

This commit is contained in:
Dream Hunter
2024-07-04 13:25:14 +08:00
committed by GitHub
parent 9448b3c754
commit 21fed3fb00
44 changed files with 344 additions and 278 deletions

View File

@@ -127,6 +127,16 @@ api.get('/admin/mails_unknow', async (c) => {
);
});
api.delete('/admin/mails/:id', async (c) => {
const { id } = c.req.param();
const { success } = await c.env.DB.prepare(
`DELETE FROM raw_mails WHERE id = ? `
).bind(id).run();
return c.json({
success: success
})
})
api.get('/admin/address_sender', async (c) => {
const { address, limit, offset } = c.req.query();
if (address) {
@@ -166,6 +176,16 @@ api.post('/admin/address_sender', async (c) => {
})
})
api.delete('/admin/address_sender/:id', async (c) => {
const { id } = c.req.param();
const { success } = await c.env.DB.prepare(
`DELETE FROM address_sender WHERE id = ? `
).bind(id).run();
return c.json({
success: success
})
})
api.get('/admin/sendbox', async (c) => {
const { address, limit, offset } = c.req.query();
if (address) {
@@ -182,6 +202,16 @@ api.get('/admin/sendbox', async (c) => {
);
})
api.delete('/admin/sendbox/:id', async (c) => {
const { id } = c.req.param();
const { success } = await c.env.DB.prepare(
`DELETE FROM sendbox WHERE id = ? `
).bind(id).run();
return c.json({
success: success
})
})
api.get('/admin/statistics', async (c) => {
const { count: mailCount } = await c.env.DB.prepare(
`SELECT count(*) as count FROM raw_mails`

View File

@@ -4,7 +4,7 @@ import { createMimeMessage } from 'mimetext';
import { Resend } from 'resend';
import { CONSTANTS } from '../constants'
import { getJsonSetting, getDomains, getIntValue } from '../utils';
import { getJsonSetting, getDomains, getIntValue, getBooleanValue } from '../utils';
import { GeoData } from '../models'
import { handleListQuery } from '../common'
import { HonoCustomType } from '../types';
@@ -89,64 +89,6 @@ const sendMailByResend = async (
console.log(`Resend success: ${JSON.stringify(data)}`);
}
const sendMailByMailChannels = async (
c: Context<HonoCustomType>, address: string,
reqJson: {
from_name: string, to_mail: string, to_name: string,
subject: string, content: string, is_html: boolean
}
): Promise<void> => {
/* eslint-disable prefer-const */
let {
from_name, to_mail, to_name,
subject, content, is_html
} = reqJson;
/* eslint-enable prefer-const */
from_name = from_name || address;
to_name = to_name || to_mail;
let dmikBody = {}
if (c.env.DKIM_SELECTOR && c.env.DKIM_PRIVATE_KEY && address.includes("@")) {
dmikBody = {
"dkim_domain": address.split("@")[1],
"dkim_selector": c.env.DKIM_SELECTOR,
"dkim_private_key": c.env.DKIM_PRIVATE_KEY,
}
}
const body = {
"personalizations": [
{
"to": [{
"email": to_mail,
"name": to_name,
}],
...dmikBody,
}
],
"from": {
"email": address,
"name": from_name,
},
"subject": subject,
"content": [{
"type": is_html ? "text/html" : "text/plain",
"value": content,
}],
};
const send_request = new Request("https://api.mailchannels.net/tx/v1/send", {
"method": "POST",
"headers": {
"content-type": "application/json",
},
"body": JSON.stringify(body),
});
const resp = await fetch(send_request);
const respText = await resp.text();
console.log(resp.status + " " + resp.statusText + ": " + respText);
if (resp.status >= 300) {
throw new Error(`Mailchannels error: ${resp.status} ${respText}`);
}
}
export const sendMail = async (
c: Context<HonoCustomType>, address: string,
reqJson: {
@@ -208,9 +150,8 @@ export const sendMail = async (
else if (resendEnabled) {
await sendMailByResend(c, address, reqJson);
}
// send by mailchannels
else {
await sendMailByMailChannels(c, address, reqJson);
throw new Error("Please enable resend or verified address list")
}
// update balance
if (!sendByVerifiedAddressList) {
@@ -292,3 +233,17 @@ api.get('/api/sendbox', async (c) => {
const { limit, offset } = c.req.query();
return getSendbox(c, address, limit, offset);
})
api.delete('/api/sendbox/:id', async (c) => {
if (!getBooleanValue(c.env.ENABLE_USER_DELETE_EMAIL)) {
return c.text("User delete email is disabled", 403)
}
const { address } = c.get("jwtPayload")
const { id } = c.req.param();
const { success } = await c.env.DB.prepare(
`DELETE FROM sendbox WHERE address = ? and id = ? `
).bind(address, id).run();
return c.json({
success: success
})
})

View File

@@ -33,10 +33,6 @@ export type Bindings = {
S3_BUCKET: string | undefined
S3_URL_EXPIRES: number | undefined
// dkim
DKIM_SELECTOR: string | undefined
DKIM_PRIVATE_KEY: string | undefined
// cf turnstile
CF_TURNSTILE_SITE_KEY: string | undefined
CF_TURNSTILE_SECRET_KEY: string | undefined

View File

@@ -47,9 +47,6 @@ ENABLE_AUTO_REPLY = false
# Turnstile verification
# CF_TURNSTILE_SITE_KEY = ""
# CF_TURNSTILE_SECRET_KEY = ""
# dkim config
# DKIM_SELECTOR = ""
# DKIM_PRIVATE_KEY = ""
# telegram bot
# TG_MAX_ACCOUNTS = 5
# global forward address list, if set, all emails will be forwarded to these addresses