Table of Contents

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

dbPath string

The 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

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<int>

A task whose result is the number of endpoints.

Exceptions

OperationCanceledException

cancellationToken was canceled.

Delete(IReadOnlyList<string>, CancellationToken)

Deletes endpoints by ID; an ID not stored is skipped.

public Task Delete(IReadOnlyList<string> ids, CancellationToken cancellationToken)

Parameters

ids IReadOnlyList<string>

The IDs.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task

A task that represents the asynchronous operation.

Remarks

It deletes them in one transaction.

Exceptions

OperationCanceledException

cancellationToken was canceled.

DeleteAll(DeleteAllOptions, CancellationToken)

Deletes the endpoints the options select.

public Task DeleteAll(DeleteAllOptions options, CancellationToken cancellationToken)

Parameters

options DeleteAllOptions

The groups of endpoints to delete.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task

A task that represents the asynchronous operation.

Exceptions

OperationCanceledException

cancellationToken was canceled.

DisableAllFailed(CancellationToken)

Disables every endpoint used whose last use did not succeed.

public Task DisableAllFailed(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The 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

cancellationToken was 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

id string

The endpoint's Id.

cancellationToken CancellationToken

The 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

cancellationToken was canceled.

GetQueuePosition(CancellationToken)

Gets the queue position, the count of connections the endpoints' rotation has reached.

public Task<long> GetQueuePosition(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<long>

A task whose result is the queue position; 0 when none is stored.

Exceptions

OperationCanceledException

cancellationToken was canceled.

List(CancellationToken)

Lists every endpoint.

public Task<IReadOnlyList<ProxyEndPointRecord>> List(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<IReadOnlyList<ProxyEndPointRecord>>

A task whose result is the endpoints, in no set order.

Exceptions

OperationCanceledException

cancellationToken was 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

options ProxyEndPointStoreListParams

The filter and the page.

cancellationToken CancellationToken

The 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

cancellationToken was 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

newEndPoints IReadOnlyList<ProxyEndPoint>

The new list.

maxItemCount int?

How many endpoints to keep, or null for the default.

maxPenalty int?

The penalty above which a used endpoint goes after the new ones, or null for the default.

removeDuplicateIps bool

true to drop a new endpoint whose host and protocol match a stored one whose last use succeeded.

cancellationToken CancellationToken

The 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

maxItemCount is 0 or less.

OperationCanceledException

cancellationToken was canceled.

ResetStatuses(CancellationToken)

Clears every endpoint's status, and the queue position.

public Task ResetStatuses(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task

A task that represents the asynchronous operation.

Exceptions

OperationCanceledException

cancellationToken was canceled.

SetCountryCode(string, string?, CancellationToken)

Sets the country code of a stored endpoint.

public Task SetCountryCode(string id, string? countryCode, CancellationToken cancellationToken)

Parameters

id string

The endpoint's ID; an ID not stored changes nothing.

countryCode string

The country code, or null to clear it.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task

A task that represents the asynchronous operation.

Exceptions

OperationCanceledException

cancellationToken was 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

queuePosition long

The queue position.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task

A task that represents the asynchronous operation.

Exceptions

OperationCanceledException

cancellationToken was 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

infos IReadOnlyList<ProxyEndPointInfo>

The endpoints and their statuses.

cancellationToken CancellationToken

The 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

cancellationToken was 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

records IReadOnlyList<ProxyEndPointRecord>

The endpoints.

keepExistingStatus bool

true to keep a stored endpoint's status.

keepExistingEnabled bool

true to keep whether a stored endpoint is enabled.

cancellationToken CancellationToken

The 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

cancellationToken was canceled.