PeerTube/server/core/helpers/geo-ip.ts

148 lines
4.4 KiB
TypeScript
Raw Normal View History

import { pathExists } from 'fs-extra/esm'
import { writeFile } from 'fs/promises'
2023-12-28 02:12:20 -06:00
import maxmind, { CityResponse, CountryResponse, Reader } from 'maxmind'
import { join } from 'path'
import { CONFIG } from '@server/initializers/config.js'
import { logger, loggerTagsFactory } from './logger.js'
import { isBinaryResponse, peertubeGot } from './requests.js'
2023-12-28 02:12:20 -06:00
import { isArray } from './custom-validators/misc.js'
import { throttle } from 'lodash-es'
const lTags = loggerTagsFactory('geo-ip')
export class GeoIP {
private static instance: GeoIP
2023-12-28 02:12:20 -06:00
private countryReader: Reader<CountryResponse>
private cityReader: Reader<CityResponse>
private readonly INIT_READERS_RETRY_INTERVAL = 1000 * 60 * 10 // 10 minutes
2023-12-28 02:12:20 -06:00
private readonly countryDBPath = join(CONFIG.STORAGE.BIN_DIR, 'dbip-country-lite-latest.mmdb')
private readonly cityDBPath = join(CONFIG.STORAGE.BIN_DIR, 'dbip-city-lite-latest.mmdb')
private constructor () {
}
2023-12-28 02:12:20 -06:00
async safeIPISOLookup (ip: string): Promise<{ country: string, subdivisionName: string }> {
const emptyResult = { country: null, subdivisionName: null }
if (CONFIG.GEO_IP.ENABLED === false) return emptyResult
try {
await this.initReadersIfNeededThrottle()
2023-12-28 02:12:20 -06:00
const countryResult = this.countryReader?.get(ip)
const cityResult = this.cityReader?.get(ip)
2023-12-28 02:12:20 -06:00
return {
country: this.getISOCountry(countryResult),
subdivisionName: this.getISOSubdivision(cityResult)
}
} catch (err) {
2023-12-28 02:12:20 -06:00
logger.error('Cannot get country/city information from IP.', { err })
2023-12-28 02:12:20 -06:00
return emptyResult
}
}
2023-12-28 02:12:20 -06:00
// ---------------------------------------------------------------------------
private getISOCountry (countryResult: CountryResponse) {
return countryResult?.country?.iso_code || null
}
private getISOSubdivision (subdivisionResult: CityResponse) {
const subdivisions = subdivisionResult?.subdivisions
if (!isArray(subdivisions) || subdivisions.length === 0) return null
// The last subdivision is the more precise one
const subdivision = subdivisions[subdivisions.length - 1]
return subdivision.names?.en || null
}
// ---------------------------------------------------------------------------
async updateDatabases () {
if (CONFIG.GEO_IP.ENABLED === false) return
2023-12-28 02:12:20 -06:00
await this.updateCountryDatabase()
await this.updateCityDatabase()
}
private async updateCountryDatabase () {
if (!CONFIG.GEO_IP.COUNTRY.DATABASE_URL) return false
await this.updateDatabaseFile(CONFIG.GEO_IP.COUNTRY.DATABASE_URL, this.countryDBPath)
this.countryReader = undefined
return true
}
2023-12-28 02:12:20 -06:00
private async updateCityDatabase () {
if (!CONFIG.GEO_IP.CITY.DATABASE_URL) return false
2023-12-28 02:12:20 -06:00
await this.updateDatabaseFile(CONFIG.GEO_IP.CITY.DATABASE_URL, this.cityDBPath)
this.cityReader = undefined
return true
}
private async updateDatabaseFile (url: string, destination: string) {
logger.info('Updating GeoIP databases from %s.', url, lTags())
const gotOptions = { context: { bodyKBLimit: 800_000 }, responseType: 'buffer' as 'buffer' }
try {
const gotResult = await peertubeGot(url, gotOptions)
if (!isBinaryResponse(gotResult)) {
throw new Error('Not a binary response')
}
2023-12-28 02:12:20 -06:00
await writeFile(destination, gotResult.body)
2023-12-28 02:12:20 -06:00
logger.info('GeoIP database updated %s.', destination, lTags())
} catch (err) {
logger.error('Cannot update GeoIP database from %s.', url, { err, ...lTags() })
}
}
2023-12-28 02:12:20 -06:00
// ---------------------------------------------------------------------------
private async initReadersIfNeeded () {
if (!this.countryReader) {
let open = true
if (!await pathExists(this.countryDBPath)) {
open = await this.updateCountryDatabase()
}
2023-12-28 02:12:20 -06:00
if (open) {
this.countryReader = await maxmind.open(this.countryDBPath)
}
}
if (!this.cityReader) {
let open = true
if (!await pathExists(this.cityDBPath)) {
open = await this.updateCityDatabase()
}
if (open) {
this.cityReader = await maxmind.open(this.cityDBPath)
}
}
}
private readonly initReadersIfNeededThrottle = throttle(this.initReadersIfNeeded.bind(this), this.INIT_READERS_RETRY_INTERVAL)
2023-12-28 02:12:20 -06:00
// ---------------------------------------------------------------------------
static get Instance () {
return this.instance || (this.instance = new this())
}
}