updates the webUI to not kill itself if you look at it wrong,

enables localchat
This commit is contained in:
Madpeter 2020-05-15 23:24:17 +01:00
parent 375a85667b
commit 9fe2b9927c
2 changed files with 348 additions and 48 deletions

View file

@ -15,6 +15,7 @@ using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web;
namespace BetterSecondBot.HttpServer namespace BetterSecondBot.HttpServer
{ {
@ -104,10 +105,20 @@ namespace BetterSecondBot.HttpServer
HttpListenerRequest req = ctx.Request; HttpListenerRequest req = ctx.Request;
HttpListenerResponse resp = ctx.Response; HttpListenerResponse resp = ctx.Response;
resp.Cookies = req.Cookies; resp.Cookies = req.Cookies;
string test = req.Url.AbsolutePath.Substring(1); string test = req.Url.AbsoluteUri;
if (helpers.notempty(Config.Http_PublicUrl) == true) if (helpers.notempty(Config.Http_PublicUrl) == true)
{ {
// normal mapped
test = test.Replace(Config.Http_PublicUrl, ""); test = test.Replace(Config.Http_PublicUrl, "");
//rproxy weirdness
string use_http_url = Config.Http_PublicUrl.Replace("http://", "");
use_http_url = Config.Http_PublicUrl.Replace("https://", "");
test = test.Replace(":" + Config.Http_Port.ToString(), "");
test = test.Replace(use_http_url, "");
test = test.Replace("https://", "");
test = test.Replace("http://", "");
} }
json_http_reply reply = new json_http_reply(); json_http_reply reply = new json_http_reply();
List<string> http_args = test.Split('/', StringSplitOptions.RemoveEmptyEntries).ToList(); List<string> http_args = test.Split('/', StringSplitOptions.RemoveEmptyEntries).ToList();
@ -149,7 +160,7 @@ namespace BetterSecondBot.HttpServer
string[] bits = B.Split('=', StringSplitOptions.RemoveEmptyEntries); string[] bits = B.Split('=', StringSplitOptions.RemoveEmptyEntries);
if (bits.Length == 2) if (bits.Length == 2)
{ {
post_args.Add(bits[0].Trim(), bits[1].Trim()); post_args.Add(bits[0].Trim(), HttpUtility.UrlDecode(bits[1].Trim()));
} }
} }
resp.StatusCode = 200; resp.StatusCode = 200;
@ -195,13 +206,20 @@ namespace BetterSecondBot.HttpServer
reply.message = String.Join("###", post_controler.Call(command, arg)); reply.message = String.Join("###", post_controler.Call(command, arg));
} }
} }
if(reply.redirect != "") if (reply.redirect != "")
{ {
if(reply.redirect == "/") if (reply.redirect != null)
{ {
reply.redirect = ""; if (reply.redirect == "/")
{
reply.redirect = Config.Http_PublicUrl;
}
else
{
reply.redirect = Config.Http_PublicUrl + reply.redirect;
}
} }
reply.redirect = "" + Config.Http_PublicUrl + "" + reply.redirect + "";
} }
data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(reply)); data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(reply));
resp.ContentLength64 = data.LongLength; resp.ContentLength64 = data.LongLength;
@ -230,15 +248,21 @@ namespace BetterSecondBot.HttpServer
} }
if (ok == true) if (ok == true)
{ {
try bool clean_exit = false;
ConsoleLog.Status("Listening for connections on " + url + "");
while (clean_exit == false)
{ {
ConsoleLog.Status("Listening for connections on " + url + ""); try
Task listenTask = HandleIncomingConnections(); {
listenTask.GetAwaiter().GetResult(); ConsoleLog.Status("HTTP ready");
} Task listenTask = HandleIncomingConnections();
catch(Exception e) listenTask.GetAwaiter().GetResult();
{ }
ConsoleLog.Status("Http interface killed itself: "+e.ToString()+""); catch (Exception e)
{
ConsoleLog.Status("HTTP died");
//ConsoleLog.Status("Http interface killed itself: " + e.ToString() + "");
}
} }
} }
listener.Close(); listener.Close();

