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