-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproxy.ts
More file actions
278 lines (237 loc) · 7.94 KB
/
Copy pathproxy.ts
File metadata and controls
278 lines (237 loc) · 7.94 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { getToken } from 'next-auth/jwt'
import {
handleBotRequest,
isBotRequest,
} from './packages/lib/middleware/bot-handler'
import {
FILE_URL_PATTERN,
PROTECTED_PAGE_PATHS,
SUPERADMIN_PATHS,
VIDEO_EXTENSIONS,
} from './packages/lib/middleware/constants'
import { Permission, hasPermission } from './packages/lib/permissions'
declare global {
var __nextAuthLoginContext: Record<string, any>
}
if (!globalThis.__nextAuthLoginContext) {
globalThis.__nextAuthLoginContext = {}
}
// Computed once per isolate, not on every request
const BASE_URL = process.env.NEXT_PUBLIC_BASE_URL || 'https://embrly.ca'
const MAIN_HOST = new URL(BASE_URL).hostname
const VIDEO_EXTENSIONS_SET = new Set(VIDEO_EXTENSIONS)
const ALPHA_CUTOFF_DATE = new Date('2025-12-27T00:00:00.000Z')
function getClientIP(request: NextRequest): string | undefined {
const forwarded = request.headers.get('x-forwarded-for')
if (forwarded) return forwarded.split(',')[0]?.trim()
return (
request.headers.get('x-real-ip') ??
request.headers.get('cf-connecting-ip') ??
request.headers.get('x-client-ip') ??
undefined
)
}
function getGeoInfo(request: NextRequest) {
const country =
request.headers.get('x-vercel-ip-country') ||
request.headers.get('cf-ipcountry') ||
null
const city =
request.headers.get('x-vercel-ip-city') ||
request.headers.get('cf-ipcity') ||
null
return { country, city }
}
export async function proxy(request: NextRequest) {
const pathname = request.nextUrl.pathname
// Trim trailing slash without regex
const normalizedPathname =
pathname.length > 1 && pathname.endsWith('/')
? pathname.slice(0, -1)
: pathname
const incomingHost = request.headers.get('host')?.replace(/:\d+$/, '')
if (
incomingHost &&
incomingHost !== MAIN_HOST &&
incomingHost !== 'localhost'
) {
if (pathname === '/') {
try {
const internalBase = `http://localhost:${process.env.PORT || 3000}`
const lookupUrl = new URL('/api/internal/domain-lookup', internalBase)
lookupUrl.searchParams.set('hostname', incomingHost)
const res = await fetch(lookupUrl.toString())
if (res.ok) {
const data = await res.json()
if (data.found && data.profileSlug) {
return NextResponse.rewrite(
new URL(`/user/${data.profileSlug}`, request.url)
)
}
}
} catch (e) {
console.error('[Proxy] Custom domain lookup failed:', e)
}
}
}
if (
pathname === '/api/auth/callback/credentials' &&
request.method === 'POST'
) {
const ip = getClientIP(request)
const { country, city } = getGeoInfo(request)
const userAgent = request.headers.get('user-agent')
const contextKey = `login_context:${Date.now()}`
globalThis.__nextAuthLoginContext[contextKey] = {
ip: ip || undefined,
userAgent: userAgent || undefined,
geo: country || city ? { country, city } : null,
}
const now = Date.now()
for (const key in globalThis.__nextAuthLoginContext) {
try {
const ts = parseInt(key.split(':')[1])
if (now - ts > 60000) {
delete globalThis.__nextAuthLoginContext[key]
}
} catch {}
}
}
let tokenPromise: Promise<null | Record<string, any>> | null = null
const getAuthToken = () => {
if (!tokenPromise) {
tokenPromise = getToken({ req: request }) as Promise<null | Record<
string,
any
>>
}
return tokenPromise
}
const isAlphaMigrationPage = pathname === '/auth/alpha-migration'
const isAlphaMigrationApi = pathname === '/api/auth/alpha-migration'
const isNextAuthRoute = pathname.startsWith('/api/auth/')
const isApiRoute = pathname.startsWith('/api/')
const isFileUrl =
FILE_URL_PATTERN.test(normalizedPathname) &&
!normalizedPathname.endsWith('/raw') &&
!normalizedPathname.endsWith('/direct')
if (isFileUrl) {
const fileExt = normalizedPathname.split('.').pop()?.toLowerCase()
const rangeHeader = request.headers.get('range')
const acceptHeader = request.headers.get('accept') || ''
const isMediaRequest =
rangeHeader != null ||
(acceptHeader !== '' && !acceptHeader.includes('text/html'))
if (fileExt && VIDEO_EXTENSIONS_SET.has(fileExt) && isMediaRequest) {
const url = new URL(request.url)
url.pathname = `${normalizedPathname}/raw`
return NextResponse.rewrite(url)
}
if (pathname === normalizedPathname) {
const userAgent = request.headers.get('user-agent') || ''
if (!isBotRequest(userAgent)) {
const url = new URL(request.url)
url.pathname = `${pathname}/`
return NextResponse.rewrite(url)
}
}
}
const token = await getAuthToken()
if (token) {
const createdAt = token.createdAt ? new Date(token.createdAt) : null
const isPreCutoffUser = createdAt && createdAt < ALPHA_CUTOFF_DATE
const hasVerifiedEmail = token.emailVerified === true
const needsMigration = isPreCutoffUser && !hasVerifiedEmail
if (
needsMigration &&
!isAlphaMigrationPage &&
!isAlphaMigrationApi &&
!isNextAuthRoute &&
!isApiRoute
) {
return NextResponse.redirect(new URL('/auth/alpha-migration', BASE_URL))
}
}
const isVerifyEmailPage = pathname === '/auth/verify-email'
const isVerifyEmailApi = pathname === '/api/auth/verify-email'
const isAuthPage = pathname.startsWith('/auth/')
if (token) {
const isEmailVerified = !!token.emailVerified
if (
!isEmailVerified &&
!isVerifyEmailPage &&
!isVerifyEmailApi &&
!isAuthPage &&
!isNextAuthRoute &&
!isApiRoute
) {
return NextResponse.redirect(new URL('/auth/verify-email', BASE_URL))
}
}
const isProfileSecurityTab =
pathname === '/me' && request.nextUrl.searchParams.get('tab') === 'security'
const isProfilePath = pathname === '/me'
const isDashboardRoot = pathname === '/dashboard'
if (token && token.passwordBreachDetectedAt) {
if (isProfileSecurityTab) {
return NextResponse.next()
}
if (
isDashboardRoot ||
(isProfilePath && !request.nextUrl.searchParams.get('tab'))
) {
return NextResponse.redirect(new URL('/me?tab=security', BASE_URL))
}
}
if (
normalizedPathname.endsWith('/raw') ||
normalizedPathname.endsWith('/direct') ||
pathname.startsWith('/u/')
) {
return NextResponse.next()
}
if (pathname.startsWith('/api/')) {
return NextResponse.next()
}
if (pathname.startsWith('/auth/') || pathname.startsWith('/setup')) {
return NextResponse.next()
}
const ensureAuthenticated = async () => {
const t = await getAuthToken()
if (!t) {
return NextResponse.redirect(new URL('/auth/login', BASE_URL))
}
return { token: t }
}
if (pathname.startsWith('/admin/') || pathname === '/admin') {
const auth = await ensureAuthenticated()
if (auth instanceof NextResponse) return auth
const role = auth.token?.role
const isSuperAdminRoute = SUPERADMIN_PATHS.some((path) =>
pathname.startsWith(path)
)
if (isSuperAdminRoute) {
if (!hasPermission(role as any, Permission.PERFORM_SUPERADMIN_ACTIONS)) {
return NextResponse.redirect(new URL('/dashboard', BASE_URL))
}
} else if (!hasPermission(role as any, Permission.ACCESS_ADMIN_PANEL)) {
return NextResponse.redirect(new URL('/dashboard', BASE_URL))
}
}
if (PROTECTED_PAGE_PATHS.some((p) => pathname.startsWith(p))) {
const t = await getAuthToken()
if (!t) {
return NextResponse.redirect(new URL('/auth/login', BASE_URL))
}
}
const botResponse = await handleBotRequest(request)
if (botResponse) return botResponse
return NextResponse.next()
}
export const config = {
matcher: [
'/((?!_next/static|_next/image|_next/webpack-hmr|favicon.ico|icon.svg|.*\\.css|.*\\.js|.*\\.woff|.*\\.woff2|.*\\.ttf|.*\\.otf).*)',
],
}