Class ProxyEndPointStore
- Namespace
- VpnHood.Core.Proxies.Management.Sqlite
- Assembly
- VpnHood.Core.Proxies.Management.Sqlite.dll
Provides the store of a managed proxy list in a SQLite file that the app and the VPN service share, as in an iOS app group's container.
public class ProxyEndPointStore : IProxyEndPointStore, IDisposable
- Inheritance
-
ProxyEndPointStore
- Implements
- Inherited Members
Remarks
Each operation opens its own connection and closes it when it ends, so no lock on the file outlives it, but for a connection whose setup fails after its open, which is left to the GC: iOS kills a suspended process that holds a lock on a file of its app group. The file is in write-ahead-log mode, and each command waits up to 5 seconds for another process's lock. Within a store, operations run one at a time.
The first operation makes the file and its tables. When its first open or check of the file fails for any reason, a lock or a cancellation included, or finds another schema version, the file is deleted and made again, empty. Passwords are stored as given, unencrypted.
Constructors
ProxyEndPointStore(string)
Provides the store of a managed proxy list in a SQLite file that the app and the VPN service share, as in an iOS app group's container.
public ProxyEndPointStore(string dbPath)
Parameters
dbPathstringThe file's path; its folder is made if missing.
Remarks
Each operation opens its own connection and closes it when it ends, so no lock on the file outlives it, but for a connection whose setup fails after its open, which is left to the GC: iOS kills a suspended process that holds a lock on a file of its app group. The file is in write-ahead-log mode, and each command waits up to 5 seconds for another process's lock. Within a store, operations run one at a time.
The first operation makes the file and its tables. When its first open or check of the file fails for any reason, a lock or a cancellation included, or finds another schema version, the file is deleted and made again, empty. Passwords are stored as given, unencrypted.
Methods
Count(CancellationToken)
Counts the endpoints.
public Task<int> Count(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.
Delete(IReadOnlyList<string>, CancellationToken)
Deletes endpoints by ID; an ID not stored is skipped.
public Task Delete(IReadOnlyList<string> ids, CancellationToken cancellationToken)
Parameters
idsIReadOnlyList<string>The IDs.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
Remarks
It deletes them in one transaction.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.
DeleteAll(DeleteAllOptions, CancellationToken)
Deletes the endpoints the options select.
public Task DeleteAll(DeleteAllOptions options, CancellationToken cancellationToken)
Parameters
optionsDeleteAllOptionsThe groups of endpoints to delete.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.
DisableAllFailed(CancellationToken)
Disables every endpoint used whose last use did not succeed.
public Task DisableAllFailed(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
Remarks
An endpoint whose last success and last failure are at the same moment counts as failed here.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.
Dispose()
Does nothing.
public void Dispose()
Get(string, CancellationToken)
Gets an endpoint by its ID.
public Task<ProxyEndPointRecord?> Get(string id, CancellationToken cancellationToken)
Parameters
idstringThe endpoint's Id.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<ProxyEndPointRecord>
A task whose result is the endpoint, or null when there is none of the ID.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.
GetQueuePosition(CancellationToken)
Gets the queue position, the count of connections the endpoints' rotation has reached.
public Task<long> GetQueuePosition(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.
List(CancellationToken)
Lists every endpoint.
public Task<IReadOnlyList<ProxyEndPointRecord>> List(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<IReadOnlyList<ProxyEndPointRecord>>
A task whose result is the endpoints, in no set order.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.
List(ProxyEndPointStoreListParams, CancellationToken)
Lists a page of the endpoints that match a filter, enabled ones first, then by quality, best first, all within the store, so the caller never loads the whole list.
public Task<ListResult<ProxyEndPointRecord>> List(ProxyEndPointStoreListParams options, CancellationToken cancellationToken)
Parameters
optionsProxyEndPointStoreListParamsThe filter and the page.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<ListResult<ProxyEndPointRecord>>
A task whose result is the page, with the number of all the matching endpoints.
Remarks
Endpoints of equal state and quality come in the order they were added. An endpoint used whose last success and last failure are at the same moment is in none of the groups, and is never listed.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.
Merge(IReadOnlyList<ProxyEndPoint>, int?, int?, bool, CancellationToken)
Merges a new list of endpoints into the store, by Merge(IEnumerable<ProxyEndPointInfo>, ProxyEndPoint[], int?, int?, bool)'s order: it deletes the endpoints the merge leaves out and adds the new ones, in one transaction, and a stored endpoint keeps its status and enabled state.
public Task Merge(IReadOnlyList<ProxyEndPoint> newEndPoints, int? maxItemCount, int? maxPenalty, bool removeDuplicateIps, CancellationToken cancellationToken)
Parameters
newEndPointsIReadOnlyList<ProxyEndPoint>The new list.
maxItemCountint?How many endpoints to keep, or null for the default.
maxPenaltyint?The penalty above which a used endpoint goes after the new ones, or null for the default.
removeDuplicateIpsbooltrue to drop a new endpoint whose host and protocol match a stored one whose last use succeeded.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
Remarks
It reads the stored endpoints before its transaction, which then makes the changes.
Exceptions
- ArgumentException
maxItemCountis 0 or less.- OperationCanceledException
cancellationTokenwas canceled.
ResetStatuses(CancellationToken)
Clears every endpoint's status, and the queue position.
public Task ResetStatuses(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.
SetCountryCode(string, string?, CancellationToken)
Sets the country code of a stored endpoint.
public Task SetCountryCode(string id, string? countryCode, CancellationToken cancellationToken)
Parameters
idstringThe endpoint's ID; an ID not stored changes nothing.
countryCodestringThe country code, or null to clear it.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.
SetQueuePosition(long, CancellationToken)
Sets the queue position, the count of connections the endpoints' rotation has reached.
public Task SetQueuePosition(long queuePosition, CancellationToken cancellationToken)
Parameters
queuePositionlongThe queue position.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.
UpdateStatuses(IReadOnlyList<ProxyEndPointInfo>, CancellationToken)
Updates the status and the enabled state of stored endpoints, by ID. It adds none, so it cannot bring back an endpoint the other process has deleted.
public Task UpdateStatuses(IReadOnlyList<ProxyEndPointInfo> infos, CancellationToken cancellationToken)
Parameters
infosIReadOnlyList<ProxyEndPointInfo>The endpoints and their statuses.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
Remarks
It writes the statuses in one transaction.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.
Upsert(IReadOnlyList<ProxyEndPointRecord>, bool, bool, CancellationToken)
Adds endpoints, or updates those of the same IDs: their protocol, host, port, user name and password, and their country code where the record gives one.
public Task Upsert(IReadOnlyList<ProxyEndPointRecord> records, bool keepExistingStatus, bool keepExistingEnabled, CancellationToken cancellationToken)
Parameters
recordsIReadOnlyList<ProxyEndPointRecord>The endpoints.
keepExistingStatusbooltrue to keep a stored endpoint's status.
keepExistingEnabledbooltrue to keep whether a stored endpoint is enabled.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
Remarks
It writes the records in one transaction.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.