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