-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat: add ip region for chat logs #6010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import path from 'node:path'; | ||
| import type { LocationName } from './type'; | ||
|
|
||
| export const dbPath = path.join(process.cwd(), 'data/GeoLite2-City.mmdb'); | ||
|
|
||
| export const privateOrOtherLocationName: LocationName = { | ||
| city: undefined, | ||
| country: { | ||
| en: 'Other', | ||
| zh: '其他' | ||
| }, | ||
| province: undefined | ||
| }; | ||
|
|
||
| export const cleanupIntervalMs = 6 * 60 * 60 * 1000; // Run cleanup every 6 hours |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import fs from 'node:fs'; | ||
| import type { ReaderModel } from '@maxmind/geoip2-node'; | ||
| import { Reader } from '@maxmind/geoip2-node'; | ||
| import { cleanupIntervalMs, dbPath, privateOrOtherLocationName } from './constants'; | ||
| import type { LocationName } from './type'; | ||
| import { extractLocationData } from './utils'; | ||
| import type { NextApiRequest } from 'next'; | ||
| import { getClientIp } from 'request-ip'; | ||
| import { addLog } from '../system/log'; | ||
|
|
||
| let reader: ReaderModel | null = null; | ||
|
|
||
| const locationIpMap = new Map<string, LocationName>(); | ||
|
|
||
| function loadGeoDB() { | ||
| const dbBuffer = fs.readFileSync(dbPath); | ||
| reader = Reader.openBuffer(dbBuffer); | ||
| return reader; | ||
| } | ||
|
|
||
| export function getGeoReader() { | ||
| if (!reader) { | ||
| return loadGeoDB(); | ||
| } | ||
| return reader; | ||
| } | ||
|
|
||
| export function getLocationFromIp(ip: string): LocationName { | ||
| const reader = getGeoReader(); | ||
|
|
||
| let locationName = locationIpMap.get(ip); | ||
| if (locationName) { | ||
| return locationName; | ||
| } | ||
|
|
||
| try { | ||
| const response = reader.city(ip); | ||
| const data = extractLocationData(response); | ||
| locationName = { | ||
| city: { | ||
| en: data.city.en, | ||
| zh: data.city.zh | ||
| }, | ||
| country: { | ||
| en: data.country.en, | ||
| zh: data.country.zh | ||
| }, | ||
| province: { | ||
| en: data.province.en, | ||
| zh: data.province.zh | ||
| } | ||
| }; | ||
| locationIpMap.set(ip, locationName); | ||
| return locationName; | ||
| } catch (error) { | ||
| locationIpMap.set(ip, privateOrOtherLocationName); | ||
| return privateOrOtherLocationName; | ||
| } | ||
| } | ||
|
|
||
| let cleanupInterval: NodeJS.Timeout | null = null; | ||
| function cleanupIpMap() { | ||
| locationIpMap.clear(); | ||
| } | ||
|
|
||
| export function clearCleanupInterval() { | ||
| if (cleanupInterval) { | ||
| clearInterval(cleanupInterval); | ||
| cleanupInterval = null; | ||
| } | ||
| } | ||
|
|
||
| export function initGeo() { | ||
| cleanupInterval = setInterval(cleanupIpMap, cleanupIntervalMs); | ||
|
|
||
| try { | ||
| loadGeoDB(); | ||
| } catch (error) { | ||
| clearCleanupInterval(); | ||
| addLog.error(`Failed to load geo db`, error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| export function getIpFromRequest(request: NextApiRequest): string { | ||
| const ip = getClientIp(request); | ||
| if (!ip || ip === '::1') { | ||
| return '127.0.0.1'; | ||
| } | ||
| return ip; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| export type I18nName = { | ||
| zh?: string; | ||
| en?: string; | ||
| }; | ||
|
|
||
| export type LocationName = { | ||
| country?: I18nName; | ||
| province?: I18nName; | ||
| city?: I18nName; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import type { City } from '@maxmind/geoip2-node'; | ||
|
|
||
| export function extractLocationData(response: City) { | ||
| return { | ||
| city: { | ||
| id: response.city?.geonameId, | ||
| en: response.city?.names.en, | ||
| zh: response.city?.names['zh-CN'] | ||
| }, | ||
| country: { | ||
| id: response.country?.geonameId, | ||
| en: response.country?.names.en, | ||
| zh: response.country?.names['zh-CN'] | ||
| }, | ||
| province: { | ||
| id: response.subdivisions?.[0]?.geonameId, | ||
| en: response.subdivisions?.[0]?.names.en, | ||
| zh: response.subdivisions?.[0]?.names['zh-CN'] | ||
| } | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xqvvu marked this conversation as resolved.
Show resolved
Hide resolved
|
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.