From 7d236a591d514c69ace54b6754395ff3d6f05ce5 Mon Sep 17 00:00:00 2001 From: Madpeter Date: Sun, 26 Jul 2020 13:06:58 +0100 Subject: [PATCH] adds name2key key2name endpoint support (much better than SLs service) [see: https://hub.docker.com/repository/docker/madpeter/secondlife_name2key_endpoint] --- BSBshared/Json/JsonConfig.cs | 5 ++ CLI/Dockerfile-ForDebug | 4 ++ Core/bottypes/AVstorageBot.cs | 131 +++++++++++++++++++++++++--------- Dockerfile | 5 ++ 4 files changed, 112 insertions(+), 33 deletions(-) diff --git a/BSBshared/Json/JsonConfig.cs b/BSBshared/Json/JsonConfig.cs index 52ca403..22737a4 100644 --- a/BSBshared/Json/JsonConfig.cs +++ b/BSBshared/Json/JsonConfig.cs @@ -41,6 +41,11 @@ namespace BetterSecondBotShared.Json public string Http_Host { get; set; } public string Http_PublicUrl { get; set; } + // Name2Key DB + public bool Name2Key_Enable { get; set; } + public string Name2Key_Url { get; set; } + public string Name2Key_Key { get; set; } + } } diff --git a/CLI/Dockerfile-ForDebug b/CLI/Dockerfile-ForDebug index 0c4e35c..ba7ddfb 100644 --- a/CLI/Dockerfile-ForDebug +++ b/CLI/Dockerfile-ForDebug @@ -51,6 +51,10 @@ ENV Http_Enable='false' ENV Http_Port='80' ENV Http_Host='http://localhost:80' ENV Http_PublicUrl='http://localhost/' + +ENV Name2Key_Enable='false' +ENV Name2Key_Url='http://localhost:1234' +ENV Name2Key_Key='magic' EXPOSE 80 ENV ASPNETCORE_URLS http://+:80 diff --git a/Core/bottypes/AVstorageBot.cs b/Core/bottypes/AVstorageBot.cs index 0e832d4..d36e57f 100644 --- a/Core/bottypes/AVstorageBot.cs +++ b/Core/bottypes/AVstorageBot.cs @@ -1,5 +1,7 @@ using BetterSecondBotShared.Static; +using Newtonsoft.Json; using OpenMetaverse; +using RestSharp; using System; using System.Collections.Generic; using System.Linq; @@ -18,9 +20,6 @@ namespace BSB.bottypes protected Dictionary> PendingAvatarFinds_vianame = new Dictionary>(); protected Dictionary> PendingAvatarFinds_viauuid = new Dictionary>(); - protected long max_wait_for_retry_name2key = 30; // How long to wait before we attempt a name to key lookup again for the same avatar (in secs) - protected long max_wait_for_retry_key2name = 10; // How long to wait before we attempt a key to name lookup again for the same avatar (in secs) - protected int max_retry_attempts = 2; // how many times should we attempt to get the avatar before giving up. protected long max_db_storage_age = 240; // how long should we keep an avatar in memory before clearing (in secs) [now - last accessed] protected bool show_av_storage_info_in_status = true; @@ -63,18 +62,7 @@ namespace BSB.bottypes List giveup_key2name = new List(); foreach (KeyValuePair> pair in PendingAvatarFinds_viauuid) { - long dif = now - pair.Value.Key; - if (dif > max_wait_for_retry_key2name) - { - if (pair.Value.Value < max_retry_attempts) - { - retrigger_key2name.Add(pair.Key); - } - else - { - giveup_key2name.Add(pair.Key); - } - } + giveup_key2name.Add(pair.Key); } if(retrigger_key2name.Count > 0) { @@ -97,18 +85,7 @@ namespace BSB.bottypes List giveup_name2key = new List(); foreach (KeyValuePair> pair in PendingAvatarFinds_vianame) { - long dif = now - pair.Value.Key; - if (dif > max_wait_for_retry_key2name) - { - if (pair.Value.Value < max_retry_attempts) - { - retrigger_name2key.Add(pair.Key); - } - else - { - giveup_name2key.Add(pair.Key); - } - } + giveup_name2key.Add(pair.Key); } foreach (string avname in retrigger_name2key) @@ -226,9 +203,16 @@ namespace BSB.bottypes } } } - public string FindAvatarName2Key(string avatar_name) { + return FindAvatarName2Key(avatar_name, false); + } + public string FindAvatarName2Key(string avatar_name,bool skip_http) + { + if (myconfig.Name2Key_Enable == false) + { + skip_http = true; + } List bits = avatar_name.Split(' ').ToList(); if(bits.Count() == 1) { @@ -245,14 +229,54 @@ namespace BSB.bottypes { if(PendingAvatarFinds_vianame.ContainsKey(avatar_name) == false) { - PendingAvatarFinds_vianame.Add(avatar_name, new KeyValuePair(now, 0)); - Client.Directory.StartPeopleSearch(avatar_name, 0); + if (skip_http == false) + { + var client = new RestClient(myconfig.Name2Key_Url); + var request = new RestRequest("name2key/" + avatar_name + "/" + myconfig.Name2Key_Key + "", Method.GET); + client.Timeout = 3000; // ? does this even do anything + request.Timeout = 3000; + IRestResponse endpoint_checks = client.Get(request); + if (endpoint_checks.StatusCode == System.Net.HttpStatusCode.OK) + { + try + { + namekeyservicereply server_reply = JsonConvert.DeserializeObject(endpoint_checks.Content); + if (server_reply.status == true) + { + if (server_reply.found == true) + { + AddAvatarToDB((UUID)server_reply.uuid, server_reply.name); + return server_reply.uuid; + } + } + } + catch + { + + } + } + return FindAvatarName2Key(avatar_name, true); + + } + else + { + PendingAvatarFinds_vianame.Add(avatar_name, new KeyValuePair(now, 0)); + Client.Directory.StartPeopleSearch(avatar_name, 0); + } } } return "lookup"; } public string FindAvatarKey2Name(UUID avatar_uuid) { + return FindAvatarKey2Name(avatar_uuid, false); + } + public string FindAvatarKey2Name(UUID avatar_uuid, bool skip_http) + { + if (myconfig.Name2Key_Enable == false) + { + skip_http = true; + } long now = helpers.UnixTimeNow(); if (AvatarKey2Name.ContainsKey(avatar_uuid) == true) { @@ -261,10 +285,41 @@ namespace BSB.bottypes } else { - if (PendingAvatarFinds_viauuid.ContainsKey(avatar_uuid) == false) + if (skip_http == false) { - PendingAvatarFinds_viauuid.Add(avatar_uuid, new KeyValuePair(now, 0)); - Client.Avatars.RequestAvatarName(avatar_uuid); + var client = new RestClient(myconfig.Name2Key_Url); + var request = new RestRequest("key2name/" + avatar_uuid.ToString() + "/" + myconfig.Name2Key_Key + "", Method.GET); + client.Timeout = 3000; // ? does this even do anything + request.Timeout = 3000; + IRestResponse endpoint_checks = client.Get(request); + if (endpoint_checks.StatusCode == System.Net.HttpStatusCode.OK) + { + try + { + namekeyservicereply server_reply = JsonConvert.DeserializeObject(endpoint_checks.Content); + if (server_reply.status == true) + { + if(server_reply.found == true) + { + AddAvatarToDB((UUID)server_reply.uuid, server_reply.name); + return server_reply.name; + } + } + } + catch + { + + } + } + return FindAvatarKey2Name(avatar_uuid, true); + } + else + { + if (PendingAvatarFinds_viauuid.ContainsKey(avatar_uuid) == false) + { + PendingAvatarFinds_viauuid.Add(avatar_uuid, new KeyValuePair(now, 0)); + Client.Avatars.RequestAvatarName(avatar_uuid); + } } return "lookup"; } @@ -275,4 +330,14 @@ namespace BSB.bottypes } } + + public class namekeyservicereply + { + public string lookingfor { get; set; } + public string uuid { get; set; } + public string name { get; set; } + public bool found { get; set; } + public bool status { get; set; } + public string message { get; set; } + } } diff --git a/Dockerfile b/Dockerfile index f258874..fa2c675 100644 --- a/Dockerfile +++ b/Dockerfile @@ -51,6 +51,11 @@ ENV Http_Enable='false' ENV Http_Port='80' ENV Http_Host='http://localhost:80' ENV Http_PublicUrl='http://localhost/' + +ENV Name2Key_Enable='false' +ENV Name2Key_Url='http://localhost:1234' +ENV Name2Key_Key='magic' + EXPOSE 80 ENV ASPNETCORE_URLS http://+:80