-
Notifications
You must be signed in to change notification settings - Fork 0
Add Tunnel Nodes system for managing remote nodes #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { NextResponse } from 'next/server'; | ||
| import { query } from '@/lib/db'; | ||
| import { z } from 'zod'; | ||
| import { handleApiError } from '@/lib/api-utils'; | ||
| import crypto from 'crypto'; | ||
|
|
||
| export const dynamic = 'force-dynamic'; | ||
|
|
||
| const TunnelNodeSchema = z.object({ | ||
| name: z.string().min(1).max(255), | ||
| location: z.string().min(1).max(255), | ||
| country_code: z.string().max(10).optional(), | ||
| flag_emoji: z.string().max(10).optional(), | ||
| remote_ip: z.string().min(1), | ||
| tunnel_port: z.number().int().min(1).max(65535).default(443), | ||
| tunnel_type: z.enum(['wss', 'grpc', 'quic', 'h2']).default('wss'), | ||
| local_forward_port: z.number().int().min(1).max(65535), | ||
| sni_host: z.string().default('www.google.com'), | ||
| }); | ||
|
|
||
| // Generate a secure random secret for tunnel authentication | ||
| function generateTunnelSecret(): string { | ||
| return crypto.randomBytes(32).toString('base64url'); | ||
| } | ||
|
|
||
| export async function GET() { | ||
| try { | ||
| const nodes = await query(` | ||
| SELECT * FROM tunnel_nodes | ||
| ORDER BY created_at DESC | ||
| `); | ||
| return NextResponse.json(nodes); | ||
| } catch (error) { | ||
| return handleApiError(error); | ||
| } | ||
| } | ||
|
|
||
| export async function POST(req: Request) { | ||
| try { | ||
| const body = await req.json(); | ||
| const validated = TunnelNodeSchema.parse(body); | ||
|
|
||
| const tunnel_secret = generateTunnelSecret(); | ||
|
|
||
| const result: any = await query( | ||
| `INSERT INTO tunnel_nodes | ||
| (name, location, country_code, flag_emoji, remote_ip, tunnel_port, tunnel_type, tunnel_secret, local_forward_port, sni_host, status) | ||
| VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'pending')`, | ||
| [ | ||
| validated.name, | ||
| validated.location, | ||
| validated.country_code || null, | ||
| validated.flag_emoji || null, | ||
| validated.remote_ip, | ||
| validated.tunnel_port, | ||
| validated.tunnel_type, | ||
| tunnel_secret, | ||
| validated.local_forward_port, | ||
| validated.sni_host, | ||
| ] | ||
| ); | ||
|
|
||
| // Return the created node with the generated secret | ||
| const newNode = await query('SELECT * FROM tunnel_nodes WHERE id = ?', [result.insertId]); | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| node: (newNode as any[])[0], | ||
| tunnel_secret | ||
| }); | ||
| } catch (error) { | ||
| return handleApiError(error); | ||
| } | ||
| } | ||
|
|
||
| export async function DELETE(req: Request) { | ||
| try { | ||
| const { searchParams } = new URL(req.url); | ||
| const id = searchParams.get('id'); | ||
| if (!id) throw new Error('ID required'); | ||
|
|
||
| await query('DELETE FROM tunnel_nodes WHERE id = ?', [id]); | ||
| return NextResponse.json({ success: true }); | ||
| } catch (error) { | ||
| return handleApiError(error); | ||
| } | ||
| } | ||
|
|
||
| export async function PATCH(req: Request) { | ||
| try { | ||
| const { searchParams } = new URL(req.url); | ||
| const id = searchParams.get('id'); | ||
| if (!id) throw new Error('ID required'); | ||
|
|
||
| const body = await req.json(); | ||
| const { status, is_active } = body; | ||
|
|
||
| if (status) { | ||
| await query('UPDATE tunnel_nodes SET status = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?', [status, id]); | ||
| } | ||
|
|
||
| if (typeof is_active === 'boolean') { | ||
| await query('UPDATE tunnel_nodes SET is_active = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?', [is_active ? 1 : 0, id]); | ||
| } | ||
|
|
||
| return NextResponse.json({ success: true }); | ||
| } catch (error) { | ||
| return handleApiError(error); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The list endpoint selects and returns all columns from
tunnel_nodes, which includestunnel_secret. This secret is the relay authentication credential used in generated commands, so exposing it in a broad list response allows anyone with API access to impersonate a tunnel node. The listing should omittunnel_secretand only return it through a narrowly scoped privileged flow.Useful? React with 👍 / 👎.