HGStandaloneAssetService.cs 7.2 KB

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