Table of Contents

Class LocalIpLocationProvider

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

Provides locations, the country only, from IP ranges held in memory, which Deserialize(Stream, string?) reads and BuildLocalIpLocationDb(Stream, string, CancellationToken) builds.

public class LocalIpLocationProvider : IIpLocationProvider, ICurrentIpLocationProvider, IDisposable
Inheritance
LocalIpLocationProvider
Implements
Inherited Members

Constructors

LocalIpLocationProvider(IEnumerable<IpRangeInfo>, string?)

Initializes a new instance of the LocalIpLocationProvider class.

public LocalIpLocationProvider(IEnumerable<LocalIpLocationProvider.IpRangeInfo> ipRangeInfos, string? userAgent = null)

Parameters

ipRangeInfos IEnumerable<LocalIpLocationProvider.IpRangeInfo>

The IP ranges with their countries.

userAgent string

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

Methods

Deserialize(Stream, string?)

Reads IP ranges that Serialize(Stream) wrote.

public static LocalIpLocationProvider Deserialize(Stream stream, string? userAgent = null)

Parameters

stream Stream

The stream to read from; it is closed after.

userAgent string

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

Returns

LocalIpLocationProvider

A new provider with the ranges.

Exceptions

EndOfStreamException

The stream ends early.

Dispose()

Does nothing: the provider holds nothing to release.

public void Dispose()

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.

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.

Remarks

An IPv4-mapped IPv6 address is looked up as IPv4.

Exceptions

KeyNotFoundException

No range holds the address.

Serialize(Stream)

Writes the IP ranges in the binary form Deserialize(Stream, string?) reads.

public void Serialize(Stream stream)

Parameters

stream Stream

The stream to write to; it stays open.