Class HttpAccessManager
- Namespace
- VpnHood.Core.Server.Access.Managers.HttpAccessManagers
- Assembly
- VpnHood.Core.Server.Access.dll
Represents an access manager reached over HTTP, at its agent API under api/agent/.
public class HttpAccessManager : ApiClientBase, IAccessManager, IDisposable
- Inheritance
-
HttpAccessManager
- Implements
- Inherited Members
Remarks
JSON goes in camel case. The agent's 503, a gateway's 502 or 504, Cloudflare's 520 to 524, and a refused connection are taken as maintenance, and fail a call with MaintenanceException; 500 is the agent's own error.
Constructors
HttpAccessManager(HttpClient)
Initializes a new instance of the HttpAccessManager class over an HTTP client, whose own base address and headers serve.
public HttpAccessManager(HttpClient httpClient)
Parameters
httpClientHttpClientThe HTTP client, which stays the caller's.
HttpAccessManager(HttpClient, HttpAccessManagerOptions)
Initializes a new instance of the HttpAccessManager class over an HTTP client, at the options'
address under api/agent/, with their authorization where it parses.
public HttpAccessManager(HttpClient httpClient, HttpAccessManagerOptions options)
Parameters
httpClientHttpClientThe HTTP client, which stays the caller's.
optionsHttpAccessManagerOptionsThe access manager's address and authorization.
HttpAccessManager(HttpAccessManagerOptions)
Initializes a new instance of the HttpAccessManager class with an HTTP client of its own.
public HttpAccessManager(HttpAccessManagerOptions options)
Parameters
optionsHttpAccessManagerOptionsThe access manager's address and authorization.
Methods
Acme_GetHttp01KeyAuthorization(string, CancellationToken)
Gets the key authorization of an ACME HTTP-01 challenge's token, which the server serves on port 80.
public Task<string> Acme_GetHttp01KeyAuthorization(string token, CancellationToken cancellationToken)
Parameters
tokenstringThe challenge's token.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
Exceptions
- KeyNotFoundException
The access manager knows no such token, or serves no HTTP-01 challenge.
- KeyNotFoundException
The access manager answered 404: it knows no such token.
- MaintenanceException
The access manager is under maintenance, or refused the connection.
- ApiException
The access manager answered with another error, or with an answer that could not be read, or that read as null where a body was expected.
- OperationCanceledException
cancellationTokenwas canceled, or the HTTP client's own timeout passed.
CreateSerializerSettings()
Creates the JSON settings, once. Without a resolver of its own, the settings read by reflection where it is on.
protected override JsonSerializerOptions CreateSerializerSettings()
Returns
- JsonSerializerOptions
The settings. The default is .NET's defaults.
Dispose()
Does nothing, not even for an HTTP client it made itself.
public virtual void Dispose()
HttpSendAsync<T>(string, Dictionary<string, object?>?, HttpRequestMessage, CancellationToken)
Sends a request the caller made, and reads its response as an object, with the response.
protected override Task<ApiClientBase.HttpResult<T>> HttpSendAsync<T>(string urlPart, Dictionary<string, object?>? parameters, HttpRequestMessage request, CancellationToken cancellationToken)
Parameters
urlPartstringThe URL, relative to DefaultBaseAddress or absolute.
parametersDictionary<string, object>The query's parameters, or null for none.
requestHttpRequestMessageThe request, which the caller keeps owning.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<ApiClientBase.HttpResult<T>>
A task whose result is the object with the response.
Type Parameters
TThe object's type.
Exceptions
- ApiException
The status is not a success, or the response cannot be read.
- Exception
The exception ToException(ApiError) makes for an error response, or the client has no HttpClient.
- OperationCanceledException
cancellationTokenwas canceled, or the HTTP client timed out.- MaintenanceException
The access manager refused the connection, or its answer's status says it is under maintenance.
ProcessResponseAsync(HttpClient, HttpResponseMessage, CancellationToken)
Processes a response before it is read; the base does nothing.
protected override Task ProcessResponseAsync(HttpClient client, HttpResponseMessage response, CancellationToken cancellationToken)
Parameters
clientHttpClientThe HTTP client.
responseHttpResponseMessageThe response.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
Exceptions
- MaintenanceException
The answer's status says the access manager is under maintenance.
Server_Configure(ServerInfo, CancellationToken)
Sends what the server is and has, and gets its configuration.
public Task<ServerConfig> Server_Configure(ServerInfo serverInfo, CancellationToken cancellationToken)
Parameters
serverInfoServerInfoThe server's info.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<ServerConfig>
A task whose result is the configuration.
Exceptions
- MaintenanceException
The access manager is under maintenance, or refused the connection.
- ApiException
The access manager answered with another error, or with an answer that could not be read, or that read as null where a body was expected.
- OperationCanceledException
cancellationTokenwas canceled, or the HTTP client's own timeout passed.
Server_UpdateStatus(ServerStatus, CancellationToken)
Sends the server's status, with its sessions' usage, and gets what the access manager wants of it.
public Task<ServerCommand> Server_UpdateStatus(ServerStatus serverStatus, CancellationToken cancellationToken)
Parameters
serverStatusServerStatusThe status.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<ServerCommand>
A task whose result is the access manager's answer: the configuration's code and the sessions' states.
Exceptions
- MaintenanceException
The access manager is under maintenance, or refused the connection.
- ApiException
The access manager answered with another error, or with an answer that could not be read, or that read as null where a body was expected.
- OperationCanceledException
cancellationTokenwas canceled, or the HTTP client's own timeout passed.
Session_AddUsage(ulong, Traffic, string?, CancellationToken)
Adds a session's traffic, and a rewarded ad's data, without closing it.
public Task<SessionResponse> Session_AddUsage(ulong sessionId, Traffic traffic, string? adData, CancellationToken cancellationToken)
Parameters
sessionIdulongThe session's ID.
trafficTrafficThe session's traffic since its last report.
adDatastringA rewarded ad's data, or null.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<SessionResponse>
A task whose result is the session's state.
Exceptions
- MaintenanceException
The access manager is under maintenance, or refused the connection.
- ApiException
The access manager answered with another error, or with an answer that could not be read, or that read as null where a body was expected.
- OperationCanceledException
cancellationTokenwas canceled, or the HTTP client's own timeout passed.
Session_AddUsages(SessionUsage[], CancellationToken)
Adds the traffic of many sessions, and their closes, in one call.
public Task<Dictionary<ulong, SessionResponse>> Session_AddUsages(SessionUsage[] sessionUsages, CancellationToken cancellationToken)
Parameters
sessionUsagesSessionUsage[]The sessions' usage.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<Dictionary<ulong, SessionResponse>>
A task whose result is each session's state, by its ID; the server sends again no entry the answer leaves out or answers with an error.
Exceptions
- MaintenanceException
The access manager is under maintenance, or refused the connection.
- ApiException
The access manager answered with another error, or with an answer that could not be read, or that read as null where a body was expected.
- OperationCanceledException
cancellationTokenwas canceled, or the HTTP client's own timeout passed.
Session_Close(ulong, Traffic, CancellationToken)
Closes a session with its last traffic.
public Task<SessionResponse> Session_Close(ulong sessionId, Traffic traffic, CancellationToken cancellationToken)
Parameters
sessionIdulongThe session's ID.
trafficTrafficThe session's traffic since its last report.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<SessionResponse>
A task whose result is the session's last state.
Exceptions
- MaintenanceException
The access manager is under maintenance, or refused the connection.
- ApiException
The access manager answered with another error, or with an answer that could not be read, or that read as null where a body was expected.
- OperationCanceledException
cancellationTokenwas canceled, or the HTTP client's own timeout passed.
Session_Create(SessionRequestEx, CancellationToken)
Creates a session for a client's hello.
public Task<SessionResponseEx> Session_Create(SessionRequestEx sessionRequestEx, CancellationToken cancellationToken)
Parameters
sessionRequestExSessionRequestExThe client's request, with what the server knows of the client.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<SessionResponseEx>
A task whose result is the session, or its refusal in its error code.
Exceptions
- MaintenanceException
The access manager is under maintenance, or refused the connection.
- ApiException
The access manager answered with another error, or with an answer that could not be read, or that read as null where a body was expected.
- OperationCanceledException
cancellationTokenwas canceled, or the HTTP client's own timeout passed.
Session_Get(ulong, IPEndPoint, IPAddress?, CancellationToken)
Gets a session the server does not hold in memory, as after a restart, to take it up again.
public Task<SessionResponseEx> Session_Get(ulong sessionId, IPEndPoint hostEndPoint, IPAddress? clientIp, CancellationToken cancellationToken)
Parameters
sessionIdulongThe session's ID.
hostEndPointIPEndPointThe server's endpoint the client came to.
clientIpIPAddressThe client's IP, or null.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<SessionResponseEx>
A task whose result is the session, or its error in its error code.
Exceptions
- MaintenanceException
The access manager is under maintenance, or refused the connection.
- ApiException
The access manager answered with another error, or with an answer that could not be read, or that read as null where a body was expected.
- OperationCanceledException
cancellationTokenwas canceled, or the HTTP client's own timeout passed.
Session_GetAll(CancellationToken)
Gets the server's sessions, to take them up again at its start.
public Task<SessionResponseEx[]> Session_GetAll(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<SessionResponseEx[]>
A task whose result is the sessions.
Exceptions
- MaintenanceException
The access manager is under maintenance, or refused the connection.
- ApiException
The access manager answered with another error, or with an answer that could not be read, or that read as null where a body was expected.
- OperationCanceledException
cancellationTokenwas canceled, or the HTTP client's own timeout passed.