added ip info support to api
This commit is contained in:
parent
4e01083cd6
commit
163f732ac7
|
@ -7,8 +7,7 @@
|
|||
"plugins": ["@typescript-eslint"],
|
||||
"extends": [
|
||||
"eslint:recommended",
|
||||
"plugin:@typescript-eslint/recommended",
|
||||
"prettier"
|
||||
"plugin:@typescript-eslint/recommended"
|
||||
],
|
||||
"rules": {
|
||||
"@typescript-eslint/no-unused-vars": "warn",
|
||||
|
|
|
@ -134,4 +134,6 @@ dist
|
|||
**/jwtRS256.key.pub
|
||||
|
||||
**/yarn.lock
|
||||
.husky
|
||||
.husky
|
||||
|
||||
**/GeoLite2-City*
|
|
@ -0,0 +1,15 @@
|
|||
#!/bin/bash
|
||||
|
||||
# API_KEY="PIiA7448yJCk23aO"
|
||||
# URL="https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=$API_KEY&suffix=tar.gz"
|
||||
URL="https://download.db-ip.com/free/dbip-city-lite-2023-05.mmdb.gz"
|
||||
wget -O "GeoLite2-City.gz" "$URL"
|
||||
|
||||
# unzip gz file
|
||||
gzip -d GeoLite2-City.gz
|
||||
|
||||
|
||||
mv GeoLite2-City "GeoLite2-City.mmdb"
|
||||
|
||||
rm -rf GeoLite2-City.gz
|
||||
echo "Done!!"
|
|
@ -0,0 +1,37 @@
|
|||
import IPInfo from '../services/ipinfo.service'
|
||||
import { Request, Response, NextFunction } from 'express'
|
||||
|
||||
const { getIPInfo } = new IPInfo()
|
||||
|
||||
export default class IPInfoController {
|
||||
public async fetchCurrentIPInfo(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<any> {
|
||||
try {
|
||||
const ip =
|
||||
req.headers['x-forwarded-for'] ||
|
||||
req.socket.remoteAddress ||
|
||||
req.ip
|
||||
const data = await getIPInfo(ip as string)
|
||||
res.status(200).json(data)
|
||||
} catch (error: any) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
public async fetchIPInfo(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<any> {
|
||||
try {
|
||||
const { ip } = req.params
|
||||
const data = await getIPInfo(ip)
|
||||
res.status(200).json(data)
|
||||
} catch (error: any) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
import fs from 'fs'
|
||||
import mmdb, { CityResponse } from 'mmdb-lib'
|
||||
|
||||
const db = fs.readFileSync('GeoLite2-City.mmdb')
|
||||
|
||||
export default new mmdb.Reader<CityResponse>(db)
|
|
@ -29,6 +29,7 @@ app.use(morgan('dev'))
|
|||
app.use(express.json())
|
||||
app.use(express.urlencoded({ extended: true, limit: '50mb' }))
|
||||
app.set('view engine', 'ejs')
|
||||
app.set('trust proxy', true)
|
||||
|
||||
console.log('☄ ', 'Base Route', '/')
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
"graphql-request": "^5.0.0",
|
||||
"helmet": "^6.0.1",
|
||||
"joi": "^17.7.0",
|
||||
"mmdb-lib": "^2.0.2",
|
||||
"morgan": "^1.10.0",
|
||||
"multer": "^1.4.5-lts.1",
|
||||
"multer-s3": "^3.0.1",
|
||||
|
@ -48,6 +49,7 @@
|
|||
"@swc/wasm": "^1.3.23",
|
||||
"@types/cors": "^2.8.13",
|
||||
"@types/morgan": "^1.9.3",
|
||||
"@typescript-eslint/eslint-plugin": "^5.59.2",
|
||||
"concurrently": "^7.6.0",
|
||||
"cross-env": "^7.0.3",
|
||||
"graphqurl": "^1.0.1",
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
import { Router } from 'express'
|
||||
import IPController from '../../controllers/ipinfo.controller'
|
||||
|
||||
const router = Router()
|
||||
const ipController = new IPController()
|
||||
|
||||
router.get('/current', ipController.fetchCurrentIPInfo)
|
||||
router.get('/:ip', ipController.fetchIPInfo)
|
||||
|
||||
export default router
|
|
@ -0,0 +1,8 @@
|
|||
import mmdb_client from '../helpers/mmdb_client'
|
||||
|
||||
export default class IPInfo {
|
||||
public getIPInfo = async (ip: string) => {
|
||||
const data = await mmdb_client.get(ip)
|
||||
return data
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue