| 51 | | { type: 'not_found' }; |
| 52 | |
| 53 | function routeUrl(url: URL, env: Env): RouteDecision { |
| 54 | const searchParams = new URLSearchParams(url.search); |
| 55 | |
| 56 | if (searchParams.has('t')) { |
| 57 | const name = searchParams.get('t'); |
| 58 | |
| 59 | if (name) { |
| 60 | return { type: 'tunnel', name }; |
| 61 | } |
| 62 | |
| 63 | return { type: 'management' }; |
| 64 | } |
| 65 | |
| 66 | if (!url.host.includes(env.WORKER_HOST)) { |
| 67 | return { type: 'not_found' }; |
| 68 | } |
| 69 | |
| 70 | const parts = url.host.split('.'); |
| 71 | |
| 72 | if (parts.length === 2) { |
| 73 | return { type: 'management' }; |
| 74 | } |
| 75 | |
| 76 | const tunnelName = parts[0]; |
| 77 | |
| 78 | if (tunnelName === 'admin') { |
| 79 | return { type: 'management' }; |
| 80 | } |
| 81 | |
| 82 | if (parts.length === 3) { |
| 83 | return { type: 'tunnel', name: parts[0] }; |
| 84 | } |
| 85 | |
| 86 | return { type: 'not_found' }; |
| 87 | } |
| 88 | |
| 89 | async function handleManagementRequest(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> { |
| 90 | const authHeader = request.headers.get('authorization'); |