Files
Ragon/Ragon.Server/Sources/Handler/AuthorizationOperation.cs
T

121 lines
3.8 KiB
C#
Raw Normal View History

2023-03-06 10:06:43 +04:00
/*
2024-05-19 12:26:42 +03:00
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
2023-03-06 10:06:43 +04:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Ragon.Protocol;
2023-10-11 19:37:50 +03:00
using Ragon.Server.IO;
2023-04-09 10:52:18 +04:00
using Ragon.Server.Lobby;
using Ragon.Server.Logging;
using Ragon.Server.Plugin;
2023-03-06 10:06:43 +04:00
namespace Ragon.Server.Handler
2023-03-06 10:06:43 +04:00
{
public sealed class AuthorizationOperation : BaseOperation
2023-03-06 10:06:43 +04:00
{
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(AuthorizationOperation));
private readonly IServerPlugin _serverPlugin;
2024-05-19 11:28:36 +03:00
private readonly IRagonServer _server;
private readonly RagonContextObserver _observer;
private readonly RagonServerConfiguration _configuration;
private readonly RagonBuffer _writer;
2023-04-09 10:52:18 +04:00
public AuthorizationOperation(RagonBuffer reader,
RagonBuffer writer,
2024-05-19 11:28:36 +03:00
IRagonServer server,
IServerPlugin serverPlugin,
RagonContextObserver observer,
RagonServerConfiguration configuration) : base(reader, writer)
2023-04-09 10:52:18 +04:00
{
_serverPlugin = serverPlugin;
_configuration = configuration;
_observer = observer;
_writer = writer;
2024-05-19 11:28:36 +03:00
_server = server;
2023-03-06 10:06:43 +04:00
}
2023-07-01 07:47:57 +03:00
public override void Handle(RagonContext context, NetworkChannel channel)
2023-04-09 10:52:18 +04:00
{
if (context.ConnectionStatus == ConnectionStatus.Authorized)
{
_logger.Warning("Player already authorized!");
2023-04-09 10:52:18 +04:00
return;
}
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)
{
2024-05-19 11:28:36 +03:00
var authorizeViaPlugin = _serverPlugin.OnAuthorize(new ConnectionRequest(_server, context.Connection.Id, payload));
if (authorizeViaPlugin)
return;
2024-05-19 11:28:36 +03:00
var id = Guid.NewGuid().ToString();
Approve(context, new ConnectionResponse(id, name, payload));
}
else
{
2024-07-21 09:46:01 +03:00
_logger.Warning($"Invalid key for connection {context.Connection.Id}");
Reject(context);
}
2023-04-09 10:52:18 +04:00
}
2024-05-19 11:28:36 +03:00
public void Approve(RagonContext context, ConnectionResponse result)
2023-04-09 10:52:18 +04:00
{
2024-05-19 11:28:36 +03:00
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, result.Id, result.Name, result.Payload);
context.SetPlayer(lobbyPlayer);
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);
2024-07-21 09:46:01 +03:00
_logger.Trace($"Approved {context.Connection.Id} as {playerId}|{context.LobbyPlayer.Name}");
2023-04-09 10:52:18 +04:00
}
2023-03-06 10:06:43 +04:00
public void Reject(RagonContext context)
{
_writer.Clear();
_writer.WriteOperation(RagonOperation.AUTHORIZED_FAILED);
2023-04-09 10:52:18 +04:00
var sendData = _writer.ToArray();
2023-10-07 19:30:52 +03:00
context.Connection.Reliable.Send(sendData);
context.Connection.Close();
2023-10-07 19:30:52 +03:00
2024-07-21 09:46:01 +03:00
_logger.Trace($"Rejected Connectin:{context.Connection.Id}");
}
}
2023-03-06 10:06:43 +04:00
}