using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; using System.Runtime.Serialization.Formatters.Binary; using MiniHttpd; namespace AquaparkMonitor { public partial class Form1 : Form { string sServerSettingsXML = "MiniHttpdSettings.xml"; string sServerLogFile = "MiniHttpdlog.log"; // serwer HttpWebServer server = new HttpWebServer(); bool autoStart = true; bool logToScreen = true; StreamWriter logWriter; bool silent = false; string rootPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); MemoryStream serializedVirtualRoot; public enum DirectoryType { Drive, Virtual } private void ToogleRootFolderType() { if (server.Root is VirtualDirectory) { BinaryFormatter formatter = new BinaryFormatter(); serializedVirtualRoot = new MemoryStream(); formatter.Serialize(serializedVirtualRoot, server.Root); server.Root = new DriveDirectory(rootPath); } else { if (serializedVirtualRoot == null) server.Root = new VirtualDirectory(); else { BinaryFormatter formatter = new BinaryFormatter(); serializedVirtualRoot.Seek(0, SeekOrigin.Begin); server.Root = formatter.Deserialize(serializedVirtualRoot) as VirtualDirectory; } } } void writer_OnWrite(char[] buffer, int index, int count) { string text = new string(buffer, index, count); if (logToScreen) Console.Write(text); if (logWriter != null) logWriter.Write(text); } private void LoadSettings() { XmlDocument doc; XmlElement docElement; try { doc = new XmlDocument(); doc.Load(sServerSettingsXML); docElement = doc.DocumentElement; } catch { return; } try { rootPath = Path.GetDirectoryName(Application.ExecutablePath);//docElement["RootFolder"].InnerText; } catch { } try { DirectoryType type = (DirectoryType)Enum.Parse(typeof(DirectoryType), docElement["RootType"].InnerText); if ((server.Root is VirtualDirectory && type == DirectoryType.Drive) || server.Root is DriveDirectory && type == DirectoryType.Virtual) ToogleRootFolderType(); } catch { } try { serializedVirtualRoot = new MemoryStream(Convert.FromBase64String(docElement["VirtualDir"].InnerText)); if (server.Root is VirtualDirectory) { BinaryFormatter formatter = new BinaryFormatter(); serializedVirtualRoot.Seek(0, SeekOrigin.Begin); server.Root = formatter.Deserialize(serializedVirtualRoot) as VirtualDirectory; } } catch { } try { server.Port = int.Parse(docElement["Port"].InnerText); } catch { } try { autoStart = bool.Parse(docElement["AutoStart"].InnerText); } catch { } try { server.LogConnections = bool.Parse(docElement["LogConnections"].InnerText); } catch { } try { server.LogRequests = bool.Parse(docElement["LogRequests"].InnerText); } catch { } try { ToggleLogWriter(bool.Parse(docElement["LogToFile"].InnerText)); } catch { } try { logToScreen = bool.Parse(docElement["LogToScreen"].InnerText); } catch { } try { server.HostName = docElement["HostName"].InnerText; } catch { } try { foreach (XmlElement user in docElement["Users"].ChildNodes) { logWriter.Write(user["Name"].InnerText); logWriter.Write(user["Password"].InnerText); (server.Authenticator as BasicAuthenticator).AddUserByHash(user["Name"].InnerText, user["Password"].InnerText); } } //catch { } finally { } } private void ToggleLogWriter() { if (logWriter != null) ToggleLogWriter(false); else ToggleLogWriter(true); } void ToggleLogWriter(bool logToFile) { if (logToFile) { if (logWriter == null) { try { FileStream stream = new FileStream(sServerLogFile, FileMode.Append, FileAccess.Write, FileShare.Read, 128); logWriter = new StreamWriter(stream); logWriter.AutoFlush = true; logWriter.WriteLine("-------------------------------------------------------"); logWriter.WriteLine("* Beginning log at " + DateTime.Now); logWriter.WriteLine("-------------------------------------------------------"); } catch { } } } else { if (logWriter != null) { logWriter.Close(); logWriter = null; } } } public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { ConsoleWriter writer = new ConsoleWriter(); server.Log = writer; server.RequireAuthentication = true; writer.OnWrite += new ConsoleWriter.WriteEventHandler(writer_OnWrite); ToggleLogWriter(true); LoadSettings(); if (silent) { Console.SetOut(System.IO.TextWriter.Null); Console.SetError(System.IO.TextWriter.Null); try { server.Start(); while (server.IsRunning) System.Threading.Thread.Sleep(1000); return; } catch (Exception e1) { Console.Error.WriteLine(e1); return; } finally { } } if (autoStart) { try { server.Start(); Console.WriteLine(); } catch (Exception e2) { Console.Error.WriteLine(e2); } } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { server.Dispose(); } } }