Пример #1
0
        public void FillOutbox(NanoDB from)
        {
            if (!Directory.Exists(Strings.Upload))
            {
                Directory.CreateDirectory(Strings.Upload);
            }

            new PngContainerCreatorNew().SaveToPngContainer(from);
            from.WriteNewPosts(false);
        }
Пример #2
0
        public void SaveToPngContainer(NanoDB db)
        {
            var files = new DirectoryInfo(Strings.Containers).GetFiles("*.png");

            if (files.Length == 0)
            {
                NotificationHandler.Instance.AddNotification("Не найдены PNG файлы в папке containers.");
                return;
            }

            FileInfo file = files[random.Next(files.Length - 1)];
            var bmp = new Bitmap(file.FullName);
            int capacity = (int)(bmp.Width * bmp.Height * 3 / 8);
            string sessionPrefix = random.Next().ToString("x8");
            sessionPrefix += random.Next().ToString("x8");

            byte[] packed = null;
            byte[] prevPacked = null;
            int amount = 1;

            int realAmount = 0;

            while (amount < 65535 && (prevPacked == null || packed == null || packed.Length < capacity - 32))
            {
                prevPacked = packed;
                var lastPosts = db.GetNLastPosts(amount).ExceptHidden(db);
                var randomPosts = db.GetNRandomPosts(amount).ExceptHidden(db).FilterBySize(16384);
                var posts = new List<NanoPost>();
                posts.AddRange(lastPosts);
                posts.AddRange(randomPosts);
                realAmount = posts.Count;
                packed = Pack(posts.ToArray().Randomized());
                amount = 1 + amount + amount * 10 / 15;
            }

            packed = prevPacked;
            Console.WriteLine(string.Format("PNG capacity:{0}, posts amount (not unique):{1}, packed size:{2}", capacity, realAmount, packed.Length));

            if (capacity - packed.Length > 32)
            {
                var noise = new byte[capacity - packed.Length - 32];
                random.NextBytes(noise);
                var temp = new List<byte>();
                temp.AddRange(packed);
                temp.AddRange(noise);
                packed = temp.ToArray();
            }

            new PngStegoUtil().HideBytesInPng(
                        file.FullName,
                        Strings.Upload + Path.DirectorySeparatorChar + sessionPrefix + Strings.PngExt,
                        packed);
            NotificationHandler.Instance.AddNotification("Контейнер сохранён: " + Strings.Upload + Path.DirectorySeparatorChar + sessionPrefix + Strings.PngExt);
        }
Пример #3
0
        public ApplicationStarter()
        {
            PrepareFolders();

            var db = new NanoDB();
            new DownloadCheckDaemon(db);
            db.ReadPosts();
            /*form.FormClosed += (object sender, FormClosedEventArgs e) => {
                daemon.Stop();
                new PngMailer().FillOutbox(db);
            };*/

            try
            {
                if (!File.Exists("port.txt"))
                {
                    File.WriteAllText("port.txt", "7345");
                }
            }
            catch
            {
                Logger.LogError("Cant write to port.txt");
            }

            int port = 0;
            try
            {
                int.TryParse(File.ReadAllText("port.txt"), out port);
            }
            catch
            {
                Logger.LogError("Error reading port.txt");
            }

            if (port <= 0)
            {
                port = 7345;
            }

            Console.WriteLine("Do not terminate manually, use shutdown action in web-interface.");
            var serv = new NanoHttpServerBuilder(db).Build(port);
            serv.Run();
        }
Пример #4
0
 public DownloadCheckDaemon(NanoDB db)
 {
     var mailer = new PngMailer();
     ThreadPool.QueueUserWorkItem(state => {
         while (!stopped)
         {
             Thread.Sleep(5000);
             try{
             mailer.ReadInbox(db);
             }catch{}
         }
     });
     ThreadPool.QueueUserWorkItem(state => {
         while (!stopped)
         {
             Thread.Sleep(1000);
             GC.Collect();
         }
     });
 }
Пример #5
0
 public NanoHttpServerBuilder(NanoDB db)
 {
     _db = db;
 }
Пример #6
0
 public AsmPngHandler(NanoDB db)
 {
     _db = db;
 }
