diff --git a/Dockerfile b/Dockerfile index 0205e19..2e037f9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -81,6 +81,7 @@ ENV basic_Username='' \ homebound_ReturnToHomeSimAfterMins=5 \ http_Enabled='false' \ http_Port=80 \ + http_DisableCommandValidation='false' \ interaction_Enabled='true' \ interaction_AcceptTeleports='false' \ interaction_AcceptGroupInvites='false' \ diff --git a/SecondBotEvents/Config/HttpConfig.cs b/SecondBotEvents/Config/HttpConfig.cs index 0843c4e..297b75b 100644 --- a/SecondBotEvents/Config/HttpConfig.cs +++ b/SecondBotEvents/Config/HttpConfig.cs @@ -11,12 +11,18 @@ namespace SecondBotEvents.Config filename = "http"; settings.Add("Enabled"); settings.Add("HideStatusOutput"); + settings.Add("DisableCommandValidation"); } public bool GetEnabled() { return ReadSettingAsBool("Enabled", true); } + + public bool GetDisableCommandValidation() + { + return ReadSettingAsBool("DisableCommandValidation", false); + } } } \ No newline at end of file diff --git a/SecondBotEvents/Services/HttpService.cs b/SecondBotEvents/Services/HttpService.cs index 5c121a0..2d8ddab 100644 --- a/SecondBotEvents/Services/HttpService.cs +++ b/SecondBotEvents/Services/HttpService.cs @@ -15,13 +15,14 @@ using System.Threading; using System.Threading.Tasks; using Newtonsoft.Json; using Org.BouncyCastle.Utilities; +using Swan; namespace SecondBotEvents.Services { public class HttpService : BotServices { protected WebServer HTTPendpoint = null; - protected new HttpConfig myConfig; + public new HttpConfig myConfig; protected bool acceptBotCommands = false; public HttpService(EventsSecondBot setMaster) : base(setMaster) { @@ -133,11 +134,34 @@ namespace SecondBotEvents.Services .WithMode(HttpListenerMode.EmbedIO)) .WithCors() .WithWebApi("/api", SerializationCallback, m => m.WithController(() => new HttpWebBot(master))) + .WithModule(new ActionModule("/json", HttpVerbs.Post, ctx => ctx.SendDataAsync(JsonInput(ctx)))) .WithModule(new ActionModule("/", HttpVerbs.Any, ctx => ctx.SendDataAsync(new { Message = "Error" })) ); HTTPendpoint.RunAsync(); LogFormater.Info("HTTP service [Enabled] on port 80"); } + + protected string JsonInput(IHttpContext httpContext) + { + if (master.HttpService.GetAcceptBotCommands() == false) + { + return "No bot connected yet"; + } + string body = httpContext.GetRequestBodyAsStringAsync().Await(); + if(body == null || body.Length < 1) + { + return "No body sent"; + } + SignedCommand C = new(master.CommandsService, "http", body, !master.HttpService.myConfig.GetDisableCommandValidation(), + master.CommandsService.myConfig.GetEnforceTimeWindow(), + master.CommandsService.myConfig.GetTimeWindowSecs(), + master.CommandsService.myConfig.GetSharedSecret()); + if (C.accepted == false) + { + return "Command request rejected"; + } + return JsonConvert.SerializeObject(master.CommandsService.RunCommand("HTTP", C)); + } public static async Task SerializationCallback(IHttpContext context, object? data) { Validate.NotNull(nameof(context), context).Response.ContentType = MimeType.Json; @@ -356,7 +380,7 @@ namespace SecondBotEvents.Services SignedCommand C = new( master.CommandsService,"http", commandName, signing, myArgs, cmdUnixtime, null,true, master.CommandsService.myConfig.GetTimeWindowSecs(), - master.CommandsService.myConfig.GetSharedSecret()); + master.CommandsService.myConfig.GetSharedSecret(), !master.HttpService.myConfig.GetDisableCommandValidation()); if(C.accepted == false) { return "Command request rejected";