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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nancy;
using Nancy.Authentication.Forms;
using Nancy.Bootstrapper;
using Nancy.Hosting.Self;
using Nancy.ModelBinding;
using Nancy.Security;
using Nancy.TinyIoc;
using Newtonsoft.Json;
using ShiftOS.Objects;
namespace ShiftOS.Server.WebAdmin
{
class Program
{
static void Main(string[] args)
{
var HostConf = new HostConfiguration();
HostConf.UrlReservations.CreateAutomatically = true;
using(var nancy = new NancyHost(HostConf, new Uri("http://localhost:13371/mudadmin/")))
{
nancy.Start();
Console.WriteLine($"[{DateTime.Now}] <AdminPanel/NancyInit> Initiating on localhost:13371...");
Console.ReadLine();
}
}
}
public static class PageBuilder
{
public static string Build(string page, Dictionary<string, string> templateParams = null)
{
string templatehtml = Properties.Resources.HtmlTemplate;
if (templateParams == null)
{
templateParams = new Dictionary<string, string>();
}
if (!templateParams.ContainsKey("{logout}"))
{
templateParams.Add("{logout}", "<li><a href=\"/mudadmin/logout\">Log out</a></li>");
}
switch (page)
{
case "status":
templatehtml = templatehtml.Replace("{body}", Properties.Resources.Status);
break;
case "login":
templatehtml = templatehtml.Replace("{body}", Properties.Resources.LoginView);
break;
case "initialsetup":
templatehtml = templatehtml.Replace("{body}", Properties.Resources.SetupView);
break;
}
try
{
foreach (var param in templateParams)
{
templatehtml = templatehtml.Replace(param.Key, param.Value);
}
}
catch { }
return templatehtml;
}
}
public class MudUserIdentity : IUserIdentity
{
public MudUserIdentity(string username)
{
_username = username;
}
public IEnumerable<string> Claims
{
get
{
return SystemManager.GetClaims(_username);
}
}
private string _username = "";
public string UserName
{
get
{
return _username;
}
}
}
public static class SystemManager
{
public static List<string> GetClaims(string username)
{
foreach (var user in JsonConvert.DeserializeObject<List<MudUser>>(ShiftOS.Server.Program.ReadEncFile("users.json")))
{
if(user.Username == username)
{
return user.Claims;
}
}
return new List<string>(new[] { "User" });
}
public static bool Login(string username, string password, out Guid id)
{
foreach (var user in JsonConvert.DeserializeObject<List<MudUser>>(ShiftOS.Server.Program.ReadEncFile("users.json")))
{
if (user.Username == username && user.Password == Encryption.Encrypt(password))
{
id = user.ID;
return true;
}
}
id = new Guid();
return false;
}
public static string GetCPWorth()
{
if (System.IO.Directory.Exists("saves"))
{
int cp = 0;
foreach(var file in System.IO.Directory.GetFiles("saves"))
{
if (file.EndsWith(".save"))
{
var save = JsonConvert.DeserializeObject<Save>(Server.Program.ReadEncFile(file));
cp += save.Codepoints;
}
}
return cp.ToString();
}
else
{
return "0";
}
}
public static string GetUserCount()
{
if (System.IO.Directory.Exists("saves"))
{
return System.IO.Directory.GetFiles("saves").Length.ToString();
}
else
{
return "0";
}
}
public static MudUserIdentity GetIdentity(Guid id)
{
foreach (var user in JsonConvert.DeserializeObject<List<MudUser>>(ShiftOS.Server.Program.ReadEncFile("users.json")))
{
if (user.ID == id)
{
return new WebAdmin.MudUserIdentity(user.Username);
}
}
return null;
}
}
public class MudUser
{
public string Username { get; set; }
public string Password { get; set; }
public List<string> Claims { get; set; }
public Guid ID { get; set; }
}
public class MudBootstrapper : DefaultNancyBootstrapper
{
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
var formsAuthConfiguration = new FormsAuthenticationConfiguration();
formsAuthConfiguration.RedirectUrl = "~/login";
formsAuthConfiguration.UserMapper = container.Resolve<IUserMapper>();
FormsAuthentication.Enable(pipelines, formsAuthConfiguration);
base.ApplicationStartup(container, pipelines);
}
}
public class MudUserMapper : IUserMapper
{
public IUserIdentity GetUserFromIdentifier(Guid identifier, NancyContext context)
{
return SystemManager.GetIdentity(identifier);
}
}
public class LoginModule : NancyModule
{
public LoginModule()
{
Get["/login"] = parameters =>
{
if (System.IO.File.Exists("users.json"))
{
return PageBuilder.Build("login", new Dictionary<string, string>
{
{"{logout}", "" }
});
}
else
{
return PageBuilder.Build("initialsetup", new Dictionary<string, string>
{
{"{logout}", "" }
});
}
};
Get["/logout"] = parameters =>
{
return this.Logout("/");
};
Post["/login"] = parameters =>
{
var p = this.Bind<LoginRequest>();
Guid id = new Guid();
if (System.IO.File.Exists("users.json"))
{
if (SystemManager.Login(p.username, p.password, out id) == true)
{
return this.Login(id);
}
else
{
return PageBuilder.Build("loginFailed", new Dictionary<string, string>
{
{"{logout}", "" }
});
}
}
else
{
var mudUser = new MudUser();
mudUser.Username = p.username;
mudUser.Password = Encryption.Encrypt(p.password);
mudUser.Claims = new List<string>(new[] { "Admin" });
mudUser.ID = Guid.NewGuid();
id = mudUser.ID;
List<MudUser> users = new List<MudUser>(new[] { mudUser });
ShiftOS.Server.Program.WriteEncFile("users.json", JsonConvert.SerializeObject(users, Formatting.Indented));
return this.Login(id);
}
};
}
}
public class UserModule : NancyModule
{
public UserModule()
{
this.RequiresAuthentication();
this.RequiresClaims("Admin");
Get["/"] = _ =>
{
return PageBuilder.Build("status", new Dictionary<string, string>{
{ "{cp_worth}", SystemManager.GetCPWorth() },
{ "{user_count}", SystemManager.GetUserCount() },
{ "{system_time}", DateTime.Now.ToString() },
});
};
Get["/status"] = _ =>
{
return PageBuilder.Build("status");
};
}
}
public class LoginRequest
{
public string username { get; set; }
public string password { get; set; }
}
}
|