From 4c2bff30c9bb32c31f034620821ff6d2b978b23f Mon Sep 17 00:00:00 2001 From: Contextualist Date: Sun, 12 Feb 2023 16:40:10 -0800 Subject: [PATCH] feat: edge: v2 exchange endpoint v2 endpoint exchange info with JSON, so that we can ensure client's forward compatibility easily by ignoring unknown fields. --- edge/index.ts | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/edge/index.ts b/edge/index.ts index 58663e2..f6542fa 100644 --- a/edge/index.ts +++ b/edge/index.ts @@ -1,8 +1,37 @@ -import { serve } from "https://deno.land/std@0.171.0/http/server.ts"; +import { serve, type ConnInfo } from "https://deno.land/std@0.171.0/http/server.ts"; + + +interface ClientInfo { + priAddr: string, + chanName: string, +} + +async function handleExchangeV2(req: Request, connInfo: ConnInfo): Promise { + if (req.method != "POST") + return new Response("Invalid method", { status: 405 }) + if (req.headers.get("Content-Type") != "application/octet-stream") + return new Response("Invalid content type", { status: 415 }) + + const pubAddr = joinHostPort(connInfo.remoteAddr) + const conn = req.body!.getReader({ mode: "byob" }) + const { priAddr, chanName }: ClientInfo = JSON.parse( + new TextDecoder().decode(await receivePacket(conn)) + ) + const x0 = JSON.stringify({ pubAddr, priAddr }) + //console.log(`accepted from ${x0}`) + + const x1 = await exchange(chanName, x0, conn) + if (x1 == "") + return new Response("") + //console.log(`exchanged, got ${x1}`) + + const msg = marshallPacket(new TextEncoder().encode(x1)) + return new Response(msg) +} // (priAddr0|chanName) -> pubAddr1|priAddr1 -async function handleExchange(req: Request, connInfo: ConnInfo): Promise { +async function handleExchangeV1(req: Request, connInfo: ConnInfo): Promise { if (req.method != "POST") return new Response("Invalid method", { status: 405 }) if (req.headers.get("Content-Type") != "application/octet-stream") @@ -117,8 +146,10 @@ async function handler(req: Request, connInfo: ConnInfo): Promise { `, { headers: { "Content-Type": "text/plain; charset=utf-8" } } ) + case "/v2/exchange": + return await handleExchangeV2(req, connInfo) case "/exchange": - return await handleExchange(req, connInfo) + return await handleExchangeV1(req, connInfo) default: return new Response("Not found", { status: 404 }) }