HGStandaloneAssetService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.Reflection;
  29. using log4net;
  30. using Nini.Config;
  31. using OpenMetaverse;
  32. using OpenSim.Data;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications;
  35. using OpenSim.Framework.Communications.Cache;
  36. using OpenSim.Framework.Servers;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. namespace OpenSim.Region.CoreModules.Hypergrid
  40. {
  41. public class HGStandaloneAssetService : IRegionModule
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private static bool initialized = false;
  45. private static bool enabled = false;
  46. Scene m_scene;
  47. //AssetService m_assetService;
  48. #region IRegionModule interface
  49. public void Initialise(Scene scene, IConfigSource config)
  50. {
  51. if (!initialized)
  52. {
  53. initialized = true;
  54. m_scene = scene;
  55. // This module is only on for standalones in hypergrid mode
  56. enabled = !config.Configs["Startup"].GetBoolean("gridmode", true) && config.Configs["Startup"].GetBoolean("hypergrid", false);
  57. }
  58. }
  59. public void PostInitialise()
  60. {
  61. if (enabled)
  62. {
  63. m_log.Info("[HGStandaloneAssetService]: Starting...");
  64. //m_assetService = new AssetService(m_scene);
  65. new AssetService(m_scene);
  66. }
  67. }
  68. public void Close()
  69. {
  70. }
  71. public string Name
  72. {
  73. get { return "HGStandaloneAssetService"; }
  74. }
  75. public bool IsSharedModule
  76. {
  77. get { return true; }
  78. }
  79. #endregion
  80. }
  81. public class AssetService
  82. {
  83. private IUserService m_userService;
  84. private bool m_doLookup = false;
  85. public bool DoLookup
  86. {
  87. get { return m_doLookup; }
  88. set { m_doLookup = value; }
  89. }
  90. private static readonly ILog m_log
  91. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  92. public AssetService(Scene m_scene)
  93. {
  94. AddHttpHandlers(m_scene);
  95. m_userService = m_scene.CommsManager.UserService;
  96. }
  97. protected void AddHttpHandlers(Scene m_scene)
  98. {
  99. IAssetDataPlugin m_assetProvider
  100. = ((AssetServerBase)m_scene.CommsManager.AssetCache.AssetServer).AssetProviderPlugin;
  101. BaseHttpServer httpServer = m_scene.CommsManager.HttpServer;
  102. httpServer.AddStreamHandler(new GetAssetStreamHandler(m_assetProvider));
  103. httpServer.AddStreamHandler(new PostAssetStreamHandler(m_assetProvider));
  104. }
  105. ///// <summary>
  106. ///// Check that the source of an inventory request is one that we trust.
  107. ///// </summary>
  108. ///// <param name="peer"></param>
  109. ///// <returns></returns>
  110. //public bool CheckTrustSource(IPEndPoint peer)
  111. //{
  112. // if (m_doLookup)
  113. // {
  114. // m_log.InfoFormat("[GRID AGENT INVENTORY]: Checking trusted source {0}", peer);
  115. // UriBuilder ub = new UriBuilder(m_userserver_url);
  116. // IPAddress[] uaddrs = Dns.GetHostAddresses(ub.Host);
  117. // foreach (IPAddress uaddr in uaddrs)
  118. // {
  119. // if (uaddr.Equals(peer.Address))
  120. // {
  121. // return true;
  122. // }
  123. // }
  124. // m_log.WarnFormat(
  125. // "[GRID AGENT INVENTORY]: Rejecting request since source {0} was not in the list of trusted sources",
  126. // peer);
  127. // return false;
  128. // }
  129. // else
  130. // {
  131. // return true;
  132. // }
  133. //}
  134. /// <summary>
  135. /// Check that the source of an inventory request for a particular agent is a current session belonging to
  136. /// that agent.
  137. /// </summary>
  138. /// <param name="session_id"></param>
  139. /// <param name="avatar_id"></param>
  140. /// <returns></returns>
  141. public bool CheckAuthSession(string session_id, string avatar_id)
  142. {
  143. if (m_doLookup)
  144. {
  145. m_log.InfoFormat("[HGStandaloneInvService]: checking authed session {0} {1}", session_id, avatar_id);
  146. UUID userID = UUID.Zero;
  147. UUID sessionID = UUID.Zero;
  148. UUID.TryParse(avatar_id, out userID);
  149. UUID.TryParse(session_id, out sessionID);
  150. if (userID.Equals(UUID.Zero) || sessionID.Equals(UUID.Zero))
  151. {
  152. m_log.Info("[HGStandaloneInvService]: Invalid user or session id " + avatar_id + "; " + session_id);
  153. return false;
  154. }
  155. UserProfileData userProfile = m_userService.GetUserProfile(userID);
  156. if (userProfile != null && userProfile.CurrentAgent != null &&
  157. userProfile.CurrentAgent.SessionID == sessionID)
  158. {
  159. m_log.Info("[HGStandaloneInvService]: user is logged in and session is valid. Authorizing access.");
  160. return true;
  161. }
  162. m_log.Warn("[HGStandaloneInvService]: unknown user or session_id, request rejected");
  163. return false;
  164. }
  165. else
  166. {
  167. return true;
  168. }
  169. }
  170. }
  171. }