forked from paul-vd/ip-info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts.bak
45 lines (38 loc) · 1.38 KB
/
index.ts.bak
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
import { NowRequest, NowResponse } from '@now/node'
import fetch from 'isomorphic-unfetch'
async function getUserIP(req: NowRequest) {
let ip: string = ''
const xForwadedFor = req.headers['x-forwarded-for']
if (xForwadedFor) {
if (typeof xForwadedFor === 'string') ip = xForwadedFor.split(',')[0]
else ip = xForwadedFor[0]
} else {
const realip = req.headers['x-forwarded-for']
if (realip && typeof realip === 'string') ip = realip.split(',')[0]
else ip = req.connection.remoteAddress || ''
}
if (!ip || process.env.NODE_ENV === 'development') await fetch('https://api6.ipify.org').then(async (res: any) => (ip = await res.text()))
return ip
}
export default async (req: NowRequest, res: NowResponse) => {
res.setHeader('Access-Control-Allow-Origin', '*')
try {
if (req.method !== 'GET') throw new Error('Only GET methods are allowed')
const queryIp = req.query.ip
let ip = queryIp as string
if (!ip) ip = await getUserIP(req)
if (!ip) throw new Error('No valid ip found')
const response = await fetch(`https://freegeoip.app/json/${ip}`, {
method: 'get',
mode: 'no-cors',
headers: {
accept: 'application/json',
},
})
if (!response.ok) throw new Error('API error')
const data = await response.json()
return res.json(data)
} catch (error) {
return res.json({ message: error.message })
}
}