Skip to content

Commit a64ceb4

Browse files
committed
Address country gate review comments
1 parent 7419107 commit a64ceb4

3 files changed

Lines changed: 12 additions & 20 deletions

File tree

web/src/app/api/v1/chat/completions/_post.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,7 @@ import {
6565
OpenRouterError,
6666
} from '@/llm-api/openrouter'
6767
import { checkSessionAdmissible } from '@/server/free-session/public-api'
68-
import {
69-
extractClientIp,
70-
getFreeModeCountryAccess,
71-
getFreeModeCountryCodeForClient,
72-
} from '@/server/free-mode-country'
68+
import { getFreeModeCountryAccess } from '@/server/free-mode-country'
7369

7470
import type { SessionGateResult } from '@/server/free-session/public-api'
7571
import { extractApiKeyFromHeader } from '@/util/auth'
@@ -261,15 +257,14 @@ export async function postChatCompletions(params: {
261257
// For free mode requests, require a resolved allowlisted country.
262258
if (isFreeModeRequest) {
263259
const countryAccess = getFreeModeCountryAccess(req)
264-
const clientIp = extractClientIp(req)
265260

266261
logger.info(
267262
{
268263
cfHeader: countryAccess.cfCountry,
269264
geoipResult: countryAccess.geoipCountry,
270265
resolvedCountry: countryAccess.countryCode,
271266
countryBlockReason: countryAccess.blockReason,
272-
clientIp: clientIp ? '[redacted]' : undefined,
267+
clientIp: countryAccess.hasClientIp ? '[redacted]' : undefined,
273268
},
274269
'Free mode country detection',
275270
)
@@ -282,7 +277,7 @@ export async function postChatCompletions(params: {
282277
error: 'free_mode_not_available_in_country',
283278
countryCode: countryAccess.countryCode,
284279
countryBlockReason: countryAccess.blockReason,
285-
clientIp: clientIp ? '[redacted]' : undefined,
280+
clientIp: countryAccess.hasClientIp ? '[redacted]' : undefined,
286281
},
287282
logger,
288283
})
@@ -291,7 +286,7 @@ export async function postChatCompletions(params: {
291286
{
292287
error: 'free_mode_unavailable',
293288
message: 'Free mode is not available in your country.',
294-
countryCode: getFreeModeCountryCodeForClient(countryAccess),
289+
countryCode: countryAccess.countryCode ?? 'UNKNOWN',
295290
},
296291
{ status: 403 },
297292
)

web/src/app/api/v1/freebuff/session/_handlers.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import {
55
getSessionState,
66
requestSession,
77
} from '@/server/free-session/public-api'
8-
import {
9-
getFreeModeCountryAccess,
10-
getFreeModeCountryCodeForClient,
11-
} from '@/server/free-mode-country'
8+
import { getFreeModeCountryAccess } from '@/server/free-mode-country'
129
import { extractApiKeyFromHeader } from '@/util/auth'
1310

1411
import type { SessionDeps } from '@/server/free-session/public-api'
@@ -31,7 +28,7 @@ function countryBlockedResponse(req: NextRequest): NextResponse | null {
3128
return NextResponse.json(
3229
{
3330
status: 'country_blocked',
34-
countryCode: getFreeModeCountryCodeForClient(countryAccess),
31+
countryCode: countryAccess.countryCode ?? 'UNKNOWN',
3532
},
3633
{ status: 403 },
3734
)

web/src/server/free-mode-country.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export type FreeModeCountryAccess = {
3535
blockReason: FreeModeCountryBlockReason | null
3636
cfCountry: string | null
3737
geoipCountry: string | null
38+
hasClientIp: boolean
3839
}
3940

4041
export function extractClientIp(req: NextRequest): string | undefined {
@@ -58,6 +59,7 @@ export function getFreeModeCountryAccess(
5859
blockReason: 'anonymized_or_unknown_country',
5960
cfCountry,
6061
geoipCountry: null,
62+
hasClientIp: Boolean(clientIp),
6163
}
6264
}
6365

@@ -69,6 +71,7 @@ export function getFreeModeCountryAccess(
6971
blockReason: allowed ? null : 'country_not_allowed',
7072
cfCountry,
7173
geoipCountry: null,
74+
hasClientIp: Boolean(clientIp),
7275
}
7376
}
7477

@@ -79,6 +82,7 @@ export function getFreeModeCountryAccess(
7982
blockReason: 'missing_client_ip',
8083
cfCountry: null,
8184
geoipCountry: null,
85+
hasClientIp: false,
8286
}
8387
}
8488

@@ -90,6 +94,7 @@ export function getFreeModeCountryAccess(
9094
blockReason: 'unresolved_client_ip',
9195
cfCountry: null,
9296
geoipCountry: null,
97+
hasClientIp: true,
9398
}
9499
}
95100

@@ -100,11 +105,6 @@ export function getFreeModeCountryAccess(
100105
blockReason: allowed ? null : 'country_not_allowed',
101106
cfCountry: null,
102107
geoipCountry,
108+
hasClientIp: true,
103109
}
104110
}
105-
106-
export function getFreeModeCountryCodeForClient(
107-
countryAccess: FreeModeCountryAccess,
108-
): string {
109-
return countryAccess.countryCode ?? 'UNKNOWN'
110-
}

0 commit comments

Comments
 (0)