From 810c9fb3e77c0891b0eb328059761ca9bac2d4b3 Mon Sep 17 00:00:00 2001 From: Madpeterz <57852827+Madpeterz@users.noreply.github.com> Date: Sun, 4 Apr 2021 22:48:48 +0100 Subject: [PATCH] Correct bugs --- BetterSecondbot/BetterSecondbot.cs | 11 ++- Core/HttpService/HttpAsService.cs | 20 +++-- Core/bottypes/CommandsBot.cs | 127 +++++++++++++++++++++-------- 3 files changed, 117 insertions(+), 41 deletions(-) diff --git a/BetterSecondbot/BetterSecondbot.cs b/BetterSecondbot/BetterSecondbot.cs index 3b66310..39542de 100644 --- a/BetterSecondbot/BetterSecondbot.cs +++ b/BetterSecondbot/BetterSecondbot.cs @@ -11,7 +11,9 @@ namespace BetterSecondBot static class Program { static void Main(string[] args) - { + { + AppDomain currentDomain = AppDomain.CurrentDomain; + currentDomain.UnhandledException += new UnhandledExceptionEventHandler(PanicMode); if (helpers.notempty(Environment.GetEnvironmentVariable("Basic_BotUserName")) == true) { new CliDocker(); @@ -22,6 +24,13 @@ namespace BetterSecondBot } LogFormater.Status("- Exiting in 5 secs -"); Thread.Sleep(5000); + } + + static void PanicMode(object sender, UnhandledExceptionEventArgs args) + { + Exception e = (Exception)args.ExceptionObject; + Console.WriteLine("PanicMode caught : " + e.Message); + Console.WriteLine("Runtime terminating: {0}", args.IsTerminating); } public static class AssemblyInfo diff --git a/Core/HttpService/HttpAsService.cs b/Core/HttpService/HttpAsService.cs index b9b525e..e1aee8c 100644 --- a/Core/HttpService/HttpAsService.cs +++ b/Core/HttpService/HttpAsService.cs @@ -407,13 +407,21 @@ namespace BetterSecondBot.HttpService public IPAddress handleGetClientIP() { - if(disableIPchecks == true) + try { - return IPAddress.Parse("127.0.0.1"); + if (disableIPchecks == true) + { + return IPAddress.Parse("127.0.0.1"); + } + else + { + return HttpContext.Request.RemoteEndPoint.Address; + } } - else + catch (Exception e) { - return HttpContext.Request.RemoteEndPoint.Address; + Console.WriteLine("Error getting client ip"); + return IPAddress.Parse("0.0.0.0"); } } @@ -486,7 +494,7 @@ namespace BetterSecondBot.HttpService protected Object BasicReply(string input, string command) { - return BasicReply(input, command); + return BasicReply(input, command, new string[] { }); } protected Object BasicReply(string input,string command, string[] args) @@ -503,7 +511,7 @@ namespace BetterSecondBot.HttpService protected Object Failure(string input, string command) { - return Failure(input, command); + return Failure(input, command, new string[] { }); } diff --git a/Core/bottypes/CommandsBot.cs b/Core/bottypes/CommandsBot.cs index f5e5d2e..74d9144 100644 --- a/Core/bottypes/CommandsBot.cs +++ b/Core/bottypes/CommandsBot.cs @@ -279,8 +279,16 @@ namespace BetterSecondBot.bottypes } string outputto = "none"; List bigbits = message.Split(new[] { "#|#" }, StringSplitOptions.RemoveEmptyEntries).ToList(); + if(bigbits.Count == 0) + { + bigbits.Add(""); + } List bits = bigbits[0].Split(new[] { "|||" }, StringSplitOptions.RemoveEmptyEntries).ToList(); - if (bits.Count == 1) + if(bits.Count < 1) + { + bits.Add(""); + } + if (bits.Count < 2) { bits.Add(""); } @@ -293,7 +301,17 @@ namespace BetterSecondBot.bottypes { frommaster = true; } - CoreCommandLib(sender_uuid, frommaster, bits[0], bits[1].Split("~#~", StringSplitOptions.RemoveEmptyEntries), signing_code, outputto); + try + { + string command = bits[0]; + string[] args = bits[1].Split("~#~", StringSplitOptions.RemoveEmptyEntries); + CoreCommandLib(sender_uuid, frommaster, command, args, signing_code, outputto); + + } + catch (Exception e) + { + LogFormater.Crit("[CoreCommandLib] exploded: " + e.Message + ""); + } } } @@ -305,17 +323,45 @@ namespace BetterSecondBot.bottypes string ott = Tokens.OneTimeToken(); WebApiControllerWithTokens Endpoint = commandEndpoints[endpointcommandmap[command]]; MethodInfo theMethod = Endpoint.GetType().GetMethod(command); - List argsList = args.ToList(); - argsList.Add(ott); - string reply = "Inconnect number of args expected: "+ theMethod.GetParameters().Count().ToString()+" but got: "+ argsList.Count.ToString(); - bool status = false; - if (argsList.Count == theMethod.GetParameters().Count()) + if (theMethod != null) { - status = true; - object processed = theMethod.Invoke(Endpoint, argsList.ToArray()); - reply = JsonConvert.SerializeObject(processed); + List argsList = args.ToList(); + argsList.Add(ott); + string reply = "Inconnect number of args expected: " + theMethod.GetParameters().Count().ToString() + " but got: " + argsList.Count.ToString(); + bool status = false; + if (argsList.Count == theMethod.GetParameters().Count()) + { + status = true; + try + { + if (Endpoint == null) + { + return new KeyValuePair(false, "Endpoint is null"); + } + if (argsList.Count == 0) + { + return new KeyValuePair(false, "Zero args at final check require at min 1"); + } + object[] argsWorker = argsList.ToArray(); + object processed = theMethod.Invoke(Endpoint, argsWorker); + reply = "Error"; + if(processed != null) + { + reply = JsonConvert.SerializeObject(processed); + } + return new KeyValuePair(status, reply); + } + catch (Exception e) + { + return new KeyValuePair(false, e.Message); + } + } + return new KeyValuePair(status, reply); + } + else + { + return new KeyValuePair(false, "theMethod is null"); } - return new KeyValuePair(status, reply); } catch (Exception e) { @@ -349,37 +395,50 @@ namespace BetterSecondBot.bottypes { return CallAPI(command, args, replyvia, false); } + + protected string customCommand(string command, string[] args, string replyvia, bool customcommand) + { + if (customcommand == true) + { + SmartCommandReply(false, replyvia, "Custom command lockout", command); + return "{ status: \"false\", message: \"Custom command lockout\" }"; + } + foreach (string A in custom_commands[command]) + { + List command_args_split = A.Split(new[] { "|||" }, StringSplitOptions.RemoveEmptyEntries).ToList(); + int loop = 1; + string args_passed = ""; + if (command_args_split.Count() == 2) + { + args_passed = command_args_split[1]; + while ((loop <= 5) && (loop <= args.Length)) + { + args_passed = args_passed.Replace("[C_ARG_" + loop.ToString() + "]", args[loop - 1]); + loop++; + } + } + CallAPI(command_args_split[0], args_passed.Split("~#~"), "None", true); + } + return "{ status: \"true\", message: \"mixed reply custom command\" }"; + } public string CallAPI(string command, string[] args, string replyvia, bool customcommand) { - if(commandnameLowerToReal.ContainsKey(command.ToLowerInvariant()) == false) + if(command == null) + { + return "No command"; + } + if (replyvia == null) + { + replyvia = "none"; + } + if (commandnameLowerToReal.ContainsKey(command.ToLowerInvariant()) == false) { if (custom_commands.ContainsKey(command) == false) { SmartCommandReply(false, replyvia, "Unknown command", command); return "{ status: \"false\", message: \"Unknown\" }"; } - if(customcommand == true) - { - SmartCommandReply(false, replyvia, "Custom command lockout", command); - return "{ status: \"false\", message: \"Custom command lockout\" }"; - } - foreach (string A in custom_commands[command]) - { - List command_args_split = A.Split(new[] { "|||" }, StringSplitOptions.RemoveEmptyEntries).ToList(); - int loop = 1; - string args_passed = ""; - if (command_args_split.Count() == 2) - { - args_passed = command_args_split[1]; - while ((loop <= 5) && (loop <= args.Length)) - { - args_passed = args_passed.Replace("[C_ARG_" + loop.ToString() + "]", args[loop - 1]); - loop++; - } - } - CallAPI(command_args_split[0], args_passed.Split("~#~"),"None",true); - } - return "{ status: \"true\", message: \"mixed reply custom command\" }"; + customCommand(command, args, replyvia, customcommand); } KeyValuePair statusreply = callAPIcommand(command, args); SmartCommandReply(statusreply.Key, replyvia, statusreply.Value, command);