Skip to content

Commit

Permalink
Improved UA tag search, cleaned code and removed redundancy
Browse files Browse the repository at this point in the history
  • Loading branch information
obfuscated-loop committed Mar 14, 2024
1 parent e8910d6 commit fd7f928
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions middleware.js
Original file line number Diff line number Diff line change
@@ -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))
}
}
}

0 comments on commit fd7f928

Please sign in to comment.