From 4fde353447c9ceeadaf5ab0359a4264991a480a0 Mon Sep 17 00:00:00 2001 From: zizifn <1803942+zizifn@users.noreply.github.com> Date: Fri, 19 May 2023 22:04:31 +0800 Subject: [PATCH] add uuid into path --- functions/_middleware.ts | 28 ++++++++++++++++++----- functions/{ => sample}/connect.ts | 1 + functions/{ => sample}/hello.ts | 0 functions/{ => sample}/http2.ts | 0 functions/{ => sample}/ws.ts | 3 ++- functions/util.ts | 21 +++++++++++++---- functions/{vless.ts => vless/[wspath].ts} | 20 ++++++++++++---- nx.json | 2 +- 8 files changed, 58 insertions(+), 17 deletions(-) rename functions/{ => sample}/connect.ts (97%) rename functions/{ => sample}/hello.ts (100%) rename functions/{ => sample}/http2.ts (100%) rename functions/{ => sample}/ws.ts (86%) rename functions/{vless.ts => vless/[wspath].ts} (94%) diff --git a/functions/_middleware.ts b/functions/_middleware.ts index dc7ff81..21f8c35 100644 --- a/functions/_middleware.ts +++ b/functions/_middleware.ts @@ -1,6 +1,5 @@ -import { index401 } from './util'; +import { index401, page404 } from './util'; import { parse, stringify, validate } from 'uuid'; -const skipUrls = ['ws', 'assets', 'http2', 'connect', 'vless']; async function errorHandling(context: EventContext) { try { @@ -10,7 +9,16 @@ async function errorHandling(context: EventContext) { } } -function authentication(context: EventContext) { +async function authentication( + context: EventContext< + any, + any, + { + digestUUID: string; + } + > +) { + // context.data It’s an arbitrary object you can attach data to that will persist during the request. The most common use-cases are for middleware that handles auth and may need to set context.data.username or similar. // if not set UUID, return 401 page const userID = context.env['UUID'] || ''; let isVaildUser = validate(userID); @@ -23,8 +31,8 @@ function authentication(context: EventContext) { }); } // skip authentication + const url = new URL(context.request.url); if ( - skipUrls.filter((url) => context.request.url.includes(url)).length || // if url has uuid, skip auth context.request.url.includes(userID) ) { @@ -44,16 +52,24 @@ function authentication(context: EventContext) { } else { const url = new URL(context.request.url); if (url.pathname === '/') { + const wspath = `/vless/${userID}`; return new Response(``, { status: 302, headers: { 'content-type': 'text/html; charset=utf-8', - Location: `./${userID}?wspath=${encodeURIComponent('/vless')}`, + Location: `./${userID}?wspath=${encodeURIComponent(wspath)}`, }, }); - } else { + } + if (url.pathname.startsWith('/assets')) { return context.next(); } + return new Response(page404, { + status: 404, + headers: { + 'content-type': 'text/html; charset=utf-8', + }, + }); } } diff --git a/functions/connect.ts b/functions/sample/connect.ts similarity index 97% rename from functions/connect.ts rename to functions/sample/connect.ts index 6f996bf..a7dcaaa 100644 --- a/functions/connect.ts +++ b/functions/sample/connect.ts @@ -3,6 +3,7 @@ import { connect } from 'cloudflare:sockets'; export const onRequest: PagesFunction = async (context) => { + context.params.user; console.log('start fetch'); const socket = connect({ hostname: 'neverssl.com', diff --git a/functions/hello.ts b/functions/sample/hello.ts similarity index 100% rename from functions/hello.ts rename to functions/sample/hello.ts diff --git a/functions/http2.ts b/functions/sample/http2.ts similarity index 100% rename from functions/http2.ts rename to functions/sample/http2.ts diff --git a/functions/ws.ts b/functions/sample/ws.ts similarity index 86% rename from functions/ws.ts rename to functions/sample/ws.ts index 3e41d15..00ea14d 100644 --- a/functions/ws.ts +++ b/functions/sample/ws.ts @@ -2,7 +2,8 @@ interface Env { KV: KVNamespace; } -export const onRequest: PagesFunction = async ({ request }) => { +export const onRequest: PagesFunction = async ({ request, data }) => { + console.log(data); const upgradeHeader = request.headers.get('Upgrade'); if (!upgradeHeader || upgradeHeader !== 'websocket') { return new Response('Expected Upgrade: websocket', { status: 426 }); diff --git a/functions/util.ts b/functions/util.ts index b1ee6ec..3705b17 100644 --- a/functions/util.ts +++ b/functions/util.ts @@ -34,10 +34,23 @@ const index401 = ` `; const page404 = ` - - -

404

There's nothing here.
Take me home
+ +404 Not Found + +

404 Not Found

+
nginx/1.23.4
+ `; -export { index401, page404 }; +async function digestMessage(message: string) { + const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array + const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8); // hash the message + const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array + const hashHex = hashArray + .map((b) => b.toString(16).padStart(2, '0')) + .join(''); // convert bytes to hex string + return hashHex; +} + +export { index401, page404, digestMessage }; diff --git a/functions/vless.ts b/functions/vless/[wspath].ts similarity index 94% rename from functions/vless.ts rename to functions/vless/[wspath].ts index 4c5f375..4d7921f 100644 --- a/functions/vless.ts +++ b/functions/vless/[wspath].ts @@ -4,7 +4,7 @@ import { vlessJs, } from 'vless-js'; import { connect } from 'cloudflare:sockets'; -import { page404 } from './util'; +import { page404 } from '../util'; interface Env { KV: KVNamespace; @@ -12,21 +12,31 @@ interface Env { } export const onRequest: PagesFunction = async (context) => { + const userID = context.env['UUID']; + if (context.params.wspath !== userID) { + return new Response(``, { + status: 401, + headers: { + 'content-type': 'text/html; charset=utf-8', + 'WWW-Authenticate': 'Basic', + }, + }); + } + console.log(context.params.wspath); let address = ''; let portWithRandomLog = ''; - const userID = context.env['UUID']; const log = (info: string, event?: any) => { console.log(`[${address}:${portWithRandomLog}] ${info}`, event || ''); }; const upgradeHeader = context.request.headers.get('Upgrade'); + // index page if (!upgradeHeader || upgradeHeader !== 'websocket') { - return new Response(``, { - status: 401, + return new Response(`need Upgrade to ws`, { + status: 200, headers: { 'content-type': 'text/html; charset=utf-8', - 'WWW-Authenticate': 'Basic', }, }); } diff --git a/nx.json b/nx.json index 3ca03bd..f9b4448 100644 --- a/nx.json +++ b/nx.json @@ -3,7 +3,7 @@ "npmScope": "edge-bypass", "tasksRunnerOptions": { "default": { - "runner": "nx-cloud", + "runner": "nx/tasks-runners/default", "options": { "cacheableOperations": ["build", "lint", "test", "e2e"] }