Changeset 394
- Timestamp:
- Aug 8, 2022, 8:07:44 PM (2 years ago)
- Location:
- binary-improvements2/WebServer/src
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/WebServer/src/UrlHandlers/ApiHandler.cs
r391 r394 48 48 } 49 49 50 #if ENABLE_PROFILER51 50 private static readonly UnityEngine.Profiling.CustomSampler apiHandlerSampler = UnityEngine.Profiling.CustomSampler.Create ("API_Handler"); 52 #endif53 51 54 52 public override void HandleRequest (RequestContext _context) { … … 83 81 84 82 try { 85 #if ENABLE_PROFILER86 83 apiHandlerSampler.Begin (); 87 #endif88 84 api.HandleRequest (_context); 89 #if ENABLE_PROFILER90 85 apiHandlerSampler.End (); 91 #endif92 86 } catch (Exception e) { 93 87 Log.Error ($"Error in {nameof(ApiHandler)}.HandleRequest(): Handler {api.Name} threw an exception:"); -
binary-improvements2/WebServer/src/UrlHandlers/SessionHandler.cs
r391 r394 1 1 using System; 2 using System.IO;3 2 using System.Net; 4 using System.Text;5 3 6 4 namespace Webserver.UrlHandlers { 7 5 public class SessionHandler : AbsHandler { 8 private const string pageBasePath = "/"; 6 private const string pageBasePath = "/app"; 7 private const string pageErrorPath = "/app/error/"; 9 8 private const string steamOpenIdVerifyUrl = "verifysteamopenid"; 10 9 private const string steamLoginUrl = "loginsteam"; 11 12 private readonly string footer = "";13 private readonly string header = "";14 10 15 11 private readonly ConnectionHandler connectionHandler; 16 12 17 public SessionHandler ( string _dataFolder,ConnectionHandler _connectionHandler) : base (null) {13 public SessionHandler (ConnectionHandler _connectionHandler) : base (null) { 18 14 connectionHandler = _connectionHandler; 19 20 if (File.Exists (_dataFolder + "/sessionheader.tmpl")) {21 header = File.ReadAllText (_dataFolder + "/sessionheader.tmpl");22 }23 24 if (File.Exists (_dataFolder + "/sessionfooter.tmpl")) {25 footer = File.ReadAllText (_dataFolder + "/sessionfooter.tmpl");26 }27 15 } 28 16 29 17 public override void HandleRequest (RequestContext _context) { 30 31 IPEndPoint reqRemoteEndPoint = _context.Request.RemoteEndPoint; 32 if (reqRemoteEndPoint == null) { 33 _context.Response.Redirect (pageBasePath); 18 if (_context.Request.RemoteEndPoint == null) { 19 _context.Response.Redirect (pageErrorPath + "NoRemoteEndpoint"); 34 20 return; 35 21 } … … 37 23 string subpath = _context.RequestPath.Remove (0, urlBasePath.Length); 38 24 39 StringBuilder result = new StringBuilder (); 40 result.Append (header); 25 if (subpath.StartsWith (steamOpenIdVerifyUrl)) { 26 HandleSteamVerification (_context); 27 return; 28 } 41 29 42 if (subpath.StartsWith (steamOpenIdVerifyUrl)) { 43 string remoteEndpointString = reqRemoteEndPoint.ToString (); 30 if (subpath.StartsWith ("logout")) { 31 HandleLogout (_context); 32 return; 33 } 44 34 45 try { 46 ulong id = OpenID.Validate (_context.Request); 47 if (id > 0) { 48 WebConnection con = connectionHandler.LogIn (id, reqRemoteEndPoint.Address); 49 int level = GameManager.Instance.adminTools.GetUserPermissionLevel (con.UserId); 50 Log.Out ("Steam OpenID login from {0} with ID {1}, permission level {2}", 51 remoteEndpointString, con.UserId, level); 52 53 Cookie cookie = new Cookie ("sid", con.SessionID, "/") { 54 Expired = false, 55 Expires = DateTime.MinValue, 56 HttpOnly = true, 57 Secure = false 58 }; 59 _context.Response.AppendCookie (cookie); 60 _context.Response.Redirect (pageBasePath); 35 if (subpath.StartsWith (steamLoginUrl)) { 36 HandleSteamLogin (_context); 37 return; 38 } 61 39 62 return; 63 } 64 } catch (Exception e) { 65 Log.Error ("Error validating login:"); 66 Log.Exception (e); 67 } 40 _context.Response.Redirect (pageErrorPath + "InvalidSessionsCommand"); 41 } 68 42 69 Log.Out ($"Steam OpenID login failed from {remoteEndpointString}"); 70 result.Append ($"<h1>Login failed, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>"); 71 } else if (subpath.StartsWith ("logout")) { 72 if (_context.Connection != null) { 73 connectionHandler.LogOut (_context.Connection.SessionID); 74 Cookie cookie = new Cookie ("sid", "", "/") { 75 Expired = true 43 private void HandleSteamLogin (RequestContext _context) { 44 string host = (WebUtils.IsSslRedirected (_context.Request) ? "https://" : "http://") + _context.Request.UserHostName; 45 string url = OpenID.GetOpenIdLoginUrl (host, host + urlBasePath + steamOpenIdVerifyUrl); 46 _context.Response.Redirect (url); 47 } 48 49 private void HandleLogout (RequestContext _context) { 50 Cookie cookie = new Cookie ("sid", "", "/") { 51 Expired = true 52 }; 53 _context.Response.AppendCookie (cookie); 54 55 if (_context.Connection == null) { 56 _context.Response.Redirect (pageErrorPath + "NotLoggedIn"); 57 return; 58 } 59 60 connectionHandler.LogOut (_context.Connection.SessionID); 61 _context.Response.Redirect (pageBasePath); 62 } 63 64 private void HandleSteamVerification (RequestContext _context) { 65 string remoteEndpointString = _context.Request.RemoteEndPoint!.ToString (); 66 67 try { 68 ulong id = OpenID.Validate (_context.Request); 69 if (id > 0) { 70 WebConnection con = connectionHandler.LogIn (id, _context.Request.RemoteEndPoint.Address); 71 int level = GameManager.Instance.adminTools.GetUserPermissionLevel (con.UserId); 72 Log.Out ("Steam OpenID login from {0} with ID {1}, permission level {2}", 73 remoteEndpointString, con.UserId, level); 74 75 Cookie cookie = new Cookie ("sid", con.SessionID, "/") { 76 Expired = false, 77 Expires = DateTime.MinValue, 78 HttpOnly = true, 79 Secure = false 76 80 }; 77 81 _context.Response.AppendCookie (cookie); 78 82 _context.Response.Redirect (pageBasePath); 83 79 84 return; 80 85 } 81 82 result.Append ($"<h1>Not logged in, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>"); 83 } else if (subpath.StartsWith (steamLoginUrl)) { 84 string host = (WebUtils.IsSslRedirected (_context.Request) ? "https://" : "http://") + _context.Request.UserHostName; 85 string url = OpenID.GetOpenIdLoginUrl (host, host + urlBasePath + steamOpenIdVerifyUrl); 86 _context.Response.Redirect (url); 87 return; 88 } else { 89 result.Append ($"<h1>Unknown command, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>"); 86 } catch (Exception e) { 87 Log.Error ("Error validating login:"); 88 Log.Exception (e); 90 89 } 91 90 92 result.Append (footer); 91 Log.Out ($"Steam OpenID login failed from {remoteEndpointString}"); 92 _context.Response.Redirect (pageErrorPath + "SteamLoginFailed"); 93 } 93 94 94 WebUtils.WriteText (_context.Response, result.ToString (), _mimeType: WebUtils.MimeHtml);95 }96 95 } 97 96 } -
binary-improvements2/WebServer/src/Web.cs
r391 r394 63 63 RegisterWebMods (useStaticCache); 64 64 65 RegisterPathHandler ("/session/", new SessionHandler ( webfilesFolder,connectionHandler));65 RegisterPathHandler ("/session/", new SessionHandler (connectionHandler)); 66 66 RegisterPathHandler ("/userstatus", new UserStatusHandler ()); 67 67 RegisterPathHandler ("/sse/", new SseHandler ()); … … 152 152 } 153 153 154 #if ENABLE_PROFILER 154 private readonly UnityEngine.Profiling.CustomSampler getContextSampler = UnityEngine.Profiling.CustomSampler.Create ("GetCtx"); 155 155 private readonly UnityEngine.Profiling.CustomSampler authSampler = UnityEngine.Profiling.CustomSampler.Create ("Auth"); 156 private readonly UnityEngine.Profiling.CustomSampler cookieSampler = UnityEngine.Profiling.CustomSampler.Create ("ConCookie"); 156 157 private readonly UnityEngine.Profiling.CustomSampler handlerSampler = UnityEngine.Profiling.CustomSampler.Create ("Handler"); 157 #endif158 158 159 159 private void HandleRequest (IAsyncResult _result) { … … 165 165 #if ENABLE_PROFILER 166 166 UnityEngine.Profiling.Profiler.BeginThreadProfiling ("AllocsMods", "WebRequest"); 167 getContextSampler.Begin (); 167 168 HttpListenerContext ctx = listenerInstance.EndGetContext (_result); 169 getContextSampler.End (); 168 170 try { 169 171 #else … … 189 191 } 190 192 191 #if ENABLE_PROFILER192 193 authSampler.Begin (); 193 #endif194 194 int permissionLevel = DoAuthentication (request, out WebConnection conn); 195 #if ENABLE_PROFILER196 195 authSampler.End (); 197 #endif198 196 199 197 //Log.Out ("Login status: conn!=null: {0}, permissionlevel: {1}", conn != null, permissionLevel); 200 198 199 cookieSampler.Begin (); 201 200 if (conn != null) { 202 201 Cookie cookie = new Cookie ("sid", conn.SessionID, "/") { … … 208 207 response.AppendCookie (cookie); 209 208 } 209 cookieSampler.End (); 210 210 211 211 string requestPath = request.Url.AbsolutePath; … … 256 256 } 257 257 } else { 258 #if ENABLE_PROFILER259 258 handlerSampler.Begin (); 260 #endif261 259 handler.HandleRequest (_context); 262 #if ENABLE_PROFILER263 260 handlerSampler.End (); 264 #endif265 261 } 266 262 … … 291 287 } 292 288 293 string remoteEndpointString = reqRemoteEndPoint.ToString ();294 295 289 if (_req.QueryString ["adminuser"] == null || _req.QueryString ["admintoken"] == null) { 296 290 return guestPermissionLevel; … … 303 297 } 304 298 305 Log.Warning ("Invalid Admintoken used from " + re moteEndpointString);299 Log.Warning ("Invalid Admintoken used from " + reqRemoteEndPoint); 306 300 307 301 return guestPermissionLevel; -
binary-improvements2/WebServer/src/WebAPI/AbsRestApi.cs
r391 r394 6 6 namespace Webserver.WebAPI { 7 7 public abstract class AbsRestApi : AbsWebAPI { 8 private static readonly UnityEngine.Profiling.CustomSampler jsonDeserializeSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_Deserialize"); 9 8 10 public sealed override void HandleRequest (RequestContext _context) { 9 11 JsonNode jsonBody = null; … … 14 16 if (!string.IsNullOrEmpty (body)) { 15 17 try { 18 jsonDeserializeSampler.Begin (); 16 19 jsonBody = Parser.Parse (body); 20 jsonDeserializeSampler.End (); 17 21 } catch (Exception e) { 22 jsonDeserializeSampler.End (); 23 18 24 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, null, "INVALID_BODY", e); 19 25 return; -
binary-improvements2/WebServer/src/WebAPI/GetPlayerList.cs
r391 r394 13 13 new Regex (@"^(>=|=>|>|<=|=<|<|==|=)?\s*([0-9]+(\.[0-9]*)?)$"); 14 14 15 #if ENABLE_PROFILER16 15 private static readonly UnityEngine.Profiling.CustomSampler jsonSerializeSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_Build"); 17 #endif18 16 19 17 public override void HandleRequest (RequestContext _context) { … … 42 40 List<JsonObject> playerList = new List<JsonObject> (); 43 41 44 #if ENABLE_PROFILER45 42 jsonSerializeSampler.Begin (); 46 #endif47 43 48 44 foreach (KeyValuePair<PlatformUserIdentifierAbs, Player> kvp in playersList.Dict) { … … 76 72 } 77 73 78 #if ENABLE_PROFILER79 74 jsonSerializeSampler.End (); 80 #endif81 75 82 76 IEnumerable<JsonObject> list = playerList; -
binary-improvements2/WebServer/src/WebUtils.cs
r391 r394 12 12 public const string MimeJson = "application/json"; 13 13 14 #if ENABLE_PROFILER15 14 private static readonly UnityEngine.Profiling.CustomSampler jsonSerializeSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_Serialize"); 16 15 private static readonly UnityEngine.Profiling.CustomSampler netWriteSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_Write"); 17 #endif18 16 19 17 public static void WriteJson (HttpListenerResponse _resp, JsonNode _root, HttpStatusCode _statusCode = HttpStatusCode.OK) { 20 #if ENABLE_PROFILER21 18 jsonSerializeSampler.Begin (); 22 #endif23 19 StringBuilder sb = new StringBuilder (); 24 20 _root.ToString (sb); 25 #if ENABLE_PROFILER26 21 jsonSerializeSampler.End (); 27 22 netWriteSampler.Begin (); 28 #endif29 23 WriteText (_resp, sb.ToString(), _statusCode, MimeJson); 30 #if ENABLE_PROFILER31 24 netWriteSampler.End (); 32 #endif33 25 } 34 26
Note:
See TracChangeset
for help on using the changeset viewer.