Class IpLocationSqliteProvider
- Namespace
- VpnHood.Net.IpLocations.Providers.SqliteProvider
- Assembly
- VpnHood.Net.IpLocations.Providers.SqliteProvider.dll
Provides locations, the country only, and each country's IP ranges, from the SQLite database IpLocationSqliteBuilder builds.
public class IpLocationSqliteProvider : IIpRangeLocationProvider, IIpLocationProvider, ICurrentIpLocationProvider, IDisposable, IAsyncDisposable
- Inheritance
-
IpLocationSqliteProvider
- Implements
- Inherited Members
Methods
Dispose()
Disposes the connection, unless the caller keeps it. A failure to close the connection is logged, not thrown. Calling it again does nothing.
public void Dispose()
DisposeAsync()
Disposes the provider, as Dispose() does; the task is already complete.
public ValueTask DisposeAsync()
Returns
- ValueTask
A completed task.
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 in the database.
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
The code's case does not matter; an unknown country has no ranges.
Exceptions
- ArgumentException
countryCodeis empty or white space.- ArgumentNullException
countryCodeis null.
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.
Remarks
An IPv4-mapped IPv6 address is looked up as IPv4.
Exceptions
- KeyNotFoundException
No range holds the address.
Open(SqliteConnection, bool, string?)
Opens a provider on a connection, which it opens when it is closed.
public static Task<IpLocationSqliteProvider> Open(SqliteConnection connection, bool leaveOpen = false, string? userAgent = null)
Parameters
connectionSqliteConnectionThe connection.
leaveOpenbooltrue to leave the connection to the caller when the provider is disposed; false to dispose it with the provider.
userAgentstringThe User-Agent header the current location's public-IP lookup sends, or null.
Returns
- Task<IpLocationSqliteProvider>
A task whose result is the provider.
Exceptions
- ArgumentNullException
connectionis null.- SqliteException
The connection cannot be opened.
Open(string, string?)
Opens a provider on a database file, read-only.
public static Task<IpLocationSqliteProvider> Open(string dbPath, string? userAgent = null)
Parameters
dbPathstringThe database file.
userAgentstringThe User-Agent header the current location's public-IP lookup sends, or null.
Returns
- Task<IpLocationSqliteProvider>
A task whose result is the provider, which owns its connection.
Exceptions
- SqliteException
The file cannot be opened.
ToBytes(IPAddress)
Converts an IP address to the database's key form: its 16 IPv6 bytes, an IPv4 address mapped to IPv6 first.
public static byte[] ToBytes(IPAddress ipAddress)
Parameters
ipAddressIPAddressThe IP address.
Returns
- byte[]
The 16 bytes.