Example #1
0
        public void FillOutbox(NanoDB from)
        {
            if (!Directory.Exists(Strings.Upload))
            {
                Directory.CreateDirectory(Strings.Upload);
            }

            new PngContainerCreatorNew().SaveToPngContainer(from);
            from.WriteNewPosts(false);
        }
        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);
        }
        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();
        }
 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();
         }
     });
 }
 public NanoHttpServerBuilder(NanoDB db)
 {
     _db = db;
 }
Example #6
0
 public AsmPngHandler(NanoDB db)
 {
     _db = db;
 }
Example #7
0
 public ShutdownHandler(NanoHttpServer server, NanoDB db)
 {
     _server = server;
     _db     = db;
 }
Example #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);
        }
 public ThreadViewHandler(NanoDB db, bool expand = false)
 {
     _db     = db;
     _expand = expand;
 }
Example #10
0
 public FreshPostsHandlder(NanoDB db)
 {
     _db = db;
 }
Example #11
0
 public LastPostsHandler(NanoDB db)
 {
     _db = db;
 }
Example #12
0
 public PostCountHandler(NanoDB db)
 {
     _db = db;
 }
Example #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;
        }
Example #14
0
 public WriteHandler(NanoDB db)
 {
     _db = db;
 }
Example #15
0
 public HideHandler(NanoDB db)
 {
     _db = db;
 }
Example #16
0
 public GetPostHandler(NanoDB db)
 {
     _db = db;
 }
Example #17
0
 public AsmPngHandler(NanoDB db)
 {
     _db = db;
 }
Example #18
0
 public LastPostsHandler(NanoDB db)
 {
     _db = db;
 }
Example #19
0
 public static NanoPost[] ExceptInvalid(this NanoPost[] posts, NanoDB db)
 {
     return posts.Where(p => !p.Invalid).ToArray();
 }
Example #20
0
 public FreshPostsHandlder(NanoDB db)
 {
     _db = db;
 }
        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);
        }
Example #22
0
 public PostCountHandler(NanoDB db)
 {
     _db = db;
 }
Example #23
0
 public DumpHandler(NanoDB db)
 {
     _db = db;
 }
Example #24
0
 public static NanoPost[] ExceptInvalid(this NanoPost[] posts, NanoDB db)
 {
     return(posts.Where(p => !p.Invalid).ToArray());
 }
Example #25
0
 public ShutdownHandler(NanoHttpServer server, NanoDB db)
 {
     _server = server;
     _db = db;
 }
Example #26
0
 public SaveHandler(NanoDB db)
 {
     _db = db;
 }
Example #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>");
 }
Example #28
0
 public SaveHandler(NanoDB db)
 {
     _db = db;
 }
Example #29
0
 public HideAllHandler(NanoDB db)
 {
     _db = db;
 }
Example #30
0
 public ThreadViewHandler(NanoDB db, bool expand = false)
 {
     _db = db;
     _expand = expand;
 }
Example #31
0
 public ReplyViewHandler(NanoDB db)
 {
     _db = db;
 }
Example #32
0
 public NanoHttpServerBuilder(NanoDB db)
 {
     _db = db;
 }
Example #33
0
 public ChildrenHandler(NanoDB db, bool expanded)
 {
     _db = db;
     _expanded = expanded;
 }
Example #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();
 }
Example #35
0
 public WriteHandler(NanoDB db)
 {
     _db = db;
 }
Example #36
0
 public GetPostHandler(NanoDB db)
 {
     _db = db;
 }