From f36772bbfac3ba041f9d8e72377eda58e07cdd7e Mon Sep 17 00:00:00 2001 From: IP2Location Date: Wed, 22 Jan 2025 09:12:23 +0800 Subject: [PATCH 1/2] Added IP2Location.io API as failover for geolocation --- src/actions/AccountReferralActions.ts | 14 +++++++++++--- src/types/TweakTypes.ts | 4 ++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/actions/AccountReferralActions.ts b/src/actions/AccountReferralActions.ts index 45926c5f370..daf9b152774 100644 --- a/src/actions/AccountReferralActions.ts +++ b/src/actions/AccountReferralActions.ts @@ -5,7 +5,7 @@ import { EdgeAccount } from 'edge-core-js/types' import { ENV } from '../env' import { RootState, ThunkAction } from '../types/reduxTypes' import { AccountReferral, Promotion, ReferralCache } from '../types/ReferralTypes' -import { asCurrencyCode, asIpApi, asMessageTweak, asPluginTweak } from '../types/TweakTypes' +import { asCurrencyCode, asIpApi, asIp2LocationIo, asMessageTweak, asPluginTweak } from '../types/TweakTypes' import { fetchReferral } from '../util/network' import { lockStartDates, TweakSource } from '../util/ReferralHelpers' import { logEvent } from '../util/tracking' @@ -208,8 +208,16 @@ export const getCountryCodeByIp = async (): Promise => { const { countryCode } = asIpApi(await reply.json()) return countryCode } catch (e: any) { - console.warn(`getCountryCodeByIp() failed: ${String(e)}`) - return undefined + try { + const reply2 = await fetch(`https://api.ip2location.io`) + let json2 = await reply2.json() + json2 = json2.replace("country_code", "countryCode") + const { countryCode } = asIp2LocationIo(json2) + return countryCode + } catch (e: any) { + console.warn(`getCountryCodeByIp() failed: ${String(e)}`) + return undefined + } } } diff --git a/src/types/TweakTypes.ts b/src/types/TweakTypes.ts index 7e0fa687dad..4282d37cffc 100644 --- a/src/types/TweakTypes.ts +++ b/src/types/TweakTypes.ts @@ -80,3 +80,7 @@ export const asPluginTweak = asObject({ export const asIpApi = asObject({ countryCode: asString }) + +export const asIp2LocationIo = asObject({ + countryCode: asString +}) From d1f0cf7c35f975a849adc0ac2f706b4bd371b152 Mon Sep 17 00:00:00 2001 From: IP2Location Date: Wed, 22 Jan 2025 10:14:20 +0800 Subject: [PATCH 2/2] Fixed formatting --- src/actions/AccountReferralActions.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/actions/AccountReferralActions.ts b/src/actions/AccountReferralActions.ts index daf9b152774..8b381648296 100644 --- a/src/actions/AccountReferralActions.ts +++ b/src/actions/AccountReferralActions.ts @@ -5,7 +5,7 @@ import { EdgeAccount } from 'edge-core-js/types' import { ENV } from '../env' import { RootState, ThunkAction } from '../types/reduxTypes' import { AccountReferral, Promotion, ReferralCache } from '../types/ReferralTypes' -import { asCurrencyCode, asIpApi, asIp2LocationIo, asMessageTweak, asPluginTweak } from '../types/TweakTypes' +import { asCurrencyCode, asIp2LocationIo, asIpApi, asMessageTweak, asPluginTweak } from '../types/TweakTypes' import { fetchReferral } from '../util/network' import { lockStartDates, TweakSource } from '../util/ReferralHelpers' import { logEvent } from '../util/tracking' @@ -211,7 +211,7 @@ export const getCountryCodeByIp = async (): Promise => { try { const reply2 = await fetch(`https://api.ip2location.io`) let json2 = await reply2.json() - json2 = json2.replace("country_code", "countryCode") + json2 = json2.replace(`country_code`, `countryCode`) const { countryCode } = asIp2LocationIo(json2) return countryCode } catch (e: any) {