MapAddServerConnector.cs 8.0 KB

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