Skip to content

Commit

Permalink
feat: add ip
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Jun 29, 2023
1 parent 969a321 commit 8f62651
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ import { kvKeys, redis } from '~/lib/redis.server'

import {
REQUEST_GEO,
REQUEST_IP,
REQUEST_PATHNAME,
REQUEST_QUERY,
} from './constants/system'

export default async function middleware(req: NextRequest) {
const { pathname, search } = req.nextUrl
const { geo, ip } = req
const { geo } = req
let ip = req.ip ?? req.headers.get('x-real-ip')
const forwardedFor = req.headers.get('x-forwarded-for')
if (!ip && forwardedFor) {
ip = forwardedFor.split(',').at(0) ?? ''
}

// console.debug(`${req.method} ${req.nextUrl.pathname}${req.nextUrl.search}`)

Expand All @@ -31,6 +37,7 @@ export default async function middleware(req: NextRequest) {
requestHeaders.set(REQUEST_PATHNAME, pathname)
requestHeaders.set(REQUEST_QUERY, search)
requestHeaders.set(REQUEST_GEO, geo?.country || 'unknown')
requestHeaders.set(REQUEST_IP, ip || '')

const isApi = pathname.startsWith('/api/')

Expand Down

0 comments on commit 8f62651

Please sign in to comment.