source: binary-improvements2/WebServer/src/Web.cs@ 400

Last change on this file since 400 was 398, checked in by alloc, 2 years ago

Added detection of "webserver" folder in mods, last mod with such a folder is being used to serve "/files"

File size: 10.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Net.Sockets;
5using AllocsFixes.FileCache;
6using MapRendering;
7using SpaceWizards.HttpListener;
8using UnityEngine;
9using Webserver.UrlHandlers;
10using Cookie = System.Net.Cookie;
11using HttpStatusCode = System.Net.HttpStatusCode;
12using IPEndPoint = System.Net.IPEndPoint;
13
14namespace Webserver {
15 public class Web : IConsoleServer {
16 private const int guestPermissionLevel = 2000;
17 private const string indexPageUrl = "/app";
18
19 private readonly List<AbsHandler> handlers = new List<AbsHandler> ();
20 public readonly List<WebMod> webMods = new List<WebMod> ();
21 private readonly ConnectionHandler connectionHandler;
22
23 private readonly HttpListener listener = new HttpListener ();
24 private readonly Version httpProtocolVersion = new Version(1, 1);
25
26 public Web (string _modInstancePath) {
27 try {
28 int webPort = GamePrefs.GetInt (EnumUtils.Parse<EnumGamePrefs> ("ControlPanelPort"));
29 if (webPort < 1 || webPort > 65533) {
30 Log.Out ("[Web] Webserver not started (ControlPanelPort not within 1-65533)");
31 return;
32 }
33
34 // TODO: Remove once this becomes the default control panel
35 webPort += 2;
36
37 if (!HttpListener.IsSupported) {
38 Log.Out ("[Web] Webserver not started (HttpListener.IsSupported returned false)");
39 return;
40 }
41
42 // TODO: Read from config
43 bool useStaticCache = false;
44
45 string webfilesFolder = DetectWebserverFolder (_modInstancePath);
46 string webfilesFolderLegacy = _modInstancePath + "/weblegacy";
47
48 connectionHandler = new ConnectionHandler ();
49
50 RegisterPathHandler ("/", new RewriteHandler ("/files/"));
51
52 // React virtual routing
53 RegisterPathHandler ("/app", new RewriteHandler ("/files/index.html", true));
54
55 // Legacy web page
56 RegisterPathHandler ("/weblegacy", new StaticHandler (
57 webfilesFolderLegacy,
58 useStaticCache ? (AbstractCache)new SimpleCache () : new DirectAccess (),
59 false)
60 );
61
62 // Do mods relatively early as they should not be requested a lot, unlike the later registrations, especially for API and map tiles
63 RegisterWebMods (useStaticCache);
64
65 RegisterPathHandler ("/session/", new SessionHandler (connectionHandler));
66 RegisterPathHandler ("/userstatus", new UserStatusHandler ());
67 RegisterPathHandler ("/sse/", new SseHandler ());
68 RegisterPathHandler ("/files/", new StaticHandler (
69 webfilesFolder,
70 useStaticCache ? (AbstractCache) new SimpleCache () : new DirectAccess (),
71 false)
72 );
73 RegisterPathHandler ("/itemicons/", new ItemIconHandler (true));
74 RegisterPathHandler ("/map/", new StaticHandler (
75 GameIO.GetSaveGameDir () + "/map",
76 MapRenderer.GetTileCache (),
77 false,
78 "web.map")
79 );
80 RegisterPathHandler ("/api/", new ApiHandler ());
81
82 listener.Prefixes.Add ($"http://+:{webPort}/");
83 // listener.Prefixes.Add ($"http://[::1]:{webPort}/");
84 listener.Start ();
85 listener.BeginGetContext (HandleRequest, listener);
86
87 SdtdConsole.Instance.RegisterServer (this);
88
89 Log.Out ("[Web] Started Webserver on port " + webPort);
90 } catch (Exception e) {
91 Log.Error ("[Web] Error in Web.ctor: ");
92 Log.Exception (e);
93 }
94 }
95
96 private static string DetectWebserverFolder (string _modInstancePath) {
97 string webserverFolder = _modInstancePath + "/webserver";
98
99 foreach (Mod mod in ModManager.GetLoadedMods ()) {
100 string modServerFolder = mod.Path + "/webserver";
101
102 if (Directory.Exists (modServerFolder)) {
103 webserverFolder = modServerFolder;
104 }
105 }
106
107 Log.Out ($"[Web] Serving basic webserver files from {webserverFolder}");
108
109 return webserverFolder;
110 }
111
112 public void RegisterPathHandler (string _urlBasePath, AbsHandler _handler) {
113 foreach (AbsHandler handler in handlers) {
114 if (handler.UrlBasePath != _urlBasePath) {
115 continue;
116 }
117
118 Log.Error ($"[Web] Handler for relative path {_urlBasePath} already registerd.");
119 return;
120 }
121
122 handlers.Add (_handler);
123 _handler.SetBasePathAndParent (this, _urlBasePath);
124 }
125
126 private void RegisterWebMods (bool _useStaticCache) {
127 foreach (Mod mod in ModManager.GetLoadedMods ()) {
128 try {
129 string webModPath = mod.Path + "/WebMod";
130 if (!Directory.Exists (webModPath)) {
131 continue;
132 }
133
134 try {
135 WebMod webMod = new WebMod (this, mod, _useStaticCache);
136 webMods.Add (webMod);
137 } catch (InvalidDataException e) {
138 Log.Error ($"[Web] Could not load webmod from mod {mod.ModInfo.Name.Value}: {e.Message}");
139 }
140 } catch (Exception e) {
141 Log.Error ("[Web] Failed loading web mods from mod " + mod.ModInfo.Name.Value);
142 Log.Exception (e);
143 }
144 }
145 }
146
147 public void Disconnect () {
148 try {
149 listener.Stop ();
150 listener.Close ();
151 } catch (Exception e) {
152 Log.Out ("[Web] Error in Web.Disconnect: " + e);
153 }
154 }
155
156 public void Shutdown () {
157 foreach (AbsHandler handler in handlers) {
158 handler.Shutdown ();
159 }
160 }
161
162 public void SendLine (string _line) {
163 connectionHandler.SendLine (_line);
164 }
165
166 public void SendLog (string _formattedMessage, string _plainMessage, string _trace, LogType _type, DateTime _timestamp, long _uptime) {
167 // Do nothing, handled by LogBuffer internally
168 }
169
170 private readonly UnityEngine.Profiling.CustomSampler getContextSampler = UnityEngine.Profiling.CustomSampler.Create ("GetCtx");
171 private readonly UnityEngine.Profiling.CustomSampler authSampler = UnityEngine.Profiling.CustomSampler.Create ("Auth");
172 private readonly UnityEngine.Profiling.CustomSampler cookieSampler = UnityEngine.Profiling.CustomSampler.Create ("ConCookie");
173 private readonly UnityEngine.Profiling.CustomSampler handlerSampler = UnityEngine.Profiling.CustomSampler.Create ("Handler");
174
175 private void HandleRequest (IAsyncResult _result) {
176 HttpListener listenerInstance = (HttpListener)_result.AsyncState;
177 if (!listenerInstance.IsListening) {
178 return;
179 }
180
181#if ENABLE_PROFILER
182 UnityEngine.Profiling.Profiler.BeginThreadProfiling ("AllocsMods", "WebRequest");
183 getContextSampler.Begin ();
184 HttpListenerContext ctx = listenerInstance.EndGetContext (_result);
185 getContextSampler.End ();
186 try {
187#else
188 HttpListenerContext ctx = listenerInstance.EndGetContext (_result);
189 listenerInstance.BeginGetContext (HandleRequest, listenerInstance);
190#endif
191 try {
192 HttpListenerRequest request = ctx.Request;
193 HttpListenerResponse response = ctx.Response;
194 response.SendChunked = false;
195
196 response.ProtocolVersion = httpProtocolVersion;
197
198 // No game yet -> fail request
199 if (GameManager.Instance.World == null) {
200 response.StatusCode = (int) HttpStatusCode.ServiceUnavailable;
201 return;
202 }
203
204 if (request.Url == null) {
205 response.StatusCode = (int) HttpStatusCode.BadRequest;
206 return;
207 }
208
209 authSampler.Begin ();
210 int permissionLevel = DoAuthentication (request, out WebConnection conn);
211 authSampler.End ();
212
213 //Log.Out ("Login status: conn!=null: {0}, permissionlevel: {1}", conn != null, permissionLevel);
214
215 cookieSampler.Begin ();
216 if (conn != null) {
217 Cookie cookie = new Cookie ("sid", conn.SessionID, "/") {
218 Expired = false,
219 Expires = DateTime.MinValue,
220 HttpOnly = true,
221 Secure = false
222 };
223 response.AppendCookie (cookie);
224 }
225 cookieSampler.End ();
226
227 string requestPath = request.Url.AbsolutePath;
228
229 if (requestPath.Length < 2) {
230 response.Redirect (indexPageUrl);
231 return;
232 }
233
234 RequestContext context = new RequestContext (requestPath, request, response, conn, permissionLevel);
235
236 ApplyPathHandler (context);
237
238 } catch (IOException e) {
239 if (e.InnerException is SocketException) {
240 Log.Out ("[Web] Error in Web.HandleRequest(): Remote host closed connection: " + e.InnerException.Message);
241 } else {
242 Log.Out ("[Web] Error (IO) in Web.HandleRequest(): " + e);
243 }
244 } catch (Exception e) {
245 Log.Error ("[Web] Error in Web.HandleRequest(): ");
246 Log.Exception (e);
247 } finally {
248 if (!ctx.Response.SendChunked) {
249 ctx.Response.Close ();
250 }
251 }
252#if ENABLE_PROFILER
253 } finally {
254 listenerInstance.BeginGetContext (HandleRequest, listenerInstance);
255 UnityEngine.Profiling.Profiler.EndThreadProfiling ();
256 }
257#endif
258 }
259
260 public void ApplyPathHandler (RequestContext _context) {
261 for (int i = handlers.Count - 1; i >= 0; i--) {
262 AbsHandler handler = handlers [i];
263
264 if (!_context.RequestPath.StartsWith (handler.UrlBasePath)) {
265 continue;
266 }
267
268 if (!handler.IsAuthorizedForHandler (_context.Connection, _context.PermissionLevel)) {
269 _context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
270 if (_context.Connection != null) {
271 //Log.Out ("Web.HandleRequest: user '{0}' not allowed to access '{1}'", _con.SteamID, handler.ModuleName);
272 }
273 } else {
274 handlerSampler.Begin ();
275 handler.HandleRequest (_context);
276 handlerSampler.End ();
277 }
278
279 return;
280 }
281
282 // Not really relevant for non-debugging purposes:
283 //Log.Out ("Error in Web.HandleRequest(): No handler found for path \"" + _requestPath + "\"");
284 _context.Response.StatusCode = (int) HttpStatusCode.NotFound;
285 }
286
287 private int DoAuthentication (HttpListenerRequest _req, out WebConnection _con) {
288 _con = null;
289
290 string sessionId = _req.Cookies ["sid"]?.Value;
291
292 IPEndPoint reqRemoteEndPoint = _req.RemoteEndPoint;
293 if (reqRemoteEndPoint == null) {
294 Log.Warning ("[Web] No RemoteEndPoint on web request");
295 return guestPermissionLevel;
296 }
297
298 if (!string.IsNullOrEmpty (sessionId)) {
299 _con = connectionHandler.IsLoggedIn (sessionId, reqRemoteEndPoint.Address);
300 if (_con != null) {
301 return GameManager.Instance.adminTools.GetUserPermissionLevel (_con.UserId);
302 }
303 }
304
305 if (_req.QueryString ["adminuser"] == null || _req.QueryString ["admintoken"] == null) {
306 return guestPermissionLevel;
307 }
308
309 WebPermissions.AdminToken admin = WebPermissions.Instance.GetWebAdmin (_req.QueryString ["adminuser"],
310 _req.QueryString ["admintoken"]);
311 if (admin != null) {
312 return admin.permissionLevel;
313 }
314
315 Log.Warning ("[Web] Invalid Admintoken used from " + reqRemoteEndPoint);
316
317 return guestPermissionLevel;
318 }
319 }
320}
Note: See TracBrowser for help on using the repository browser.