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