-
Notifications
You must be signed in to change notification settings - Fork 514
Block freebuff waiting room for disallowed countries #522
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import geoip from 'geoip-lite' | ||
|
|
||
| import type { NextRequest } from 'next/server' | ||
|
|
||
| export const FREE_MODE_ALLOWED_COUNTRIES = new Set([ | ||
| 'US', 'CA', | ||
| 'GB', 'AU', 'NZ', | ||
| 'NO', 'SE', 'NL', 'DK', 'DE', 'FI', 'BE', 'LU', 'CH', 'IE', 'IS', | ||
| ]) | ||
|
|
||
| export function extractClientIp(req: NextRequest): string | undefined { | ||
| const forwardedFor = req.headers.get('x-forwarded-for') | ||
| if (forwardedFor) { | ||
| return forwardedFor.split(',')[0].trim() | ||
| } | ||
| return req.headers.get('x-real-ip') ?? undefined | ||
| } | ||
|
|
||
| export function getCountryCode(req: NextRequest): string | null { | ||
| const cfCountry = req.headers.get('cf-ipcountry') | ||
| if (cfCountry && cfCountry !== 'XX' && cfCountry !== 'T1') { | ||
| return cfCountry.toUpperCase() | ||
| } | ||
|
|
||
| const clientIp = extractClientIp(req) | ||
| if (!clientIp) { | ||
| return null | ||
| } | ||
| const geo = geoip.lookup(clientIp) | ||
| return geo?.country ?? null | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if the request's resolved country is allowed to use free | ||
| * mode, false if it's explicitly disallowed. Returns null when country can't | ||
| * be determined (VPN / localhost / corporate proxy) — callers should fail | ||
| * open in that case to match the chat-completions gate. | ||
| */ | ||
| export function isCountryAllowedForFreeMode(req: NextRequest): boolean | null { | ||
| const countryCode = getCountryCode(req) | ||
| if (!countryCode) return null | ||
| return FREE_MODE_ALLOWED_COUNTRIES.has(countryCode) | ||
| } | ||
|
Comment on lines
+39
to
+43
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This helper was added to the shared module but nothing in the codebase actually imports it — both |
||
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 new tests cover blocked (
FR) and allowed (US) countries, but the shared module's stated contract — "null country (VPN/localhost) fails open" — is not exercised. A request withoutcf-ipcountryand no resolvable IP should pass through to queue admission. Given this is the main escape hatch for VPN/localhost users, a test that verifiespostFreebuffSessioncreates a queued session when no country headers are present would round out the coverage.