aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Server/Handlers/Map/MapRemoveServerConnector.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Server/Handlers/Map/MapRemoveServerConnector.cs')
-rw-r--r--OpenSim/Server/Handlers/Map/MapRemoveServerConnector.cs249
1 files changed, 249 insertions, 0 deletions
diff --git a/OpenSim/Server/Handlers/Map/MapRemoveServerConnector.cs b/OpenSim/Server/Handlers/Map/MapRemoveServerConnector.cs
new file mode 100644
index 0000000..80e3535
--- /dev/null
+++ b/OpenSim/Server/Handlers/Map/MapRemoveServerConnector.cs
@@ -0,0 +1,249 @@
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
28using System;
29using System.Collections.Generic;
30using System.IO;
31using System.Reflection;
32using System.Xml;
33
34using Nini.Config;
35using log4net;
36using OpenMetaverse;
37
38using OpenSim.Framework;
39using OpenSim.Server.Base;
40using OpenSim.Services.Interfaces;
41using OpenSim.Framework.Servers.HttpServer;
42using OpenSim.Server.Handlers.Base;
43
44using GridRegion = OpenSim.Services.Interfaces.GridRegion;
45
46namespace OpenSim.Server.Handlers.MapImage
47{
48 public class MapRemoveServiceConnector : ServiceConnector
49 {
50 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
51
52 private IMapImageService m_MapService;
53 private IGridService m_GridService;
54 private string m_ConfigName = "MapImageService";
55
56 public MapRemoveServiceConnector(IConfigSource config, IHttpServer server, string configName) :
57 base(config, server, configName)
58 {
59 IConfig serverConfig = config.Configs[m_ConfigName];
60 if (serverConfig == null)
61 throw new Exception(String.Format("No section {0} in config file", m_ConfigName));
62
63 string mapService = serverConfig.GetString("LocalServiceModule",
64 String.Empty);
65
66 if (mapService == String.Empty)
67 throw new Exception("No LocalServiceModule in config file");
68
69 Object[] args = new Object[] { config };
70 m_MapService = ServerUtils.LoadPlugin<IMapImageService>(mapService, args);
71
72 string gridService = serverConfig.GetString("GridService", String.Empty);
73 if (gridService != string.Empty)
74 m_GridService = ServerUtils.LoadPlugin<IGridService>(gridService, args);
75
76 if (m_GridService != null)
77 m_log.InfoFormat("[MAP IMAGE HANDLER]: GridService check is ON");
78 else
79 m_log.InfoFormat("[MAP IMAGE HANDLER]: GridService check is OFF");
80
81 bool proxy = serverConfig.GetBoolean("HasProxy", false);
82 server.AddStreamHandler(new MapServerRemoveHandler(m_MapService, m_GridService, proxy));
83
84 }
85 }
86
87 class MapServerRemoveHandler : BaseStreamHandler
88 {
89 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
90 private IMapImageService m_MapService;
91 private IGridService m_GridService;
92 bool m_Proxy;
93
94 public MapServerRemoveHandler(IMapImageService service, IGridService grid, bool proxy) :
95 base("POST", "/removemap")
96 {
97 m_MapService = service;
98 m_GridService = grid;
99 m_Proxy = proxy;
100 }
101
102 public override byte[] Handle(string path, Stream requestData, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
103 {
104// m_log.DebugFormat("[MAP SERVICE IMAGE HANDLER]: Received {0}", path);
105 StreamReader sr = new StreamReader(requestData);
106 string body = sr.ReadToEnd();
107 sr.Close();
108 body = body.Trim();
109
110 try
111 {
112 Dictionary<string, object> request = ServerUtils.ParseQueryString(body);
113
114 if (!request.ContainsKey("X") || !request.ContainsKey("Y"))
115 {
116 httpResponse.StatusCode = (int)OSHttpStatusCode.ClientErrorBadRequest;
117 return FailureResult("Bad request.");
118 }
119 int x = 0, y = 0;
120 Int32.TryParse(request["X"].ToString(), out x);
121 Int32.TryParse(request["Y"].ToString(), out y);
122
123 m_log.DebugFormat("[MAP REMOVE SERVER CONNECTOR]: Received position data for region at {0}-{1}", x, y);
124
125 if (m_GridService != null)
126 {
127 System.Net.IPAddress ipAddr = GetCallerIP(httpRequest);
128 GridRegion r = m_GridService.GetRegionByPosition(UUID.Zero, x * (int)Constants.RegionSize, y * (int)Constants.RegionSize);
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 return FailureResult("IP address of caller does not match IP address of registered region");
135 }
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 return FailureResult("Region not found at given coordinates");
143 }
144 }
145
146 string reason = string.Empty;
147 bool result = m_MapService.RemoveMapTile(x, y, out reason);
148
149 if (result)
150 return SuccessResult();
151 else
152 return FailureResult(reason);
153
154 }
155 catch (Exception e)
156 {
157 m_log.ErrorFormat("[MAP SERVICE IMAGE HANDLER]: Exception {0} {1}", e.Message, e.StackTrace);
158 }
159
160 return FailureResult("Unexpected server error");
161 }
162
163 private byte[] SuccessResult()
164 {
165 XmlDocument doc = new XmlDocument();
166
167 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
168 "", "");
169
170 doc.AppendChild(xmlnode);
171
172 XmlElement rootElement = doc.CreateElement("", "ServerResponse",
173 "");
174
175 doc.AppendChild(rootElement);
176
177 XmlElement result = doc.CreateElement("", "Result", "");
178 result.AppendChild(doc.CreateTextNode("Success"));
179
180 rootElement.AppendChild(result);
181
182 return DocToBytes(doc);
183 }
184
185 private byte[] FailureResult(string msg)
186 {
187 XmlDocument doc = new XmlDocument();
188
189 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
190 "", "");
191
192 doc.AppendChild(xmlnode);
193
194 XmlElement rootElement = doc.CreateElement("", "ServerResponse",
195 "");
196
197 doc.AppendChild(rootElement);
198
199 XmlElement result = doc.CreateElement("", "Result", "");
200 result.AppendChild(doc.CreateTextNode("Failure"));
201
202 rootElement.AppendChild(result);
203
204 XmlElement message = doc.CreateElement("", "Message", "");
205 message.AppendChild(doc.CreateTextNode(msg));
206
207 rootElement.AppendChild(message);
208
209 return DocToBytes(doc);
210 }
211
212 private byte[] DocToBytes(XmlDocument doc)
213 {
214 MemoryStream ms = new MemoryStream();
215 XmlTextWriter xw = new XmlTextWriter(ms, null);
216 xw.Formatting = Formatting.Indented;
217 doc.WriteTo(xw);
218 xw.Flush();
219
220 return ms.ToArray();
221 }
222
223 private System.Net.IPAddress GetCallerIP(IOSHttpRequest request)
224 {
225 if (!m_Proxy)
226 return request.RemoteIPEndPoint.Address;
227
228 // We're behind a proxy
229 string xff = "X-Forwarded-For";
230 string xffValue = request.Headers[xff.ToLower()];
231 if (xffValue == null || (xffValue != null && xffValue == string.Empty))
232 xffValue = request.Headers[xff];
233
234 if (xffValue == null || (xffValue != null && xffValue == string.Empty))
235 {
236 m_log.WarnFormat("[MAP IMAGE HANDLER]: No XFF header");
237 return request.RemoteIPEndPoint.Address;
238 }
239
240 System.Net.IPEndPoint ep = Util.GetClientIPFromXFF(xffValue);
241 if (ep != null)
242 return ep.Address;
243
244 // Oops
245 return request.RemoteIPEndPoint.Address;
246 }
247
248 }
249}