Changeset 367 for binary-improvements/MapRendering
- Timestamp:
- Aug 11, 2021, 6:22:03 PM (3 years ago)
- Location:
- binary-improvements/MapRendering
- Files:
-
- 4 added
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/MapRendering/API.cs
r351 r367 4 4 namespace AllocsFixes { 5 5 public class API : IModApi { 6 private Web webInstance; 7 6 8 public void InitMod () { 7 9 ModEvents.GameStartDone.RegisterHandler (GameStartDone); … … 12 14 private void GameStartDone () { 13 15 // ReSharper disable once ObjectCreationAsStatement 14 new Web ();16 webInstance = new Web (); 15 17 LogBuffer.Init (); 16 18 … … 21 23 22 24 private void GameShutdown () { 25 webInstance.Shutdown (); 23 26 MapRendering.MapRendering.Shutdown (); 24 27 } -
binary-improvements/MapRendering/Web/API/Null.cs
r351 r367 4 4 namespace AllocsFixes.NetConnections.Servers.Web.API { 5 5 public class Null : WebAPI { 6 public Null (string _name) : base(_name) { 7 } 8 6 9 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 7 10 int _permissionLevel) { -
binary-improvements/MapRendering/Web/API/WebAPI.cs
r351 r367 2 2 using System.Text; 3 3 using AllocsFixes.JSON; 4 using UnityEngine.Profiling;5 4 6 5 namespace AllocsFixes.NetConnections.Servers.Web.API { … … 8 7 public readonly string Name; 9 8 10 protected WebAPI ( ) {11 Name = GetType ().Name;9 protected WebAPI (string _name = null) { 10 Name = _name ?? GetType ().Name; 12 11 } 13 12 14 13 #if ENABLE_PROFILER 15 private static readonly CustomSampler jsonSerializeSampler =CustomSampler.Create ("JSON_Serialize");16 private static readonly CustomSampler netWriteSampler =CustomSampler.Create ("JSON_Write");14 private static readonly UnityEngine.Profiling.CustomSampler jsonSerializeSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_Serialize"); 15 private static readonly UnityEngine.Profiling.CustomSampler netWriteSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_Write"); 17 16 #endif 18 17 -
binary-improvements/MapRendering/Web/Handlers/ApiHandler.cs
r351 r367 4 4 using System.Reflection; 5 5 using AllocsFixes.NetConnections.Servers.Web.API; 6 using UnityEngine.Profiling;7 6 8 7 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 9 8 public class ApiHandler : PathHandler { 10 9 private readonly Dictionary<string, WebAPI> apis = new CaseInsensitiveStringDictionary<WebAPI> (); 11 private readonly string staticPart;12 10 13 public ApiHandler (string _staticPart, string _moduleName = null) : base (_moduleName) { 14 staticPart = _staticPart; 11 public ApiHandler (string _moduleName = null) : base (_moduleName) { 15 12 16 13 foreach (Type t in Assembly.GetExecutingAssembly ().GetTypes ()) { … … 19 16 if (ctor != null) { 20 17 WebAPI apiInstance = (WebAPI) ctor.Invoke (new object [0]); 21 addApi (apiInstance .Name, apiInstance);18 addApi (apiInstance); 22 19 } 23 20 } 24 21 } 25 22 26 // Add dummy types 27 Type dummy_t = typeof (Null); 28 ConstructorInfo dummy_ctor = dummy_t.GetConstructor (new Type [0]); 29 if (dummy_ctor != null) { 30 WebAPI dummy_apiInstance = (WebAPI) dummy_ctor.Invoke (new object[0]); 31 32 // Permissions that don't map to a real API 33 addApi ("viewallclaims", dummy_apiInstance); 34 addApi ("viewallplayers", dummy_apiInstance); 35 } 23 // Permissions that don't map to a real API 24 addApi (new Null ("viewallclaims")); 25 addApi (new Null ("viewallplayers")); 36 26 } 37 27 38 private void addApi ( string _apiName,WebAPI _api) {39 apis.Add (_api Name, _api);40 WebPermissions.Instance.AddKnownModule ("webapi." + _api Name, _api.DefaultPermissionLevel ());28 private void addApi (WebAPI _api) { 29 apis.Add (_api.Name, _api); 30 WebPermissions.Instance.AddKnownModule ("webapi." + _api.Name, _api.DefaultPermissionLevel ()); 41 31 } 42 32 43 33 #if ENABLE_PROFILER 44 private static readonly CustomSampler apiHandlerSampler =CustomSampler.Create ("API_Handler");34 private static readonly UnityEngine.Profiling.CustomSampler apiHandlerSampler = UnityEngine.Profiling.CustomSampler.Create ("API_Handler"); 45 35 #endif 46 36 47 37 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 48 38 int _permissionLevel) { 49 string apiName = _req.Url.AbsolutePath.Remove (0, staticPart.Length);39 string apiName = _req.Url.AbsolutePath.Remove (0, urlBasePath.Length); 50 40 51 WebAPI api; 52 if (!apis.TryGetValue (apiName, out api)) { 53 Log.Out ("Error in ApiHandler.HandleRequest(): No handler found for API \"" + apiName + "\""); 41 if (!apis.TryGetValue (apiName, out WebAPI api)) { 42 Log.Out ($"Error in {nameof(ApiHandler)}.HandleRequest(): No handler found for API \"{apiName}\""); 54 43 _resp.StatusCode = (int) HttpStatusCode.NotFound; 55 44 return; 56 45 } 57 46 58 if (!AuthorizeFor Command (apiName, _user, _permissionLevel)) {47 if (!AuthorizeForApi (apiName, _permissionLevel)) { 59 48 _resp.StatusCode = (int) HttpStatusCode.Forbidden; 60 49 if (_user != null) { 61 //Log.Out ( "ApiHandler: user '{0}' not allowed to execute '{1}'", user.SteamID, apiName);50 //Log.Out ($"{nameof(ApiHandler)}: user '{user.SteamID}' not allowed to execute '{apiName}'"); 62 51 } 63 52 … … 74 63 #endif 75 64 } catch (Exception e) { 76 Log.Error ( "Error in ApiHandler.HandleRequest(): Handler {0} threw an exception:", api.Name);65 Log.Error ($"Error in {nameof(ApiHandler)}.HandleRequest(): Handler {api.Name} threw an exception:"); 77 66 Log.Exception (e); 78 67 _resp.StatusCode = (int) HttpStatusCode.InternalServerError; … … 80 69 } 81 70 82 private bool AuthorizeFor Command (string _apiName, WebConnection _user, int _permissionLevel) {71 private bool AuthorizeForApi (string _apiName, int _permissionLevel) { 83 72 return WebPermissions.Instance.ModuleAllowedWithLevel ("webapi." + _apiName, _permissionLevel); 84 73 } -
binary-improvements/MapRendering/Web/Handlers/ItemIconHandler.cs
r354 r367 11 11 private readonly bool logMissingFiles; 12 12 13 private readonly string staticPart;14 13 private bool loaded; 15 14 … … 18 17 } 19 18 20 public ItemIconHandler (string _staticPart, bool _logMissingFiles, string _moduleName = null) : base (_moduleName) { 21 staticPart = _staticPart; 19 public ItemIconHandler (bool _logMissingFiles, string _moduleName = null) : base (_moduleName) { 22 20 logMissingFiles = _logMissingFiles; 23 21 Instance = this; … … 34 32 } 35 33 36 string requestFileName = _req.Url.AbsolutePath.Remove (0, staticPart.Length);34 string requestFileName = _req.Url.AbsolutePath.Remove (0, urlBasePath.Length); 37 35 requestFileName = requestFileName.Remove (requestFileName.LastIndexOf ('.')); 38 36 -
binary-improvements/MapRendering/Web/Handlers/PathHandler.cs
r351 r367 3 3 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 4 4 public abstract class PathHandler { 5 private readonly string moduleName; 5 protected readonly string moduleName; 6 protected string urlBasePath; 7 protected Web parent; 6 8 7 9 protected PathHandler (string _moduleName, int _defaultPermissionLevel = 0) { … … 24 26 return true; 25 27 } 28 29 public virtual void Shutdown () { 30 } 31 32 public virtual void SetBasePathAndParent (Web _parent, string _relativePath) { 33 parent = _parent; 34 urlBasePath = _relativePath; 35 } 26 36 } 27 37 } -
binary-improvements/MapRendering/Web/Handlers/SessionHandler.cs
r351 r367 7 7 private readonly string footer = ""; 8 8 private readonly string header = ""; 9 private readonly Web parent;10 private readonly string staticPart;11 9 12 public SessionHandler (string _staticPart, string _dataFolder, Web _parent, string _moduleName = null) : 13 base (_moduleName) { 14 staticPart = _staticPart; 15 parent = _parent; 16 10 public SessionHandler (string _dataFolder, string _moduleName = null) : base (_moduleName) { 17 11 if (File.Exists (_dataFolder + "/sessionheader.tmpl")) { 18 12 header = File.ReadAllText (_dataFolder + "/sessionheader.tmpl"); … … 26 20 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 27 21 int _permissionLevel) { 28 string subpath = _req.Url.AbsolutePath.Remove (0, staticPart.Length);22 string subpath = _req.Url.AbsolutePath.Remove (0, urlBasePath.Length); 29 23 30 24 StringBuilder result = new StringBuilder (); … … 52 46 "<h1>Not logged in, <a href=\"/static/index.html\">click to return to main page</a>.</h1>"); 53 47 } else if (subpath.StartsWith ("login")) { 54 string host = (Web. isSslRedirected (_req) ? "https://" : "http://") + _req.UserHostName;48 string host = (Web.IsSslRedirected (_req) ? "https://" : "http://") + _req.UserHostName; 55 49 string url = OpenID.GetOpenIdLoginUrl (host, host + "/session/verify"); 56 50 _resp.Redirect (url); -
binary-improvements/MapRendering/Web/Handlers/StaticHandler.cs
r351 r367 8 8 private readonly string datapath; 9 9 private readonly bool logMissingFiles; 10 private readonly string staticPart;11 10 12 public StaticHandler (string _ staticPart, string _filePath, AbstractCache _cache, bool _logMissingFiles,11 public StaticHandler (string _filePath, AbstractCache _cache, bool _logMissingFiles, 13 12 string _moduleName = null) : base (_moduleName) { 14 staticPart = _staticPart;15 13 datapath = _filePath + (_filePath [_filePath.Length - 1] == '/' ? "" : "/"); 16 14 cache = _cache; … … 20 18 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 21 19 int _permissionLevel) { 22 string fn = _req.Url.AbsolutePath.Remove (0, staticPart.Length);20 string fn = _req.Url.AbsolutePath.Remove (0, urlBasePath.Length); 23 21 24 22 byte[] content = cache.GetFileContent (datapath + fn); -
binary-improvements/MapRendering/Web/Web.cs
r360 r367 9 9 using AllocsFixes.FileCache; 10 10 using AllocsFixes.NetConnections.Servers.Web.Handlers; 11 using AllocsFixes.NetConnections.Servers.Web.SSE; 11 12 using UnityEngine; 12 using UnityEngine.Profiling;13 13 14 14 namespace AllocsFixes.NetConnections.Servers.Web { … … 18 18 public static int currentHandlers; 19 19 public static long totalHandlingTime = 0; 20 private readonly HttpListener _listener = new HttpListener (); 21 private readonly string dataFolder; 20 private readonly HttpListener listener = new HttpListener (); 22 21 private readonly Dictionary<string, PathHandler> handlers = new CaseInsensitiveStringDictionary<PathHandler> (); 23 private readonly bool useStaticCache; 24 25 public ConnectionHandler connectionHandler; 22 23 public readonly ConnectionHandler connectionHandler; 26 24 27 25 public Web () { … … 40 38 41 39 // TODO: Read from config 42 useStaticCache = false;43 44 dataFolder = Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location) + "/webserver";40 bool useStaticCache = false; 41 42 string dataFolder = Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location) + "/webserver"; 45 43 46 44 if (!HttpListener.IsSupported) { … … 49 47 } 50 48 51 handlers.Add ( 52 "/index.htm", 53 new SimpleRedirectHandler ("/static/index.html")); 54 handlers.Add ( 55 "/favicon.ico", 56 new SimpleRedirectHandler ("/static/favicon.ico")); 57 handlers.Add ( 58 "/session/", 59 new SessionHandler ( 60 "/session/", 49 50 RegisterPathHandler ("/index.htm", new SimpleRedirectHandler ("/static/index.html")); 51 RegisterPathHandler ("/favicon.ico", new SimpleRedirectHandler ("/static/favicon.ico")); 52 RegisterPathHandler ("/session/", new SessionHandler (dataFolder)); 53 RegisterPathHandler ("/userstatus", new UserStatusHandler ()); 54 RegisterPathHandler ("/static/", new StaticHandler ( 61 55 dataFolder, 62 this) 56 useStaticCache ? (AbstractCache) new SimpleCache () : new DirectAccess (), 57 false) 63 58 ); 64 handlers.Add ( 65 "/userstatus", 66 new UserStatusHandler () 67 ); 68 if (useStaticCache) { 69 handlers.Add ( 70 "/static/", 71 new StaticHandler ( 72 "/static/", 73 dataFolder, 74 new SimpleCache (), 75 false) 76 ); 77 } else { 78 handlers.Add ( 79 "/static/", 80 new StaticHandler ( 81 "/static/", 82 dataFolder, 83 new DirectAccess (), 84 false) 85 ); 86 } 87 88 handlers.Add ( 89 "/itemicons/", 90 new ItemIconHandler ( 91 "/itemicons/", 92 true) 93 ); 94 95 handlers.Add ( 96 "/map/", 97 new StaticHandler ( 98 "/map/", 59 RegisterPathHandler ("/itemicons/", new ItemIconHandler (true)); 60 RegisterPathHandler ("/map/", new StaticHandler ( 99 61 GameUtils.GetSaveGameDir () + "/map", 100 62 MapRendering.MapRendering.GetTileCache (), … … 102 64 "web.map") 103 65 ); 104 105 handlers.Add ( 106 "/api/", 107 new ApiHandler ("/api/") 108 ); 66 RegisterPathHandler ("/api/", new ApiHandler ()); 67 RegisterPathHandler ("/sse/", new SseHandler ()); 109 68 110 69 connectionHandler = new ConnectionHandler (); 111 70 112 _listener.Prefixes.Add (string.Format ("http://*:{0}/", webPort + 2));113 _listener.Start ();71 listener.Prefixes.Add ($"http://*:{webPort + 2}/"); 72 listener.Start (); 114 73 115 74 SdtdConsole.Instance.RegisterServer (this); 116 75 117 _listener.BeginGetContext (HandleRequest, _listener);76 listener.BeginGetContext (HandleRequest, listener); 118 77 119 78 Log.Out ("Started Webserver on " + (webPort + 2)); … … 123 82 } 124 83 84 public void RegisterPathHandler (string _urlBasePath, PathHandler _handler) { 85 if (handlers.ContainsKey (_urlBasePath)) { 86 Log.Error ($"Web: Handler for relative path {_urlBasePath} already registerd."); 87 return; 88 } 89 90 handlers.Add (_urlBasePath, _handler); 91 _handler.SetBasePathAndParent (this, _urlBasePath); 92 } 93 125 94 public void Disconnect () { 126 95 try { 127 _listener.Stop ();128 _listener.Close ();96 listener.Stop (); 97 listener.Close (); 129 98 } catch (Exception e) { 130 99 Log.Out ("Error in Web.Disconnect: " + e); … … 132 101 } 133 102 103 public void Shutdown () { 104 foreach (KeyValuePair<string, PathHandler> kvp in handlers) { 105 kvp.Value.Shutdown (); 106 } 107 } 108 134 109 public void SendLine (string _line) { 135 110 connectionHandler.SendLine (_line); … … 140 115 } 141 116 142 public static bool isSslRedirected (HttpListenerRequest _req) {117 public static bool IsSslRedirected (HttpListenerRequest _req) { 143 118 string proto = _req.Headers ["X-Forwarded-Proto"]; 144 if (!string.IsNullOrEmpty (proto)) { 145 return proto.Equals ("https", StringComparison.OrdinalIgnoreCase); 146 } 147 148 return false; 119 return !string.IsNullOrEmpty (proto) && proto.Equals ("https", StringComparison.OrdinalIgnoreCase); 149 120 } 150 121 … … 152 123 153 124 #if ENABLE_PROFILER 154 private readonly CustomSampler authSampler =CustomSampler.Create ("Auth");155 private readonly CustomSampler handlerSampler =CustomSampler.Create ("Handler");125 private readonly UnityEngine.Profiling.CustomSampler authSampler = UnityEngine.Profiling.CustomSampler.Create ("Auth"); 126 private readonly UnityEngine.Profiling.CustomSampler handlerSampler = UnityEngine.Profiling.CustomSampler.Create ("Handler"); 156 127 #endif 157 128 158 129 private void HandleRequest (IAsyncResult _result) { 159 if (! _listener.IsListening) {130 if (!listener.IsListening) { 160 131 return; 161 132 } … … 166 137 // MicroStopwatch msw = new MicroStopwatch (); 167 138 #if ENABLE_PROFILER 168 Profiler.BeginThreadProfiling ("AllocsMods", "WebRequest");139 UnityEngine.Profiling.Profiler.BeginThreadProfiling ("AllocsMods", "WebRequest"); 169 140 HttpListenerContext ctx = _listener.EndGetContext (_result); 170 141 try { 171 142 #else 172 HttpListenerContext ctx = _listener.EndGetContext (_result);173 _listener.BeginGetContext (HandleRequest, _listener);143 HttpListenerContext ctx = listener.EndGetContext (_result); 144 listener.BeginGetContext (HandleRequest, listener); 174 145 #endif 175 146 try { … … 180 151 response.ProtocolVersion = HttpProtocolVersion; 181 152 182 WebConnection conn;183 153 #if ENABLE_PROFILER 184 154 authSampler.Begin (); 185 155 #endif 186 int permissionLevel = DoAuthentication (request, out conn);156 int permissionLevel = DoAuthentication (request, out WebConnection conn); 187 157 #if ENABLE_PROFILER 188 158 authSampler.End (); … … 194 164 195 165 if (conn != null) { 196 Cookie cookie = new Cookie ("sid", conn.SessionID, "/"); 197 cookie.Expired = false; 198 cookie.Expires = new DateTime (2020, 1, 1); 199 cookie.HttpOnly = true; 200 cookie.Secure = false; 166 Cookie cookie = new Cookie ("sid", conn.SessionID, "/") { 167 Expired = false, 168 Expires = DateTime.MinValue, 169 HttpOnly = true, 170 Secure = false 171 }; 201 172 response.AppendCookie (cookie); 202 173 } … … 260 231 } finally { 261 232 _listener.BeginGetContext (HandleRequest, _listener); 262 Profiler.EndThreadProfiling ();233 UnityEngine.Profiling.Profiler.EndThreadProfiling (); 263 234 } 264 235 #endif … … 281 252 } 282 253 254 string remoteEndpointString = _req.RemoteEndPoint.ToString (); 255 283 256 if (_req.QueryString ["adminuser"] != null && _req.QueryString ["admintoken"] != null) { 284 257 WebPermissions.AdminToken admin = WebPermissions.Instance.GetWebAdmin (_req.QueryString ["adminuser"], … … 288 261 } 289 262 290 Log.Warning ("Invalid Admintoken used from " + _req.RemoteEndPoint);263 Log.Warning ("Invalid Admintoken used from " + remoteEndpointString); 291 264 } 292 265 … … 299 272 int level = GameManager.Instance.adminTools.GetUserPermissionLevel (id.ToString ()); 300 273 Log.Out ("Steam OpenID login from {0} with ID {1}, permission level {2}", 301 _req.RemoteEndPoint.ToString (), con.SteamID, level);274 remoteEndpointString, con.SteamID, level); 302 275 return level; 303 276 } 304 277 305 Log.Out ("Steam OpenID login failed from {0}", _req.RemoteEndPoint.ToString ());278 Log.Out ("Steam OpenID login failed from {0}", remoteEndpointString); 306 279 } catch (Exception e) { 307 280 Log.Error ("Error validating login:"); -
binary-improvements/MapRendering/WebAndMapRendering.csproj
r355 r367 33 33 <Reference Include="LogLibrary"> 34 34 <HintPath>..\7dtd-binaries\LogLibrary.dll</HintPath> 35 <Private>False</Private> 36 </Reference> 37 <Reference Include="mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 38 <HintPath>..\7dtd-binaries\mscorlib.dll</HintPath> 35 39 <Private>False</Private> 36 40 </Reference> … … 75 79 <Compile Include="Web\API\GetHostileLocation.cs" /> 76 80 <Compile Include="Web\API\Null.cs" /> 81 <Compile Include="Web\SSE\EventLog.cs" /> 82 <Compile Include="Web\SSE\SseHandler.cs" /> 83 <Compile Include="Web\SSE\EventBase.cs" /> 77 84 <Compile Include="Web\Web.cs" /> 78 85 <Compile Include="Web\MimeType.cs" /> … … 117 124 </ProjectReference> 118 125 </ItemGroup> 119 <ItemGroup />120 126 <ItemGroup> 121 127 <None Include="ModInfo.xml">
Note:
See TracChangeset
for help on using the changeset viewer.