This commit is contained in:
Eauldane
2025-08-22 02:19:48 +01:00
commit a4c82452be
373 changed files with 52044 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
using MareSynchronos.API.Dto.Account;
using MareSynchronos.API.Routes;
using MareSynchronos.Services;
using MareSynchronos.Services.ServerConfiguration;
using MareSynchronos.Utils;
using MareSynchronos.WebAPI.SignalR;
using Microsoft.Extensions.Logging;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Reflection;
using System.Security.Cryptography;
namespace MareSynchronos.WebAPI;
public sealed class AccountRegistrationService : IDisposable
{
private readonly HttpClient _httpClient;
private readonly ILogger<AccountRegistrationService> _logger;
private readonly ServerConfigurationManager _serverManager;
private readonly RemoteConfigurationService _remoteConfig;
private string GenerateSecretKey()
{
return Convert.ToHexString(SHA256.HashData(RandomNumberGenerator.GetBytes(64)));
}
public AccountRegistrationService(ILogger<AccountRegistrationService> logger, ServerConfigurationManager serverManager, RemoteConfigurationService remoteConfig)
{
_logger = logger;
_serverManager = serverManager;
_remoteConfig = remoteConfig;
_httpClient = new(
new HttpClientHandler
{
AllowAutoRedirect = true,
MaxAutomaticRedirections = 5
}
);
var ver = Assembly.GetExecutingAssembly().GetName().Version;
_httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("MareSynchronos", ver!.Major + "." + ver!.Minor + "." + ver!.Build));
}
public void Dispose()
{
_httpClient.Dispose();
}
public async Task<RegisterReplyDto> RegisterAccount(CancellationToken token)
{
var authApiUrl = _serverManager.CurrentApiUrl;
// Override the API URL used for auth from remote config, if one is available
if (authApiUrl.Equals(ApiController.ElezenServiceUri, StringComparison.Ordinal))
{
var config = await _remoteConfig.GetConfigAsync<HubConnectionConfig>("mainServer").ConfigureAwait(false) ?? new();
if (!string.IsNullOrEmpty(config.ApiUrl))
authApiUrl = config.ApiUrl;
else
authApiUrl = ApiController.ElezenServiceApiUri;
}
var secretKey = GenerateSecretKey();
var hashedSecretKey = secretKey.GetHash256();
Uri postUri = MareAuth.AuthRegisterV2FullPath(new Uri(authApiUrl
.Replace("wss://", "https://", StringComparison.OrdinalIgnoreCase)
.Replace("ws://", "http://", StringComparison.OrdinalIgnoreCase)));
var result = await _httpClient.PostAsync(postUri, new FormUrlEncodedContent([
new("hashedSecretKey", hashedSecretKey)
]), token).ConfigureAwait(false);
result.EnsureSuccessStatusCode();
var response = await result.Content.ReadFromJsonAsync<RegisterReplyV2Dto>(token).ConfigureAwait(false) ?? new();
return new RegisterReplyDto()
{
Success = response.Success,
ErrorMessage = response.ErrorMessage,
UID = response.UID,
SecretKey = secretKey
};
}
}

View File

