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
zipAssetIAssetThe zip.
currentCountryCodeFuncFunc<string>Returns the current country's code, tried first, or null to try the one GetCurrentLocation(CancellationToken) found last.
userAgentstringThe 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
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
GetCurrentLocation(CancellationToken)
Finds where this device's public IP address is.
public Task<IpLocation> GetCurrentLocation(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe 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
countryCodestringThe country's two-letter code.
cancellationTokenCancellationTokenThe 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
cancellationTokenwas canceled.
GetLocation(IPAddress, CancellationToken)
Finds where an IP address is.
public Task<IpLocation> GetLocation(IPAddress ipAddress, CancellationToken cancellationToken)
Parameters
ipAddressIPAddressThe IP address.
cancellationTokenCancellationTokenThe 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.