From e8910d6ed3b0406daa90331d13ec61572482df82 Mon Sep 17 00:00:00 2001 From: obfuscated-loop <107592590+obfuscated-loop@users.noreply.github.com> Date: Wed, 13 Mar 2024 23:28:18 +0000 Subject: [PATCH 1/3] Format code --- middleware.js | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/middleware.js b/middleware.js index 10ea7ab..b8fba5d 100644 --- a/middleware.js +++ b/middleware.js @@ -2,24 +2,28 @@ 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){ +export async function middleware(req) { const ua = userAgent(req)?.ua; - const source = ["Mozilla/5.0 (compatible; Discordbot/","Twitterbot/"].find(u=>ua?.startsWith(u)) + 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 an user (or an user on Discord)."), - footer:{ - text:"Requested page: "+page.slice(0,500), - }, - }], - }),headers:{"content-type":"application/json"},method:"POST"}) - if(source){ + + await fetch(webhook, { + body: JSON.stringify({ + embeds: [{ + title: "Triggered view-logger", + description: (source ? "Source user-agent: " + ua : "It was loaded 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{ + 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)); + return NextResponse.rewrite(new URL("/page.html", req.url)); } } From fd7f9286b06558b85141b3810bca8f5856f83e8b Mon Sep 17 00:00:00 2001 From: obfuscated-loop <107592590+obfuscated-loop@users.noreply.github.com> Date: Thu, 14 Mar 2024 00:43:21 +0000 Subject: [PATCH 2/3] Improved UA tag search, cleaned code and removed redundancy --- middleware.js | 53 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/middleware.js b/middleware.js index b8fba5d..359cbc3 100644 --- a/middleware.js +++ b/middleware.js @@ -1,29 +1,42 @@ -import { NextRequest, NextResponse, userAgent } from 'next/server'; +import { NextRequest, NextResponse, userAgent } from 'next/server' const webhook = process.env.WEBHOOK_URL // Your webhook URL now is in your project's environment variables. +// This format allows for multiple different useragent tags to be added per source +// TODO: Add more sources to this object +const sourcesToUserAgentTag = { + 'Discord': ['Discordbot'], + 'Twitter': ['Twitterbot'] +} + export async function middleware(req) { - const ua = userAgent(req)?.ua; - 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 an user (or an user on Discord)."), - footer: { - text: "Requested page: " + page.slice(0, 500), - }, - }], - }), headers: { "content-type": "application/json" }, method: "POST" - }) + const userAgentString = userAgent(req)?.ua + + for (const [sourceName, sourceUserAgentTags] of Object.entries(sourcesToUserAgentTag)) { + if (sourceUserAgentTags.some(tag => userAgentString.includes(tag))) { + var source = sourceName + break + } + } + + // Only send a webhook and image if it's a verified source + if (typeof source !== 'undefined') { + await fetch(webhook, { + body: JSON.stringify({ + embeds: [{ + title: 'Triggered view-logger', + description: 'A user just viewed your message!', + footer: { + text: `Source: ${source}`, + }, + }], + }), headers: { 'content-type': 'application/json' }, method: 'POST' + }) - if (source) { // Return the image. - return NextResponse.rewrite(new URL("/mini.png", req.url)) + 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)); + return NextResponse.rewrite(new URL('/page.html', req.url)) } -} +} \ No newline at end of file From 556f62984d778f114c20dc6a61c898a8d167a679 Mon Sep 17 00:00:00 2001 From: obfuscated-loop <107592590+obfuscated-loop@users.noreply.github.com> Date: Thu, 14 Mar 2024 00:59:37 +0000 Subject: [PATCH 3/3] Add page path to embed --- middleware.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/middleware.js b/middleware.js index 359cbc3..0fe4afc 100644 --- a/middleware.js +++ b/middleware.js @@ -27,7 +27,7 @@ export async function middleware(req) { title: 'Triggered view-logger', description: 'A user just viewed your message!', footer: { - text: `Source: ${source}`, + text: `Source: ${source}\nPage: ${req.nextUrl.pathname}`, }, }], }), headers: { 'content-type': 'application/json' }, method: 'POST'