@@ -0,0 +1,510 @@
using Dalamud.Utility;
using K4os.Compression.LZ4.Streams;
using MareSynchronos.API.Data;
using MareSynchronos.API.Dto.Files;
using MareSynchronos.API.Routes;
using MareSynchronos.FileCache;
using MareSynchronos.PlayerData.Handlers;
using MareSynchronos.Services.Mediator;
using MareSynchronos.Utils;
using MareSynchronos.WebAPI.Files.Models;
using Microsoft.Extensions.Logging;
using System.Net;
using System.Net.Http.Json;
using System.Security.Cryptography;
namespace MareSynchronos.WebAPI.Files;
public partial class FileDownloadManager : DisposableMediatorSubscriberBase
{
private readonly Dictionary<string, FileDownloadStatus> _downloadStatus;
private readonly FileCompactor _fileCompactor;
private readonly FileCacheManager _fileDbManager;
private readonly FileTransferOrchestrator _orchestrator;
private readonly List<ThrottledStream> _activeDownloadStreams;
public FileDownloadManager(ILogger<FileDownloadManager> logger, MareMediator mediator,
FileTransferOrchestrator orchestrator,
FileCacheManager fileCacheManager, FileCompactor fileCompactor) : base(logger, mediator)
{
_downloadStatus = new Dictionary<string, FileDownloadStatus>(StringComparer.Ordinal);
_orchestrator = orchestrator;
_fileDbManager = fileCacheManager;
_fileCompactor = fileCompactor;
_activeDownloadStreams = [];
Mediator.Subscribe<DownloadLimitChangedMessage>(this, (msg) =>
{
if (!_activeDownloadStreams.Any()) return;
var newLimit = _orchestrator.DownloadLimitPerSlot();
Logger.LogTrace("Setting new Download Speed Limit to {newLimit}", newLimit);
foreach (var stream in _activeDownloadStreams)
{
stream.BandwidthLimit = newLimit;
}
});
}
public List<DownloadFileTransfer> CurrentDownloads { get; private set; } = [];
public List<FileTransfer> ForbiddenTransfers => _orchestrator.ForbiddenTransfers;
public bool IsDownloading => !CurrentDownloads.Any();
public void ClearDownload()
{
CurrentDownloads.Clear();
_downloadStatus.Clear();
}
public async Task DownloadFiles(GameObjectHandler gameObject, List<FileReplacementData> fileReplacementDto, CancellationToken ct)
{
Mediator.Publish(new HaltScanMessage(nameof(DownloadFiles)));
try
{
await DownloadFilesInternal(gameObject, fileReplacementDto, ct).ConfigureAwait(false);
}
catch
{
ClearDownload();
}
finally
{
Mediator.Publish(new DownloadFinishedMessage(gameObject));
Mediator.Publish(new ResumeScanMessage(nameof(DownloadFiles)));
}
}
protected override void Dispose(bool disposing)
{
ClearDownload();
foreach (var stream in _activeDownloadStreams.ToList())
{
try
{
stream.Dispose();
}
catch
{
// do nothing
//
}
}
base.Dispose(disposing);
}
private static byte ConvertReadByte(int byteOrEof)
{
if (byteOrEof == -1)
{
throw new EndOfStreamException();
}
return (byte)byteOrEof;
}
private static (string fileHash, long fileLengthBytes) ReadBlockFileHeader(FileStream fileBlockStream)
{
List<char> hashName = [];
List<char> fileLength = [];
var separator = (char)ConvertReadByte(fileBlockStream.ReadByte());
if (separator != '#') throw new InvalidDataException("Data is invalid, first char is not #");
bool readHash = false;
while (true)
{
int readByte = fileBlockStream.ReadByte();
if (readByte == -1)
throw new EndOfStreamException();
var readChar = (char)ConvertReadByte(readByte);
if (readChar == ':')
{
readHash = true;
continue;
}
if (readChar == '#') break;
if (!readHash) hashName.Add(readChar);
else fileLength.Add(readChar);
}
if (fileLength.Count == 0)
fileLength.Add('0');
return (string.Join("", hashName), long.Parse(string.Join("", fileLength)));
}
private async Task DownloadAndMungeFileHttpClient(string downloadGroup, Guid requestId, List<DownloadFileTransfer> fileTransfer, string tempPath, IProgress<long> progress, CancellationToken ct)
{
Logger.LogDebug("GUID {requestId} on server {uri} for files {files}", requestId, fileTransfer[0].DownloadUri, string.Join(", ", fileTransfer.Select(c => c.Hash).ToList()));
await WaitForDownloadReady(fileTransfer, requestId, ct).ConfigureAwait(false);
_downloadStatus[downloadGroup].DownloadStatus = DownloadStatus.Downloading;
HttpResponseMessage response = null!;
var requestUrl = MareFiles.CacheGetFullPath(fileTransfer[0].DownloadUri, requestId);
Logger.LogDebug("Downloading {requestUrl} for request {id}", requestUrl, requestId);
try
{
response = await _orchestrator.SendRequestAsync(HttpMethod.Get, requestUrl, ct, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException ex)
{
Logger.LogWarning(ex, "Error during download of {requestUrl}, HttpStatusCode: {code}", requestUrl, ex.StatusCode);
if (ex.StatusCode is HttpStatusCode.NotFound or HttpStatusCode.Unauthorized)
{
throw new InvalidDataException($"Http error {ex.StatusCode} (cancelled: {ct.IsCancellationRequested}): {requestUrl}", ex);
}
}
ThrottledStream? stream = null;
try
{
var fileStream = File.Create(tempPath);
await using (fileStream.ConfigureAwait(false))
{
var bufferSize = response.Content.Headers.ContentLength > 1024 * 1024 ? 65536 : 8196;
var buffer = new byte[bufferSize];
var bytesRead = 0;
var limit = _orchestrator.DownloadLimitPerSlot();
Logger.LogTrace("Starting Download of {id} with a speed limit of {limit} to {tempPath}", requestId, limit, tempPath);
stream = new ThrottledStream(await response.Content.ReadAsStreamAsync(ct).ConfigureAwait(false), limit);
_activeDownloadStreams.Add(stream);
while ((bytesRead = await stream.ReadAsync(buffer, ct).ConfigureAwait(false)) > 0)
{
ct.ThrowIfCancellationRequested();
await fileStream.WriteAsync(buffer.AsMemory(0, bytesRead), ct).ConfigureAwait(false);
progress.Report(bytesRead);
}
Logger.LogDebug("{requestUrl} downloaded to {tempPath}", requestUrl, tempPath);
}
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception)
{
try
{
if (!tempPath.IsNullOrEmpty())
File.Delete(tempPath);
}
catch
{
// ignore if file deletion fails
}
throw;
}
finally
{
if (stream != null)
{
_activeDownloadStreams.Remove(stream);
await stream.DisposeAsync().ConfigureAwait(false);
}
}
}
public async Task<List<DownloadFileTransfer>> InitiateDownloadList(GameObjectHandler gameObjectHandler, List<FileReplacementData> fileReplacement, CancellationToken ct)
{
Logger.LogDebug("Download start: {id}", gameObjectHandler.Name);
List<DownloadFileDto> downloadFileInfoFromService =
[
.. await FilesGetSizes(fileReplacement.Select(f => f.Hash).Distinct(StringComparer.Ordinal).ToList(), ct).ConfigureAwait(false),
];
Logger.LogDebug("Files with size 0 or less: {files}", string.Join(", ", downloadFileInfoFromService.Where(f => f.Size <= 0).Select(f => f.Hash)));
foreach (var dto in downloadFileInfoFromService.Where(c => c.IsForbidden))
{
if (!_orchestrator.ForbiddenTransfers.Exists(f => string.Equals(f.Hash, dto.Hash, StringComparison.Ordinal)))
{
_orchestrator.ForbiddenTransfers.Add(new DownloadFileTransfer(dto));
}
}
CurrentDownloads = downloadFileInfoFromService.Distinct().Select(d => new DownloadFileTransfer(d))
.Where(d => d.CanBeTransferred).ToList();
return CurrentDownloads;
}
private async Task DownloadFilesInternal(GameObjectHandler gameObjectHandler, List<FileReplacementData> fileReplacement, CancellationToken ct)
{
var downloadGroups = CurrentDownloads.GroupBy(f => f.DownloadUri.Host + ":" + f.DownloadUri.Port, StringComparer.Ordinal);
foreach (var downloadGroup in downloadGroups)
{
_downloadStatus[downloadGroup.Key] = new FileDownloadStatus()
{
DownloadStatus = DownloadStatus.Initializing,
TotalBytes = downloadGroup.Sum(c => c.Total),
TotalFiles = 1,
TransferredBytes = 0,
TransferredFiles = 0
};
}
Mediator.Publish(new DownloadStartedMessage(gameObjectHandler, _downloadStatus));
await Parallel.ForEachAsync(downloadGroups, new ParallelOptions()
{
MaxDegreeOfParallelism = downloadGroups.Count(),
CancellationToken = ct,
},
async (fileGroup, token) =>
{
// let server predownload files
var requestIdResponse = await _orchestrator.SendRequestAsync(HttpMethod.Post, MareFiles.RequestEnqueueFullPath(fileGroup.First().DownloadUri),
fileGroup.Select(c => c.Hash), token).ConfigureAwait(false);
Logger.LogDebug("Sent request for {n} files on server {uri} with result {result}", fileGroup.Count(), fileGroup.First().DownloadUri,
await requestIdResponse.Content.ReadAsStringAsync(token).ConfigureAwait(false));
Guid requestId = Guid.Parse((await requestIdResponse.Content.ReadAsStringAsync().ConfigureAwait(false)).Trim('"'));
Logger.LogDebug("GUID {requestId} for {n} files on server {uri}", requestId, fileGroup.Count(), fileGroup.First().DownloadUri);
var blockFile = _fileDbManager.GetCacheFilePath(requestId.ToString("N"), "blk");
FileInfo fi = new(blockFile);
try
{
_downloadStatus[fileGroup.Key].DownloadStatus = DownloadStatus.WaitingForSlot;
await _orchestrator.WaitForDownloadSlotAsync(token).ConfigureAwait(false);
_downloadStatus[fileGroup.Key].DownloadStatus = DownloadStatus.WaitingForQueue;
Progress<long> progress = new((bytesDownloaded) =>
{
try
{
if (!_downloadStatus.TryGetValue(fileGroup.Key, out FileDownloadStatus? value)) return;
value.TransferredBytes += bytesDownloaded;
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Could not set download progress");
}
});
await DownloadAndMungeFileHttpClient(fileGroup.Key, requestId, [.. fileGroup], blockFile, progress, token).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
Logger.LogDebug("{dlName}: Detected cancellation of download, partially extracting files for {id}", fi.Name, gameObjectHandler);
}
catch (Exception ex)
{
_orchestrator.ReleaseDownloadSlot();
File.Delete(blockFile);
Logger.LogError(ex, "{dlName}: Error during download of {id}", fi.Name, requestId);
ClearDownload();
return;
}
FileStream? fileBlockStream = null;
var threadCount = Math.Clamp((int)(Environment.ProcessorCount / 2.0f), 2, 8);
var tasks = new List<Task>();
try
{
if (_downloadStatus.TryGetValue(fileGroup.Key, out var status))
{
status.TransferredFiles = 1;
status.DownloadStatus = DownloadStatus.Decompressing;
}
fileBlockStream = File.OpenRead(blockFile);
while (fileBlockStream.Position < fileBlockStream.Length)
{
(string fileHash, long fileLengthBytes) = ReadBlockFileHeader(fileBlockStream);
var chunkPosition = fileBlockStream.Position;
fileBlockStream.Position += fileLengthBytes;
while (tasks.Count > threadCount && tasks.Where(t => !t.IsCompleted).Count() > 4)
await Task.Delay(10, CancellationToken.None).ConfigureAwait(false);
var fileExtension = fileReplacement.First(f => string.Equals(f.Hash, fileHash, StringComparison.OrdinalIgnoreCase)).GamePaths[0].Split(".")[^1];
var tmpPath = _fileDbManager.GetCacheFilePath(Guid.NewGuid().ToString(), "tmp");
var filePath = _fileDbManager.GetCacheFilePath(fileHash, fileExtension);
Logger.LogDebug("{dlName}: Decompressing {file}:{le} => {dest}", fi.Name, fileHash, fileLengthBytes, filePath);
tasks.Add(Task.Run(() => {
try
{
using var tmpFileStream = new HashingStream(new FileStream(tmpPath, new FileStreamOptions()
{
Mode = FileMode.CreateNew,
Access = FileAccess.Write,
Share = FileShare.None
}), SHA1.Create());
using var fileChunkStream = new FileStream(blockFile, new FileStreamOptions()
{
BufferSize = 80000,
Mode = FileMode.Open,
Access = FileAccess.Read
});
fileChunkStream.Position = chunkPosition;
using var innerFileStream = new LimitedStream(fileChunkStream, fileLengthBytes);
using var decoder = LZ4Frame.Decode(innerFileStream);
long startPos = fileChunkStream.Position;
decoder.AsStream().CopyTo(tmpFileStream);
long readBytes = fileChunkStream.Position - startPos;
if (readBytes != fileLengthBytes)
{
throw new EndOfStreamException();
}
string calculatedHash = BitConverter.ToString(tmpFileStream.Finish()).Replace("-", "", StringComparison.Ordinal);
if (!calculatedHash.Equals(fileHash, StringComparison.Ordinal))
{
Logger.LogError("Hash mismatch after extracting, got {hash}, expected {expectedHash}, deleting file", calculatedHash, fileHash);
return;
}
tmpFileStream.Close();
_fileCompactor.RenameAndCompact(filePath, tmpPath);
PersistFileToStorage(fileHash, filePath, fileLengthBytes);
}
catch (EndOfStreamException)
{
Logger.LogWarning("{dlName}: Failure to extract file {fileHash}, stream ended prematurely", fi.Name, fileHash);
}
catch (Exception e)
{
Logger.LogWarning(e, "{dlName}: Error during decompression of {hash}", fi.Name, fileHash);
foreach (var fr in fileReplacement)
Logger.LogWarning(" - {h}: {x}", fr.Hash, fr.GamePaths[0]);
}
finally
{
if (File.Exists(tmpPath))
File.Delete(tmpPath);
}
}, CancellationToken.None));
}
Task.WaitAll([..tasks], CancellationToken.None);
}
catch (EndOfStreamException)
{
Logger.LogDebug("{dlName}: Failure to extract file header data, stream ended", fi.Name);
}
catch (Exception ex)
{
Logger.LogError(ex, "{dlName}: Error during block file read", fi.Name);
}
finally
{
Task.WaitAll([..tasks], CancellationToken.None);
_orchestrator.ReleaseDownloadSlot();
if (fileBlockStream != null)
await fileBlockStream.DisposeAsync().ConfigureAwait(false);
File.Delete(blockFile);
}
}).ConfigureAwait(false);
Logger.LogDebug("Download end: {id}", gameObjectHandler);
ClearDownload();
}
private async Task<List<DownloadFileDto>> FilesGetSizes(List<string> hashes, CancellationToken ct)
{
if (!_orchestrator.IsInitialized) throw new InvalidOperationException("FileTransferManager is not initialized");
var response = await _orchestrator.SendRequestAsync(HttpMethod.Get, MareFiles.ServerFilesGetSizesFullPath(_orchestrator.FilesCdnUri!), hashes, ct).ConfigureAwait(false);
return await response.Content.ReadFromJsonAsync<List<DownloadFileDto>>(cancellationToken: ct).ConfigureAwait(false) ?? [];
}
private void PersistFileToStorage(string fileHash, string filePath, long? compressedSize = null)
{
try
{
var entry = _fileDbManager.CreateCacheEntry(filePath, fileHash);
if (entry != null && !string.Equals(entry.Hash, fileHash, StringComparison.OrdinalIgnoreCase))
{
_fileDbManager.RemoveHashedFile(entry.Hash, entry.PrefixedFilePath);
entry = null;
}
if (entry != null)
entry.CompressedSize = compressedSize;
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Error creating cache entry");
}
}
private async Task WaitForDownloadReady(List<DownloadFileTransfer> downloadFileTransfer, Guid requestId, CancellationToken downloadCt)
{
bool alreadyCancelled = false;
try
{
CancellationTokenSource localTimeoutCts = new();
localTimeoutCts.CancelAfter(TimeSpan.FromSeconds(5));
CancellationTokenSource composite = CancellationTokenSource.CreateLinkedTokenSource(downloadCt, localTimeoutCts.Token);
while (!_orchestrator.IsDownloadReady(requestId))
{
try
{
await Task.Delay(250, composite.Token).ConfigureAwait(false);
}
catch (TaskCanceledException)
{
if (downloadCt.IsCancellationRequested) throw;
var req = await _orchestrator.SendRequestAsync(HttpMethod.Get, MareFiles.RequestCheckQueueFullPath(downloadFileTransfer[0].DownloadUri, requestId),
downloadFileTransfer.Select(c => c.Hash).ToList(), downloadCt).ConfigureAwait(false);
req.EnsureSuccessStatusCode();
localTimeoutCts.Dispose();
composite.Dispose();
localTimeoutCts = new();
localTimeoutCts.CancelAfter(TimeSpan.FromSeconds(5));
composite = CancellationTokenSource.CreateLinkedTokenSource(downloadCt, localTimeoutCts.Token);
}
}
localTimeoutCts.Dispose();
composite.Dispose();
Logger.LogDebug("Download {requestId} ready", requestId);
}
catch (TaskCanceledException)
{
try
{
await _orchestrator.SendRequestAsync(HttpMethod.Get, MareFiles.RequestCancelFullPath(downloadFileTransfer[0].DownloadUri, requestId)).ConfigureAwait(false);
alreadyCancelled = true;
}
catch
{
// ignore whatever happens here
}
throw;
}
finally
{
if (downloadCt.IsCancellationRequested && !alreadyCancelled)
{
try
{
await _orchestrator.SendRequestAsync(HttpMethod.Get, MareFiles.RequestCancelFullPath(downloadFileTransfer[0].DownloadUri, requestId)).ConfigureAwait(false);
}
catch
{
// ignore whatever happens here
}
}
_orchestrator.ClearDownloadRequest(requestId);
}
}
}

View File

@@ -0,0 +1,177 @@
using MareSynchronos.MareConfiguration;
using MareSynchronos.Services.Mediator;
using MareSynchronos.WebAPI.Files.Models;
using MareSynchronos.WebAPI.SignalR;
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Reflection;
namespace MareSynchronos.WebAPI.Files;
public class FileTransferOrchestrator : DisposableMediatorSubscriberBase
{
private readonly ConcurrentDictionary<Guid, bool> _downloadReady = new();
private readonly HttpClient _httpClient;
private readonly MareConfigService _mareConfig;
private readonly Lock _semaphoreModificationLock = new();
private readonly TokenProvider _tokenProvider;
private int _availableDownloadSlots;
private SemaphoreSlim _downloadSemaphore;
private int CurrentlyUsedDownloadSlots => _availableDownloadSlots - _downloadSemaphore.CurrentCount;
public FileTransferOrchestrator(ILogger<FileTransferOrchestrator> logger, MareConfigService mareConfig,
MareMediator mediator, TokenProvider tokenProvider) : base(logger, mediator)
{
_mareConfig = mareConfig;
_tokenProvider = tokenProvider;
_httpClient = new()
{
Timeout = TimeSpan.FromSeconds(3000)
};
var ver = Assembly.GetExecutingAssembly().GetName().Version;
_httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("MareSynchronos", ver!.Major + "." + ver!.Minor + "." + ver!.Build));
_availableDownloadSlots = mareConfig.Current.ParallelDownloads;
_downloadSemaphore = new(_availableDownloadSlots, _availableDownloadSlots);
Mediator.Subscribe<ConnectedMessage>(this, (msg) =>
{
FilesCdnUri = msg.Connection.ServerInfo.FileServerAddress;
});
Mediator.Subscribe<DisconnectedMessage>(this, (msg) =>
{
FilesCdnUri = null;
});
Mediator.Subscribe<DownloadReadyMessage>(this, (msg) =>
{
_downloadReady[msg.RequestId] = true;
});
}
public Uri? FilesCdnUri { private set; get; }
public List<FileTransfer> ForbiddenTransfers { get; } = [];
public bool IsInitialized => FilesCdnUri != null;
public void ClearDownloadRequest(Guid guid)
{
_downloadReady.Remove(guid, out _);
}
public bool IsDownloadReady(Guid guid)
{
if (_downloadReady.TryGetValue(guid, out bool isReady) && isReady)
{
return true;
}
return false;
}
public void ReleaseDownloadSlot()
{
try
{
_downloadSemaphore.Release();
Mediator.Publish(new DownloadLimitChangedMessage());
}
catch (SemaphoreFullException)
{
// ignore
}
}
public async Task<HttpResponseMessage> SendRequestAsync(HttpMethod method, Uri uri,
CancellationToken? ct = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead)
{
using var requestMessage = new HttpRequestMessage(method, uri);
return await SendRequestInternalAsync(requestMessage, ct, httpCompletionOption).ConfigureAwait(false);
}
public async Task<HttpResponseMessage> SendRequestAsync<T>(HttpMethod method, Uri uri, T content, CancellationToken ct) where T : class
{
using var requestMessage = new HttpRequestMessage(method, uri);
if (content is not ByteArrayContent)
requestMessage.Content = JsonContent.Create(content);
else
requestMessage.Content = content as ByteArrayContent;
return await SendRequestInternalAsync(requestMessage, ct).ConfigureAwait(false);
}
public async Task<HttpResponseMessage> SendRequestStreamAsync(HttpMethod method, Uri uri, ProgressableStreamContent content, CancellationToken ct)
{
using var requestMessage = new HttpRequestMessage(method, uri);
requestMessage.Content = content;
return await SendRequestInternalAsync(requestMessage, ct).ConfigureAwait(false);
}
public async Task WaitForDownloadSlotAsync(CancellationToken token)
{
lock (_semaphoreModificationLock)
{
if (_availableDownloadSlots != _mareConfig.Current.ParallelDownloads && _availableDownloadSlots == _downloadSemaphore.CurrentCount)
{
_availableDownloadSlots = _mareConfig.Current.ParallelDownloads;
_downloadSemaphore = new(_availableDownloadSlots, _availableDownloadSlots);
}
}
await _downloadSemaphore.WaitAsync(token).ConfigureAwait(false);
Mediator.Publish(new DownloadLimitChangedMessage());
}
public long DownloadLimitPerSlot()
{
var limit = _mareConfig.Current.DownloadSpeedLimitInBytes;
if (limit <= 0) return 0;
limit = _mareConfig.Current.DownloadSpeedType switch
{
MareConfiguration.Models.DownloadSpeeds.Bps => limit,
MareConfiguration.Models.DownloadSpeeds.KBps => limit * 1024,
MareConfiguration.Models.DownloadSpeeds.MBps => limit * 1024 * 1024,
_ => limit,
};
var currentUsedDlSlots = CurrentlyUsedDownloadSlots;
var avaialble = _availableDownloadSlots;
var currentCount = _downloadSemaphore.CurrentCount;
var dividedLimit = limit / (currentUsedDlSlots == 0 ? 1 : currentUsedDlSlots);
if (dividedLimit < 0)
{
Logger.LogWarning("Calculated Bandwidth Limit is negative, returning Infinity: {value}, CurrentlyUsedDownloadSlots is {currentSlots}, " +
"DownloadSpeedLimit is {limit}, available slots: {avail}, current count: {count}", dividedLimit, currentUsedDlSlots, limit, avaialble, currentCount);
return long.MaxValue;
}
return Math.Clamp(dividedLimit, 1, long.MaxValue);
}
private async Task<HttpResponseMessage> SendRequestInternalAsync(HttpRequestMessage requestMessage,
CancellationToken? ct = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead)
{
var token = await _tokenProvider.GetToken().ConfigureAwait(false);
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
if (requestMessage.Content != null && requestMessage.Content is not StreamContent && requestMessage.Content is not ByteArrayContent)
{
var content = await ((JsonContent)requestMessage.Content).ReadAsStringAsync().ConfigureAwait(false);
Logger.LogDebug("Sending {method} to {uri} (Content: {content})", requestMessage.Method, requestMessage.RequestUri, content);
}
else
{
Logger.LogDebug("Sending {method} to {uri}", requestMessage.Method, requestMessage.RequestUri);
}
try
{
if (ct != null)
return await _httpClient.SendAsync(requestMessage, httpCompletionOption, ct.Value).ConfigureAwait(false);
return await _httpClient.SendAsync(requestMessage, httpCompletionOption).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Error during SendRequestInternal for {uri}", requestMessage.RequestUri);
throw;
}
}
}

View File

