source: binary-improvements/MapRendering/Web/Web.cs@ 279

Last change on this file since 279 was 279, checked in by alloc, 8 years ago

Mod stuff

File size: 8.2 KB
RevLine 
[230]1using System;
2using System.Collections.Generic;
[244]3using System.Collections.Specialized;
[230]4using System.IO;
5using System.Net;
6using System.Net.Sockets;
7using System.Reflection;
8using System.Text;
9using System.Threading;
10using UnityEngine;
11
[244]12using AllocsFixes.NetConnections.Servers.Web.Handlers;
13
[230]14namespace AllocsFixes.NetConnections.Servers.Web
15{
16 public class Web : IConsoleServer {
[244]17 private const int GUEST_PERMISSION_LEVEL = 2000;
[230]18 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;
[279]22 public static long totalHandlingTime = 0;
[230]23 private string dataFolder;
[244]24 private bool useStaticCache = false;
[230]25
[244]26 public bool isSslRedirected {
27 private set;
28 get;
29 }
30
31 public ConnectionHandler connectionHandler;
32
[230]33 public Web () {
34 try {
35 int webPort = GamePrefs.GetInt (EnumGamePrefs.ControlPanelPort);
36 if (webPort < 1 || webPort > 65533) {
[244]37 Log.Out ("Webserver not started (ControlPanelPort not within 1-65533)");
[230]38 return;
39 }
40 if (!Directory.Exists (Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location) + "/webserver")) {
41 Log.Out ("Webserver not started (folder \"webserver\" not found in WebInterface mod folder)");
42 return;
43 }
44
[244]45 // TODO: Read from config
46 isSslRedirected = false;
47 useStaticCache = false;
48
[230]49 dataFolder = Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location) + "/webserver";
50
51 if (!HttpListener.IsSupported) {
52 Log.Out ("Webserver not started (needs Windows XP SP2, Server 2003 or later or Mono)");
53 return;
54 }
55
56 handlers.Add (
57 "/index.htm",
58 new SimpleRedirectHandler ("/static/index.html"));
59 handlers.Add (
60 "/favicon.ico",
61 new SimpleRedirectHandler ("/static/favicon.ico"));
62 handlers.Add (
[244]63 "/session/",
64 new SessionHandler (
65 "/session/",
66 dataFolder,
67 this)
68 );
69 handlers.Add (
70 "/userstatus",
71 new UserStatusHandler ()
72 );
73 if (useStaticCache) {
74 handlers.Add (
75 "/static/",
76 new StaticHandler (
77 "/static/",
78 dataFolder,
79 new AllocsFixes.FileCache.SimpleCache (),
80 true)
81 );
82 } else {
83 handlers.Add (
84 "/static/",
85 new StaticHandler (
86 "/static/",
87 dataFolder,
88 new AllocsFixes.FileCache.DirectAccess (),
89 true)
90 );
91 }
[230]92
93 handlers.Add (
[238]94 "/itemicons/",
95 new ItemIconHandler (
96 "/itemicons/",
97 true)
98 );
99
100 handlers.Add (
[230]101 "/map/",
102 new StaticHandler (
103 "/map/",
[238]104 GameUtils.GetSaveGameDir () + "/map",
[230]105 MapRendering.MapRendering.GetTileCache (),
[244]106 false,
107 "web.map")
[230]108 );
109
[244]110 handlers.Add (
111 "/api/",
112 new ApiHandler ("/api/")
113 );
[230]114
[244]115 connectionHandler = new ConnectionHandler (this);
116
[230]117 _listener.Prefixes.Add (String.Format ("http://*:{0}/", webPort + 2));
118 _listener.Start ();
119
120 SdtdConsole.Instance.RegisterServer (this);
121
122 _listener.BeginGetContext (new AsyncCallback (HandleRequest), _listener);
123
[244]124 Log.Out ("Started Webserver on " + (webPort + 2));
[230]125 } catch (Exception e) {
126 Log.Out ("Error in Web.ctor: " + e);
127 }
128 }
129
130 private void HandleRequest (IAsyncResult result) {
131 if (_listener.IsListening) {
132 Interlocked.Increment (ref handlingCount);
133 Interlocked.Increment (ref currentHandlers);
[279]134 MicroStopwatch msw = new MicroStopwatch ();
[230]135 HttpListenerContext ctx = _listener.EndGetContext (result);
136 _listener.BeginGetContext (new AsyncCallback (HandleRequest), _listener);
137 try {
[244]138 HttpListenerRequest request = ctx.Request;
139 HttpListenerResponse response = ctx.Response;
[279]140 response.SendChunked = false;
[230]141
[244]142 response.ProtocolVersion = new Version ("1.1");
[230]143
[244]144 WebConnection conn;
145 int permissionLevel = DoAuthentication (request, out conn);
146
147
148 //Log.Out ("Login status: conn!=null: {0}, permissionlevel: {1}", conn != null, permissionLevel);
149
150
151 if (conn != null) {
152 Cookie cookie = new Cookie ("sid", conn.SessionID, "/");
153 cookie.Expired = false;
154 cookie.Expires = new DateTime (2020, 1, 1);
155 cookie.HttpOnly = true;
156 cookie.Secure = false;
157 response.AppendCookie (cookie);
158 }
159
160 if (request.Url.AbsolutePath.Length < 2) {
161 handlers ["/index.htm"].HandleRequest (request, response, conn, permissionLevel);
162 return;
163 } else {
164 foreach (KeyValuePair<string, PathHandler> kvp in handlers) {
165 if (request.Url.AbsolutePath.StartsWith (kvp.Key)) {
166 if (!kvp.Value.IsAuthorizedForHandler (conn, permissionLevel)) {
167 response.StatusCode = (int)HttpStatusCode.Forbidden;
168 if (conn != null) {
[247]169 //Log.Out ("Web.HandleRequest: user '{0}' not allowed to access '{1}'", conn.SteamID, kvp.Value.ModuleName);
[244]170 } else {
[247]171 //Log.Out ("Web.HandleRequest: unidentified user from '{0}' not allowed to access '{1}'", request.RemoteEndPoint.Address, kvp.Value.ModuleName);
[244]172 }
173 } else {
174 kvp.Value.HandleRequest (request, response, conn, permissionLevel);
[230]175 }
[244]176 return;
[230]177 }
178 }
[244]179 }
[230]180
[244]181 Log.Out ("Error in Web.HandleRequest(): No handler found for path \"" + request.Url.AbsolutePath + "\"");
182 response.StatusCode = (int)HttpStatusCode.NotFound;
[230]183 } catch (IOException e) {
184 if (e.InnerException is SocketException) {
185 Log.Out ("Error in Web.HandleRequest(): Remote host closed connection: " + e.InnerException.Message);
186 } else {
187 Log.Out ("Error (IO) in Web.HandleRequest(): " + e);
188 }
189 } catch (Exception e) {
190 Log.Out ("Error in Web.HandleRequest(): " + e);
191 } finally {
[279]192 if (ctx != null && !ctx.Response.SendChunked) {
[244]193 ctx.Response.Close ();
[230]194 }
[279]195 msw.Stop ();
196 totalHandlingTime += msw.ElapsedMicroseconds;
197 Log.Out ("Web.HandleRequest(): Took {0} µs", msw.ElapsedMicroseconds);
[230]198 Interlocked.Decrement (ref currentHandlers);
199 }
200 }
201 }
202
[244]203 private int DoAuthentication (HttpListenerRequest _req, out WebConnection _con) {
204 _con = null;
205
206 string sessionId = null;
207 if (_req.Cookies ["sid"] != null) {
208 sessionId = _req.Cookies ["sid"].Value;
[230]209 }
[244]210
211 if (!string.IsNullOrEmpty (sessionId)) {
212 WebConnection con = connectionHandler.IsLoggedIn (sessionId, _req.RemoteEndPoint.Address.ToString ());
213 if (con != null) {
214 _con = con;
215 return GameManager.Instance.adminTools.GetAdminToolsClientInfo (_con.SteamID.ToString ()).PermissionLevel;
216 }
217 }
218
219 if (_req.QueryString ["adminuser"] != null && _req.QueryString ["admintoken"] != null) {
220 WebPermissions.AdminToken admin = WebPermissions.Instance.GetWebAdmin (_req.QueryString ["adminuser"], _req.QueryString ["admintoken"]);
221 if (admin != null) {
222 return admin.permissionLevel;
223 } else {
224 Log.Warning ("Invalid Admintoken used from " + _req.RemoteEndPoint.ToString ());
225 }
226 }
227
228 if (_req.Url.AbsolutePath.StartsWith ("/session/verify")) {
229 ulong id = OpenID.Validate (_req);
230 if (id > 0) {
231 WebConnection con = connectionHandler.LogIn (id, _req.RemoteEndPoint.Address.ToString ());
232 _con = con;
[249]233 int level = GameManager.Instance.adminTools.GetAdminToolsClientInfo (id.ToString ()).PermissionLevel;
234 Log.Out ("Steam OpenID login from {0} with ID {1}, permission level {2}", _req.RemoteEndPoint.ToString (), con.SteamID, level);
235 return level;
[244]236 } else {
237 Log.Out ("Steam OpenID login failed from {0}", _req.RemoteEndPoint.ToString ());
238 }
239 }
240
241 return GUEST_PERMISSION_LEVEL;
[230]242 }
243
244 public void Disconnect () {
245 try {
246 _listener.Stop ();
247 _listener.Close ();
248 } catch (Exception e) {
249 Log.Out ("Error in Web.Disconnect: " + e);
250 }
251 }
252
253 public void SendLine (string line) {
[244]254 connectionHandler.SendLine (line);
[230]255 }
256
257 public void SendLog (string text, string trace, UnityEngine.LogType type) {
[250]258 // Do nothing, handled by LogBuffer internally
[230]259 }
260
261 public static void SetResponseTextContent (HttpListenerResponse resp, string text) {
262 byte[] buf = Encoding.UTF8.GetBytes (text);
263 resp.ContentLength64 = buf.Length;
264 resp.ContentType = "text/html";
265 resp.ContentEncoding = Encoding.UTF8;
266 resp.OutputStream.Write (buf, 0, buf.Length);
267 }
268
269 }
270}
Note: See TracBrowser for help on using the repository browser.