mirror of
https://github.com/lush2020/edgetunnel.git
synced 2026-03-24 00:48:39 +08:00
31
functions/_middleware.ts
Normal file
31
functions/_middleware.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
const skipUrls = ["ws"];
|
||||
|
||||
async function errorHandling(context: EventContext<any, any, any>) {
|
||||
try {
|
||||
return await context.next();
|
||||
} catch (err) {
|
||||
return new Response(`${err.message}\n${err.stack}`, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
function authentication(context: EventContext<any, any, any>) {
|
||||
// skip authentication
|
||||
if (skipUrls.filter((url) => context.request.url.endsWith(url))) {
|
||||
return context.next();
|
||||
}
|
||||
const basicAuth = context.request.headers.get("Authorization") || "";
|
||||
const authString = basicAuth.split(" ")?.[1] || "";
|
||||
if (!atob(authString).includes("test")) {
|
||||
return new Response(``, {
|
||||
status: 401,
|
||||
headers: {
|
||||
"content-type": "text/html; charset=utf-8",
|
||||
"WWW-Authenticate": "Basic",
|
||||
},
|
||||
});
|
||||
} else {
|
||||
return context.next();
|
||||
}
|
||||
}
|
||||
|
||||
export const onRequest = [errorHandling, authentication];
|
||||
Reference in New Issue
Block a user