UserAgentServiceConnector.cs 22 KB

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