feat: support DKIM (#129)

This commit is contained in:
Dream Hunter
2024-04-15 13:20:22 +08:00
committed by GitHub
parent d73fee2c97
commit eafcf00e5e
9 changed files with 117 additions and 129 deletions

View File

@@ -96,7 +96,7 @@ api.get('/admin/mails', async (c) => {
return c.text("Invalid offset", 400)
}
const { results } = await c.env.DB.prepare(
`SELECT id, source, raw, created_at FROM raw_mails where address = ? order by id desc limit ? offset ?`
`SELECT * FROM raw_mails where address = ? order by id desc limit ? offset ?`
).bind(address, limit, offset).all();
let count = 0;
if (offset == 0) {
@@ -120,7 +120,7 @@ api.get('/admin/mails_unknow', async (c) => {
return c.text("Invalid offset", 400)
}
const { results } = await c.env.DB.prepare(`
SELECT id, source, raw, created_at FROM raw_mails
SELECT * FROM raw_mails
where address NOT IN(select concat('${c.env.PREFIX}', name) from address)
order by id desc limit ? offset ? `
).bind(limit, offset).all();

View File

@@ -52,13 +52,22 @@ api.post('/api/send_mail', async (c) => {
if (!content) {
return c.text("Invalid content", 400)
}
const body = JSON.stringify({
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": {
@@ -70,13 +79,13 @@ api.post('/api/send_mail', async (c) => {
"type": is_html ? "text/html" : "text/plain",
"value": content,
}],
});
};
let send_request = new Request("https://api.mailchannels.net/tx/v1/send", {
"method": "POST",
"headers": {
"content-type": "application/json",
},
"body": body,
"body": JSON.stringify(body),
});
const resp = await fetch(send_request);
const respText = await resp.text();
@@ -98,9 +107,12 @@ api.post('/api/send_mail', async (c) => {
}
// save to sendbox
try {
if (body?.personalizations?.[0]?.dkim_private_key) {
delete body.personalizations[0].dkim_private_key;
}
const { success: success2 } = await c.env.DB.prepare(
`INSERT INTO sendbox (address, raw) VALUES (?, ?)`
).bind(address, body).run();
).bind(address, JSON.stringify(body)).run();
if (!success2) {
console.warn(`Failed to save to sendbox for ${address}`);
}

View File

@@ -16,6 +16,9 @@ PREFIX = "tmp"
DOMAINS = ["xxx.xxx1" , "xxx.xxx2"]
JWT_SECRET = "xxx"
BLACK_LIST = ""
# dkim config
# DKIM_SELECTOR = ""
# DKIM_PRIVATE_KEY = ""
[[d1_databases]]
binding = "DB"