UserAgentServiceConnector.cs 22 KB

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