1
0

UserAgentServiceConnector.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 : IUserAgentService
  46. {
  47. private static readonly ILog m_log =
  48. LogManager.GetLogger(
  49. MethodBase.GetCurrentMethod().DeclaringType);
  50. string m_ServerURL;
  51. Uri m_Uri;
  52. public UserAgentServiceConnector(string url)
  53. {
  54. m_ServerURL = url;
  55. try
  56. {
  57. m_Uri = new Uri(m_ServerURL);
  58. IPAddress ip = Util.GetHostFromDNS(m_Uri.Host);
  59. m_ServerURL = "http://" + ip.ToString() + ":" + m_Uri.Port;
  60. }
  61. catch (Exception e)
  62. {
  63. m_log.DebugFormat("[USER AGENT CONNECTOR]: Malformed Uri {0}: {1}", m_ServerURL, e.Message);
  64. }
  65. }
  66. public UserAgentServiceConnector(IConfigSource config)
  67. {
  68. }
  69. public bool LoginAgentToGrid(AgentCircuitData agent, GridRegion gatekeeper, GridRegion finalDestination, IPEndPoint ipaddress, out string reason)
  70. {
  71. // not available over remote calls
  72. reason = "Method not available over remote calls";
  73. return false;
  74. }
  75. public bool LoginAgentToGrid(AgentCircuitData aCircuit, GridRegion gatekeeper, GridRegion destination, out string reason)
  76. {
  77. reason = String.Empty;
  78. if (destination == null)
  79. {
  80. reason = "Destination is null";
  81. m_log.Debug("[USER AGENT CONNECTOR]: Given destination is null");
  82. return false;
  83. }
  84. string uri = m_ServerURL + "/homeagent/" + aCircuit.AgentID + "/";
  85. Console.WriteLine(" >>> LoginAgentToGrid <<< " + uri);
  86. HttpWebRequest AgentCreateRequest = (HttpWebRequest)WebRequest.Create(uri);
  87. AgentCreateRequest.Method = "POST";
  88. AgentCreateRequest.ContentType = "application/json";
  89. AgentCreateRequest.Timeout = 10000;
  90. //AgentCreateRequest.KeepAlive = false;
  91. //AgentCreateRequest.Headers.Add("Authorization", authKey);
  92. // Fill it in
  93. OSDMap args = PackCreateAgentArguments(aCircuit, gatekeeper, destination);
  94. string strBuffer = "";
  95. byte[] buffer = new byte[1];
  96. try
  97. {
  98. strBuffer = OSDParser.SerializeJsonString(args);
  99. Encoding str = Util.UTF8;
  100. buffer = str.GetBytes(strBuffer);
  101. }
  102. catch (Exception e)
  103. {
  104. m_log.WarnFormat("[USER AGENT CONNECTOR]: Exception thrown on serialization of ChildCreate: {0}", e.Message);
  105. // ignore. buffer will be empty, caller should check.
  106. }
  107. Stream os = null;
  108. try
  109. { // send the Post
  110. AgentCreateRequest.ContentLength = buffer.Length; //Count bytes to send
  111. os = AgentCreateRequest.GetRequestStream();
  112. os.Write(buffer, 0, strBuffer.Length); //Send it
  113. m_log.InfoFormat("[USER AGENT CONNECTOR]: Posted CreateAgent request to remote sim {0}, region {1}, x={2} y={3}",
  114. uri, destination.RegionName, destination.RegionLocX, destination.RegionLocY);
  115. }
  116. //catch (WebException ex)
  117. catch
  118. {
  119. //m_log.InfoFormat("[USER AGENT CONNECTOR]: Bad send on ChildAgentUpdate {0}", ex.Message);
  120. reason = "cannot contact remote region";
  121. return false;
  122. }
  123. finally
  124. {
  125. if (os != null)
  126. os.Close();
  127. }
  128. // Let's wait for the response
  129. //m_log.Info("[USER AGENT CONNECTOR]: Waiting for a reply after DoCreateChildAgentCall");
  130. WebResponse webResponse = null;
  131. StreamReader sr = null;
  132. try
  133. {
  134. webResponse = AgentCreateRequest.GetResponse();
  135. if (webResponse == null)
  136. {
  137. m_log.Info("[USER AGENT CONNECTOR]: Null reply on DoCreateChildAgentCall post");
  138. }
  139. else
  140. {
  141. sr = new StreamReader(webResponse.GetResponseStream());
  142. string response = sr.ReadToEnd().Trim();
  143. m_log.InfoFormat("[USER AGENT CONNECTOR]: DoCreateChildAgentCall reply was {0} ", response);
  144. if (!String.IsNullOrEmpty(response))
  145. {
  146. try
  147. {
  148. // we assume we got an OSDMap back
  149. OSDMap r = Util.GetOSDMap(response);
  150. bool success = r["success"].AsBoolean();
  151. reason = r["reason"].AsString();
  152. return success;
  153. }
  154. catch (NullReferenceException e)
  155. {
  156. m_log.InfoFormat("[USER AGENT CONNECTOR]: exception on reply of DoCreateChildAgentCall {0}", e.Message);
  157. // check for old style response
  158. if (response.ToLower().StartsWith("true"))
  159. return true;
  160. return false;
  161. }
  162. }
  163. }
  164. }
  165. catch (WebException ex)
  166. {
  167. m_log.InfoFormat("[USER AGENT CONNECTOR]: exception on reply of DoCreateChildAgentCall {0}", ex.Message);
  168. reason = "Destination did not reply";
  169. return false;
  170. }
  171. finally
  172. {
  173. if (sr != null)
  174. sr.Close();
  175. }
  176. return true;
  177. }
  178. protected OSDMap PackCreateAgentArguments(AgentCircuitData aCircuit, GridRegion gatekeeper, GridRegion destination)
  179. {
  180. OSDMap args = null;
  181. try
  182. {
  183. args = aCircuit.PackAgentCircuitData();
  184. }
  185. catch (Exception e)
  186. {
  187. m_log.Debug("[USER AGENT CONNECTOR]: PackAgentCircuitData failed with exception: " + e.Message);
  188. }
  189. // Add the input arguments
  190. args["gatekeeper_host"] = OSD.FromString(gatekeeper.ExternalHostName);
  191. args["gatekeeper_port"] = OSD.FromString(gatekeeper.HttpPort.ToString());
  192. args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
  193. args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
  194. args["destination_name"] = OSD.FromString(destination.RegionName);
  195. args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
  196. return args;
  197. }
  198. public void SetClientToken(UUID sessionID, string token)
  199. {
  200. // no-op
  201. }
  202. public GridRegion GetHomeRegion(UUID userID, out Vector3 position, out Vector3 lookAt)
  203. {
  204. position = Vector3.UnitY; lookAt = Vector3.UnitY;
  205. Hashtable hash = new Hashtable();
  206. hash["userID"] = userID.ToString();
  207. IList paramList = new ArrayList();
  208. paramList.Add(hash);
  209. XmlRpcRequest request = new XmlRpcRequest("get_home_region", paramList);
  210. XmlRpcResponse response = null;
  211. try
  212. {
  213. response = request.Send(m_ServerURL, 10000);
  214. }
  215. catch (Exception)
  216. {
  217. return null;
  218. }
  219. if (response.IsFault)
  220. {
  221. return null;
  222. }
  223. hash = (Hashtable)response.Value;
  224. //foreach (Object o in hash)
  225. // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value);
  226. try
  227. {
  228. bool success = false;
  229. Boolean.TryParse((string)hash["result"], out success);
  230. if (success)
  231. {
  232. GridRegion region = new GridRegion();
  233. UUID.TryParse((string)hash["uuid"], out region.RegionID);
  234. //m_log.Debug(">> HERE, uuid: " + region.RegionID);
  235. int n = 0;
  236. if (hash["x"] != null)
  237. {
  238. Int32.TryParse((string)hash["x"], out n);
  239. region.RegionLocX = n;
  240. //m_log.Debug(">> HERE, x: " + region.RegionLocX);
  241. }
  242. if (hash["y"] != null)
  243. {
  244. Int32.TryParse((string)hash["y"], out n);
  245. region.RegionLocY = n;
  246. //m_log.Debug(">> HERE, y: " + region.RegionLocY);
  247. }
  248. if (hash["region_name"] != null)
  249. {
  250. region.RegionName = (string)hash["region_name"];
  251. //m_log.Debug(">> HERE, name: " + region.RegionName);
  252. }
  253. if (hash["hostname"] != null)
  254. region.ExternalHostName = (string)hash["hostname"];
  255. if (hash["http_port"] != null)
  256. {
  257. uint p = 0;
  258. UInt32.TryParse((string)hash["http_port"], out p);
  259. region.HttpPort = p;
  260. }
  261. if (hash["internal_port"] != null)
  262. {
  263. int p = 0;
  264. Int32.TryParse((string)hash["internal_port"], out p);
  265. region.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), p);
  266. }
  267. if (hash["position"] != null)
  268. Vector3.TryParse((string)hash["position"], out position);
  269. if (hash["lookAt"] != null)
  270. Vector3.TryParse((string)hash["lookAt"], out lookAt);
  271. // Successful return
  272. return region;
  273. }
  274. }
  275. catch (Exception)
  276. {
  277. return null;
  278. }
  279. return null;
  280. }
  281. public bool AgentIsComingHome(UUID sessionID, string thisGridExternalName)
  282. {
  283. Hashtable hash = new Hashtable();
  284. hash["sessionID"] = sessionID.ToString();
  285. hash["externalName"] = thisGridExternalName;
  286. IList paramList = new ArrayList();
  287. paramList.Add(hash);
  288. XmlRpcRequest request = new XmlRpcRequest("agent_is_coming_home", paramList);
  289. string reason = string.Empty;
  290. return GetBoolResponse(request, out reason);
  291. }
  292. public bool VerifyAgent(UUID sessionID, string token)
  293. {
  294. Hashtable hash = new Hashtable();
  295. hash["sessionID"] = sessionID.ToString();
  296. hash["token"] = token;
  297. IList paramList = new ArrayList();
  298. paramList.Add(hash);
  299. XmlRpcRequest request = new XmlRpcRequest("verify_agent", paramList);
  300. string reason = string.Empty;
  301. return GetBoolResponse(request, out reason);
  302. }
  303. public bool VerifyClient(UUID sessionID, string token)
  304. {
  305. Hashtable hash = new Hashtable();
  306. hash["sessionID"] = sessionID.ToString();
  307. hash["token"] = token;
  308. IList paramList = new ArrayList();
  309. paramList.Add(hash);
  310. XmlRpcRequest request = new XmlRpcRequest("verify_client", paramList);
  311. string reason = string.Empty;
  312. return GetBoolResponse(request, out reason);
  313. }
  314. public void LogoutAgent(UUID userID, UUID sessionID)
  315. {
  316. Hashtable hash = new Hashtable();
  317. hash["sessionID"] = sessionID.ToString();
  318. hash["userID"] = userID.ToString();
  319. IList paramList = new ArrayList();
  320. paramList.Add(hash);
  321. XmlRpcRequest request = new XmlRpcRequest("logout_agent", paramList);
  322. string reason = string.Empty;
  323. GetBoolResponse(request, out reason);
  324. }
  325. private bool GetBoolResponse(XmlRpcRequest request, out string reason)
  326. {
  327. //m_log.Debug("[USER AGENT CONNECTOR]: GetBoolResponse from/to " + m_ServerURL);
  328. XmlRpcResponse response = null;
  329. try
  330. {
  331. response = request.Send(m_ServerURL, 10000);
  332. }
  333. catch (Exception e)
  334. {
  335. m_log.DebugFormat("[USER AGENT CONNECTOR]: Unable to contact remote server {0}", m_ServerURL);
  336. reason = "Exception: " + e.Message;
  337. return false;
  338. }
  339. if (response.IsFault)
  340. {
  341. m_log.ErrorFormat("[USER AGENT CONNECTOR]: remote call to {0} returned an error: {1}", m_ServerURL, response.FaultString);
  342. reason = "XMLRPC Fault";
  343. return false;
  344. }
  345. Hashtable hash = (Hashtable)response.Value;
  346. //foreach (Object o in hash)
  347. // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value);
  348. try
  349. {
  350. if (hash == null)
  351. {
  352. m_log.ErrorFormat("[USER AGENT CONNECTOR]: Got null response from {0}! THIS IS BAAAAD", m_ServerURL);
  353. reason = "Internal error 1";
  354. return false;
  355. }
  356. bool success = false;
  357. reason = string.Empty;
  358. if (hash.ContainsKey("result"))
  359. Boolean.TryParse((string)hash["result"], out success);
  360. else
  361. {
  362. reason = "Internal error 2";
  363. m_log.WarnFormat("[USER AGENT CONNECTOR]: response from {0} does not have expected key 'result'", m_ServerURL);
  364. }
  365. return success;
  366. }
  367. catch (Exception e)
  368. {
  369. m_log.ErrorFormat("[USER AGENT CONNECTOR]: Got exception on GetBoolResponse response.");
  370. if (hash.ContainsKey("result") && hash["result"] != null)
  371. m_log.ErrorFormat("Reply was ", (string)hash["result"]);
  372. reason = "Exception: " + e.Message;
  373. return false;
  374. }
  375. }
  376. }
  377. }