Exemplo n.º 1
0
 private void SaveData()
 {
     try {
         using (FileStream fs = new FileStream(DataFilePath + "range.conf", FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
             using (StreamWriter sw = new StreamWriter(fs)) {
                 sw.WriteLine(MinValue.ToString());
                 sw.WriteLine(MaxValue.ToString());
                 Logger.Write("Save range", Logger.LogLevel.Information);
             }
         }
         if (HasAvoid)
         {
             using (FileStream fs = new FileStream(DataFilePath + "avoid.conf", FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
                 using (StreamWriter sw = new StreamWriter(fs)) {
                     foreach (int i in AvoidValues)
                     {
                         sw.WriteLine(DataManage.Encrypt(i.ToString(), Key));
                     }
                 }
             }
             Logger.Write("Save avoid values", Logger.LogLevel.Information);
         }
         if (HasMore)
         {
             using (FileStream fs = new FileStream(DataFilePath + "more.conf", FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
                 using (StreamWriter sw = new StreamWriter(fs)) {
                     sw.WriteLine(DataManage.Encrypt(MoreRate.ToString(), Key));
                     for (int i = 0; i < MoreValues.Count; i++)
                     {
                         sw.WriteLine(DataManage.Encrypt(MoreValues[i].ToString(), Key));
                     }
                 }
             }
             Logger.Write("Save more values", Logger.LogLevel.Information);
         }
     } catch (UnauthorizedAccessException ex) {
         MessageBox.Show(ex.Message + "\nFile IO Exception!", "UnauthorizedAccessException", MessageBoxButtons.OK, MessageBoxIcon.Error);
         Logger.Write("UnauthorizedAccessException in SaveData()", Logger.LogLevel.Exception);
         Logger.Write(ex.ToString(), Logger.LogLevel.Exception);
     } catch (Exception ex) {
         MessageBox.Show(ex.ToString(), "UnHandledException", MessageBoxButtons.OK, MessageBoxIcon.Error);
         Logger.Write("UnhandledException in SaveData()", Logger.LogLevel.Exception);
         Logger.Write(ex.ToString(), Logger.LogLevel.Exception);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 读取数据文件
        /// </summary>
        private void LoadData()
        {
            try {
                Logger.Write("Loading data from " + DataFilePath, Logger.LogLevel.Information);
                if (!Directory.Exists(DataFilePath))
                {
                    Directory.CreateDirectory(DataFilePath);
                    Logger.Write("Create directory " + DataFilePath, Logger.LogLevel.Information);
                }

                //读取范围
                if (File.Exists(DataFilePath + "range.conf"))
                {
                    using (StreamReader file = new StreamReader(DataFilePath + "range.conf")) {
                        List <string> fileStrLines = new List <string>();
                        while (!file.EndOfStream)
                        {
                            fileStrLines.Add(file.ReadLine());
                        }

                        //判断范围文件为空
                        if (fileStrLines.Count == 0)
                        {
                            using (FileStream fs = new FileStream(DataFilePath + "range.conf", FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
                                using (StreamWriter sw = new StreamWriter(fs)) {
                                    sw.WriteLine("1");
                                    sw.WriteLine("50");
                                }
                            }
                        }
                        else if (fileStrLines.Count == 2)
                        {
                            bool convertSuccessful =
                                int.TryParse(fileStrLines[0], out MinValue) &&
                                int.TryParse(fileStrLines[1], out MaxValue);
                            if (!convertSuccessful || MinValue >= MaxValue)
                            {
                                MinValue = 1;
                                MaxValue = 50;
                            }
                        }
                        else
                        {
                            Logger.Write("Range file syntax error", Logger.LogLevel.Warning);
                            return;
                        }

                        Logger.Write(string.Format("Read range {0}-{1}", MinValue, MaxValue), Logger.LogLevel.Information);
                    }
                }
                else
                {
                    using (FileStream fs = new FileStream(DataFilePath + "range.conf", FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
                        using (StreamWriter sw = new StreamWriter(fs)) {
                            sw.WriteLine("1");
                            sw.WriteLine("50");
                        }
                    }
                    Logger.Write("Create range.conf", Logger.LogLevel.Information);
                }

                //读取密钥
                if (File.Exists(DataFilePath + "key.conf"))
                {
                    bool regenerate = false;
                    using (StreamReader fs = new StreamReader(DataFilePath + "key.conf")) {
                        if (!fs.EndOfStream)
                        {
                            string key = fs.ReadLine();
                            if (key == Key)
                            {
                                regenerate = true;
                            }
                            else
                            {
                                Key = key;
                            }
                        }
                        else
                        {
                            regenerate = true;
                        }
                    }
                    if (regenerate)
                    {
                        using (StreamWriter sw = new StreamWriter(new FileStream(DataFilePath + "key.conf", FileMode.Truncate, FileAccess.ReadWrite))) {
                            Random keyGenerator = new Random();
                            string key          = "";
                            for (int i = 0; i < 8; i++)
                            {
                                key += Convert.ToChar(keyGenerator.Next(49, 123));
                            }
                            sw.WriteLine(key);
                        }
                        Logger.Write("Regenerate key", Logger.LogLevel.Information);
                    }
                    Logger.Write("Load key", Logger.LogLevel.Information);
                }
                else
                {
                    using (FileStream fsw = new FileStream(DataFilePath + "key.conf", FileMode.Create, FileAccess.Write)) {
                        string key = "";
                        using (StreamWriter sw = new StreamWriter(fsw)) {
                            Random keyGenerator = new Random();
                            for (int i = 0; i < 8; i++)
                            {
                                key += Convert.ToChar(keyGenerator.Next(48, 123));
                            }
                            sw.WriteLine(key);
                            Key = key;
                        }
                    }
                    Logger.Write("Create key.conf", Logger.LogLevel.Information);
                }

                //读取后门文件 - avoid
                if (File.Exists(DataFilePath + "avoid.conf"))
                {
                    HasAvoid = true;
                    using (StreamReader fs = new StreamReader(DataFilePath + "avoid.conf")) {
                        List <string> fileStrs = new List <string>();
                        while (!fs.EndOfStream)
                        {
                            fileStrs.Add(fs.ReadLine());
                        }

                        if (fileStrs.Count == 0)
                        {
                            HasAvoid = false;
                            Logger.Write("No avoid values", Logger.LogLevel.Information);
                        }
                        else
                        {
                            string logAvoid = "";
                            foreach (string str in fileStrs)
                            {
                                if (int.TryParse(DataManage.Decrypt(str.ToCharArray(), Key), out int num))
                                {
                                    AvoidValues.Add(num);
                                    logAvoid += num.ToString() + " ";
                                }
                            }
                            Logger.Write("Load avoid values - " + logAvoid, Logger.LogLevel.Information);
                        }
                    }
                }

                //读取后门文件 - more
                if (File.Exists(DataFilePath + "more.conf"))
                {
                    HasMore = true;
                    using (StreamReader fs = new StreamReader(DataFilePath + "more.conf")) {
                        List <string> fileStrs = new List <string>();
                        while (!fs.EndOfStream)
                        {
                            fileStrs.Add(fs.ReadLine());
                        }

                        if (fileStrs.Count <= 1)
                        {
                            HasMore = false;
                            Logger.Write("No more values", Logger.LogLevel.Information);
                        }
                        else
                        {
                            string logMore = "";
                            if (int.TryParse(DataManage.Decrypt(fileStrs[0].ToCharArray(), Key), out int rate))
                            {
                                MoreRate = rate;
                            }
                            for (int i = 1; i < fileStrs.Count; i++)
                            {
                                if (int.TryParse(DataManage.Decrypt(fileStrs[i].ToCharArray(), Key), out int num))
                                {
                                    MoreValues.Add(num);
                                    logMore += num.ToString();
                                }
                            }
                            Logger.Write("Load more values - " + logMore, Logger.LogLevel.Information);
                        }
                    }
                }

                if (!HasAvoid)
                {
                    File.Delete(DataFilePath + "avoid.conf");
                }
                if (!HasMore)
                {
                    File.Delete(DataFilePath + "more.conf");
                }
            } catch (UnauthorizedAccessException ex) {
                MessageBox.Show(ex.Message, "UnauthorizedAccessException", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Logger.Write("UnauthorizedAccessException in LoadData()", Logger.LogLevel.Exception);
                Logger.Write(ex.ToString(), Logger.LogLevel.Exception);
            } catch (Exception ex) {
                MessageBox.Show(ex.ToString(), "UnHandledException", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Logger.Write("UnhandledException in LoadData()", Logger.LogLevel.Exception);
                Logger.Write(ex.ToString(), Logger.LogLevel.Exception);
            }
        }