@@ -0,0 +1,289 @@
using MareSynchronos.API.Data;
using MareSynchronos.API.Dto.Files;
using MareSynchronos.API.Routes;
using MareSynchronos.FileCache;
using MareSynchronos.MareConfiguration;
using MareSynchronos.Services.Mediator;
using MareSynchronos.Services.ServerConfiguration;
using MareSynchronos.UI;
using MareSynchronos.WebAPI.Files.Models;
using Microsoft.Extensions.Logging;
using System.Net.Http.Headers;
using System.Net.Http.Json;
namespace MareSynchronos.WebAPI.Files;
public sealed class FileUploadManager : DisposableMediatorSubscriberBase
{
private readonly FileCacheManager _fileDbManager;
private readonly MareConfigService _mareConfigService;
private readonly FileTransferOrchestrator _orchestrator;
private readonly ServerConfigurationManager _serverManager;
private readonly Dictionary<string, DateTime> _verifiedUploadedHashes = new(StringComparer.Ordinal);
private CancellationTokenSource? _uploadCancellationTokenSource = new();
public FileUploadManager(ILogger<FileUploadManager> logger, MareMediator mediator,
MareConfigService mareConfigService,
FileTransferOrchestrator orchestrator,
FileCacheManager fileDbManager,
ServerConfigurationManager serverManager) : base(logger, mediator)
{
_mareConfigService = mareConfigService;
_orchestrator = orchestrator;
_fileDbManager = fileDbManager;
_serverManager = serverManager;
Mediator.Subscribe<DisconnectedMessage>(this, (msg) =>
{
Reset();
});
}
public List<FileTransfer> CurrentUploads { get; } = [];
public bool IsUploading => CurrentUploads.Count > 0;
public bool CancelUpload()
{
if (CurrentUploads.Any())
{
Logger.LogDebug("Cancelling current upload");
_uploadCancellationTokenSource?.Cancel();
_uploadCancellationTokenSource?.Dispose();
_uploadCancellationTokenSource = null;
CurrentUploads.Clear();
return true;
}
return false;
}
public async Task DeleteAllFiles()
{
if (!_orchestrator.IsInitialized) throw new InvalidOperationException("FileTransferManager is not initialized");
await _orchestrator.SendRequestAsync(HttpMethod.Post, MareFiles.ServerFilesDeleteAllFullPath(_orchestrator.FilesCdnUri!)).ConfigureAwait(false);
}
public async Task<List<string>> UploadFiles(List<string> hashesToUpload, IProgress<string> progress, CancellationToken? ct = null)
{
Logger.LogDebug("Trying to upload files");
var filesPresentLocally = hashesToUpload.Where(h => _fileDbManager.GetFileCacheByHash(h) != null).ToHashSet(StringComparer.Ordinal);
var locallyMissingFiles = hashesToUpload.Except(filesPresentLocally, StringComparer.Ordinal).ToList();
if (locallyMissingFiles.Any())
{
return locallyMissingFiles;
}
progress.Report($"Starting upload for {filesPresentLocally.Count} files");
var filesToUpload = await FilesSend([.. filesPresentLocally], [], ct ?? CancellationToken.None).ConfigureAwait(false);
if (filesToUpload.Exists(f => f.IsForbidden))
{
return [.. filesToUpload.Where(f => f.IsForbidden).Select(f => f.Hash)];
}
Task uploadTask = Task.CompletedTask;
int i = 1;
foreach (var file in filesToUpload)
{
progress.Report($"Uploading file {i++}/{filesToUpload.Count}. Please wait until the upload is completed.");
Logger.LogDebug("[{hash}] Compressing", file);
var data = await _fileDbManager.GetCompressedFileData(file.Hash, ct ?? CancellationToken.None).ConfigureAwait(false);
Logger.LogDebug("[{hash}] Starting upload for {filePath}", data.Item1, _fileDbManager.GetFileCacheByHash(data.Item1)!.ResolvedFilepath);
await uploadTask.ConfigureAwait(false);
uploadTask = UploadFile(data.Item2, file.Hash, false, ct ?? CancellationToken.None);
(ct ?? CancellationToken.None).ThrowIfCancellationRequested();
}
await uploadTask.ConfigureAwait(false);
return [];
}
public async Task<CharacterData> UploadFiles(CharacterData data, List<UserData> visiblePlayers)
{
CancelUpload();
_uploadCancellationTokenSource = new CancellationTokenSource();
var uploadToken = _uploadCancellationTokenSource.Token;
Logger.LogDebug("Sending Character data {hash} to service {url}", data.DataHash.Value, _serverManager.CurrentRealApiUrl);
HashSet<string> unverifiedUploads = GetUnverifiedFiles(data);
if (unverifiedUploads.Any())
{
await UploadUnverifiedFiles(unverifiedUploads, visiblePlayers, uploadToken).ConfigureAwait(false);
Logger.LogInformation("Upload complete for {hash}", data.DataHash.Value);
}
foreach (var kvp in data.FileReplacements)
{
data.FileReplacements[kvp.Key].RemoveAll(i => _orchestrator.ForbiddenTransfers.Exists(f => string.Equals(f.Hash, i.Hash, StringComparison.OrdinalIgnoreCase)));
}
return data;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
Reset();
}
private async Task<List<UploadFileDto>> FilesSend(List<string> hashes, List<string> uids, CancellationToken ct)
{
if (!_orchestrator.IsInitialized) throw new InvalidOperationException("FileTransferManager is not initialized");
FilesSendDto filesSendDto = new()
{
FileHashes = hashes,
UIDs = uids
};
var response = await _orchestrator.SendRequestAsync(HttpMethod.Post, MareFiles.ServerFilesFilesSendFullPath(_orchestrator.FilesCdnUri!), filesSendDto, ct).ConfigureAwait(false);
return await response.Content.ReadFromJsonAsync<List<UploadFileDto>>(cancellationToken: ct).ConfigureAwait(false) ?? [];
}
private HashSet<string> GetUnverifiedFiles(CharacterData data)
{
HashSet<string> unverifiedUploadHashes = new(StringComparer.Ordinal);
foreach (var item in data.FileReplacements.SelectMany(c => c.Value.Where(f => string.IsNullOrEmpty(f.FileSwapPath)).Select(v => v.Hash).Distinct(StringComparer.Ordinal)).Distinct(StringComparer.Ordinal).ToList())
{
if (!_verifiedUploadedHashes.TryGetValue(item, out var verifiedTime))
{
verifiedTime = DateTime.MinValue;
}
if (verifiedTime < DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(10)))
{
Logger.LogTrace("Verifying {item}, last verified: {date}", item, verifiedTime);
unverifiedUploadHashes.Add(item);
}
}
return unverifiedUploadHashes;
}
private void Reset()
{
_uploadCancellationTokenSource?.Cancel();
_uploadCancellationTokenSource?.Dispose();
_uploadCancellationTokenSource = null;
CurrentUploads.Clear();
_verifiedUploadedHashes.Clear();
}
private async Task UploadFile(byte[] compressedFile, string fileHash, bool postProgress, CancellationToken uploadToken)
{
if (!_orchestrator.IsInitialized) throw new InvalidOperationException("FileTransferManager is not initialized");
Logger.LogInformation("[{hash}] Uploading {size}", fileHash, UiSharedService.ByteToString(compressedFile.Length));
if (uploadToken.IsCancellationRequested) return;
try
{
await UploadFileStream(compressedFile, fileHash, munged: false, postProgress, uploadToken).ConfigureAwait(false);
_verifiedUploadedHashes[fileHash] = DateTime.UtcNow;
}
catch (Exception ex)
{
Logger.LogWarning(ex, "[{hash}] File upload cancelled", fileHash);
}
}
private async Task UploadFileStream(byte[] compressedFile, string fileHash, bool munged, bool postProgress, CancellationToken uploadToken)
{
if (munged)
throw new InvalidOperationException();
using var ms = new MemoryStream(compressedFile);
Progress<UploadProgress>? prog = !postProgress ? null : new((prog) =>
{
try
{
CurrentUploads.Single(f => string.Equals(f.Hash, fileHash, StringComparison.Ordinal)).Transferred = prog.Uploaded;
}
catch (Exception ex)
{
Logger.LogWarning(ex, "[{hash}] Could not set upload progress", fileHash);
}
});
var streamContent = new ProgressableStreamContent(ms, prog);
streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
HttpResponseMessage response;
if (!munged)
response = await _orchestrator.SendRequestStreamAsync(HttpMethod.Post, MareFiles.ServerFilesUploadFullPath(_orchestrator.FilesCdnUri!, fileHash), streamContent, uploadToken).ConfigureAwait(false);
else
response = await _orchestrator.SendRequestStreamAsync(HttpMethod.Post, MareFiles.ServerFilesUploadMunged(_orchestrator.FilesCdnUri!, fileHash), streamContent, uploadToken).ConfigureAwait(false);
Logger.LogDebug("[{hash}] Upload Status: {status}", fileHash, response.StatusCode);
}
private async Task UploadUnverifiedFiles(HashSet<string> unverifiedUploadHashes, List<UserData> visiblePlayers, CancellationToken uploadToken)
{
unverifiedUploadHashes = unverifiedUploadHashes.Where(h => _fileDbManager.GetFileCacheByHash(h) != null).ToHashSet(StringComparer.Ordinal);
Logger.LogDebug("Verifying {count} files", unverifiedUploadHashes.Count);
var filesToUpload = await FilesSend([.. unverifiedUploadHashes], visiblePlayers.Select(p => p.UID).ToList(), uploadToken).ConfigureAwait(false);
foreach (var file in filesToUpload.Where(f => !f.IsForbidden).DistinctBy(f => f.Hash))
{
try
{
CurrentUploads.Add(new UploadFileTransfer(file)
{
Total = new FileInfo(_fileDbManager.GetFileCacheByHash(file.Hash)!.ResolvedFilepath).Length,
});
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Tried to request file {hash} but file was not present", file.Hash);
}
}
foreach (var file in filesToUpload.Where(c => c.IsForbidden))
{
if (_orchestrator.ForbiddenTransfers.TrueForAll(f => !string.Equals(f.Hash, file.Hash, StringComparison.Ordinal)))
{
_orchestrator.ForbiddenTransfers.Add(new UploadFileTransfer(file)
{
LocalFile = _fileDbManager.GetFileCacheByHash(file.Hash)?.ResolvedFilepath ?? string.Empty,
});
}
_verifiedUploadedHashes[file.Hash] = DateTime.UtcNow;
}
var totalSize = CurrentUploads.Sum(c => c.Total);
Logger.LogDebug("Compressing and uploading files");
Task uploadTask = Task.CompletedTask;
foreach (var file in CurrentUploads.Where(f => f.CanBeTransferred && !f.IsTransferred).ToList())
{
Logger.LogDebug("[{hash}] Compressing", file);
var data = await _fileDbManager.GetCompressedFileData(file.Hash, uploadToken).ConfigureAwait(false);
CurrentUploads.Single(e => string.Equals(e.Hash, file.Hash, StringComparison.Ordinal)).Total = data.Item2.Length;
Logger.LogDebug("[{hash}] Starting upload for {filePath}", file.Hash, _fileDbManager.GetFileCacheByHash(file.Hash)!.ResolvedFilepath);
await uploadTask.ConfigureAwait(false);
uploadTask = UploadFile(data.Item2, file.Hash, true, uploadToken);
uploadToken.ThrowIfCancellationRequested();
}
if (CurrentUploads.Any())
{
await uploadTask.ConfigureAwait(false);
var compressedSize = CurrentUploads.Sum(c => c.Total);
Logger.LogDebug("Upload complete, compressed {size} to {compressed}", UiSharedService.ByteToString(totalSize), UiSharedService.ByteToString(compressedSize));
_fileDbManager.WriteOutFullCsv();
}
foreach (var file in unverifiedUploadHashes.Where(c => !CurrentUploads.Exists(u => string.Equals(u.Hash, c, StringComparison.Ordinal))))
{
_verifiedUploadedHashes[file] = DateTime.UtcNow;
}
CurrentUploads.Clear();
}
}

View File

@@ -0,0 +1,24 @@
using MareSynchronos.API.Dto.Files;
namespace MareSynchronos.WebAPI.Files.Models;
public class DownloadFileTransfer : FileTransfer
{
public DownloadFileTransfer(DownloadFileDto dto) : base(dto)
{
}
public override bool CanBeTransferred => Dto.FileExists && !Dto.IsForbidden && Dto.Size > 0;
public Uri DownloadUri => new(Dto.Url);
public override long Total
{
set
{
// nothing to set
}
get => Dto.Size;
}
public long TotalRaw => 0; // XXX
private DownloadFileDto Dto => (DownloadFileDto)TransferDto;
}

View File

@@ -0,0 +1,10 @@
namespace MareSynchronos.WebAPI.Files.Models;
public enum DownloadStatus
{
Initializing,
WaitingForSlot,
WaitingForQueue,
Downloading,
Decompressing
}

View File

@@ -0,0 +1,10 @@
namespace MareSynchronos.WebAPI.Files.Models;
public class FileDownloadStatus
{
public DownloadStatus DownloadStatus { get; set; }
public long TotalBytes { get; set; }
public int TotalFiles { get; set; }
public long TransferredBytes { get; set; }
public int TransferredFiles { get; set; }
}

View File

@@ -0,0 +1,27 @@
using MareSynchronos.API.Dto.Files;
namespace MareSynchronos.WebAPI.Files.Models;
public abstract class FileTransfer
{
protected readonly ITransferFileDto TransferDto;
protected FileTransfer(ITransferFileDto transferDto)
{
TransferDto = transferDto;
}
public virtual bool CanBeTransferred => !TransferDto.IsForbidden && (TransferDto is not DownloadFileDto dto || dto.FileExists);
public string ForbiddenBy => TransferDto.ForbiddenBy;
public string Hash => TransferDto.Hash;
public bool IsForbidden => TransferDto.IsForbidden;
public bool IsInTransfer => Transferred != Total && Transferred > 0;
public bool IsTransferred => Transferred == Total;
public abstract long Total { get; set; }
public long Transferred { get; set; } = 0;
public override string ToString()
{
return Hash;
}
}

View File

