-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v2 endpoint exchange info with JSON, so that we can ensure client's forward compatibility easily by ignoring unknown fields.
- Loading branch information
1 parent
53fcb8d
commit 4c2bff3
Showing
1 changed file
with
34 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,37 @@ | ||
import { serve } from "https://deno.land/[email protected]/http/server.ts"; | ||
import { serve, type ConnInfo } from "https://deno.land/[email protected]/http/server.ts"; | ||
|
||
|
||
interface ClientInfo { | ||
priAddr: string, | ||
chanName: string, | ||
} | ||
|
||
async function handleExchangeV2(req: Request, connInfo: ConnInfo): Promise<Response> { | ||
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<Response> { | ||
async function handleExchangeV1(req: Request, connInfo: ConnInfo): Promise<Response> { | ||
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<Response> { | |
`, | ||
{ 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 }) | ||
} | ||
|