-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
143 lines (134 loc) · 4.58 KB
/
proxy.ts
File metadata and controls
143 lines (134 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { NextRequest, NextResponse } from "next/server"
import { isAccessAllowed, getClientIp } from "@/lib/access-control"
import { checkRateLimit } from "@/lib/rate-limit"
function blockedHtml(clientIp: string): string {
// Sanitize IP for safe HTML embedding (strip anything not valid in an IP)
const safeIp = clientIp.replace(/[^a-fA-F0-9.:]/g, "")
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Access Denied - Voltgres</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: #09090b;
color: #fafafa;
}
.container {
text-align: center;
max-width: 420px;
padding: 2rem;
}
.icon {
width: 48px;
height: 48px;
margin: 0 auto 1.5rem;
border-radius: 12px;
background: rgba(239, 68, 68, 0.1);
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
user-select: none;
-webkit-user-select: none;
}
.icon svg { width: 24px; height: 24px; color: #ef4444; }
h1 { font-size: 1.25rem; font-weight: 600; margin-bottom: 0.5rem; }
p { color: #a1a1aa; font-size: 0.875rem; line-height: 1.5; }
.code { margin-top: 1rem; color: #52525b; font-size: 0.75rem; font-family: monospace; }
.ip { margin-top: 0.75rem; color: #52525b; font-size: 0.75rem; font-family: monospace; display: none; }
</style>
</head>
<body>
<div class="container">
<div class="icon" id="lock">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M16.5 10.5V6.75a4.5 4.5 0 1 0-9 0v3.75m-.75 11.25h10.5a2.25 2.25 0 0 0 2.25-2.25v-6.75a2.25 2.25 0 0 0-2.25-2.25H6.75a2.25 2.25 0 0 0-2.25 2.25v6.75a2.25 2.25 0 0 0 2.25 2.25Z" />
</svg>
</div>
<h1>Access Denied</h1>
<p>Your IP address is not authorized to access this Voltgres instance. Contact the administrator if you believe this is an error.</p>
<p class="code">403 Forbidden</p>
<p class="ip" id="ip">Your IP: ${safeIp}</p>
</div>
<script>
var c=0;document.getElementById("lock").onclick=function(){if(++c>=5)document.getElementById("ip").style.display="block"}
</script>
</body>
</html>`
}
export async function proxy(req: NextRequest) {
const { pathname } = req.nextUrl
// Always allow health check endpoint (for uptime monitors)
if (pathname === "/api/health") {
return NextResponse.next()
}
// Access control check
const allowed = await isAccessAllowed(req)
if (!allowed) {
const clientIp = getClientIp(req)
return new NextResponse(blockedHtml(clientIp), {
status: 403,
headers: { "Content-Type": "text/html" },
})
}
// Rate limit setup and SQL endpoints (auth endpoints are rate-limited by Better Auth)
if (pathname === "/api/setup") {
const ip = getClientIp(req)
const limit = checkRateLimit(`setup:${ip}`, { max: 5, windowMs: 60_000 })
if (!limit.allowed) {
return NextResponse.json(
{ error: "Too many requests. Please try again later." },
{
status: 429,
headers: {
"Retry-After": String(Math.ceil(limit.resetMs / 1000)),
},
}
)
}
}
// Rate limit SQL execution endpoint
if (pathname === "/api/pg/sql") {
const ip = getClientIp(req)
const limit = checkRateLimit(`sql:${ip}`, { max: 30, windowMs: 60_000 })
if (!limit.allowed) {
return NextResponse.json(
{ error: "Too many requests. Please try again later." },
{
status: 429,
headers: {
"Retry-After": String(Math.ceil(limit.resetMs / 1000)),
},
}
)
}
}
// Block public registration when disabled (both UI and API)
const allowRegistration = process.env.ALLOW_REGISTRATION === "true"
if (!allowRegistration) {
if (pathname === "/signup") {
return NextResponse.redirect(new URL("/login", req.url))
}
// Block all Better Auth sign-up API endpoints
if (pathname.startsWith("/api/auth/sign-up")) {
return NextResponse.json(
{ error: "Registration is disabled" },
{ status: 403 }
)
}
}
return NextResponse.next()
}
export const config = {
matcher: [
"/((?!_next/static|_next/image|favicon.ico|icon-|apple-icon).*)",
],
}