@@ -0,0 +1,93 @@
using System.Net;
namespace MareSynchronos.WebAPI.Files.Models;
public class ProgressableStreamContent : StreamContent
{
private const int _defaultBufferSize = 4096;
private readonly int _bufferSize;
private readonly IProgress<UploadProgress>? _progress;
private readonly Stream _streamToWrite;
private bool _contentConsumed;
public ProgressableStreamContent(Stream streamToWrite, IProgress<UploadProgress>? downloader)
: this(streamToWrite, _defaultBufferSize, downloader)
{
}
public ProgressableStreamContent(Stream streamToWrite, int bufferSize, IProgress<UploadProgress>? progress)
: base(streamToWrite, bufferSize)
{
if (streamToWrite == null)
{
throw new ArgumentNullException(nameof(streamToWrite));
}
if (bufferSize <= 0)
{
throw new ArgumentOutOfRangeException(nameof(bufferSize));
}
_streamToWrite = streamToWrite;
_bufferSize = bufferSize;
_progress = progress;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_streamToWrite.Dispose();
}
base.Dispose(disposing);
}
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context)
{
PrepareContent();
var buffer = new byte[_bufferSize];
var size = _streamToWrite.Length;
var uploaded = 0;
using (_streamToWrite)
{
while (true)
{
var length = await _streamToWrite.ReadAsync(buffer).ConfigureAwait(false);
if (length <= 0)
{
break;
}
uploaded += length;
_progress?.Report(new UploadProgress(uploaded, size));
await stream.WriteAsync(buffer.AsMemory(0, length)).ConfigureAwait(false);
}
}
}
protected override bool TryComputeLength(out long length)
{
length = _streamToWrite.Length;
return true;
}
private void PrepareContent()
{
if (_contentConsumed)
{
if (_streamToWrite.CanSeek)
{
_streamToWrite.Position = 0;
}
else
{
throw new InvalidOperationException("The stream has already been read.");
}
}
_contentConsumed = true;
}
}

View File

@@ -0,0 +1,13 @@
using MareSynchronos.API.Dto.Files;
namespace MareSynchronos.WebAPI.Files.Models;
public class UploadFileTransfer : FileTransfer
{
public UploadFileTransfer(UploadFileDto dto) : base(dto)
{
}
public string LocalFile { get; set; } = string.Empty;
public override long Total { get; set; }
}

View File

@@ -0,0 +1,3 @@
namespace MareSynchronos.WebAPI.Files.Models;
public record UploadProgress(long Uploaded, long Size);

View File

@@ -0,0 +1,231 @@
namespace MareSynchronos.WebAPI.Files
{
/// <summary>
/// Class for streaming data with throttling support.
/// Borrowed from https://github.com/bezzad/Downloader
/// </summary>
internal class ThrottledStream : Stream
{
public static long Infinite => long.MaxValue;
private readonly Stream _baseStream;
private long _bandwidthLimit;
private readonly Bandwidth _bandwidth = new();
private CancellationTokenSource _bandwidthChangeTokenSource = new();
/// <summary>
/// Initializes a new instance of the <see cref="T:ThrottledStream" /> class.
/// </summary>
/// <param name="baseStream">The base stream.</param>
/// <param name="bandwidthLimit">The maximum bytes per second that can be transferred through the base stream.</param>
/// <exception cref="ArgumentNullException">Thrown when <see cref="baseStream" /> is a null reference.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <see cref="BandwidthLimit" /> is a negative value.</exception>
public ThrottledStream(Stream baseStream, long bandwidthLimit)
{
if (bandwidthLimit < 0)
{
throw new ArgumentOutOfRangeException(nameof(bandwidthLimit),
bandwidthLimit, "The maximum number of bytes per second can't be negative.");
}
_baseStream = baseStream ?? throw new ArgumentNullException(nameof(baseStream));
BandwidthLimit = bandwidthLimit;
}
/// <summary>
/// Bandwidth Limit (in B/s)
/// </summary>
/// <value>The maximum bytes per second.</value>
public long BandwidthLimit
{
get => _bandwidthLimit;
set
{
if (_bandwidthLimit == value) return;
_bandwidthLimit = value <= 0 ? Infinite : value;
_bandwidth.BandwidthLimit = _bandwidthLimit;
_bandwidthChangeTokenSource.Cancel();
_bandwidthChangeTokenSource.Dispose();
_bandwidthChangeTokenSource = new();
}
}
/// <inheritdoc />
public override bool CanRead => _baseStream.CanRead;
/// <inheritdoc />
public override bool CanSeek => _baseStream.CanSeek;
/// <inheritdoc />
public override bool CanWrite => _baseStream.CanWrite;
/// <inheritdoc />
public override long Length => _baseStream.Length;
/// <inheritdoc />
public override long Position
{
get => _baseStream.Position;
set => _baseStream.Position = value;
}
/// <inheritdoc />
public override void Flush()
{
_baseStream.Flush();
}
/// <inheritdoc />
public override long Seek(long offset, SeekOrigin origin)
{
return _baseStream.Seek(offset, origin);
}
/// <inheritdoc />
public override void SetLength(long value)
{
_baseStream.SetLength(value);
}
/// <inheritdoc />
public override int Read(byte[] buffer, int offset, int count)
{
Throttle(count).Wait();
return _baseStream.Read(buffer, offset, count);
}
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
CancellationToken cancellationToken)
{
await Throttle(count, cancellationToken).ConfigureAwait(false);
#pragma warning disable CA1835
return await _baseStream.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
#pragma warning restore CA1835
}
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
{
await Throttle(buffer.Length, cancellationToken).ConfigureAwait(false);
return await _baseStream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc />
public override void Write(byte[] buffer, int offset, int count)
{
Throttle(count).Wait();
_baseStream.Write(buffer, offset, count);
}
/// <inheritdoc />
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
await Throttle(count, cancellationToken).ConfigureAwait(false);
#pragma warning disable CA1835
await _baseStream.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
#pragma warning restore CA1835
}
/// <inheritdoc />
public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
await Throttle(buffer.Length, cancellationToken).ConfigureAwait(false);
await _baseStream.WriteAsync(buffer, cancellationToken).ConfigureAwait(false);
}
public override void Close()
{
_baseStream.Close();
base.Close();
}
private async Task Throttle(int transmissionVolume, CancellationToken token = default)
{
// Make sure the buffer isn't empty.
if (BandwidthLimit > 0 && transmissionVolume > 0)
{
// Calculate the time to sleep.
_bandwidth.CalculateSpeed(transmissionVolume);
await Sleep(_bandwidth.PopSpeedRetrieveTime(), token).ConfigureAwait(false);
}
}
private async Task Sleep(int time, CancellationToken token = default)
{
try
{
if (time > 0)
{
var bandWidthtoken = _bandwidthChangeTokenSource.Token;
var linked = CancellationTokenSource.CreateLinkedTokenSource(token, bandWidthtoken).Token;
await Task.Delay(time, linked).ConfigureAwait(false);
}
}
catch (TaskCanceledException)
{
// ignore
}
}
/// <inheritdoc />
public override string ToString()
{
return _baseStream?.ToString() ?? string.Empty;
}
private sealed class Bandwidth
{
private long _count;
private int _lastSecondCheckpoint;
private long _lastTransferredBytesCount;
private int _speedRetrieveTime;
public double Speed { get; private set; }
public double AverageSpeed { get; private set; }
public long BandwidthLimit { get; set; }
public Bandwidth()
{
BandwidthLimit = long.MaxValue;
Reset();
}
public void CalculateSpeed(long receivedBytesCount)
{
int elapsedTime = Environment.TickCount - _lastSecondCheckpoint + 1;
receivedBytesCount = Interlocked.Add(ref _lastTransferredBytesCount, receivedBytesCount);
double momentSpeed = receivedBytesCount * 1000 / (double)elapsedTime; // B/s
if (1000 < elapsedTime)
{
Speed = momentSpeed;
AverageSpeed = ((AverageSpeed * _count) + Speed) / (_count + 1);
_count++;
SecondCheckpoint();
}
if (momentSpeed >= BandwidthLimit)
{
var expectedTime = receivedBytesCount * 1000 / BandwidthLimit;
Interlocked.Add(ref _speedRetrieveTime, (int)expectedTime - elapsedTime);
}
}
public int PopSpeedRetrieveTime()
{
return Interlocked.Exchange(ref _speedRetrieveTime, 0);
}
public void Reset()
{
SecondCheckpoint();
_count = 0;
Speed = 0;
AverageSpeed = 0;
}
private void SecondCheckpoint()
{
Interlocked.Exchange(ref _lastSecondCheckpoint, Environment.TickCount);
Interlocked.Exchange(ref _lastTransferredBytesCount, 0);
}
}
}
}

View File

@@ -0,0 +1,116 @@
using MareSynchronos.API.Data;
using MareSynchronos.API.Dto.User;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging;
using System.Text;
namespace MareSynchronos.WebAPI;
public partial class ApiController
{
public async Task PushCharacterData(CharacterData data, List<UserData> visibleCharacters)
{
if (!IsConnected) return;
try
{
Logger.LogDebug("Pushing Character data {hash} to {visible}", data.DataHash, string.Join(", ", visibleCharacters.Select(v => v.AliasOrUID)));
await PushCharacterDataInternal(data, [.. visibleCharacters]).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
Logger.LogDebug("Upload operation was cancelled");
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Error during upload of files");
}
}
public async Task UserAddPair(UserDto user)
{
if (!IsConnected) return;
await _mareHub!.SendAsync(nameof(UserAddPair), user).ConfigureAwait(false);
}
public async Task UserChatSendMsg(UserDto user, ChatMessage message)
{
CheckConnection();
await _mareHub!.SendAsync(nameof(UserChatSendMsg), user, message).ConfigureAwait(false);
}
public async Task UserDelete()
{
CheckConnection();
await _mareHub!.SendAsync(nameof(UserDelete)).ConfigureAwait(false);
await CreateConnections().ConfigureAwait(false);
}
public async Task<List<OnlineUserIdentDto>> UserGetOnlinePairs()
{
return await _mareHub!.InvokeAsync<List<OnlineUserIdentDto>>(nameof(UserGetOnlinePairs)).ConfigureAwait(false);
}
public async Task<List<UserPairDto>> UserGetPairedClients()
{
return await _mareHub!.InvokeAsync<List<UserPairDto>>(nameof(UserGetPairedClients)).ConfigureAwait(false);
}
public async Task<UserProfileDto> UserGetProfile(UserDto dto)
{
if (!IsConnected) return new UserProfileDto(dto.User, false, null, null, null);
return await _mareHub!.InvokeAsync<UserProfileDto>(nameof(UserGetProfile), dto).ConfigureAwait(false);
}
public async Task UserPushData(UserCharaDataMessageDto dto)
{
try
{
await _mareHub!.InvokeAsync(nameof(UserPushData), dto).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Failed to Push character data");
}
}
public async Task UserRemovePair(UserDto userDto)
{
if (!IsConnected) return;
await _mareHub!.SendAsync(nameof(UserRemovePair), userDto).ConfigureAwait(false);
}
public async Task UserReportProfile(UserProfileReportDto userDto)
{
if (!IsConnected) return;
await _mareHub!.SendAsync(nameof(UserReportProfile), userDto).ConfigureAwait(false);
}
public async Task UserSetPairPermissions(UserPermissionsDto userPermissions)
{
await _mareHub!.SendAsync(nameof(UserSetPairPermissions), userPermissions).ConfigureAwait(false);
}
public async Task UserSetProfile(UserProfileDto userDescription)
{
if (!IsConnected) return;
await _mareHub!.InvokeAsync(nameof(UserSetProfile), userDescription).ConfigureAwait(false);
}
private async Task PushCharacterDataInternal(CharacterData character, List<UserData> visibleCharacters)
{
Logger.LogInformation("Pushing character data for {hash} to {charas}", character.DataHash.Value, string.Join(", ", visibleCharacters.Select(c => c.AliasOrUID)));
StringBuilder sb = new();
foreach (var kvp in character.FileReplacements.ToList())
{
sb.AppendLine($"FileReplacements for {kvp.Key}: {kvp.Value.Count}");
}
foreach (var item in character.GlamourerData)
{
sb.AppendLine($"GlamourerData for {item.Key}: {!string.IsNullOrEmpty(item.Value)}");
}
Logger.LogDebug("Chara data contained: {nl} {data}", Environment.NewLine, sb.ToString());
await UserPushData(new(visibleCharacters, character)).ConfigureAwait(false);
}
}

View File

