void TableChangedProgressOnProgressChanged(object sender, string e)
 {
     //防止发生关于sqlite null connection or database handle的未捕获异常
     try
     {
         //refresh data
         log.Debug("PornItemTableViewWithPreview TableChangedProgressOnProgressChanged: " + e);
         if (e == "porn_items")
         {
             log.Debug("Refresh table");
             int oldCount = _pornItemDataTable.Rows.Count;
             _pornItemDataTable.Clear();
             _pornDB.FillPornItemsDataTable(ref _pornItemDataTable);
             int newCount = _pornItemDataTable.Rows.Count;
             //如果有变化,但是前后的行数相同,则表示可能没有读取到最新的数据
             //所以使用新的连接再次读取
             //该修改用于修正原来代码中,网页里面过滤了图片,主界面也提示过滤了若干图片
             //但是防护记录中却为空的错误
             if (oldCount == newCount)
             {
                 log.Info("Old count is equal to new count of p**n items, fill the p**n itmes with new connection!");
                 //PornDatabase pd = new PornDatabase();
                 using (PornDatabase pd = new PornDatabase())
                 {
                     pd.FillPornItemsDataTable(ref _pornItemDataTable);
                 }
             }
             //_sqliteDataAdapter.Fill(_pornItemDataTable);
             log.Debug("Count of p**n items: " + _pornItemDataTable.Rows.Count);
         }
     }
     catch (Exception exp)
     { log.Error(exp.ToString()); }
 }
Ejemplo n.º 2
0
        private void GetData()
        {
            try
            {
                //PornDB = new PornDatabase();
                PornDB = PornDatabase.Instance;
                porn_pics_binding_source.DataSource = PornDB.CreatePornPicsDataTable();

                // Resize the DataGridView columns to fit the newly loaded content.
                data_grid_view_porn_pics.Columns["id"].Visible = false;
                data_grid_view_porn_pics.Columns["created_at"].DefaultCellStyle.Format = "HH:mm:ss, MMMM dd, yyyy (dddd)";
                data_grid_view_porn_pics.Columns["url"].HeaderText        = "地址";
                data_grid_view_porn_pics.Columns["type"].HeaderText       = "类型";
                data_grid_view_porn_pics.Columns["created_at"].HeaderText = "时间";

                data_grid_view_porn_pics.Sort(data_grid_view_porn_pics.Columns["created_at"], ListSortDirection.Descending);

                blocked_pages_binding_source.DataSource = PornDB.CreateBlockedPagesDataTable();

                data_grid_view_pages.Columns["id"].Visible = false;
                data_grid_view_pages.Columns["created_at"].DefaultCellStyle.Format = "HH:mm:ss MMMM dd, yyyy (dddd)";
                data_grid_view_pages.Columns["url"].HeaderText        = "地址";
                data_grid_view_pages.Columns["created_at"].HeaderText = "时间";

                data_grid_view_pages.Sort(data_grid_view_pages.Columns["created_at"], ListSortDirection.Descending);

                data_grid_view_porn_pics.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                data_grid_view_pages.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
            }
            catch (SqlException e)
            {
                log.Error(e.ToString());
            }
        }
        static public void UpdateDatabase(Object state)
        {
            log.Info("Update Database");
            PornDatabase db = new PornDatabase();

            db.MaintainDatabase();
        }
Ejemplo n.º 4
0
        static void InitForBusinessLogic()
        {
            //如果数据库文件不存在,则建立数据库文件
            PornDatabase.CreateDatabase();
            PornClassifier.Init();
            FFMPEGWrapper.Init();
            WechatForm.Init();

            update_domain_list_timer = new System.Threading.Timer(PornDatabase.UpdateDatabase, null, new TimeSpan(0, 0, 5), new TimeSpan(4, 0, 0));
            delete_history_timer     = new System.Threading.Timer(PornDatabase.DeleteHistroy, null, new TimeSpan(0, 1, 0), System.Threading.Timeout.InfiniteTimeSpan);
        }
