Skip to content

Commit

Permalink
add basic auth in index page
Browse files Browse the repository at this point in the history
  • Loading branch information
zizifn committed Dec 13, 2022
1 parent e7451f3 commit 8a32b53
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
37 changes: 20 additions & 17 deletions apps/deno-vless/src/deno/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import {
serveFile,
} from 'https://deno.land/[email protected]/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, {
Expand All @@ -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 };
12 changes: 12 additions & 0 deletions apps/deno-vless/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions doc/edge-tunnel-deno.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 8a32b53

Please sign in to comment.