diff --git a/BSBshared/Json/JsonConfigApi.cs b/BSBshared/Json/JsonConfigApi.cs index fcb307f..f92dd20 100644 --- a/BSBshared/Json/JsonConfigApi.cs +++ b/BSBshared/Json/JsonConfigApi.cs @@ -220,7 +220,7 @@ namespace BetterSecondBotShared.Json { ConsoleLog.Warn("Http disabled: Httpkey length must be 12 or more"); } - else if (jsonConfig.Httpport < 81) + else if (jsonConfig.Httpport < 80) { ConsoleLog.Warn("Http disabled: Httpport range below protected range - Given: (" + jsonConfig.Httpport + ")"); } diff --git a/BSBshared/Staic/Helpers.cs b/BSBshared/Staic/Helpers.cs index 18e66cb..2b1321c 100644 --- a/BSBshared/Staic/Helpers.cs +++ b/BSBshared/Staic/Helpers.cs @@ -1,8 +1,11 @@ using BetterSecondBotShared.Json; using BetterSecondBotShared.logs; +using Microsoft.Extensions.ObjectPool; using System; using System.Collections.Generic; +using System.IO; using System.Linq; +using System.Reflection; using System.Security.Cryptography; using System.Text; @@ -22,6 +25,54 @@ namespace BetterSecondBotShared.Static } public static class helpers { + // + + public static byte[] ReadResourceFileBinary(Assembly targetenv, string filename) + { + try + { + byte[] dataset; + string[] files = targetenv.GetManifestResourceNames(); + string resourceName = files.Single(str => str.EndsWith(filename)); + using (Stream stream = targetenv.GetManifestResourceStream(resourceName)) + using (BinaryReader br = new BinaryReader(stream)) + { + const int bufferSize = 4096; + using (var ms = new MemoryStream()) + { + byte[] buffer = new byte[bufferSize]; + int count; + while ((count = br.Read(buffer, 0, buffer.Length)) != 0) + ms.Write(buffer, 0, count); + dataset = ms.ToArray(); + } + } + return dataset; + } + catch (Exception e) + { + return Encoding.UTF8.GetBytes(e.Message); + } + } + public static string ReadResourceFile(Assembly targetenv,string filename) + { + try + { + string[] files = targetenv.GetManifestResourceNames(); + string resourceName = files.Single(str => str.EndsWith(filename)); + string result = ""; + using (Stream stream = targetenv.GetManifestResourceStream(resourceName)) + using (StreamReader reader = new StreamReader(stream)) + { + result = reader.ReadToEnd(); + } + return result; + } + catch (Exception e) + { + return e.Message; + } + } public static bool inrange(float current,float min,float max) { if(current <= max) diff --git a/CLI/BetterSecondBot.csproj b/CLI/BetterSecondBot.csproj index 36bb74d..a4a98b0 100644 --- a/CLI/BetterSecondBot.csproj +++ b/CLI/BetterSecondBot.csproj @@ -3,7 +3,7 @@ AnyCPU;x64 - + git describe --long --always @@ -26,6 +26,15 @@ linux-x64 + + + + + + + + + @@ -37,6 +46,15 @@ + + + + + + + + + diff --git a/CLI/HttpServer/SecondBotHttpServer.cs b/CLI/HttpServer/SecondBotHttpServer.cs index dd8bbf1..9870093 100644 --- a/CLI/HttpServer/SecondBotHttpServer.cs +++ b/CLI/HttpServer/SecondBotHttpServer.cs @@ -1,4 +1,5 @@ -using BetterSecondBotShared.IO; +using BetterSecondBot.HttpWebUi; +using BetterSecondBotShared.IO; using BetterSecondBotShared.Json; using BetterSecondBotShared.logs; using BetterSecondBotShared.Static; @@ -22,8 +23,7 @@ namespace BetterSecondBot.HttpServer public SecondBot GetBot { get { return Bot; } } protected JsonConfig Config; public JsonConfig GetConfig { get { return Config; } } - protected HTTPCommandsInterfaceGet get_controler; - protected HTTPCommandsInterfacePost post_controler; + protected HTTPCommandsInterface post_controler; protected string url = ""; public bool ShutdownHTTP { get; set; } public bool HTTPCnCmode { get; set; } @@ -93,6 +93,7 @@ namespace BetterSecondBot.HttpServer protected async Task HandleIncomingConnections() { + webui myUI = new webui(Config,Bot); while (Bot.KillMe == false) { HttpListenerContext ctx = await listener.GetContextAsync(); @@ -107,16 +108,11 @@ namespace BetterSecondBot.HttpServer byte[] data; resp.StatusCode = 200; resp.ContentType = "text/html"; - resp.ContentEncoding = Encoding.UTF8; if (req.HttpMethod == "GET") { - string command = "status"; - if (http_args.Count > 0) - { - command = http_args.Last(); - } - string content = String.Join("{@}", get_controler.Call(command, "")); - data = Encoding.UTF8.GetBytes(content); + KeyValuePair replydata = myUI.process(http_args, req.Cookies); + resp.ContentType = replydata.Key; + data = replydata.Value; resp.ContentLength64 = data.LongLength; await resp.OutputStream.WriteAsync(data, 0, data.Length); resp.Close(); @@ -190,8 +186,7 @@ namespace BetterSecondBot.HttpServer } protected void OpenHttpService() { - get_controler = new HTTPCommandsInterfaceGet(Bot,this); - post_controler = new HTTPCommandsInterfacePost(Bot,this); + post_controler = new HTTPCommandsInterface(Bot,this); Thread t = new Thread(delegate (object _) { bool ok = true; diff --git a/CLI/HttpServer/control/Actions/logout.cs b/CLI/HttpServer/control/Actions/logout.cs index 646d120..76f314d 100644 --- a/CLI/HttpServer/control/Actions/logout.cs +++ b/CLI/HttpServer/control/Actions/logout.cs @@ -4,7 +4,7 @@ using System.Text; namespace BetterSecondBot.HttpServer.Control.Actions { - public class Logout : HTTP_commands_post + public class Logout : HTTP_commands { public override string Helpfile { get { return "Makes the bot logout
Returns OK,I dont want to go if accepted"; } } diff --git a/CLI/HttpServer/control/Interface/cmd_lib.cs b/CLI/HttpServer/control/Interface/cmd_lib.cs index 9afe597..5dccba1 100644 --- a/CLI/HttpServer/control/Interface/cmd_lib.cs +++ b/CLI/HttpServer/control/Interface/cmd_lib.cs @@ -5,7 +5,7 @@ using System.Text; namespace BetterSecondBot.HttpServer.Control.Interface { - public class CoreCMD : HTTP_commands_post + public class CoreCMD : HTTP_commands { public override string[] ArgTypes { get { return new [] { "Text","Text" }; } } public override string[] ArgHints { get { return new [] { "Command","Args (~#~ split)" }; } } diff --git a/CLI/HttpServer/http_commands.cs b/CLI/HttpServer/http_commands.cs index 9ead6c5..c5ca419 100644 --- a/CLI/HttpServer/http_commands.cs +++ b/CLI/HttpServer/http_commands.cs @@ -11,15 +11,17 @@ using BetterSecondBotShared.logs; namespace BetterSecondBot.HttpServer { - public abstract class HTTPCommandsInterface : API_supported_interface + public class HTTPCommandsInterface : API_supported_interface { protected SecondBot bot; protected SecondBotHttpServer httpserver; - protected HTTPCommandsInterface(SecondBot linktobot, SecondBotHttpServer sethttpserver) + public HTTPCommandsInterface(SecondBot linktobot, SecondBotHttpServer sethttpserver) { bot = linktobot; httpserver = sethttpserver; + API_type = typeof(HTTP_commands); + LoadCommandsList(); } public string[] Call(string command, string arg) @@ -38,30 +40,6 @@ namespace BetterSecondBot.HttpServer } } } - public class HTTPCommandsInterfaceGet : HTTPCommandsInterface - { - public HTTPCommandsInterfaceGet(SecondBot linktobot, SecondBotHttpServer sethttpserver) : base(linktobot, sethttpserver) - { - API_type = typeof(HTTP_commands_get); - LoadCommandsList(); - } - } - public class HTTPCommandsInterfacePost : HTTPCommandsInterface - { - public HTTPCommandsInterfacePost(SecondBot linktobot, SecondBotHttpServer sethttpserver) : base(linktobot, sethttpserver) - { - API_type = typeof(HTTP_commands_post); - LoadCommandsList(); - } - } - - - public abstract class HTTP_commands_get : HTTP_commands - { - } - public abstract class HTTP_commands_post : HTTP_commands - { - } public abstract class HTTP_commands : API_interface { diff --git a/CLI/HttpServer/view/Basic/status.cs b/CLI/HttpServer/view/Basic/status.cs deleted file mode 100644 index 064375f..0000000 --- a/CLI/HttpServer/view/Basic/status.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace BetterSecondBot.HttpServer.View.Basic -{ - public class Status : HTTP_commands_get - { - public override string Helpfile { get { return "returns the status of the bot"; } } - - public override string[] CallFunction(string[] args) - { - if (bot != null) - { - if (ArgsCheck(args) == true) - { - return new string[] { bot.GetStatus() }; - } - else - { - return Failed("Incorrect number of args"); - } - } - else - { - return Failed("No Bot"); - } - } - } -} diff --git a/CLI/HttpWebUi/theme/css/site.css b/CLI/HttpWebUi/theme/css/site.css new file mode 100644 index 0000000..582030c --- /dev/null +++ b/CLI/HttpWebUi/theme/css/site.css @@ -0,0 +1,4 @@ +.logo +{ + width: 100%; +} diff --git a/CLI/HttpWebUi/theme/images/favicon-32x32.png b/CLI/HttpWebUi/theme/images/favicon-32x32.png new file mode 100644 index 0000000..dc83cd0 Binary files /dev/null and b/CLI/HttpWebUi/theme/images/favicon-32x32.png differ diff --git a/CLI/HttpWebUi/theme/images/logo.png b/CLI/HttpWebUi/theme/images/logo.png new file mode 100644 index 0000000..540dbea Binary files /dev/null and b/CLI/HttpWebUi/theme/images/logo.png differ diff --git a/CLI/HttpWebUi/theme/js/site.js b/CLI/HttpWebUi/theme/js/site.js new file mode 100644 index 0000000..9dec0ef --- /dev/null +++ b/CLI/HttpWebUi/theme/js/site.js @@ -0,0 +1,75 @@ +$( document ).ready(function() { + $(".ajax").submit(function(e) { + e.preventDefault(); + var form = $(this); + var url = form.attr('action'); + var method = form.attr('method'); + $.ajax({ + type: method, + url: url, + data: form.serialize(), + success: function(data) + { + try + { + jsondata = JSON.parse(data); + var redirectdelay = 1500; + if(jsondata.hasOwnProperty('status')) + { + if(jsondata.hasOwnProperty('message')) + { + if(jsondata.status == true) + { + if(jsondata.message != "") alert_success(jsondata.message); + } + else + { + redirectdelay = 3500; + if(jsondata.message != "") alert_warning(jsondata.message); + } + } + if(jsondata.hasOwnProperty('redirect')) + { + setTimeout(function(){ $(location).attr('href', jsondata.redirect) }, redirectdelay); + } + } + else + { + alert_error("Reply from server is not vaild please reload the page and try again."); + } + } + catch(e) + { + alert_warning("Unable to process reply, please reload the page and try again!"); + } + }, + error: function (data) + { + alert_error(data); + } + }); + }); +}); + +function alert_success(smsg) +{ + alert(smsg,"success"); +} +function alert_error(smsg) +{ + alert(smsg,"error"); +} +function alert_warning(smsg) +{ + alert(smsg,"warning"); +} +function alert(smsg,alerttype) +{ + $.notify({ + // options + message: smsg + },{ + // settings + type: alerttype + }); +} diff --git a/CLI/HttpWebUi/theme/layout/cdn.css.layout b/CLI/HttpWebUi/theme/layout/cdn.css.layout new file mode 100644 index 0000000..57c2197 --- /dev/null +++ b/CLI/HttpWebUi/theme/layout/cdn.css.layout @@ -0,0 +1,2 @@ + + diff --git a/CLI/HttpWebUi/theme/layout/cdn.js.layout b/CLI/HttpWebUi/theme/layout/cdn.js.layout new file mode 100644 index 0000000..689180f --- /dev/null +++ b/CLI/HttpWebUi/theme/layout/cdn.js.layout @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/CLI/HttpWebUi/theme/layout/full.layout b/CLI/HttpWebUi/theme/layout/full.layout new file mode 100644 index 0000000..b002a70 --- /dev/null +++ b/CLI/HttpWebUi/theme/layout/full.layout @@ -0,0 +1,33 @@ + + + [[html_title]] - [[html_title_after]] + [[html_cs_top]] + + + + +
+
+
+
+
+ +
+
+
+
+ [[page_content]] +
+
+
+
+
+ [[html_js_bottom]] + + + + diff --git a/CLI/HttpWebUi/theme/layout/login.block b/CLI/HttpWebUi/theme/layout/login.block new file mode 100644 index 0000000..c336d96 --- /dev/null +++ b/CLI/HttpWebUi/theme/layout/login.block @@ -0,0 +1,14 @@ +
+
+
Bot webUI auth required
+
+
+
+
+
+
+
+
+
+
+
diff --git a/CLI/HttpWebUi/theme/layout/sidemenu.layout b/CLI/HttpWebUi/theme/layout/sidemenu.layout new file mode 100644 index 0000000..36e5555 --- /dev/null +++ b/CLI/HttpWebUi/theme/layout/sidemenu.layout @@ -0,0 +1,59 @@ + + + [[html_title]] - [[html_title_after]] + [[html_cs_top]] + + + + +
+
+
+
+
+
+
+
+ +
+
+
+
+

+
+
+
+
+
+ +
+
+
+ + +
+
+
+
+
[[page_title]]
+
[[page_actions]]
+
+
+
+ [[page_content]] +
+
+
+
+
+ [[html_js_bottom]] + + + + diff --git a/CLI/HttpWebUi/webui.cs b/CLI/HttpWebUi/webui.cs new file mode 100644 index 0000000..d5838f2 --- /dev/null +++ b/CLI/HttpWebUi/webui.cs @@ -0,0 +1,79 @@ +using BetterSecondBotShared.Json; +using BetterSecondBotShared.Static; +using BSB.bottypes; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Reflection; +using System.Text; + +namespace BetterSecondBot.HttpWebUi +{ + public class webui + { + JsonConfig siteconfig; + SecondBot bot; + public webui(JsonConfig setconfig,SecondBot setbot) + { + bot = setbot; + siteconfig = setconfig; + } + public KeyValuePair process(List args,CookieCollection cookies) + { + Cookie logincookie = null; + foreach(Cookie C in cookies) + { + if(C.Name == "logincookie") + { + logincookie = C; + break; + } + } + if (logincookie == null) + { + if (args != null) + { + if (args.Count >= 1) + { + if (args[0] == "theme") + { + if (args.Count >= 2) + { + if (args[1] == "images") + { + return new KeyValuePair("image/png", helpers.ReadResourceFileBinary(Assembly.GetExecutingAssembly(), args[2])); + } + else if (args[1] == "css") + { + return new KeyValuePair("text/css", Encoding.UTF8.GetBytes(helpers.ReadResourceFile(Assembly.GetExecutingAssembly(), args[2]))); + } + else if (args[1] == "js") + { + return new KeyValuePair("text/javascript", Encoding.UTF8.GetBytes(helpers.ReadResourceFile(Assembly.GetExecutingAssembly(), args[2]))); + } + } + } + return new KeyValuePair("text/html", Encoding.UTF8.GetBytes("")); + } + } + string layout = helpers.ReadResourceFile(Assembly.GetExecutingAssembly(), "full.layout"); + layout = layout.Replace("[[page_content]]", helpers.ReadResourceFile(Assembly.GetExecutingAssembly(), "login.block")); + layout = layout.Replace("[[url_base]]", siteconfig.HttpPublicUrlBase); + layout = layout.Replace("[[html_title_after]]", "Secondbot web UI"); + layout = layout.Replace("[[html_title]]", siteconfig.userName); + layout = layout.Replace("[[html_cs_top]]", helpers.ReadResourceFile(Assembly.GetExecutingAssembly(), "cdn.css.layout")); + layout = layout.Replace("[[html_js_bottom]]", helpers.ReadResourceFile(Assembly.GetExecutingAssembly(), "cdn.js.layout")); + + layout = layout.Replace("[[html_js_onready]]", ""); + return new KeyValuePair("text/html", Encoding.UTF8.GetBytes(layout)); + + } + else + { + return new KeyValuePair("text/html", Encoding.UTF8.GetBytes("")); + } + } + } + +} diff --git a/CLI/WikiMake/wiki_header.txt b/CLI/WikiMake/wiki_header.txt index de4e535..28a6b76 100644 --- a/CLI/WikiMake/wiki_header.txt +++ b/CLI/WikiMake/wiki_header.txt @@ -16,7 +16,6 @@ Main commands [Core] JSON [Bot Config file] Restrained Love API [RLV] - Get [HTTP] Post [HTTP]