[382] | 1 | using System;
|
---|
[244] | 2 | using System.IO;
|
---|
| 3 | using System.Net;
|
---|
| 4 | using System.Text;
|
---|
[382] | 5 | using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;
|
---|
| 6 | using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
|
---|
[244] | 7 |
|
---|
[325] | 8 | namespace AllocsFixes.NetConnections.Servers.Web.Handlers {
|
---|
[382] | 9 | public class SessionHandler : AbsHandler {
|
---|
| 10 | private const string pageBasePath = "/";
|
---|
| 11 | private const string steamOpenIdVerifyUrl = "verifysteamopenid";
|
---|
| 12 | private const string steamLoginUrl = "loginsteam";
|
---|
| 13 |
|
---|
[325] | 14 | private readonly string footer = "";
|
---|
| 15 | private readonly string header = "";
|
---|
[244] | 16 |
|
---|
[382] | 17 | private readonly ConnectionHandler connectionHandler;
|
---|
| 18 |
|
---|
| 19 | public SessionHandler (string _dataFolder, ConnectionHandler _connectionHandler) : base (null) {
|
---|
| 20 | connectionHandler = _connectionHandler;
|
---|
| 21 |
|
---|
[244] | 22 | if (File.Exists (_dataFolder + "/sessionheader.tmpl")) {
|
---|
| 23 | header = File.ReadAllText (_dataFolder + "/sessionheader.tmpl");
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | if (File.Exists (_dataFolder + "/sessionfooter.tmpl")) {
|
---|
| 27 | footer = File.ReadAllText (_dataFolder + "/sessionfooter.tmpl");
|
---|
| 28 | }
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[382] | 31 | public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con,
|
---|
[351] | 32 | int _permissionLevel) {
|
---|
[382] | 33 |
|
---|
| 34 | IPEndPoint reqRemoteEndPoint = _req.RemoteEndPoint;
|
---|
| 35 | if (reqRemoteEndPoint == null) {
|
---|
| 36 | _resp.Redirect (pageBasePath);
|
---|
| 37 | return;
|
---|
| 38 | }
|
---|
[244] | 39 |
|
---|
[382] | 40 | string subpath = _requestPath.Remove (0, urlBasePath.Length);
|
---|
| 41 |
|
---|
[244] | 42 | StringBuilder result = new StringBuilder ();
|
---|
| 43 | result.Append (header);
|
---|
| 44 |
|
---|
[382] | 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);
|
---|
[244] | 70 | }
|
---|
[325] | 71 |
|
---|
[382] | 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>");
|
---|
[244] | 74 | } else if (subpath.StartsWith ("logout")) {
|
---|
[382] | 75 | if (_con != null) {
|
---|
| 76 | connectionHandler.LogOut (_con.SessionID);
|
---|
| 77 | Cookie cookie = new Cookie ("sid", "", "/") {
|
---|
| 78 | Expired = true
|
---|
| 79 | };
|
---|
[351] | 80 | _resp.AppendCookie (cookie);
|
---|
[382] | 81 | _resp.Redirect (pageBasePath);
|
---|
[244] | 82 | return;
|
---|
| 83 | }
|
---|
[325] | 84 |
|
---|
[382] | 85 | result.Append ($"<h1>Not logged in, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>");
|
---|
| 86 | } else if (subpath.StartsWith (steamLoginUrl)) {
|
---|
[367] | 87 | string host = (Web.IsSslRedirected (_req) ? "https://" : "http://") + _req.UserHostName;
|
---|
[382] | 88 | string url = OpenID.GetOpenIdLoginUrl (host, host + urlBasePath + steamOpenIdVerifyUrl);
|
---|
[351] | 89 | _resp.Redirect (url);
|
---|
[244] | 90 | return;
|
---|
| 91 | } else {
|
---|
[382] | 92 | result.Append ($"<h1>Unknown command, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>");
|
---|
[244] | 93 | }
|
---|
| 94 |
|
---|
| 95 | result.Append (footer);
|
---|
| 96 |
|
---|
[351] | 97 | _resp.ContentType = MimeType.GetMimeType (".html");
|
---|
| 98 | _resp.ContentEncoding = Encoding.UTF8;
|
---|
[244] | 99 | byte[] buf = Encoding.UTF8.GetBytes (result.ToString ());
|
---|
[351] | 100 | _resp.ContentLength64 = buf.Length;
|
---|
| 101 | _resp.OutputStream.Write (buf, 0, buf.Length);
|
---|
[244] | 102 | }
|
---|
| 103 | }
|
---|
[325] | 104 | }
|
---|