HGStandaloneAssetService.cs 7.1 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. m_scene.AddStreamHandler(new GetAssetStreamHandler(m_assetProvider));
  104. m_scene.AddStreamHandler(new PostAssetStreamHandler(m_assetProvider));
  105. }
  106. ///// <summary>
  107. ///// Check that the source of an inventory request is one that we trust.
  108. ///// </summary>
  109. ///// <param name="peer"></param>
  110. ///// <returns></returns>
  111. //public bool CheckTrustSource(IPEndPoint peer)
  112. //{
  113. // if (m_doLookup)
  114. // {
  115. // m_log.InfoFormat("[GRID AGENT INVENTORY]: Checking trusted source {0}", peer);
  116. // UriBuilder ub = new UriBuilder(m_userserver_url);
  117. // IPAddress[] uaddrs = Dns.GetHostAddresses(ub.Host);
  118. // foreach (IPAddress uaddr in uaddrs)
  119. // {
  120. // if (uaddr.Equals(peer.Address))
  121. // {
  122. // return true;
  123. // }
  124. // }
  125. // m_log.WarnFormat(
  126. // "[GRID AGENT INVENTORY]: Rejecting request since source {0} was not in the list of trusted sources",
  127. // peer);
  128. // return false;
  129. // }
  130. // else
  131. // {
  132. // return true;
  133. // }
  134. //}
  135. /// <summary>
  136. /// Check that the source of an inventory request for a particular agent is a current session belonging to
  137. /// that agent.
  138. /// </summary>
  139. /// <param name="session_id"></param>
  140. /// <param name="avatar_id"></param>
  141. /// <returns></returns>
  142. public bool CheckAuthSession(string session_id, string avatar_id)
  143. {
  144. if (m_doLookup)
  145. {
  146. m_log.InfoFormat("[HGStandaloneInvService]: checking authed session {0} {1}", session_id, avatar_id);
  147. UUID userID = UUID.Zero;
  148. UUID sessionID = UUID.Zero;
  149. UUID.TryParse(avatar_id, out userID);
  150. UUID.TryParse(session_id, out sessionID);
  151. if (userID.Equals(UUID.Zero) || sessionID.Equals(UUID.Zero))
  152. {
  153. m_log.Info("[HGStandaloneInvService]: Invalid user or session id " + avatar_id + "; " + session_id);
  154. return false;
  155. }
  156. UserProfileData userProfile = m_userService.GetUserProfile(userID);
  157. if (userProfile != null && userProfile.CurrentAgent != null &&
  158. userProfile.CurrentAgent.SessionID == sessionID)
  159. {
  160. m_log.Info("[HGStandaloneInvService]: user is logged in and session is valid. Authorizing access.");
  161. return true;
  162. }
  163. m_log.Warn("[HGStandaloneInvService]: unknown user or session_id, request rejected");
  164. return false;
  165. }
  166. else
  167. {
  168. return true;
  169. }
  170. }
  171. }
  172. }