Elffuss BraidHub

kikocis/osiris public

master · 1.2 KiBRawPermalink
1import { NextResponse } from 'next/server';
2
3export async function GET(req: Request) {
4 const { searchParams } = new URL(req.url);
5 const mac = searchParams.get('mac');
6
7 if (!mac) {
8 return NextResponse.json({ error: 'Missing MAC parameter' }, { status: 400 });
9 }
10
11 // Clean the MAC address format to allow varied inputs
12 const cleanMac = mac.trim().toUpperCase().replace(/[^A-F0-9:-]/g, '');
13
14 try {
15 const res = await fetch(`https://macvendors.co/api/${encodeURIComponent(cleanMac)}`, {
16 signal: AbortSignal.timeout(8000),
17 headers: { 'Accept': 'application/json' }
18 });
19
20 if (!res.ok) {
21 throw new Error(`MacVendors API HTTP ${res.status}`);
22 }
23
24 const data = await res.json();
25 if (data && data.result && data.result.company) {
26 return NextResponse.json({
27 mac: cleanMac,
28 vendor: data.result.company,
29 address: data.result.address,
30 prefix: data.result.mac_prefix
31 });
32 } else {
33 return NextResponse.json({ mac: cleanMac, vendor: 'Not Found' });
34 }
35 } catch (error: any) {
36 return NextResponse.json({ error: 'MAC lookup failed', detail: error.message }, { status: 502 });
37 }
38}