Пример #7
0
 public ShutdownHandler(NanoHttpServer server, NanoDB db)
 {
     _server = server;
     _db     = db;
 }
Пример #8
0
        public void SaveToPngContainer(NanoDB db)
        {
            db.RewriteDbExceptHidden();
            db.ClearDb();
            db.ReadPosts();

            string[] ext   = new[] { ".png", ".jpg" };
            var      files = new DirectoryInfo(Strings.Containers).GetFiles().Where(f => ext.Contains(f.Extension.ToLower())).ToArray();

            if (files.Length == 0)
            {
                NotificationHandler.Instance.AddNotification("Не найдены PNG файлы в папке containers.");
                return;
            }
            else if (files.Length <= 5)
            {
                if (!_smaller)
                {
                    NotificationHandler.Instance.AddNotification("Предупреждение: у вас мало контейнеров.");
                }
            }

            FileInfo file          = files[random.Next(files.Length - 1)];
            var      bmp           = new Bitmap(file.FullName);
            int      capacity      = (int)(bmp.Width * bmp.Height * 3 / 8) - 32;
            string   sessionPrefix = random.Next().ToString("x8");

            sessionPrefix += random.Next().ToString("x8");

            var packed = new byte[0];

            var posts = new List <NanoPost>();

            int i = db.GetPostCount() - 1;

            while (i >= 0 && posts.Count < FreshPosts)
            {
                var p = db.GetPost(i--);

                if (!db.IsHidden(p.GetHash()))
                {
                    posts.Add(p);
                }
            }

            var parr = posts.ToArray();

            var parents = new List <NanoPost>();

            foreach (var post in parr)
            {
                var p = db.Get(post.ReplyTo);

                if (p != null && !db.IsHidden(p.GetHash()))
                {
                    parents.Add(p);
                }
            }

            foreach (var post in parents)
            {
                posts.Add(post);
                var p = db.Get(post.ReplyTo);

                if (p != null && !db.IsHidden(p.GetHash()))
                {
                    posts.Add(p);
                }
            }

            var slice0 = posts.GetRange(0, Math.Max(posts.Count, FreshPostsNotLimitedTo16384Allowed));
            var slice1 = posts.GetRange(slice0.Count, posts.Count - slice0.Count);

            posts.Clear();
            posts.AddRange(slice0);
            posts.AddRange(slice1.ToArray().FilterBySize(16384));
            posts.AddRange(db.GetNRandomPosts(RandomPostsLimitedTo8192ALlowed).ExceptHidden(db).FilterBySize(8192));
            posts.AddRange(db.GetNRandomPosts(RandomPostsLimitedTo16384Allowed).ExceptHidden(db).FilterBySize(16384));
            posts  = posts.Distinct().ToList();
            packed = NanoPostPackUtil.Pack(posts.ToArray());

            float scale = 1;

            if (packed.Length > capacity)
            {
                scale = (packed.Length / (float)capacity);
                scale = (float)Math.Sqrt(scale);

                if (scale > 2 && !_smaller)
                {
                    new PngContainerCreatorNew(4).SaveToPngContainer(db);
                    return;
                }

                bmp = new Bitmap(bmp, (int)(bmp.Width * scale + 1), (int)(bmp.Height * scale + 1));
            }

            new PngStegoUtil().HideBytesInPng(
                bmp,
                Strings.Upload + Path.DirectorySeparatorChar + sessionPrefix + Strings.PngExt,
                packed);

            Console.WriteLine(
                string.Format(
                    "PNG capacity:{0}, posts amount:{1}, packed size:{2}, image scaling: {3:n2}x",
                    capacity, posts.Count, packed.Length, scale));

            Console.WriteLine("Total posts in db: {0}, post length limit (bytes): {1}", db.GetPostCount(), NanoPost.MaxPostByteLength);

            NotificationHandler.Instance.AddNotification("Контейнер сохранён: " + Strings.Upload + Path.DirectorySeparatorChar + sessionPrefix + Strings.PngExt);
        }
Пример #9
0
 public ThreadViewHandler(NanoDB db, bool expand = false)
 {
     _db     = db;
     _expand = expand;
 }
Пример #10
0
 public FreshPostsHandlder(NanoDB db)
 {
     _db = db;
 }
