UserAgentServiceConnector.cs 22 KB

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