@@ -0,0 +1,405 @@
using MareSynchronos.API.Data;
using MareSynchronos.API.Data.Enum;
using MareSynchronos.API.Dto;
using MareSynchronos.API.Dto.Chat;
using MareSynchronos.API.Dto.CharaData;
using MareSynchronos.API.Dto.Group;
using MareSynchronos.API.Dto.User;
using MareSynchronos.MareConfiguration.Models;
using MareSynchronos.Services.Mediator;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging;
using static FFXIVClientStructs.FFXIV.Client.Game.UI.MapMarkerData.Delegates;
namespace MareSynchronos.WebAPI;
public partial class ApiController
{
public Task Client_DownloadReady(Guid requestId)
{
Logger.LogDebug("Server sent {requestId} ready", requestId);
Mediator.Publish(new DownloadReadyMessage(requestId));
return Task.CompletedTask;
}
public Task Client_GroupChangePermissions(GroupPermissionDto groupPermission)
{
Logger.LogTrace("Client_GroupChangePermissions: {perm}", groupPermission);
ExecuteSafely(() => _pairManager.SetGroupPermissions(groupPermission));
return Task.CompletedTask;
}
public Task Client_GroupChatMsg(GroupChatMsgDto groupChatMsgDto)
{
Logger.LogDebug("Client_GroupChatMsg: {msg}", groupChatMsgDto.Message);
Mediator.Publish(new GroupChatMsgMessage(groupChatMsgDto.Group, groupChatMsgDto.Message));
return Task.CompletedTask;
}
public Task Client_GroupPairChangePermissions(GroupPairUserPermissionDto dto)
{
Logger.LogTrace("Client_GroupPairChangePermissions: {dto}", dto);
ExecuteSafely(() =>
{
if (string.Equals(dto.UID, UID, StringComparison.Ordinal)) _pairManager.SetGroupUserPermissions(dto);
else _pairManager.SetGroupPairUserPermissions(dto);
});
return Task.CompletedTask;
}
public Task Client_GroupDelete(GroupDto groupDto)
{
Logger.LogTrace("Client_GroupDelete: {dto}", groupDto);
ExecuteSafely(() => _pairManager.RemoveGroup(groupDto.Group));
return Task.CompletedTask;
}
public Task Client_GroupPairChangeUserInfo(GroupPairUserInfoDto userInfo)
{
Logger.LogTrace("Client_GroupPairChangeUserInfo: {dto}", userInfo);
ExecuteSafely(() =>
{
if (string.Equals(userInfo.UID, UID, StringComparison.Ordinal)) _pairManager.SetGroupStatusInfo(userInfo);
else _pairManager.SetGroupPairStatusInfo(userInfo);
});
return Task.CompletedTask;
}
public Task Client_GroupPairJoined(GroupPairFullInfoDto groupPairInfoDto)
{
Logger.LogTrace("Client_GroupPairJoined: {dto}", groupPairInfoDto);
ExecuteSafely(() => _pairManager.AddGroupPair(groupPairInfoDto));
return Task.CompletedTask;
}
public Task Client_GroupPairLeft(GroupPairDto groupPairDto)
{
Logger.LogTrace("Client_GroupPairLeft: {dto}", groupPairDto);
ExecuteSafely(() => _pairManager.RemoveGroupPair(groupPairDto));
return Task.CompletedTask;
}
public Task Client_GroupSendFullInfo(GroupFullInfoDto groupInfo)
{
Logger.LogTrace("Client_GroupSendFullInfo: {dto}", groupInfo);
ExecuteSafely(() => _pairManager.AddGroup(groupInfo));
return Task.CompletedTask;
}
public Task Client_GroupSendInfo(GroupInfoDto groupInfo)
{
Logger.LogTrace("Client_GroupSendInfo: {dto}", groupInfo);
ExecuteSafely(() => _pairManager.SetGroupInfo(groupInfo));
return Task.CompletedTask;
}
public Task Client_ReceiveServerMessage(MessageSeverity messageSeverity, string message)
{
switch (messageSeverity)
{
case MessageSeverity.Error:
Mediator.Publish(new NotificationMessage("Warning from " + _serverManager.CurrentServer!.ServerName, message, NotificationType.Error, TimeSpan.FromSeconds(7.5)));
break;
case MessageSeverity.Warning:
Mediator.Publish(new NotificationMessage("Warning from " + _serverManager.CurrentServer!.ServerName, message, NotificationType.Warning, TimeSpan.FromSeconds(7.5)));
break;
case MessageSeverity.Information:
if (_doNotNotifyOnNextInfo)
{
_doNotNotifyOnNextInfo = false;
break;
}
Mediator.Publish(new NotificationMessage("Info from " + _serverManager.CurrentServer!.ServerName, message, NotificationType.Info, TimeSpan.FromSeconds(5)));
break;
}
return Task.CompletedTask;
}
public Task Client_UpdateSystemInfo(SystemInfoDto systemInfo)
{
SystemInfoDto = systemInfo;
return Task.CompletedTask;
}
public Task Client_UserAddClientPair(UserPairDto dto)
{
Logger.LogDebug("Client_UserAddClientPair: {dto}", dto);
ExecuteSafely(() => _pairManager.AddUserPair(dto, addToLastAddedUser: true));
return Task.CompletedTask;
}
public Task Client_UserChatMsg(UserChatMsgDto chatMsgDto)
{
Logger.LogDebug("Client_UserChatMsg: {msg}", chatMsgDto.Message);
Mediator.Publish(new UserChatMsgMessage(chatMsgDto.Message));
return Task.CompletedTask;
}
public Task Client_UserReceiveCharacterData(OnlineUserCharaDataDto dataDto)
{
Logger.LogTrace("Client_UserReceiveCharacterData: {user}", dataDto.User);
ExecuteSafely(() => _pairManager.ReceiveCharaData(dataDto));
return Task.CompletedTask;
}
public Task Client_UserReceiveUploadStatus(UserDto dto)
{
Logger.LogTrace("Client_UserReceiveUploadStatus: {dto}", dto);
ExecuteSafely(() => _pairManager.ReceiveUploadStatus(dto));
return Task.CompletedTask;
}
public Task Client_UserRemoveClientPair(UserDto dto)
{
Logger.LogDebug("Client_UserRemoveClientPair: {dto}", dto);
ExecuteSafely(() => _pairManager.RemoveUserPair(dto));
return Task.CompletedTask;
}
public Task Client_UserSendOffline(UserDto dto)
{
Logger.LogDebug("Client_UserSendOffline: {dto}", dto);
ExecuteSafely(() => _pairManager.MarkPairOffline(dto.User));
return Task.CompletedTask;
}
public Task Client_UserSendOnline(OnlineUserIdentDto dto)
{
Logger.LogDebug("Client_UserSendOnline: {dto}", dto);
ExecuteSafely(() => _pairManager.MarkPairOnline(dto));
return Task.CompletedTask;
}
public Task Client_UserUpdateOtherPairPermissions(UserPermissionsDto dto)
{
Logger.LogDebug("Client_UserUpdateOtherPairPermissions: {dto}", dto);
ExecuteSafely(() => _pairManager.UpdatePairPermissions(dto));
return Task.CompletedTask;
}
public Task Client_UserUpdateProfile(UserDto dto)
{
Logger.LogDebug("Client_UserUpdateProfile: {dto}", dto);
ExecuteSafely(() => Mediator.Publish(new ClearProfileDataMessage(dto.User)));
return Task.CompletedTask;
}
public Task Client_UserUpdateSelfPairPermissions(UserPermissionsDto dto)
{
Logger.LogDebug("Client_UserUpdateSelfPairPermissions: {dto}", dto);
ExecuteSafely(() => _pairManager.UpdateSelfPairPermissions(dto));
return Task.CompletedTask;
}
public Task Client_GposeLobbyJoin(UserData userData)
{
Logger.LogDebug("Client_GposeLobbyJoin: {dto}", userData);
ExecuteSafely(() => Mediator.Publish(new GposeLobbyUserJoin(userData)));
return Task.CompletedTask;
}
public Task Client_GposeLobbyLeave(UserData userData)
{
Logger.LogDebug("Client_GposeLobbyLeave: {dto}", userData);
ExecuteSafely(() => Mediator.Publish(new GPoseLobbyUserLeave(userData)));
return Task.CompletedTask;
}
public Task Client_GposeLobbyPushCharacterData(CharaDataDownloadDto charaDownloadDto)
{
Logger.LogDebug("Client_GposeLobbyPushCharacterData: {dto}", charaDownloadDto.Uploader);
ExecuteSafely(() => Mediator.Publish(new GPoseLobbyReceiveCharaData(charaDownloadDto)));
return Task.CompletedTask;
}
public Task Client_GposeLobbyPushPoseData(UserData userData, PoseData poseData)
{
Logger.LogDebug("Client_GposeLobbyPushPoseData: {dto}", userData);
ExecuteSafely(() => Mediator.Publish(new GPoseLobbyReceivePoseData(userData, poseData)));
return Task.CompletedTask;
}
public Task Client_GposeLobbyPushWorldData(UserData userData, WorldData worldData)
{
//Logger.LogDebug("Client_GposeLobbyPushWorldData: {dto}", userData);
ExecuteSafely(() => Mediator.Publish(new GPoseLobbyReceiveWorldData(userData, worldData)));
return Task.CompletedTask;
}
public void OnDownloadReady(Action<Guid> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_DownloadReady), act);
}
public void OnGroupChangePermissions(Action<GroupPermissionDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_GroupChangePermissions), act);
}
public void OnGroupChatMsg(Action<GroupChatMsgDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_GroupChatMsg), act);
}
public void OnGroupPairChangePermissions(Action<GroupPairUserPermissionDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_GroupPairChangePermissions), act);
}
public void OnGroupDelete(Action<GroupDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_GroupDelete), act);
}
public void OnGroupPairChangeUserInfo(Action<GroupPairUserInfoDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_GroupPairChangeUserInfo), act);
}
public void OnGroupPairJoined(Action<GroupPairFullInfoDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_GroupPairJoined), act);
}
public void OnGroupPairLeft(Action<GroupPairDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_GroupPairLeft), act);
}
public void OnGroupSendFullInfo(Action<GroupFullInfoDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_GroupSendFullInfo), act);
}
public void OnGroupSendInfo(Action<GroupInfoDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_GroupSendInfo), act);
}
public void OnReceiveServerMessage(Action<MessageSeverity, string> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_ReceiveServerMessage), act);
}
public void OnUpdateSystemInfo(Action<SystemInfoDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_UpdateSystemInfo), act);
}
public void OnUserAddClientPair(Action<UserPairDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_UserAddClientPair), act);
}
public void OnUserChatMsg(Action<UserChatMsgDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_UserChatMsg), act);
}
public void OnUserReceiveCharacterData(Action<OnlineUserCharaDataDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_UserReceiveCharacterData), act);
}
public void OnUserReceiveUploadStatus(Action<UserDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_UserReceiveUploadStatus), act);
}
public void OnUserRemoveClientPair(Action<UserDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_UserRemoveClientPair), act);
}
public void OnUserSendOffline(Action<UserDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_UserSendOffline), act);
}
public void OnUserSendOnline(Action<OnlineUserIdentDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_UserSendOnline), act);
}
public void OnUserUpdateOtherPairPermissions(Action<UserPermissionsDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_UserUpdateOtherPairPermissions), act);
}
public void OnUserUpdateProfile(Action<UserDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_UserUpdateProfile), act);
}
public void OnUserUpdateSelfPairPermissions(Action<UserPermissionsDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_UserUpdateSelfPairPermissions), act);
}
public void OnGposeLobbyJoin(Action<UserData> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_GposeLobbyJoin), act);
}
public void OnGposeLobbyLeave(Action<UserData> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_GposeLobbyLeave), act);
}
public void OnGposeLobbyPushCharacterData(Action<CharaDataDownloadDto> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_GposeLobbyPushCharacterData), act);
}
public void OnGposeLobbyPushPoseData(Action<UserData, PoseData> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_GposeLobbyPushPoseData), act);
}
public void OnGposeLobbyPushWorldData(Action<UserData, WorldData> act)
{
if (_initialized) return;
_mareHub!.On(nameof(Client_GposeLobbyPushWorldData), act);
}
private void ExecuteSafely(Action act)
{
try
{
act();
}
catch (Exception ex)
{
Logger.LogCritical(ex, "Error on executing safely");
}
}
}

View File

@@ -0,0 +1,228 @@
using MareSynchronos.API.Data;
using MareSynchronos.API.Dto.CharaData;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging;
namespace MareSynchronos.WebAPI;
public partial class ApiController
{
public async Task<CharaDataFullDto?> CharaDataCreate()
{
if (!IsConnected) return null;
try
{
Logger.LogDebug("Creating new Character Data");
return await _mareHub!.InvokeAsync<CharaDataFullDto?>(nameof(CharaDataCreate)).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Failed to create new character data");
return null;
}
}
public async Task<CharaDataFullDto?> CharaDataUpdate(CharaDataUpdateDto updateDto)
{
if (!IsConnected) return null;
try
{
Logger.LogDebug("Updating chara data for {id}", updateDto.Id);
return await _mareHub!.InvokeAsync<CharaDataFullDto?>(nameof(CharaDataUpdate), updateDto).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Failed to update chara data for {id}", updateDto.Id);
return null;
}
}
public async Task<bool> CharaDataDelete(string id)
{
if (!IsConnected) return false;
try
{
Logger.LogDebug("Deleting chara data for {id}", id);
return await _mareHub!.InvokeAsync<bool>(nameof(CharaDataDelete), id).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Failed to delete chara data for {id}", id);
return false;
}
}
public async Task<CharaDataMetaInfoDto?> CharaDataGetMetainfo(string id)
{
if (!IsConnected) return null;
try
{
Logger.LogDebug("Getting metainfo for chara data {id}", id);
return await _mareHub!.InvokeAsync<CharaDataMetaInfoDto?>(nameof(CharaDataGetMetainfo), id).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Failed to get meta info for chara data {id}", id);
return null;
}
}
public async Task<CharaDataFullDto?> CharaDataAttemptRestore(string id)
{
if (!IsConnected) return null;
try
{
Logger.LogDebug("Attempting to restore chara data {id}", id);
return await _mareHub!.InvokeAsync<CharaDataFullDto?>(nameof(CharaDataAttemptRestore), id).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Failed to restore chara data for {id}", id);
return null;
}
}
public async Task<List<CharaDataFullDto>> CharaDataGetOwn()
{
if (!IsConnected) return [];
try
{
Logger.LogDebug("Getting all own chara data");
return await _mareHub!.InvokeAsync<List<CharaDataFullDto>>(nameof(CharaDataGetOwn)).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Failed to get own chara data");
return [];
}
}
public async Task<List<CharaDataMetaInfoDto>> CharaDataGetShared()
{
if (!IsConnected) return [];
try
{
Logger.LogDebug("Getting all own chara data");
return await _mareHub!.InvokeAsync<List<CharaDataMetaInfoDto>>(nameof(CharaDataGetShared)).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Failed to get shared chara data");
return [];
}
}
public async Task<CharaDataDownloadDto?> CharaDataDownload(string id)
{
if (!IsConnected) return null;
try
{
Logger.LogDebug("Getting download chara data for {id}", id);
return await _mareHub!.InvokeAsync<CharaDataDownloadDto>(nameof(CharaDataDownload), id).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Failed to get download chara data for {id}", id);
return null;
}
}
public async Task<string> GposeLobbyCreate()
{
if (!IsConnected) return string.Empty;
try
{
Logger.LogDebug("Creating GPose Lobby");
return await _mareHub!.InvokeAsync<string>(nameof(GposeLobbyCreate)).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Failed to create GPose lobby");
return string.Empty;
}
}
public async Task<bool> GposeLobbyLeave()
{
if (!IsConnected) return true;
try
{
Logger.LogDebug("Leaving current GPose Lobby");
return await _mareHub!.InvokeAsync<bool>(nameof(GposeLobbyLeave)).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Failed to leave GPose lobby");
return false;
}
}
public async Task<List<UserData>> GposeLobbyJoin(string lobbyId)
{
if (!IsConnected) return [];
try
{
Logger.LogDebug("Joining GPose Lobby {id}", lobbyId);
return await _mareHub!.InvokeAsync<List<UserData>>(nameof(GposeLobbyJoin), lobbyId).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Failed to join GPose lobby {id}", lobbyId);
return [];
}
}
public async Task GposeLobbyPushCharacterData(CharaDataDownloadDto charaDownloadDto)
{
if (!IsConnected) return;
try
{
Logger.LogDebug("Sending Chara Data to GPose Lobby");
await _mareHub!.InvokeAsync(nameof(GposeLobbyPushCharacterData), charaDownloadDto).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Failed to send Chara Data to GPose lobby");
}
}
public async Task GposeLobbyPushPoseData(PoseData poseData)
{
if (!IsConnected) return;
try
{
Logger.LogDebug("Sending Pose Data to GPose Lobby");
await _mareHub!.InvokeAsync(nameof(GposeLobbyPushPoseData), poseData).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Failed to send Pose Data to GPose lobby");
}
}
public async Task GposeLobbyPushWorldData(WorldData worldData)
{
if (!IsConnected) return;
try
{
await _mareHub!.InvokeAsync(nameof(GposeLobbyPushWorldData), worldData).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Failed to send World Data to GPose lobby");
}
}
}

