Rename Adminmodel.cs to AdminModel.cs, cause it sould be Uppercase!
This commit is contained in:
parent
6d28eb4b37
commit
9b98bd8660
@ -1,157 +1,157 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using BlubbFish.Utils;
|
using BlubbFish.Utils;
|
||||||
using BlubbFish.Utils.IoT.Bots;
|
using BlubbFish.Utils.IoT.Bots;
|
||||||
using LitJson;
|
using LitJson;
|
||||||
|
|
||||||
namespace Fraunhofer.Fit.IoT.LoraMap.Model.Admin {
|
namespace Fraunhofer.Fit.IoT.LoraMap.Model.Admin {
|
||||||
class AdminModel {
|
class AdminModel {
|
||||||
public delegate void AdminEvent(Object sender, EventArgs e);
|
public delegate void AdminEvent(Object sender, EventArgs e);
|
||||||
public event AdminEvent NamesUpdate;
|
public event AdminEvent NamesUpdate;
|
||||||
|
|
||||||
private readonly Dictionary<Int64, AdminSession> session = new Dictionary<Int64, AdminSession>();
|
private readonly Dictionary<Int64, AdminSession> session = new Dictionary<Int64, AdminSession>();
|
||||||
private readonly Dictionary<String, String> settings;
|
private readonly Dictionary<String, String> settings;
|
||||||
|
|
||||||
public AdminModel(Dictionary<String, String> settings) {
|
public AdminModel(Dictionary<String, String> settings) {
|
||||||
this.settings = settings;
|
this.settings = settings;
|
||||||
if(!settings.ContainsKey("admin_user") || !settings.ContainsKey("admin_pass")) {
|
if(!settings.ContainsKey("admin_user") || !settings.ContainsKey("admin_pass")) {
|
||||||
Helper.WriteError("Kann die Einstellungen [webserver] admin_user und admin_pass nicht laden!");
|
Helper.WriteError("Kann die Einstellungen [webserver] admin_user und admin_pass nicht laden!");
|
||||||
throw new FileNotFoundException("Kann die Einstellungen [webserver] admin_user und admin_pass nicht laden!");
|
throw new FileNotFoundException("Kann die Einstellungen [webserver] admin_user und admin_pass nicht laden!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean ParseReuqest(HttpListenerContext cont) {
|
public Boolean ParseReuqest(HttpListenerContext cont) {
|
||||||
if(cont.Request.Url.PathAndQuery == "/admin/login") {
|
if(cont.Request.Url.PathAndQuery == "/admin/login") {
|
||||||
return this.Login(cont);
|
return this.Login(cont);
|
||||||
}
|
}
|
||||||
if(!this.CheckAuth(cont)) {
|
if(!this.CheckAuth(cont)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if(cont.Request.Url.PathAndQuery.StartsWith("/admin/get_json_")) {
|
if(cont.Request.Url.PathAndQuery.StartsWith("/admin/get_json_")) {
|
||||||
return this.SendJson(cont);
|
return this.SendJson(cont);
|
||||||
}
|
}
|
||||||
if(cont.Request.Url.PathAndQuery.StartsWith("/admin/set_json_")) {
|
if(cont.Request.Url.PathAndQuery.StartsWith("/admin/set_json_")) {
|
||||||
return this.GetJson(cont);
|
return this.GetJson(cont);
|
||||||
}
|
}
|
||||||
return Webserver.SendFileResponse(cont);
|
return Webserver.SendFileResponse(cont);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Boolean GetJson(HttpListenerContext cont) {
|
private Boolean GetJson(HttpListenerContext cont) {
|
||||||
if(cont.Request.Url.PathAndQuery == "/admin/set_json_names") {
|
if(cont.Request.Url.PathAndQuery == "/admin/set_json_names") {
|
||||||
StreamReader reader = new StreamReader(cont.Request.InputStream, cont.Request.ContentEncoding);
|
StreamReader reader = new StreamReader(cont.Request.InputStream, cont.Request.ContentEncoding);
|
||||||
String rawData = reader.ReadToEnd();
|
String rawData = reader.ReadToEnd();
|
||||||
cont.Request.InputStream.Close();
|
cont.Request.InputStream.Close();
|
||||||
reader.Close();
|
reader.Close();
|
||||||
try {
|
try {
|
||||||
JsonMapper.ToObject(rawData);
|
JsonMapper.ToObject(rawData);
|
||||||
} catch(Exception) {
|
} catch(Exception) {
|
||||||
Helper.WriteError("501 - Error recieving names.json " + cont.Request.Url.PathAndQuery);
|
Helper.WriteError("501 - Error recieving names.json " + cont.Request.Url.PathAndQuery);
|
||||||
cont.Response.StatusCode = 501;
|
cont.Response.StatusCode = 501;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
File.WriteAllText("json/names.json", rawData);
|
File.WriteAllText("json/names.json", rawData);
|
||||||
Console.WriteLine("200 - Post names.json " + cont.Request.Url.PathAndQuery);
|
Console.WriteLine("200 - Post names.json " + cont.Request.Url.PathAndQuery);
|
||||||
this.NamesUpdate?.Invoke(this, new EventArgs());
|
this.NamesUpdate?.Invoke(this, new EventArgs());
|
||||||
return true;
|
return true;
|
||||||
} else if(cont.Request.Url.PathAndQuery == "/admin/set_json_geo") {
|
} else if(cont.Request.Url.PathAndQuery == "/admin/set_json_geo") {
|
||||||
StreamReader reader = new StreamReader(cont.Request.InputStream, cont.Request.ContentEncoding);
|
StreamReader reader = new StreamReader(cont.Request.InputStream, cont.Request.ContentEncoding);
|
||||||
String rawData = reader.ReadToEnd();
|
String rawData = reader.ReadToEnd();
|
||||||
cont.Request.InputStream.Close();
|
cont.Request.InputStream.Close();
|
||||||
reader.Close();
|
reader.Close();
|
||||||
try {
|
try {
|
||||||
JsonMapper.ToObject(rawData);
|
JsonMapper.ToObject(rawData);
|
||||||
} catch(Exception) {
|
} catch(Exception) {
|
||||||
Helper.WriteError("501 - Error recieving geo.json " + cont.Request.Url.PathAndQuery);
|
Helper.WriteError("501 - Error recieving geo.json " + cont.Request.Url.PathAndQuery);
|
||||||
cont.Response.StatusCode = 501;
|
cont.Response.StatusCode = 501;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
File.WriteAllText("json/geo.json", rawData);
|
File.WriteAllText("json/geo.json", rawData);
|
||||||
Console.WriteLine("200 - Post geo.json " + cont.Request.Url.PathAndQuery);
|
Console.WriteLine("200 - Post geo.json " + cont.Request.Url.PathAndQuery);
|
||||||
this.NamesUpdate?.Invoke(this, new EventArgs());
|
this.NamesUpdate?.Invoke(this, new EventArgs());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Boolean SendJson(HttpListenerContext cont) {
|
private Boolean SendJson(HttpListenerContext cont) {
|
||||||
if(cont.Request.Url.PathAndQuery == "/admin/get_json_names") {
|
if(cont.Request.Url.PathAndQuery == "/admin/get_json_names") {
|
||||||
String file = File.ReadAllText("json/names.json");
|
String file = File.ReadAllText("json/names.json");
|
||||||
Byte[] buf = Encoding.UTF8.GetBytes(file);
|
Byte[] buf = Encoding.UTF8.GetBytes(file);
|
||||||
cont.Response.ContentLength64 = buf.Length;
|
cont.Response.ContentLength64 = buf.Length;
|
||||||
cont.Response.OutputStream.Write(buf, 0, buf.Length);
|
cont.Response.OutputStream.Write(buf, 0, buf.Length);
|
||||||
Console.WriteLine("200 - Send names.json " + cont.Request.Url.PathAndQuery);
|
Console.WriteLine("200 - Send names.json " + cont.Request.Url.PathAndQuery);
|
||||||
return true;
|
return true;
|
||||||
} else if(cont.Request.Url.PathAndQuery == "/admin/get_json_geo") {
|
} else if(cont.Request.Url.PathAndQuery == "/admin/get_json_geo") {
|
||||||
String file = File.ReadAllText("json/geo.json");
|
String file = File.ReadAllText("json/geo.json");
|
||||||
Byte[] buf = Encoding.UTF8.GetBytes(file);
|
Byte[] buf = Encoding.UTF8.GetBytes(file);
|
||||||
cont.Response.ContentLength64 = buf.Length;
|
cont.Response.ContentLength64 = buf.Length;
|
||||||
cont.Response.OutputStream.Write(buf, 0, buf.Length);
|
cont.Response.OutputStream.Write(buf, 0, buf.Length);
|
||||||
Console.WriteLine("200 - Send geo.json " + cont.Request.Url.PathAndQuery);
|
Console.WriteLine("200 - Send geo.json " + cont.Request.Url.PathAndQuery);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Helper.WriteError("404 - Section in get_json not found " + cont.Request.Url.PathAndQuery + "!");
|
Helper.WriteError("404 - Section in get_json not found " + cont.Request.Url.PathAndQuery + "!");
|
||||||
cont.Response.StatusCode = 404;
|
cont.Response.StatusCode = 404;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Boolean Login(HttpListenerContext cont) {
|
private Boolean Login(HttpListenerContext cont) {
|
||||||
Dictionary<String, String> POST = Webserver.GetPostParams(cont.Request);
|
Dictionary<String, String> POST = Webserver.GetPostParams(cont.Request);
|
||||||
if(POST.ContainsKey("user") && POST["user"] == this.settings["admin_user"] && POST.ContainsKey("pass") && POST["pass"] == this.settings["admin_pass"]) {
|
if(POST.ContainsKey("user") && POST["user"] == this.settings["admin_user"] && POST.ContainsKey("pass") && POST["pass"] == this.settings["admin_pass"]) {
|
||||||
Int64 sessionid = 0;
|
Int64 sessionid = 0;
|
||||||
while(true) {
|
while(true) {
|
||||||
sessionid = AdminSession.GetRandomSessionid();
|
sessionid = AdminSession.GetRandomSessionid();
|
||||||
if(!this.session.ContainsKey(sessionid)) {
|
if(!this.session.ContainsKey(sessionid)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(cont.Request.Cookies["loramapsession"] != null) {
|
if(cont.Request.Cookies["loramapsession"] != null) {
|
||||||
if(Int64.TryParse(cont.Request.Cookies["loramapsession"].Value, out Int64 cookiesessionid)) {
|
if(Int64.TryParse(cont.Request.Cookies["loramapsession"].Value, out Int64 cookiesessionid)) {
|
||||||
if(this.session.ContainsKey(cookiesessionid)) {
|
if(this.session.ContainsKey(cookiesessionid)) {
|
||||||
if(!this.session[sessionid].IsLoggedin) {
|
if(!this.session[sessionid].IsLoggedin) {
|
||||||
sessionid = cookiesessionid;
|
sessionid = cookiesessionid;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(!this.session.ContainsKey(sessionid)) {
|
if(!this.session.ContainsKey(sessionid)) {
|
||||||
this.session.Add(sessionid, new AdminSession());
|
this.session.Add(sessionid, new AdminSession());
|
||||||
}
|
}
|
||||||
this.session[sessionid].IsLoggedin = true;
|
this.session[sessionid].IsLoggedin = true;
|
||||||
cont.Response.AppendCookie(new Cookie("loramapsession", sessionid.ToString()) {
|
cont.Response.AppendCookie(new Cookie("loramapsession", sessionid.ToString()) {
|
||||||
Expires = DateTime.Now.AddYears(1)
|
Expires = DateTime.Now.AddYears(1)
|
||||||
});
|
});
|
||||||
cont.Response.AddHeader("Location", "/admin");
|
cont.Response.AddHeader("Location", "/admin");
|
||||||
cont.Response.StatusCode = 307;
|
cont.Response.StatusCode = 307;
|
||||||
Console.WriteLine("200 - Login OK! " + cont.Request.Url.PathAndQuery);
|
Console.WriteLine("200 - Login OK! " + cont.Request.Url.PathAndQuery);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
cont.Response.AddHeader("Location", "/admin/login.html");
|
cont.Response.AddHeader("Location", "/admin/login.html");
|
||||||
cont.Response.StatusCode = 307;
|
cont.Response.StatusCode = 307;
|
||||||
Helper.WriteError("307 - Login WRONG! " + cont.Request.Url.PathAndQuery);
|
Helper.WriteError("307 - Login WRONG! " + cont.Request.Url.PathAndQuery);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Boolean CheckAuth(HttpListenerContext cont) {
|
private Boolean CheckAuth(HttpListenerContext cont) {
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
return true;
|
return true;
|
||||||
#endif
|
#endif
|
||||||
if(cont.Request.Url.PathAndQuery.StartsWith("/admin/login.html")) {
|
if(cont.Request.Url.PathAndQuery.StartsWith("/admin/login.html")) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
if(cont.Request.Cookies["loramapsession"] != null) {
|
if(cont.Request.Cookies["loramapsession"] != null) {
|
||||||
if(Int64.TryParse(cont.Request.Cookies["loramapsession"].Value, out Int64 sessionid)) {
|
if(Int64.TryParse(cont.Request.Cookies["loramapsession"].Value, out Int64 sessionid)) {
|
||||||
if(this.session.ContainsKey(sessionid)) {
|
if(this.session.ContainsKey(sessionid)) {
|
||||||
return this.session[sessionid].IsLoggedin;
|
return this.session[sessionid].IsLoggedin;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cont.Response.StatusCode = 403;
|
cont.Response.StatusCode = 403;
|
||||||
Helper.WriteError("403 - " + cont.Request.Url.PathAndQuery);
|
Helper.WriteError("403 - " + cont.Request.Url.PathAndQuery);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user