[Tweak] Panda web server POST upgrade (#628)

* [Feat] Panda web server POST upgrade

* Move token in comms
This commit is contained in:
HitPanda
2024-01-21 14:04:38 +03:00
committed by Aviu00
parent 39e7e2e0fa
commit 9de8ef540a
14 changed files with 106 additions and 33 deletions

View File

@@ -125,7 +125,7 @@ public sealed class BanCommand : LocalizedCommands
Rid = EntitySystem.Get<GameTicker>().RoundId, Rid = EntitySystem.Get<GameTicker>().RoundId,
BanId = banId BanId = banId
}; };
_pandaWeb.SendBotMessage(utkaBanned); _pandaWeb.SendBotPostMessage(utkaBanned);
_entMan.EventBus.RaiseEvent(EventSource.Local, utkaBanned); _entMan.EventBus.RaiseEvent(EventSource.Local, utkaBanned);
//WD end //WD end
} }

View File

@@ -529,7 +529,7 @@ public sealed class BanManager : IBanManager, IPostInjectInit
BanId = banId BanId = banId
}; };
_pandaWeb.SendBotMessage(utkaBanned); _pandaWeb.SendBotPostMessage(utkaBanned);
_entMan.EventBus.RaiseEvent(EventSource.Local, utkaBanned); _entMan.EventBus.RaiseEvent(EventSource.Local, utkaBanned);
} }

View File

@@ -658,7 +658,7 @@ namespace Content.Server.Administration.Systems
Entity = entity Entity = entity
}; };
_pandaWeb.SendBotMessage(utkaAhelpEvent); _pandaWeb.SendBotPostMessage(utkaAhelpEvent);
} }
//WD-EDIT //WD-EDIT
} }

View File

@@ -331,14 +331,14 @@ namespace Content.Server.Chat.Managers
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"OOC from {player:Player}: {message}"); _adminLogger.Add(LogType.Chat, LogImpact.Low, $"OOC from {player:Player}: {message}");
//WD-EDIT //WD-EDIT
var toUtkaMessage = new UtkaChatEventMessage() var toUtkaMessage = new UtkaChatMessageEvent()
{ {
Command = "ooc", Command = "ooc",
Ckey = player.Name, Ckey = player.Name,
Message = message, Message = message,
}; };
_pandaWeb.SendBotMessage(toUtkaMessage); _pandaWeb.SendBotPostMessage(toUtkaMessage);
//WD-EDIT //WD-EDIT
} }
@@ -374,14 +374,14 @@ namespace Content.Server.Chat.Managers
_adminLogger.Add(LogType.Chat, $"Admin chat from {player:Player}: {message}"); _adminLogger.Add(LogType.Chat, $"Admin chat from {player:Player}: {message}");
//WD-EDIT //WD-EDIT
var asayEventMessage = new UtkaChatEventMessage() var asayEventMessage = new UtkaChatMessageEvent()
{ {
Command = "asay", Command = "asay",
Ckey = player.Name, Ckey = player.Name,
Message = message Message = message
}; };
_pandaWeb.SendBotMessage(asayEventMessage); _pandaWeb.SendBotPostMessage(asayEventMessage);
//WD-EDIT //WD-EDIT
} }

View File

@@ -648,7 +648,7 @@ public sealed partial class ChatSystem : SharedChatSystem
CharacterName = MetaData(source).EntityName CharacterName = MetaData(source).EntityName
}; };
_pandaWeb.SendBotMessage(utkaEmoteEvent); _pandaWeb.SendBotPostMessage(utkaEmoteEvent);
//WD-EDIT //WD-EDIT
} }

View File

@@ -567,7 +567,7 @@ namespace Content.Server.GameTicking
Message = status Message = status
}; };
_pandaWeb.SendBotMessage(utkaRoundStatusEvent); _pandaWeb.SendBotPostMessage(utkaRoundStatusEvent);
} }

View File

@@ -307,7 +307,7 @@ namespace Content.Server.RoundEnd
Message = status Message = status
}; };
_pandaWeb.SendBotMessage(utkaRoundStatusEvent); _pandaWeb.SendBotPostMessage(utkaRoundStatusEvent);
} }
//WD-EDIT //WD-EDIT

View File

