HGStandaloneInventoryService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**
  2. * Copyright (c) 2008, Contributors. All rights reserved.
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * * Neither the name of the Organizations nor the names of Individual
  14. * Contributors may be used to endorse or promote products derived from
  15. * this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  20. * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  22. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  23. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  25. * OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Net;
  31. using System.Reflection;
  32. using log4net;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Communications;
  37. using OpenSim.Framework.Communications.Cache;
  38. using OpenSim.Framework.Servers;
  39. using OpenSim.Region.Environment.Interfaces;
  40. using OpenSim.Region.Environment.Scenes;
  41. namespace OpenSim.Region.Environment.Modules.Hypergrid
  42. {
  43. public class HGStandaloneInventoryService : IRegionModule
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private static bool initialized = false;
  47. private static bool enabled = false;
  48. Scene m_scene;
  49. //InventoryService m_inventoryService;
  50. #region IRegionModule interface
  51. public void Initialise(Scene scene, IConfigSource config)
  52. {
  53. if (!initialized)
  54. {
  55. initialized = true;
  56. m_scene = scene;
  57. // This module is only on for standalones
  58. enabled = !config.Configs["Startup"].GetBoolean("gridmode", true) && config.Configs["Startup"].GetBoolean("hypergrid", false);
  59. }
  60. }
  61. public void PostInitialise()
  62. {
  63. if (enabled)
  64. {
  65. m_log.Info("[HGStandaloneInvService]: Starting...");
  66. //m_inventoryService = new InventoryService(m_scene);
  67. new InventoryService(m_scene);
  68. }
  69. }
  70. public void Close()
  71. {
  72. }
  73. public string Name
  74. {
  75. get { return "HGStandaloneInventoryService"; }
  76. }
  77. public bool IsSharedModule
  78. {
  79. get { return true; }
  80. }
  81. #endregion
  82. }
  83. public class InventoryService
  84. {
  85. private static readonly ILog m_log
  86. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  87. private InventoryServiceBase m_inventoryService;
  88. private IUserService m_userService;
  89. private bool m_doLookup = false;
  90. public bool DoLookup
  91. {
  92. get { return m_doLookup; }
  93. set { m_doLookup = value; }
  94. }
  95. public InventoryService(Scene m_scene)
  96. {
  97. m_inventoryService = (InventoryServiceBase)m_scene.CommsManager.SecureInventoryService;
  98. m_userService = m_scene.CommsManager.UserService;
  99. AddHttpHandlers(m_scene);
  100. }
  101. protected void AddHttpHandlers(Scene m_scene)
  102. {
  103. BaseHttpServer httpServer = m_scene.CommsManager.HttpServer;
  104. httpServer.AddStreamHandler(
  105. new RestDeserialiseSecureHandler<Guid, InventoryCollection>(
  106. "POST", "/GetInventory/", GetUserInventory, CheckAuthSession));
  107. httpServer.AddStreamHandler(
  108. new RestDeserialiseSecureHandler<InventoryFolderBase, bool>(
  109. "POST", "/NewFolder/", m_inventoryService.AddFolder, CheckAuthSession));
  110. httpServer.AddStreamHandler(
  111. new RestDeserialiseSecureHandler<InventoryFolderBase, bool>(
  112. "POST", "/UpdateFolder/", m_inventoryService.UpdateFolder, CheckAuthSession));
  113. httpServer.AddStreamHandler(
  114. new RestDeserialiseSecureHandler<InventoryFolderBase, bool>(
  115. "POST", "/MoveFolder/", m_inventoryService.MoveFolder, CheckAuthSession));
  116. httpServer.AddStreamHandler(
  117. new RestDeserialiseSecureHandler<InventoryFolderBase, bool>(
  118. "POST", "/PurgeFolder/", m_inventoryService.PurgeFolder, CheckAuthSession));
  119. httpServer.AddStreamHandler(
  120. new RestDeserialiseSecureHandler<InventoryItemBase, bool>(
  121. "POST", "/NewItem/", m_inventoryService.AddItem, CheckAuthSession));
  122. httpServer.AddStreamHandler(
  123. new RestDeserialiseSecureHandler<InventoryItemBase, bool>(
  124. "POST", "/DeleteItem/", m_inventoryService.DeleteItem, CheckAuthSession));
  125. //// WARNING: Root folders no longer just delivers the root and immediate child folders (e.g
  126. //// system folders such as Objects, Textures), but it now returns the entire inventory skeleton.
  127. //// It would have been better to rename this request, but complexities in the BaseHttpServer
  128. //// (e.g. any http request not found is automatically treated as an xmlrpc request) make it easier
  129. //// to do this for now.
  130. //m_scene.AddStreamHandler(
  131. // new RestDeserialiseTrustedHandler<Guid, List<InventoryFolderBase>>
  132. // ("POST", "/RootFolders/", GetInventorySkeleton, CheckTrustSource));
  133. //// for persistent active gestures
  134. //m_scene.AddStreamHandler(
  135. // new RestDeserialiseTrustedHandler<Guid, List<InventoryItemBase>>
  136. // ("POST", "/ActiveGestures/", GetActiveGestures, CheckTrustSource));
  137. }
  138. ///// <summary>
  139. ///// Check that the source of an inventory request is one that we trust.
  140. ///// </summary>
  141. ///// <param name="peer"></param>
  142. ///// <returns></returns>
  143. //public bool CheckTrustSource(IPEndPoint peer)
  144. //{
  145. // if (m_doLookup)
  146. // {
  147. // m_log.InfoFormat("[GRID AGENT INVENTORY]: Checking trusted source {0}", peer);
  148. // UriBuilder ub = new UriBuilder(m_userserver_url);
  149. // IPAddress[] uaddrs = Dns.GetHostAddresses(ub.Host);
  150. // foreach (IPAddress uaddr in uaddrs)
  151. // {
  152. // if (uaddr.Equals(peer.Address))
  153. // {
  154. // return true;
  155. // }
  156. // }
  157. // m_log.WarnFormat(
  158. // "[GRID AGENT INVENTORY]: Rejecting request since source {0} was not in the list of trusted sources",
  159. // peer);
  160. // return false;
  161. // }
  162. // else
  163. // {
  164. // return true;
  165. // }
  166. //}
  167. /// <summary>
  168. /// Check that the source of an inventory request for a particular agent is a current session belonging to
  169. /// that agent.
  170. /// </summary>
  171. /// <param name="session_id"></param>
  172. /// <param name="avatar_id"></param>
  173. /// <returns></returns>
  174. public bool CheckAuthSession(string session_id, string avatar_id)
  175. {
  176. if (m_doLookup)
  177. {
  178. m_log.InfoFormat("[HGStandaloneInvService]: checking authed session {0} {1}", session_id, avatar_id);
  179. UUID userID = UUID.Zero;
  180. UUID sessionID = UUID.Zero;
  181. UUID.TryParse(avatar_id, out userID);
  182. UUID.TryParse(session_id, out sessionID);
  183. if (userID.Equals(UUID.Zero) || sessionID.Equals(UUID.Zero))
  184. {
  185. m_log.Info("[HGStandaloneInvService]: Invalid user or session id " + avatar_id + "; " + session_id);
  186. return false;
  187. }
  188. UserProfileData userProfile = m_userService.GetUserProfile(userID);
  189. if (userProfile != null && userProfile.CurrentAgent != null &&
  190. userProfile.CurrentAgent.SessionID == sessionID)
  191. {
  192. m_log.Info("[HGStandaloneInvService]: user is logged in and session is valid. Authorizing access.");
  193. return true;
  194. }
  195. m_log.Warn("[HGStandaloneInvService]: unknown user or session_id, request rejected");
  196. return false;
  197. }
  198. else
  199. {
  200. return true;
  201. }
  202. }
  203. /// <summary>
  204. /// Return a user's entire inventory
  205. /// </summary>
  206. /// <param name="rawUserID"></param>
  207. /// <returns>The user's inventory. If an inventory cannot be found then an empty collection is returned.</returns>
  208. public InventoryCollection GetUserInventory(Guid rawUserID)
  209. {
  210. UUID userID = new UUID(rawUserID);
  211. m_log.Info("[HGStandaloneInvService]: Processing request for inventory of " + userID);
  212. // Uncomment me to simulate a slow responding inventory server
  213. //Thread.Sleep(16000);
  214. InventoryCollection invCollection = new InventoryCollection();
  215. List<InventoryFolderBase> allFolders = ((InventoryServiceBase)m_inventoryService).GetInventorySkeleton(userID);
  216. if (null == allFolders)
  217. {
  218. m_log.WarnFormat("[HGStandaloneInvService]: No inventory found for user {0}", rawUserID);
  219. return invCollection;
  220. }
  221. List<InventoryItemBase> allItems = new List<InventoryItemBase>();
  222. foreach (InventoryFolderBase folder in allFolders)
  223. {
  224. List<InventoryItemBase> items = ((InventoryServiceBase)m_inventoryService).RequestFolderItems(folder.ID);
  225. if (items != null)
  226. {
  227. allItems.InsertRange(0, items);
  228. }
  229. }
  230. invCollection.UserID = userID;
  231. invCollection.Folders = allFolders;
  232. invCollection.Items = allItems;
  233. // foreach (InventoryFolderBase folder in invCollection.Folders)
  234. // {
  235. // m_log.DebugFormat("[GRID AGENT INVENTORY]: Sending back folder {0} {1}", folder.Name, folder.ID);
  236. // }
  237. //
  238. // foreach (InventoryItemBase item in invCollection.Items)
  239. // {
  240. // m_log.DebugFormat("[GRID AGENT INVENTORY]: Sending back item {0} {1}, folder {2}", item.Name, item.ID, item.Folder);
  241. // }
  242. m_log.InfoFormat(
  243. "[HGStandaloneInvService]: Sending back inventory response to user {0} containing {1} folders and {2} items",
  244. invCollection.UserID, invCollection.Folders.Count, invCollection.Items.Count);
  245. return invCollection;
  246. }
  247. /// <summary>
  248. /// Guid to UUID wrapper for same name IInventoryServices method
  249. /// </summary>
  250. /// <param name="rawUserID"></param>
  251. /// <returns></returns>
  252. public List<InventoryFolderBase> GetInventorySkeleton(Guid rawUserID)
  253. {
  254. UUID userID = new UUID(rawUserID);
  255. return ((InventoryServiceBase)m_inventoryService).GetInventorySkeleton(userID);
  256. }
  257. public List<InventoryItemBase> GetActiveGestures(Guid rawUserID)
  258. {
  259. UUID userID = new UUID(rawUserID);
  260. m_log.InfoFormat("[HGStandaloneInvService]: fetching active gestures for user {0}", userID);
  261. return ((InventoryServiceBase)m_inventoryService).GetActiveGestures(userID);
  262. }
  263. }
  264. }