AgentHandlers.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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.IO;
  30. using System.Reflection;
  31. using System.Net;
  32. using System.Text;
  33. using OpenSim.Server.Base;
  34. using OpenSim.Server.Handlers.Base;
  35. using OpenSim.Services.Interfaces;
  36. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Servers.HttpServer;
  39. using OpenMetaverse;
  40. using OpenMetaverse.StructuredData;
  41. using Nini.Config;
  42. using log4net;
  43. namespace OpenSim.Server.Handlers.Simulation
  44. {
  45. public class AgentHandler
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private ISimulationService m_SimulationService;
  49. protected bool m_Proxy = false;
  50. public AgentHandler() { }
  51. public AgentHandler(ISimulationService sim)
  52. {
  53. m_SimulationService = sim;
  54. }
  55. public Hashtable Handler(Hashtable request)
  56. {
  57. // m_log.Debug("[CONNECTION DEBUGGING]: AgentHandler Called");
  58. //
  59. // m_log.Debug("---------------------------");
  60. // m_log.Debug(" >> uri=" + request["uri"]);
  61. // m_log.Debug(" >> content-type=" + request["content-type"]);
  62. // m_log.Debug(" >> http-method=" + request["http-method"]);
  63. // m_log.Debug("---------------------------\n");
  64. Hashtable responsedata = new Hashtable();
  65. responsedata["content_type"] = "text/html";
  66. responsedata["keepalive"] = false;
  67. UUID agentID;
  68. UUID regionID;
  69. string action;
  70. if (!Utils.GetParams((string)request["uri"], out agentID, out regionID, out action))
  71. {
  72. m_log.InfoFormat("[AGENT HANDLER]: Invalid parameters for agent message {0}", request["uri"]);
  73. responsedata["int_response_code"] = 404;
  74. responsedata["str_response_string"] = "false";
  75. return responsedata;
  76. }
  77. // Next, let's parse the verb
  78. string method = (string)request["http-method"];
  79. if (method.Equals("PUT"))
  80. {
  81. DoAgentPut(request, responsedata);
  82. return responsedata;
  83. }
  84. else if (method.Equals("POST"))
  85. {
  86. DoAgentPost(request, responsedata, agentID);
  87. return responsedata;
  88. }
  89. else if (method.Equals("GET"))
  90. {
  91. DoAgentGet(request, responsedata, agentID, regionID);
  92. return responsedata;
  93. }
  94. else if (method.Equals("DELETE"))
  95. {
  96. DoAgentDelete(request, responsedata, agentID, action, regionID);
  97. return responsedata;
  98. }
  99. else if (method.Equals("QUERYACCESS"))
  100. {
  101. DoQueryAccess(request, responsedata, agentID, regionID);
  102. return responsedata;
  103. }
  104. else
  105. {
  106. m_log.InfoFormat("[AGENT HANDLER]: method {0} not supported in agent message", method);
  107. responsedata["int_response_code"] = HttpStatusCode.MethodNotAllowed;
  108. responsedata["str_response_string"] = "Method not allowed";
  109. return responsedata;
  110. }
  111. }
  112. protected void DoAgentPost(Hashtable request, Hashtable responsedata, UUID id)
  113. {
  114. OSDMap args = Utils.GetOSDMap((string)request["body"]);
  115. if (args == null)
  116. {
  117. responsedata["int_response_code"] = HttpStatusCode.BadRequest;
  118. responsedata["str_response_string"] = "Bad request";
  119. return;
  120. }
  121. // retrieve the input arguments
  122. int x = 0, y = 0;
  123. UUID uuid = UUID.Zero;
  124. string regionname = string.Empty;
  125. uint teleportFlags = 0;
  126. if (args.ContainsKey("destination_x") && args["destination_x"] != null)
  127. Int32.TryParse(args["destination_x"].AsString(), out x);
  128. else
  129. m_log.WarnFormat(" -- request didn't have destination_x");
  130. if (args.ContainsKey("destination_y") && args["destination_y"] != null)
  131. Int32.TryParse(args["destination_y"].AsString(), out y);
  132. else
  133. m_log.WarnFormat(" -- request didn't have destination_y");
  134. if (args.ContainsKey("destination_uuid") && args["destination_uuid"] != null)
  135. UUID.TryParse(args["destination_uuid"].AsString(), out uuid);
  136. if (args.ContainsKey("destination_name") && args["destination_name"] != null)
  137. regionname = args["destination_name"].ToString();
  138. if (args.ContainsKey("teleport_flags") && args["teleport_flags"] != null)
  139. teleportFlags = args["teleport_flags"].AsUInteger();
  140. GridRegion destination = new GridRegion();
  141. destination.RegionID = uuid;
  142. destination.RegionLocX = x;
  143. destination.RegionLocY = y;
  144. destination.RegionName = regionname;
  145. AgentCircuitData aCircuit = new AgentCircuitData();
  146. try
  147. {
  148. aCircuit.UnpackAgentCircuitData(args);
  149. }
  150. catch (Exception ex)
  151. {
  152. m_log.InfoFormat("[AGENT HANDLER]: exception on unpacking ChildCreate message {0}", ex.Message);
  153. responsedata["int_response_code"] = HttpStatusCode.BadRequest;
  154. responsedata["str_response_string"] = "Bad request";
  155. return;
  156. }
  157. OSDMap resp = new OSDMap(2);
  158. string reason = String.Empty;
  159. // This is the meaning of POST agent
  160. //m_regionClient.AdjustUserInformation(aCircuit);
  161. //bool result = m_SimulationService.CreateAgent(destination, aCircuit, teleportFlags, out reason);
  162. bool result = CreateAgent(destination, aCircuit, teleportFlags, out reason);
  163. resp["reason"] = OSD.FromString(reason);
  164. resp["success"] = OSD.FromBoolean(result);
  165. // Let's also send out the IP address of the caller back to the caller (HG 1.5)
  166. resp["your_ip"] = OSD.FromString(GetCallerIP(request));
  167. // TODO: add reason if not String.Empty?
  168. responsedata["int_response_code"] = HttpStatusCode.OK;
  169. responsedata["str_response_string"] = OSDParser.SerializeJsonString(resp);
  170. }
  171. private string GetCallerIP(Hashtable request)
  172. {
  173. if (!m_Proxy)
  174. return Util.GetCallerIP(request);
  175. // We're behind a proxy
  176. Hashtable headers = (Hashtable)request["headers"];
  177. //// DEBUG
  178. //foreach (object o in headers.Keys)
  179. // m_log.DebugFormat("XXX {0} = {1}", o.ToString(), (headers[o] == null? "null" : headers[o].ToString()));
  180. string xff = "X-Forwarded-For";
  181. if (headers.ContainsKey(xff.ToLower()))
  182. xff = xff.ToLower();
  183. if (!headers.ContainsKey(xff) || headers[xff] == null)
  184. {
  185. m_log.WarnFormat("[AGENT HANDLER]: No XFF header");
  186. return Util.GetCallerIP(request);
  187. }
  188. m_log.DebugFormat("[AGENT HANDLER]: XFF is {0}", headers[xff]);
  189. IPEndPoint ep = Util.GetClientIPFromXFF((string)headers[xff]);
  190. if (ep != null)
  191. return ep.Address.ToString();
  192. // Oops
  193. return Util.GetCallerIP(request);
  194. }
  195. // subclasses can override this
  196. protected virtual bool CreateAgent(GridRegion destination, AgentCircuitData aCircuit, uint teleportFlags, out string reason)
  197. {
  198. return m_SimulationService.CreateAgent(destination, aCircuit, teleportFlags, out reason);
  199. }
  200. protected void DoAgentPut(Hashtable request, Hashtable responsedata)
  201. {
  202. OSDMap args = Utils.GetOSDMap((string)request["body"]);
  203. if (args == null)
  204. {
  205. responsedata["int_response_code"] = HttpStatusCode.BadRequest;
  206. responsedata["str_response_string"] = "Bad request";
  207. return;
  208. }
  209. // retrieve the input arguments
  210. int x = 0, y = 0;
  211. UUID uuid = UUID.Zero;
  212. string regionname = string.Empty;
  213. if (args.ContainsKey("destination_x") && args["destination_x"] != null)
  214. Int32.TryParse(args["destination_x"].AsString(), out x);
  215. if (args.ContainsKey("destination_y") && args["destination_y"] != null)
  216. Int32.TryParse(args["destination_y"].AsString(), out y);
  217. if (args.ContainsKey("destination_uuid") && args["destination_uuid"] != null)
  218. UUID.TryParse(args["destination_uuid"].AsString(), out uuid);
  219. if (args.ContainsKey("destination_name") && args["destination_name"] != null)
  220. regionname = args["destination_name"].ToString();
  221. GridRegion destination = new GridRegion();
  222. destination.RegionID = uuid;
  223. destination.RegionLocX = x;
  224. destination.RegionLocY = y;
  225. destination.RegionName = regionname;
  226. string messageType;
  227. if (args["message_type"] != null)
  228. messageType = args["message_type"].AsString();
  229. else
  230. {
  231. m_log.Warn("[AGENT HANDLER]: Agent Put Message Type not found. ");
  232. messageType = "AgentData";
  233. }
  234. bool result = true;
  235. if ("AgentData".Equals(messageType))
  236. {
  237. AgentData agent = new AgentData();
  238. try
  239. {
  240. agent.Unpack(args);
  241. }
  242. catch (Exception ex)
  243. {
  244. m_log.InfoFormat("[AGENT HANDLER]: exception on unpacking ChildAgentUpdate message {0}", ex.Message);
  245. responsedata["int_response_code"] = HttpStatusCode.BadRequest;
  246. responsedata["str_response_string"] = "Bad request";
  247. return;
  248. }
  249. //agent.Dump();
  250. // This is one of the meanings of PUT agent
  251. result = UpdateAgent(destination, agent);
  252. }
  253. else if ("AgentPosition".Equals(messageType))
  254. {
  255. AgentPosition agent = new AgentPosition();
  256. try
  257. {
  258. agent.Unpack(args);
  259. }
  260. catch (Exception ex)
  261. {
  262. m_log.InfoFormat("[AGENT HANDLER]: exception on unpacking ChildAgentUpdate message {0}", ex.Message);
  263. return;
  264. }
  265. //agent.Dump();
  266. // This is one of the meanings of PUT agent
  267. result = m_SimulationService.UpdateAgent(destination, agent);
  268. }
  269. responsedata["int_response_code"] = HttpStatusCode.OK;
  270. responsedata["str_response_string"] = result.ToString();
  271. //responsedata["str_response_string"] = OSDParser.SerializeJsonString(resp); ??? instead
  272. }
  273. // subclasses can override this
  274. protected virtual bool UpdateAgent(GridRegion destination, AgentData agent)
  275. {
  276. return m_SimulationService.UpdateAgent(destination, agent);
  277. }
  278. protected virtual void DoQueryAccess(Hashtable request, Hashtable responsedata, UUID id, UUID regionID)
  279. {
  280. if (m_SimulationService == null)
  281. {
  282. m_log.Debug("[AGENT HANDLER]: Agent QUERY called. Harmless but useless.");
  283. responsedata["content_type"] = "application/json";
  284. responsedata["int_response_code"] = HttpStatusCode.NotImplemented;
  285. responsedata["str_response_string"] = string.Empty;
  286. return;
  287. }
  288. // m_log.DebugFormat("[AGENT HANDLER]: Received QUERYACCESS with {0}", (string)request["body"]);
  289. OSDMap args = Utils.GetOSDMap((string)request["body"]);
  290. Vector3 position = Vector3.Zero;
  291. if (args.ContainsKey("position"))
  292. position = Vector3.Parse(args["position"].AsString());
  293. GridRegion destination = new GridRegion();
  294. destination.RegionID = regionID;
  295. string reason;
  296. bool result = m_SimulationService.QueryAccess(destination, id, position, out reason);
  297. responsedata["int_response_code"] = HttpStatusCode.OK;
  298. OSDMap resp = new OSDMap(2);
  299. resp["success"] = OSD.FromBoolean(result);
  300. resp["reason"] = OSD.FromString(reason);
  301. responsedata["str_response_string"] = OSDParser.SerializeJsonString(resp);
  302. }
  303. protected virtual void DoAgentGet(Hashtable request, Hashtable responsedata, UUID id, UUID regionID)
  304. {
  305. if (m_SimulationService == null)
  306. {
  307. m_log.Debug("[AGENT HANDLER]: Agent GET called. Harmless but useless.");
  308. responsedata["content_type"] = "application/json";
  309. responsedata["int_response_code"] = HttpStatusCode.NotImplemented;
  310. responsedata["str_response_string"] = string.Empty;
  311. return;
  312. }
  313. GridRegion destination = new GridRegion();
  314. destination.RegionID = regionID;
  315. IAgentData agent = null;
  316. bool result = m_SimulationService.RetrieveAgent(destination, id, out agent);
  317. OSDMap map = null;
  318. if (result)
  319. {
  320. if (agent != null) // just to make sure
  321. {
  322. map = agent.Pack();
  323. string strBuffer = "";
  324. try
  325. {
  326. strBuffer = OSDParser.SerializeJsonString(map);
  327. }
  328. catch (Exception e)
  329. {
  330. m_log.WarnFormat("[AGENT HANDLER]: Exception thrown on serialization of DoAgentGet: {0}", e.Message);
  331. responsedata["int_response_code"] = HttpStatusCode.InternalServerError;
  332. // ignore. buffer will be empty, caller should check.
  333. }
  334. responsedata["content_type"] = "application/json";
  335. responsedata["int_response_code"] = HttpStatusCode.OK;
  336. responsedata["str_response_string"] = strBuffer;
  337. }
  338. else
  339. {
  340. responsedata["int_response_code"] = HttpStatusCode.InternalServerError;
  341. responsedata["str_response_string"] = "Internal error";
  342. }
  343. }
  344. else
  345. {
  346. responsedata["int_response_code"] = HttpStatusCode.NotFound;
  347. responsedata["str_response_string"] = "Not Found";
  348. }
  349. }
  350. protected void DoAgentDelete(Hashtable request, Hashtable responsedata, UUID id, string action, UUID regionID)
  351. {
  352. m_log.Debug(" >>> DoDelete action:" + action + "; RegionID:" + regionID);
  353. GridRegion destination = new GridRegion();
  354. destination.RegionID = regionID;
  355. if (action.Equals("release"))
  356. ReleaseAgent(regionID, id);
  357. else
  358. m_SimulationService.CloseAgent(destination, id);
  359. responsedata["int_response_code"] = HttpStatusCode.OK;
  360. responsedata["str_response_string"] = "OpenSim agent " + id.ToString();
  361. m_log.Debug("[AGENT HANDLER]: Agent Released/Deleted.");
  362. }
  363. protected virtual void ReleaseAgent(UUID regionID, UUID id)
  364. {
  365. m_SimulationService.ReleaseAgent(regionID, id, "");
  366. }
  367. }
  368. }