2023-07-31 14:34:36 +02:00
|
|
|
import { pathExists } from 'fs-extra/esm'
|
|
|
|
import { writeFile } from 'fs/promises'
|
2023-12-28 09:12:20 +01:00
|
|
|
import maxmind, { CityResponse, CountryResponse, Reader } from 'maxmind'
|
2022-03-24 13:36:47 +01:00
|
|
|
import { join } from 'path'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { CONFIG } from '@server/initializers/config.js'
|
|
|
|
import { logger, loggerTagsFactory } from './logger.js'
|
|
|
|
import { isBinaryResponse, peertubeGot } from './requests.js'
|
2023-12-28 09:12:20 +01:00
|
|
|
import { isArray } from './custom-validators/misc.js'
|
2022-03-24 13:36:47 +01:00
|
|
|
|
|
|
|
const lTags = loggerTagsFactory('geo-ip')
|
|
|
|
|
|
|
|
export class GeoIP {
|
|
|
|
private static instance: GeoIP
|
|
|
|
|
2023-12-28 09:12:20 +01:00
|
|
|
private countryReader: Reader<CountryResponse>
|
|
|
|
private cityReader: Reader<CityResponse>
|
|
|
|
|
|
|
|
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')
|
2022-03-24 13:36:47 +01:00
|
|
|
|
|
|
|
private constructor () {
|
|
|
|
}
|
|
|
|
|
2023-12-28 09:12:20 +01:00
|
|
|
async safeIPISOLookup (ip: string): Promise<{ country: string, subdivisionName: string }> {
|
|
|
|
const emptyResult = { country: null, subdivisionName: null }
|
|
|
|
if (CONFIG.GEO_IP.ENABLED === false) return emptyResult
|
2022-03-24 13:36:47 +01:00
|
|
|
|
2023-12-28 09:12:20 +01:00
|
|
|
await this.initReadersIfNeeded()
|
2022-03-24 13:36:47 +01:00
|
|
|
|
|
|
|
try {
|
2023-12-28 09:12:20 +01:00
|
|
|
const countryResult = this.countryReader?.get(ip)
|
|
|
|
const cityResult = this.cityReader?.get(ip)
|
2022-03-24 13:36:47 +01:00
|
|
|
|
2023-12-28 09:12:20 +01:00
|
|
|
return {
|
|
|
|
country: this.getISOCountry(countryResult),
|
|
|
|
subdivisionName: this.getISOSubdivision(cityResult)
|
|
|
|
}
|
2022-03-24 13:36:47 +01:00
|
|
|
} catch (err) {
|
2023-12-28 09:12:20 +01:00
|
|
|
logger.error('Cannot get country/city information from IP.', { err })
|
2022-03-24 13:36:47 +01:00
|
|
|
|
2023-12-28 09:12:20 +01:00
|
|
|
return emptyResult
|
2022-03-24 13:36:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 09:12:20 +01: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 () {
|
2022-03-24 13:36:47 +01:00
|
|
|
if (CONFIG.GEO_IP.ENABLED === false) return
|
|
|
|
|
2023-12-28 09:12:20 +01: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
|
|
|
|
}
|
2022-03-24 13:36:47 +01:00
|
|
|
|
2023-12-28 09:12:20 +01:00
|
|
|
private async updateCityDatabase () {
|
|
|
|
if (!CONFIG.GEO_IP.CITY.DATABASE_URL) return false
|
2022-03-24 13:36:47 +01:00
|
|
|
|
2023-12-28 09:12:20 +01: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' }
|
2022-03-24 13:36:47 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
const gotResult = await peertubeGot(url, gotOptions)
|
|
|
|
|
|
|
|
if (!isBinaryResponse(gotResult)) {
|
|
|
|
throw new Error('Not a binary response')
|
|
|
|
}
|
|
|
|
|
2023-12-28 09:12:20 +01:00
|
|
|
await writeFile(destination, gotResult.body)
|
2022-03-24 13:36:47 +01:00
|
|
|
|
2023-12-28 09:12:20 +01:00
|
|
|
logger.info('GeoIP database updated %s.', destination, lTags())
|
2022-03-24 13:36:47 +01:00
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot update GeoIP database from %s.', url, { err, ...lTags() })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 09:12:20 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
private async initReadersIfNeeded () {
|
|
|
|
if (!this.countryReader) {
|
|
|
|
let open = true
|
|
|
|
|
|
|
|
if (!await pathExists(this.countryDBPath)) {
|
|
|
|
open = await this.updateCountryDatabase()
|
2022-03-24 13:36:47 +01:00
|
|
|
}
|
|
|
|
|
2023-12-28 09:12:20 +01: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)
|
|
|
|
}
|
2022-03-24 13:36:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 09:12:20 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-03-24 13:36:47 +01:00
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
}
|