Esempio n. 1
0
 private static void Reload(object sender, EventArgs e)
 {
     lock (db)
     {
         MainClass.DebugLog("Reloading user");
         List <string> file = new List <string>();
         file.AddRange(File.ReadAllLines(Configuration.UserDB));
         db.Clear();
         foreach (string line in file)
         {
             if (!line.Contains(":"))
             {
                 MainClass.DebugLog("Invalid line: " + line);
                 continue;
             }
             string[] info = line.Split(':');
             if (info.Length < 2)
             {
                 MainClass.DebugLog("Invalid user: " + line);
                 continue;
             }
             db.Add(info[0].ToLower(), info[1]);
         }
     }
 }
Esempio n. 2
0
        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);
                }
            }
        }
Esempio n. 3
0
 public static bool ValidName(string name)
 {
     if (name == null || name == "")
     {
         MainClass.DebugLog("Empty project name, ignoring");
         return(false);
     }
     if (name.Contains(":") ||
         name.Contains("*") ||
         name.Contains(" ") ||
         name.Contains("/") ||
         name.Contains("\\") ||
         name.Contains("?"))
     {
         MainClass.DebugLog("Dangerous char in project name: >>" + name + "<<");
         return(false);
     }
     return(true);
 }
Esempio n. 4
0
        public static bool Init()
        {
            if (Configuration.UserDB != null)
            {
                List <string> file = new List <string>();
                file.AddRange(File.ReadAllLines(Configuration.UserDB));
                foreach (string line in file)
                {
                    if (!line.Contains(":"))
                    {
                        MainClass.DebugLog("Invalid line: " + line);
                        continue;
                    }
                    string[] info = line.Split(':');
                    if (info.Length < 2)
                    {
                        MainClass.DebugLog("Invalid user: "******"FATAL: you require authentication but there is no user db defined");
                return(false);
            }
            return(true);
        }
Esempio n. 5
0
 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");
     }
 }
Esempio n. 6
0
        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--;
        }
Esempio n. 7
0
        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);
            }
        }