From a5edc7677cb4447a8d21d377dd07531515e3012b Mon Sep 17 00:00:00 2001 From: Leo dev Date: Mon, 13 Oct 2025 23:23:11 +0200 Subject: [PATCH 1/3] Server by id (api) --- src/app/api/server/[id]/route.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/app/api/server/[id]/route.ts diff --git a/src/app/api/server/[id]/route.ts b/src/app/api/server/[id]/route.ts new file mode 100644 index 0000000..f695cac --- /dev/null +++ b/src/app/api/server/[id]/route.ts @@ -0,0 +1,27 @@ +import { NextRequest, NextResponse } from "next/server"; +import { supabase } from "../../supa"; +import { StatusCodes } from "http-status-codes"; + +/** + * GET /api/server/ + */ +export async function GET( + req: NextRequest, + { params }: { params: Promise<{ id: string }> } +) { + const { id } = await params; + + const { data: profile } = await supabase + .from("servers") + .select("id, created_at, owner, address") + .eq("id", id) + .maybeSingle(); + + if (!profile) + return NextResponse.json( + { message: "not found" }, + { status: StatusCodes.NOT_FOUND } + ); + + return NextResponse.json({ message: "ok", ...profile }); +} From 7f8482dc5a1a324761dd69abc590b978211f434d Mon Sep 17 00:00:00 2001 From: Leo dev Date: Mon, 13 Oct 2025 23:41:54 +0200 Subject: [PATCH 2/3] Working auth (hopefully) --- src/app/api/auth/route.ts | 47 +++++++++++++++++++++++--------------- src/components/Sidebar.tsx | 4 +--- src/lib/auth.ts | 29 +++++++---------------- 3 files changed, 37 insertions(+), 43 deletions(-) diff --git a/src/app/api/auth/route.ts b/src/app/api/auth/route.ts index a8757e8..96915e2 100644 --- a/src/app/api/auth/route.ts +++ b/src/app/api/auth/route.ts @@ -4,24 +4,36 @@ import { supabase } from "../supa"; const SERVER_AUTH_TOKENS = new Map< string, - { user_id: string; server_ip: string } + { user_id: string; server_id: string } >(); /** - * GET /api/auth?token= + * GET /api/auth?token=?key= * Called by a (DM node / Server) to verify a temporary relay token. */ export async function GET(req: NextRequest) { const url = new URL(req.url); const token = url.searchParams.get("token"); + const key = url.searchParams.get("key"); - if (!token) { + if (!token || !key) { return NextResponse.json( - { message: "There should be a token parameter" }, + { message: "There should be a token and a key parameter" }, { status: StatusCodes.BAD_REQUEST } ); } + const { data: server } = await supabase + .from("servers") + .select("id, created_at, owner, address") + .eq("key", key) + .maybeSingle(); + if (!server) + return NextResponse.json( + { message: "Key unauthorized" }, + { status: StatusCodes.UNAUTHORIZED } + ); + const auth = SERVER_AUTH_TOKENS.get(token); if (!auth) { return NextResponse.json( @@ -30,19 +42,11 @@ export async function GET(req: NextRequest) { ); } - const ip = - req.headers.get("x-real-ip") || - req.headers.get("x-forwarded-for")?.split(",")[0] || - "127.0.0.1"; - - console.log("Auth request from IP:", ip); - - if (auth.server_ip !== ip && ip !== "::1") { + if (auth.server_id !== server.id) return NextResponse.json( - { message: "Invalid address" }, + { message: "Invalid id" }, { status: StatusCodes.UNAUTHORIZED } ); - } SERVER_AUTH_TOKENS.delete(token); @@ -53,19 +57,24 @@ export async function GET(req: NextRequest) { * POST /api/auth * Called by the client to request a temporary relay token for a DM server. * - * Body: { server_ip: string } + * Body: { server_id: string } * Header: Authorization: Bearer */ export async function POST(req: NextRequest) { - const { server_ip } = await req.json(); + const { server_id } = await req.json(); const authHeader = req.cookies.get("token"); - if (!authHeader) { + if (!server_id) + return NextResponse.json( + { message: "Missing server_id in the json body" }, + { status: StatusCodes.BAD_REQUEST } + ); + + if (!authHeader) return NextResponse.json( { message: "Missing Supabase Authorization header" }, { status: StatusCodes.BAD_REQUEST } ); - } const supabaseToken = authHeader.value; @@ -85,7 +94,7 @@ export async function POST(req: NextRequest) { SERVER_AUTH_TOKENS.set(relayToken, { user_id: user.id, - server_ip: String(server_ip), + server_id, }); return NextResponse.json({ message: "ok", token: relayToken }); diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx index d0cc0d3..cdaf2c6 100644 --- a/src/components/Sidebar.tsx +++ b/src/components/Sidebar.tsx @@ -5,7 +5,6 @@ import { MessageCircle, Plus, Search, - SettingsIcon, Volume2Icon, } from "lucide-react"; import { Server, Channel, Message } from "@/types/types"; @@ -27,10 +26,9 @@ import { Button } from "./ui/button"; import { Card } from "./ui/card"; import { ScrollArea } from "./ui/scroll-area"; import { useRouter } from "next/navigation"; -import Link from "next/link"; import useUser, { UserProfile } from "@/hooks/get-user"; import ProfilePicture from "./ProfilePicture"; -import auth, { makeAddress } from "@/lib/auth"; +import auth from "@/lib/auth"; import { toast } from "sonner"; import { StringMap } from "@/types/typeUtils"; import SettingsDialog from "./settings/SettingsDialog"; diff --git a/src/lib/auth.ts b/src/lib/auth.ts index ad1bd2f..47a5196 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -2,34 +2,21 @@ import { Message, Server } from "@/types/types"; import axios from "axios"; import { Dispatch, RefObject, SetStateAction } from "react"; -async function getIP(domain: string) { - const res = await fetch(`https://dns.google/resolve?name=${domain}&type=A`); - const data = await res.json(); - return data.Answer[0]?.data || null; -} - -export async function makeAddress(ip: string, defaultPort = 7080) { - // Apparently wss doesn't like ports (or maybe the tunnel I'm using) - // return ip.includes(":") ? `${ip}` : `${ip}:${defaultPort}`; - if (ip.match(/^\d\.\d\.\d\.\d/)) { - return ip; - } - - return await getIP(ip); -} - export default async function auth( - ip: string, + id: string, wsRef: RefObject, setServer: Dispatch>, setMessages: Dispatch>, onNewMessage?: (m: Message) => void ) { + console.log("Authenticating with server id:", id); + + const ip = ((await axios.get(`/api/server/${id}`)).data as any) + .address as string; + console.log("Authenticating with server at:", ip); - const server_auth = ( - (await axios.post("/api/auth", { server_ip: await makeAddress(ip) })) - .data as any - ).token; + const server_auth = ((await axios.post("/api/auth", { id })).data as any) + .token; // Open the WebSocket connection const ws = new WebSocket(`wss://${ip}`); From 68ad062523bdad31ca42c618892ab0e255cfdaf9 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Mon, 13 Oct 2025 23:48:57 +0200 Subject: [PATCH 3/3] Fix --- src/lib/auth.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/auth.ts b/src/lib/auth.ts index 47a5196..21ee5a1 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -15,7 +15,7 @@ export default async function auth( .address as string; console.log("Authenticating with server at:", ip); - const server_auth = ((await axios.post("/api/auth", { id })).data as any) + const server_auth = ((await axios.post("/api/auth", { server_id: id })).data as any) .token; // Open the WebSocket connection