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

@@ -3,7 +3,7 @@
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<title>CfPage-test</title> <title>Edge Tunnel VLESS CF</title>
<base href="/" /> <base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />

View File

@@ -33,6 +33,13 @@
} }
} }
}, },
"serve-cf-page": {
"executor": "nx:run-commands",
"options": {
"command": "wrangler pages dev dist/apps/cf-page"
},
"dependsOn": ["^build"]
},
"test": { "test": {
"executor": "@nrwl/vite:test", "executor": "@nrwl/vite:test",
"outputs": ["{projectRoot}/coverage"], "outputs": ["{projectRoot}/coverage"],

View File

@@ -1,13 +1,14 @@
import { StrictMode } from 'react'; import { StrictMode } from 'react';
import * as ReactDOM from 'react-dom/client'; import * as ReactDOM from 'react-dom/client';
import App from './app/app'; // import App from './app/app';
import { EdgeApp } from 'edge-ui';
const root = ReactDOM.createRoot( const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement document.getElementById('root') as HTMLElement
); );
root.render( root.render(
<StrictMode> <StrictMode>
<App /> <EdgeApp />
</StrictMode> </StrictMode>
); );

View File

@@ -9,6 +9,7 @@ module.exports = {
'{src,pages,components}/**/*!(*.stories|*.spec).{ts,tsx,html}' '{src,pages,components}/**/*!(*.stories|*.spec).{ts,tsx,html}'
), ),
...createGlobPatternsForDependencies(__dirname), ...createGlobPatternsForDependencies(__dirname),
'libs/edge-ui/src/lib/*.tsx',
], ],
theme: { theme: {
extend: {}, extend: {},

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>) { async function errorHandling(context: EventContext<any, any, any>) {
try { try {
@@ -9,18 +11,29 @@ async function errorHandling(context: EventContext<any, any, any>) {
} }
function authentication(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 // skip authentication
if (skipUrls.filter((url) => context.request.url.endsWith(url))) { if (skipUrls.filter((url) => context.request.url.endsWith(url)).length) {
return context.next(); return context.next();
} }
const basicAuth = context.request.headers.get("Authorization") || ""; const basicAuth = context.request.headers.get('Authorization') || '';
const authString = basicAuth.split(" ")?.[1] || ""; const authString = basicAuth.split(' ')?.[1] || '';
if (!atob(authString).includes("test")) { if (!atob(authString).includes(userID)) {
return new Response(``, { return new Response(``, {
status: 401, status: 401,
headers: { headers: {
"content-type": "text/html; charset=utf-8", 'content-type': 'text/html; charset=utf-8',
"WWW-Authenticate": "Basic", 'WWW-Authenticate': 'Basic',
}, },
}); });
} else { } else {

View File

@@ -3,5 +3,6 @@ interface Env {
} }
export const onRequest: PagesFunction<Env> = async (context) => { export const onRequest: PagesFunction<Env> = async (context) => {
console.log('xxxxx', context.env);
return new Response(`Hello, world! ${context.request.url}`); return new Response(`Hello, world! ${context.request.url}`);
}; };

1
functions/setup.md Normal file
View File

@@ -0,0 +1 @@
for local env, create `.dev.vars` env file

36
functions/util.ts Normal file
View File

@@ -0,0 +1,36 @@
const index401 = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>401 - UUID Not Valid</title>
</head>
<body>
<h1 style="color: red;">Not set valid UUID in Environment Variables.</h1>
<h2>Please use tool to generate and <span style="color: red;">remember</span> UUID or use this one <span
style="color: blue;" id="uuidSpan"></span>
</h2>
<h3> You must use same UUID for login this page after config valid UUID Environment Variables
</h3>
<h2>Please refer to <a
href="https://github.com/zizifn/edgetunnel/blob/main/doc/edge-tunnel-deno.md#%E6%B5%81%E7%A8%8B%E6%BC%94%E7%A4%BA">deno
deploy guide</a>
</h2>
<h3>Or maybe check below <a
href="https://raw.githubusercontent.com/zizifn/edgetunnel/main/doc/deno-deploy2.gif">GIF</a> </h3>
<img src="https://raw.githubusercontent.com/zizifn/edgetunnel/main/doc/deno-deploy2.gif" alt="guide" srcset="">
<script>
let uuid = URL.createObjectURL(new Blob([])).substr(-36);
document.getElementById('uuidSpan').textContent = uuid
</script>
</body>
</html>`;
export { index401 };