UserAgentServiceConnector.cs 23 KB

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