Add AuthLifetimeService

Refactor AuthManager
This commit is contained in:
2021-06-04 18:32:47 +02:00
parent adcde19346
commit 2b81f3e5ba
13 changed files with 281 additions and 58 deletions

View File

@@ -9,22 +9,25 @@ using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Akari.Prototype.Server.Services
{
public class TcpProviderService : BackgroundService
// TODO Process multiple clients simultaneously
public sealed class TcpProviderService : BackgroundService
{
public const int BufferLength = 4096;
public const int MaxDataLength = MaxTokenLength + 1 + MaxNameLength;
public const int MaxTokenLength = 24;
public const int ReceiveTimeout = 500_000;
public const int MaxNameLength = 200;
public const int ReceiveTimeout = 5_000;
private readonly ILogger<TcpProviderService> _logger;
private readonly TcpProviderOptions _options;
private readonly IHostApplicationLifetime _hostApplicationLifetime;
private readonly IFingerprintManager _fingerprintManager;
private Task _backgroundTask;
private TcpListener _listener;
public TcpProviderService(ILogger<TcpProviderService> logger, IOptions<TcpProviderOptions> options,
@@ -36,6 +39,16 @@ namespace Akari.Prototype.Server.Services
_fingerprintManager = fingerprintManager;
}
public override Task StartAsync(CancellationToken cancellationToken)
{
_listener = new TcpListener(IPAddress.Parse(_options.ListeningAddress), _options.Port);
_listener.Start();
_logger.LogInformation($"Now listening on: {_listener.LocalEndpoint}");
return Task.CompletedTask;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await TcpLoop(stoppingToken);
@@ -50,11 +63,6 @@ namespace Akari.Prototype.Server.Services
try
{
_listener = new TcpListener(IPAddress.Parse(_options.ListeningAddress), _options.Port);
_listener.Start();
_logger.LogInformation($"Now listening on: {_listener.LocalEndpoint}");
while (!cancellationToken.IsCancellationRequested)
{
_logger.LogDebug("Waiting for client...");
@@ -88,17 +96,14 @@ namespace Akari.Prototype.Server.Services
Environment.Exit(1);
}
finally
{
_listener?.Stop();
}
}
private async Task ReceiveAuth(TcpClient client, CancellationToken cancellationToken)
{
using var stream = client.GetStream();
var data = new Memory<byte>(new byte[MaxTokenLength]);
var buffer = new Memory<byte>(new byte[BufferLength]);
Memory<byte> data = new byte[MaxDataLength];
Memory<byte> buffer = new byte[BufferLength];
// Receive token
int position = 0;
@@ -106,12 +111,12 @@ namespace Akari.Prototype.Server.Services
var timeoutToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, new CancellationTokenSource(ReceiveTimeout).Token).Token;
_logger.LogDebug("Waiting for token...");
while (position < MaxTokenLength && (read = await stream.ReadAsync(buffer, timeoutToken)) != 0)
_logger.LogDebug("Waiting for data...");
while (position < MaxDataLength && (read = await stream.ReadAsync(buffer, timeoutToken)) != 0)
{
if (position + read > data.Length)
{
// Invalid token
// Invalid data
return;
}
@@ -120,9 +125,24 @@ namespace Akari.Prototype.Server.Services
position += read;
}
_logger.LogDebug($"Received token: {Convert.ToBase64String(data[..position].Span)}");
var text = Encoding.UTF8.GetString(data.Span);
var splitIndex = text.IndexOf('$');
await _fingerprintManager.VerifyToken(data[..position].ToArray());
_logger.LogDebug($"Received text: {text}");
if (cancellationToken.IsCancellationRequested)
{
return;
}
_fingerprintManager.VerifyFingerprint(text[..splitIndex], text[(splitIndex + 1)..]);
}
public override void Dispose()
{
_listener?.Stop();
base.Dispose();
}
}
}