mirror of
https://github.com/4ev-link/4ev.link.git
synced 2026-01-14 08:38:39 +00:00
Feat: send ntfy notify on link create
This commit is contained in:
@@ -1,70 +1,35 @@
|
|||||||
const genSlug = l =>
|
const genSlug = l => [...Array(l)]
|
||||||
[...Array(l)]
|
.map(()=>"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"[Math.random()*62|0])
|
||||||
.map(
|
|
||||||
() =>
|
|
||||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"[
|
|
||||||
(Math.random() * 62) | 0
|
|
||||||
]
|
|
||||||
)
|
|
||||||
.join("");
|
.join("");
|
||||||
|
|
||||||
const RESERVED = new Set([
|
const RESERVED = new Set([
|
||||||
"api",
|
"api","dash","admin","login","logout","signin","signup","register","account",
|
||||||
"dash",
|
"settings","profile","password","user","users","link","links","url","urls",
|
||||||
"admin",
|
"robots","sitemap","favicon","well-known","assets","static","img","js","css","public"
|
||||||
"login",
|
|
||||||
"logout",
|
|
||||||
"signin",
|
|
||||||
"signup",
|
|
||||||
"register",
|
|
||||||
"account",
|
|
||||||
"settings",
|
|
||||||
"profile",
|
|
||||||
"password",
|
|
||||||
"user",
|
|
||||||
"users",
|
|
||||||
"link",
|
|
||||||
"links",
|
|
||||||
"url",
|
|
||||||
"urls",
|
|
||||||
"robots",
|
|
||||||
"sitemap",
|
|
||||||
"favicon",
|
|
||||||
"well-known",
|
|
||||||
"assets",
|
|
||||||
"static",
|
|
||||||
"img",
|
|
||||||
"js",
|
|
||||||
"css",
|
|
||||||
"public",
|
|
||||||
]);
|
]);
|
||||||
|
const ntfy = (env,topic,tags,msg,p=3) =>
|
||||||
const notify = (env, title, message, level = 2) =>
|
env.NTFY_TOPIC ?
|
||||||
env.NTFY_TOPIC
|
fetch(`https://ntfy.sh/${topic}`,{
|
||||||
? fetch(`https://ntfy.sh/${env.NTFY_TOPIC}`, {
|
|
||||||
method:"POST",
|
method:"POST",
|
||||||
headers:{
|
headers:{
|
||||||
"Content-Type": "text/plain",
|
"Title":tags,
|
||||||
Title: title,
|
"Priority":String(p),
|
||||||
Priority: String(level),
|
"Content-Type":"text/plain"
|
||||||
},
|
},
|
||||||
body: message,
|
body:msg
|
||||||
}).catch(() => {})
|
}).catch(()=>{}) :
|
||||||
: Promise.resolve();
|
Promise.resolve();
|
||||||
|
|
||||||
export async function onRequestPost({ request, env }) {
|
export async function onRequestPost({ request, env }) {
|
||||||
try {
|
try {
|
||||||
const { "g-recaptcha-response":token, ...body } = await request.json();
|
const { "g-recaptcha-response":token, ...body } = await request.json();
|
||||||
|
|
||||||
const vR = await fetch(
|
const vR = await fetch(
|
||||||
"https://www.google.com/recaptcha/api/siteverify",
|
"https://www.google.com/recaptcha/api/siteverify",
|
||||||
{
|
{
|
||||||
method:"POST",
|
method:"POST",
|
||||||
headers:{ "Content-Type":"application/x-www-form-urlencoded" },
|
headers:{ "Content-Type":"application/x-www-form-urlencoded" },
|
||||||
body: `secret=${env.RECAPCHA_KEY}&response=${token}`,
|
body:`secret=${env.RECAPCHA_KEY}&response=${token}`
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!(await vR.json()).success)
|
if (!(await vR.json()).success)
|
||||||
return new Response("CAPTCHA verification failed.",{ status:403 });
|
return new Response("CAPTCHA verification failed.",{ status:403 });
|
||||||
|
|
||||||
@@ -72,66 +37,51 @@ export async function onRequestPost({ request, env }) {
|
|||||||
if (!destination_url || !username || !pass_hash)
|
if (!destination_url || !username || !pass_hash)
|
||||||
return new Response("Missing fields",{ status:400 });
|
return new Response("Missing fields",{ status:400 });
|
||||||
|
|
||||||
const user = await env.D1_EV.prepare(
|
const user = await env.D1_EV
|
||||||
"SELECT pass_hash, custom_slugs FROM users WHERE username = ?"
|
.prepare("SELECT pass_hash, custom_slugs FROM users WHERE username = ?")
|
||||||
)
|
|
||||||
.bind(username)
|
.bind(username)
|
||||||
.first();
|
.first();
|
||||||
|
|
||||||
if (user?.pass_hash !== pass_hash)
|
if (user?.pass_hash !== pass_hash)
|
||||||
return new Response("Invalid credentials",{ status:401 });
|
return new Response("Invalid credentials",{ status:401 });
|
||||||
|
|
||||||
let finalSlug = slug;
|
let finalSlug = slug;
|
||||||
|
|
||||||
if (finalSlug) {
|
if (finalSlug) {
|
||||||
if (
|
if (
|
||||||
RESERVED.has(finalSlug.toLowerCase()) ||
|
RESERVED.has(finalSlug.toLowerCase()) ||
|
||||||
!/^[a-zA-Z0-9-]{3,32}$/.test(finalSlug) ||
|
!/^[a-zA-Z0-9-]{3,32}$/.test(finalSlug) ||
|
||||||
(await env.KV_EV.get(finalSlug))
|
await env.KV_EV.get(finalSlug)
|
||||||
)
|
) return new Response("Invalid or taken slug",{ status:400 });
|
||||||
return new Response("Invalid or taken slug", { status: 400 });
|
|
||||||
} else {
|
} else {
|
||||||
do {
|
do { finalSlug = genSlug(6) }
|
||||||
finalSlug = genSlug(6);
|
while (await env.KV_EV.get(finalSlug));
|
||||||
} while (await env.KV_EV.get(finalSlug));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let url = destination_url.startsWith("http")
|
let url = destination_url.startsWith("http")
|
||||||
? destination_url
|
? destination_url
|
||||||
: `https://${destination_url}`;
|
: `https://${destination_url}`;
|
||||||
|
try { new URL(url) }
|
||||||
try {
|
catch { return new Response("Invalid destination URL",{ status:400 }) }
|
||||||
new URL(url);
|
|
||||||
} catch {
|
|
||||||
return new Response("Invalid destination URL", { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const dest_no_proto = url.replace(/^https?:\/\//,"");
|
const dest_no_proto = url.replace(/^https?:\/\//,"");
|
||||||
|
|
||||||
let slugs;
|
let slugs;
|
||||||
try {
|
try { slugs = JSON.parse(user.custom_slugs) } catch {}
|
||||||
slugs = JSON.parse(user.custom_slugs);
|
|
||||||
} catch {}
|
|
||||||
|
|
||||||
const newSlugs = Array.isArray(slugs) ? slugs : [];
|
const newSlugs = Array.isArray(slugs) ? slugs : [];
|
||||||
newSlugs.push(finalSlug);
|
newSlugs.push(finalSlug);
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
env.KV_EV.put(finalSlug,dest_no_proto),
|
env.KV_EV.put(finalSlug,dest_no_proto),
|
||||||
env.D1_EV.prepare(
|
env.D1_EV
|
||||||
"UPDATE users SET custom_slugs = ? WHERE username = ?"
|
.prepare("UPDATE users SET custom_slugs = ? WHERE username = ?")
|
||||||
)
|
|
||||||
.bind(JSON.stringify(newSlugs),username)
|
.bind(JSON.stringify(newSlugs),username)
|
||||||
.run(),
|
.run(),
|
||||||
]);
|
ntfy(
|
||||||
|
|
||||||
env.NTFY_TOPIC &&
|
|
||||||
notify(
|
|
||||||
env,
|
env,
|
||||||
"4ev.link: link created",
|
env.NTFY_TOPIC,
|
||||||
`user=${username} slug=${finalSlug} dest=${dest_no_proto}`,
|
"link-create",
|
||||||
2
|
`event=create\nuser=${username}\nslug=${finalSlug}\ndestination=${dest_no_proto}`,
|
||||||
);
|
3
|
||||||
|
)
|
||||||
|
]);
|
||||||
|
|
||||||
return Response.json({ slug:finalSlug },{ status:201 });
|
return Response.json({ slug:finalSlug },{ status:201 });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user