1
0

GridRestModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.Reflection;
  32. using System.Xml;
  33. using log4net;
  34. using OpenMetaverse;
  35. using OpenSim.Data;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Communications;
  38. using OpenSim.Framework.Servers.HttpServer;
  39. using OpenSim.Grid.Framework;
  40. namespace OpenSim.Grid.GridServer.Modules
  41. {
  42. public class GridRestModule
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private GridDBService m_gridDBService;
  46. private IGridServiceCore m_gridCore;
  47. protected GridConfig m_config;
  48. /// <value>
  49. /// Used to notify old regions as to which OpenSim version to upgrade to
  50. /// </value>
  51. //private string m_opensimVersion;
  52. protected BaseHttpServer m_httpServer;
  53. /// <summary>
  54. /// Constructor
  55. /// </summary>
  56. /// <param name="opensimVersion">
  57. /// Used to notify old regions as to which OpenSim version to upgrade to
  58. /// </param>
  59. public GridRestModule()
  60. {
  61. }
  62. public void Initialise(string opensimVersion, GridDBService gridDBService, IGridServiceCore gridCore, GridConfig config)
  63. {
  64. //m_opensimVersion = opensimVersion;
  65. m_gridDBService = gridDBService;
  66. m_gridCore = gridCore;
  67. m_config = config;
  68. RegisterHandlers();
  69. }
  70. public void PostInitialise()
  71. {
  72. }
  73. public void RegisterHandlers()
  74. {
  75. //have these in separate method as some servers restart the http server and reregister all the handlers.
  76. m_httpServer = m_gridCore.GetHttpServer();
  77. m_httpServer.AddStreamHandler(new RestStreamHandler("GET", "/sims/", RestGetSimMethod));
  78. m_httpServer.AddStreamHandler(new RestStreamHandler("POST", "/sims/", RestSetSimMethod));
  79. m_httpServer.AddStreamHandler(new RestStreamHandler("GET", "/regions/", RestGetRegionMethod));
  80. m_httpServer.AddStreamHandler(new RestStreamHandler("POST", "/regions/", RestSetRegionMethod));
  81. }
  82. /// <summary>
  83. /// Performs a REST Get Operation
  84. /// </summary>
  85. /// <param name="request"></param>
  86. /// <param name="path"></param>
  87. /// <param name="param"></param>
  88. /// <param name="httpRequest">HTTP request header object</param>
  89. /// <param name="httpResponse">HTTP response header object</param>
  90. /// <returns></returns>
  91. public string RestGetRegionMethod(string request, string path, string param,
  92. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  93. {
  94. return RestGetSimMethod(String.Empty, "/sims/", param, httpRequest, httpResponse);
  95. }
  96. /// <summary>
  97. /// Performs a REST Set Operation
  98. /// </summary>
  99. /// <param name="request"></param>
  100. /// <param name="path"></param>
  101. /// <param name="param"></param>
  102. /// <param name="httpRequest">HTTP request header object</param>
  103. /// <param name="httpResponse">HTTP response header object</param>
  104. /// <returns></returns>
  105. public string RestSetRegionMethod(string request, string path, string param,
  106. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  107. {
  108. return RestSetSimMethod(String.Empty, "/sims/", param, httpRequest, httpResponse);
  109. }
  110. /// <summary>
  111. /// Returns information about a sim via a REST Request
  112. /// </summary>
  113. /// <param name="request"></param>
  114. /// <param name="path"></param>
  115. /// <param name="param">A string representing the sim's UUID</param>
  116. /// <param name="httpRequest">HTTP request header object</param>
  117. /// <param name="httpResponse">HTTP response header object</param>
  118. /// <returns>Information about the sim in XML</returns>
  119. public string RestGetSimMethod(string request, string path, string param,
  120. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  121. {
  122. string respstring = String.Empty;
  123. RegionProfileData TheSim;
  124. UUID UUID;
  125. if (UUID.TryParse(param, out UUID))
  126. {
  127. TheSim = m_gridDBService.GetRegion(UUID);
  128. if (!(TheSim == null))
  129. {
  130. respstring = "<Root>";
  131. respstring += "<authkey>" + TheSim.regionSendKey + "</authkey>";
  132. respstring += "<sim>";
  133. respstring += "<uuid>" + TheSim.UUID.ToString() + "</uuid>";
  134. respstring += "<regionname>" + TheSim.regionName + "</regionname>";
  135. respstring += "<sim_ip>" + TheSim.serverIP + "</sim_ip>";
  136. respstring += "<sim_port>" + TheSim.serverPort.ToString() + "</sim_port>";
  137. respstring += "<region_locx>" + TheSim.regionLocX.ToString() + "</region_locx>";
  138. respstring += "<region_locy>" + TheSim.regionLocY.ToString() + "</region_locy>";
  139. respstring += "<estate_id>1</estate_id>";
  140. respstring += "</sim>";
  141. respstring += "</Root>";
  142. }
  143. }
  144. else
  145. {
  146. respstring = "<Root>";
  147. respstring += "<error>Param must be a UUID</error>";
  148. respstring += "</Root>";
  149. }
  150. return respstring;
  151. }
  152. /// <summary>
  153. /// Creates or updates a sim via a REST Method Request
  154. /// BROKEN with SQL Update
  155. /// </summary>
  156. /// <param name="request"></param>
  157. /// <param name="path"></param>
  158. /// <param name="param"></param>
  159. /// <param name="httpRequest">HTTP request header object</param>
  160. /// <param name="httpResponse">HTTP response header object</param>
  161. /// <returns>"OK" or an error</returns>
  162. public string RestSetSimMethod(string request, string path, string param,
  163. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  164. {
  165. m_log.Info("Processing region update via REST method");
  166. RegionProfileData theSim;
  167. theSim = m_gridDBService.GetRegion(new UUID(param));
  168. if (theSim == null)
  169. {
  170. theSim = new RegionProfileData();
  171. UUID UUID = new UUID(param);
  172. theSim.UUID = UUID;
  173. theSim.regionRecvKey = m_config.SimRecvKey;
  174. }
  175. XmlDocument doc = new XmlDocument();
  176. doc.LoadXml(request);
  177. XmlNode rootnode = doc.FirstChild;
  178. XmlNode authkeynode = rootnode.ChildNodes[0];
  179. if (authkeynode.Name != "authkey")
  180. {
  181. return "ERROR! bad XML - expected authkey tag";
  182. }
  183. XmlNode simnode = rootnode.ChildNodes[1];
  184. if (simnode.Name != "sim")
  185. {
  186. return "ERROR! bad XML - expected sim tag";
  187. }
  188. //theSim.regionSendKey = Cfg;
  189. theSim.regionRecvKey = m_config.SimRecvKey;
  190. theSim.regionSendKey = m_config.SimSendKey;
  191. theSim.regionSecret = m_config.SimRecvKey;
  192. theSim.regionDataURI = String.Empty;
  193. theSim.regionAssetURI = m_config.DefaultAssetServer;
  194. theSim.regionAssetRecvKey = m_config.AssetRecvKey;
  195. theSim.regionAssetSendKey = m_config.AssetSendKey;
  196. theSim.regionUserURI = m_config.DefaultUserServer;
  197. theSim.regionUserSendKey = m_config.UserSendKey;
  198. theSim.regionUserRecvKey = m_config.UserRecvKey;
  199. for (int i = 0; i < simnode.ChildNodes.Count; i++)
  200. {
  201. switch (simnode.ChildNodes[i].Name)
  202. {
  203. case "regionname":
  204. theSim.regionName = simnode.ChildNodes[i].InnerText;
  205. break;
  206. case "sim_ip":
  207. theSim.serverIP = simnode.ChildNodes[i].InnerText;
  208. break;
  209. case "sim_port":
  210. theSim.serverPort = Convert.ToUInt32(simnode.ChildNodes[i].InnerText);
  211. break;
  212. case "region_locx":
  213. theSim.regionLocX = Convert.ToUInt32((string)simnode.ChildNodes[i].InnerText);
  214. theSim.regionHandle = Utils.UIntsToLong((theSim.regionLocX * Constants.RegionSize), (theSim.regionLocY * Constants.RegionSize));
  215. break;
  216. case "region_locy":
  217. theSim.regionLocY = Convert.ToUInt32((string)simnode.ChildNodes[i].InnerText);
  218. theSim.regionHandle = Utils.UIntsToLong((theSim.regionLocX * Constants.RegionSize), (theSim.regionLocY * Constants.RegionSize));
  219. break;
  220. }
  221. }
  222. theSim.serverURI = "http://" + theSim.serverIP + ":" + theSim.serverPort + "/";
  223. bool requirePublic = false;
  224. bool requireValid = true;
  225. if (requirePublic &&
  226. (theSim.serverIP.StartsWith("172.16") || theSim.serverIP.StartsWith("192.168") ||
  227. theSim.serverIP.StartsWith("10.") || theSim.serverIP.StartsWith("0.") ||
  228. theSim.serverIP.StartsWith("255.")))
  229. {
  230. return "ERROR! Servers must register with public addresses.";
  231. }
  232. if (requireValid && (theSim.serverIP.StartsWith("0.") || theSim.serverIP.StartsWith("255.")))
  233. {
  234. return "ERROR! 0.*.*.* / 255.*.*.* Addresses are invalid, please check your server config and try again";
  235. }
  236. try
  237. {
  238. m_log.Info("[DATA]: " +
  239. "Updating / adding via " + m_gridDBService.GetNumberOfPlugins() + " storage provider(s) registered.");
  240. return m_gridDBService.CheckReservations(theSim, authkeynode);
  241. }
  242. catch (Exception e)
  243. {
  244. return "ERROR! Could not save to database! (" + e.ToString() + ")";
  245. }
  246. }
  247. }
  248. }