[133] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
[168] | 3 | using System.IO;
|
---|
[133] | 4 | using System.Net;
|
---|
| 5 | using System.Text;
|
---|
| 6 | using System.Threading;
|
---|
| 7 | using UnityEngine;
|
---|
| 8 |
|
---|
| 9 | namespace AllocsFixes.NetConnections.Servers.Web
|
---|
| 10 | {
|
---|
| 11 | public class Web : IServer
|
---|
| 12 | {
|
---|
| 13 | private readonly HttpListener _listener = new HttpListener ();
|
---|
| 14 | private Dictionary<string, PathHandler> handlers = new Dictionary<string, PathHandler> ();
|
---|
[172] | 15 | private bool authEnabled = false;
|
---|
| 16 | private string realm = "7dtd Admin Panel";
|
---|
[133] | 17 |
|
---|
| 18 | public Web (int port)
|
---|
| 19 | {
|
---|
| 20 | try {
|
---|
| 21 | if (!HttpListener.IsSupported)
|
---|
| 22 | throw new NotSupportedException ("Needs Windows XP SP2, Server 2003 or later.");
|
---|
| 23 |
|
---|
[154] | 24 | handlers.Add ("/index.htm", new SimpleRedirectHandler ("/static/index.html"));
|
---|
[151] | 25 | handlers.Add ("/static/", new StaticHandler ("/static/", Application.dataPath + "/../webserver", false/*true*/, true)); // TODO: Enable cache
|
---|
[134] | 26 | handlers.Add ("/map/", new StaticHandler ("/map/", StaticDirectories.GetSaveGameDir () + "/map", false, false));
|
---|
[154] | 27 | handlers.Add ("/api/", new ApiHandler ("/api/"));
|
---|
[133] | 28 |
|
---|
| 29 | _listener.Prefixes.Add (String.Format ("http://*:{0}/", port));
|
---|
[172] | 30 | authEnabled = File.Exists (Application.dataPath + "/../webserver/protect");
|
---|
| 31 | if (authEnabled)
|
---|
[168] | 32 | _listener.AuthenticationSchemes = AuthenticationSchemes.Basic;
|
---|
[133] | 33 | _listener.Start ();
|
---|
[172] | 34 | _listener.Realm = realm;
|
---|
[133] | 35 |
|
---|
| 36 | ThreadPool.QueueUserWorkItem ((o) =>
|
---|
| 37 | {
|
---|
| 38 | try {
|
---|
| 39 | while (_listener.IsListening) {
|
---|
| 40 | ThreadPool.QueueUserWorkItem ((c) =>
|
---|
| 41 | {
|
---|
[134] | 42 | HttpListenerContext ctx = c as HttpListenerContext;
|
---|
| 43 | HandleRequest (ctx);
|
---|
[133] | 44 | }, _listener.GetContext ());
|
---|
| 45 | }
|
---|
| 46 | } catch {
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 | );
|
---|
| 50 |
|
---|
[172] | 51 | Log.Out ("Started Webserver on " + port + " (authentication " + (authEnabled ? "enabled" : "disabled") + ")");
|
---|
[133] | 52 | } catch (Exception e) {
|
---|
| 53 | Log.Out ("Error in Web.ctor: " + e);
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[134] | 57 | private void HandleRequest (HttpListenerContext ctx)
|
---|
[133] | 58 | {
|
---|
| 59 | try {
|
---|
[134] | 60 | ctx.Response.ProtocolVersion = new Version ("1.1");
|
---|
[133] | 61 |
|
---|
[154] | 62 | HttpListenerBasicIdentity user = Authorize (ctx);
|
---|
[134] | 63 |
|
---|
[172] | 64 | if (!authEnabled || (user.Name.ToLower ().Equals ("admin") && user.Password.Equals (GamePrefs.GetString (EnumGamePrefs.ControlPanelPassword)))) {
|
---|
| 65 | if (ctx.Request.Url.AbsolutePath.Length < 2) {
|
---|
| 66 | handlers ["/index.htm"].HandleRequest (ctx.Request, ctx.Response, user);
|
---|
| 67 | return;
|
---|
| 68 | } else {
|
---|
| 69 | foreach (KeyValuePair<string, PathHandler> kvp in handlers) {
|
---|
| 70 | if (ctx.Request.Url.AbsolutePath.StartsWith (kvp.Key)) {
|
---|
| 71 | kvp.Value.HandleRequest (ctx.Request, ctx.Response, user);
|
---|
| 72 | return;
|
---|
| 73 | }
|
---|
[133] | 74 | }
|
---|
| 75 | }
|
---|
[172] | 76 |
|
---|
| 77 | Log.Out ("Error in Web.HandleRequest(): No handler found for path \"" + ctx.Request.Url.AbsolutePath + "\"");
|
---|
| 78 | ctx.Response.StatusCode = (int)HttpStatusCode.NotFound;
|
---|
| 79 | } else {
|
---|
| 80 | ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
|
---|
| 81 | ctx.Response.Headers ["WWW-Authenticate"] = "Basic realm=\"" + realm + "\"";
|
---|
[133] | 82 | }
|
---|
| 83 |
|
---|
| 84 | // byte[] buf = Encoding.UTF8.GetBytes ("Hello World");
|
---|
| 85 | // resp.ContentLength64 = buf.Length;
|
---|
| 86 | // resp.ContentType = "text/html";
|
---|
| 87 | // resp.ContentEncoding = Encoding.UTF8;
|
---|
| 88 | // resp.OutputStream.Write (buf, 0, buf.Length);
|
---|
[134] | 89 | } catch (Exception e) {
|
---|
| 90 | Log.Out ("Error in Web.HandleRequest(): " + e);
|
---|
[133] | 91 | } finally {
|
---|
[134] | 92 | ctx.Response.Close ();
|
---|
[133] | 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[154] | 96 | private HttpListenerBasicIdentity Authorize (HttpListenerContext ctx)
|
---|
[134] | 97 | {
|
---|
| 98 | try {
|
---|
[154] | 99 | return (HttpListenerBasicIdentity)ctx.User.Identity;
|
---|
[134] | 100 | } catch (NullReferenceException) {
|
---|
[154] | 101 | return null;
|
---|
[134] | 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[133] | 105 | public void Disconnect ()
|
---|
| 106 | {
|
---|
| 107 | try {
|
---|
| 108 | _listener.Stop ();
|
---|
| 109 | _listener.Close ();
|
---|
| 110 | } catch (Exception e) {
|
---|
| 111 | Log.Out ("Error in Web.Disconnect: " + e);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | public void WriteToClient (string line)
|
---|
| 116 | {
|
---|
| 117 | try {
|
---|
[142] | 118 | //Log.Out ("NOT IMPLEMENTED: Web.WriteToClient");
|
---|
[133] | 119 | } catch (Exception e) {
|
---|
| 120 | Log.Out ("Error in Web.WriteToClient: " + e);
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | public void WriteToClient_Single (string line, IConnection client)
|
---|
| 125 | {
|
---|
| 126 | try {
|
---|
[142] | 127 | //Log.Out ("NOT IMPLEMENTED: Web.WriteToClient_Single");
|
---|
[133] | 128 | } catch (Exception e) {
|
---|
| 129 | Log.Out ("Error in Web.WriteToClient_Single: " + e);
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[163] | 133 | public static void SetResponseTextContent (HttpListenerResponse resp, string text)
|
---|
| 134 | {
|
---|
| 135 | byte[] buf = Encoding.UTF8.GetBytes (text);
|
---|
| 136 | resp.ContentLength64 = buf.Length;
|
---|
| 137 | resp.ContentType = "text/html";
|
---|
| 138 | resp.ContentEncoding = Encoding.UTF8;
|
---|
| 139 | resp.OutputStream.Write (buf, 0, buf.Length);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[133] | 142 | }
|
---|
| 143 | }
|
---|