Changeset 325 for binary-improvements/MapRendering/Web/Web.cs
- Timestamp:
- Sep 4, 2018, 1:00:48 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/MapRendering/Web/Web.cs
r314 r325 1 1 using System; 2 2 using System.Collections.Generic; 3 using System.Collections.Specialized;4 3 using System.IO; 5 4 using System.Net; … … 8 7 using System.Text; 9 8 using System.Threading; 9 using AllocsFixes.FileCache; 10 using AllocsFixes.NetConnections.Servers.Web.Handlers; 10 11 using UnityEngine; 11 12 12 using AllocsFixes.NetConnections.Servers.Web.Handlers; 13 14 namespace AllocsFixes.NetConnections.Servers.Web 15 { 13 namespace AllocsFixes.NetConnections.Servers.Web { 16 14 public class Web : IConsoleServer { 17 15 private const int GUEST_PERMISSION_LEVEL = 2000; 16 public static int handlingCount; 17 public static int currentHandlers; 18 public static long totalHandlingTime = 0; 18 19 private readonly HttpListener _listener = new HttpListener (); 19 private Dictionary<string, PathHandler> handlers = new Dictionary<string, PathHandler> (); 20 public static int handlingCount = 0; 21 public static int currentHandlers = 0; 22 public static long totalHandlingTime = 0; 23 private string dataFolder; 24 private bool useStaticCache = false; 25 26 public static bool isSslRedirected (HttpListenerRequest req) { 27 string proto = req.Headers ["X-Forwarded-Proto"]; 28 if (!string.IsNullOrEmpty (proto)) { 29 return proto.Equals ("https", StringComparison.OrdinalIgnoreCase); 30 } 31 32 return false; 33 } 20 private readonly string dataFolder; 21 private readonly Dictionary<string, PathHandler> handlers = new Dictionary<string, PathHandler> (); 22 private readonly bool useStaticCache; 34 23 35 24 public ConnectionHandler connectionHandler; … … 42 31 return; 43 32 } 44 if (!Directory.Exists (Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location) + "/webserver")) { 33 34 if (!Directory.Exists (Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location) + 35 "/webserver")) { 45 36 Log.Out ("Webserver not started (folder \"webserver\" not found in WebInterface mod folder)"); 46 37 return; … … 56 47 return; 57 48 } 58 59 handlers.Add ( 60 "/index.htm", 61 new SimpleRedirectHandler ("/static/index.html")); 62 handlers.Add ( 63 "/favicon.ico", 64 new SimpleRedirectHandler ("/static/favicon.ico")); 65 handlers.Add ( 49 50 handlers.Add ( 51 "/index.htm", 52 new SimpleRedirectHandler ("/static/index.html")); 53 handlers.Add ( 54 "/favicon.ico", 55 new SimpleRedirectHandler ("/static/favicon.ico")); 56 handlers.Add ( 57 "/session/", 58 new SessionHandler ( 66 59 "/session/", 67 new SessionHandler ( 68 "/session/", 69 dataFolder, 70 this) 71 ); 72 handlers.Add ( 73 "/userstatus", 74 new UserStatusHandler () 60 dataFolder, 61 this) 62 ); 63 handlers.Add ( 64 "/userstatus", 65 new UserStatusHandler () 75 66 ); 76 67 if (useStaticCache) { 77 68 handlers.Add ( 69 "/static/", 70 new StaticHandler ( 78 71 "/static/", 79 new StaticHandler ( 80 "/static/", 81 dataFolder, 82 new AllocsFixes.FileCache.SimpleCache (), 83 false) 72 dataFolder, 73 new SimpleCache (), 74 false) 84 75 ); 85 76 } else { 86 77 handlers.Add ( 78 "/static/", 79 new StaticHandler ( 87 80 "/static/", 88 new StaticHandler ( 89 "/static/", 90 dataFolder, 91 new AllocsFixes.FileCache.DirectAccess (), 92 false) 81 dataFolder, 82 new DirectAccess (), 83 false) 93 84 ); 94 85 } … … 118 109 connectionHandler = new ConnectionHandler (this); 119 110 120 _listener.Prefixes.Add ( String.Format ("http://*:{0}/", webPort + 2));111 _listener.Prefixes.Add (string.Format ("http://*:{0}/", webPort + 2)); 121 112 _listener.Start (); 122 113 123 114 SdtdConsole.Instance.RegisterServer (this); 124 115 125 _listener.BeginGetContext ( new AsyncCallback (HandleRequest), _listener);116 _listener.BeginGetContext (HandleRequest, _listener); 126 117 127 118 Log.Out ("Started Webserver on " + (webPort + 2)); … … 129 120 Log.Out ("Error in Web.ctor: " + e); 130 121 } 122 } 123 124 public void Disconnect () { 125 try { 126 _listener.Stop (); 127 _listener.Close (); 128 } catch (Exception e) { 129 Log.Out ("Error in Web.Disconnect: " + e); 130 } 131 } 132 133 public void SendLine (string line) { 134 connectionHandler.SendLine (line); 135 } 136 137 public void SendLog (string text, string trace, LogType type) { 138 // Do nothing, handled by LogBuffer internally 139 } 140 141 public static bool isSslRedirected (HttpListenerRequest req) { 142 string proto = req.Headers ["X-Forwarded-Proto"]; 143 if (!string.IsNullOrEmpty (proto)) { 144 return proto.Equals ("https", StringComparison.OrdinalIgnoreCase); 145 } 146 147 return false; 131 148 } 132 149 … … 135 152 Interlocked.Increment (ref handlingCount); 136 153 Interlocked.Increment (ref currentHandlers); 154 137 155 // MicroStopwatch msw = new MicroStopwatch (); 138 156 HttpListenerContext ctx = _listener.EndGetContext (result); 139 _listener.BeginGetContext ( new AsyncCallback (HandleRequest), _listener);157 _listener.BeginGetContext (HandleRequest, _listener); 140 158 try { 141 159 HttpListenerRequest request = ctx.Request; … … 163 181 // No game yet -> fail request 164 182 if (GameManager.Instance.World == null) { 165 response.StatusCode = (int) HttpStatusCode.ServiceUnavailable;183 response.StatusCode = (int) HttpStatusCode.ServiceUnavailable; 166 184 return; 167 185 } … … 174 192 if (request.Url.AbsolutePath.StartsWith (kvp.Key)) { 175 193 if (!kvp.Value.IsAuthorizedForHandler (conn, permissionLevel)) { 176 response.StatusCode = (int) HttpStatusCode.Forbidden;194 response.StatusCode = (int) HttpStatusCode.Forbidden; 177 195 if (conn != null) { 178 196 //Log.Out ("Web.HandleRequest: user '{0}' not allowed to access '{1}'", conn.SteamID, kvp.Value.ModuleName); 179 } else {180 //Log.Out ("Web.HandleRequest: unidentified user from '{0}' not allowed to access '{1}'", request.RemoteEndPoint.Address, kvp.Value.ModuleName);181 197 } 182 198 } else { 183 199 kvp.Value.HandleRequest (request, response, conn, permissionLevel); 184 200 } 201 185 202 return; 186 203 } … … 190 207 // Not really relevant for non-debugging purposes: 191 208 //Log.Out ("Error in Web.HandleRequest(): No handler found for path \"" + request.Url.AbsolutePath + "\""); 192 response.StatusCode = (int) HttpStatusCode.NotFound;209 response.StatusCode = (int) HttpStatusCode.NotFound; 193 210 } catch (IOException e) { 194 211 if (e.InnerException is SocketException) { 195 Log.Out ("Error in Web.HandleRequest(): Remote host closed connection: " + e.InnerException.Message); 212 Log.Out ("Error in Web.HandleRequest(): Remote host closed connection: " + 213 e.InnerException.Message); 196 214 } else { 197 215 Log.Out ("Error (IO) in Web.HandleRequest(): " + e); … … 203 221 ctx.Response.Close (); 204 222 } 223 205 224 // msw.Stop (); 206 225 // totalHandlingTime += msw.ElapsedMicroseconds; … … 223 242 if (con != null) { 224 243 _con = con; 225 return GameManager.Instance.adminTools.GetAdminToolsClientInfo (_con.SteamID.ToString ()).PermissionLevel; 244 return GameManager.Instance.adminTools.GetAdminToolsClientInfo (_con.SteamID.ToString ()) 245 .PermissionLevel; 226 246 } 227 247 } 228 248 229 249 if (_req.QueryString ["adminuser"] != null && _req.QueryString ["admintoken"] != null) { 230 WebPermissions.AdminToken admin = WebPermissions.Instance.GetWebAdmin (_req.QueryString ["adminuser"], _req.QueryString ["admintoken"]); 250 WebPermissions.AdminToken admin = WebPermissions.Instance.GetWebAdmin (_req.QueryString ["adminuser"], 251 _req.QueryString ["admintoken"]); 231 252 if (admin != null) { 232 253 return admin.permissionLevel; 233 } else {234 Log.Warning ("Invalid Admintoken used from " + _req.RemoteEndPoint.ToString ()); 235 }254 } 255 256 Log.Warning ("Invalid Admintoken used from " + _req.RemoteEndPoint); 236 257 } 237 258 … … 242 263 WebConnection con = connectionHandler.LogIn (id, _req.RemoteEndPoint.Address.ToString ()); 243 264 _con = con; 244 int level = GameManager.Instance.adminTools.GetAdminToolsClientInfo (id.ToString ()).PermissionLevel; 245 Log.Out ("Steam OpenID login from {0} with ID {1}, permission level {2}", _req.RemoteEndPoint.ToString (), con.SteamID, level); 265 int level = GameManager.Instance.adminTools.GetAdminToolsClientInfo (id.ToString ()) 266 .PermissionLevel; 267 Log.Out ("Steam OpenID login from {0} with ID {1}, permission level {2}", 268 _req.RemoteEndPoint.ToString (), con.SteamID, level); 246 269 return level; 247 } else {248 Log.Out ("Steam OpenID login failed from {0}", _req.RemoteEndPoint.ToString ()); 249 }270 } 271 272 Log.Out ("Steam OpenID login failed from {0}", _req.RemoteEndPoint.ToString ()); 250 273 } catch (Exception e) { 251 274 Log.Error ("Error validating login:"); … … 255 278 256 279 return GUEST_PERMISSION_LEVEL; 257 }258 259 public void Disconnect () {260 try {261 _listener.Stop ();262 _listener.Close ();263 } catch (Exception e) {264 Log.Out ("Error in Web.Disconnect: " + e);265 }266 }267 268 public void SendLine (string line) {269 connectionHandler.SendLine (line);270 }271 272 public void SendLog (string text, string trace, UnityEngine.LogType type) {273 // Do nothing, handled by LogBuffer internally274 280 } 275 281 … … 281 287 resp.OutputStream.Write (buf, 0, buf.Length); 282 288 } 283 284 289 } 285 290 }
Note:
See TracChangeset
for help on using the changeset viewer.