public static void Exec() { MainClass.DebugLog("Listening on TCP port " + Configuration.Port); System.Net.Sockets.TcpListener server = new System.Net.Sockets.TcpListener(IPAddress.Any, Configuration.Port); server.Start(); while (true) { try { System.Net.Sockets.TcpClient connection = server.AcceptTcpClient(); Thread _client = new Thread(HandleClient); _client.Start(connection); } catch (Exception fail) { MainClass.exceptionHandler(fail); } } }
private static void exec() { try { while (IsWriting) { List <Line> temp = new List <Line>(); lock (db) { temp.AddRange(db); db.Clear(); } foreach (Line line in temp) { try { System.IO.File.AppendAllText(line.file, line.text + "\n"); }catch (Exception fail) { MainClass.exceptionHandler(fail); MainClass.DebugLog("Unable to store a line, returning back to db"); lock (db) { db.Add(line); } continue; } } System.Threading.Thread.Sleep(8000); } } catch (Exception fail) { MainClass.exceptionHandler(fail); MainClass.Log("ERROR: Writer is down"); } }
private static void HandleClient(object data) { try { System.Net.Sockets.TcpClient connection = (System.Net.Sockets.TcpClient)data; MainClass.DebugLog("Incoming connection from: " + connection.Client.RemoteEndPoint.ToString()); Connections++; OpenConnections++; connection.NoDelay = true; System.Net.Sockets.NetworkStream ns = connection.GetStream(); System.IO.StreamReader Reader = new System.IO.StreamReader(ns); string text; string timestamp = ""; // give the user access to global cache // save the reference to global cache because we might need it in future System.IO.StreamWriter Writer = new System.IO.StreamWriter(ns); while (connection.Connected && !Reader.EndOfStream) { text = Reader.ReadLine(); string command = text; List <string> list = new List <string>(); string parameters = ""; if (command.Contains(" ")) { parameters = command.Substring(command.IndexOf(" ") + 1); command = command.Substring(0, command.IndexOf(" ")); if (parameters.Contains(" ")) { list.AddRange(parameters.Split(' ')); } } string project = null; string section = null; string l = null; string token = null; int type = 0; switch (command.ToLower()) { case "n": case "s": case "store": if (Configuration.RequireAuth) { Writer.WriteLine("ERROR: you need to authenticate to log here"); Writer.Flush(); continue; } if (list.Count < 3) { Writer.WriteLine("ERROR: you are missing parameters for this command"); Writer.Flush(); continue; } project = list[0]; section = null; if (project.Contains(":")) { section = project.Substring(project.IndexOf(":") + 1); project = project.Substring(0, project.IndexOf(":")); if (!Logger.ValidName(section)) { Writer.WriteLine("ERROR: you provided invalid section name"); Writer.Flush(); continue; } } if (!Logger.ValidName(project)) { Writer.WriteLine("ERROR: you provided invalid section name"); Writer.Flush(); continue; } if (Auth.RequireLogin(project)) { Writer.WriteLine("ERROR: you need to authenticate to log here"); Writer.Flush(); continue; } type = 0; l = text.Substring(list[0].Length + list[1].Length + command.Length + 3); if (!int.TryParse(list[1], out type)) { Writer.WriteLine("ERROR: you provided invalid log type"); Writer.Flush(); continue; } if (Logger.Write(l, project, section, type)) { if (command != "n") { Writer.WriteLine("STORED"); Writer.Flush(); } continue; } Writer.WriteLine("ERROR: internal error, check debug log"); Writer.Flush(); continue; case "a": if (list.Count < 4) { Writer.WriteLine("ERROR: you are missing parameters for this command"); Writer.Flush(); continue; } project = list[0]; section = null; if (project.Contains(":")) { section = project.Substring(project.IndexOf(":") + 1); project = project.Substring(0, project.IndexOf(":")); if (!Logger.ValidName(section)) { Writer.WriteLine("ERROR: you provided invalid section name"); Writer.Flush(); continue; } } if (!Logger.ValidName(project)) { Writer.WriteLine("ERROR: you provided invalid section name"); Writer.Flush(); continue; } token = list[2]; type = 0; l = text.Substring(list[0].Length + list[1].Length + command.Length + token.Length + 4); if (!Auth.Login(project, token)) { Writer.WriteLine("ERROR: you provided invalid token"); Writer.Flush(); continue; } if (!int.TryParse(list[1], out type)) { Writer.WriteLine("ERROR: you provided invalid log type"); Writer.Flush(); continue; } if (Logger.Write(l, project, section, type)) { if (command != "n") { Writer.WriteLine("STORED"); Writer.Flush(); } continue; } Writer.WriteLine("ERROR: internal error, check debug log"); Writer.Flush(); continue; case "quit": connection.Close(); OpenConnections--; return; } Writer.WriteLine("ERROR"); Writer.Flush(); } } catch (Exception fail) { MainClass.exceptionHandler(fail); } OpenConnections--; }
public static void exec() { try { while (true) { int recv; byte[] data = new byte[1024]; IPEndPoint ipep = new IPEndPoint(IPAddress.Any, Configuration.Port); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); newsock.Bind(ipep); IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint Remote = (EndPoint)(sender); recv = newsock.ReceiveFrom(data, ref Remote); MainClass.DebugLog("Message received from: " + Remote.ToString()); string t = Encoding.ASCII.GetString(data, 0, recv); List <string> xx = new List <string>(); if (t.Contains("\n")) { xx.AddRange(t.Split('\n')); } else { xx.Add(t); } foreach (string text in xx) { string command = text; List <string> list = new List <string>(); string parameters = ""; if (command.Contains(" ")) { parameters = command.Substring(command.IndexOf(" ") + 1); command = command.Substring(0, command.IndexOf(" ")); if (parameters.Contains(" ")) { list.AddRange(parameters.Split(' ')); } } string project = null; string section = null; int type = 0; string token = null; string l = null; switch (command.ToLower()) { case "s": case "store": if (Configuration.RequireAuth) { continue; } if (list.Count < 3) { MainClass.DebugLog(Remote.ToString() + ": ERROR: you are missing parameters for this command"); continue; } project = list[0]; section = null; if (project.Contains(":")) { section = project.Substring(project.IndexOf(":") + 1); project = project.Substring(0, project.IndexOf(":")); if (!Logger.ValidName(section)) { MainClass.DebugLog(Remote.ToString() + ": ERROR: you provided invalid section name"); continue; } } if (!Logger.ValidName(project)) { MainClass.DebugLog(Remote.ToString() + ": ERROR: you provided invalid section name"); continue; } if (Auth.RequireLogin(project)) { MainClass.DebugLog(Remote.ToString() + ": ERROR: you need to authenticate to log here"); continue; } type = 0; l = text.Substring(list[0].Length + list[1].Length + command.Length + 3); if (!int.TryParse(list[1], out type)) { MainClass.DebugLog("ERROR: you provided invalid log type"); continue; } if (Logger.Write(l, project, section, type)) { MainClass.DebugLog("STORED"); continue; } MainClass.DebugLog("ERROR: internal error, check debug log"); continue; case "a": if (list.Count < 4) { MainClass.DebugLog(Remote.ToString() + ": ERROR: you are missing parameters for this command"); continue; } project = list[0]; section = null; token = list[2]; if (project.Contains(":")) { section = project.Substring(project.IndexOf(":") + 1); project = project.Substring(0, project.IndexOf(":")); if (!Logger.ValidName(section)) { MainClass.DebugLog(Remote.ToString() + ": ERROR: you provided invalid section name"); continue; } } if (!Logger.ValidName(project)) { MainClass.DebugLog(Remote.ToString() + ": ERROR: you provided invalid section name"); continue; } if (!Auth.Login(project, token)) { MainClass.DebugLog(Remote.ToString() + ": ERROR: you provided invalid token"); continue; } type = 0; l = text.Substring(list[0].Length + list[1].Length + command.Length + token.Length + 4); if (!int.TryParse(list[1], out type)) { MainClass.DebugLog("ERROR: you provided invalid log type"); continue; } if (Logger.Write(l, project, section, type)) { MainClass.DebugLog("STORED"); continue; } MainClass.DebugLog("ERROR: internal error, check debug log"); continue; } newsock.Close(); } } }catch (Exception fail) { MainClass.exceptionHandler(fail); } }
public static bool Write(string log, string file, string section, int type) { try { if (!Directory.Exists(Configuration.Root + Path.DirectorySeparatorChar + file)) { Directory.CreateDirectory(Configuration.Root + Path.DirectorySeparatorChar + file); } // format the name of file switch (type) { case 3: case 1: case 5: section = section + "_" + DateTime.Now.ToString("yyyy_MM_dd"); break; } string suffix = ".txt"; switch (type) { case 2: case 3: suffix = ".html"; break; case 4: case 5: suffix = ".xml"; break; } string name = Configuration.Root + Path.DirectorySeparatorChar + file + Path.DirectorySeparatorChar + file + "_" + section + suffix; if (section == "" || section == null) { name = Configuration.Root + Path.DirectorySeparatorChar + file + Path.DirectorySeparatorChar + file + suffix; } string data = DateTime.Now.ToString() + ": " + log; // format text switch (type) { case 2: case 3: data = "<font class=\"timestamp\">" + DateTime.Now.ToString() + "</font><font class=\"text\">: " + System.Web.HttpUtility.HtmlEncode(log) + "</font><br>"; break; case 4: case 5: data = "<data time=\"" + Unix() + "\">" + System.Web.HttpUtility.HtmlEncode(log) + "</data>"; break; } Writer.InsertText(data, name); return(true); } catch (Exception fail) { MainClass.exceptionHandler(fail); } return(false); }