refactoring: removed external dependencies from server, removed webhooks from server

This commit is contained in:
2024-05-19 08:54:49 +03:00
parent b84538b238
commit e9dc45265a
53 changed files with 642 additions and 1049 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,105 +14,103 @@
* limitations under the License.
*/
using NLog;
using Ragon.Protocol;
using Ragon.Server.IO;
using Ragon.Server.Lobby;
using Ragon.Server.Plugin.Web;
using Ragon.Server.Logging;
using Ragon.Server.Plugin;
namespace Ragon.Server.Handler;
public sealed class AuthorizationOperation: BaseOperation
namespace Ragon.Server.Handler
{
private Logger _logger = LogManager.GetCurrentClassLogger();
private readonly RagonWebHookPlugin _webhook;
private readonly RagonContextObserver _observer;
private readonly RagonBuffer _writer;
private readonly RagonServerConfiguration _configuration;
public AuthorizationOperation(
RagonBuffer reader,
RagonBuffer writer,
RagonWebHookPlugin webhook,
RagonContextObserver observer,
RagonServerConfiguration configuration): base(reader, writer)
public sealed class AuthorizationOperation : BaseOperation
{
_webhook = webhook;
_observer = observer;
_writer = writer;
_configuration = configuration;
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
if (context.ConnectionStatus == ConnectionStatus.Authorized)
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(AuthorizationOperation));
private readonly IServerPlugin _serverPlugin;
private readonly RagonContextObserver _observer;
private readonly RagonServerConfiguration _configuration;
private readonly RagonBuffer _writer;
public AuthorizationOperation(RagonBuffer reader,
RagonBuffer writer,
IServerPlugin serverPlugin,
RagonContextObserver observer,
RagonServerConfiguration configuration) : base(reader, writer)
{
_logger.Warn("Player already authorized!");
return;
_serverPlugin = serverPlugin;
_configuration = configuration;
_observer = observer;
_writer = writer;
}
if (context.ConnectionStatus == ConnectionStatus.InProcess)
public override void Handle(RagonContext context, NetworkChannel channel)
{
_logger.Warn("Player already request authorization!");
return;
}
var configuration = _configuration;
var key = Reader.ReadString();
var name = Reader.ReadString();
var payload = Reader.ReadString();
if (key == configuration.ServerKey)
{
if (_webhook.RequestAuthorization(context, payload))
if (context.ConnectionStatus == ConnectionStatus.Authorized)
{
_logger.Warning("Player already authorized!");
return;
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, Guid.NewGuid().ToString(), name, payload);
context.SetPlayer(lobbyPlayer);
Approve(context);
}
if (context.ConnectionStatus == ConnectionStatus.InProcess)
{
_logger.Warning("Player already request authorization!");
return;
}
var configuration = _configuration;
var key = Reader.ReadString();
var name = Reader.ReadString();
var payload = Reader.ReadString();
if (key == configuration.ServerKey)
{
var authorizeViaWebHook = _serverPlugin.OnAuthorize(new ConnectionRequest() { PeerID = context.Connection.Id });
if (authorizeViaWebHook)
return;
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, Guid.NewGuid().ToString(), name, payload);
context.SetPlayer(lobbyPlayer);
Approve(context);
}
else
{
Reject(context);
}
}
else
public void Approve(RagonContext context)
{
Reject(context);
context.ConnectionStatus = ConnectionStatus.Authorized;
_observer.OnAuthorized(context);
var playerId = context.LobbyPlayer.Id;
var playerName = context.LobbyPlayer.Name;
var playerPayload = context.LobbyPlayer.Payload;
_writer.Clear();
_writer.WriteOperation(RagonOperation.AUTHORIZED_SUCCESS);
_writer.WriteString(playerId);
_writer.WriteString(playerName);
_writer.WriteString(playerPayload);
var sendData = _writer.ToArray();
context.Connection.Reliable.Send(sendData);
_logger.Trace($"Connection {context.Connection.Id} as {playerId}|{context.LobbyPlayer.Name} authorized");
}
public void Reject(RagonContext context)
{
_writer.Clear();
_writer.WriteOperation(RagonOperation.AUTHORIZED_FAILED);
var sendData = _writer.ToArray();
context.Connection.Reliable.Send(sendData);
context.Connection.Close();
_logger.Trace($"Connection {context.Connection.Id}");
}
}
public void Approve(RagonContext context)
{
context.ConnectionStatus = ConnectionStatus.Authorized;
_observer.OnAuthorized(context);
var playerId = context.LobbyPlayer.Id;
var playerName = context.LobbyPlayer.Name;
var playerPayload = context.LobbyPlayer.Payload;
_writer.Clear();
_writer.WriteOperation(RagonOperation.AUTHORIZED_SUCCESS);
_writer.WriteString(playerId);
_writer.WriteString(playerName);
_writer.WriteString(playerPayload);
var sendData = _writer.ToArray();
context.Connection.Reliable.Send(sendData);
_logger.Trace($"Connection {context.Connection.Id} as {playerId}|{context.LobbyPlayer.Name} authorized");
}
public void Reject(RagonContext context)
{
_writer.Clear();
_writer.WriteOperation(RagonOperation.AUTHORIZED_FAILED);
var sendData = _writer.ToArray();
context.Connection.Reliable.Send(sendData);
context.Connection.Close();
_logger.Trace($"Connection {context.Connection.Id}");
}
}