@@ -584,6 +584,6 @@ public sealed partial class EmergencyShuttleSystem : EntitySystem
Message = status Message = status
}; };
_pandaWeb.SendBotMessage(utkaRoundStatusEvent); _pandaWeb.SendBotPostMessage(utkaRoundStatusEvent);
} }
} }

View File

@@ -131,7 +131,7 @@ public sealed class PandaBanCommand : IPandaCommand
BanId = banId BanId = banId
}; };
_pandaWeb.SendBotMessage(utkaBanned); _pandaWeb.SendBotPostMessage(utkaBanned);
_entMan.EventBus.RaiseEvent(EventSource.Local, utkaBanned); _entMan.EventBus.RaiseEvent(EventSource.Local, utkaBanned);
} }

View File

@@ -17,6 +17,7 @@ public interface IPandaStatusHandlerContext
Stream RequestBody { get; } Stream RequestBody { get; }
Uri Url { get; } Uri Url { get; }
bool IsGetLike { get; } bool IsGetLike { get; }
bool IsPostLike { get; }
IReadOnlyDictionary<string, StringValues> RequestHeaders { get; } IReadOnlyDictionary<string, StringValues> RequestHeaders { get; }
IDictionary<string, string> ResponseHeaders { get; } IDictionary<string, string> ResponseHeaders { get; }

View File