View File

@@ -0,0 +1,128 @@
using MareSynchronos.API.Data;
using MareSynchronos.API.Dto.Group;
using MareSynchronos.WebAPI.SignalR.Utils;
using Microsoft.AspNetCore.SignalR.Client;
namespace MareSynchronos.WebAPI;
public partial class ApiController
{
public async Task GroupBanUser(GroupPairDto dto, string reason)
{
CheckConnection();
await _mareHub!.SendAsync(nameof(GroupBanUser), dto, reason).ConfigureAwait(false);
}
public async Task GroupChangeGroupPermissionState(GroupPermissionDto dto)
{
CheckConnection();
await _mareHub!.SendAsync(nameof(GroupChangeGroupPermissionState), dto).ConfigureAwait(false);
}
public async Task GroupChangeIndividualPermissionState(GroupPairUserPermissionDto dto)
{
CheckConnection();
await _mareHub!.SendAsync(nameof(GroupChangeIndividualPermissionState), dto).ConfigureAwait(false);
}
public async Task GroupChangeOwnership(GroupPairDto groupPair)
{
CheckConnection();
await _mareHub!.SendAsync(nameof(GroupChangeOwnership), groupPair).ConfigureAwait(false);
}
public async Task<bool> GroupChangePassword(GroupPasswordDto groupPassword)
{
CheckConnection();
return await _mareHub!.InvokeAsync<bool>(nameof(GroupChangePassword), groupPassword).ConfigureAwait(false);
}
public async Task GroupChatSendMsg(GroupDto group, ChatMessage message)
{
CheckConnection();
await _mareHub!.SendAsync(nameof(GroupChatSendMsg), group, message).ConfigureAwait(false);
}
public async Task GroupClear(GroupDto group)
{
CheckConnection();
await _mareHub!.SendAsync(nameof(GroupClear), group).ConfigureAwait(false);
}
public async Task<GroupPasswordDto> GroupCreate()
{
CheckConnection();
return await _mareHub!.InvokeAsync<GroupPasswordDto>(nameof(GroupCreate)).ConfigureAwait(false);
}
public async Task<List<string>> GroupCreateTempInvite(GroupDto group, int amount)
{
CheckConnection();
return await _mareHub!.InvokeAsync<List<string>>(nameof(GroupCreateTempInvite), group, amount).ConfigureAwait(false);
}
public async Task GroupDelete(GroupDto group)
{
CheckConnection();
await _mareHub!.SendAsync(nameof(GroupDelete), group).ConfigureAwait(false);
}
public async Task<List<BannedGroupUserDto>> GroupGetBannedUsers(GroupDto group)
{
CheckConnection();
return await _mareHub!.InvokeAsync<List<BannedGroupUserDto>>(nameof(GroupGetBannedUsers), group).ConfigureAwait(false);
}
public async Task<bool> GroupJoin(GroupPasswordDto passwordedGroup)
{
CheckConnection();
return await _mareHub!.InvokeAsync<bool>(nameof(GroupJoin), passwordedGroup).ConfigureAwait(false);
}
public async Task GroupLeave(GroupDto group)
{
CheckConnection();
await _mareHub!.SendAsync(nameof(GroupLeave), group).ConfigureAwait(false);
}
public async Task GroupRemoveUser(GroupPairDto groupPair)
{
CheckConnection();
await _mareHub!.SendAsync(nameof(GroupRemoveUser), groupPair).ConfigureAwait(false);
}
public async Task GroupSetUserInfo(GroupPairUserInfoDto groupPair)
{
CheckConnection();
await _mareHub!.SendAsync(nameof(GroupSetUserInfo), groupPair).ConfigureAwait(false);
}
public async Task<int> GroupPrune(GroupDto group, int days, bool execute)
{
CheckConnection();
return await _mareHub!.InvokeAsync<int>(nameof(GroupPrune), group, days, execute).ConfigureAwait(false);
}
public async Task<List<GroupFullInfoDto>> GroupsGetAll()
{
CheckConnection();
return await _mareHub!.InvokeAsync<List<GroupFullInfoDto>>(nameof(GroupsGetAll)).ConfigureAwait(false);
}
public async Task<List<GroupPairFullInfoDto>> GroupsGetUsersInGroup(GroupDto group)
{
CheckConnection();
return await _mareHub!.InvokeAsync<List<GroupPairFullInfoDto>>(nameof(GroupsGetUsersInGroup), group).ConfigureAwait(false);
}
public async Task GroupUnbanUser(GroupPairDto groupPair)
{
CheckConnection();
await _mareHub!.SendAsync(nameof(GroupUnbanUser), groupPair).ConfigureAwait(false);
}
private void CheckConnection()
{
if (ServerState is not (ServerState.Connected or ServerState.Connecting or ServerState.Reconnecting)) throw new InvalidDataException("Not connected");
}
}

View File

