HGEntityTransferModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 void AgentHasMovedAway(UUID sessionID, bool logout)
  120. {
  121. if (logout)
  122. // Log them out of this grid
  123. m_aScene.PresenceService.LogoutAgent(sessionID);
  124. }
  125. protected override bool CreateAgent(ScenePresence sp, GridRegion reg, GridRegion finalDestination, AgentCircuitData agentCircuit, uint teleportFlags, out string reason, out bool logout)
  126. {
  127. reason = string.Empty;
  128. logout = false;
  129. int flags = m_aScene.GridService.GetRegionFlags(m_aScene.RegionInfo.ScopeID, reg.RegionID);
  130. if (flags == -1 /* no region in DB */ || (flags & (int)OpenSim.Data.RegionFlags.Hyperlink) != 0)
  131. {
  132. // this user is going to another grid
  133. if (agentCircuit.ServiceURLs.ContainsKey("HomeURI"))
  134. {
  135. string userAgentDriver = agentCircuit.ServiceURLs["HomeURI"].ToString();
  136. IUserAgentService connector = new UserAgentServiceConnector(userAgentDriver);
  137. bool success = connector.LoginAgentToGrid(agentCircuit, reg, finalDestination, out reason);
  138. logout = success; // flag for later logout from this grid; this is an HG TP
  139. return success;
  140. }
  141. else
  142. {
  143. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Agent does not have a HomeURI address");
  144. return false;
  145. }
  146. }
  147. return m_aScene.SimulationService.CreateAgent(reg, agentCircuit, teleportFlags, out reason);
  148. }
  149. public override void TeleportHome(UUID id, IClientAPI client)
  150. {
  151. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Request to teleport {0} {1} home", client.FirstName, client.LastName);
  152. // Let's find out if this is a foreign user or a local user
  153. UserAccount account = m_aScene.UserAccountService.GetUserAccount(m_aScene.RegionInfo.ScopeID, id);
  154. if (account != null)
  155. {
  156. // local grid user
  157. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: User is local");
  158. base.TeleportHome(id, client);
  159. return;
  160. }
  161. // Foreign user wants to go home
  162. //
  163. AgentCircuitData aCircuit = ((Scene)(client.Scene)).AuthenticateHandler.GetAgentCircuitData(client.CircuitCode);
  164. if (aCircuit == null || (aCircuit != null && !aCircuit.ServiceURLs.ContainsKey("HomeURI")))
  165. {
  166. client.SendTeleportFailed("Your information has been lost");
  167. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Unable to locate agent's gateway information");
  168. return;
  169. }
  170. IUserAgentService userAgentService = new UserAgentServiceConnector(aCircuit.ServiceURLs["HomeURI"].ToString());
  171. Vector3 position = Vector3.UnitY, lookAt = Vector3.UnitY;
  172. GridRegion finalDestination = userAgentService.GetHomeRegion(aCircuit.AgentID, out position, out lookAt);
  173. if (finalDestination == null)
  174. {
  175. client.SendTeleportFailed("Your home region could not be found");
  176. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Agent's home region not found");
  177. return;
  178. }
  179. ScenePresence sp = ((Scene)(client.Scene)).GetScenePresence(client.AgentId);
  180. if (sp == null)
  181. {
  182. client.SendTeleportFailed("Internal error");
  183. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Agent not found in the scene where it is supposed to be");
  184. return;
  185. }
  186. IEventQueue eq = sp.Scene.RequestModuleInterface<IEventQueue>();
  187. GridRegion homeGatekeeper = MakeRegion(aCircuit);
  188. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: teleporting user {0} {1} home to {2} via {3}:{4}:{5}",
  189. aCircuit.firstname, aCircuit.lastname, finalDestination.RegionName, homeGatekeeper.ExternalHostName, homeGatekeeper.HttpPort, homeGatekeeper.RegionName);
  190. DoTeleport(sp, homeGatekeeper, finalDestination, position, lookAt, (uint)(Constants.TeleportFlags.SetLastToTarget | Constants.TeleportFlags.ViaHome), eq);
  191. }
  192. #endregion
  193. #region IUserAgentVerificationModule
  194. public bool VerifyClient(AgentCircuitData aCircuit, string token)
  195. {
  196. if (aCircuit.ServiceURLs.ContainsKey("HomeURI"))
  197. {
  198. string url = aCircuit.ServiceURLs["HomeURI"].ToString();
  199. IUserAgentService security = new UserAgentServiceConnector(url);
  200. return security.VerifyClient(aCircuit.SessionID, token);
  201. }
  202. return false;
  203. }
  204. void OnConnectionClosed(IClientAPI obj)
  205. {
  206. if (obj.IsLoggingOut)
  207. {
  208. object sp = null;
  209. if (obj.Scene.TryGetScenePresence(obj.AgentId, out sp))
  210. {
  211. if (((ScenePresence)sp).IsChildAgent)
  212. return;
  213. }
  214. // Let's find out if this is a foreign user or a local user
  215. UserAccount account = m_aScene.UserAccountService.GetUserAccount(m_aScene.RegionInfo.ScopeID, obj.AgentId);
  216. if (account != null)
  217. {
  218. // local grid user
  219. return;
  220. }
  221. AgentCircuitData aCircuit = ((Scene)(obj.Scene)).AuthenticateHandler.GetAgentCircuitData(obj.CircuitCode);
  222. if (aCircuit.ServiceURLs.ContainsKey("HomeURI"))
  223. {
  224. string url = aCircuit.ServiceURLs["HomeURI"].ToString();
  225. IUserAgentService security = new UserAgentServiceConnector(url);
  226. security.LogoutAgent(obj.AgentId, obj.SessionId);
  227. //m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Sent logout call to UserAgentService @ {0}", url);
  228. }
  229. else
  230. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: HomeURI not found for agent {0} logout", obj.AgentId);
  231. }
  232. }
  233. #endregion
  234. private GridRegion MakeRegion(AgentCircuitData aCircuit)
  235. {
  236. GridRegion region = new GridRegion();
  237. Uri uri = null;
  238. if (!aCircuit.ServiceURLs.ContainsKey("HomeURI") ||
  239. (aCircuit.ServiceURLs.ContainsKey("HomeURI") && !Uri.TryCreate(aCircuit.ServiceURLs["HomeURI"].ToString(), UriKind.Absolute, out uri)))
  240. return null;
  241. region.ExternalHostName = uri.Host;
  242. region.HttpPort = (uint)uri.Port;
  243. region.RegionName = string.Empty;
  244. region.InternalEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("0.0.0.0"), (int)0);
  245. return region;
  246. }
  247. }
  248. }