Ejemplo n.º 5
0
        static private void InitLogAndDirs()
        {
            //change workdir to the path of executable
#if !DEBUG
            var fi = new FileInfo(Application.ExecutablePath);

            Directory.SetCurrentDirectory(fi.DirectoryName);
#endif
#if DEBUG
            AllocConsole();
#endif
            //var versionInfo = FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly().Location);
            //var companyName = versionInfo.CompanyName;
            AppLocalDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "/masatek/trollwiz/";
            //upgrade user settings
            //create directory for work
            if (!Directory.Exists(Program.AppLocalDir))
            {
                Directory.CreateDirectory(Program.AppLocalDir);
            }
            //create directory for images
            if (!Directory.Exists(Program.AppLocalDir + Properties.Settings.Default.imagesDir))
            {
                Directory.CreateDirectory(Program.AppLocalDir + Properties.Settings.Default.imagesDir);
            }
            //create directory for updates
            if (!Directory.Exists(Program.AppLocalDir + Properties.Settings.Default.updateDir))
            {
                Directory.CreateDirectory(Program.AppLocalDir + Properties.Settings.Default.updateDir);
            }
            Utils.Log_Init();
            //#if !DEBUG
            if (!Properties.Settings.Default.upgraded)
            {
                log.Info("Upgrade settings!");
                Properties.Settings.Default.Upgrade();
                Properties.Settings.Default.upgraded = true;
                Properties.Settings.Default.Save();
                PornDatabase.DeleteOldDatabase();
            }
            if (Properties.Settings.Default.firstTime)
            {
                FirstTime = true;
                Properties.Settings.Default.firstTime = false;
                Properties.Settings.Default.Save();
            }
            if (Properties.Settings.Default.guid == "")
            {
                Properties.Settings.Default.guid = System.Guid.NewGuid().ToString();
                Properties.Settings.Default.Save();
            }
        }
        static public void Test()
        {
            PornDatabase  porndb  = new PornDatabase();
            SQLiteCommand ncmd    = new SQLiteCommand(kGetLastIDSelect, porndb.PornDBConnection);
            int           last_id = int.Parse(ncmd.ExecuteScalar().ToString());

            ncmd = new SQLiteCommand(String.Format(kUpdateLastIDInsert, 2), porndb.PornDBConnection);
            ncmd.ExecuteNonQuery();
            //DomainType t = porndb.GetDomainType("haoskys.com");
            //PornDatabase.DeleteHistroy(null);

            //porndb.MaintainDatabase();
            Task t = porndb.InsertBlackDomainDetecedAsync("kkk.com");
        }
        public PornItemTableViewWithPreview(bool viewProtectionLogs)
        {
            _viewProtectionLogs = viewProtectionLogs;
            //InitializeComponent();
            _pornDB = new PornDatabase();

            BackColor                    = Color.White;
            _dataGridView                = new PornDataGridView(!viewProtectionLogs);
            _previewPictureBox           = new PictureBox();
            _previewPictureBox.BackColor = Color.FromArgb(0xf3, 0xf3, 0xf3);
            _previewPictureBox.SizeMode  = PictureBoxSizeMode.Zoom;
            //Padding = new System.Windows.Forms.Padding(12, 0, 12, 0);
            Controls.Add(_dataGridView);
            Controls.Add(_previewPictureBox);
            Load += PornItemTabelViewWithPreviewOnLoad;
            _dataGridView.SelectionChanged += _dataGridViewOnSelectionChanged;
            SizeChanged += PornItemTableViewWithPreviewOnSizeChanged;

            //DataTable dta = _pornDB.CreatePornItemsDataTable();
            _pornItemDataTable = _pornDB.CreatePornItemsDataTable();

            //if (_viewProtectionLogs)
            //    _pornItemDataTable = dta;
            //else
            if (!_viewProtectionLogs)
            {
                _pornItemDataTable = _pornItemDataTable.Clone();
                DataColumn dc = new DataColumn("checked");
                dc.DataType     = typeof(bool);
                dc.DefaultValue = true;
                _pornItemDataTable.Columns.Add(dc);
            }

            _dataGridView.DataSource          = _pornItemBindingSource;
            _pornItemBindingSource.DataSource = _pornItemDataTable;
            //_pornItemBindingSource.DataSource = _pornItemDataTable;
            if (_viewProtectionLogs)
            {
                PornDatabase.TableChangedProgress.ProgressChanged += TableChangedProgressOnProgressChanged;
            }
        }
