aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/geo-ip.ts
blob: 9e44d660f2789e593a33db2706efdb3fbb86fd91 (plain) (blame)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { pathExists, writeFile } from 'fs-extra'
import maxmind, { CountryResponse, Reader } from 'maxmind'
import { join } from 'path'
import { CONFIG } from '@server/initializers/config'
import { logger, loggerTagsFactory } from './logger'
import { isBinaryResponse, peertubeGot } from './requests'

const lTags = loggerTagsFactory('geo-ip')

const mmbdFilename = 'dbip-country-lite-latest.mmdb'
const mmdbPath = join(CONFIG.STORAGE.BIN_DIR, mmbdFilename)

export class GeoIP {
  private static instance: GeoIP

  private reader: Reader<CountryResponse>

  private constructor () {
  }

  async safeCountryISOLookup (ip: string): Promise<string> {
    if (CONFIG.GEO_IP.ENABLED === false) return null

    await this.initReaderIfNeeded()

    try {
      const result = this.reader.get(ip)
      if (!result) return null

      return result.country.iso_code
    } catch (err) {
      logger.error('Cannot get country from IP.', { err })

      return null
    }
  }

  async updateDatabase () {
    if (CONFIG.GEO_IP.ENABLED === false) return

    const url = CONFIG.GEO_IP.COUNTRY.DATABASE_URL

    logger.info('Updating GeoIP database from %s.', url, lTags())

    const gotOptions = { context: { bodyKBLimit: 200_000 }, responseType: 'buffer' as 'buffer' }

    try {
      const gotResult = await peertubeGot(url, gotOptions)

      if (!isBinaryResponse(gotResult)) {
        throw new Error('Not a binary response')
      }

      await writeFile(mmdbPath, gotResult.body)

      // Reinit reader
      this.reader = undefined

      logger.info('GeoIP database updated %s.', mmdbPath, lTags())
    } catch (err) {
      logger.error('Cannot update GeoIP database from %s.', url, { err, ...lTags() })
    }
  }

  private async initReaderIfNeeded () {
    if (!this.reader) {
      if (!await pathExists(mmdbPath)) {
        await this.updateDatabase()
      }

      this.reader = await maxmind.open(mmdbPath)
    }
  }

  static get Instance () {
    return this.instance || (this.instance = new this())
  }
}