Table of Contents

Class LocalIpRangeLocationProvider

Namespace
VpnHood.Net.IpLocations.Providers.Offlines
Assembly
VpnHood.Net.IpLocations.dll

Provides locations, the country only, and each country's IP ranges, from a zip with an entry of IP ranges per country (cc.ips), which BuildLocalIpRangeLocationDb(Stream, string, CancellationToken) builds.

public class LocalIpRangeLocationProvider : IIpRangeLocationProvider, IIpLocationProvider, ICurrentIpLocationProvider, IDisposable
Inheritance
LocalIpRangeLocationProvider
Implements
Inherited Members

Remarks

A country's ranges are read on the first lookup that needs them and kept, since a lookup walks many countries. A lookup tries the current country first, then every country in turn. Where the asset's stream cannot seek, as in an Android package, the zip is copied into memory for the provider's life.

Constructors

LocalIpRangeLocationProvider(IAsset, Func<string?>, string?)

Provides locations, the country only, and each country's IP ranges, from a zip with an entry of IP ranges per country (cc.ips), which BuildLocalIpRangeLocationDb(Stream, string, CancellationToken) builds.

public LocalIpRangeLocationProvider(IAsset zipAsset, Func<string?> currentCountryCodeFunc, string? userAgent = null)

Parameters

zipAsset IAsset

The zip.

currentCountryCodeFunc Func<string>

Returns the current country's code, tried first, or null to try the one GetCurrentLocation(CancellationToken) found last.

userAgent string

The User-Agent header the current location's public-IP lookup sends, or null.

Remarks

A country's ranges are read on the first lookup that needs them and kept, since a lookup walks many countries. A lookup tries the current country first, then every country in turn. Where the asset's stream cannot seek, as in an Android package, the zip is copied into memory for the provider's life.

Methods

Dispose()

Closes the zip.

public void Dispose()

GetCountryCodes(CancellationToken)

Gets the countries the database has IP ranges for.

public Task<string[]> GetCountryCodes(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<string[]>

A task whose result is the countries' two-letter codes, in upper case.

GetCurrentLocation(CancellationToken)

Finds where this device's public IP address is.

public Task<IpLocation> GetCurrentLocation(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<IpLocation>

A task whose result is the location.

Remarks

It finds the device's public IP address online, IPv4 first, then looks the address up locally.

Exceptions

NotSupportedException

The provider does not support the lookup.

Exception

No public IP address was found.

KeyNotFoundException

No range holds the address.

GetIpRanges(string, CancellationToken)

Gets a country's IP ranges.

public Task<IpRangeOrderedList> GetIpRanges(string countryCode, CancellationToken cancellationToken)

Parameters

countryCode string

The country's two-letter code.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<IpRangeOrderedList>

A task whose result is the country's ranges, in order.

Remarks

A country the zip has no entry for has no ranges. One whose entry cannot be read, or the zip itself, gives none for that call, and the next call reads it again. Either error is logged.

Exceptions

OperationCanceledException

cancellationToken was canceled.

GetLocation(IPAddress, CancellationToken)

Finds where an IP address is.

public Task<IpLocation> GetLocation(IPAddress ipAddress, CancellationToken cancellationToken)

Parameters

ipAddress IPAddress

The IP address.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<IpLocation>

A task whose result is the location.

Exceptions

KeyNotFoundException

No country's ranges hold the address.