Class VpnHoodClient
- Namespace
- VpnHood.Core.Client
- Assembly
- VpnHood.Core.Client.dll
Represents the VPN client: it finds a server, builds a session with it, and runs the session's tunnel on a VPN adapter, until the session ends or the client is disposed.
public class VpnHoodClient : IDisposable, IAsyncDisposable
- Inheritance
-
VpnHoodClient
- Implements
- Inherited Members
Remarks
A client connects once: its session's end disposes it, and the next connect takes a new client.
Constructors
VpnHoodClient(IVpnAdapter, IClientSocketFactory, NetFilter?, IProxyConnector?, ITracker?, string, ClientOptions, HostNameAddresses?)
Initializes a new instance of the VpnHoodClient class.
public VpnHoodClient(IVpnAdapter vpnAdapter, IClientSocketFactory socketFactory, NetFilter? netFilter, IProxyConnector? proxyConnector, ITracker? tracker, string workingFolder, ClientOptions options, HostNameAddresses? knownHostAddresses = null)
Parameters
vpnAdapterIVpnAdapterThe VPN adapter, which the connect starts; the client disposes it with itself where AutoDisposeVpnAdapter, and the adapter's end disposes the client.
socketFactoryIClientSocketFactoryThe factory of the client's sockets; the client protects each socket it makes from the VPN adapter, where the adapter can, but not the ones the factory's QUIC opens for itself.
netFilterNetFilterThe caller's filters, the splits by IP and by domain and the IP mapper, or null.
proxyConnectorIProxyConnectorThe proxies to reach the servers through, which the client owns, or null to connect directly.
trackerITrackerThe tracker to report the client's events to, or null.
workingFolderstringThe client's folder, which it makes where it is missing.
optionsClientOptionsThe options.
knownHostAddressesHostNameAddressesThe addresses the access key's host name gave already, which the connect takes rather than asking again, or null.
Exceptions
- ArgumentNullException
The options' longest packet channel age is below their shortest and not infinite.
- FormatException
The access key is not valid.
- NotSupportedException
The access key's version is not 4.
- JsonException
The access key's JSON is not valid.
- InvalidDataException
The access key's JSON is
null.- OverflowException
A duration in the access key is out of range.
- InvalidOperationException
The access key has no farm secret.
Properties
ChannelProtocol
Gets or sets the requested protocol of the tunnel's packet channels (ChannelProtocol), which the session applies at once; its status gives the one in effect, as a requested QUIC or UDP that cannot be had falls back to TCP.
public ChannelProtocol ChannelProtocol { get; set; }
Property Value
Config
Gets the client's settings, from the options it was made with.
public VpnHoodClientConfig Config { get; }
Property Value
DropQuic
Gets or sets the requested dropping of the QUIC bound for the tunnel (DropQuic), which the session applies at once; its status gives what is in effect.
public bool DropQuic { get; set; }
Property Value
Remarks
It starts false whatever the options say, and gives the last value set; a value set before the session is built does not change how the session starts, which is from the options.
DropUdp
Gets or sets the requested dropping of the UDP bound for the tunnel (DropUdp), which the session applies at once; it is in effect only while the client forwards IPv4 TCP.
public bool DropUdp { get; set; }
Property Value
Remarks
It starts false whatever the options say, and gives the last value set; a value set before the session is built does not change how the session starts, which is from the options.
LastException
Gets the error that ended the client's connect, or else its session's last one, or null; a connect's error is not kept where a shutdown was asked for meanwhile.
public Exception? LastException { get; }
Property Value
NetFilter
Gets the client's filters, the outermost stages over the caller's, so its owner can roll a reconfigure down them
(IIpFilter.Reconfigure).
public NetFilter NetFilter { get; }
Property Value
Remarks
The client neither knows nor cares what reacts: the stages that own an outside configuration read it again, the change rolls back up, and the caches and the reading of server names follow by themselves.
ProxyConnector
Gets the proxies the client reaches the servers through, or null where it connects directly.
public IProxyConnector? ProxyConnector { get; }
Property Value
RequiredSession
Gets the client's session.
public IClientSession RequiredSession { get; }
Property Value
Exceptions
- InvalidOperationException
The session is not built yet.
Session
Gets the client's session, or null before it is built.
public IClientSession? Session { get; }
Property Value
State
Gets the client's state.
public ClientState State { get; }
Property Value
StateChangedTime
Gets when the client's state last changed, in UTC.
public DateTime StateChangedTime { get; }
Property Value
StateProgress
Gets the progress of the state's work, while the client finds a server or checks its proxies, or null.
public ProgressStatus? StateProgress { get; }
Property Value
Tracker
Gets the tracker the client reports its events to, or null.
public ITracker? Tracker { get; }
Property Value
UnroutedIpMode
Gets or sets what happens to a destination the server does not route (UnroutedIpMode); it applies at once.
public SplitUnsupportedIpMode UnroutedIpMode { get; set; }
Property Value
Remarks
Only the filter's verdict changes: the VPN adapter keeps what it captures from the connect, so a change that needs it to capture otherwise, as from Exclude to Block against a server that splits, takes a reconnect.
UnsupportedIpV6Mode
Gets or sets what happens to IPv6 where the server carries none (UnsupportedIpV6Mode); it applies at once.
public SplitUnsupportedIpMode UnsupportedIpV6Mode { get; set; }
Property Value
Remarks
As with UnroutedIpMode, only the filter's verdict changes.
UseTcpForwarding
Gets or sets the requested TCP forwarding (UseTcpForwarding), which the session applies at once; its status gives what is in effect, which the server and the domain filters decide too.
public bool UseTcpForwarding { get; set; }
Property Value
Remarks
It starts false whatever the options say, and gives the last value set; a value set before the session is built does not change how the session starts, which is from the options.
Methods
Connect(CancellationToken)
Connects: finds a server, builds a session with it, and starts the session's tunnel on the VPN adapter.
public Task Connect(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that completes once the client is connected.
Remarks
A failure disposes the client; where the session ended with an error of its own meanwhile, that error is thrown, rather than how the connect found out.
Exceptions
- ObjectDisposedException
The client is disposed.
- Exception
The client has connected, or is connecting, already; a redirect named no server endpoint; or the ad the session required failed, with that failure.
- InvalidOperationException
The VPN adapter has started already; the tunnel's MTU is below 1280; or no DNS server can go through the tunnel where IncludeAll keeps DNS in it.
- UnreachableServerException
No server answered.
- UnreachableProxyServerException
No proxy server answered.
- NotSupportedException
The server's protocol is outside the client's, it named no client address, or it carries TCP neither as packets nor by forwarding.
- ConnectionTimeoutException
The client could not connect to the server in time.
- TimeoutException
A request to the server got no answer in time.
- SessionException
The server refused the session.
- MaintenanceException
The server is in maintenance.
- UnauthorizedAccessException
The server refused the request's proof.
- OperationCanceledException
cancellationTokenwas canceled, or the client was disposed meanwhile.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Remarks
The quick close: the session, the proxies, the filters and, where AutoDisposeVpnAdapter, the VPN adapter close without waiting, some of their closes going on in the background, and the state becomes Disposed.
DisposeAsync()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
public ValueTask DisposeAsync()
Returns
- ValueTask
A task that represents the asynchronous dispose operation.
Remarks
The graceful close: it closes the session, which tells its server where it can, and the proxies, and releases the client as Dispose() does, which the session's end may have started meanwhile. A second call waits for the first one's end.
Events
StateChanged
Occurs when the client's state changes, raised on the thread pool, each change on its own, so two raises may overlap or come out of order; the handlers are let go once the Disposed raise has run.
public event EventHandler? StateChanged