OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
MapAddServerConnector.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections.Generic;
30 using System.IO;
31 using System.Reflection;
32 using System.Xml;
33 
34 using Nini.Config;
35 using log4net;
36 using OpenMetaverse;
37 
38 using OpenSim.Framework;
39 using OpenSim.Server.Base;
40 using OpenSim.Services.Interfaces;
41 using OpenSim.Framework.ServiceAuth;
42 using OpenSim.Framework.Servers.HttpServer;
43 using OpenSim.Server.Handlers.Base;
44 
46 
47 namespace OpenSim.Server.Handlers.MapImage
48 {
50  {
51  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
52 
53  private IMapImageService m_MapService;
54  private IGridService m_GridService;
55  private string m_ConfigName = "MapImageService";
56 
57  public MapAddServiceConnector(IConfigSource config, IHttpServer server, string configName) :
58  base(config, server, configName)
59  {
60  IConfig serverConfig = config.Configs[m_ConfigName];
61  if (serverConfig == null)
62  throw new Exception(String.Format("No section {0} in config file", m_ConfigName));
63 
64  string mapService = serverConfig.GetString("LocalServiceModule",
65  String.Empty);
66 
67  if (mapService == String.Empty)
68  throw new Exception("No LocalServiceModule in config file");
69 
70  Object[] args = new Object[] { config };
71  m_MapService = ServerUtils.LoadPlugin<IMapImageService>(mapService, args);
72 
73  string gridService = serverConfig.GetString("GridService", String.Empty);
74  if (gridService != string.Empty)
75  m_GridService = ServerUtils.LoadPlugin<IGridService>(gridService, args);
76 
77  if (m_GridService != null)
78  m_log.InfoFormat("[MAP IMAGE HANDLER]: GridService check is ON");
79  else
80  m_log.InfoFormat("[MAP IMAGE HANDLER]: GridService check is OFF");
81 
82  bool proxy = serverConfig.GetBoolean("HasProxy", false);
83  IServiceAuth auth = ServiceAuth.Create(config, m_ConfigName);
84  server.AddStreamHandler(new MapServerPostHandler(m_MapService, m_GridService, proxy, auth));
85 
86  }
87  }
88 
90  {
91  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
92  private IMapImageService m_MapService;
93  private IGridService m_GridService;
94  bool m_Proxy;
95 
96  public MapServerPostHandler(IMapImageService service, IGridService grid, bool proxy, IServiceAuth auth) :
97  base("POST", "/map", auth)
98  {
99  m_MapService = service;
100  m_GridService = grid;
101  m_Proxy = proxy;
102  }
103 
104  protected override byte[] ProcessRequest(string path, Stream requestData, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
105  {
106 // m_log.DebugFormat("[MAP SERVICE IMAGE HANDLER]: Received {0}", path);
107  StreamReader sr = new StreamReader(requestData);
108  string body = sr.ReadToEnd();
109  sr.Close();
110  body = body.Trim();
111 
112  try
113  {
114  Dictionary<string, object> request = ServerUtils.ParseQueryString(body);
115 
116  if (!request.ContainsKey("X") || !request.ContainsKey("Y") || !request.ContainsKey("DATA"))
117  {
118  httpResponse.StatusCode = (int)OSHttpStatusCode.ClientErrorBadRequest;
119  return FailureResult("Bad request.");
120  }
121  int x = 0, y = 0;
122 // UUID scopeID = new UUID("07f8d88e-cd5e-4239-a0ed-843f75d09992");
123  UUID scopeID = UUID.Zero;
124  Int32.TryParse(request["X"].ToString(), out x);
125  Int32.TryParse(request["Y"].ToString(), out y);
126  if (request.ContainsKey("SCOPE"))
127  UUID.TryParse(request["SCOPE"].ToString(), out scopeID);
128 
129  m_log.DebugFormat("[MAP ADD SERVER CONNECTOR]: Received map data for region at {0}-{1}", x, y);
130 
131 // string type = "image/jpeg";
132 //
133 // if (request.ContainsKey("TYPE"))
134 // type = request["TYPE"].ToString();
135 
136  if (m_GridService != null)
137  {
138  System.Net.IPAddress ipAddr = GetCallerIP(httpRequest);
139  GridRegion r = m_GridService.GetRegionByPosition(UUID.Zero, (int)Util.RegionToWorldLoc((uint)x), (int)Util.RegionToWorldLoc((uint)y));
140  if (r != null)
141  {
142  if (r.ExternalEndPoint.Address.ToString() != ipAddr.ToString())
143  {
144  m_log.WarnFormat("[MAP IMAGE HANDLER]: IP address {0} may be trying to impersonate region in IP {1}", ipAddr, r.ExternalEndPoint.Address);
145  return FailureResult("IP address of caller does not match IP address of registered region");
146  }
147 
148  }
149  else
150  {
151  m_log.WarnFormat("[MAP IMAGE HANDLER]: IP address {0} may be rogue. Region not found at coordinates {1}-{2}",
152  ipAddr, x, y);
153  return FailureResult("Region not found at given coordinates");
154  }
155  }
156 
157  byte[] data = Convert.FromBase64String(request["DATA"].ToString());
158 
159  string reason = string.Empty;
160 
161  bool result = m_MapService.AddMapTile((int)x, (int)y, data, scopeID, out reason);
162 
163  if (result)
164  return SuccessResult();
165  else
166  return FailureResult(reason);
167 
168  }
169  catch (Exception e)
170  {
171  m_log.ErrorFormat("[MAP SERVICE IMAGE HANDLER]: Exception {0} {1}", e.Message, e.StackTrace);
172  }
173 
174  return FailureResult("Unexpected server error");
175  }
176 
177  private byte[] SuccessResult()
178  {
179  XmlDocument doc = new XmlDocument();
180 
181  XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
182  "", "");
183 
184  doc.AppendChild(xmlnode);
185 
186  XmlElement rootElement = doc.CreateElement("", "ServerResponse",
187  "");
188 
189  doc.AppendChild(rootElement);
190 
191  XmlElement result = doc.CreateElement("", "Result", "");
192  result.AppendChild(doc.CreateTextNode("Success"));
193 
194  rootElement.AppendChild(result);
195 
196  return Util.DocToBytes(doc);
197  }
198 
199  private byte[] FailureResult(string msg)
200  {
201  XmlDocument doc = new XmlDocument();
202 
203  XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
204  "", "");
205 
206  doc.AppendChild(xmlnode);
207 
208  XmlElement rootElement = doc.CreateElement("", "ServerResponse",
209  "");
210 
211  doc.AppendChild(rootElement);
212 
213  XmlElement result = doc.CreateElement("", "Result", "");
214  result.AppendChild(doc.CreateTextNode("Failure"));
215 
216  rootElement.AppendChild(result);
217 
218  XmlElement message = doc.CreateElement("", "Message", "");
219  message.AppendChild(doc.CreateTextNode(msg));
220 
221  rootElement.AppendChild(message);
222 
223  return Util.DocToBytes(doc);
224  }
225 
226  private System.Net.IPAddress GetCallerIP(IOSHttpRequest request)
227  {
228  if (!m_Proxy)
229  return request.RemoteIPEndPoint.Address;
230 
231  // We're behind a proxy
232  string xff = "X-Forwarded-For";
233  string xffValue = request.Headers[xff.ToLower()];
234  if (xffValue == null || (xffValue != null && xffValue == string.Empty))
235  xffValue = request.Headers[xff];
236 
237  if (xffValue == null || (xffValue != null && xffValue == string.Empty))
238  {
239  m_log.WarnFormat("[MAP IMAGE HANDLER]: No XFF header");
240  return request.RemoteIPEndPoint.Address;
241  }
242 
243  System.Net.IPEndPoint ep = Util.GetClientIPFromXFF(xffValue);
244  if (ep != null)
245  return ep.Address;
246 
247  // Oops
248  return request.RemoteIPEndPoint.Address;
249  }
250 
251  }
252 }
OpenSim.Services.Interfaces.GridRegion GridRegion
MapServerPostHandler(IMapImageService service, IGridService grid, bool proxy, IServiceAuth auth)
MapAddServiceConnector(IConfigSource config, IHttpServer server, string configName)
Interface to OpenSimulator's built in HTTP server. Use this to register handlers (http, llsd, xmlrpc, etc.) for given URLs.
Definition: IHttpServer.cs:36
Interactive OpenSim region server
Definition: OpenSim.cs:55
OSHttpStatusCode
HTTP status codes (almost) as defined by W3C in http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html and IETF in http://tools.ietf.org/html/rfc6585
override byte[] ProcessRequest(string path, Stream requestData, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)