HGStandaloneInventoryService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 InventoryServiceBase m_inventoryService;
  86. private IUserService m_userService;
  87. private bool m_doLookup = false;
  88. public bool DoLookup
  89. {
  90. get { return m_doLookup; }
  91. set { m_doLookup = value; }
  92. }
  93. private static readonly ILog m_log
  94. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  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. m_scene.AddStreamHandler(
  104. new RestDeserialiseSecureHandler<Guid, InventoryCollection>(
  105. "POST", "/GetInventory/", GetUserInventory, CheckAuthSession));
  106. m_scene.AddStreamHandler(
  107. new RestDeserialiseSecureHandler<InventoryFolderBase, bool>(
  108. "POST", "/NewFolder/", m_inventoryService.AddFolder, CheckAuthSession));
  109. m_scene.AddStreamHandler(
  110. new RestDeserialiseSecureHandler<InventoryFolderBase, bool>(
  111. "POST", "/UpdateFolder/", m_inventoryService.UpdateFolder, CheckAuthSession));
  112. m_scene.AddStreamHandler(
  113. new RestDeserialiseSecureHandler<InventoryFolderBase, bool>(
  114. "POST", "/MoveFolder/", m_inventoryService.MoveFolder, CheckAuthSession));
  115. m_scene.AddStreamHandler(
  116. new RestDeserialiseSecureHandler<InventoryFolderBase, bool>(
  117. "POST", "/PurgeFolder/", m_inventoryService.PurgeFolder, CheckAuthSession));
  118. m_scene.AddStreamHandler(
  119. new RestDeserialiseSecureHandler<InventoryItemBase, bool>(
  120. "POST", "/NewItem/", m_inventoryService.AddItem, CheckAuthSession));
  121. m_scene.AddStreamHandler(
  122. new RestDeserialiseSecureHandler<InventoryItemBase, bool>(
  123. "POST", "/DeleteItem/", m_inventoryService.DeleteItem, CheckAuthSession));
  124. //// WARNING: Root folders no longer just delivers the root and immediate child folders (e.g
  125. //// system folders such as Objects, Textures), but it now returns the entire inventory skeleton.
  126. //// It would have been better to rename this request, but complexities in the BaseHttpServer
  127. //// (e.g. any http request not found is automatically treated as an xmlrpc request) make it easier
  128. //// to do this for now.
  129. //m_scene.AddStreamHandler(
  130. // new RestDeserialiseTrustedHandler<Guid, List<InventoryFolderBase>>
  131. // ("POST", "/RootFolders/", GetInventorySkeleton, CheckTrustSource));
  132. //// for persistent active gestures
  133. //m_scene.AddStreamHandler(
  134. // new RestDeserialiseTrustedHandler<Guid, List<InventoryItemBase>>
  135. // ("POST", "/ActiveGestures/", GetActiveGestures, CheckTrustSource));
  136. }
  137. ///// <summary>
  138. ///// Check that the source of an inventory request is one that we trust.
  139. ///// </summary>
  140. ///// <param name="peer"></param>
  141. ///// <returns></returns>
  142. //public bool CheckTrustSource(IPEndPoint peer)
  143. //{
  144. // if (m_doLookup)
  145. // {
  146. // m_log.InfoFormat("[GRID AGENT INVENTORY]: Checking trusted source {0}", peer);
  147. // UriBuilder ub = new UriBuilder(m_userserver_url);
  148. // IPAddress[] uaddrs = Dns.GetHostAddresses(ub.Host);
  149. // foreach (IPAddress uaddr in uaddrs)
  150. // {
  151. // if (uaddr.Equals(peer.Address))
  152. // {
  153. // return true;
  154. // }
  155. // }
  156. // m_log.WarnFormat(
  157. // "[GRID AGENT INVENTORY]: Rejecting request since source {0} was not in the list of trusted sources",
  158. // peer);
  159. // return false;
  160. // }
  161. // else
  162. // {
  163. // return true;
  164. // }
  165. //}
  166. /// <summary>
  167. /// Check that the source of an inventory request for a particular agent is a current session belonging to
  168. /// that agent.
  169. /// </summary>
  170. /// <param name="session_id"></param>
  171. /// <param name="avatar_id"></param>
  172. /// <returns></returns>
  173. public bool CheckAuthSession(string session_id, string avatar_id)
  174. {
  175. if (m_doLookup)
  176. {
  177. m_log.InfoFormat("[HGStandaloneInvService]: checking authed session {0} {1}", session_id, avatar_id);
  178. UUID userID = UUID.Zero;
  179. UUID sessionID = UUID.Zero;
  180. UUID.TryParse(avatar_id, out userID);
  181. UUID.TryParse(session_id, out sessionID);
  182. if (userID.Equals(UUID.Zero) || sessionID.Equals(UUID.Zero))
  183. {
  184. m_log.Info("[HGStandaloneInvService]: Invalid user or session id " + avatar_id + "; " + session_id);
  185. return false;
  186. }
  187. UserProfileData userProfile = m_userService.GetUserProfile(userID);
  188. if (userProfile != null && userProfile.CurrentAgent != null &&
  189. userProfile.CurrentAgent.SessionID == sessionID)
  190. {
  191. m_log.Info("[HGStandaloneInvService]: user is logged in and session is valid. Authorizing access.");
  192. return true;
  193. }
  194. m_log.Warn("[HGStandaloneInvService]: unknown user or session_id, request rejected");
  195. return false;
  196. }
  197. else
  198. {
  199. return true;
  200. }
  201. }
  202. /// <summary>
  203. /// Return a user's entire inventory
  204. /// </summary>
  205. /// <param name="rawUserID"></param>
  206. /// <returns>The user's inventory. If an inventory cannot be found then an empty collection is returned.</returns>
  207. public InventoryCollection GetUserInventory(Guid rawUserID)
  208. {
  209. UUID userID = new UUID(rawUserID);
  210. m_log.Info("[HGStandaloneInvService]: Processing request for inventory of " + userID);
  211. // Uncomment me to simulate a slow responding inventory server
  212. //Thread.Sleep(16000);
  213. InventoryCollection invCollection = new InventoryCollection();
  214. List<InventoryFolderBase> allFolders = ((InventoryServiceBase)m_inventoryService).GetInventorySkeleton(userID);
  215. if (null == allFolders)
  216. {
  217. m_log.WarnFormat("[HGStandaloneInvService]: No inventory found for user {0}", rawUserID);
  218. return invCollection;
  219. }
  220. List<InventoryItemBase> allItems = new List<InventoryItemBase>();
  221. foreach (InventoryFolderBase folder in allFolders)
  222. {
  223. List<InventoryItemBase> items = ((InventoryServiceBase)m_inventoryService).RequestFolderItems(folder.ID);
  224. if (items != null)
  225. {
  226. allItems.InsertRange(0, items);
  227. }
  228. }
  229. invCollection.UserID = userID;
  230. invCollection.Folders = allFolders;
  231. invCollection.Items = allItems;
  232. // foreach (InventoryFolderBase folder in invCollection.Folders)
  233. // {
  234. // m_log.DebugFormat("[GRID AGENT INVENTORY]: Sending back folder {0} {1}", folder.Name, folder.ID);
  235. // }
  236. //
  237. // foreach (InventoryItemBase item in invCollection.Items)
  238. // {
  239. // m_log.DebugFormat("[GRID AGENT INVENTORY]: Sending back item {0} {1}, folder {2}", item.Name, item.ID, item.Folder);
  240. // }
  241. m_log.InfoFormat(
  242. "[HGStandaloneInvService]: Sending back inventory response to user {0} containing {1} folders and {2} items",
  243. invCollection.UserID, invCollection.Folders.Count, invCollection.Items.Count);
  244. return invCollection;
  245. }
  246. /// <summary>
  247. /// Guid to UUID wrapper for same name IInventoryServices method
  248. /// </summary>
  249. /// <param name="rawUserID"></param>
  250. /// <returns></returns>
  251. public List<InventoryFolderBase> GetInventorySkeleton(Guid rawUserID)
  252. {
  253. UUID userID = new UUID(rawUserID);
  254. return ((InventoryServiceBase)m_inventoryService).GetInventorySkeleton(userID);
  255. }
  256. public List<InventoryItemBase> GetActiveGestures(Guid rawUserID)
  257. {
  258. UUID userID = new UUID(rawUserID);
  259. m_log.InfoFormat("[HGStandaloneInvService]: fetching active gestures for user {0}", userID);
  260. return ((InventoryServiceBase)m_inventoryService).GetActiveGestures(userID);
  261. }
  262. }
  263. }