Esempio n. 1
0
 public Server(ServerBooru Booru, Logger Logger, MailNotificator MailNotificator, ushort ThreadCount)
 {
     _ThreadPool = new SThreadPool(ThreadCount);
     _Booru      = Booru;
     _Logger     = Logger;
     _MN         = MailNotificator;
 }
Esempio n. 2
0
        public static BooruPostList DoSearch(string Pattern, ServerBooru Booru)
        {
            string[] parts = SplitString(Pattern);
            if (parts.Length < 1)
            {
                using (DataTable postTable = Booru.DB.ExecuteTable(SQLStatements.GetPosts))
                    return(BooruPostList.FromTable(postTable));
            }

            //Get all posts
            //Perform all the special patterns
            //return the post ids

            List <string> tagSearchQueries = new List <string>();
            var           specialPatterns  = new List <SpecialPattern>();

            //Extract all the needed information
            for (int i = 0; i < parts.Length; i++)
            {
                string part   = parts[i];
                bool   negate = ExtractNegate(ref part);

                if (!IsSpecialPattern(part))
                {
                    DataRow  tagRow = Booru.DB.ExecuteRow(SQLStatements.GetTagByTagString, part);
                    BooruTag tag    = BooruTag.FromRow(tagRow);
                    if (tag != null)
                    {
                        tagSearchQueries.Add(string.Format("id {0} (SELECT post FROM post_tags WHERE tag = {1})", negate ? "NOT IN" : "IN", tag.ID));
                    }
                }
                else
                {
                    SpecialPattern sPattern = ExtractSpecialPattern(part);
                    sPattern.Negate = negate;
                    specialPatterns.Add(sPattern);
                }
            }

            string tagSearchQuery = tagSearchQueries.Count > 0 ?
                                    "SELECT * FROM posts WHERE " + string.Join(" AND ", tagSearchQueries) + " ORDER BY creationdate DESC"
                : SQLStatements.GetPosts;

            using (DataTable postTable = Booru.DB.ExecuteTable(tagSearchQuery))
            {
                BooruPostList postList = new BooruPostList();
                foreach (BooruPost post in BooruPostList.FromTable(postTable))
                {
                    if (DoSpecialPatternChecks(specialPatterns, post))
                    {
                        postList.Add(post);
                    }
                }
                return(postList);
            }
        }
Esempio n. 3
0
        private static void MainStage2(Logger logger, string booruPath)
        {
            Console.Write("\x1b]0;SharpBooru Server\x07");
            // Console.Title = "SharpBooru Server";
            Console.TreatControlCAsInput = true;

            if (Environment.OSVersion.Platform != PlatformID.Unix)
            {
                throw new PlatformNotSupportedException("Only Linux is supported");
            }

            if (Type.GetType("Mono.Runtime") == null)
            {
                throw new PlatformNotSupportedException("Only Mono is supported");
            }

            logger.LogLine("Loading configuration...");
            Config config = new Config("config.xml");

            X509Certificate2 cert = null;

            if (config.CertificateNeeded)
            {
                logger.LogLine("Loading certificate...");
                cert = new X509Certificate2(config.Certificate);
            }

            logger.LogLine("Loading booru...");
            ServerBooru booru = new ServerBooru(booruPath);

            Server server = null;

            SocketListener[] sockListeners = null;
            MailNotificator  mn            = null;

            try
            {
                logger.LogLine("Binding sockets...");
                // if (File.Exists(unixSocketPath))
                // {
                //     logger.LogLine("Socket exists, removing it...");
                //     File.Delete(unixSocketPath);
                // }
                Socket[] sockets = new Socket[config.SocketConfigs.Count];
                sockListeners = new SocketListener[sockets.Length];
                for (byte i = 0; i < sockets.Length; i++)
                {
                    var sockConf = config.SocketConfigs[i];
                    sockets[i] = sockConf.Socket;
                    sockets[i].Bind(sockConf.EndPoint);
                    if (sockConf.UnixSocketPath != null)
                    {
                        SyscallEx.chmod(sockConf.UnixSocketPath, sockConf.UnixSocketPerms);
                        SyscallEx.chown(sockConf.UnixSocketPath, config.User);
                        _UnixSocketPaths.Add(sockConf.UnixSocketPath);
                    }
                }

                logger.LogLine("Changing UID to {0}...", config.User);
                SyscallEx.setuid(config.User);

                logger.LogLine("Starting server...");
                if (config.EnableMailNotificator)
                {
                    mn = new MailNotificator(logger,
                                             config.MailNotificatorServer,
                                             config.MailNotificatorPort,
                                             config.MailNotificatorUsername,
                                             config.MailNotificatorPassword,
                                             config.MailNotificatorSender,
                                             config.MailNotificatorReceiver);
                }
                server = new Server(booru, logger, mn, 2);
                for (int i = 0; i < sockets.Length; i++)
                {
                    bool useTLS = config.SocketConfigs[i].UseTLS;
                    sockListeners[i] = new SocketListener(sockets[i]);
                    sockListeners[i].SocketAccepted += socket =>
                    {
                        logger.LogLine("Client connected");
                        NetworkStream ns = new NetworkStream(socket, true);
                        server.AddConnectedClient(ns, useTLS ? cert : null);
                    };
                    sockListeners[i].Start();
                }

                // Ctrl+C is not supported due to heavy crashes of Mono
                using (UnixSignal sigtermSignal = new UnixSignal(Signum.SIGTERM))
                {
                    logger.LogLine("Startup finished, waiting for SIGTERM...");
                    sigtermSignal.WaitOne();
                }
            }
            catch (Exception ex) { logger.LogException("MainStage3", ex); }
            finally
            {
                logger.LogLine("Stopping server and closing sockets...");
                server.Dispose();
                foreach (var listener in sockListeners)
                {
                    listener.Dispose();
                }
            }

            logger.LogLine("Closing booru...");
            booru.Dispose();
        }