@@ -0,0 +1,482 @@
using Dalamud.Utility;
using MareSynchronos.API.Data;
using MareSynchronos.API.Data.Extensions;
using MareSynchronos.API.Dto;
using MareSynchronos.API.Dto.User;
using MareSynchronos.API.SignalR;
using MareSynchronos.MareConfiguration;
using MareSynchronos.MareConfiguration.Models;
using MareSynchronos.PlayerData.Pairs;
using MareSynchronos.Services;
using MareSynchronos.Services.Mediator;
using MareSynchronos.Services.ServerConfiguration;
using MareSynchronos.WebAPI.SignalR;
using MareSynchronos.WebAPI.SignalR.Utils;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging;
using System.Reflection;
namespace MareSynchronos.WebAPI;
#pragma warning disable MA0040
public sealed partial class ApiController : DisposableMediatorSubscriberBase, IMareHubClient
{
public const string ElezenServer = "Snowcloak Main Server";
public const string ElezenServiceUri = "wss://hub.snowcloak-sync.com";
public const string ElezenServiceApiUri = "wss://hub.snowcloak-sync.com/";
public const string ElezenServiceHubUri = "wss://hub.snowcloak-sync.com/mare";
private readonly DalamudUtilService _dalamudUtil;
private readonly HubFactory _hubFactory;
private readonly PairManager _pairManager;
private readonly ServerConfigurationManager _serverManager;
private readonly TokenProvider _tokenProvider;
private CancellationTokenSource _connectionCancellationTokenSource;
private ConnectionDto? _connectionDto;
private bool _doNotNotifyOnNextInfo = false;
private CancellationTokenSource? _healthCheckTokenSource = new();
private bool _initialized;
private HubConnection? _mareHub;
private ServerState _serverState;
private CensusUpdateMessage? _lastCensus;
public ApiController(ILogger<ApiController> logger, HubFactory hubFactory, DalamudUtilService dalamudUtil,
PairManager pairManager, ServerConfigurationManager serverManager, MareMediator mediator,
TokenProvider tokenProvider) : base(logger, mediator)
{
_hubFactory = hubFactory;
_dalamudUtil = dalamudUtil;
_pairManager = pairManager;
_serverManager = serverManager;
_tokenProvider = tokenProvider;
_connectionCancellationTokenSource = new CancellationTokenSource();
Mediator.Subscribe<DalamudLoginMessage>(this, (_) => DalamudUtilOnLogIn());
Mediator.Subscribe<DalamudLogoutMessage>(this, (_) => DalamudUtilOnLogOut());
Mediator.Subscribe<HubClosedMessage>(this, (msg) => MareHubOnClosed(msg.Exception));
Mediator.Subscribe<HubReconnectedMessage>(this, (msg) => _ = MareHubOnReconnected());
Mediator.Subscribe<HubReconnectingMessage>(this, (msg) => MareHubOnReconnecting(msg.Exception));
Mediator.Subscribe<CyclePauseMessage>(this, (msg) => _ = CyclePause(msg.UserData));
Mediator.Subscribe<CensusUpdateMessage>(this, (msg) => _lastCensus = msg);
Mediator.Subscribe<PauseMessage>(this, (msg) => _ = Pause(msg.UserData));
ServerState = ServerState.Offline;
if (_dalamudUtil.IsLoggedIn)
{
DalamudUtilOnLogIn();
}
}
public string AuthFailureMessage { get; private set; } = string.Empty;
public Version CurrentClientVersion => _connectionDto?.CurrentClientVersion ?? new Version(0, 0, 0);
public string DisplayName => _connectionDto?.User.AliasOrUID ?? string.Empty;
public bool IsConnected => ServerState == ServerState.Connected;
public bool IsCurrentVersion => (Assembly.GetExecutingAssembly().GetName().Version ?? new Version(0, 0, 0, 0)) >= (_connectionDto?.CurrentClientVersion ?? new Version(0, 0, 0, 0));
public int OnlineUsers => SystemInfoDto.OnlineUsers;
public bool ServerAlive => ServerState is ServerState.Connected or ServerState.RateLimited or ServerState.Unauthorized or ServerState.Disconnected;
public ServerInfo ServerInfo => _connectionDto?.ServerInfo ?? new ServerInfo();
public ServerState ServerState
{
get => _serverState;
private set
{
Logger.LogDebug("New ServerState: {value}, prev ServerState: {_serverState}", value, _serverState);
_serverState = value;
}
}
public SystemInfoDto SystemInfoDto { get; private set; } = new();
public string UID => _connectionDto?.User.UID ?? string.Empty;
public async Task<bool> CheckClientHealth()
{
return await _mareHub!.InvokeAsync<bool>(nameof(CheckClientHealth)).ConfigureAwait(false);
}
public async Task CreateConnections()
{
Logger.LogDebug("CreateConnections called");
if (_serverManager.CurrentServer?.FullPause ?? true)
{
Logger.LogInformation("Not recreating Connection, paused");
_connectionDto = null;
await StopConnection(ServerState.Disconnected).ConfigureAwait(false);
_connectionCancellationTokenSource?.Cancel();
return;
}
var secretKey = _serverManager.GetSecretKey(out bool multi);
if (multi)
{
Logger.LogWarning("Multiple secret keys for current character");
_connectionDto = null;
Mediator.Publish(new NotificationMessage("Multiple Identical Characters detected", "Your Service configuration has multiple characters with the same name and world set up. Delete the duplicates in the character management to be able to connect to Mare.",
NotificationType.Error));
await StopConnection(ServerState.MultiChara).ConfigureAwait(false);
_connectionCancellationTokenSource?.Cancel();
return;
}
if (secretKey == null)
{
Logger.LogWarning("No secret key set for current character");
_connectionDto = null;
await StopConnection(ServerState.NoSecretKey).ConfigureAwait(false);
_connectionCancellationTokenSource?.Cancel();
return;
}
await StopConnection(ServerState.Disconnected).ConfigureAwait(false);
Logger.LogInformation("Recreating Connection");
Mediator.Publish(new EventMessage(new Services.Events.Event(nameof(ApiController), Services.Events.EventSeverity.Informational,
$"Starting Connection to {_serverManager.CurrentServer.ServerName}")));
_connectionCancellationTokenSource?.Cancel();
_connectionCancellationTokenSource?.Dispose();
_connectionCancellationTokenSource = new CancellationTokenSource();
var token = _connectionCancellationTokenSource.Token;
while (ServerState is not ServerState.Connected && !token.IsCancellationRequested)
{
AuthFailureMessage = string.Empty;
await StopConnection(ServerState.Disconnected).ConfigureAwait(false);
ServerState = ServerState.Connecting;
try
{
Logger.LogDebug("Building connection");
try
{
await _tokenProvider.GetOrUpdateToken(token).ConfigureAwait(false);
}
catch (MareAuthFailureException ex)
{
AuthFailureMessage = ex.Reason;
throw new HttpRequestException("Error during authentication", ex, System.Net.HttpStatusCode.Unauthorized);
}
while (!await _dalamudUtil.GetIsPlayerPresentAsync().ConfigureAwait(false) && !token.IsCancellationRequested)
{
Logger.LogDebug("Player not loaded in yet, waiting");
await Task.Delay(TimeSpan.FromSeconds(1), token).ConfigureAwait(false);
}
if (token.IsCancellationRequested) break;
_mareHub = await _hubFactory.GetOrCreate(token).ConfigureAwait(false);
InitializeApiHooks();
await _mareHub.StartAsync(token).ConfigureAwait(false);
_connectionDto = await GetConnectionDto().ConfigureAwait(false);
ServerState = ServerState.Connected;
var currentClientVer = Assembly.GetExecutingAssembly().GetName().Version!;
if (_connectionDto.ServerVersion != IMareHub.ApiVersion)
{
if (_connectionDto.CurrentClientVersion > currentClientVer)
{
Mediator.Publish(new NotificationMessage("Client incompatible",
$"Your client is outdated ({currentClientVer.Major}.{currentClientVer.Minor}.{currentClientVer.Build}), current is: " +
$"{_connectionDto.CurrentClientVersion.Major}.{_connectionDto.CurrentClientVersion.Minor}.{_connectionDto.CurrentClientVersion.Build}. " +
$"This client version is incompatible and will not be able to connect. Please update your Elezen client.",
NotificationType.Error));
}
await StopConnection(ServerState.VersionMisMatch).ConfigureAwait(false);
return;
}
if (_connectionDto.CurrentClientVersion > currentClientVer)
{
Mediator.Publish(new NotificationMessage("Client outdated",
$"Your client is outdated ({currentClientVer.Major}.{currentClientVer.Minor}.{currentClientVer.Build}), current is: " +
$"{_connectionDto.CurrentClientVersion.Major}.{_connectionDto.CurrentClientVersion.Minor}.{_connectionDto.CurrentClientVersion.Build}. " +
$"Please keep your Elezen client up-to-date.",
NotificationType.Warning, TimeSpan.FromSeconds(15)));
}
if (_dalamudUtil.HasModifiedGameFiles)
{
Logger.LogWarning("Detected modified game files on connection");
#if false
Mediator.Publish(new NotificationMessage("Modified Game Files detected",
"Dalamud has reported modified game files in your FFXIV installation. " +
"You will be able to connect, but the synchronization functionality might be (partially) broken. " +
"Exit the game and repair it through XIVLauncher to get rid of this message.",
NotificationType.Error, TimeSpan.FromSeconds(15)));
#endif
}
await LoadIninitialPairs().ConfigureAwait(false);
await LoadOnlinePairs().ConfigureAwait(false);
}
catch (OperationCanceledException)
{
Logger.LogWarning("Connection attempt cancelled");
return;
}
catch (HttpRequestException ex)
{
Logger.LogWarning(ex, "HttpRequestException on Connection");
if (ex.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
await StopConnection(ServerState.Unauthorized).ConfigureAwait(false);
return;
}
ServerState = ServerState.Reconnecting;
Logger.LogInformation("Failed to establish connection, retrying");
await Task.Delay(TimeSpan.FromSeconds(new Random().Next(5, 20)), token).ConfigureAwait(false);
}
catch (InvalidOperationException ex)
{
Logger.LogWarning(ex, "InvalidOperationException on connection");
await StopConnection(ServerState.Disconnected).ConfigureAwait(false);
return;
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Exception on Connection");
Logger.LogInformation("Failed to establish connection, retrying");
await Task.Delay(TimeSpan.FromSeconds(new Random().Next(5, 20)), token).ConfigureAwait(false);
}
}
}
public Task CyclePause(UserData userData)
{
CancellationTokenSource cts = new();
cts.CancelAfter(TimeSpan.FromSeconds(5));
_ = Task.Run(async () =>
{
var pair = _pairManager.GetOnlineUserPairs().Single(p => p.UserPair != null && p.UserData == userData);
var perm = pair.UserPair!.OwnPermissions;
perm.SetPaused(paused: true);
await UserSetPairPermissions(new UserPermissionsDto(userData, perm)).ConfigureAwait(false);
// wait until it's changed
while (pair.UserPair!.OwnPermissions != perm)
{
await Task.Delay(250, cts.Token).ConfigureAwait(false);
Logger.LogTrace("Waiting for permissions change for {data}", userData);
}
perm.SetPaused(paused: false);
await UserSetPairPermissions(new UserPermissionsDto(userData, perm)).ConfigureAwait(false);
}, cts.Token).ContinueWith((t) => cts.Dispose());
return Task.CompletedTask;
}
public async Task Pause(UserData userData)
{
var pair = _pairManager.GetOnlineUserPairs().Single(p => p.UserPair != null && p.UserData == userData);
var perm = pair.UserPair!.OwnPermissions;
perm.SetPaused(paused: true);
await UserSetPairPermissions(new UserPermissionsDto(userData, perm)).ConfigureAwait(false);
}
public Task<ConnectionDto> GetConnectionDto() => GetConnectionDto(true);
public async Task<ConnectionDto> GetConnectionDto(bool publishConnected = true)
{
var dto = await _mareHub!.InvokeAsync<ConnectionDto>(nameof(GetConnectionDto)).ConfigureAwait(false);
if (publishConnected) Mediator.Publish(new ConnectedMessage(dto));
return dto;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
_healthCheckTokenSource?.Cancel();
_ = Task.Run(async () => await StopConnection(ServerState.Disconnected).ConfigureAwait(false));
_connectionCancellationTokenSource?.Cancel();
}
private async Task ClientHealthCheck(CancellationToken ct)
{
while (!ct.IsCancellationRequested && _mareHub != null)
{
await Task.Delay(TimeSpan.FromSeconds(30), ct).ConfigureAwait(false);
Logger.LogDebug("Checking Client Health State");
_ = await CheckClientHealth().ConfigureAwait(false);
}
}
private void DalamudUtilOnLogIn()
{
_ = Task.Run(() => CreateConnections());
}
private void DalamudUtilOnLogOut()
{
_ = Task.Run(async () => await StopConnection(ServerState.Disconnected).ConfigureAwait(false));
ServerState = ServerState.Offline;
}
private void InitializeApiHooks()
{
if (_mareHub == null) return;
Logger.LogDebug("Initializing data");
OnDownloadReady((guid) => _ = Client_DownloadReady(guid));
OnReceiveServerMessage((sev, msg) => _ = Client_ReceiveServerMessage(sev, msg));
OnUpdateSystemInfo((dto) => _ = Client_UpdateSystemInfo(dto));
OnUserSendOffline((dto) => _ = Client_UserSendOffline(dto));
OnUserAddClientPair((dto) => _ = Client_UserAddClientPair(dto));
OnUserReceiveCharacterData((dto) => _ = Client_UserReceiveCharacterData(dto));
OnUserRemoveClientPair(dto => _ = Client_UserRemoveClientPair(dto));
OnUserSendOnline(dto => _ = Client_UserSendOnline(dto));
OnUserUpdateOtherPairPermissions(dto => _ = Client_UserUpdateOtherPairPermissions(dto));
OnUserUpdateSelfPairPermissions(dto => _ = Client_UserUpdateSelfPairPermissions(dto));
OnUserReceiveUploadStatus(dto => _ = Client_UserReceiveUploadStatus(dto));
OnUserUpdateProfile(dto => _ = Client_UserUpdateProfile(dto));
OnGroupChangePermissions((dto) => _ = Client_GroupChangePermissions(dto));
OnGroupDelete((dto) => _ = Client_GroupDelete(dto));
OnGroupPairChangeUserInfo((dto) => _ = Client_GroupPairChangeUserInfo(dto));
OnGroupPairJoined((dto) => _ = Client_GroupPairJoined(dto));
OnGroupPairLeft((dto) => _ = Client_GroupPairLeft(dto));
OnGroupSendFullInfo((dto) => _ = Client_GroupSendFullInfo(dto));
OnGroupSendInfo((dto) => _ = Client_GroupSendInfo(dto));
OnGroupPairChangePermissions((dto) => _ = Client_GroupPairChangePermissions(dto));
OnUserChatMsg((dto) => _ = Client_UserChatMsg(dto));
OnGroupChatMsg((dto) => _ = Client_GroupChatMsg(dto));
OnGposeLobbyJoin((dto) => _ = Client_GposeLobbyJoin(dto));
OnGposeLobbyLeave((dto) => _ = Client_GposeLobbyLeave(dto));
OnGposeLobbyPushCharacterData((dto) => _ = Client_GposeLobbyPushCharacterData(dto));
OnGposeLobbyPushPoseData((dto, data) => _ = Client_GposeLobbyPushPoseData(dto, data));
OnGposeLobbyPushWorldData((dto, data) => _ = Client_GposeLobbyPushWorldData(dto, data));
_healthCheckTokenSource?.Cancel();
_healthCheckTokenSource?.Dispose();
_healthCheckTokenSource = new CancellationTokenSource();
_ = ClientHealthCheck(_healthCheckTokenSource.Token);
_initialized = true;
}
private async Task LoadIninitialPairs()
{
foreach (var userPair in await UserGetPairedClients().ConfigureAwait(false))
{
Logger.LogDebug("Individual Pair: {userPair}", userPair);
_pairManager.AddUserPair(userPair, addToLastAddedUser: false);
}
foreach (var entry in await GroupsGetAll().ConfigureAwait(false))
{
Logger.LogDebug("Group: {entry}", entry);
_pairManager.AddGroup(entry);
}
foreach (var group in _pairManager.GroupPairs.Keys)
{
var users = await GroupsGetUsersInGroup(group).ConfigureAwait(false);
foreach (var user in users)
{
Logger.LogDebug("Group Pair: {user}", user);
_pairManager.AddGroupPair(user);
}
}
}
private async Task LoadOnlinePairs()
{
foreach (var entry in await UserGetOnlinePairs().ConfigureAwait(false))
{
Logger.LogDebug("Pair online: {pair}", entry);
_pairManager.MarkPairOnline(entry, sendNotif: false);
}
}
private void MareHubOnClosed(Exception? arg)
{
_healthCheckTokenSource?.Cancel();
Mediator.Publish(new DisconnectedMessage());
ServerState = ServerState.Offline;
if (arg != null)
{
Logger.LogWarning(arg, "Connection closed");
}
else
{
Logger.LogInformation("Connection closed");
}
}
private async Task MareHubOnReconnected()
{
ServerState = ServerState.Reconnecting;
try
{
InitializeApiHooks();
_connectionDto = await GetConnectionDto(publishConnected: false).ConfigureAwait(false);
if (_connectionDto.ServerVersion != IMareHub.ApiVersion)
{
await StopConnection(ServerState.VersionMisMatch).ConfigureAwait(false);
return;
}
ServerState = ServerState.Connected;
await LoadIninitialPairs().ConfigureAwait(false);
await LoadOnlinePairs().ConfigureAwait(false);
Mediator.Publish(new ConnectedMessage(_connectionDto));
}
catch (Exception ex)
{
Logger.LogCritical(ex, "Failure to obtain data after reconnection");
await StopConnection(ServerState.Disconnected).ConfigureAwait(false);
}
}
private void MareHubOnReconnecting(Exception? arg)
{
_doNotNotifyOnNextInfo = true;
_healthCheckTokenSource?.Cancel();
ServerState = ServerState.Reconnecting;
Logger.LogWarning(arg, "Connection closed... Reconnecting");
Mediator.Publish(new EventMessage(new Services.Events.Event(nameof(ApiController), Services.Events.EventSeverity.Warning,
$"Connection interrupted, reconnecting to {_serverManager.CurrentServer.ServerName}")));
}
private async Task StopConnection(ServerState state)
{
ServerState = ServerState.Disconnecting;
Logger.LogInformation("Stopping existing connection");
await _hubFactory.DisposeHubAsync().ConfigureAwait(false);
if (_mareHub is not null)
{
Mediator.Publish(new EventMessage(new Services.Events.Event(nameof(ApiController), Services.Events.EventSeverity.Informational,
$"Stopping existing connection to {_serverManager.CurrentServer.ServerName}")));
_initialized = false;
_healthCheckTokenSource?.Cancel();
Mediator.Publish(new DisconnectedMessage());
_mareHub = null;
_connectionDto = null;
}
ServerState = state;
}
}
#pragma warning restore MA0040

View File

@@ -0,0 +1,50 @@
using Microsoft.AspNetCore.Http.Connections;
using System.Text.Json.Serialization;
namespace MareSynchronos.WebAPI.SignalR;
public record HubConnectionConfig
{
[JsonPropertyName("api_url")]
public string ApiUrl { get; set; } = string.Empty;
[JsonPropertyName("hub_url")]
public string HubUrl { get; set; } = string.Empty;
private readonly bool? _skipNegotiation;
[JsonPropertyName("skip_negotiation")]
public bool SkipNegotiation
{
get => _skipNegotiation ?? true;
init => _skipNegotiation = value;
}
[JsonPropertyName("transports")]
public string[]? Transports { get; set; }
[JsonIgnore]
public HttpTransportType TransportType
{
get
{
if (Transports == null || Transports.Length == 0)
return HttpTransportType.WebSockets;
HttpTransportType result = HttpTransportType.None;
foreach (var transport in Transports)
{
result |= transport.ToLowerInvariant() switch
{
"websockets" => HttpTransportType.WebSockets,
"serversentevents" => HttpTransportType.ServerSentEvents,
"longpolling" => HttpTransportType.LongPolling,
_ => HttpTransportType.None
};
}
return result;
}
}
}

View File

