From 16921924af64b15a8cc2e544df2521c6a2a55fae Mon Sep 17 00:00:00 2001 From: zizifn <1803942+zizifn@users.noreply.github.com> Date: Wed, 14 Dec 2022 00:53:06 +0800 Subject: [PATCH] add basic auth in index page --- apps/deno-vless/src/deno/client.ts | 37 ++++++++++++++++-------------- apps/deno-vless/src/main.ts | 12 ++++++++++ doc/edge-tunnel-deno.md | 7 +++--- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/apps/deno-vless/src/deno/client.ts b/apps/deno-vless/src/deno/client.ts index 23bb1d1..5e703ba 100644 --- a/apps/deno-vless/src/deno/client.ts +++ b/apps/deno-vless/src/deno/client.ts @@ -3,9 +3,6 @@ import { serveFile, } from 'https://deno.land/std@0.167.0/http/file_server.ts'; async function serveClient(req: Request, basePath: string) { - for await (const entry of Deno.readDir('.')) { - console.log(entry); - } const pathname = new URL(req.url).pathname; if (pathname.startsWith('/assets')) { const resp = await serveDir(req, { @@ -19,21 +16,27 @@ async function serveClient(req: Request, basePath: string) { req, `${Deno.cwd()}/apps/deno-vless/src/client/index.html` ); - // Do dynamic responses - // const indexHtml = await Deno.readFile(`${Deno.cwd()}/client/index.html`); - // return new Response(indexHtml, { - // headers: { - // 'content-type': 'text/html; charset=utf-8', - // }, - // }); } - - return new Response(``, { - status: 404, - headers: { - 'content-type': 'text/html; charset=utf-8', - }, - }); + const basicAuth = req.headers.get('Authorization') || ''; + const authString = basicAuth.split(' ')?.[1] || ''; + if (atob(authString).includes(basePath)) { + console.log('302'); + return new Response(``, { + status: 302, + headers: { + 'content-type': 'text/html; charset=utf-8', + Location: `./${basePath}`, + }, + }); + } else { + return new Response(``, { + status: 401, + headers: { + 'content-type': 'text/html; charset=utf-8', + 'WWW-Authenticate': 'Basic', + }, + }); + } } export { serveClient }; diff --git a/apps/deno-vless/src/main.ts b/apps/deno-vless/src/main.ts index d074913..f01c184 100644 --- a/apps/deno-vless/src/main.ts +++ b/apps/deno-vless/src/main.ts @@ -4,8 +4,20 @@ import { chunk, join } from 'https://jspm.dev/lodash-es'; import { serveClient } from './deno/client.ts'; const userID = Deno.env.get('UUID') || ''; +let isVaildUser = validate(userID); +if (!isVaildUser) { + console.log('not set valid UUID'); +} const handler = async (req: Request): Promise => { + if (!isVaildUser) { + return new Response(``, { + status: 401, + headers: { + 'content-type': 'text/html; charset=utf-8', + }, + }); + } const upgrade = req.headers.get('upgrade') || ''; if (upgrade.toLowerCase() != 'websocket') { return await serveClient(req, userID); diff --git a/doc/edge-tunnel-deno.md b/doc/edge-tunnel-deno.md index cb9119f..0ccd750 100644 --- a/doc/edge-tunnel-deno.md +++ b/doc/edge-tunnel-deno.md @@ -36,11 +36,10 @@ https://raw.githubusercontent.com/zizifn/edgetunnel/main/doc/deno-deploy2.gif ## 项目地址 -点击 View 项目会自动打开。一开始返回 `404`. 不要慌张, 把你设置的 `UUID` 添加到 URL 后面。 +点击 View 项目会自动打开。一开始返回 `401`. -下面是示例,请把 URL 和 UUID 换成自己的。 -`https://xxxxxx.deno.dev/015897c1-663d-4b2a-9f90-053b189cdc47`。 +不要慌张, 把你设置的 `UUID` 输入到弹出的用户名或者密码中。 -如果出现下面的界面,都代表一切正常。 +然后会自动跳转到如下界面。 ![index](./index.jpg)