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