HGEntityTransferModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.Framework.Interfaces;
  32. using OpenSim.Region.Framework.Scenes;
  33. using OpenSim.Services.Connectors.Hypergrid;
  34. using OpenSim.Services.Interfaces;
  35. using OpenSim.Server.Base;
  36. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  37. using OpenMetaverse;
  38. using log4net;
  39. using Nini.Config;
  40. namespace OpenSim.Region.CoreModules.Framework.EntityTransfer
  41. {
  42. public class HGEntityTransferModule : EntityTransferModule, ISharedRegionModule, IEntityTransferModule, IUserAgentVerificationModule
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private bool m_Initialized = false;
  46. private GatekeeperServiceConnector m_GatekeeperConnector;
  47. #region ISharedRegionModule
  48. public override string Name
  49. {
  50. get { return "HGEntityTransferModule"; }
  51. }
  52. public override void Initialise(IConfigSource source)
  53. {
  54. IConfig moduleConfig = source.Configs["Modules"];
  55. if (moduleConfig != null)
  56. {
  57. string name = moduleConfig.GetString("EntityTransferModule", "");
  58. if (name == Name)
  59. {
  60. m_agentsInTransit = new List<UUID>();
  61. m_Enabled = true;
  62. m_log.InfoFormat("[HG ENTITY TRANSFER MODULE]: {0} enabled.", Name);
  63. }
  64. }
  65. }
  66. public override void AddRegion(Scene scene)
  67. {
  68. base.AddRegion(scene);
  69. if (m_Enabled)
  70. {
  71. scene.RegisterModuleInterface<IUserAgentVerificationModule>(this);
  72. }
  73. }
  74. protected override void OnNewClient(IClientAPI client)
  75. {
  76. client.OnTeleportHomeRequest += TeleportHome;
  77. client.OnConnectionClosed += new Action<IClientAPI>(OnConnectionClosed);
  78. }
  79. public override void RegionLoaded(Scene scene)
  80. {
  81. base.RegionLoaded(scene);
  82. if (m_Enabled)
  83. if (!m_Initialized)
  84. {
  85. m_GatekeeperConnector = new GatekeeperServiceConnector(scene.AssetService);
  86. m_Initialized = true;
  87. }
  88. }
  89. public override void RemoveRegion(Scene scene)
  90. {
  91. base.AddRegion(scene);
  92. if (m_Enabled)
  93. {
  94. scene.UnregisterModuleInterface<IUserAgentVerificationModule>(this);
  95. }
  96. }
  97. #endregion
  98. #region HG overrides of IEntiryTransferModule
  99. protected override GridRegion GetFinalDestination(GridRegion region)
  100. {
  101. int flags = m_aScene.GridService.GetRegionFlags(m_aScene.RegionInfo.ScopeID, region.RegionID);
  102. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: region {0} flags: {1}", region.RegionID, flags);
  103. if ((flags & (int)OpenSim.Data.RegionFlags.Hyperlink) != 0)
  104. {
  105. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Destination region {0} is hyperlink", region.RegionID);
  106. return m_GatekeeperConnector.GetHyperlinkRegion(region, region.RegionID);
  107. }
  108. return region;
  109. }
  110. protected override bool NeedsClosing(uint oldRegionX, uint newRegionX, uint oldRegionY, uint newRegionY, GridRegion reg)
  111. {
  112. if (base.NeedsClosing(oldRegionX, newRegionX, oldRegionY, newRegionY, reg))
  113. return true;
  114. int flags = m_aScene.GridService.GetRegionFlags(m_aScene.RegionInfo.ScopeID, reg.RegionID);
  115. if (flags == -1 /* no region in DB */ || (flags & (int)OpenSim.Data.RegionFlags.Hyperlink) != 0)
  116. return true;
  117. return false;
  118. }
  119. protected override bool CreateAgent(ScenePresence sp, GridRegion reg, GridRegion finalDestination, AgentCircuitData agentCircuit, uint teleportFlags, out string reason)
  120. {
  121. reason = string.Empty;
  122. int flags = m_aScene.GridService.GetRegionFlags(m_aScene.RegionInfo.ScopeID, reg.RegionID);
  123. if (flags == -1 /* no region in DB */ || (flags & (int)OpenSim.Data.RegionFlags.Hyperlink) != 0)
  124. {
  125. // this user is going to another grid
  126. if (agentCircuit.ServiceURLs.ContainsKey("HomeURI"))
  127. {
  128. string userAgentDriver = agentCircuit.ServiceURLs["HomeURI"].ToString();
  129. IUserAgentService connector = new UserAgentServiceConnector(userAgentDriver);
  130. bool success = connector.LoginAgentToGrid(agentCircuit, reg, finalDestination, out reason);
  131. if (success)
  132. // Log them out of this grid
  133. m_aScene.PresenceService.LogoutAgent(agentCircuit.SessionID);
  134. return success;
  135. }
  136. else
  137. {
  138. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Agent does not have a HomeURI address");
  139. return false;
  140. }
  141. }
  142. return m_aScene.SimulationService.CreateAgent(reg, agentCircuit, teleportFlags, out reason);
  143. }
  144. public override void TeleportHome(UUID id, IClientAPI client)
  145. {
  146. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Request to teleport {0} {1} home", client.FirstName, client.LastName);
  147. // Let's find out if this is a foreign user or a local user
  148. UserAccount account = m_aScene.UserAccountService.GetUserAccount(m_aScene.RegionInfo.ScopeID, id);
  149. if (account != null)
  150. {
  151. // local grid user
  152. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: User is local");
  153. base.TeleportHome(id, client);
  154. return;
  155. }
  156. // Foreign user wants to go home
  157. //
  158. AgentCircuitData aCircuit = ((Scene)(client.Scene)).AuthenticateHandler.GetAgentCircuitData(client.CircuitCode);
  159. if (aCircuit == null || (aCircuit != null && !aCircuit.ServiceURLs.ContainsKey("HomeURI")))
  160. {
  161. client.SendTeleportFailed("Your information has been lost");
  162. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Unable to locate agent's gateway information");
  163. return;
  164. }
  165. IUserAgentService userAgentService = new UserAgentServiceConnector(aCircuit.ServiceURLs["HomeURI"].ToString());
  166. Vector3 position = Vector3.UnitY, lookAt = Vector3.UnitY;
  167. GridRegion finalDestination = userAgentService.GetHomeRegion(aCircuit.AgentID, out position, out lookAt);
  168. if (finalDestination == null)
  169. {
  170. client.SendTeleportFailed("Your home region could not be found");
  171. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Agent's home region not found");
  172. return;
  173. }
  174. ScenePresence sp = ((Scene)(client.Scene)).GetScenePresence(client.AgentId);
  175. if (sp == null)
  176. {
  177. client.SendTeleportFailed("Internal error");
  178. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Agent not found in the scene where it is supposed to be");
  179. return;
  180. }
  181. IEventQueue eq = sp.Scene.RequestModuleInterface<IEventQueue>();
  182. GridRegion homeGatekeeper = MakeRegion(aCircuit);
  183. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: teleporting user {0} {1} home to {2} via {3}:{4}:{5}",
  184. aCircuit.firstname, aCircuit.lastname, finalDestination.RegionName, homeGatekeeper.ExternalHostName, homeGatekeeper.HttpPort, homeGatekeeper.RegionName);
  185. DoTeleport(sp, homeGatekeeper, finalDestination, position, lookAt, (uint)(Constants.TeleportFlags.SetLastToTarget | Constants.TeleportFlags.ViaHome), eq);
  186. }
  187. #endregion
  188. #region IUserAgentVerificationModule
  189. public bool VerifyClient(AgentCircuitData aCircuit, string token)
  190. {
  191. if (aCircuit.ServiceURLs.ContainsKey("HomeURI"))
  192. {
  193. string url = aCircuit.ServiceURLs["HomeURI"].ToString();
  194. IUserAgentService security = new UserAgentServiceConnector(url);
  195. return security.VerifyClient(aCircuit.SessionID, token);
  196. }
  197. return false;
  198. }
  199. void OnConnectionClosed(IClientAPI obj)
  200. {
  201. if (obj.IsLoggingOut)
  202. {
  203. object sp = null;
  204. if (obj.Scene.TryGetScenePresence(obj.AgentId, out sp))
  205. {
  206. if (((ScenePresence)sp).IsChildAgent)
  207. return;
  208. }
  209. // Let's find out if this is a foreign user or a local user
  210. UserAccount account = m_aScene.UserAccountService.GetUserAccount(m_aScene.RegionInfo.ScopeID, obj.AgentId);
  211. if (account != null)
  212. {
  213. // local grid user
  214. return;
  215. }
  216. AgentCircuitData aCircuit = ((Scene)(obj.Scene)).AuthenticateHandler.GetAgentCircuitData(obj.CircuitCode);
  217. if (aCircuit.ServiceURLs.ContainsKey("HomeURI"))
  218. {
  219. string url = aCircuit.ServiceURLs["HomeURI"].ToString();
  220. IUserAgentService security = new UserAgentServiceConnector(url);
  221. security.LogoutAgent(obj.AgentId, obj.SessionId);
  222. //m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Sent logout call to UserAgentService @ {0}", url);
  223. }
  224. else
  225. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: HomeURI not found for agent {0} logout", obj.AgentId);
  226. }
  227. }
  228. #endregion
  229. private GridRegion MakeRegion(AgentCircuitData aCircuit)
  230. {
  231. GridRegion region = new GridRegion();
  232. Uri uri = null;
  233. if (!aCircuit.ServiceURLs.ContainsKey("HomeURI") ||
  234. (aCircuit.ServiceURLs.ContainsKey("HomeURI") && !Uri.TryCreate(aCircuit.ServiceURLs["HomeURI"].ToString(), UriKind.Absolute, out uri)))
  235. return null;
  236. region.ExternalHostName = uri.Host;
  237. region.HttpPort = (uint)uri.Port;
  238. region.RegionName = string.Empty;
  239. region.InternalEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("0.0.0.0"), (int)0);
  240. return region;
  241. }
  242. }
  243. }