Skip to content

Commit

Permalink
worker: Translate the country code into the country name
Browse files Browse the repository at this point in the history
This uses the code from the `cf-ipcountry` header that is provided by
Cloudflare to directly get the country name that we use in the
Dashboard. Since the city/lat/long were removed from the dashboard this
seems like a good compromise.

Implements the second strategy as explaiend in #30.
  • Loading branch information
jorgelbg committed Jul 20, 2021
1 parent cfbe90b commit 2de9c35
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 22 deletions.
22 changes: 3 additions & 19 deletions src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { toLabels } from './headers'
import { ipInfo } from './ipinfo'
import { referrer, shorten } from 'inbound'
import { URL } from '@cliqz/url-parser'
import { getName } from 'country-list'
import { UAParser } from 'ua-parser-js'
import { hash_hex, string_to_u8 } from 'siphash'
import logfmt from 'logfmt'
// import { parse } from './referer'
import { encode } from 'ngeohash'

let sessionKey = string_to_u8(FINGERPRINT)

Expand All @@ -30,7 +28,6 @@ const EXCLUDE = JSON.parse(OPTIONS) || {

let batchedEvents: Array<Hash<any>> = []
let currentHost: string | null = ''
let ipInfoQuotaReached = false

const INCLUDE_LABELS = new Set([
'method',
Expand Down Expand Up @@ -161,6 +158,7 @@ async function handleRequest(event: FetchEvent): Promise<Response> {

let parsed = new URL(url)
let userAgent = request.headers.get('user-agent')
let countryCode = request.headers.get('cf-ipcountry') || 'unknown'

parser.setUA(`${userAgent}`)

Expand All @@ -182,12 +180,13 @@ async function handleRequest(event: FetchEvent): Promise<Response> {
os_version: parser.getOS().version,
// the ua-parser-js library identify desktop clients as an empty device type
device_type: parser.getDevice().type ? parser.getDevice().type : 'desktop',
country: request.headers.get('cf-ipcountry'),
country: countryCode,
type: '',
network: '',
client: '',
referer_domain: '',
duration,
country_name: getName(countryCode),
}

if (DEBUG_HEADERS) {
Expand All @@ -199,21 +198,6 @@ async function handleRequest(event: FetchEvent): Promise<Response> {
}
}

if (ipInfoQuotaReached == false && clientIP.length > 0) {
try {
const ip = await ipInfo(clientIP)

let geohash = encode(ip.lat, ip.lon)
let country_name = `${getName(ip.country)}`

labels = { ...labels, ...ip, ...{ country_name, geohash } }
} catch (error) {
// We catched 429 Too Many Requests, this means that we reached our current
// ipinfo quota. Avoid making extra requests.
ipInfoQuotaReached = true
}
}

if (request.headers.get('referer')) {
let refData: any = await new Promise((resolve) => {
const ref = request.headers.get('referer') || ''
Expand Down
3 changes: 0 additions & 3 deletions test/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ describe('request handler', () => {
'os="Mac OS"',
'device_type=desktop',
'country=US',
'geohash=9yegjbpfr',
'country_name="United States of America"',
'method=GET',
'status=200',
Expand All @@ -70,7 +69,6 @@ describe('request handler', () => {
)

expect(bodyObj.streams[0].labels).to.not.include('url')
expect(bodyObj.streams[0].labels).to.not.include('geohash')
expect(bodyObj.streams[0].labels).to.not.include('origin')
})

Expand Down Expand Up @@ -149,7 +147,6 @@ describe('request handler', () => {
// the referer of the original request is detected and parsed
'network=twitter',
'type=social',
'geohash=9yegjbpfr',
].every((bit) => string.includes(bit)),
)
})
Expand Down

0 comments on commit 2de9c35

Please sign in to comment.