add cf page

This commit is contained in:
zizifn
2023-01-02 04:03:26 +08:00
committed by zizifn
parent fed5ce3ad7
commit 31924d8a83
8 changed files with 70 additions and 10 deletions

View File

@@ -1,4 +1,6 @@
const skipUrls = ["ws"];
import { index401 } from './util';
import { parse, stringify, validate } from 'uuid';
const skipUrls = ['ws'];
async function errorHandling(context: EventContext<any, any, any>) {
try {
@@ -9,18 +11,29 @@ async function errorHandling(context: EventContext<any, any, any>) {
}
function authentication(context: EventContext<any, any, any>) {
// if not set UUID, return 401 page
const userID = context.env['UUID'] || '';
let isVaildUser = validate(userID);
if (!isVaildUser) {
return new Response(index401, {
status: 401,
headers: {
'content-type': 'text/html; charset=utf-8',
},
});
}
// skip authentication
if (skipUrls.filter((url) => context.request.url.endsWith(url))) {
if (skipUrls.filter((url) => context.request.url.endsWith(url)).length) {
return context.next();
}
const basicAuth = context.request.headers.get("Authorization") || "";
const authString = basicAuth.split(" ")?.[1] || "";
if (!atob(authString).includes("test")) {
const basicAuth = context.request.headers.get('Authorization') || '';
const authString = basicAuth.split(' ')?.[1] || '';
if (!atob(authString).includes(userID)) {
return new Response(``, {
status: 401,
headers: {
"content-type": "text/html; charset=utf-8",
"WWW-Authenticate": "Basic",
'content-type': 'text/html; charset=utf-8',
'WWW-Authenticate': 'Basic',
},
});
} else {