Class VpnServiceManager
- Assembly
- VpnHood.Core.Client.VpnServices.Manager.dll
Manages the VPN service from the app's process: starts it with its options, follows its state, and sends it the app's requests.
public class VpnServiceManager : IDisposable
- Inheritance
-
VpnServiceManager
- Implements
- Inherited Members
Remarks
The service runs in a process of its own, as the platform's VPN extension, or, in a head that runs both halves, in the app's own process; it reports its state in a status file in its config folder and in each answer.
Constructors
VpnServiceManager(IDevice, TimeSpan?)
Initializes a new instance of the VpnServiceManager class for a device's VPN service: it makes the service's config folder, moves the service's old files there, and reads the last status.
public VpnServiceManager(IDevice device, TimeSpan? eventWatcherInterval)
Parameters
deviceIDeviceThe device, whose VPN service this manages.
eventWatcherIntervalTimeSpan?How often to refresh the connection's info in the background, for StateChanged, or null for no refresh after the first, which runs at the job runner's next tick.
Properties
ConnectionInfo
Gets the connection's info as last known; reading it starts a refresh in the background, at most one a second.
public ConnectionInfo ConnectionInfo { get; }
Property Value
IsConfigured
Gets a value indicating whether a connect has left options the VPN service can start from.
public bool IsConfigured { get; }
Property Value
IsReconfiguring
Gets a value indicating whether a reconfigure is being sent to the service.
public bool IsReconfiguring { get; }
Property Value
IsStarted
Gets a value indicating whether a start is under way or the connection is started (IsStarted()).
public bool IsStarted { get; }
Property Value
IsStopping
Gets a value indicating whether a stop is under way.
public bool IsStopping { get; }
Property Value
LogFilePath
Gets the path of the VPN service's log, in its config folder; each start deletes the last one.
public string LogFilePath { get; }
Property Value
Methods
ClearConfiguration(CancellationToken)
Deletes the saved options, so a start without the app fails, as before the first connect, rather than reach a server the app no longer has.
public Task ClearConfiguration(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that completes once the options are gone.
Exceptions
- IOException
The file could not be reached, as where the service still holds it after 2 seconds.
- UnauthorizedAccessException
The file may not be deleted.
- OperationCanceledException
cancellationTokenwas canceled.
Dispose()
Stops following the VPN service, which goes on running until it is stopped.
public void Dispose()
Reconfigure(VpnServiceOptions, CancellationToken)
Saves options made again from the app's settings, connected or not, for a later start without the app, where a connect has saved options before (IsConfigured); then, where the service is started, sends it at once the part a session takes live.
public Task Reconfigure(VpnServiceOptions serviceOptions, CancellationToken cancellationToken)
Parameters
serviceOptionsVpnServiceOptionsThe options; the saved plan and always-on mark are kept (Update(VpnServiceOptions, CancellationToken)).
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that completes once the service took them, or they are saved where it is not started.
Remarks
A start that is still bringing the service up takes them from the saved options, so a request that cannot reach the service then is no error.
Exceptions
- Exception
The service refused them: its error, as ClientExceptionConverter makes it.
- VpnServiceNotReadyException
The service cannot take requests.
- VpnServiceUnreachableException
The service cannot be reached.
- IOException
The saved options could not be read or written, as where the service still holds the file after 2 seconds.
- OperationCanceledException
cancellationTokenwas canceled.
RefreshState(CancellationToken)
Refreshes the connection's info now: from the service's status file, and from its answer while it is started.
public Task RefreshState(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that completes once the info is refreshed. Where the service cannot be reached, its info becomes Disposed, or None while a stop runs; any other failure is logged, and the last info stays.
SetAdFailed(Exception, CancellationToken)
Tells the VPN service the ad the session waits for could not be shown.
public Task SetAdFailed(Exception ex, CancellationToken cancellationToken)
Parameters
exExceptionThe error the ad failed with.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that completes once the service took it.
Exceptions
- Exception
The service refused it: its error, as ClientExceptionConverter makes it.
- VpnServiceNotReadyException
The service cannot take requests.
- VpnServiceUnreachableException
The service cannot be reached.
- OperationCanceledException
cancellationTokenwas canceled.
SetAdOk(AdResult, bool, CancellationToken)
Tells the VPN service the ad the session waits for was shown.
public Task SetAdOk(AdResult adResult, bool isRewarded, CancellationToken cancellationToken)
Parameters
adResultAdResultThe ad's result.
isRewardedbooltrue if it was a rewarded ad, whose data the service sends the server where it has any.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that completes once the service took it.
Exceptions
- Exception
The service refused it: its error, as ClientExceptionConverter makes it.
- VpnServiceNotReadyException
The service cannot take requests.
- VpnServiceUnreachableException
The service cannot be reached.
- OperationCanceledException
cancellationTokenwas canceled.
SetWaitForAd(CancellationToken)
Tells the VPN service the app is about to show the ad the session requires, so the session waits for it.
public Task SetWaitForAd(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that completes once the service took it.
Exceptions
- Exception
The service refused it: its error, as ClientExceptionConverter makes it.
- VpnServiceNotReadyException
The service cannot take requests.
- VpnServiceUnreachableException
The service cannot be reached.
- OperationCanceledException
cancellationTokenwas canceled.
Start(IUiContext?, VpnServiceOptions, CancellationToken)
Starts the VPN service with options, after it tries to stop one already started, and waits until it is connected.
public Task Start(IUiContext? uiContext, VpnServiceOptions serviceOptions, CancellationToken cancellationToken)
Parameters
uiContextIUiContextThe caller's window, which asks the person for the VPN permission where the OS needs that (Android), or null where it has none, and then a missing permission fails.
serviceOptionsVpnServiceOptionsThe options, which it saves for the service to read.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that completes once the service is connected.
Remarks
The OS's permission may take 2 minutes; once the device has started the service, the service has 20 seconds to say it is up; the connect itself has no time limit here. Under a debugger, both limits become the debugger's timeout, a day by default. With UseNullCapture, no permission is asked.
Exceptions
- VpnServiceTimeoutException
The service did not say it is up in 20 seconds.
- VpnServiceException
The service ended without a connection, and without an error.
- IOException
The options could not be saved, as where the service still holds the file after 2 seconds.
- OperationCanceledException
cancellationTokenwas canceled.- Exception
The connect failed: the service's error, as ClientExceptionConverter makes it; or the device's, where it could not ask for the permission or start the service, as Android's TimeoutException for a prompt not answered in time, or whose wait was canceled.
StartFromLastConfig(IUiContext?, CancellationToken)
Starts the VPN service with its saved options, unchanged, as always-on does, and waits until it is connected.
public Task StartFromLastConfig(IUiContext? uiContext, CancellationToken cancellationToken)
Parameters
uiContextIUiContextThe caller's window, as Start(IUiContext?, VpnServiceOptions, CancellationToken) takes it.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that completes once the service is connected.
Remarks
Check IsConfigured first; the rest is as Start(IUiContext?, VpnServiceOptions, CancellationToken).
Exceptions
- InvalidOperationException
There are no saved options, or they cannot be read.
- ObjectDisposedException
The manager is disposed.
- VpnServiceTimeoutException
The service did not say it is up in 20 seconds.
- VpnServiceException
The service ended without a connection, and without an error.
- IOException
The options could not be saved again, as where the service still holds the file after 2 seconds.
- OperationCanceledException
cancellationTokenwas canceled.- Exception
The connect failed: the service's error, as ClientExceptionConverter makes it; or the device's, where it could not ask for the permission or start the service.
TryStop(TimeSpan?)
Asks the VPN service to disconnect, if it is started, and waits until it says it has ended, which it does once it has let go of its resources, or can no longer be reached, which counts as its end.
public Task<bool> TryStop(TimeSpan? timeout = null)
Parameters
timeoutTimeSpan?How long the request to disconnect may take, or null for 5 seconds; the wait for the end takes up to 5 seconds more.
Returns
- Task<bool>
A task whose result is true if the service has ended, or had, by its word or as it can no longer be reached; otherwise, false, which it logs.
Remarks
It throws only for a bad timeout, and a second call while one runs waits for it. The wait's 5 seconds start once the request has ended, but where the request used up its whole time, the wait ends at once.
Exceptions
- ArgumentOutOfRangeException
timeoutis negative, other than infinite, or too long.
Events
Reconfigured
Occurs once a started service took a reconfigure, with its settings.
public event EventHandler<ClientReconfigureParams>? Reconfigured
Event Type
StateChanged
Occurs when the connection's state, or whether its adapter has started, changes; raised on the thread pool.
public event EventHandler? StateChanged