Changeset 394 for binary-improvements2/WebServer/src/UrlHandlers
- Timestamp:
- Aug 8, 2022, 8:07:44 PM (2 years ago)
- Location:
- binary-improvements2/WebServer/src/UrlHandlers
- Files:
-
- 2 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 }
Note:
See TracChangeset
for help on using the changeset viewer.