예제 #1
0
파일: Class1.cs 프로젝트: creeperlv/L2KDB
        public List <string> GetForms()
        {
            List <string> vs = new List <string>();

            if (isConnected == true)
            {
                {
                    AdvancedStream.SendMessage(ref Writer, $"L2KDB:Basic:GetForms|{sessionID}", aes);
                    var Command = aes.Decrypt(Reader.ReadLine());
                    var data    = AdvancedStream.ReadToCurrentEnd(ref Reader, aes, false);
                    if (Command == "L2KDB:Basic:DatabaseGetFormsResult")
                    {
                        StringReader stringReader = new StringReader(data);
                        var          temp         = "";
                        while ((temp = stringReader.ReadLine()) != null)
                        {
                            if (temp == "L2KDB:Basic:NoForms")
                            {
                            }
                            else
                            {
                                vs.Add(temp);
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("" + Command);
                    }
                }
                return(vs);
            }
            throw new Exception("Unconnected");
        }
예제 #2
0
파일: L2KDB.cs 프로젝트: creeperlv/L2KDB
        private static DatabaseDescription OpenDescription(string location, CryptographyCredential credential)
        {
            DatabaseDescription description = new DatabaseDescription();

            if (credential == null)
            {
                var c = File.ReadAllText(location);
                var l = c.Split(':');
                description.isPublic = bool.Parse(l[1]);
            }
            else if (credential.Key == "")
            {
                var c = File.ReadAllText(location);
                var l = c.Split(':');
                description.isPublic = bool.Parse(l[1]);
            }
            else
            {
                var         c   = File.ReadAllText(location);
                CustomedAES aes = new CustomedAES();
                aes.Key = credential.Key;
                aes.IV  = credential.IV;
                var l = (aes.Decrypt(c)).Split(':');
                description.isPublic = bool.Parse(l[1]);
            }
            return(new DatabaseDescription());
        }
예제 #3
0
        public static string ReadToCurrentEnd(ref StreamReader streamReader, CustomedAES aes, bool isHeaderRequired = true)
        {
            string content = "";
            string tmp;

            while (true)
            {
                try
                {
                    tmp = aes.Decrypt(streamReader.ReadLine());
                    if (tmp == "")
                    {
                        break;
                    }
                    if (tmp == "L2KDB:Basic:EndOfCurrentTransmission")
                    {
                        break;
                    }
                    if (content == "")
                    {
                        content += tmp;
                    }
                    else
                    {
                        content += Environment.NewLine + tmp;
                    }
                    if (content.StartsWith("L2KDB:"))
                    {
                    }
                    else if (isHeaderRequired == true)
                    {
                        return("WRONG HEADER");
                    }
                }
                catch (Exception)
                {
                    break;
                }
            }
            return(content);
        }
예제 #4
0
파일: Session.cs 프로젝트: creeperlv/L2KDB
        public async void SessionWorker()
        {
            SessionID = Guid.NewGuid();
            Diagnotor.CurrentDiagnotor.Log("Session Opened:" + SessionID + ", AuthID=" + AuthID);
            SessionKey      = CustomedAES.GenerateKey();
            SessionIV       = CustomedAES.GenerateIV();
            CustomedAES.Key = SessionKey;
            CustomedAES.IV  = SessionIV;
            int ExceptionOccurred = 0;

            AdvancedStream.SendMessage(ref Writer, "L2KDB:Basic:ConnectionAccept," + SessionID.ToString() + $",{SessionKey},{SessionIV}");
            while (willStop == false)
            {
                try
                {
                    var Command = CustomedAES.Decrypt(await Reader.ReadLineAsync());
                    var data    = AdvancedStream.ReadToCurrentEnd(ref Reader, CustomedAES, false);

                    /**
                     * Data Structure:
                     * [Command]|[SessionID]|[Key(Optional)]|[IV(Optional)]
                     * [Cont...
                     * en...
                     * t]
                     **/
                    Diagnotor.CurrentDiagnotor.Log($"Command from {SessionID}:" + Command);

                    var cmd = Command.Split('|');
                    if (cmd[1] != SessionID.ToString())
                    {
                        Stop(StopReason.Unknown);
                    }

                    var result = CmdletProcesser(cmd, data);
                    if (result != "-1")
                    {
                        AdvancedStream.SendMessage(ref Writer, $"{result}", CustomedAES);
                    }
                }
                catch (Exception e)
                {
                    if (e.GetType() == typeof(SocketException))
                    {
                        Diagnotor.CurrentDiagnotor.Log($"Shutting {SessionID} Down.");

                        Stop();
                        break;
                    }
                    else if (e.GetType() == typeof(IOException))
                    {
                        Diagnotor.CurrentDiagnotor.Log($"Shutting {SessionID} Down.");
                        Stop();
                        break;
                    }
                    else
                    {
                        ExceptionOccurred++;
                        Diagnotor.CurrentDiagnotor.LogWarning("Captured:" + e.Message);
                        if (ExceptionOccurred >= 100)
                        {
                            Diagnotor.CurrentDiagnotor.LogError($"Session:{SessionID} occurred too many errors, force to shutdown.");
                            Stop();
                        }
                    }
                }
            }
        }
예제 #5
0
        Dictionary <string, object> LoadFile(string name)
        {
            Dictionary <string, object> d = new Dictionary <string, object>();
            FileInfo fi          = new FileInfo(Path.Combine(FormDirectory.FullName, name + ".lite-db"));
            string   Content     = "";
            string   id2         = "";
            var      tR          = fi.OpenText();
            bool     StopReading = false;
            bool     isCombine   = false;
            bool     isFSL       = false;

            while (StopReading == false)
            {
                var tmp = tR.ReadLine();
                if (tmp == null)
                {
                    //Console.WriteLine("Cannot find:" + id2);
                    break;
                }
                if (isCombine == false)
                {
                    if (tmp.StartsWith("#DATA:"))
                    {
                        isCombine = true;
                        id2       = tmp.Substring("#DATA:".Length);
                    }
                }
                else
                {
                    if (tmp != "DATA#")
                    {
                        if (Content == "")
                        {
                            Content = tmp;
                        }
                        else
                        {
                            Content += "\r\n" + tmp;
                        }
                    }
                    else
                    {
                        isFSL     = true;
                        isCombine = false;
                        if (cryptographyCredential.Key != "")
                        {
                            d.Add(id2, aes.Decrypt(Content));
                        }
                        else
                        {
                            d.Add(id2, Content);
                        }
                        Content = "";
                        id2     = "";
                    }
                }
            }
            try
            {
                tR.Close();
            }
            catch (Exception)
            {
            }
            try
            {
                tR.Dispose();
            }
            catch (Exception)
            {
            }

            return(d);
        }