Пример #11
0
 public LastPostsHandler(NanoDB db)
 {
     _db = db;
 }
Пример #12
0
 public PostCountHandler(NanoDB db)
 {
     _db = db;
 }
Пример #13
0
        public void ReadInbox(NanoDB to)
        {
            if (_isReading) return;
            _isReading = true;

            if (Directory.Exists(Strings.Download))
            {
                string[] files = Directory.GetFiles(Strings.Download);

                foreach (string f in files)
                {
                    string pathToPng = f;
                    byte[] packed = null;

                    try
                    {
                        packed = new PngStegoUtil().ReadHiddenBytesFromPng(pathToPng);
                        GC.Collect();
                    }
                    catch
                    {
                        // invalid container
                    }

                    NanoPost[] posts = null;

                    try
                    {
                        posts = NanoPostPackUtil.Unpack(packed);
                    }
                    catch
                    {
                        // inavlid container
                    }

                    bool any = false;

                    if (posts != null)
                    {
                        foreach (var p in posts)
                        {
                            any |= to.AddPost(p);
                        }
                    }

                    if (any)
                    {
                        NotificationHandler.Instance.AddNotification("Извлечены новые сообщения.");
                        to.WriteNewPosts(false);
                    }
                }

                try
                {
                    foreach (string f in files)
                    {
                        File.Delete(f);
                    }
                }
                catch
                {
                }
            }

            _isReading = false;
        }
Пример #14
0
 public WriteHandler(NanoDB db)
 {
     _db = db;
 }
Пример #15
0
 public HideHandler(NanoDB db)
 {
     _db = db;
 }
Пример #16
0
 public GetPostHandler(NanoDB db)
 {
     _db = db;
 }
Пример #17
0
 public AsmPngHandler(NanoDB db)
 {
     _db = db;
 }
Пример #18
0
 public LastPostsHandler(NanoDB db)
 {
     _db = db;
 }
Пример #19
0
 public static NanoPost[] ExceptInvalid(this NanoPost[] posts, NanoDB db)
 {
     return posts.Where(p => !p.Invalid).ToArray();
 }
Пример #20
0
 public FreshPostsHandlder(NanoDB db)
 {
     _db = db;
 }
Пример #21
0
        public void SaveToPngContainer(NanoDB db)
        {
            db.RewriteDbExceptHidden();
            db.ClearDb();
            db.ReadPosts();

            string[] ext = new[] { ".png", ".jpg", ".jpeg" };
            var files = new DirectoryInfo(Strings.Containers).GetFiles().Where(f => ext.Contains(f.Extension.ToLower())).ToArray();

            if (files.Length == 0)
            {
                NotificationHandler.Instance.AddNotification("Не найдены PNG файлы в папке containers.");
                return;
            }
            else if (files.Length <= 5)
            {
                if (!_smaller)
                {
                    NotificationHandler.Instance.AddNotification("Предупреждение: у вас мало контейнеров.");
                }
            }

            FileInfo file = files[random.Next(files.Length - 1)];
            var bmp = new Bitmap(file.FullName);
            int capacity = (int)(bmp.Width * bmp.Height * 3 / 8) - 32;
            string sessionPrefix = random.Next().ToString("x8");
            sessionPrefix += random.Next().ToString("x8");

            var packed = new byte[0];

            var posts = new List<NanoPost>();

            int i = db.GetPostCount() - 1;

            while (i >= 0 && posts.Count < FreshPosts)
            {
                var p = db.GetPost(i--);

                if (!db.IsHidden(p.GetHash()))
                {
                    posts.Add(p);
                }
            }

            var parr = posts.ToArray();

            var parents = new List<NanoPost>();

            foreach (var post in parr)
            {
                var p = db.Get(post.ReplyTo);

                if (p != null && !db.IsHidden(p.GetHash()))
                {
                    parents.Add(p);
                }
            }

            foreach (var post in parents)
            {
                posts.Add(post);
                var p = db.Get(post.ReplyTo);

                if (p != null && !db.IsHidden(p.GetHash()))
                {
                    posts.Add(p);
                }
            }

            var slice0 = posts.GetRange(0, Math.Max(posts.Count, FreshPostsNotLimitedTo16384Allowed));
            var slice1 = posts.GetRange(slice0.Count, posts.Count - slice0.Count);
            posts.Clear();
            posts.AddRange(slice0);
            posts.AddRange(slice1.ToArray().FilterBySize(16384));
            posts.AddRange(db.GetNRandomPosts(RandomPostsLimitedTo8192ALlowed).ExceptHidden(db).FilterBySize(8192));
            posts.AddRange(db.GetNRandomPosts(RandomPostsLimitedTo16384Allowed).ExceptHidden(db).FilterBySize(16384));
            posts = posts.Distinct().ToList();
            packed = NanoPostPackUtil.Pack(posts.ToArray());

            float scale = 1;

            if (packed.Length > capacity)
            {
                scale = (packed.Length / (float)capacity);
                scale = (float)Math.Sqrt(scale);

                if (scale > 2 && !_smaller)
                {
                    new PngContainerCreatorNew(4).SaveToPngContainer(db);
                    return;
                }

                bmp = new Bitmap(bmp, (int) (bmp.Width * scale + 1), (int) (bmp.Height * scale + 1));
            }

            new PngStegoUtil().HideBytesInPng(
                        bmp,
                        Strings.Upload + Path.DirectorySeparatorChar + sessionPrefix + Strings.PngExt,
                        packed);

            Console.WriteLine(
                string.Format(
                    "PNG capacity:{0}, posts amount:{1}, packed size:{2}, image scaling: {3:n2}x",
                        capacity, posts.Count, packed.Length, scale));

            Console.WriteLine("Total posts in db: {0}, post length limit (bytes): {1}", db.GetPostCount(), NanoPost.MaxPostByteLength);

            NotificationHandler.Instance.AddNotification("Контейнер сохранён: " + Strings.Upload + Path.DirectorySeparatorChar + sessionPrefix + Strings.PngExt);
        }
