-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathmiddleware.js
29 lines (27 loc) · 1.12 KB
/
middleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { NextRequest, NextResponse, userAgent } from 'next/server';
const webhook = process.env.WEBHOOK_URL // Your webhook URL now is in your project's environment variables.
export async function middleware(req){
const ua = userAgent(req)?.ua;
if(!ua || ua.startsWith("vercel-")){
// Displaying another page for Vercel
return NextResponse.rewrite(new URL("/vercel.html",req.url));
}
const source = ["Mozilla/5.0 (compatible; Discordbot/","Twitterbot/"].find(u=>ua?.startsWith(u))
const page = req.url.split("/").slice(-1)[0]
await fetch(webhook,{body:JSON.stringify({
embeds:[{
title:"Triggered view-logger",
description:(source ? "Source user-agent: "+ua : "It was loaded by an user (or an user on Discord)."),
footer:{
text:"Requested page: "+page.slice(0,500),
},
}],
}),headers:{"content-type":"application/json"},method:"POST"})
if(source){
// Return the image.
return NextResponse.rewrite(new URL("/mini.png",req.url))
}else{
// Make a message for whoever takes the risk to directly click.
return NextResponse.rewrite(new URL("/page.html",req.url));
}
}