UserAgentServiceConnector.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Net;
  32. using System.Reflection;
  33. using System.Text;
  34. using OpenSim.Framework;
  35. using OpenSim.Services.Interfaces;
  36. using OpenSim.Services.Connectors.Simulation;
  37. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  38. using OpenMetaverse;
  39. using OpenMetaverse.StructuredData;
  40. using log4net;
  41. using Nwc.XmlRpc;
  42. using Nini.Config;
  43. namespace OpenSim.Services.Connectors.Hypergrid
  44. {
  45. public class UserAgentServiceConnector : SimulationServiceConnector, IUserAgentService
  46. {
  47. private static readonly ILog m_log =
  48. LogManager.GetLogger(
  49. MethodBase.GetCurrentMethod().DeclaringType);
  50. private string m_ServerURLHost;
  51. private string m_ServerURL;
  52. private GridRegion m_Gatekeeper;
  53. public UserAgentServiceConnector(string url) : this(url, true)
  54. {
  55. }
  56. public UserAgentServiceConnector(string url, bool dnsLookup)
  57. {
  58. m_ServerURL = m_ServerURLHost = url;
  59. if (dnsLookup)
  60. {
  61. // Doing this here, because XML-RPC or mono have some strong ideas about
  62. // caching DNS translations.
  63. try
  64. {
  65. Uri m_Uri = new Uri(m_ServerURL);
  66. IPAddress ip = Util.GetHostFromDNS(m_Uri.Host);
  67. m_ServerURL = m_ServerURL.Replace(m_Uri.Host, ip.ToString());
  68. if (!m_ServerURL.EndsWith("/"))
  69. m_ServerURL += "/";
  70. }
  71. catch (Exception e)
  72. {
  73. m_log.DebugFormat("[USER AGENT CONNECTOR]: Malformed Uri {0}: {1}", url, e.Message);
  74. }
  75. }
  76. //m_log.DebugFormat("[USER AGENT CONNECTOR]: new connector to {0} ({1})", url, m_ServerURL);
  77. }
  78. public UserAgentServiceConnector(IConfigSource config)
  79. {
  80. IConfig serviceConfig = config.Configs["UserAgentService"];
  81. if (serviceConfig == null)
  82. {
  83. m_log.Error("[USER AGENT CONNECTOR]: UserAgentService missing from ini");
  84. throw new Exception("UserAgent connector init error");
  85. }
  86. string serviceURI = serviceConfig.GetString("UserAgentServerURI",
  87. String.Empty);
  88. if (serviceURI == String.Empty)
  89. {
  90. m_log.Error("[USER AGENT CONNECTOR]: No Server URI named in section UserAgentService");
  91. throw new Exception("UserAgent connector init error");
  92. }
  93. m_ServerURL = m_ServerURLHost = serviceURI;
  94. if (!m_ServerURL.EndsWith("/"))
  95. m_ServerURL += "/";
  96. //m_log.DebugFormat("[USER AGENT CONNECTOR]: new connector to {0}", m_ServerURL);
  97. }
  98. protected override string AgentPath()
  99. {
  100. return "homeagent/";
  101. }
  102. // The Login service calls this interface with fromLogin=true
  103. // Sims call it with fromLogin=false
  104. // Either way, this is verified by the handler
  105. public bool LoginAgentToGrid(GridRegion source, AgentCircuitData aCircuit, GridRegion gatekeeper, GridRegion destination, bool fromLogin, out string reason)
  106. {
  107. reason = String.Empty;
  108. if (destination == null)
  109. {
  110. reason = "Destination is null";
  111. m_log.Debug("[USER AGENT CONNECTOR]: Given destination is null");
  112. return false;
  113. }
  114. GridRegion home = new GridRegion();
  115. home.ServerURI = m_ServerURL;
  116. home.RegionID = destination.RegionID;
  117. home.RegionLocX = destination.RegionLocX;
  118. home.RegionLocY = destination.RegionLocY;
  119. m_Gatekeeper = gatekeeper;
  120. Console.WriteLine(" >>> LoginAgentToGrid <<< " + home.ServerURI);
  121. uint flags = fromLogin ? (uint)TeleportFlags.ViaLogin : (uint)TeleportFlags.ViaHome;
  122. EntityTransferContext ctx = new EntityTransferContext();
  123. return CreateAgent(source, home, aCircuit, flags, ctx, out reason);
  124. }
  125. // The simulators call this interface
  126. public bool LoginAgentToGrid(GridRegion source, AgentCircuitData aCircuit, GridRegion gatekeeper, GridRegion destination, out string reason)
  127. {
  128. return LoginAgentToGrid(source, aCircuit, gatekeeper, destination, false, out reason);
  129. }
  130. protected override void PackData(OSDMap args, GridRegion source, AgentCircuitData aCircuit, GridRegion destination, uint flags)
  131. {
  132. base.PackData(args, source, aCircuit, destination, flags);
  133. args["gatekeeper_serveruri"] = OSD.FromString(m_Gatekeeper.ServerURI);
  134. args["gatekeeper_host"] = OSD.FromString(m_Gatekeeper.ExternalHostName);
  135. args["gatekeeper_port"] = OSD.FromString(m_Gatekeeper.HttpPort.ToString());
  136. args["destination_serveruri"] = OSD.FromString(destination.ServerURI);
  137. }
  138. public void SetClientToken(UUID sessionID, string token)
  139. {
  140. // no-op
  141. }
  142. private Hashtable CallServer(string methodName, Hashtable hash)
  143. {
  144. IList paramList = new ArrayList();
  145. paramList.Add(hash);
  146. XmlRpcRequest request = new XmlRpcRequest(methodName, paramList);
  147. // Send and get reply
  148. XmlRpcResponse response = null;
  149. try
  150. {
  151. response = request.Send(m_ServerURL, 10000);
  152. }
  153. catch (Exception e)
  154. {
  155. m_log.DebugFormat("[USER AGENT CONNECTOR]: {0} call to {1} failed: {2}", methodName, m_ServerURLHost, e.Message);
  156. throw;
  157. }
  158. if (response.IsFault)
  159. {
  160. throw new Exception(string.Format("[USER AGENT CONNECTOR]: {0} call to {1} returned an error: {2}", methodName, m_ServerURLHost, response.FaultString));
  161. }
  162. hash = (Hashtable)response.Value;
  163. if (hash == null)
  164. {
  165. throw new Exception(string.Format("[USER AGENT CONNECTOR]: {0} call to {1} returned null", methodName, m_ServerURLHost));
  166. }
  167. return hash;
  168. }
  169. public GridRegion GetHomeRegion(UUID userID, out Vector3 position, out Vector3 lookAt)
  170. {
  171. position = Vector3.UnitY; lookAt = Vector3.UnitY;
  172. Hashtable hash = new Hashtable();
  173. hash["userID"] = userID.ToString();
  174. hash = CallServer("get_home_region", hash);
  175. bool success;
  176. if (!Boolean.TryParse((string)hash["result"], out success) || !success)
  177. return null;
  178. GridRegion region = new GridRegion();
  179. UUID.TryParse((string)hash["uuid"], out region.RegionID);
  180. //m_log.Debug(">> HERE, uuid: " + region.RegionID);
  181. int n = 0;
  182. if (hash["x"] != null)
  183. {
  184. Int32.TryParse((string)hash["x"], out n);
  185. region.RegionLocX = n;
  186. //m_log.Debug(">> HERE, x: " + region.RegionLocX);
  187. }
  188. if (hash["y"] != null)
  189. {
  190. Int32.TryParse((string)hash["y"], out n);
  191. region.RegionLocY = n;
  192. //m_log.Debug(">> HERE, y: " + region.RegionLocY);
  193. }
  194. if (hash["size_x"] != null)
  195. {
  196. Int32.TryParse((string)hash["size_x"], out n);
  197. region.RegionSizeX = n;
  198. //m_log.Debug(">> HERE, x: " + region.RegionLocX);
  199. }
  200. if (hash["size_y"] != null)
  201. {
  202. Int32.TryParse((string)hash["size_y"], out n);
  203. region.RegionSizeY = n;
  204. //m_log.Debug(">> HERE, y: " + region.RegionLocY);
  205. }
  206. if (hash["region_name"] != null)
  207. {
  208. region.RegionName = (string)hash["region_name"];
  209. //m_log.Debug(">> HERE, name: " + region.RegionName);
  210. }
  211. if (hash["hostname"] != null)
  212. region.ExternalHostName = (string)hash["hostname"];
  213. if (hash["http_port"] != null)
  214. {
  215. uint p = 0;
  216. UInt32.TryParse((string)hash["http_port"], out p);
  217. region.HttpPort = p;
  218. }
  219. if (hash.ContainsKey("server_uri") && hash["server_uri"] != null)
  220. region.ServerURI = (string)hash["server_uri"];
  221. if (hash["internal_port"] != null)
  222. {
  223. int p = 0;
  224. Int32.TryParse((string)hash["internal_port"], out p);
  225. region.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), p);
  226. }
  227. if (hash["position"] != null)
  228. Vector3.TryParse((string)hash["position"], out position);
  229. if (hash["lookAt"] != null)
  230. Vector3.TryParse((string)hash["lookAt"], out lookAt);
  231. // Successful return
  232. return region;
  233. }
  234. public bool IsAgentComingHome(UUID sessionID, string thisGridExternalName)
  235. {
  236. Hashtable hash = new Hashtable();
  237. hash["sessionID"] = sessionID.ToString();
  238. hash["externalName"] = thisGridExternalName;
  239. IList paramList = new ArrayList();
  240. paramList.Add(hash);
  241. XmlRpcRequest request = new XmlRpcRequest("agent_is_coming_home", paramList);
  242. string reason = string.Empty;
  243. return GetBoolResponse(request, out reason);
  244. }
  245. public bool VerifyAgent(UUID sessionID, string token)
  246. {
  247. Hashtable hash = new Hashtable();
  248. hash["sessionID"] = sessionID.ToString();
  249. hash["token"] = token;
  250. IList paramList = new ArrayList();
  251. paramList.Add(hash);
  252. XmlRpcRequest request = new XmlRpcRequest("verify_agent", paramList);
  253. string reason = string.Empty;
  254. return GetBoolResponse(request, out reason);
  255. }
  256. public bool VerifyClient(UUID sessionID, string token)
  257. {
  258. Hashtable hash = new Hashtable();
  259. hash["sessionID"] = sessionID.ToString();
  260. hash["token"] = token;
  261. IList paramList = new ArrayList();
  262. paramList.Add(hash);
  263. XmlRpcRequest request = new XmlRpcRequest("verify_client", paramList);
  264. string reason = string.Empty;
  265. return GetBoolResponse(request, out reason);
  266. }
  267. public void LogoutAgent(UUID userID, UUID sessionID)
  268. {
  269. Hashtable hash = new Hashtable();
  270. hash["sessionID"] = sessionID.ToString();
  271. hash["userID"] = userID.ToString();
  272. IList paramList = new ArrayList();
  273. paramList.Add(hash);
  274. XmlRpcRequest request = new XmlRpcRequest("logout_agent", paramList);
  275. string reason = string.Empty;
  276. GetBoolResponse(request, out reason);
  277. }
  278. [Obsolete]
  279. public List<UUID> StatusNotification(List<string> friends, UUID userID, bool online)
  280. {
  281. Hashtable hash = new Hashtable();
  282. hash["userID"] = userID.ToString();
  283. hash["online"] = online.ToString();
  284. int i = 0;
  285. foreach (string s in friends)
  286. {
  287. hash["friend_" + i.ToString()] = s;
  288. i++;
  289. }
  290. IList paramList = new ArrayList();
  291. paramList.Add(hash);
  292. XmlRpcRequest request = new XmlRpcRequest("status_notification", paramList);
  293. // string reason = string.Empty;
  294. // Send and get reply
  295. List<UUID> friendsOnline = new List<UUID>();
  296. XmlRpcResponse response = null;
  297. try
  298. {
  299. response = request.Send(m_ServerURL, 6000);
  300. }
  301. catch
  302. {
  303. m_log.DebugFormat("[USER AGENT CONNECTOR]: Unable to contact remote server {0} for StatusNotification", m_ServerURLHost);
  304. // reason = "Exception: " + e.Message;
  305. return friendsOnline;
  306. }
  307. if (response.IsFault)
  308. {
  309. m_log.ErrorFormat("[USER AGENT CONNECTOR]: remote call to {0} for StatusNotification returned an error: {1}", m_ServerURLHost, response.FaultString);
  310. // reason = "XMLRPC Fault";
  311. return friendsOnline;
  312. }
  313. hash = (Hashtable)response.Value;
  314. //foreach (Object o in hash)
  315. // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value);
  316. try
  317. {
  318. if (hash == null)
  319. {
  320. m_log.ErrorFormat("[USER AGENT CONNECTOR]: GetOnlineFriends Got null response from {0}! THIS IS BAAAAD", m_ServerURLHost);
  321. // reason = "Internal error 1";
  322. return friendsOnline;
  323. }
  324. // Here is the actual response
  325. foreach (object key in hash.Keys)
  326. {
  327. if (key is string && ((string)key).StartsWith("friend_") && hash[key] != null)
  328. {
  329. UUID uuid;
  330. if (UUID.TryParse(hash[key].ToString(), out uuid))
  331. friendsOnline.Add(uuid);
  332. }
  333. }
  334. }
  335. catch
  336. {
  337. m_log.ErrorFormat("[USER AGENT CONNECTOR]: Got exception on GetOnlineFriends response.");
  338. // reason = "Exception: " + e.Message;
  339. }
  340. return friendsOnline;
  341. }
  342. [Obsolete]
  343. public List<UUID> GetOnlineFriends(UUID userID, List<string> friends)
  344. {
  345. Hashtable hash = new Hashtable();
  346. hash["userID"] = userID.ToString();
  347. int i = 0;
  348. foreach (string s in friends)
  349. {
  350. hash["friend_" + i.ToString()] = s;
  351. i++;
  352. }
  353. IList paramList = new ArrayList();
  354. paramList.Add(hash);
  355. XmlRpcRequest request = new XmlRpcRequest("get_online_friends", paramList);
  356. // string reason = string.Empty;
  357. // Send and get reply
  358. List<UUID> online = new List<UUID>();
  359. XmlRpcResponse response = null;
  360. try
  361. {
  362. response = request.Send(m_ServerURL, 10000);
  363. }
  364. catch
  365. {
  366. m_log.DebugFormat("[USER AGENT CONNECTOR]: Unable to contact remote server {0} for GetOnlineFriends", m_ServerURLHost);
  367. // reason = "Exception: " + e.Message;
  368. return online;
  369. }
  370. if (response.IsFault)
  371. {
  372. m_log.ErrorFormat("[USER AGENT CONNECTOR]: remote call to {0} for GetOnlineFriends returned an error: {1}", m_ServerURLHost, response.FaultString);
  373. // reason = "XMLRPC Fault";
  374. return online;
  375. }
  376. hash = (Hashtable)response.Value;
  377. //foreach (Object o in hash)
  378. // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value);
  379. try
  380. {
  381. if (hash == null)
  382. {
  383. m_log.ErrorFormat("[USER AGENT CONNECTOR]: GetOnlineFriends Got null response from {0}! THIS IS BAAAAD", m_ServerURLHost);
  384. // reason = "Internal error 1";
  385. return online;
  386. }
  387. // Here is the actual response
  388. foreach (object key in hash.Keys)
  389. {
  390. if (key is string && ((string)key).StartsWith("friend_") && hash[key] != null)
  391. {
  392. UUID uuid;
  393. if (UUID.TryParse(hash[key].ToString(), out uuid))
  394. online.Add(uuid);
  395. }
  396. }
  397. }
  398. catch
  399. {
  400. m_log.ErrorFormat("[USER AGENT CONNECTOR]: Got exception on GetOnlineFriends response.");
  401. // reason = "Exception: " + e.Message;
  402. }
  403. return online;
  404. }
  405. public Dictionary<string,object> GetUserInfo (UUID userID)
  406. {
  407. Hashtable hash = new Hashtable();
  408. hash["userID"] = userID.ToString();
  409. hash = CallServer("get_user_info", hash);
  410. Dictionary<string, object> info = new Dictionary<string, object>();
  411. foreach (object key in hash.Keys)
  412. {
  413. if (hash[key] != null)
  414. {
  415. info.Add(key.ToString(), hash[key]);
  416. }
  417. }
  418. return info;
  419. }
  420. public Dictionary<string, object> GetServerURLs(UUID userID)
  421. {
  422. Hashtable hash = new Hashtable();
  423. hash["userID"] = userID.ToString();
  424. hash = CallServer("get_server_urls", hash);
  425. Dictionary<string, object> serverURLs = new Dictionary<string, object>();
  426. foreach (object key in hash.Keys)
  427. {
  428. if (key is string && ((string)key).StartsWith("SRV_") && hash[key] != null)
  429. {
  430. string serverType = key.ToString().Substring(4); // remove "SRV_"
  431. serverURLs.Add(serverType, hash[key].ToString());
  432. }
  433. }
  434. return serverURLs;
  435. }
  436. public string LocateUser(UUID userID)
  437. {
  438. Hashtable hash = new Hashtable();
  439. hash["userID"] = userID.ToString();
  440. hash = CallServer("locate_user", hash);
  441. string url = string.Empty;
  442. // Here's the actual response
  443. if (hash.ContainsKey("URL"))
  444. url = hash["URL"].ToString();
  445. return url;
  446. }
  447. public string GetUUI(UUID userID, UUID targetUserID)
  448. {
  449. Hashtable hash = new Hashtable();
  450. hash["userID"] = userID.ToString();
  451. hash["targetUserID"] = targetUserID.ToString();
  452. hash = CallServer("get_uui", hash);
  453. string uui = string.Empty;
  454. // Here's the actual response
  455. if (hash.ContainsKey("UUI"))
  456. uui = hash["UUI"].ToString();
  457. return uui;
  458. }
  459. public UUID GetUUID(String first, String last)
  460. {
  461. Hashtable hash = new Hashtable();
  462. hash["first"] = first;
  463. hash["last"] = last;
  464. hash = CallServer("get_uuid", hash);
  465. if (!hash.ContainsKey("UUID"))
  466. {
  467. throw new Exception(string.Format("[USER AGENT CONNECTOR]: get_uuid call to {0} didn't return a UUID", m_ServerURLHost));
  468. }
  469. UUID uuid;
  470. if (!UUID.TryParse(hash["UUID"].ToString(), out uuid))
  471. {
  472. throw new Exception(string.Format("[USER AGENT CONNECTOR]: get_uuid call to {0} returned an invalid UUID: {1}", m_ServerURLHost, hash["UUID"].ToString()));
  473. }
  474. return uuid;
  475. }
  476. private bool GetBoolResponse(XmlRpcRequest request, out string reason)
  477. {
  478. //m_log.Debug("[USER AGENT CONNECTOR]: GetBoolResponse from/to " + m_ServerURLHost);
  479. XmlRpcResponse response = null;
  480. try
  481. {
  482. response = request.Send(m_ServerURL, 10000);
  483. }
  484. catch (Exception e)
  485. {
  486. m_log.DebugFormat("[USER AGENT CONNECTOR]: Unable to contact remote server {0} for GetBoolResponse", m_ServerURLHost);
  487. reason = "Exception: " + e.Message;
  488. return false;
  489. }
  490. if (response.IsFault)
  491. {
  492. m_log.ErrorFormat("[USER AGENT CONNECTOR]: remote call to {0} for GetBoolResponse returned an error: {1}", m_ServerURLHost, response.FaultString);
  493. reason = "XMLRPC Fault";
  494. return false;
  495. }
  496. Hashtable hash = (Hashtable)response.Value;
  497. //foreach (Object o in hash)
  498. // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value);
  499. try
  500. {
  501. if (hash == null)
  502. {
  503. m_log.ErrorFormat("[USER AGENT CONNECTOR]: Got null response from {0}! THIS IS BAAAAD", m_ServerURLHost);
  504. reason = "Internal error 1";
  505. return false;
  506. }
  507. bool success = false;
  508. reason = string.Empty;
  509. if (hash.ContainsKey("result"))
  510. Boolean.TryParse((string)hash["result"], out success);
  511. else
  512. {
  513. reason = "Internal error 2";
  514. m_log.WarnFormat("[USER AGENT CONNECTOR]: response from {0} does not have expected key 'result'", m_ServerURLHost);
  515. }
  516. return success;
  517. }
  518. catch (Exception e)
  519. {
  520. m_log.ErrorFormat("[USER AGENT CONNECTOR]: Got exception on GetBoolResponse response.");
  521. if (hash.ContainsKey("result") && hash["result"] != null)
  522. m_log.ErrorFormat("Reply was ", (string)hash["result"]);
  523. reason = "Exception: " + e.Message;
  524. return false;
  525. }
  526. }
  527. }
  528. }