add basic auth in index page

This commit is contained in:
zizifn
2022-12-14 00:53:06 +08:00
committed by zizifn
parent 25b6d3e744
commit 16921924af
3 changed files with 35 additions and 21 deletions

View File

@@ -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 };

View File

@@ -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<Response> => {
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);

View File

@@ -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)