MapAddServerConnector.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.Generic;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Net;
  32. using System.Xml;
  33. using Nini.Config;
  34. using log4net;
  35. using OpenMetaverse;
  36. using OpenSim.Framework;
  37. using OpenSim.Server.Base;
  38. using OpenSim.Services.Interfaces;
  39. using OpenSim.Framework.ServiceAuth;
  40. using OpenSim.Framework.Servers.HttpServer;
  41. using OpenSim.Server.Handlers.Base;
  42. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  43. namespace OpenSim.Server.Handlers.MapImage
  44. {
  45. public class MapAddServiceConnector : ServiceConnector
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private IMapImageService m_MapService;
  49. private IGridService m_GridService;
  50. private string m_ConfigName = "MapImageService";
  51. public MapAddServiceConnector(IConfigSource config, IHttpServer server, string configName) :
  52. base(config, server, configName)
  53. {
  54. IConfig serverConfig = config.Configs[m_ConfigName];
  55. if (serverConfig == null)
  56. throw new Exception(String.Format("No section {0} in config file", m_ConfigName));
  57. string mapService = serverConfig.GetString("LocalServiceModule",
  58. String.Empty);
  59. if (mapService == String.Empty)
  60. throw new Exception("No LocalServiceModule in config file");
  61. Object[] args = new Object[] { config };
  62. m_MapService = ServerUtils.LoadPlugin<IMapImageService>(mapService, args);
  63. string gridService = serverConfig.GetString("GridService", String.Empty);
  64. if (gridService != string.Empty)
  65. m_GridService = ServerUtils.LoadPlugin<IGridService>(gridService, args);
  66. if (m_GridService != null)
  67. m_log.InfoFormat("[MAP IMAGE HANDLER]: GridService check is ON");
  68. else
  69. m_log.InfoFormat("[MAP IMAGE HANDLER]: GridService check is OFF");
  70. bool proxy = serverConfig.GetBoolean("HasProxy", false);
  71. IServiceAuth auth = ServiceAuth.Create(config, m_ConfigName);
  72. server.AddStreamHandler(new MapServerPostHandler(m_MapService, m_GridService, proxy, auth));
  73. }
  74. }
  75. class MapServerPostHandler : BaseStreamHandler
  76. {
  77. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  78. private IMapImageService m_MapService;
  79. private IGridService m_GridService;
  80. bool m_Proxy;
  81. public MapServerPostHandler(IMapImageService service, IGridService grid, bool proxy, IServiceAuth auth) :
  82. base("POST", "/map", auth)
  83. {
  84. m_MapService = service;
  85. m_GridService = grid;
  86. m_Proxy = proxy;
  87. }
  88. protected override byte[] ProcessRequest(string path, Stream requestData, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  89. {
  90. // m_log.DebugFormat("[MAP SERVICE IMAGE HANDLER]: Received {0}", path);
  91. string body;
  92. using(StreamReader sr = new StreamReader(requestData))
  93. body = sr.ReadToEnd();
  94. body = body.Trim();
  95. try
  96. {
  97. Dictionary<string, object> request = ServerUtils.ParseQueryString(body);
  98. if (!request.ContainsKey("X") || !request.ContainsKey("Y") || !request.ContainsKey("DATA"))
  99. {
  100. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  101. return FailureResult("Bad request.");
  102. }
  103. int x = 0, y = 0;
  104. // UUID scopeID = new UUID("07f8d88e-cd5e-4239-a0ed-843f75d09992");
  105. UUID scopeID = UUID.Zero;
  106. Int32.TryParse(request["X"].ToString(), out x);
  107. Int32.TryParse(request["Y"].ToString(), out y);
  108. if (request.ContainsKey("SCOPE"))
  109. UUID.TryParse(request["SCOPE"].ToString(), out scopeID);
  110. m_log.DebugFormat("[MAP ADD SERVER CONNECTOR]: Received map data for region at {0}-{1}", x, y);
  111. // string type = "image/jpeg";
  112. //
  113. // if (request.ContainsKey("TYPE"))
  114. // type = request["TYPE"].ToString();
  115. if (m_GridService != null)
  116. {
  117. IPAddress ipAddr = httpRequest.RemoteIPEndPoint.Address;
  118. GridRegion r = m_GridService.GetRegionByPosition(UUID.Zero, (int)Util.RegionToWorldLoc((uint)x), (int)Util.RegionToWorldLoc((uint)y));
  119. if (r != null)
  120. {
  121. if (r.ExternalEndPoint.Address.ToString() != ipAddr.ToString())
  122. {
  123. m_log.WarnFormat("[MAP IMAGE HANDLER]: IP address {0} may be trying to impersonate region in IP {1}", ipAddr, r.ExternalEndPoint.Address);
  124. return FailureResult("IP address of caller does not match IP address of registered region");
  125. }
  126. }
  127. else
  128. {
  129. m_log.WarnFormat("[MAP IMAGE HANDLER]: IP address {0} may be rogue. Region not found at coordinates {1}-{2}",
  130. ipAddr, x, y);
  131. return FailureResult("Region not found at given coordinates");
  132. }
  133. }
  134. byte[] data = Convert.FromBase64String(request["DATA"].ToString());
  135. string reason = string.Empty;
  136. bool result = m_MapService.AddMapTile((int)x, (int)y, data, scopeID, out reason);
  137. if (result)
  138. return SuccessResult();
  139. else
  140. return FailureResult(reason);
  141. }
  142. catch (Exception e)
  143. {
  144. m_log.ErrorFormat("[MAP SERVICE IMAGE HANDLER]: Exception {0} {1}", e.Message, e.StackTrace);
  145. }
  146. return FailureResult("Unexpected server error");
  147. }
  148. private byte[] SuccessResult()
  149. {
  150. XmlDocument doc = new XmlDocument();
  151. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  152. "", "");
  153. doc.AppendChild(xmlnode);
  154. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  155. "");
  156. doc.AppendChild(rootElement);
  157. XmlElement result = doc.CreateElement("", "Result", "");
  158. result.AppendChild(doc.CreateTextNode("Success"));
  159. rootElement.AppendChild(result);
  160. return Util.DocToBytes(doc);
  161. }
  162. private byte[] FailureResult(string msg)
  163. {
  164. XmlDocument doc = new XmlDocument();
  165. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  166. "", "");
  167. doc.AppendChild(xmlnode);
  168. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  169. "");
  170. doc.AppendChild(rootElement);
  171. XmlElement result = doc.CreateElement("", "Result", "");
  172. result.AppendChild(doc.CreateTextNode("Failure"));
  173. rootElement.AppendChild(result);
  174. XmlElement message = doc.CreateElement("", "Message", "");
  175. message.AppendChild(doc.CreateTextNode(msg));
  176. rootElement.AppendChild(message);
  177. return Util.DocToBytes(doc);
  178. }
  179. }
  180. }