bump to 0.3.1

This commit is contained in:
Stanley Dimant
2022-08-04 01:18:05 +02:00
parent 5907202648
commit 07bad0881e
6 changed files with 103 additions and 94 deletions

View File

@@ -1,12 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using MareSynchronos.API;
using MareSynchronos.Utils;
using MareSynchronos.WebAPI.Utils;
using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Client;
namespace MareSynchronos.WebAPI
@@ -20,7 +22,7 @@ namespace MareSynchronos.WebAPI
Connected,
Unauthorized,
VersionMisMatch,
NoAccount
RateLimited
}
public partial class ApiController : IDisposable
@@ -54,6 +56,7 @@ namespace MareSynchronos.WebAPI
_connectionCancellationTokenSource = new CancellationTokenSource();
_dalamudUtil.LogIn += DalamudUtilOnLogIn;
_dalamudUtil.LogOut += DalamudUtilOnLogOut;
ServerState = ServerState.Offline;
if (_dalamudUtil.IsLoggedIn)
{
@@ -107,11 +110,9 @@ namespace MareSynchronos.WebAPI
public List<ClientPairDto> PairedClients { get; set; } = new();
public string SecretKey => _pluginConfiguration.ClientSecret.ContainsKey(ApiUri)
? _pluginConfiguration.ClientSecret[ApiUri]
: "-";
? _pluginConfiguration.ClientSecret[ApiUri] : string.Empty;
public bool ServerAlive =>
(_mareHub?.State ?? HubConnectionState.Disconnected) == HubConnectionState.Connected;
public bool ServerAlive => ServerState is ServerState.Connected or ServerState.RateLimited or ServerState.Unauthorized or ServerState.Disconnected;
public Dictionary<string, string> ServerDictionary => new Dictionary<string, string>()
{ { MainServiceUri, MainServer } }
@@ -122,29 +123,17 @@ namespace MareSynchronos.WebAPI
private string ApiUri => _pluginConfiguration.ApiUri;
public int OnlineUsers => SystemInfoDto.OnlineUsers;
public ServerState ServerState
{
get
{
var supportedByServer = SupportedServerVersions.Contains(_connectionDto?.ServerVersion ?? 0);
bool hasUid = !string.IsNullOrEmpty(UID);
if (_pluginConfiguration.FullPause)
return ServerState.Disconnected;
if (!ServerAlive)
return ServerState.Offline;
if (!hasUid && _pluginConfiguration.ClientSecret.ContainsKey(ApiUri))
return ServerState.Unauthorized;
if (!supportedByServer)
return ServerState.VersionMisMatch;
if (supportedByServer && hasUid)
return ServerState.Connected;
return ServerState.NoAccount;
}
}
public ServerState ServerState { get; private set; }
public async Task CreateConnections()
{
if (_pluginConfiguration.FullPause)
{
ServerState = ServerState.Disconnected;
_connectionDto = null;
return;
}
Logger.Info("Recreating Connection");
await StopConnection(_connectionCancellationTokenSource.Token);
@@ -154,6 +143,12 @@ namespace MareSynchronos.WebAPI
var token = _connectionCancellationTokenSource.Token;
while (ServerState is not ServerState.Connected && !token.IsCancellationRequested)
{
if (string.IsNullOrEmpty(SecretKey))
{
await Task.Delay(TimeSpan.FromSeconds(2));
continue;
}
await StopConnection(token);
try
@@ -174,14 +169,18 @@ namespace MareSynchronos.WebAPI
_mareHub.On<SystemInfoDto>(Api.OnUpdateSystemInfo, (dto) => SystemInfoDto = dto);
if (_pluginConfiguration.FullPause)
_connectionDto =
await _mareHub.InvokeAsync<ConnectionDto>(Api.InvokeHeartbeat, _dalamudUtil.PlayerNameHashed, token);
ServerState = ServerState.Connected;
if (_connectionDto.ServerVersion != Api.Version)
{
_connectionDto = null;
ServerState = ServerState.VersionMisMatch;
await StopConnection(token);
return;
}
_connectionDto =
await _mareHub.InvokeAsync<ConnectionDto>(Api.InvokeHeartbeat, _dalamudUtil.PlayerNameHashed, token);
if (ServerState is ServerState.Connected) // user is authorized && server is legit
{
await InitializeData(token);
@@ -190,17 +189,42 @@ namespace MareSynchronos.WebAPI
_mareHub.Reconnected += MareHubOnReconnected;
_mareHub.Reconnecting += MareHubOnReconnecting;
}
else if (ServerState is ServerState.VersionMisMatch or ServerState.NoAccount or ServerState.Unauthorized)
}
catch (HubException ex)
{
Logger.Warn(ex.GetType().ToString());
Logger.Warn(ex.Message);
Logger.Warn(ex.StackTrace ?? string.Empty);
ServerState = ServerState.RateLimited;
await StopConnection(token);
return;
}
catch (HttpRequestException ex)
{
Logger.Warn(ex.GetType().ToString());
Logger.Warn(ex.Message);
Logger.Warn(ex.StackTrace ?? string.Empty);
if (ex.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
break;
ServerState = ServerState.Unauthorized;
await StopConnection(token);
return;
}
else
{
ServerState = ServerState.Offline;
Logger.Info("Failed to establish connection, retrying");
await Task.Delay(TimeSpan.FromSeconds(new Random().Next(5, 20)), token);
}
}
catch (Exception ex)
{
Logger.Warn(ex.GetType().ToString());
Logger.Warn(ex.Message);
Logger.Warn(ex.StackTrace ?? string.Empty);
Logger.Info("Failed to establish connection, retrying");
await StopConnection(token);
await Task.Delay(TimeSpan.FromSeconds(new Random().Next(5, 20)), token);
}
}
@@ -260,11 +284,7 @@ namespace MareSynchronos.WebAPI
return new HubConnectionBuilder()
.WithUrl(ApiUri + hubName, options =>
{
if (!string.IsNullOrEmpty(SecretKey) && !_pluginConfiguration.FullPause)
{
options.Headers.Add("Authorization", SecretKey);
}
options.Headers.Add("Authorization", SecretKey);
options.Transports = HttpTransportType.WebSockets;
})
.WithAutomaticReconnect(new ForeverRetryPolicy())
@@ -278,6 +298,7 @@ namespace MareSynchronos.WebAPI
_uploadCancellationTokenSource?.Cancel();
Logger.Info("Connection closed");
Disconnected?.Invoke();
ServerState = ServerState.Offline;
return Task.CompletedTask;
}
@@ -294,6 +315,7 @@ namespace MareSynchronos.WebAPI
CurrentUploads.Clear();
CurrentDownloads.Clear();
_uploadCancellationTokenSource?.Cancel();
ServerState = ServerState.Disconnected;
Logger.Warn("Connection closed... Reconnecting");
Logger.Warn(arg?.Message ?? string.Empty);
Logger.Warn(arg?.StackTrace ?? string.Empty);
@@ -305,7 +327,7 @@ namespace MareSynchronos.WebAPI
{
if (_mareHub is not null)
{
Logger.Info("Stopping all connections");
Logger.Info("Stopping existing connection");
await _mareHub.StopAsync(token);
_mareHub.Closed -= MareHubOnClosed;
_mareHub.Reconnected -= MareHubOnReconnected;