add uuid into path

This commit is contained in:
zizifn
2023-05-19 22:04:31 +08:00
committed by zizifn
parent a7ed1d77f0
commit 4fde353447
8 changed files with 58 additions and 17 deletions

View File

@@ -1,6 +1,5 @@
import { index401 } from './util'; import { index401, page404 } from './util';
import { parse, stringify, validate } from 'uuid'; import { parse, stringify, validate } from 'uuid';
const skipUrls = ['ws', 'assets', 'http2', 'connect', 'vless'];
async function errorHandling(context: EventContext<any, any, any>) { async function errorHandling(context: EventContext<any, any, any>) {
try { try {
@@ -10,7 +9,16 @@ async function errorHandling(context: EventContext<any, any, any>) {
} }
} }
function authentication(context: EventContext<any, any, any>) { async function authentication(
context: EventContext<
any,
any,
{
digestUUID: string;
}
>
) {
// context.data Its 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 // if not set UUID, return 401 page
const userID = context.env['UUID'] || ''; const userID = context.env['UUID'] || '';
let isVaildUser = validate(userID); let isVaildUser = validate(userID);
@@ -23,8 +31,8 @@ function authentication(context: EventContext<any, any, any>) {
}); });
} }
// skip authentication // skip authentication
const url = new URL(context.request.url);
if ( if (
skipUrls.filter((url) => context.request.url.includes(url)).length ||
// if url has uuid, skip auth // if url has uuid, skip auth
context.request.url.includes(userID) context.request.url.includes(userID)
) { ) {
@@ -44,16 +52,24 @@ function authentication(context: EventContext<any, any, any>) {
} else { } else {
const url = new URL(context.request.url); const url = new URL(context.request.url);
if (url.pathname === '/') { if (url.pathname === '/') {
const wspath = `/vless/${userID}`;
return new Response(``, { return new Response(``, {
status: 302, status: 302,
headers: { headers: {
'content-type': 'text/html; charset=utf-8', '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 context.next();
} }
return new Response(page404, {
status: 404,
headers: {
'content-type': 'text/html; charset=utf-8',
},
});
} }
} }

View File

@@ -3,6 +3,7 @@
import { connect } from 'cloudflare:sockets'; import { connect } from 'cloudflare:sockets';
export const onRequest: PagesFunction<any> = async (context) => { export const onRequest: PagesFunction<any> = async (context) => {
context.params.user;
console.log('start fetch'); console.log('start fetch');
const socket = connect({ const socket = connect({
hostname: 'neverssl.com', hostname: 'neverssl.com',

View File

@@ -2,7 +2,8 @@ interface Env {
KV: KVNamespace; KV: KVNamespace;
} }
export const onRequest: PagesFunction<Env> = async ({ request }) => { export const onRequest: PagesFunction<Env> = async ({ request, data }) => {
console.log(data);
const upgradeHeader = request.headers.get('Upgrade'); const upgradeHeader = request.headers.get('Upgrade');
if (!upgradeHeader || upgradeHeader !== 'websocket') { if (!upgradeHeader || upgradeHeader !== 'websocket') {
return new Response('Expected Upgrade: websocket', { status: 426 }); return new Response('Expected Upgrade: websocket', { status: 426 });

View File

@@ -34,10 +34,23 @@ const index401 = `
</html>`; </html>`;
const page404 = ` const page404 = `
<!DOCTYPE html> <html>
<html lang="en"> <head><title>404 Not Found</title></head>
<div class="theme-default-content"><h1>404</h1><blockquote>There's nothing here.</blockquote><a href="/" class="">Take me home</a></div> <body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.23.4</center>
</body>
</html> </html>
`; `;
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 };

View File

@@ -4,7 +4,7 @@ import {
vlessJs, vlessJs,
} from 'vless-js'; } from 'vless-js';
import { connect } from 'cloudflare:sockets'; import { connect } from 'cloudflare:sockets';
import { page404 } from './util'; import { page404 } from '../util';
interface Env { interface Env {
KV: KVNamespace; KV: KVNamespace;
@@ -12,21 +12,31 @@ interface Env {
} }
export const onRequest: PagesFunction<Env> = async (context) => { export const onRequest: PagesFunction<Env> = 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 address = '';
let portWithRandomLog = ''; let portWithRandomLog = '';
const userID = context.env['UUID'];
const log = (info: string, event?: any) => { const log = (info: string, event?: any) => {
console.log(`[${address}:${portWithRandomLog}] ${info}`, event || ''); console.log(`[${address}:${portWithRandomLog}] ${info}`, event || '');
}; };
const upgradeHeader = context.request.headers.get('Upgrade'); const upgradeHeader = context.request.headers.get('Upgrade');
// index page
if (!upgradeHeader || upgradeHeader !== 'websocket') { if (!upgradeHeader || upgradeHeader !== 'websocket') {
return new Response(``, { return new Response(`need Upgrade to ws`, {
status: 401, status: 200,
headers: { headers: {
'content-type': 'text/html; charset=utf-8', 'content-type': 'text/html; charset=utf-8',
'WWW-Authenticate': 'Basic',
}, },
}); });
} }

View File

@@ -3,7 +3,7 @@
"npmScope": "edge-bypass", "npmScope": "edge-bypass",
"tasksRunnerOptions": { "tasksRunnerOptions": {
"default": { "default": {
"runner": "nx-cloud", "runner": "nx/tasks-runners/default",
"options": { "options": {
"cacheableOperations": ["build", "lint", "test", "e2e"] "cacheableOperations": ["build", "lint", "test", "e2e"]
} }