add support for /json endpoint to http

This commit is contained in:
Madpeter 2025-07-09 22:42:15 +01:00
parent 7179fbde47
commit 68d1cd08e0
3 changed files with 33 additions and 2 deletions

View file

@ -81,6 +81,7 @@ ENV basic_Username='' \
homebound_ReturnToHomeSimAfterMins=5 \ homebound_ReturnToHomeSimAfterMins=5 \
http_Enabled='false' \ http_Enabled='false' \
http_Port=80 \ http_Port=80 \
http_DisableCommandValidation='false' \
interaction_Enabled='true' \ interaction_Enabled='true' \
interaction_AcceptTeleports='false' \ interaction_AcceptTeleports='false' \
interaction_AcceptGroupInvites='false' \ interaction_AcceptGroupInvites='false' \

View file

@ -11,12 +11,18 @@ namespace SecondBotEvents.Config
filename = "http"; filename = "http";
settings.Add("Enabled"); settings.Add("Enabled");
settings.Add("HideStatusOutput"); settings.Add("HideStatusOutput");
settings.Add("DisableCommandValidation");
} }
public bool GetEnabled() public bool GetEnabled()
{ {
return ReadSettingAsBool("Enabled", true); return ReadSettingAsBool("Enabled", true);
} }
public bool GetDisableCommandValidation()
{
return ReadSettingAsBool("DisableCommandValidation", false);
}
} }
} }

View file

@ -15,13 +15,14 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities;
using Swan;
namespace SecondBotEvents.Services namespace SecondBotEvents.Services
{ {
public class HttpService : BotServices public class HttpService : BotServices
{ {
protected WebServer HTTPendpoint = null; protected WebServer HTTPendpoint = null;
protected new HttpConfig myConfig; public new HttpConfig myConfig;
protected bool acceptBotCommands = false; protected bool acceptBotCommands = false;
public HttpService(EventsSecondBot setMaster) : base(setMaster) public HttpService(EventsSecondBot setMaster) : base(setMaster)
{ {
@ -133,11 +134,34 @@ namespace SecondBotEvents.Services
.WithMode(HttpListenerMode.EmbedIO)) .WithMode(HttpListenerMode.EmbedIO))
.WithCors() .WithCors()
.WithWebApi("/api", SerializationCallback, m => m.WithController(() => new HttpWebBot(master))) .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" })) .WithModule(new ActionModule("/", HttpVerbs.Any, ctx => ctx.SendDataAsync(new { Message = "Error" }))
); );
HTTPendpoint.RunAsync(); HTTPendpoint.RunAsync();
LogFormater.Info("HTTP service [Enabled] on port 80"); 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) public static async Task SerializationCallback(IHttpContext context, object? data)
{ {
Validate.NotNull(nameof(context), context).Response.ContentType = MimeType.Json; Validate.NotNull(nameof(context), context).Response.ContentType = MimeType.Json;
@ -356,7 +380,7 @@ namespace SecondBotEvents.Services
SignedCommand C = new( SignedCommand C = new(
master.CommandsService,"http", master.CommandsService,"http",
commandName, signing, myArgs, cmdUnixtime, null,true, master.CommandsService.myConfig.GetTimeWindowSecs(), 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) if(C.accepted == false)
{ {
return "Command request rejected"; return "Command request rejected";