GridInfoHandlers.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.Net;
  31. using System.Reflection;
  32. using System.Security;
  33. using log4net;
  34. using Nini.Config;
  35. using Nwc.XmlRpc;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Servers.HttpServer;
  38. using OpenMetaverse;
  39. using OpenMetaverse.StructuredData;
  40. namespace OpenSim.Server.Handlers.Grid
  41. {
  42. public class GridInfoHandlers
  43. {
  44. private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private IConfigSource m_Config;
  46. private Dictionary<string, string> _info = new Dictionary<string, string>();
  47. private byte[] cachedJsonAnswer = null;
  48. private byte[] cachedRestAnswer = null;
  49. /// <summary>
  50. /// Instantiate a GridInfoService object.
  51. /// </summary>
  52. /// <param name="configPath">path to config path containing
  53. /// grid information</param>
  54. /// <remarks>
  55. /// GridInfoService uses the [GridInfo] section of the
  56. /// standard OpenSim.ini file --- which is not optimal, but
  57. /// anything else requires a general redesign of the config
  58. /// system.
  59. /// </remarks>
  60. public GridInfoHandlers(IConfigSource configSource)
  61. {
  62. m_Config = configSource;
  63. loadGridInfo(configSource);
  64. }
  65. private void loadGridInfo(IConfigSource configSource)
  66. {
  67. _info["platform"] = "OpenSim";
  68. try
  69. {
  70. IConfig gridCfg = configSource.Configs["GridInfoService"];
  71. if (gridCfg != null)
  72. {
  73. foreach (string k in gridCfg.GetKeys())
  74. _info[k] = gridCfg.GetString(k);
  75. }
  76. else
  77. {
  78. IConfig netCfg = configSource.Configs["Network"];
  79. if (netCfg != null)
  80. {
  81. _info["login"] = string.Format("http://127.0.0.1:{0}/",
  82. netCfg.GetString("http_listener_port", ConfigSettings.DefaultRegionHttpPort.ToString()));
  83. }
  84. else
  85. {
  86. _info["login"] = "http://127.0.0.1:9000/";
  87. }
  88. IssueWarning();
  89. }
  90. _info.TryGetValue("home", out string tmp);
  91. tmp = Util.GetConfigVarFromSections<string>(m_Config, "HomeURI",
  92. new string[] { "Startup", "Hypergrid" }, tmp);
  93. if (string.IsNullOrEmpty(tmp))
  94. {
  95. IConfig logincfg = m_Config.Configs["LoginService"];
  96. if (logincfg != null)
  97. tmp = logincfg.GetString("SRV_HomeURI", tmp);
  98. }
  99. if (!string.IsNullOrEmpty(tmp))
  100. _info["home"] = OSD.FromString(tmp);
  101. tmp = Util.GetConfigVarFromSections<string>(m_Config, "HomeURIAlias",
  102. new string[] { "Startup", "Hypergrid" }, string.Empty);
  103. if (!string.IsNullOrEmpty(tmp))
  104. _info["homealias"] = OSD.FromString(tmp);
  105. _info.TryGetValue("gatekeeper", out tmp);
  106. tmp = Util.GetConfigVarFromSections<string>(m_Config, "GatekeeperURI",
  107. new string[] { "Startup", "Hypergrid" }, tmp);
  108. if (!string.IsNullOrEmpty(tmp))
  109. _info["gatekeeper"] = OSD.FromString(tmp);
  110. tmp = Util.GetConfigVarFromSections<string>(m_Config, "GatekeeperURIAlias",
  111. new string[] { "Startup", "Hypergrid" }, string.Empty);
  112. if (!string.IsNullOrEmpty(tmp))
  113. _info["gatekeeperalias"] = OSD.FromString(tmp);
  114. }
  115. catch (Exception)
  116. {
  117. _log.Warn("[GRID INFO SERVICE]: Cannot get grid info from config source, using minimal defaults");
  118. }
  119. _log.DebugFormat("[GRID INFO SERVICE]: Grid info service initialized with {0} keys", _info.Count);
  120. }
  121. private void IssueWarning()
  122. {
  123. _log.Warn("[GRID INFO SERVICE]: found no [GridInfoService] section in your configuration files");
  124. _log.Warn("[GRID INFO SERVICE]: trying to guess sensible defaults, you might want to provide better ones:");
  125. foreach (string k in _info.Keys)
  126. {
  127. _log.WarnFormat("[GRID INFO SERVICE]: {0}: {1}", k, _info[k]);
  128. }
  129. }
  130. public XmlRpcResponse XmlRpcGridInfoMethod(XmlRpcRequest request, IPEndPoint remoteClient)
  131. {
  132. XmlRpcResponse response = new XmlRpcResponse();
  133. Hashtable responseData = new Hashtable();
  134. _log.Debug("[GRID INFO SERVICE]: Request for grid info");
  135. foreach (KeyValuePair<string, string> k in _info)
  136. {
  137. responseData[k.Key] = k.Value;
  138. }
  139. response.Value = responseData;
  140. return response;
  141. }
  142. public void RestGetGridInfoMethod(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  143. {
  144. httpResponse.KeepAlive = false;
  145. if (httpRequest.HttpMethod != "GET")
  146. {
  147. httpResponse.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
  148. return;
  149. }
  150. if(cachedRestAnswer == null)
  151. {
  152. osUTF8 osb = OSUTF8Cached.Acquire();
  153. osb.AppendASCII("<gridinfo>");
  154. foreach (KeyValuePair<string, string> k in _info)
  155. {
  156. osb.AppendASCII('<');
  157. osb.AppendASCII(k.Key);
  158. osb.AppendASCII('>');
  159. osb.AppendASCII(SecurityElement.Escape(k.Value.ToString()));
  160. osb.AppendASCII("</");
  161. osb.AppendASCII(k.Key);
  162. osb.AppendASCII('>');
  163. }
  164. osb.AppendASCII("</gridinfo>");
  165. cachedRestAnswer = OSUTF8Cached.GetArrayAndRelease(osb);
  166. }
  167. httpResponse.ContentType = "application/xml";
  168. httpResponse.RawBuffer = cachedRestAnswer;
  169. }
  170. /// <summary>
  171. /// Get GridInfo in json format: Used by the OSSL osGetGrid*
  172. /// Adding the SRV_HomeURI to the kvp returned for use in scripts
  173. /// </summary>
  174. /// <returns>
  175. /// json string
  176. /// </returns>
  177. /// </param>
  178. /// <param name='httpRequest'>
  179. /// Http request.
  180. /// </param>
  181. /// <param name='httpResponse'>
  182. /// Http response.
  183. /// </param>
  184. public void JsonGetGridInfoMethod(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  185. {
  186. httpResponse.KeepAlive = false;
  187. if (httpRequest.HttpMethod != "GET")
  188. {
  189. httpResponse.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
  190. return;
  191. }
  192. if (cachedJsonAnswer == null)
  193. {
  194. OSDMap map = new OSDMap();
  195. foreach (KeyValuePair<string, string> k in _info)
  196. {
  197. map[k.Key] = OSD.FromString(k.Value.ToString());
  198. }
  199. cachedJsonAnswer = OSDParser.SerializeJsonToBytes(map);
  200. }
  201. httpResponse.ContentType = "application/json";
  202. httpResponse.RawBuffer = cachedJsonAnswer;
  203. }
  204. }
  205. }