mirror of
https://github.com/multipleof4/4ev.link.git
synced 2026-01-13 23:57:55 +00:00
Feat: Implement reCAPTCHA v2 verification
This commit is contained in:
@@ -5,11 +5,14 @@ export async function onRequest({request,env,next}){
|
|||||||
const u=new URL(request.url)
|
const u=new URL(request.url)
|
||||||
if(u.pathname==='/api/create'&&request.method==='POST'){
|
if(u.pathname==='/api/create'&&request.method==='POST'){
|
||||||
try{
|
try{
|
||||||
let{url:t}=await request.json();t=n(t);if(!t)throw 0
|
let{url:t,token:k}=await request.json()
|
||||||
|
if(!k||!(await(await fetch('https://www.google.com/recaptcha/api/siteverify',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},body:`secret=${env.RECAPCHA_KEY}&response=${k}`})).json()).success)
|
||||||
|
return new Response('CAPTCHA failed',{status:403,headers:H})
|
||||||
|
t=n(t);if(!t)throw 0
|
||||||
let s;do s=r();while(await env.EV.get(s))
|
let s;do s=r();while(await env.EV.get(s))
|
||||||
await env.EV.put(s,t)
|
await env.EV.put(s,t)
|
||||||
return new Response(JSON.stringify({slug:s,target:t,shortUrl:`${u.origin}/${s}`}),{headers:{'Content-Type':'application/json',...H}})
|
return new Response(JSON.stringify({slug:s,target:t,shortUrl:`${u.origin}/${s}`}),{headers:{'Content-Type':'application/json',...H}})
|
||||||
}catch(_){return new Response('Invalid URL',{status:400,headers:H})}
|
}catch(_){return new Response('Invalid request',{status:400,headers:H})}
|
||||||
}
|
}
|
||||||
const p=u.pathname.slice(1)
|
const p=u.pathname.slice(1)
|
||||||
if(p){const t=await env.EV.get(p);if(t)return Response.redirect(t,302)}
|
if(p){const t=await env.EV.get(p);if(t)return Response.redirect(t,302)}
|
||||||
|
|||||||
Reference in New Issue
Block a user