View file

@ -2,12 +2,16 @@
using BetterSecondBotShared.Json; using BetterSecondBotShared.Json;
using BetterSecondBotShared.Static; using BetterSecondBotShared.Static;
using BSB.bottypes; using BSB.bottypes;
using Microsoft.VisualBasic.CompilerServices;
using Newtonsoft.Json;
using OpenMetaverse;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Threading;
namespace BetterSecondBot.HttpWebUi namespace BetterSecondBot.HttpWebUi
{ {
@ -16,7 +20,7 @@ namespace BetterSecondBot.HttpWebUi
JsonConfig siteconfig; JsonConfig siteconfig;
SecondBot bot; SecondBot bot;
Dictionary<string, long> vaildlogincookies = new Dictionary<string, long>(); Dictionary<string, long> vaildlogincookies = new Dictionary<string, long>();
public webui(JsonConfig setconfig,SecondBot setbot) public webui(JsonConfig setconfig, SecondBot setbot)
{ {
bot = setbot; bot = setbot;
siteconfig = setconfig; siteconfig = setconfig;
@ -25,7 +29,7 @@ namespace BetterSecondBot.HttpWebUi
{ {
return logincookievaild(reqs, resp, false); return logincookievaild(reqs, resp, false);
} }
protected KeyValuePair<bool,bool> logincookievaild(HttpListenerRequest reqs, HttpListenerResponse resp,bool force_logout) protected KeyValuePair<bool, bool> logincookievaild(HttpListenerRequest reqs, HttpListenerResponse resp, bool force_logout)
{ {
foreach (Cookie C in resp.Cookies) foreach (Cookie C in resp.Cookies)
{ {
@ -38,7 +42,6 @@ namespace BetterSecondBot.HttpWebUi
{ {
vaildlogincookies.Remove(C.Value); vaildlogincookies.Remove(C.Value);
} }
Console.WriteLine("Cookie wiped");
} }
else else
{ {
@ -46,17 +49,8 @@ namespace BetterSecondBot.HttpWebUi
{ {
if (vaildlogincookies[C.Value] > helpers.UnixTimeNow()) if (vaildlogincookies[C.Value] > helpers.UnixTimeNow())
{ {
Console.WriteLine("Cookie ok");
return new KeyValuePair<bool, bool>(true, true); return new KeyValuePair<bool, bool>(true, true);
} }
else
{
Console.WriteLine("Cookie expired");
}
}
else
{
Console.WriteLine("Cookie not vaild");
} }
} }
resp.Cookies.Remove(C); resp.Cookies.Remove(C);
@ -65,17 +59,16 @@ namespace BetterSecondBot.HttpWebUi
return new KeyValuePair<bool, bool>(true, false); return new KeyValuePair<bool, bool>(true, false);
} }
} }
Console.WriteLine("No cookie");
return new KeyValuePair<bool, bool>(false, false); return new KeyValuePair<bool, bool>(false, false);
} }
public bool Post_process(json_http_reply reply,HttpListenerRequest reqs, HttpListenerResponse resp,List<string> args, Dictionary<string,string> post_args) public bool Post_process(json_http_reply reply, HttpListenerRequest reqs, HttpListenerResponse resp, List<string> args, Dictionary<string, string> post_args)
{ {
bool processed = false; bool processed = false;
if (args != null) if (args != null)
{ {
if(args.Count >= 1) if (args.Count >= 1)
{ {
if(args[0] == "ajax") if (args[0] == "ajax")
{ {
processed = true; processed = true;
// webui only cares about ajax post requests // webui only cares about ajax post requests
@ -110,22 +103,25 @@ namespace BetterSecondBot.HttpWebUi
logincookie.Expires = DateTime.Now.AddDays(1); logincookie.Expires = DateTime.Now.AddDays(1);
logincookie.Path = "/"; logincookie.Path = "/";
resp.Cookies.Add(logincookie); resp.Cookies.Add(logincookie);
Console.WriteLine("Cookie created: "+ logincookie.Value+" <=> "+ newcookiecode+""); Console.WriteLine("Cookie created: " + logincookie.Value + " <=> " + newcookiecode + "");
reply.message = "ok"; reply.message = "ok";
reply.redirect = "/"; reply.redirect = "/";
} }
else else
{ {
reply.status = false;
reply.message = "fail"; reply.message = "fail";
} }
} }
else else
{ {
reply.status = false;
reply.message = "Missing code"; reply.message = "Missing code";
} }
} }
else else
{ {
reply.status = false;
reply.message = "No access"; reply.message = "No access";
} }
} }
@ -139,15 +135,228 @@ namespace BetterSecondBot.HttpWebUi
{ {
logincookievaild(reqs, resp, true); logincookievaild(reqs, resp, true);
reply.message = "logged-out"; reply.message = "logged-out";
reply.redirect = "/";
reply.status = false;
}
else if(mod == "interface")
{
if (area == "get")
{
reply.message = JsonConvert.SerializeObject(bot.GetLastCommands(30));
}
else if(area == "send")
{
if (post_args.ContainsKey("message") == true)
{
string[] bits = post_args["message"].Split("|||");
if(bits.Length == 2)
{
reply.status = bot.GetCommandsInterface.Call(bits[0], bits[1]);
if(reply.status == true)
{
reply.message = "ok";
}
else
{
reply.message = "Failed";
}
}
else
{
reply.status = bot.GetCommandsInterface.Call(post_args["message"]);
if (reply.status == true)
{
reply.message = "ok";
}
else
{
reply.message = "Failed";
}
}
}
else
{
reply.message = "No message";
reply.status = false;
}
}
else
{
reply.message = "Unknown area";
reply.status = false;
}
}
else if (mod == "localchat")
{
if (area == "get")
{
reply.message = JsonConvert.SerializeObject(bot.getLocalChatHistory());
}
else if (area == "send")
{
if (post_args.ContainsKey("message") == true)
{
bot.AddToLocalChat("{Via WebGUI}", post_args["message"]);
bot.GetClient.Self.Chat("{Via WebGUI} "+ post_args["message"], 0,ChatType.Normal);
reply.message = "ok";
reply.status = true;
}
else
{
reply.message = "No message";
reply.status = false;
}
}
else
{
reply.message = "Unknown area";
reply.status = false;
}
}
else if (mod == "groupchat")
{
if (area == "list")
{
Dictionary<string, string> groupname_uuid = new Dictionary<string, string>();
foreach (KeyValuePair<UUID, Group> entry in bot.MyGroups)
{
groupname_uuid.Add(entry.Key.ToString(), entry.Value.Name);
}
reply.message = JsonConvert.SerializeObject(groupname_uuid);
}
if (area == "get")
{
if (post_args.ContainsKey("groupuuid") == true)
{
if (UUID.TryParse(post_args["groupuuid"], out UUID groupuuid) == true)
{
reply.message = JsonConvert.SerializeObject(bot.GetGroupchat(groupuuid));
}
else
{
reply.message = "Unable to process group uuid";
reply.status = false;
}
}
else
{
reply.message = "No groupuuid";
reply.status = false;
}
}
else if (area == "send")
{
if (post_args.ContainsKey("groupuuid") == true)
{
if (UUID.TryParse(post_args["groupuuid"], out UUID groupuuid) == true)
{
if (post_args.ContainsKey("message") == true)
{
bot.GetClient.Self.RequestJoinGroupChat(groupuuid);
Thread.Sleep(100);
bot.GetClient.Self.InstantMessageGroup(groupuuid, "{Via WebGUI} " + post_args["message"]);
bot.AddToGroupchat(groupuuid, "{Via WebGUI}", post_args["message"]);
reply.message = "ok";
reply.status = true;
}
else
{
reply.message = "No message";
reply.status = false;
}
}
else
{
reply.message = "Unable to process group uuid";
reply.status = false;
}
}
else
{
reply.message = "No groupuuid";
reply.status = false;
}
}
else
{
reply.message = "Unknown area";
reply.status = false;
}
}
else if (mod == "im")
{
if (area == "list")
{
reply.message = JsonConvert.SerializeObject(bot.GetIMChatWindowKeyNames());
}
else if (area == "send")
{
if (post_args.ContainsKey("avataruuid") == true)
{
if (UUID.TryParse(post_args["avataruuid"], out UUID avataruuid) == true)
{
if (post_args.ContainsKey("message") == true)
{
bot.GetCommandsInterface.Call("im", "" + avataruuid.ToString() + "~#~{Via WebGUI} " + post_args["message"]);
reply.message = "ok";
reply.status = true;
}
else
{
reply.message = "No message";
}
}
else
{
reply.message = "Unable to process avatar uuid";
reply.status = false;
}
}
else
{
reply.message = "No avataruuid";
reply.status = false;
}
}
else if (area == "get")
{
if (post_args.ContainsKey("avataruuid") == true)
{
if (UUID.TryParse(post_args["avataruuid"], out UUID avataruuid) == true)
{
reply.message = JsonConvert.SerializeObject(bot.GetIMChatWindow(avataruuid));
}
else
{
reply.message = "Unable to process avatar uuid";
reply.status = false;
}
}
else
{
reply.message = "No avataruuid";
reply.status = false;
}
}
else
{
reply.message = "Unknown area";
reply.status = false;
}
}
else
{
reply.message = "No action";
reply.status = false;
} }
reply.message = "No action";
} }
else else
{ {
// login expired // login expired
reply.message = "login-expired"; reply.message = "login-expired";
reply.redirect = "/"; reply.redirect = "/";
logincookievaild(reqs, resp,true); logincookievaild(reqs, resp, true);
} }
} }
} }
@ -155,22 +364,83 @@ namespace BetterSecondBot.HttpWebUi
} }
return !processed; return !processed;
} }
public string html_logged_in(string mod, string area)
{
string layout = helpers.ReadResourceFile(Assembly.GetExecutingAssembly(), "sidemenu.layout");
Dictionary<string, string> swaps = new Dictionary<string, string>();
public KeyValuePair<string, byte[]> html_logged_out()
string pagetitle = "Dashboard";
string pagecontent = "";
string pageactions = "";
string js_onready = "";
string[] menu_text = new string[] { "Localchat", "Groupchat", "IMs", "Interface" };
string[] menu_links = new string[] { "localchat", "groupchat", "im", "interface" };
string[] meni_icons = new string[] { "fas fa-comment", "fas fa-comments", "fas fa-users","fas fa-robot" };
int loop = 0;
string menu = "";
while (loop < menu_text.Count())
{
string active = "";
if (menu_links[loop] == mod) active = "active";
menu = "" + menu + " <li class=\"nav-item\"><a href=\"[[url_base]]"+menu_links[loop]+"\" class=\"nav-link " + active+ "\"><i class=\"" + meni_icons[loop] + " text-success\"></i> " + menu_text[loop] + "</a></li>";
loop++;
}
swaps.Add("html_menu", menu);
menu = "";
if (mod == "none")
{
pagecontent = "<p>Please select a menu item on the left</p>";
}
else if(mod == "localchat")
{
pagetitle = "Localchat";
pagecontent = "<textarea readonly cols=\"82\" rows=\"14\" id=\"localchat\" name=\"localchat\"> - Loading local chat please wait -</textarea><br/>" +
"<form method=\"post\" class=\"ajaxq\" action=\"[[url_base]]ajax/localchat/send\">" +
"<input type=\"text\" name=\"message\" id=\"message\" value=\"\" size=\"33\" class=\"form-control\" placeholder=\"Say in localchat\">" +
"<button type\"submit\" class=\"btn btn-primary\">Send</button>"+
"</form>";
js_onready = "setInterval(function(){ update_localchat(\"" + siteconfig.Http_PublicUrl+"\"); },400);";
}
else if (mod == "groupchat")
{
pagetitle = "Group chat";
pagecontent = "";
if (area == "none")
{
}
}
swaps.Add("html_title", pagetitle);
swaps.Add("page_actions", pageactions);
swaps.Add("page_content", pagecontent);
swaps.Add("page_title", pagetitle);
swaps.Add("html_cs_top", "");
swaps.Add("html_js_bottom", "");
swaps.Add("html_js_onready", js_onready);
foreach (KeyValuePair<string,string> A in swaps)
{
layout = layout.Replace("[[" + A.Key + "]]", A.Value);
}
return layout;
}
public string html_logged_out()
{ {
string layout = helpers.ReadResourceFile(Assembly.GetExecutingAssembly(), "full.layout"); string layout = helpers.ReadResourceFile(Assembly.GetExecutingAssembly(), "full.layout");
layout = layout.Replace("[[page_content]]", helpers.ReadResourceFile(Assembly.GetExecutingAssembly(), "login.block")); layout = layout.Replace("[[page_content]]", helpers.ReadResourceFile(Assembly.GetExecutingAssembly(), "login.block"));
layout = layout.Replace("[[url_base]]", siteconfig.Http_PublicUrl);
layout = layout.Replace("[[html_title_after]]", "Secondbot web UI");
layout = layout.Replace("[[html_title]]", siteconfig.Basic_BotUserName);
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]]", ""); layout = layout.Replace("[[html_js_onready]]", "");
return new KeyValuePair<string, byte[]>("text/html", Encoding.UTF8.GetBytes(layout)); layout = layout.Replace("[[html_cs_top]]", "");
layout = layout.Replace("[[html_js_bottom]]", "");
layout = layout.Replace("[[html_title]]", siteconfig.Basic_BotUserName);
return layout;
} }
public KeyValuePair<string,byte[]> Get_Process(HttpListenerRequest reqs, HttpListenerResponse resp, List<string> args) public KeyValuePair<string, byte[]> Get_Process(HttpListenerRequest reqs, HttpListenerResponse resp, List<string> args)
{ {
// get content // get content
if (args != null) if (args != null)
{ {
@ -198,16 +468,17 @@ namespace BetterSecondBot.HttpWebUi
} }
// get display // get display
KeyValuePair<bool, bool> logincheck = logincookievaild(reqs, resp); KeyValuePair<bool, bool> logincheck = logincookievaild(reqs, resp);
if(logincheck.Key == true) if (logincheck.Key == true)
{ {
if(logincheck.Value == false) if (logincheck.Value == false)
{ {
logincookievaild(reqs, resp, true); logincookievaild(reqs, resp, true);
} }
} }
string reply_with = "";
if (logincheck.Value == false) if (logincheck.Value == false)
{ {
return html_logged_out(); reply_with = html_logged_out();
} }
else else
{ {
@ -221,18 +492,23 @@ namespace BetterSecondBot.HttpWebUi
{ {
area = args[1]; area = args[1];
} }
if (mod == "logout") if (mod == "logout")
{ {
logincookievaild(reqs, resp, true); logincookievaild(reqs, resp, true);
return html_logged_out(); reply_with = html_logged_out();
} }
else else
{ {
// logged in // logged in
return new KeyValuePair<string, byte[]>("text/html", Encoding.UTF8.GetBytes("logged in")); reply_with = html_logged_in(mod, area);
} }
} }
reply_with = reply_with.Replace("[[url_base]]", siteconfig.Http_PublicUrl);
reply_with = reply_with.Replace("[[html_title_after]]", "Secondbot web UI");
reply_with = reply_with.Replace("[[html_cs_top_cdn]]", helpers.ReadResourceFile(Assembly.GetExecutingAssembly(), "cdn.css.layout"));
reply_with = reply_with.Replace("[[html_js_bottom_cdn]]", helpers.ReadResourceFile(Assembly.GetExecutingAssembly(), "cdn.js.layout"));
return new KeyValuePair<string, byte[]>("text/html", Encoding.UTF8.GetBytes(reply_with));
} }
} }
} }