Changeset 382 for binary-improvements2/MapRendering/Web/Handlers
- Timestamp:
- Aug 1, 2022, 12:54:31 PM (2 years ago)
- Location:
- binary-improvements2/MapRendering/Web/Handlers
- Files:
-
- 1 added
- 6 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/MapRendering/Web/Handlers/AbsHandler.cs
r381 r382 1 1 using System.Net; 2 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest; 3 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse; 2 4 3 5 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 4 public abstract class PathHandler {6 public abstract class AbsHandler { 5 7 protected readonly string moduleName; 6 8 protected string urlBasePath; 7 9 protected Web parent; 8 10 9 protected PathHandler (string _moduleName, int _defaultPermissionLevel = 0) { 11 public string ModuleName => moduleName; 12 public string UrlBasePath => urlBasePath; 13 14 protected AbsHandler (string _moduleName, int _defaultPermissionLevel = 0) { 10 15 moduleName = _moduleName; 11 16 WebPermissions.Instance.AddKnownModule (_moduleName, _defaultPermissionLevel); 12 17 } 13 18 14 public string ModuleName => moduleName; 15 16 public abstract void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 19 public abstract void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con, 17 20 int _permissionLevel); 18 21 -
binary-improvements2/MapRendering/Web/Handlers/ApiHandler.cs
r367 r382 4 4 using System.Reflection; 5 5 using AllocsFixes.NetConnections.Servers.Web.API; 6 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest; 7 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse; 6 8 7 9 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 8 public class ApiHandler : PathHandler {10 public class ApiHandler : AbsHandler { 9 11 private readonly Dictionary<string, WebAPI> apis = new CaseInsensitiveStringDictionary<WebAPI> (); 10 12 … … 35 37 #endif 36 38 37 public override void HandleRequest ( HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,39 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con, 38 40 int _permissionLevel) { 39 string apiName = _req .Url.AbsolutePath.Remove (0, urlBasePath.Length);41 string apiName = _requestPath.Remove (0, urlBasePath.Length); 40 42 41 43 if (!apis.TryGetValue (apiName, out WebAPI api)) { … … 47 49 if (!AuthorizeForApi (apiName, _permissionLevel)) { 48 50 _resp.StatusCode = (int) HttpStatusCode.Forbidden; 49 if (_ user!= null) {51 if (_con != null) { 50 52 //Log.Out ($"{nameof(ApiHandler)}: user '{user.SteamID}' not allowed to execute '{apiName}'"); 51 53 } … … 58 60 apiHandlerSampler.Begin (); 59 61 #endif 60 api.HandleRequest (_req, _resp, _ user, _permissionLevel);62 api.HandleRequest (_req, _resp, _con, _permissionLevel); 61 63 #if ENABLE_PROFILER 62 64 apiHandlerSampler.End (); -
binary-improvements2/MapRendering/Web/Handlers/ItemIconHandler.cs
r369 r382 4 4 using System.Net; 5 5 using UnityEngine; 6 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest; 7 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse; 6 8 using Object = UnityEngine.Object; 7 9 8 10 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 9 public class ItemIconHandler : PathHandler {11 public class ItemIconHandler : AbsHandler { 10 12 private readonly Dictionary<string, byte[]> icons = new Dictionary<string, byte[]> (); 11 13 private readonly bool logMissingFiles; … … 24 26 public static ItemIconHandler Instance { get; private set; } 25 27 26 public override void HandleRequest ( HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,28 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con, 27 29 int _permissionLevel) { 28 30 if (!loaded) { … … 32 34 } 33 35 34 string requestFileName = _req .Url.AbsolutePath.Remove (0, urlBasePath.Length);36 string requestFileName = _requestPath.Remove (0, urlBasePath.Length); 35 37 requestFileName = requestFileName.Remove (requestFileName.LastIndexOf ('.')); 36 38 37 if (icons.ContainsKey (requestFileName) && _req .Url.AbsolutePath.EndsWith (".png", StringComparison.OrdinalIgnoreCase)) {39 if (icons.ContainsKey (requestFileName) && _requestPath.EndsWith (".png", StringComparison.OrdinalIgnoreCase)) { 38 40 _resp.ContentType = MimeType.GetMimeType (".png"); 39 41 … … 45 47 _resp.StatusCode = (int) HttpStatusCode.NotFound; 46 48 if (logMissingFiles) { 47 Log.Out ("Web:IconHandler:FileNotFound: \"" + _req .Url.AbsolutePath + "\" ");49 Log.Out ("Web:IconHandler:FileNotFound: \"" + _requestPath + "\" "); 48 50 } 49 51 } -
binary-improvements2/MapRendering/Web/Handlers/SessionHandler.cs
r367 r382 1 using System; 1 2 using System.IO; 2 3 using System.Net; 3 4 using System.Text; 5 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest; 6 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse; 4 7 5 8 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 6 public class SessionHandler : PathHandler { 9 public class SessionHandler : AbsHandler { 10 private const string pageBasePath = "/"; 11 private const string steamOpenIdVerifyUrl = "verifysteamopenid"; 12 private const string steamLoginUrl = "loginsteam"; 13 7 14 private readonly string footer = ""; 8 15 private readonly string header = ""; 9 16 10 public SessionHandler (string _dataFolder, string _moduleName = null) : base (_moduleName) { 17 private readonly ConnectionHandler connectionHandler; 18 19 public SessionHandler (string _dataFolder, ConnectionHandler _connectionHandler) : base (null) { 20 connectionHandler = _connectionHandler; 21 11 22 if (File.Exists (_dataFolder + "/sessionheader.tmpl")) { 12 23 header = File.ReadAllText (_dataFolder + "/sessionheader.tmpl"); … … 18 29 } 19 30 20 public override void HandleRequest ( HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,31 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con, 21 32 int _permissionLevel) { 22 string subpath = _req.Url.AbsolutePath.Remove (0, urlBasePath.Length); 33 34 IPEndPoint reqRemoteEndPoint = _req.RemoteEndPoint; 35 if (reqRemoteEndPoint == null) { 36 _resp.Redirect (pageBasePath); 37 return; 38 } 39 40 string subpath = _requestPath.Remove (0, urlBasePath.Length); 23 41 24 42 StringBuilder result = new StringBuilder (); 25 43 result.Append (header); 26 44 27 if (subpath.StartsWith ("verify")) { 28 if (_user != null) { 29 _resp.Redirect ("/static/index.html"); 45 if (subpath.StartsWith (steamOpenIdVerifyUrl)) { 46 string remoteEndpointString = reqRemoteEndPoint.ToString (); 47 48 try { 49 ulong id = OpenID.Validate (_req); 50 if (id > 0) { 51 WebConnection con = connectionHandler.LogIn (id, reqRemoteEndPoint.Address); 52 int level = GameManager.Instance.adminTools.GetUserPermissionLevel (con.UserId); 53 Log.Out ("Steam OpenID login from {0} with ID {1}, permission level {2}", 54 remoteEndpointString, con.UserId, level); 55 56 Cookie cookie = new Cookie ("sid", con.SessionID, "/") { 57 Expired = false, 58 Expires = DateTime.MinValue, 59 HttpOnly = true, 60 Secure = false 61 }; 62 _resp.AppendCookie (cookie); 63 _resp.Redirect (pageBasePath); 64 65 return; 66 } 67 } catch (Exception e) { 68 Log.Error ("Error validating login:"); 69 Log.Exception (e); 70 } 71 72 Log.Out ($"Steam OpenID login failed from {remoteEndpointString}"); 73 result.Append ($"<h1>Login failed, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>"); 74 } else if (subpath.StartsWith ("logout")) { 75 if (_con != null) { 76 connectionHandler.LogOut (_con.SessionID); 77 Cookie cookie = new Cookie ("sid", "", "/") { 78 Expired = true 79 }; 80 _resp.AppendCookie (cookie); 81 _resp.Redirect (pageBasePath); 30 82 return; 31 83 } 32 84 33 result.Append ( 34 "<h1>Login failed, <a href=\"/static/index.html\">click to return to main page</a>.</h1>"); 35 } else if (subpath.StartsWith ("logout")) { 36 if (_user != null) { 37 parent.connectionHandler.LogOut (_user.SessionID); 38 Cookie cookie = new Cookie ("sid", "", "/"); 39 cookie.Expired = true; 40 _resp.AppendCookie (cookie); 41 _resp.Redirect ("/static/index.html"); 42 return; 43 } 44 45 result.Append ( 46 "<h1>Not logged in, <a href=\"/static/index.html\">click to return to main page</a>.</h1>"); 47 } else if (subpath.StartsWith ("login")) { 85 result.Append ($"<h1>Not logged in, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>"); 86 } else if (subpath.StartsWith (steamLoginUrl)) { 48 87 string host = (Web.IsSslRedirected (_req) ? "https://" : "http://") + _req.UserHostName; 49 string url = OpenID.GetOpenIdLoginUrl (host, host + "/session/verify");88 string url = OpenID.GetOpenIdLoginUrl (host, host + urlBasePath + steamOpenIdVerifyUrl); 50 89 _resp.Redirect (url); 51 90 return; 52 91 } else { 53 result.Append ( 54 "<h1>Unknown command, <a href=\"/static/index.html\">click to return to main page</a>.</h1>"); 92 result.Append ($"<h1>Unknown command, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>"); 55 93 } 56 94 -
binary-improvements2/MapRendering/Web/Handlers/SimpleRedirectHandler.cs
r351 r382 1 1 using System.Net; 2 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest; 3 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse; 2 4 3 5 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 4 public class SimpleRedirectHandler : PathHandler {6 public class SimpleRedirectHandler : AbsHandler { 5 7 private readonly string target; 6 8 7 public SimpleRedirectHandler (string _target , string _moduleName = null) : base (_moduleName) {9 public SimpleRedirectHandler (string _target) : base (null) { 8 10 target = _target; 9 11 } 10 12 11 public override void HandleRequest ( HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,13 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con, 12 14 int _permissionLevel) { 13 15 _resp.Redirect (target); -
binary-improvements2/MapRendering/Web/Handlers/StaticHandler.cs
r367 r382 2 2 using System.Net; 3 3 using AllocsFixes.FileCache; 4 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest; 5 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse; 4 6 5 7 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 6 public class StaticHandler : PathHandler {8 public class StaticHandler : AbsHandler { 7 9 private readonly AbstractCache cache; 8 10 private readonly string datapath; … … 16 18 } 17 19 18 public override void HandleRequest ( HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,20 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con, 19 21 int _permissionLevel) { 20 string fn = _req .Url.AbsolutePath.Remove (0, urlBasePath.Length);22 string fn = _requestPath.Remove (0, urlBasePath.Length); 21 23 22 24 byte[] content = cache.GetFileContent (datapath + fn); … … 29 31 _resp.StatusCode = (int) HttpStatusCode.NotFound; 30 32 if (logMissingFiles) { 31 Log.Out ("Web:Static:FileNotFound: \"" + _req .Url.AbsolutePath + "\" @ \"" + datapath + fn + "\"");33 Log.Out ("Web:Static:FileNotFound: \"" + _requestPath + "\" @ \"" + datapath + fn + "\""); 32 34 } 33 35 } -
binary-improvements2/MapRendering/Web/Handlers/UserStatusHandler.cs
r369 r382 1 using System.Net;2 1 using AllocsFixes.JSON; 3 2 using AllocsFixes.NetConnections.Servers.Web.API; 3 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest; 4 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse; 4 5 5 6 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 6 public class UserStatusHandler : PathHandler {7 public class UserStatusHandler : AbsHandler { 7 8 public UserStatusHandler (string _moduleName = null) : base (_moduleName) { 8 9 } 9 10 10 public override void HandleRequest ( HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,11 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con, 11 12 int _permissionLevel) { 12 13 JSONObject result = new JSONObject (); 13 14 14 result.Add ("loggedin", new JSONBoolean (_ user!= null));15 result.Add ("username", new JSONString (_ user != null ? _user.UserId.ToString () : string.Empty));15 result.Add ("loggedin", new JSONBoolean (_con != null)); 16 result.Add ("username", new JSONString (_con != null ? _con.UserId.ToString () : string.Empty)); 16 17 17 18 JSONArray perms = new JSONArray ();
Note:
See TracChangeset
for help on using the changeset viewer.