@@ -8,7 +8,13 @@ public class PandaBaseMessage
public virtual string? Command { get; set; } public virtual string? Command { get; set; }
} }
public class UtkaOOCRequest : PandaBaseMessage public class PandaBaseRequestEventMessage : PandaBaseMessage
{
[JsonPropertyName("token")]
public string? Token { get; set; }
}
public class UtkaOOCRequest : PandaBaseRequestEventMessage
{ {
[JsonPropertyName("command")] [JsonPropertyName("command")]
public override string? Command => "ooc"; public override string? Command => "ooc";
@@ -20,7 +26,7 @@ public class UtkaOOCRequest : PandaBaseMessage
public string? Message { get; set; } public string? Message { get; set; }
} }
public class UtkaAsayRequest : PandaBaseMessage public class UtkaAsayRequest : PandaBaseRequestEventMessage
{ {
[JsonPropertyName("command")] [JsonPropertyName("command")]
public override string? Command => "asay"; public override string? Command => "asay";
@@ -32,7 +38,7 @@ public class UtkaAsayRequest : PandaBaseMessage
public string? Message { get; set; } public string? Message { get; set; }
} }
public class UtkaPmRequest : PandaBaseMessage public class UtkaPmRequest : PandaBaseRequestEventMessage
{ {
[JsonPropertyName("command")] [JsonPropertyName("command")]
public override string? Command => "discordpm"; public override string? Command => "discordpm";
@@ -56,7 +62,7 @@ public class UtkaPmResponse : PandaBaseMessage
public bool? Message { get; set; } public bool? Message { get; set; }
} }
public class UtkaWhoRequest : PandaBaseMessage public class UtkaWhoRequest : PandaBaseRequestEventMessage
{ {
[JsonPropertyName("command")] [JsonPropertyName("command")]
public override string? Command => "who"; public override string? Command => "who";
@@ -71,7 +77,7 @@ public class UtkaWhoResponse : PandaBaseMessage
public List<string>? Players { get; set; } public List<string>? Players { get; set; }
} }
public class UtkaAdminWhoRequest : PandaBaseMessage public class UtkaAdminWhoRequest : PandaBaseRequestEventMessage
{ {
[JsonPropertyName("command")] [JsonPropertyName("command")]
public override string? Command => "adminwho"; public override string? Command => "adminwho";
@@ -86,7 +92,7 @@ public class UtkaAdminWhoResponse : PandaBaseMessage
public List<string>? Admins { get; set; } public List<string>? Admins { get; set; }
} }
public class UtkaStatusRequest : PandaBaseMessage public class UtkaStatusRequest : PandaBaseRequestEventMessage
{ {
[JsonPropertyName("command")] [JsonPropertyName("command")]
public override string? Command => "status"; public override string? Command => "status";
@@ -116,7 +122,7 @@ public class UtkaStatusResponse : PandaBaseMessage
public string? StationCode { get; set; } public string? StationCode { get; set; }
} }
public sealed class UtkaBanRequest : PandaBaseMessage public sealed class UtkaBanRequest : PandaBaseRequestEventMessage
{ {
[JsonPropertyName("command")] [JsonPropertyName("command")]
public override string? Command => "ban"; public override string? Command => "ban";
@@ -146,7 +152,7 @@ public sealed class UtkaBanResponse : PandaBaseMessage
public bool? Banned { get; set; } public bool? Banned { get; set; }
} }
public sealed class UtkaJobBanRequest : PandaBaseMessage public sealed class UtkaJobBanRequest : PandaBaseRequestEventMessage
{ {
public override string? Command => "jobban"; public override string? Command => "jobban";
@@ -178,7 +184,7 @@ public sealed class UtkaJobBanResponse : PandaBaseMessage
public bool? Banned { get; set; } public bool? Banned { get; set; }
} }
public sealed class UtkaRestartRoundRequest : PandaBaseMessage public sealed class UtkaRestartRoundRequest : PandaBaseRequestEventMessage
{ {
[JsonPropertyName("command")] [JsonPropertyName("command")]
public override string? Command => "restartround"; public override string? Command => "restartround";
@@ -193,7 +199,7 @@ public sealed class UtkaRestartRoundResponse : PandaBaseMessage
public bool? Restarted { get; set; } public bool? Restarted { get; set; }
} }
public sealed class UtkaUnbanRequest : PandaBaseMessage public sealed class UtkaUnbanRequest : PandaBaseRequestEventMessage
{ {
[JsonPropertyName("command")] [JsonPropertyName("command")]
public override string? Command => "unban"; public override string? Command => "unban";
@@ -214,7 +220,7 @@ public sealed class UtkaUnbanResponse : PandaBaseMessage
public bool? Unbanned { get; set; } public bool? Unbanned { get; set; }
} }
public sealed class UtkaUnJobBanRequest : PandaBaseMessage public sealed class UtkaUnJobBanRequest : PandaBaseRequestEventMessage
{ {
[JsonPropertyName("command")] [JsonPropertyName("command")]
public override string? Command => "unjobban"; public override string? Command => "unjobban";
@@ -235,7 +241,7 @@ public sealed class UtkaUnJobBanResponse : PandaBaseMessage
public bool? Unbanned { get; set; } public bool? Unbanned { get; set; }
} }
public sealed class UtkaBannedEvent : PandaBaseMessage public sealed class UtkaBannedEvent : PandaBaseRequestEventMessage
{ {
[JsonPropertyName("command")] [JsonPropertyName("command")]
public override string? Command => "banned"; public override string? Command => "banned";
@@ -265,7 +271,7 @@ public sealed class UtkaBannedEvent : PandaBaseMessage
public int? BanId { get; set; } public int? BanId { get; set; }
} }
public sealed class UtkaChatEventMessage : PandaBaseMessage public sealed class UtkaChatMessageEvent : PandaBaseRequestEventMessage
{ {
[JsonPropertyName("ckey")] [JsonPropertyName("ckey")]
public string? Ckey { get; set; } public string? Ckey { get; set; }
@@ -274,7 +280,7 @@ public sealed class UtkaChatEventMessage : PandaBaseMessage
public string? Message { get; set; } public string? Message { get; set; }
} }
public sealed class UtkaRoundStatusEvent : PandaBaseMessage public sealed class UtkaRoundStatusEvent : PandaBaseRequestEventMessage
{ {
[JsonPropertyName("command")] [JsonPropertyName("command")]
public override string? Command => "roundstatus"; public override string? Command => "roundstatus";
@@ -283,7 +289,7 @@ public sealed class UtkaRoundStatusEvent : PandaBaseMessage
public string? Message { get; set; } public string? Message { get; set; }
} }
public sealed class UtkaChatMeEvent : PandaBaseMessage public sealed class UtkaChatMeEvent : PandaBaseRequestEventMessage
{ {
[JsonPropertyName("command")] [JsonPropertyName("command")]
public override string? Command => "me"; public override string? Command => "me";
@@ -298,7 +304,7 @@ public sealed class UtkaChatMeEvent : PandaBaseMessage
public string? CharacterName { get; set; } public string? CharacterName { get; set; }
} }
public sealed class UtkaAhelpPmEvent : PandaBaseMessage public sealed class UtkaAhelpPmEvent : PandaBaseRequestEventMessage
{ {
[JsonPropertyName("command")] [JsonPropertyName("command")]
public override string? Command => "pm"; public override string? Command => "pm";

View File

@@ -44,12 +44,12 @@ public sealed partial class PandaStatusHost
private async Task<bool> HandleRequest(IPandaStatusHandlerContext context) private async Task<bool> HandleRequest(IPandaStatusHandlerContext context)
{ {
if (!context.IsGetLike || context.Url!.AbsolutePath != "/request") if (!context.IsPostLike || context.Url!.AbsolutePath != "/request")
{ {
return false; return false;
} }
if (!ValidateMessage(context.Url.Query, out var message) || message == null) if (!ValidatePostMessage(context.RequestBody, out var message) || message == null)
return false; return false;
ExecuteCommand(context, message); ExecuteCommand(context, message);

View File

@@ -162,9 +162,51 @@ public sealed partial class PandaStatusHost : IDisposable
return false; return false;
var collection = HttpUtility.ParseQueryString(message); var collection = HttpUtility.ParseQueryString(message);
#pragma warning disable CS8714
var json = JsonSerializer.Serialize(collection.AllKeys.ToDictionary(y => y!, y => collection[y])); var json = JsonSerializer.Serialize(collection.AllKeys.ToDictionary(y => y!, y => collection[y]));
#pragma warning restore CS8714 var jsonDocument = JsonDocument.Parse(json);
var root = jsonDocument.RootElement;
if (!root.TryGetProperty("token", out var token))
return false;
if (token.GetString() != _token)
return false;
if (!root.TryGetProperty("command", out var commandNameElement))
return false;
var commandName = commandNameElement.GetString();
if (commandName == null)
return false;
var pandaCommand = Commands.Values.FirstOrDefault(x => x.Name == commandName);
if (pandaCommand == null)
return false;
var messageType = pandaCommand.RequestMessageType;
try
{
baseMessage = JsonConvert.DeserializeObject(json, messageType) as PandaBaseMessage;
}
catch (Exception e)
{
return false;
}
return true;
}
private bool ValidatePostMessage(Stream message, out PandaBaseMessage? baseMessage)
{
baseMessage = null;
var reader = new StreamReader(message, Encoding.UTF8);
var task = Task.Run(async () => await reader.ReadToEndAsync());
_taskManager.BlockWaitOnTask(task);
var json = task.GetAwaiter().GetResult();
var jsonDocument = JsonDocument.Parse(json); var jsonDocument = JsonDocument.Parse(json);
var root = jsonDocument.RootElement; var root = jsonDocument.RootElement;
@@ -219,6 +261,7 @@ public sealed partial class PandaStatusHost : IDisposable
public Stream RequestBody => _context.Request.InputStream; public Stream RequestBody => _context.Request.InputStream;
public Uri Url => _context.Request.Url!; public Uri Url => _context.Request.Url!;
public bool IsGetLike => RequestMethod == HttpMethod.Head || RequestMethod == HttpMethod.Get; public bool IsGetLike => RequestMethod == HttpMethod.Head || RequestMethod == HttpMethod.Get;
public bool IsPostLike => RequestMethod == HttpMethod.Post;
public IReadOnlyDictionary<string, StringValues> RequestHeaders { get; } public IReadOnlyDictionary<string, StringValues> RequestHeaders { get; }
public bool KeepAlive public bool KeepAlive

View File

@@ -1,5 +1,6 @@
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Text;
using System.Web; using System.Web;
using Content.Shared.White; using Content.Shared.White;
using Newtonsoft.Json; using Newtonsoft.Json;
@@ -28,7 +29,7 @@ public sealed class PandaWebManager
_cfg.OnValueChanged(WhiteCVars.UtkaClientBind, uri => _utkaUri = uri, true); _cfg.OnValueChanged(WhiteCVars.UtkaClientBind, uri => _utkaUri = uri, true);
} }
public async void SendBotMessage(PandaBaseMessage message) public async void SendBotGetMessage(PandaBaseMessage message)
{ {
if (_utkaUri is null || _token is null) if (_utkaUri is null || _token is null)
return; return;
@@ -50,4 +51,26 @@ public sealed class PandaWebManager
return; return;
} }
} }
public async void SendBotPostMessage(PandaBaseRequestEventMessage message)
{
if (_utkaUri is null || _token is null)
return;
message.Token = _token;
var request = $"http://{_utkaUri}";
var json = JsonSerializer.Serialize(message, message.GetType());
HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");
try
{
await _httpClient.PostAsync(request, content);
}
catch (Exception e)
{
return;
}
}
} }