HGEntityTransferModule.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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
  43. : EntityTransferModule, INonSharedRegionModule, IEntityTransferModule, IUserAgentVerificationModule
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private int m_levelHGTeleport = 0;
  47. private GatekeeperServiceConnector m_GatekeeperConnector;
  48. #region ISharedRegionModule
  49. public override string Name
  50. {
  51. get { return "HGEntityTransferModule"; }
  52. }
  53. public override void Initialise(IConfigSource source)
  54. {
  55. IConfig moduleConfig = source.Configs["Modules"];
  56. if (moduleConfig != null)
  57. {
  58. string name = moduleConfig.GetString("EntityTransferModule", "");
  59. if (name == Name)
  60. {
  61. IConfig transferConfig = source.Configs["EntityTransfer"];
  62. if (transferConfig != null)
  63. m_levelHGTeleport = transferConfig.GetInt("LevelHGTeleport", 0);
  64. InitialiseCommon(source);
  65. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: {0} enabled.", Name);
  66. }
  67. }
  68. }
  69. public override void AddRegion(Scene scene)
  70. {
  71. base.AddRegion(scene);
  72. if (m_Enabled)
  73. scene.RegisterModuleInterface<IUserAgentVerificationModule>(this);
  74. }
  75. protected override void OnNewClient(IClientAPI client)
  76. {
  77. client.OnTeleportHomeRequest += TeleportHome;
  78. client.OnTeleportLandmarkRequest += RequestTeleportLandmark;
  79. client.OnConnectionClosed += new Action<IClientAPI>(OnConnectionClosed);
  80. }
  81. public override void RegionLoaded(Scene scene)
  82. {
  83. base.RegionLoaded(scene);
  84. if (m_Enabled)
  85. m_GatekeeperConnector = new GatekeeperServiceConnector(scene.AssetService);
  86. }
  87. public override void RemoveRegion(Scene scene)
  88. {
  89. base.AddRegion(scene);
  90. if (m_Enabled)
  91. scene.UnregisterModuleInterface<IUserAgentVerificationModule>(this);
  92. }
  93. #endregion
  94. #region HG overrides of IEntiryTransferModule
  95. protected override GridRegion GetFinalDestination(GridRegion region)
  96. {
  97. int flags = Scene.GridService.GetRegionFlags(Scene.RegionInfo.ScopeID, region.RegionID);
  98. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: region {0} flags: {1}", region.RegionID, flags);
  99. if ((flags & (int)OpenSim.Data.RegionFlags.Hyperlink) != 0)
  100. {
  101. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Destination region {0} is hyperlink", region.RegionID);
  102. GridRegion real_destination = m_GatekeeperConnector.GetHyperlinkRegion(region, region.RegionID);
  103. if (real_destination != null)
  104. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: GetFinalDestination serveruri -> {0}", real_destination.ServerURI);
  105. else
  106. m_log.WarnFormat("[HG ENTITY TRANSFER MODULE]: GetHyperlinkRegion to Gatekeeper {0} failed", region.ServerURI);
  107. return real_destination;
  108. }
  109. return region;
  110. }
  111. protected override bool NeedsClosing(float drawdist, uint oldRegionX, uint newRegionX, uint oldRegionY, uint newRegionY, GridRegion reg)
  112. {
  113. if (base.NeedsClosing(drawdist, oldRegionX, newRegionX, oldRegionY, newRegionY, reg))
  114. return true;
  115. int flags = Scene.GridService.GetRegionFlags(Scene.RegionInfo.ScopeID, reg.RegionID);
  116. if (flags == -1 /* no region in DB */ || (flags & (int)OpenSim.Data.RegionFlags.Hyperlink) != 0)
  117. return true;
  118. return false;
  119. }
  120. protected override void AgentHasMovedAway(ScenePresence sp, bool logout)
  121. {
  122. base.AgentHasMovedAway(sp, logout);
  123. if (logout)
  124. {
  125. // Log them out of this grid
  126. Scene.PresenceService.LogoutAgent(sp.ControllingClient.SessionId);
  127. }
  128. }
  129. protected override bool CreateAgent(ScenePresence sp, GridRegion reg, GridRegion finalDestination, AgentCircuitData agentCircuit, uint teleportFlags, out string reason, out bool logout)
  130. {
  131. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: CreateAgent {0} {1}", reg.ServerURI, finalDestination.ServerURI);
  132. reason = string.Empty;
  133. logout = false;
  134. int flags = Scene.GridService.GetRegionFlags(Scene.RegionInfo.ScopeID, reg.RegionID);
  135. if (flags == -1 /* no region in DB */ || (flags & (int)OpenSim.Data.RegionFlags.Hyperlink) != 0)
  136. {
  137. // this user is going to another grid
  138. // check if HyperGrid teleport is allowed, based on user level
  139. if (sp.UserLevel < m_levelHGTeleport)
  140. {
  141. m_log.WarnFormat("[HG ENTITY TRANSFER MODULE]: Unable to HG teleport agent due to insufficient UserLevel.");
  142. reason = "HyperGrid teleport not permitted";
  143. return false;
  144. }
  145. if (agentCircuit.ServiceURLs.ContainsKey("HomeURI"))
  146. {
  147. string userAgentDriver = agentCircuit.ServiceURLs["HomeURI"].ToString();
  148. IUserAgentService connector = new UserAgentServiceConnector(userAgentDriver);
  149. bool success = connector.LoginAgentToGrid(agentCircuit, reg, finalDestination, out reason);
  150. logout = success; // flag for later logout from this grid; this is an HG TP
  151. if (success)
  152. sp.Scene.EventManager.TriggerTeleportStart(sp.ControllingClient, reg, finalDestination, teleportFlags, logout);
  153. return success;
  154. }
  155. else
  156. {
  157. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Agent does not have a HomeURI address");
  158. return false;
  159. }
  160. }
  161. return base.CreateAgent(sp, reg, finalDestination, agentCircuit, teleportFlags, out reason, out logout);
  162. }
  163. public override void TeleportHome(UUID id, IClientAPI client)
  164. {
  165. m_log.DebugFormat(
  166. "[ENTITY TRANSFER MODULE]: Request to teleport {0} {1} home", client.Name, client.AgentId);
  167. // Let's find out if this is a foreign user or a local user
  168. IUserManagement uMan = Scene.RequestModuleInterface<IUserManagement>();
  169. if (uMan != null && uMan.IsLocalGridUser(id))
  170. {
  171. // local grid user
  172. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: User is local");
  173. base.TeleportHome(id, client);
  174. return;
  175. }
  176. // Foreign user wants to go home
  177. //
  178. AgentCircuitData aCircuit = ((Scene)(client.Scene)).AuthenticateHandler.GetAgentCircuitData(client.CircuitCode);
  179. if (aCircuit == null || (aCircuit != null && !aCircuit.ServiceURLs.ContainsKey("HomeURI")))
  180. {
  181. client.SendTeleportFailed("Your information has been lost");
  182. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Unable to locate agent's gateway information");
  183. return;
  184. }
  185. IUserAgentService userAgentService = new UserAgentServiceConnector(aCircuit.ServiceURLs["HomeURI"].ToString());
  186. Vector3 position = Vector3.UnitY, lookAt = Vector3.UnitY;
  187. GridRegion finalDestination = userAgentService.GetHomeRegion(aCircuit.AgentID, out position, out lookAt);
  188. if (finalDestination == null)
  189. {
  190. client.SendTeleportFailed("Your home region could not be found");
  191. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Agent's home region not found");
  192. return;
  193. }
  194. ScenePresence sp = ((Scene)(client.Scene)).GetScenePresence(client.AgentId);
  195. if (sp == null)
  196. {
  197. client.SendTeleportFailed("Internal error");
  198. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Agent not found in the scene where it is supposed to be");
  199. return;
  200. }
  201. GridRegion homeGatekeeper = MakeRegion(aCircuit);
  202. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: teleporting user {0} {1} home to {2} via {3}:{4}",
  203. aCircuit.firstname, aCircuit.lastname, finalDestination.RegionName, homeGatekeeper.ServerURI, homeGatekeeper.RegionName);
  204. DoTeleport(
  205. sp, homeGatekeeper, finalDestination,
  206. position, lookAt, (uint)(Constants.TeleportFlags.SetLastToTarget | Constants.TeleportFlags.ViaHome));
  207. }
  208. /// <summary>
  209. /// Tries to teleport agent to landmark.
  210. /// </summary>
  211. /// <param name="remoteClient"></param>
  212. /// <param name="regionHandle"></param>
  213. /// <param name="position"></param>
  214. public override void RequestTeleportLandmark(IClientAPI remoteClient, AssetLandmark lm)
  215. {
  216. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Teleporting agent via landmark to {0} region {1} position {2}",
  217. (lm.Gatekeeper == string.Empty) ? "local" : lm.Gatekeeper, lm.RegionID, lm.Position);
  218. if (lm.Gatekeeper == string.Empty)
  219. {
  220. base.RequestTeleportLandmark(remoteClient, lm);
  221. return;
  222. }
  223. GridRegion info = Scene.GridService.GetRegionByUUID(UUID.Zero, lm.RegionID);
  224. // Local region?
  225. if (info != null)
  226. {
  227. ((Scene)(remoteClient.Scene)).RequestTeleportLocation(remoteClient, info.RegionHandle, lm.Position,
  228. Vector3.Zero, (uint)(Constants.TeleportFlags.SetLastToTarget | Constants.TeleportFlags.ViaLandmark));
  229. return;
  230. }
  231. else
  232. {
  233. // Foreign region
  234. Scene scene = (Scene)(remoteClient.Scene);
  235. GatekeeperServiceConnector gConn = new GatekeeperServiceConnector();
  236. GridRegion gatekeeper = new GridRegion();
  237. gatekeeper.ServerURI = lm.Gatekeeper;
  238. GridRegion finalDestination = gConn.GetHyperlinkRegion(gatekeeper, new UUID(lm.RegionID));
  239. if (finalDestination != null)
  240. {
  241. ScenePresence sp = scene.GetScenePresence(remoteClient.AgentId);
  242. IEntityTransferModule transferMod = scene.RequestModuleInterface<IEntityTransferModule>();
  243. if (transferMod != null && sp != null)
  244. transferMod.DoTeleport(
  245. sp, gatekeeper, finalDestination, lm.Position, Vector3.UnitX,
  246. (uint)(Constants.TeleportFlags.SetLastToTarget | Constants.TeleportFlags.ViaLandmark));
  247. }
  248. }
  249. // can't find the region: Tell viewer and abort
  250. remoteClient.SendTeleportFailed("The teleport destination could not be found.");
  251. }
  252. #endregion
  253. #region IUserAgentVerificationModule
  254. public bool VerifyClient(AgentCircuitData aCircuit, string token)
  255. {
  256. if (aCircuit.ServiceURLs.ContainsKey("HomeURI"))
  257. {
  258. string url = aCircuit.ServiceURLs["HomeURI"].ToString();
  259. IUserAgentService security = new UserAgentServiceConnector(url);
  260. return security.VerifyClient(aCircuit.SessionID, token);
  261. }
  262. else
  263. {
  264. m_log.DebugFormat(
  265. "[HG ENTITY TRANSFER MODULE]: Agent {0} {1} does not have a HomeURI OH NO!",
  266. aCircuit.firstname, aCircuit.lastname);
  267. }
  268. return false;
  269. }
  270. void OnConnectionClosed(IClientAPI obj)
  271. {
  272. if (obj.SceneAgent.IsChildAgent)
  273. return;
  274. // Let's find out if this is a foreign user or a local user
  275. IUserManagement uMan = Scene.RequestModuleInterface<IUserManagement>();
  276. // UserAccount account = Scene.UserAccountService.GetUserAccount(Scene.RegionInfo.ScopeID, obj.AgentId);
  277. if (uMan != null && uMan.IsLocalGridUser(obj.AgentId))
  278. {
  279. // local grid user
  280. return;
  281. }
  282. AgentCircuitData aCircuit = ((Scene)(obj.Scene)).AuthenticateHandler.GetAgentCircuitData(obj.CircuitCode);
  283. if (aCircuit.ServiceURLs.ContainsKey("HomeURI"))
  284. {
  285. string url = aCircuit.ServiceURLs["HomeURI"].ToString();
  286. IUserAgentService security = new UserAgentServiceConnector(url);
  287. security.LogoutAgent(obj.AgentId, obj.SessionId);
  288. //m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Sent logout call to UserAgentService @ {0}", url);
  289. }
  290. else
  291. {
  292. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: HomeURI not found for agent {0} logout", obj.AgentId);
  293. }
  294. }
  295. #endregion
  296. private GridRegion MakeRegion(AgentCircuitData aCircuit)
  297. {
  298. GridRegion region = new GridRegion();
  299. Uri uri = null;
  300. if (!aCircuit.ServiceURLs.ContainsKey("HomeURI") ||
  301. (aCircuit.ServiceURLs.ContainsKey("HomeURI") && !Uri.TryCreate(aCircuit.ServiceURLs["HomeURI"].ToString(), UriKind.Absolute, out uri)))
  302. return null;
  303. region.ExternalHostName = uri.Host;
  304. region.HttpPort = (uint)uri.Port;
  305. region.ServerURI = aCircuit.ServiceURLs["HomeURI"].ToString();
  306. region.RegionName = string.Empty;
  307. region.InternalEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("0.0.0.0"), (int)0);
  308. return region;
  309. }
  310. }
  311. }