@@ -0,0 +1,240 @@
using MareSynchronos.API.SignalR;
using MareSynchronos.Services;
using MareSynchronos.Services.Mediator;
using MareSynchronos.Services.ServerConfiguration;
using MareSynchronos.WebAPI.SignalR.Utils;
using MessagePack;
using MessagePack.Resolvers;
using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text.Json;
namespace MareSynchronos.WebAPI.SignalR;
public class HubFactory : MediatorSubscriberBase
{
private readonly ILoggerProvider _loggingProvider;
private readonly ServerConfigurationManager _serverConfigurationManager;
private readonly RemoteConfigurationService _remoteConfig;
private readonly TokenProvider _tokenProvider;
private HubConnection? _instance;
private string _cachedConfigFor = string.Empty;
private HubConnectionConfig? _cachedConfig;
private bool _isDisposed = false;
public HubFactory(ILogger<HubFactory> logger, MareMediator mediator,
ServerConfigurationManager serverConfigurationManager, RemoteConfigurationService remoteConfig,
TokenProvider tokenProvider, ILoggerProvider pluginLog) : base(logger, mediator)
{
_serverConfigurationManager = serverConfigurationManager;
_remoteConfig = remoteConfig;
_tokenProvider = tokenProvider;
_loggingProvider = pluginLog;
}
public async Task DisposeHubAsync()
{
if (_instance == null || _isDisposed) return;
Logger.LogDebug("Disposing current HubConnection");
_isDisposed = true;
_instance.Closed -= HubOnClosed;
_instance.Reconnecting -= HubOnReconnecting;
_instance.Reconnected -= HubOnReconnected;
await _instance.StopAsync().ConfigureAwait(false);
await _instance.DisposeAsync().ConfigureAwait(false);
_instance = null;
Logger.LogDebug("Current HubConnection disposed");
}
public async Task<HubConnection> GetOrCreate(CancellationToken ct)
{
if (!_isDisposed && _instance != null) return _instance;
_cachedConfig = await ResolveHubConfig().ConfigureAwait(false);
_cachedConfigFor = _serverConfigurationManager.CurrentApiUrl;
return BuildHubConnection(_cachedConfig, ct);
}
private async Task<HubConnectionConfig> ResolveHubConfig()
{
var stapledWellKnown = _tokenProvider.GetStapledWellKnown(_serverConfigurationManager.CurrentApiUrl);
var apiUrl = new Uri(_serverConfigurationManager.CurrentApiUrl);
HubConnectionConfig defaultConfig;
if (_cachedConfig != null && _serverConfigurationManager.CurrentApiUrl.Equals(_cachedConfigFor, StringComparison.Ordinal))
{
defaultConfig = _cachedConfig;
}
else
{
defaultConfig = new HubConnectionConfig
{
HubUrl = _serverConfigurationManager.CurrentApiUrl.TrimEnd('/') + IMareHub.Path,
Transports = []
};
}
if (_serverConfigurationManager.CurrentApiUrl.Equals(ApiController.ElezenServiceUri, StringComparison.Ordinal))
{
var mainServerConfig = await _remoteConfig.GetConfigAsync<HubConnectionConfig>("mainServer").ConfigureAwait(false) ?? new();
defaultConfig = mainServerConfig;
if (string.IsNullOrEmpty(mainServerConfig.ApiUrl))
defaultConfig.ApiUrl = ApiController.ElezenServiceApiUri;
if (string.IsNullOrEmpty(mainServerConfig.HubUrl))
defaultConfig.HubUrl = ApiController.ElezenServiceHubUri;
}
string jsonResponse;
if (stapledWellKnown != null)
{
jsonResponse = stapledWellKnown;
Logger.LogTrace("Using stapled hub config for {url}", _serverConfigurationManager.CurrentApiUrl);
}
else
{
try
{
var httpScheme = apiUrl.Scheme.ToLowerInvariant() switch
{
"ws" => "http",
"wss" => "https",
_ => apiUrl.Scheme
};
var wellKnownUrl = $"{httpScheme}://{apiUrl.Host}/.well-known/Elezen/client";
Logger.LogTrace("Fetching hub config for {uri} via {wk}", _serverConfigurationManager.CurrentApiUrl, wellKnownUrl);
using var httpClient = new HttpClient(
new HttpClientHandler
{
AllowAutoRedirect = true,
MaxAutomaticRedirections = 5
}
);
var ver = Assembly.GetExecutingAssembly().GetName().Version;
httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("MareSynchronos", ver!.Major + "." + ver!.Minor + "." + ver!.Build));
var response = await httpClient.GetAsync(wellKnownUrl).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
return defaultConfig;
var contentType = response.Content.Headers.ContentType?.MediaType;
if (contentType == null || !contentType.Equals("application/json", StringComparison.Ordinal))
return defaultConfig;
jsonResponse = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
catch (HttpRequestException ex)
{
Logger.LogWarning(ex, "HTTP request failed for .well-known");
return defaultConfig;
}
}
try
{
var config = JsonSerializer.Deserialize<HubConnectionConfig>(jsonResponse);
if (config == null)
return defaultConfig;
if (string.IsNullOrEmpty(config.ApiUrl))
config.ApiUrl = defaultConfig.ApiUrl;
if (string.IsNullOrEmpty(config.HubUrl))
config.HubUrl = defaultConfig.HubUrl;
config.Transports ??= defaultConfig.Transports ?? [];
return config;
}
catch (JsonException ex)
{
Logger.LogWarning(ex, "Invalid JSON in .well-known response");
return defaultConfig;
}
}
private HubConnection BuildHubConnection(HubConnectionConfig hubConfig, CancellationToken ct)
{
Logger.LogDebug("Building new HubConnection");
_instance = new HubConnectionBuilder()
.WithUrl(hubConfig.HubUrl, options =>
{
var transports = hubConfig.TransportType;
options.AccessTokenProvider = () => _tokenProvider.GetOrUpdateToken(ct);
options.SkipNegotiation = hubConfig.SkipNegotiation && (transports == HttpTransportType.WebSockets);
options.Transports = transports;
})
.AddMessagePackProtocol(opt =>
{
var resolver = CompositeResolver.Create(StandardResolverAllowPrivate.Instance,
BuiltinResolver.Instance,
AttributeFormatterResolver.Instance,
// replace enum resolver
DynamicEnumAsStringResolver.Instance,
DynamicGenericResolver.Instance,
DynamicUnionResolver.Instance,
DynamicObjectResolver.Instance,
PrimitiveObjectResolver.Instance,
// final fallback(last priority)
StandardResolver.Instance);
opt.SerializerOptions =
MessagePackSerializerOptions.Standard
.WithCompression(MessagePackCompression.Lz4Block)
.WithResolver(resolver);
})
.WithAutomaticReconnect(new ForeverRetryPolicy(Mediator))
.ConfigureLogging(a =>
{
a.ClearProviders().AddProvider(_loggingProvider);
a.SetMinimumLevel(LogLevel.Information);
})
.Build();
_instance.Closed += HubOnClosed;
_instance.Reconnecting += HubOnReconnecting;
_instance.Reconnected += HubOnReconnected;
_isDisposed = false;
return _instance;
}
private Task HubOnClosed(Exception? arg)
{
Mediator.Publish(new HubClosedMessage(arg));
return Task.CompletedTask;
}
private Task HubOnReconnected(string? arg)
{
Mediator.Publish(new HubReconnectedMessage(arg));
return Task.CompletedTask;
}
private Task HubOnReconnecting(Exception? arg)
{
Mediator.Publish(new HubReconnectingMessage(arg));
return Task.CompletedTask;
}
}

View File

@@ -0,0 +1,9 @@
namespace MareSynchronos.WebAPI.SignalR;
public record JwtIdentifier(string ApiUrl, string CharaHash, string SecretKey)
{
public override string ToString()
{
return "{JwtIdentifier; Url: " + ApiUrl + ", Chara: " + CharaHash + ", HasSecretKey: " + !string.IsNullOrEmpty(SecretKey) + "}";
}
}

View File

@@ -0,0 +1,11 @@
namespace MareSynchronos.WebAPI.SignalR;
public class MareAuthFailureException : Exception
{
public MareAuthFailureException(string reason)
{
Reason = reason;
}
public string Reason { get; }
}

View File

@@ -0,0 +1,197 @@
using MareSynchronos.API.Routes;
using MareSynchronos.MareConfiguration.Models;
using MareSynchronos.Services;
using MareSynchronos.Services.Mediator;
using MareSynchronos.Services.ServerConfiguration;
using MareSynchronos.Utils;
using MareSynchronos.API.Dto;
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Reflection;
namespace MareSynchronos.WebAPI.SignalR;
public sealed class TokenProvider : IDisposable, IMediatorSubscriber
{
private readonly DalamudUtilService _dalamudUtil;
private readonly HttpClient _httpClient;
private readonly ILogger<TokenProvider> _logger;
private readonly ServerConfigurationManager _serverManager;
private readonly RemoteConfigurationService _remoteConfig;
private readonly ConcurrentDictionary<JwtIdentifier, string> _tokenCache = new();
private readonly ConcurrentDictionary<string, string?> _wellKnownCache = new(StringComparer.Ordinal);
public TokenProvider(ILogger<TokenProvider> logger, ServerConfigurationManager serverManager, RemoteConfigurationService remoteConfig,
DalamudUtilService dalamudUtil, MareMediator mareMediator)
{
_logger = logger;
_serverManager = serverManager;
_remoteConfig = remoteConfig;
_dalamudUtil = dalamudUtil;
_httpClient = new(
new HttpClientHandler
{
AllowAutoRedirect = true,
MaxAutomaticRedirections = 5
}
);
var ver = Assembly.GetExecutingAssembly().GetName().Version;
Mediator = mareMediator;
Mediator.Subscribe<DalamudLogoutMessage>(this, (_) =>
{
_lastJwtIdentifier = null;
_tokenCache.Clear();
_wellKnownCache.Clear();
});
Mediator.Subscribe<DalamudLoginMessage>(this, (_) =>
{
_lastJwtIdentifier = null;
_tokenCache.Clear();
_wellKnownCache.Clear();
});
_httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("MareSynchronos", ver!.Major + "." + ver!.Minor + "." + ver!.Build));
}
public MareMediator Mediator { get; }
private JwtIdentifier? _lastJwtIdentifier;
public void Dispose()
{
Mediator.UnsubscribeAll(this);
_httpClient.Dispose();
}
public async Task<string> GetNewToken(JwtIdentifier identifier, CancellationToken token)
{
Uri tokenUri;
HttpResponseMessage result;
var authApiUrl = _serverManager.CurrentApiUrl;
// Override the API URL used for auth from remote config, if one is available
if (authApiUrl.Equals(ApiController.ElezenServiceUri, StringComparison.Ordinal))
{
var config = await _remoteConfig.GetConfigAsync<HubConnectionConfig>("mainServer").ConfigureAwait(false) ?? new();
if (!string.IsNullOrEmpty(config.ApiUrl))
authApiUrl = config.ApiUrl;
else
authApiUrl = ApiController.ElezenServiceApiUri;
}
try
{
_logger.LogDebug("GetNewToken: Requesting");
tokenUri = MareAuth.AuthV2FullPath(new Uri(authApiUrl
.Replace("wss://", "https://", StringComparison.OrdinalIgnoreCase)
.Replace("ws://", "http://", StringComparison.OrdinalIgnoreCase)));
var secretKey = _serverManager.GetSecretKey(out _)!;
var auth = secretKey.GetHash256();
result = await _httpClient.PostAsync(tokenUri, new FormUrlEncodedContent([
new("auth", auth),
new("charaIdent", await _dalamudUtil.GetPlayerNameHashedAsync().ConfigureAwait(false)),
]), token).ConfigureAwait(false);
if (!result.IsSuccessStatusCode)
{
Mediator.Publish(new NotificationMessage("Error refreshing token", "Your authentication token could not be renewed. Try reconnecting manually.", NotificationType.Error));
Mediator.Publish(new DisconnectedMessage());
var textResponse = await result.Content.ReadAsStringAsync(token).ConfigureAwait(false) ?? string.Empty;
throw new MareAuthFailureException(textResponse);
}
var response = await result.Content.ReadFromJsonAsync<AuthReplyDto>(token).ConfigureAwait(false) ?? new();
_tokenCache[identifier] = response.Token;
_wellKnownCache[_serverManager.CurrentApiUrl] = response.WellKnown;
return response.Token;
}
catch (HttpRequestException ex)
{
_tokenCache.TryRemove(identifier, out _);
_wellKnownCache.TryRemove(_serverManager.CurrentApiUrl, out _);
_logger.LogError(ex, "GetNewToken: Failure to get token");
if (ex.StatusCode == HttpStatusCode.Unauthorized)
{
Mediator.Publish(new NotificationMessage("Error refreshing token", "Your authentication token could not be renewed. Try reconnecting manually.", NotificationType.Error));
Mediator.Publish(new DisconnectedMessage());
throw new MareAuthFailureException(ex.Message);
}
throw;
}
}
private async Task<JwtIdentifier?> GetIdentifier()
{
JwtIdentifier jwtIdentifier;
try
{
var playerIdentifier = await _dalamudUtil.GetPlayerNameHashedAsync().ConfigureAwait(false);
if (string.IsNullOrEmpty(playerIdentifier))
{
_logger.LogTrace("GetIdentifier: PlayerIdentifier was null, returning last identifier {identifier}", _lastJwtIdentifier);
return _lastJwtIdentifier;
}
jwtIdentifier = new(_serverManager.CurrentApiUrl,
playerIdentifier,
_serverManager.GetSecretKey(out _)!);
_lastJwtIdentifier = jwtIdentifier;
}
catch (Exception ex)
{
if (_lastJwtIdentifier == null)
{
_logger.LogError("GetIdentifier: No last identifier found, aborting");
return null;
}
_logger.LogWarning(ex, "GetIdentifier: Could not get JwtIdentifier for some reason or another, reusing last identifier {identifier}", _lastJwtIdentifier);
jwtIdentifier = _lastJwtIdentifier;
}
_logger.LogDebug("GetIdentifier: Using identifier {identifier}", jwtIdentifier);
return jwtIdentifier;
}
public async Task<string?> GetToken()
{
JwtIdentifier? jwtIdentifier = await GetIdentifier().ConfigureAwait(false);
if (jwtIdentifier == null) return null;
if (_tokenCache.TryGetValue(jwtIdentifier, out var token))
{
return token;
}
throw new InvalidOperationException("No token present");
}
public async Task<string?> GetOrUpdateToken(CancellationToken ct)
{
JwtIdentifier? jwtIdentifier = await GetIdentifier().ConfigureAwait(false);
if (jwtIdentifier == null) return null;
if (_tokenCache.TryGetValue(jwtIdentifier, out var token))
return token;
_logger.LogTrace("GetOrUpdate: Getting new token");
return await GetNewToken(jwtIdentifier, ct).ConfigureAwait(false);
}
public string? GetStapledWellKnown(string apiUrl)
{
_wellKnownCache.TryGetValue(apiUrl, out var wellKnown);
// Treat an empty string as null -- it won't decode as JSON anyway
if (string.IsNullOrEmpty(wellKnown))
return null;
return wellKnown;
}
}

View File

@@ -0,0 +1,39 @@
using MareSynchronos.MareConfiguration.Models;
using MareSynchronos.Services.Mediator;
using Microsoft.AspNetCore.SignalR.Client;
namespace MareSynchronos.WebAPI.SignalR.Utils;
public class ForeverRetryPolicy : IRetryPolicy
{
private readonly MareMediator _mediator;
private bool _sentDisconnected = false;
public ForeverRetryPolicy(MareMediator mediator)
{
_mediator = mediator;
}
public TimeSpan? NextRetryDelay(RetryContext retryContext)
{
TimeSpan timeToWait = TimeSpan.FromSeconds(new Random().Next(10, 20));
if (retryContext.PreviousRetryCount == 0)
{
_sentDisconnected = false;
timeToWait = TimeSpan.FromSeconds(3);
}
else if (retryContext.PreviousRetryCount == 1) timeToWait = TimeSpan.FromSeconds(5);
else if (retryContext.PreviousRetryCount == 2) timeToWait = TimeSpan.FromSeconds(10);
else
{
if (!_sentDisconnected)
{
_mediator.Publish(new NotificationMessage("Connection lost", "Connection lost to server", NotificationType.Warning, TimeSpan.FromSeconds(10)));
_mediator.Publish(new DisconnectedMessage());
}
_sentDisconnected = true;
}
return timeToWait;
}
}

View File

@@ -0,0 +1,16 @@
namespace MareSynchronos.WebAPI.SignalR.Utils;
public enum ServerState
{
Offline,
Connecting,
Reconnecting,
Disconnecting,
Disconnected,
Connected,
Unauthorized,
VersionMisMatch,
RateLimited,
NoSecretKey,
MultiChara,
}