Skip to main content

User Location & Connection Status

In many scenarios, we need to know the location of the customer or the current VPN location, in case the customer is currently connected to the VPN, in order to either present them in the reports or to obtain the best smart location.

This is implemented, by performing an API call to the Instance Discovery Service /conn_status endpoint. This endpoint receives the request’s IP address and will use the Maxmind Service, to lookup information about that IP , such as Country, ISP, ASN etc.

In case the user is not connected to the VPN, the result will represent information about the user’s “real” aka ISP location. If the user is connected to the VPN, the result will represent the information about the current VPN server location. The Instance Discovery Service can thus lookup the request IP and check if it’s a known VPN IP. If that' the case the returned connection status is vpn_connected=true, otherwise it will be false.

fn get_user_location(&self, mode: LocationMode) -> Result<GeoLocation>

LocationMode ​

danger

The real location will be cached in the SDK. Keep it current by reporting every network change and every VPN connect or disconnect with the SDK Manager's networkChanged(change) method, see Keeping the location current.

NameDescription
AutoThis mode will return what ever location is currently available, based on whether the user is connected to the VPN or not.
RealLocationThis mode aims to provide the actual Internet Service Provider (ISP) location of the customer. If the customer is connected to a VPN, making the real location inaccessible, the SDK will attempt to retrieve a previously cached location. Only when the SDK has never seen a real location and the lookup fails is an exception, SdkException::RealLocationNotAvailable, thrown, which is encapsulated by an InstanceDiscoveryException. See Expired real location.
VPNLocationThis mode is only available while the user is currently connected to the VPN and will return the VPN Location. If the user is not connected, the SDK will return SdkException::VPNLocationNotAvailable.The VPN location is not cached for privacy reasons.

Expired real location ​

The server sets how long a real location is cached (about 10 minutes). Once that has passed, the SDK keeps serving it for a further window while it looks it up again: 24 hours, unless the server sends another stale-while-revalidate value.

Cached real locationRealLocation lookup
Not expiredAnswers from the cache, without a network request.
Expired, inside the windowStarts a lookup and waits up to 1 second for it. Answers with the new location if it arrived, otherwise with the expired one. The lookup finishes in the background, so a later call gets the new location.
Expired, past the windowWaits for the lookup. If it fails, answers with the last known real location, however old it is.
None ever cachedWaits for the lookup. If it fails, throws RealLocationNotAvailable.

Auto follows the same rules unless VpnConnected was reported: while the VPN is reported up, the real location is not the current one and Auto asks the server. An app that never reports the VPN state is treated as not connected. If it is connected after all, Auto can return the real location in place of the VPN one — report every VPN connect and disconnect.

Keeping the location current ​

The SDK caches the real location and uses its cached geolocation to decide whether the VPN is connected. Report every change with the SDK Manager's networkChanged(change) method, passing what changed.

fn network_changed(&self, change: NetworkChange)

NetworkChange ​

VariantWhenEffect
Switched(networkName, networkId)The underlying network changed: Wi-Fi ↔ cellular, a different Wi-Fi, a significant location change.Sets the SDK's current network to networkName (e.g. the Wi-Fi SSID) and networkId (e.g. the BSSID) on every call; pass empty strings on cellular or when unknown. Drops the cached VPN location and expires the real location, so the next lookup asks the server again; the previous network's real location is still served while that lookup runs, see Expired real location. A repeat for the same network within 5 seconds does not clear again; a different network always does.
VpnConnectedThe VPN connected, also after a reconnect or a location switch.The device has not moved, so the real location stays cached and the next RealLocation lookup answers without a network request. Auto and VPNLocation ask the server for the VPN location; Auto does not answer with the cached real location until the server reports the device off the VPN or the VPN disconnects. Not debounced.
VpnDisconnectedThe VPN disconnected.The real location cached before the connect stays valid; RealLocation and Auto answer from it without a network request. If the server reported the device off the VPN during the session, that answer is treated as expired: the next lookup asks the server again, waits up to 1 second and otherwise returns it, see Expired real location. Not debounced.
sdkManager.networkChanged(change: .switched(networkName: "Starbucks", networkId: "aa:bb:cc:dd:ee:ff"))
sdkManager.networkChanged(change: .vpnConnected)
warning

Never report a VPN connect or disconnect as Switched. It expires the real location, so the next RealLocation lookup starts an IP probe plus a conn_status request and waits up to 1 second for it instead of answering from the cache, and so does Auto. While the device is on the VPN that lookup can come back too late, and Auto then returns the real location as the current one.

info

VpnConnected and VpnDisconnected do not cover a change of the underlying network. If the device switches networks while the VPN is connected (e.g. Wi-Fi → cellular), report Switched as well, or the previous network's real location stays in use until its cache entry expires. Before the Instance Discovery module was first used, networkChanged leaves the cache alone; the network name and id of a Switched are applied anyway.

Geolocation ​

NameTypeDescription
current_ipStringIP Address
iso_country_codeStringISO country code associated with the IP
regionStringRegion code associated with the IP
ispStringISP name associated with the IP
asnStringAutonomous System Number associated with the IP
latitudeFloatLatitude of the geolocation associated with that IP
longitudeFlaotLongitude of the geolocation associated with that IP
vpn_connectedBooltrue, if that IP is associated with the brand's IP pool.