HGEntityTransferModule.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. InitialiseCommon(source);
  61. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: {0} enabled.", Name);
  62. }
  63. }
  64. }
  65. public override void AddRegion(Scene scene)
  66. {
  67. base.AddRegion(scene);
  68. if (m_Enabled)
  69. {
  70. scene.RegisterModuleInterface<IUserAgentVerificationModule>(this);
  71. }
  72. }
  73. protected override void OnNewClient(IClientAPI client)
  74. {
  75. client.OnTeleportHomeRequest += TeleportHome;
  76. client.OnTeleportLandmarkRequest += RequestTeleportLandmark;
  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. GridRegion real_destination = m_GatekeeperConnector.GetHyperlinkRegion(region, region.RegionID);
  107. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: GetFinalDestination serveruri -> {0}", real_destination.ServerURI);
  108. return real_destination;
  109. }
  110. return region;
  111. }
  112. protected override bool NeedsClosing(float drawdist, uint oldRegionX, uint newRegionX, uint oldRegionY, uint newRegionY, GridRegion reg)
  113. {
  114. if (base.NeedsClosing(drawdist, oldRegionX, newRegionX, oldRegionY, newRegionY, reg))
  115. return true;
  116. int flags = m_aScene.GridService.GetRegionFlags(m_aScene.RegionInfo.ScopeID, reg.RegionID);
  117. if (flags == -1 /* no region in DB */ || (flags & (int)OpenSim.Data.RegionFlags.Hyperlink) != 0)
  118. return true;
  119. return false;
  120. }
  121. protected override void AgentHasMovedAway(ScenePresence sp, bool logout)
  122. {
  123. base.AgentHasMovedAway(sp, logout);
  124. if (logout)
  125. {
  126. // Log them out of this grid
  127. m_aScene.PresenceService.LogoutAgent(sp.ControllingClient.SessionId);
  128. }
  129. }
  130. protected override bool CreateAgent(ScenePresence sp, GridRegion reg, GridRegion finalDestination, AgentCircuitData agentCircuit, uint teleportFlags, out string reason, out bool logout)
  131. {
  132. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: CreateAgent {0} {1}", reg.ServerURI, finalDestination.ServerURI);
  133. reason = string.Empty;
  134. logout = false;
  135. int flags = m_aScene.GridService.GetRegionFlags(m_aScene.RegionInfo.ScopeID, reg.RegionID);
  136. if (flags == -1 /* no region in DB */ || (flags & (int)OpenSim.Data.RegionFlags.Hyperlink) != 0)
  137. {
  138. // this user is going to another grid
  139. if (agentCircuit.ServiceURLs.ContainsKey("HomeURI"))
  140. {
  141. string userAgentDriver = agentCircuit.ServiceURLs["HomeURI"].ToString();
  142. IUserAgentService connector = new UserAgentServiceConnector(userAgentDriver);
  143. bool success = connector.LoginAgentToGrid(agentCircuit, reg, finalDestination, out reason);
  144. logout = success; // flag for later logout from this grid; this is an HG TP
  145. return success;
  146. }
  147. else
  148. {
  149. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Agent does not have a HomeURI address");
  150. return false;
  151. }
  152. }
  153. return m_aScene.SimulationService.CreateAgent(reg, agentCircuit, teleportFlags, out reason);
  154. }
  155. public override void TeleportHome(UUID id, IClientAPI client)
  156. {
  157. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Request to teleport {0} {1} home", client.FirstName, client.LastName);
  158. // Let's find out if this is a foreign user or a local user
  159. UserAccount account = m_aScene.UserAccountService.GetUserAccount(m_aScene.RegionInfo.ScopeID, id);
  160. if (account != null)
  161. {
  162. // local grid user
  163. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: User is local");
  164. base.TeleportHome(id, client);
  165. return;
  166. }
  167. // Foreign user wants to go home
  168. //
  169. AgentCircuitData aCircuit = ((Scene)(client.Scene)).AuthenticateHandler.GetAgentCircuitData(client.CircuitCode);
  170. if (aCircuit == null || (aCircuit != null && !aCircuit.ServiceURLs.ContainsKey("HomeURI")))
  171. {
  172. client.SendTeleportFailed("Your information has been lost");
  173. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Unable to locate agent's gateway information");
  174. return;
  175. }
  176. IUserAgentService userAgentService = new UserAgentServiceConnector(aCircuit.ServiceURLs["HomeURI"].ToString());
  177. Vector3 position = Vector3.UnitY, lookAt = Vector3.UnitY;
  178. GridRegion finalDestination = userAgentService.GetHomeRegion(aCircuit.AgentID, out position, out lookAt);
  179. if (finalDestination == null)
  180. {
  181. client.SendTeleportFailed("Your home region could not be found");
  182. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Agent's home region not found");
  183. return;
  184. }
  185. ScenePresence sp = ((Scene)(client.Scene)).GetScenePresence(client.AgentId);
  186. if (sp == null)
  187. {
  188. client.SendTeleportFailed("Internal error");
  189. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Agent not found in the scene where it is supposed to be");
  190. return;
  191. }
  192. IEventQueue eq = sp.Scene.RequestModuleInterface<IEventQueue>();
  193. GridRegion homeGatekeeper = MakeRegion(aCircuit);
  194. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: teleporting user {0} {1} home to {2} via {3}:{4}:{5}",
  195. aCircuit.firstname, aCircuit.lastname, finalDestination.RegionName, homeGatekeeper.ExternalHostName, homeGatekeeper.HttpPort, homeGatekeeper.RegionName);
  196. DoTeleport(sp, homeGatekeeper, finalDestination, position, lookAt, (uint)(Constants.TeleportFlags.SetLastToTarget | Constants.TeleportFlags.ViaHome), eq);
  197. }
  198. /// <summary>
  199. /// Tries to teleport agent to landmark.
  200. /// </summary>
  201. /// <param name="remoteClient"></param>
  202. /// <param name="regionHandle"></param>
  203. /// <param name="position"></param>
  204. public override void RequestTeleportLandmark(IClientAPI remoteClient, AssetLandmark lm)
  205. {
  206. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Teleporting agent via landmark to {0} region {1} position {2}",
  207. (lm.Gatekeeper == string.Empty) ? "local" : lm.Gatekeeper, lm.RegionID, lm.Position);
  208. if (lm.Gatekeeper == string.Empty)
  209. {
  210. base.RequestTeleportLandmark(remoteClient, lm);
  211. return;
  212. }
  213. GridRegion info = m_aScene.GridService.GetRegionByUUID(UUID.Zero, lm.RegionID);
  214. // Local region?
  215. if (info != null)
  216. {
  217. ((Scene)(remoteClient.Scene)).RequestTeleportLocation(remoteClient, info.RegionHandle, lm.Position,
  218. Vector3.Zero, (uint)(Constants.TeleportFlags.SetLastToTarget | Constants.TeleportFlags.ViaLandmark));
  219. return;
  220. }
  221. else
  222. {
  223. // Foreign region
  224. Scene scene = (Scene)(remoteClient.Scene);
  225. GatekeeperServiceConnector gConn = new GatekeeperServiceConnector();
  226. GridRegion gatekeeper = new GridRegion();
  227. gatekeeper.ServerURI = lm.Gatekeeper;
  228. GridRegion finalDestination = gConn.GetHyperlinkRegion(gatekeeper, new UUID(lm.RegionID));
  229. if (finalDestination != null)
  230. {
  231. ScenePresence sp = scene.GetScenePresence(remoteClient.AgentId);
  232. IEntityTransferModule transferMod = scene.RequestModuleInterface<IEntityTransferModule>();
  233. IEventQueue eq = sp.Scene.RequestModuleInterface<IEventQueue>();
  234. if (transferMod != null && sp != null && eq != null)
  235. transferMod.DoTeleport(sp, gatekeeper, finalDestination, lm.Position,
  236. Vector3.UnitX, (uint)(Constants.TeleportFlags.SetLastToTarget | Constants.TeleportFlags.ViaLandmark), eq);
  237. }
  238. }
  239. // can't find the region: Tell viewer and abort
  240. remoteClient.SendTeleportFailed("The teleport destination could not be found.");
  241. }
  242. #endregion
  243. #region IUserAgentVerificationModule
  244. public bool VerifyClient(AgentCircuitData aCircuit, string token)
  245. {
  246. if (aCircuit.ServiceURLs.ContainsKey("HomeURI"))
  247. {
  248. string url = aCircuit.ServiceURLs["HomeURI"].ToString();
  249. IUserAgentService security = new UserAgentServiceConnector(url);
  250. return security.VerifyClient(aCircuit.SessionID, token);
  251. }
  252. else
  253. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Agent {0} {1} does not have a HomeURI OH NO!", aCircuit.firstname, aCircuit.lastname);
  254. return false;
  255. }
  256. void OnConnectionClosed(IClientAPI obj)
  257. {
  258. if (obj.IsLoggingOut)
  259. {
  260. object sp = null;
  261. if (obj.Scene.TryGetScenePresence(obj.AgentId, out sp))
  262. {
  263. if (((ScenePresence)sp).IsChildAgent)
  264. return;
  265. }
  266. // Let's find out if this is a foreign user or a local user
  267. UserAccount account = m_aScene.UserAccountService.GetUserAccount(m_aScene.RegionInfo.ScopeID, obj.AgentId);
  268. if (account != null)
  269. {
  270. // local grid user
  271. return;
  272. }
  273. AgentCircuitData aCircuit = ((Scene)(obj.Scene)).AuthenticateHandler.GetAgentCircuitData(obj.CircuitCode);
  274. if (aCircuit.ServiceURLs.ContainsKey("HomeURI"))
  275. {
  276. string url = aCircuit.ServiceURLs["HomeURI"].ToString();
  277. IUserAgentService security = new UserAgentServiceConnector(url);
  278. security.LogoutAgent(obj.AgentId, obj.SessionId);
  279. //m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Sent logout call to UserAgentService @ {0}", url);
  280. }
  281. else
  282. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: HomeURI not found for agent {0} logout", obj.AgentId);
  283. }
  284. }
  285. #endregion
  286. private GridRegion MakeRegion(AgentCircuitData aCircuit)
  287. {
  288. GridRegion region = new GridRegion();
  289. Uri uri = null;
  290. if (!aCircuit.ServiceURLs.ContainsKey("HomeURI") ||
  291. (aCircuit.ServiceURLs.ContainsKey("HomeURI") && !Uri.TryCreate(aCircuit.ServiceURLs["HomeURI"].ToString(), UriKind.Absolute, out uri)))
  292. return null;
  293. region.ExternalHostName = uri.Host;
  294. region.HttpPort = (uint)uri.Port;
  295. region.RegionName = string.Empty;
  296. region.InternalEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("0.0.0.0"), (int)0);
  297. return region;
  298. }
  299. }
  300. }