Ejemplo n.º 8
0
        public MainForm(string[] args)
        {
            Instance = this;
            Text     = Properties.Resources.ProductionName;
            //ForceToQuit = false;
            Icon = Properties.Resources.icon_main_icon;

            //必须在UI内部初始化,保证progress对象是由UI线程初始化的
            PornDatabase.Init();

            TargetProcessedProgress = new Progress <PornDatabase.PornItemType>();

            const bool bUseIPv6 = false;

            Server = new TcpServer(Properties.Settings.Default.bindPort, bUseIPv6);
            Server.Start(GreenProxy.CreateProxy);
            Server.InitListenFinished.WaitOne();
            if (Server.InitListenException != null)
            {
                throw Server.InitListenException;
            }

            _activeFileMonitor = new ActiveFileMonitor();

            m_aeroEnabled   = false;
            FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            //InitializeComponent();
            Size                         = new System.Drawing.Size(MainFormWidth, MainFormHeight);
            mainPanelControl             = new MainPanelControl();
            scanPanelControl             = new ScanPanelControl();
            _protectionPanelControl      = new ProtectionPanelControl();
            mainPanelControl.Size        = Size;
            scanPanelControl.Size        = Size;
            _protectionPanelControl.Size = Size;
            Controls.Add(_protectionPanelControl);
            Controls.Add(scanPanelControl);
            Controls.Add(mainPanelControl);

            _mainContextMenuTrip = new System.Windows.Forms.ContextMenuStrip();
            ToolStripItem openMainPanel  = _mainContextMenuTrip.Items.Add("打开主界面");
            ToolStripItem openProtection = _mainContextMenuTrip.Items.Add("防护中心");
            ToolStripItem quitItem       = _mainContextMenuTrip.Items.Add("退出");

            openMainPanel.Click  += openMainPanelOnClick;
            quitItem.Click       += quitItemOnClick;
            openProtection.Click += openProtectionOnClick;

            _mainNotifyIcon = new NotifyIcon();
            _mainNotifyIcon.ContextMenuStrip = _mainContextMenuTrip;
            _mainNotifyIcon.Icon             = Properties.Resources.icon_main_icon;
            _mainNotifyIcon.Visible          = true;
            _mainNotifyIcon.DoubleClick     += _mainNotifyIconOnDoubleClick;

            //最小化的方法会导致防护窗口中日志目录的表格尺寸有问题
            if (args.Contains("-notvisible"))
            {
                ShowInTaskbar = false;
                _needToBeHide = true;
                Shown        += MainFormOnShown;
                StartPosition = FormStartPosition.Manual;
                Location      = new Point(-10000, -10000);
                ////Hide();
                ////WindowState = FormWindowState.Minimized;
            }
            //mainPanelControl.ScanEvent += mainPanelOnScanEvent;
            Load        += MainFormOnLoad;
            FormClosed  += MainFormOnFormClosed;
            FormClosing += MainFormOnFormClosing;
            Disposed    += MainFormOnDisposed;

            //set hotkey as ctrl+alt+backspace
            //Boolean success = FormMain.RegisterHotKey(this.Handle, this.GetType().GetHashCode(), MOD_CTRL | MOD_ALT, 0x08);//Set hotkey as 'b'
            //set the owner to avoid the main form in atl-table window
            //Form form1 = new Form();
            //form1.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            //form1.ShowInTaskbar = false;
            //Owner = form1;
        }
        void BackgroundWorker(CancellationToken ct, IProgress <PornActiveFile> progress)
        {
            Dictionary <string, PornDatabase.PornItemType> md5Set = new Dictionary <string, PornDatabase.PornItemType>();
            //Dictionary<byte[], int> md5Set = new Dictionary<byte[], int>();
            Dictionary <string, int> fileSet = new Dictionary <string, int>();

            while (true)
            {
                ct.ThrowIfCancellationRequested();
                if (_taskQueue.IsEmpty)
                {
                    _newFileEvent.WaitOne(1000);
                }
                string nfile;
                while (_taskQueue.TryDequeue(out nfile))
                {
                    fileSet[nfile] = 0;
                }
                List <string> tobeDeleted = new List <string>();
                foreach (string key in fileSet.Keys.ToList())
                {
                    try
                    {
                        log.Info("Process: " + key);
                        FileAttributes attr = File.GetAttributes(key);
                        if (!attr.HasFlag(FileAttributes.Directory))
                        {
                            FileInfo finfo = new FileInfo(key);
                            if (!_fileProcessed.ContainsKey(key) || _fileProcessed[key] != finfo.Length)
                            {
                                //string shash = GetMd5Hash(_md5hash, File.ReadAllBytes(key));
                                //log.Info("\tHash: " + shash + " Time: " + fileSet[key]);
                                //PornClassifier.ImageType itype;
                                PornDatabase.PornItemType itype = PornDatabase.PornItemType.Undefined;
                                //if (md5Set.ContainsKey(shash))
                                //{
                                //    itype = md5Set[shash];
                                //}
                                //else
                                {
                                    Exception classifyException;
                                    if (IsFileExtWith(key, ImageExts))
                                    {
                                        var t = PornClassifier.Instance.Classify(key, out classifyException);
                                        if (classifyException != null)
                                        {
                                            throw classifyException;
                                        }
                                        else if (t == PornClassifier.ImageType.P**n)
                                        {
                                            itype = PornDatabase.PornItemType.LocalImage;
                                            //md5Set[shash] = PornDatabase.PornItemType.LocalImage;
                                        }
                                    }
                                    else if (IsFileExtWith(key, VideoExts))
                                    {
                                        var t = PornClassifier.Instance.ClassifyVideoFile(key, out classifyException);
                                        if (classifyException != null)
                                        {
                                            throw classifyException;
                                        }
                                        else if (t)
                                        {
                                            itype = PornDatabase.PornItemType.LocalVideo;
                                            //md5Set[shash] = PornDatabase.PornItemType.LocalVideo;
                                        }
                                    }
                                }
                                IProgress <PornDatabase.PornItemType> ip = MainForm.Instance.TargetProcessedProgress as IProgress <PornDatabase.PornItemType>;
                                if (itype != PornDatabase.PornItemType.Undefined)
                                {
                                    log.Info("Detect Active File: " + key);
                                    PornDatabase pdb = new PornDatabase();
                                    pdb.InsertPornFile(key, itype);
                                    ip.Report(itype);
                                }
                                else
                                {
                                    ip.Report(PornDatabase.PornItemType.Undefined);
                                }

                                _fileProcessed[key] = (int)finfo.Length;
                            }
                            else
                            {
                                log.Info("\tIgnore same file: " + key);
                            }
                        }
                        tobeDeleted.Add(key);
                    }
                    //catch (System.IO.IOException ioex)
                    catch (Exception ex)
                    {
                        int value = fileSet[key];
                        if (value + 1 > 10)
                        {
                            tobeDeleted.Add(key);
                            log.Info("Delete: " + key);
                        }
                        else
                        {
                            fileSet[key] = value + 1;
                        }
                        log.Info(ex.ToString());
                    }
                    //{
                    //    log.Error(ex.ToString());
                    //}
                }
                foreach (string del in tobeDeleted)
                {
                    fileSet.Remove(del);
                }
            }
        }