Files

118 lines
3.3 KiB
C#
Raw Permalink Normal View History

2023-03-06 10:06:43 +04:00
/*
* 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.
* 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 NLog;
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;
2023-04-09 11:06:52 +04:00
using Ragon.Server.Plugin.Web;
2023-03-06 10:06:43 +04:00
2023-04-09 10:52:18 +04:00
namespace Ragon.Server.Handler;
2023-03-06 10:06:43 +04:00
2023-10-07 19:30:52 +03:00
public sealed class AuthorizationOperation: BaseOperation
2023-03-06 10:06:43 +04:00
{
private Logger _logger = LogManager.GetCurrentClassLogger();
2023-10-07 19:30:52 +03:00
private readonly RagonWebHookPlugin _webhook;
private readonly RagonContextObserver _observer;
2023-04-09 10:52:18 +04:00
private readonly RagonBuffer _writer;
private readonly RagonServerConfiguration _configuration;
2023-04-09 10:52:18 +04:00
2023-07-01 07:47:57 +03:00
public AuthorizationOperation(
2023-10-07 19:30:52 +03:00
RagonBuffer reader,
RagonBuffer writer,
RagonWebHookPlugin webhook,
RagonContextObserver observer,
RagonServerConfiguration configuration): base(reader, writer)
2023-04-09 10:52:18 +04:00
{
2023-10-07 19:30:52 +03:00
_webhook = webhook;
_observer = observer;
2023-04-09 10:52:18 +04:00
_writer = writer;
_configuration = configuration;
2023-04-09 10:52:18 +04:00
}
2023-03-06 10:06:43 +04:00
2023-10-11 19:37:50 +03:00
public override void Handle(RagonContext context, NetworkChannel channel)
2023-03-06 10:06:43 +04:00
{
2023-04-09 10:52:18 +04:00
if (context.ConnectionStatus == ConnectionStatus.Authorized)
2023-03-06 10:06:43 +04:00
{
2023-04-09 10:52:18 +04:00
_logger.Warn("Player already authorized!");
return;
}
if (context.ConnectionStatus == ConnectionStatus.InProcess)
{
_logger.Warn("Player already request authorization!");
2023-03-06 10:06:43 +04:00
return;
}
2023-07-01 07:47:57 +03:00
var configuration = _configuration;
2023-10-07 19:30:52 +03:00
var key = Reader.ReadString();
var name = Reader.ReadString();
var payload = Reader.ReadString();
2023-04-09 10:52:18 +04:00
2023-07-01 07:47:57 +03:00
if (key == configuration.ServerKey)
2023-04-09 10:52:18 +04:00
{
2023-10-07 19:30:52 +03:00
if (_webhook.RequestAuthorization(context, name, payload))
2023-04-09 10:52:18 +04:00
return;
2023-04-13 20:42:05 +04:00
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, Guid.NewGuid().ToString(), name, payload);
2023-04-09 10:52:18 +04:00
context.SetPlayer(lobbyPlayer);
Approve(context);
}
else
{
Reject(context);
}
}
2023-03-06 10:06:43 +04:00
2023-04-09 10:52:18 +04:00
public void Approve(RagonContext context)
{
context.ConnectionStatus = ConnectionStatus.Authorized;
2023-04-13 20:42:05 +04:00
2023-10-07 19:30:52 +03:00
_observer.OnAuthorized(context);
2023-04-13 20:42:05 +04:00
2023-03-06 10:06:43 +04:00
var playerId = context.LobbyPlayer.Id;
2023-04-09 10:52:18 +04:00
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);
2023-03-06 10:06:43 +04:00
2023-04-09 10:52:18 +04:00
var sendData = _writer.ToArray();
2023-03-06 10:06:43 +04:00
context.Connection.Reliable.Send(sendData);
2023-04-09 10:52:18 +04:00
_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}");
2023-03-06 10:06:43 +04:00
}
2023-10-07 19:30:52 +03:00
2023-03-06 10:06:43 +04:00
}