File size: 577 Bytes
ee5bd94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import crypto from "node:crypto"

import { NextApiRequest, NextApiResponse } from "next"

async function handler(req: NextApiRequest, res: NextApiResponse) {
  let ipAddress = req.headers["x-real-ip"] as string

  const forwardedFor = req.headers["x-forwarded-for"] as string

  if (!ipAddress && forwardedFor) {
    ipAddress = forwardedFor?.split(",").at(0) ?? "Unknown"
  }

  console.log("ipAddress:", ipAddress)
  const hash = crypto.createHash('sha256')
  hash.update(ipAddress)
  const digest = hash.digest('hex')
  res.status(200).json(digest)
}

export default handler