/
AdminHandler.cs
194 lines (169 loc) · 7.75 KB
/
AdminHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using Maps.Search;
using Maps.Utilities;
using System;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Web;
namespace Maps.Admin
{
internal abstract class AdminHandlerBase : Maps.HandlerBase, IHttpHandler
{
public static bool AdminAuthorized(HttpContext context)
{
if (context.Request.IsLocal)
return true;
if (!context.Request.IsSecureConnection)
return false;
if (context.Request["key"] == System.Configuration.ConfigurationManager.AppSettings["AdminKey"])
return true;
SendError(context.Response, 403, "Forbidden", "Access Denied");
return false;
}
public bool IsReusable => true;
public void ProcessRequest(HttpContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
if (!AdminAuthorized(context))
{
context.Response.TrySkipIisCustomErrors = true;
context.Response.StatusCode = 403;
context.Response.StatusDescription = "Forbidden";
context.Response.ContentType = ContentTypes.Text.Plain;
context.Response.Output.WriteLine("Incorrect secret or connection not secure.");
context.Response.Flush();
return;
}
Process(context, new ResourceManager(context.Server));
}
protected abstract void Process(HttpContext context, ResourceManager resourceManager);
// TODO: Dedupe w/ DataResponder
protected static string GetStringOption(HttpContext context, string name)
{
if (context.Request[name] != null)
return context.Request[name];
var queryDefaults = Defaults(context);
if (queryDefaults != null && queryDefaults.ContainsKey(name))
return queryDefaults[name].ToString();
return null;
}
protected static bool GetBoolOption(HttpContext context, string name, bool defaultValue = false)
{
if (context.Request[name] != null)
return context.Request[name] == "1";
var queryDefaults = Defaults(context);
if (queryDefaults != null && queryDefaults.ContainsKey(name))
return queryDefaults[name].ToString() == "1";
return defaultValue;
}
}
internal class AdminHandler : AdminHandlerBase
{
protected override void Process(HttpContext context, ResourceManager resourceManager)
{
context.Server.ScriptTimeout = 3600; // An hour should be plenty
context.Response.ContentType = ContentTypes.Text.Html;
context.Response.BufferOutput = false;
Action<string> WriteLine = (string s) => { context.Response.Write(s); context.Response.Write("\n"); };
WriteLine("<!DOCTYPE html>");
WriteLine("<title>Admin Page</title>");
WriteLine("<style>");
using (var reader = new StreamReader(context.Server.MapPath("~/site.css"), Encoding.UTF8))
{
while (!reader.EndOfStream)
{
WriteLine(reader.ReadLine());
}
}
WriteLine("</style>");
WriteLine("<h1>Traveller Map - Administration</h1>");
context.Response.Flush();
string action = GetStringOption(context, "action");
switch (action)
{
case "reindex": Reindex(context); return;
case "flush": Flush(context); return;
case "profile": Profile(context); return;
}
Write(context.Response, "Unknown action: <pre>" + action + "</pre>");
Write(context.Response, "<b>Ω</b>");
}
private static void Write(HttpResponse response, string line)
{
response.Write("<div>");
response.Write(line);
response.Write("</div>\n");
response.Flush();
}
private static void Flush(HttpContext context)
{
SectorMap.Flush();
var enumerator = context.Cache.GetEnumerator();
while (enumerator.MoveNext())
{
context.Cache.Remove(enumerator.Key.ToString());
}
Write(context.Response, "Sector map flushed.");
Write(context.Response, "<b>Ω</b>");
}
private static void WriteStat<T>(HttpResponse response, string name, T value)
{
Write(response, name + ": " + value.ToString());
}
private static void Profile(HttpContext context)
{
WriteStat(context.Response, "Cache.Count", context.Cache.Count);
WriteStat(context.Response, "Cache.EffectivePercentagePhysicalMemoryLimit", context.Cache.EffectivePercentagePhysicalMemoryLimit);
WriteStat(context.Response, "Cache.EffectivePrivateBytesLimit", context.Cache.EffectivePrivateBytesLimit);
var process = System.Diagnostics.Process.GetCurrentProcess();
WriteStat(context.Response, "Process.MinWorkingSet", process.MinWorkingSet);
WriteStat(context.Response, "Process.MaxWorkingSet", process.MaxWorkingSet);
WriteStat(context.Response, "Process.PeakWorkingSet64", process.PeakWorkingSet64);
WriteStat(context.Response, "Process.PagedMemorySize64", process.PagedMemorySize64);
WriteStat(context.Response, "Process.PeakPagedMemorySize64", process.PeakPagedMemorySize64);
WriteStat(context.Response, "Process.PrivateMemorySize64", process.PrivateMemorySize64);
WriteStat(context.Response, "Process.VirtualMemorySize64", process.VirtualMemorySize64);
WriteStat(context.Response, "Process.WorkingSet64", process.WorkingSet64);
Write(context.Response, "<b>Ω</b>");
}
private static void Reindex(HttpContext context)
{
Write(context.Response, "Initializing resource manager...");
ResourceManager resourceManager = new ResourceManager(context.Server);
SearchEngine.PopulateDatabase(resourceManager, s => Write(context.Response, s));
Write(context.Response, " ");
Write(context.Response, "Summary:");
using (var connection = DBUtil.MakeConnection())
{
foreach (string table in new string[] { "sectors", "subsectors", "worlds", "labels" })
{
string sql = $"SELECT COUNT(*) FROM {table}";
using (var command = new SqlCommand(sql, connection))
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
Write(context.Response, $"{table}: {reader.GetInt32(0)}");
}
}
}
{
Write(context.Response, " ");
Write(context.Response, "Worlds by Milieu:");
string sql = $"SELECT milieu, COUNT(*) FROM worlds GROUP BY milieu ORDER BY milieu";
using (var command = new SqlCommand(sql, connection))
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
Write(context.Response, $"{reader.GetString(0)} — {reader.GetInt32(1)}");
}
}
}
}
Write(context.Response, "<b>Ω</b>");
}
}
}