1
0

PresenceServerPostHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using Nini.Config;
  28. using log4net;
  29. using System;
  30. using System.Reflection;
  31. using System.IO;
  32. using System.Net;
  33. using System.Text;
  34. using System.Text.RegularExpressions;
  35. using System.Xml;
  36. using System.Xml.Serialization;
  37. using System.Collections.Generic;
  38. using OpenSim.Server.Base;
  39. using OpenSim.Services.Interfaces;
  40. using OpenSim.Framework;
  41. using OpenSim.Framework.Servers.HttpServer;
  42. using OpenSim.Framework.ServiceAuth;
  43. using OpenMetaverse;
  44. namespace OpenSim.Server.Handlers.Presence
  45. {
  46. public class PresenceServerPostHandler : BaseStreamHandler
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private IPresenceService m_PresenceService;
  50. public PresenceServerPostHandler(IPresenceService service, IServiceAuth auth) :
  51. base("POST", "/presence", auth)
  52. {
  53. m_PresenceService = service;
  54. }
  55. protected override byte[] ProcessRequest(string path, Stream requestData,
  56. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  57. {
  58. StreamReader sr = new StreamReader(requestData);
  59. string body = sr.ReadToEnd();
  60. sr.Close();
  61. body = body.Trim();
  62. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  63. string method = string.Empty;
  64. try
  65. {
  66. Dictionary<string, object> request = ServerUtils.ParseQueryString(body);
  67. if (!request.TryGetValue("METHOD", out object tmpobj) || tmpobj is not string)
  68. return FailureResult();
  69. method = (string)tmpobj;
  70. switch (method)
  71. {
  72. case "login":
  73. {
  74. //return LoginAgent(request); this is ilegal
  75. if (request.TryGetValue("UserID", out object uo) && uo is string user)
  76. m_log.Debug($"[PRESENCE HANDLER]: ilegal login try from {httpRequest.RemoteIPEndPoint} for userID {user}");
  77. else
  78. m_log.Debug($"[PRESENCE HANDLER]: ilegal login try from {httpRequest.RemoteIPEndPoint} for unkown user");
  79. return FailureResult();
  80. }
  81. case "logout":
  82. return LogoutAgent(request);
  83. case "logoutregion":
  84. return LogoutRegionAgents(request);
  85. case "report":
  86. return Report(request);
  87. case "getagent":
  88. return GetAgent(request);
  89. case "getagents":
  90. return GetAgents(request);
  91. }
  92. m_log.Debug($"[PRESENCE HANDLER]: unknown method request: {method}");
  93. }
  94. catch (Exception e)
  95. {
  96. m_log.Debug($"[PRESENCE HANDLER]: Exception in method {method}: {e.Message}");
  97. }
  98. return FailureResult();
  99. }
  100. byte[] LoginAgent(Dictionary<string, object> request)
  101. {
  102. string user = String.Empty;
  103. UUID session = UUID.Zero;
  104. UUID ssession = UUID.Zero;
  105. if (!request.ContainsKey("UserID") || !request.ContainsKey("SessionID"))
  106. return FailureResult();
  107. user = request["UserID"].ToString();
  108. if (!UUID.TryParse(request["SessionID"].ToString(), out session))
  109. return FailureResult();
  110. if (request.ContainsKey("SecureSessionID"))
  111. // If it's malformed, we go on with a Zero on it
  112. UUID.TryParse(request["SecureSessionID"].ToString(), out ssession);
  113. if (m_PresenceService.LoginAgent(user, session, ssession))
  114. return SuccessResult();
  115. return FailureResult();
  116. }
  117. byte[] LogoutAgent(Dictionary<string, object> request)
  118. {
  119. UUID session = UUID.Zero;
  120. if (!request.ContainsKey("SessionID"))
  121. return FailureResult();
  122. if (!UUID.TryParse(request["SessionID"].ToString(), out session))
  123. return FailureResult();
  124. if (m_PresenceService.LogoutAgent(session))
  125. return SuccessResult();
  126. return FailureResult();
  127. }
  128. byte[] LogoutRegionAgents(Dictionary<string, object> request)
  129. {
  130. UUID region = UUID.Zero;
  131. if (!request.ContainsKey("RegionID"))
  132. return FailureResult();
  133. if (!UUID.TryParse(request["RegionID"].ToString(), out region))
  134. return FailureResult();
  135. if (m_PresenceService.LogoutRegionAgents(region))
  136. return SuccessResult();
  137. return FailureResult();
  138. }
  139. byte[] Report(Dictionary<string, object> request)
  140. {
  141. UUID session = UUID.Zero;
  142. UUID region = UUID.Zero;
  143. if (!request.ContainsKey("SessionID") || !request.ContainsKey("RegionID"))
  144. return FailureResult();
  145. if (!UUID.TryParse(request["SessionID"].ToString(), out session))
  146. return FailureResult();
  147. if (!UUID.TryParse(request["RegionID"].ToString(), out region))
  148. return FailureResult();
  149. if (m_PresenceService.ReportAgent(session, region))
  150. {
  151. return SuccessResult();
  152. }
  153. return FailureResult();
  154. }
  155. byte[] GetAgent(Dictionary<string, object> request)
  156. {
  157. UUID session = UUID.Zero;
  158. if (!request.ContainsKey("SessionID"))
  159. return FailureResult();
  160. if (!UUID.TryParse(request["SessionID"].ToString(), out session))
  161. return FailureResult();
  162. PresenceInfo pinfo = m_PresenceService.GetAgent(session);
  163. Dictionary<string, object> result = new Dictionary<string, object>();
  164. if (pinfo == null)
  165. result["result"] = "null";
  166. else
  167. result["result"] = pinfo.ToKeyValuePairs();
  168. string xmlString = ServerUtils.BuildXmlResponse(result);
  169. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  170. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  171. }
  172. byte[] GetAgents(Dictionary<string, object> request)
  173. {
  174. string[] userIDs;
  175. if (!request.ContainsKey("uuids"))
  176. {
  177. m_log.DebugFormat("[PRESENCE HANDLER]: GetAgents called without required uuids argument");
  178. return FailureResult();
  179. }
  180. if (!(request["uuids"] is List<string>))
  181. {
  182. m_log.DebugFormat("[PRESENCE HANDLER]: GetAgents input argument was of unexpected type {0}", request["uuids"].GetType().ToString());
  183. return FailureResult();
  184. }
  185. userIDs = ((List<string>)request["uuids"]).ToArray();
  186. PresenceInfo[] pinfos = m_PresenceService.GetAgents(userIDs);
  187. Dictionary<string, object> result = new Dictionary<string, object>();
  188. if ((pinfos == null) || ((pinfos != null) && (pinfos.Length == 0)))
  189. result["result"] = "null";
  190. else
  191. {
  192. int i = 0;
  193. foreach (PresenceInfo pinfo in pinfos)
  194. {
  195. Dictionary<string, object> rinfoDict = pinfo.ToKeyValuePairs();
  196. result["presence" + i] = rinfoDict;
  197. i++;
  198. }
  199. }
  200. string xmlString = ServerUtils.BuildXmlResponse(result);
  201. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  202. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  203. }
  204. private byte[] SuccessResult()
  205. {
  206. XmlDocument doc = new XmlDocument();
  207. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  208. "", "");
  209. doc.AppendChild(xmlnode);
  210. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  211. "");
  212. doc.AppendChild(rootElement);
  213. XmlElement result = doc.CreateElement("", "result", "");
  214. result.AppendChild(doc.CreateTextNode("Success"));
  215. rootElement.AppendChild(result);
  216. return Util.DocToBytes(doc);
  217. }
  218. private byte[] FailureResult()
  219. {
  220. XmlDocument doc = new XmlDocument();
  221. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  222. "", "");
  223. doc.AppendChild(xmlnode);
  224. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  225. "");
  226. doc.AppendChild(rootElement);
  227. XmlElement result = doc.CreateElement("", "result", "");
  228. result.AppendChild(doc.CreateTextNode("Failure"));
  229. rootElement.AppendChild(result);
  230. return Util.DocToBytes(doc);
  231. }
  232. }
  233. }