Skip to content

Commit

Permalink
检测alpnProtocol
Browse files Browse the repository at this point in the history
  • Loading branch information
masx200 committed Jul 6, 2022
1 parent 180748a commit 387044d
Show file tree
Hide file tree
Showing 18 changed files with 467 additions and 405 deletions.
7 changes: 7 additions & 0 deletions ConnInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type ConnInfo = Readonly<{
/** The local address of the connection. */
readonly localAddr: Deno.Addr;
/** The remote address of the connection. */
readonly remoteAddr: Deno.Addr;
alpnProtocol: string;
}>;
8 changes: 8 additions & 0 deletions Handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ConnInfo } from "./ConnInfo.ts";

// }

export type Handler = (
request: Request,
connInfo: ConnInfo,
) => Response | Promise<Response>;
7 changes: 7 additions & 0 deletions Handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Handler } from "./Handler.ts";

export type Handlers = Partial<{
request: Handler;
connect: Handler;
upgrade: Handler;
}>;
19 changes: 19 additions & 0 deletions ServeHttpInit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ConnInfo } from "./ConnInfo.ts";
import { Handler } from "./Handler.ts";

export type ServeHttpInit = Partial<
Deno.ListenOptions & {
onNotFound?: Handler;
signal?: AbortSignal;
port?: number;
hostname?: string;

onError?: (
// deno-lint-ignore no-explicit-any
reason: any,
request: Request,
connInfo: ConnInfo,
) => Response | PromiseLike<Response>;
onListen?: (params: { hostname: string; port: number }) => void;
}
>;
53 changes: 53 additions & 0 deletions ServeHttpsInit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { ConnInfo } from "./ConnInfo.ts";
import { Handler } from "./Handler.ts";

// function upgrade(req: Request, connInfo: ConnInfo) {
// const { socket, response } = Deno.upgradeWebSocket(req);
// const { url, headers, method } = req;
// const data = {
// ...connInfo,
// url,
// method,
// headers: Object.fromEntries(headers),
// };
// const body = JSON.stringify(data);
// console.log("upgrade", body);
// socket.addEventListener("open", () => {
// socket.send(body);
// socket.close();
// });
// return response;
// }
// async function main() {
// let { port, hostname, certFile, keyFile } = parse(Deno.args);
// if (port || hostname || certFile || keyFile) {
// port ??= 8000;
// hostname ??= "0.0.0.0";
// await serve_https(
// { request: handler, upgrade },
// { port, hostname, certFile, keyFile }
// );
// } else {
// console.log("Listening on http://localhost:8000");
// await serve(handler, { hostname: "0.0.0.0", port: 8000 });
// }
// }

export type ServeHttpsInit = Partial<
Deno.ListenTlsOptions & {
onNotFound?: Handler;
signal?: AbortSignal;
port?: number;
hostname?: string;
certFile?: string;
keyFile?: string;

onError?: (
// deno-lint-ignore no-explicit-any
reason: any,
request: Request,
connInfo: ConnInfo,
) => Response | PromiseLike<Response>;
onListen?: (params: { hostname: string; port: number }) => void;
}
>;
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"tasks": {
"udd": "udd '*/*.ts' '*.ts'"
"udd": "udd '*/*.ts' '*.ts'",
"example": "deno run --unstable -A example.ts"
},
"lint": {},
"fmt": {
Expand Down
8 changes: 4 additions & 4 deletions example.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
copy,
writeAll,
} from "https://deno.land/std@0.147.0/streams/conversion.ts";
import { ConnInfo, serve_http, serve_https } from "./mod.ts";
export function upgrade(req: Request, connInfo: ConnInfo) {
const { socket, response } = Deno.upgradeWebSocket(req);
Expand Down Expand Up @@ -77,10 +81,6 @@ export async function connect(
}
export const handlers = { upgrade, connect, request: handler };

import {
copy,
writeAll,
} from "https://deno.land/std@0.147.0/streams/conversion.ts";
export const key = await (
await fetch("https://unpkg.com/self-signed-cert@1.0.1/key.pem")
).text();
Expand Down
6 changes: 6 additions & 0 deletions hostnameForDisplay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function hostnameForDisplay(hostname: string) {
// If the hostname is "0.0.0.0", we display "localhost" in console
// because browsers in Windows don't resolve "0.0.0.0".
// See the discussion in https://github.com/denoland/deno_std/issues/1165
return hostname === "0.0.0.0" ? "localhost" : hostname;
}
6 changes: 6 additions & 0 deletions is_connect_or_upgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function is_connect_or_upgrade(request: Request) {
return (
request.method === "CONNECT" ||
request.headers.get("Connection")?.toLowerCase() === "upgrade"
);
}
Loading

0 comments on commit 387044d

Please sign in to comment.