| 1 | import { NextResponse } from 'next/server'; |
| 2 | import type { NextRequest, NextFetchEvent } from 'next/server'; |
| 3 | |
| 4 | export function middleware(request: NextRequest, event: NextFetchEvent) { |
| 5 | const url = request.nextUrl.pathname; |
| 6 | |
| 7 | const ip = request.headers.get('cf-connecting-ip') || request.headers.get('x-forwarded-for') || '127.0.0.1'; |
| 8 | const userAgent = request.headers.get('user-agent') || 'Unknown OSIRIS Client'; |
| 9 | |
| 10 | const basePayload = { |
| 11 | hostname: request.nextUrl.hostname, |
| 12 | language: "en-US", |
| 13 | referrer: request.headers.get('referer') || "", |
| 14 | screen: "1920x1080", |
| 15 | title: "OSIRIS", |
| 16 | url: url, |
| 17 | website: process.env.UMAMI_WEBSITE_ID || "cd8f216c-fc3f-45f5-ba1a-e10309a61d18" |
| 18 | }; |
| 19 | |
| 20 | const pageView = fetch('http://umami-umami-1:3000/api/send', { |
| 21 | method: 'POST', |
| 22 | headers: { 'Content-Type': 'application/json', 'User-Agent': userAgent, 'x-forwarded-for': ip }, |
| 23 | body: JSON.stringify({ payload: basePayload, type: "event" }) |
| 24 | }).catch(() => {}); |
| 25 | |
| 26 | const ipEvent = fetch('http://umami-umami-1:3000/api/send', { |
| 27 | method: 'POST', |
| 28 | headers: { 'Content-Type': 'application/json', 'User-Agent': userAgent, 'x-forwarded-for': ip }, |
| 29 | body: JSON.stringify({ |
| 30 | payload: { ...basePayload, name: "Network Log", data: { IP: ip } }, |
| 31 | type: "event" |
| 32 | }) |
| 33 | }).catch(() => {}); |
| 34 | |
| 35 | event.waitUntil(Promise.all([pageView, ipEvent])); |
| 36 | |
| 37 | return NextResponse.next(); |
| 38 | } |
| 39 | |
| 40 | export const config = { |
| 41 | matcher: [ |
| 42 | '/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)', |
| 43 | ], |
| 44 | } |