makefuture / games / accept-the-cookie / game / systems / geolocation.js
149 words
 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
export async function collectGeolocation(session) {
    if (!session.consent.analytics || !session.consent.personalization) {
        session.geo = {
            status: 'Not collected: analytics and personalization consent required',
            city: 'Not collected',
            country: 'Not collected',
            vpnLikely: null,
        };
        return session.geo;
    }

    try {
        const controller = new AbortController();
        const timeout = window.setTimeout(() => controller.abort(), 2500);
        const response = await fetch('https://ipwho.is/', {
            signal: controller.signal,
            referrerPolicy: 'no-referrer',
        });
        window.clearTimeout(timeout);
        if (!response.ok) throw new Error(`Geolocation failed: ${response.status}`);
        const data = await response.json();
        const timezone = session.device.timezone || '';
        const geoTz = data.timezone || '';
        const vpnLikely = Boolean(timezone && geoTz && timezone !== geoTz);
        session.geo = {
            status: 'Collected with consent',
            city: data.city || 'Unknown',
            region: data.region || 'Unknown',
            country: data.country || data.country_code || 'Unknown',
            timezone: geoTz || 'Unknown',
            org: data.connection?.org || data.connection?.isp || 'Unknown network',
            vpnLikely,
        };
    } catch (error) {
        session.geo = {
            status: 'Unavailable or blocked',
            city: 'Unknown',
            country: 'Unknown',
            vpnLikely: null,
        };
    }

    return session.geo;
}

static mirror of HEAD · about · clone: git clone https://git.makefuture.ro/makefuture.git