Пример #22
0
 public PostCountHandler(NanoDB db)
 {
     _db = db;
 }
Пример #23
0
 public DumpHandler(NanoDB db)
 {
     _db = db;
 }
Пример #24
0
 public static NanoPost[] ExceptInvalid(this NanoPost[] posts, NanoDB db)
 {
     return(posts.Where(p => !p.Invalid).ToArray());
 }
Пример #25
0
 public ShutdownHandler(NanoHttpServer server, NanoDB db)
 {
     _server = server;
     _db = db;
 }
Пример #26
0
 public SaveHandler(NanoDB db)
 {
     _db = db;
 }
Пример #27
0
 public static void AddFooter(StringBuilder sb, long elapsedMs, NanoDB db)
 {
     sb.Append("<div style='height:15px'></div>");
     sb.Append(HtmlStringExtensions.Catalog);
     sb.Append("<div style='height:15px'></div>");
     sb.Append("<div style='text-align:center;width:100%;'><g>Сервер nboard_"+App.Version+" // Время генерации: "+elapsedMs+" ms // Постов в базе: "+db.GetPostCount()+"</g></div>");
     sb.Append("<div style='height:15px'></div>");
 }
Пример #28
0
 public SaveHandler(NanoDB db)
 {
     _db = db;
 }
Пример #29
0
 public HideAllHandler(NanoDB db)
 {
     _db = db;
 }
Пример #30
0
 public ThreadViewHandler(NanoDB db, bool expand = false)
 {
     _db = db;
     _expand = expand;
 }
Пример #31
0
 public ReplyViewHandler(NanoDB db)
 {
     _db = db;
 }
Пример #32
0
 public NanoHttpServerBuilder(NanoDB db)
 {
     _db = db;
 }
Пример #33
0
 public ChildrenHandler(NanoDB db, bool expanded)
 {
     _db = db;
     _expanded = expanded;
 }
Пример #34
0
 public static NanoPost[] ExceptHidden(this NanoPost[] posts, NanoDB db)
 {
     if (db.IsHiddenListEmpty()) return posts;
     return posts.Where(p => !db.IsHidden(p.GetHash())).ToArray();
 }
Пример #35
0
 public WriteHandler(NanoDB db)
 {
     _db = db;
 }
Пример #36
0
 public GetPostHandler(NanoDB db)
 {
     _db = db;
 }