init edge tunnel

This commit is contained in:
zizifn
2022-12-04 03:42:48 +08:00
committed by zizifn
parent 6a3b43efcb
commit d4937e4106
52 changed files with 22530 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
{
"name": "deno-bypass",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/deno-bypass/src",
"projectType": "application",
"targets": {
"run": {
"executor": "nx:run-commands",
"options": {
"command": "deno run --allow-net apps/deno-bypass/src/bypass.ts "
}
},
"serve": {
"executor": "nx:run-commands",
"options": {
"command": "deno run --allow-net --watch apps/deno-bypass/src/bypass.ts "
}
}
},
"tags": []
}

View File

@@ -0,0 +1,44 @@
import { serve } from 'https://deno.land/std@0.157.0/http/server.ts';
const userID = Deno.env.get('UUID');
const handler = async (request: Request): Promise<Response> => {
const headers = request.headers;
const serverAddress = headers.get('x-host') || '';
const remotePort = headers.get('x-port') || 443;
const uuid = headers.get('x-uuid');
if (!serverAddress || !remotePort || !userID) {
return new Response(
`Version 0.0.1-2022/12/03!!
${userID ? 'has UUID env' : 'no UUID env'}
感谢 deno deploy 严肃对待 web standard。支持 HTTP request & response streaming。
`,
{
status: 200,
headers: {},
}
);
}
console.log(
`want to proxy to server address ${serverAddress}, and port ${remotePort}`
);
if (uuid !== userID) {
return new Response('Do not send right UUID!', {
status: 403,
headers: {},
});
}
const connection = await Deno.connect({
port: Number(remotePort),
hostname: serverAddress,
});
const proxyResp = request.body?.pipeThrough(connection);
return new Response(proxyResp, {
status: 200,
headers: {},
});
};
serve(handler, { port: 8080, hostname: '0.0.0.0' });