示例#1
0
 public Task GlobalAsync()
 {
     CodeSite.EnterMethod(this, "GlobalAsync");
     try
     {
         CheckTask();
         cts  = new CancellationTokenSource();
         Task = Task.Run(() =>
         {
             try
             {
                 var save = SaveAsync();
                 Thread.CurrentThread.Name = "GlobalAsync";
                 IPAddress ip = IPAddress.Parse(LastProgress.Ping);
                 CodeSite.Send("LastIP", ip.ToString());
                 IPNetwork net = IPNetwork.Parse(ip.ToString(), 10);
                 uint count    = (uint)net.Total / 256;
                 for (uint i = net.Network.ToUInt32() >> 8; i <= uint.MaxValue >> 8; i++)
                 {
                     if (cts.IsCancellationRequested)
                     {
                         break;
                     }
                     uint uip = i << 8;
                     if (!IPNetworkSet.IPv4Reserved.Contains(uip) && !IPNetworkSet.IPv4Assigned.Contains(uip) ^ Properties.Settings.Default.IPv4Assigned)
                     {
                         for (uint j = 0; j < 256; j++)
                         {
                             listIP.Add(uip + j);
                         }
                     }
                     if (i % count == count - 1)//每组
                     {
                         if (listIP.Count > 0)
                         {
                             //CodeSite.Send("listIP.Count", listIP.Count);
                             LastProgress.Ping = listIP.First().ToIPAddress().ToString();
                             CodeSite.Send("StartIP", LastProgress.Ping);
                             progressFormat = string.Format("{0}-{1},{{0,8}}/{2},新增{{1,8}}", listIP.First().ToIPAddress(), listIP.Last().ToIPAddress(), listIP.Count);
                             ExceptDB();
                             CheckList();
                             listIP.Clear();
                         }
                     }
                 }
                 bcIPv4SSL.CompleteAdding();
                 save.Wait();
             }
             catch (Exception ex)
             {
                 ex.SendCodeSite("GlobalAsync");
             }
         }, cts.Token);
         return(Task);
     }
     finally
     {
         CodeSite.ExitMethod(this, "GlobalAsync");
     }
 }
示例#2
0
        static bool SaveFile(string filename, IEnumerable <string> list)
        {
            FileInfo fi = new FileInfo(filename);

            if (!fi.Directory.Exists)
            {
                CodeSite.Send("目录不存在", fi.DirectoryName);
                return(false);
            }
            if (fi.Exists)
            {
                var old = File.ReadAllLines(fi.FullName).AsEnumerable();
                if (list.SequenceEqual(old))
                {
                    CodeSite.SendNote("内容无变化{0}", fi.FullName);
                    return(false);
                }
                else
                {
                    CodeSite.SendCollection("新增", list.Except(old));
                }
            }
            File.WriteAllLines(fi.FullName, list);
            CodeSite.SendCollection(fi.FullName, list);
            return(true);
        }
示例#3
0
        public static string GetCookies(HttpWebResponse httpWebResponse)
        {
            var script1      = "";
            var script2      = "";
            var chromeDriver = new ChromeDriver();

            try
            {
                using (var responseStream = httpWebResponse.GetResponseStream())
                    using (var streamReader = new StreamReader(responseStream ?? throw new InvalidOperationException(), Encoding.UTF8))
                        script1 = streamReader.ReadToEnd();
                chromeDriver.Navigate().GoToUrl(httpWebResponse.ResponseUri);
                var run = script1.Substring("<script>", "</script>").Replace("{eval(", "{return (");
                run     = $"function script2(){{{run}}}; return script2();";
                script2 = chromeDriver.ExecuteScript(run).ToString();
                var del = script2.Substring("=function(){", "document.cookie=");
                run = script2.Replace(del + "document.cookie=", "return ");
                var name = script2.Substring("var ", "=");
                run = $"{run}; return {name}();";
                var cookie = chromeDriver.ExecuteScript(run).ToString();
                return(cookie);
            }
            catch (Exception ex)
            {
                ex.SendCodeSite();
                CodeSite.Send("script1", script1);
                CodeSite.Send("script2", script2);
                return("");
            }
            finally
            {
                chromeDriver.Quit();
            }
        }
示例#4
0
        void ThreadCheckList()
        {
            ConcurrentBag <uint> csIP = new ConcurrentBag <uint>(IPList);

            Thread[] threads = new Thread[2000];
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(() =>
                {
                    uint uip;
                    while (csIP.TryTake(out uip))
                    {
                        if (cts.IsCancellationRequested)
                        {
                            return;
                        }
                        Interlocked.Increment(ref finishCount);
                        if (WebCheck(uip))
                        {
                            Interlocked.Increment(ref saveCount);
                        }
                    }
                    if (!csIP.IsEmpty)
                    {
                        CodeSite.Send("csIP.IsEmpty", csIP.IsEmpty);
                    }
                });
                threads[i].Priority = ThreadPriority.Lowest;
                threads[i].Start();
            }
            foreach (var item in threads)
            {
                item.Join();
            }
        }
示例#5
0
        /// <summary>
        /// 输出异常的CodeSite日志
        /// </summary>
        /// <param name="value">要处理的异常</param>
        /// <param name="msg">日志的标识</param>
        /// <returns>始终返回 <see langword="false"/></returns>
        public static bool SendCodeSite(this Exception value, [CallerMemberName] string msg = null)
        {
            string text = value.ToString();

            if (value is WebException we && we.Response is HttpWebResponse hwr)
            {
                if (hwr.ContentType.StartsWith("text/"))
                {
                    Encoding encoding = Encoding.Default;
                    try { encoding = Encoding.GetEncoding(hwr.CharacterSet); }
                    catch (Exception ex) { CodeSite.SendException(hwr.CharacterSet, ex); }
                    try
                    {
                        StreamReader sr = new StreamReader(hwr.GetResponseStream(), encoding);
                        text += Environment.NewLine + sr.ReadToEnd();
                        sr.Close();
                    }
                    catch (Exception ex)
                    {
                        text += Environment.NewLine + ex.ToString();
                    }
                }
            }
            CodeSite.Send(CodeSiteMsgType.Exception, msg ?? value.TargetSite?.ToString(), text);
            return(false);
        }
示例#6
0
        private void Test()
        {
            CodeSite.EnterMethod(this, "Test");
            ScrapingBrowser scrapingBrowser = new ScrapingBrowser();

            scrapingBrowser.IgnoreCookies = true;
re:
            try
            {
                var s = scrapingBrowser.NavigateTo(new Uri("https://www.yidaiyilu.gov.cn/info/iList.jsp?cat_id=10009"), HttpVerb.Get, "");
                CodeSite.Send("s", s);
            }
            catch (Exception ex)
            {
                if (ex is WebException we && we.Response is HttpWebResponse hwr)
                {
                    if ((int)hwr.StatusCode == 521)
                    {
                        CodeSite.SendReminder("破解爬虫防护");
                        scrapingBrowser.SetCookies(new Uri("https://www.yidaiyilu.gov.cn/"), Jsl.GetCookies(hwr));
                        goto re;
                    }
                }
            }
            finally
            {
                CodeSite.ExitMethod(this, "Test");
            }
        }
示例#7
0
        void ExceptDB()
        {
            using (IPv4DataContext db = new IPv4DataContext())
            {
                var listDB = (from item in db.IPv4SSL
                              where item.Address >= IPSet.Min && item.Address <= IPSet.Max
                              select item.Address).ToList().Select(p => (uint)p);
                finishCount = IPSet.Count;
                IPSet.ExceptWith(listDB);
                finishCount -= IPSet.Count;
            }
            if (finishCount > 0)
            {
                CodeSite.Send("SkipCount", finishCount);
            }
            IPList.Clear();
            IPList.AddRange(IPSet);
            Random random = new Random();

            for (int i = 0; i < IPList.Count; i++)
            {
                int  j = random.Next(i, IPList.Count);
                uint t = IPList[i];
                IPList[i] = IPList[j];
                IPList[j] = t;
            }
        }
示例#8
0
 public static void SendCodeSite(this Exception value, string msg = null)
 {
     if (msg == null)
     {
         msg = value.TargetSite.Name;
     }
     CodeSite.Send(CodeSiteMsgType.Exception, msg, value.ToString());
 }
示例#9
0
        private void CheckList()
        {
            progressCount = 0;
            int   newCount = 0, lastCount = progressCount, tempCount;
            Timer timer;

            if (ProgressString != null)
            {
                timer = new Timer(_ =>
                {
                    ProgressString.Report(string.Format(progressFormat, progressCount, newCount));
                    tempCount = progressCount;
                    //CodeSite.Send("progressCount", tempCount - lastCount);
                    lastCount = tempCount;
                }, null, 1000, 1000);
            }
            else
            {
                timer = null;
            }

            try
            {
                ParallelOptions po = new ParallelOptions()
                {
                    CancellationToken = cts.Token, MaxDegreeOfParallelism = MaxDegreeOfParallelism
                };
                //CodeSite.Send("po.MaxDegreeOfParallelism", po.MaxDegreeOfParallelism);
                Parallel.ForEach(listIP, po, (uip) =>
                {
                    Interlocked.Increment(ref progressCount);
                    IPv4SSL ip = new IPv4SSL(uip);
                    WebCheck(ip);
                    if (ip.IsSSL)
                    {
                        Interlocked.Increment(ref newCount);
                        bcIPv4SSL.Add(ip);
                    }
                });
            }
            catch (OperationCanceledException ex)
            {
                ex.SendCodeSite("OperationCanceledException");
            }
            forceSave = true;
            while (forceSave)
            {
                //CodeSite.SendReminder("等待数据保存完毕");
                Thread.Sleep(1000);
            }
            CodeSite.Send("新增数据", newCount);

            if (timer != null)
            {
                timer.Dispose();
            }
        }
示例#10
0
        private void buttonIPv4DB_Click(object sender, EventArgs e)
        {
            CodeSite.SendCollection("0.255.255.255", IPv4Location.Find(IPAddress.Parse("0.255.255.255")));
            Random r = new Random();

            for (int i = 0; i < 10; i++)
            {
                int       ip  = r.Next(int.MinValue, int.MaxValue);
                IPAddress ipa = ((uint)ip).ToIPAddress();
                CodeSite.SendCollection(ipa.ToString(), IPv4Location.Find(ipa));
            }
        }
示例#11
0
        private void CheckList()
        {
            int   newCount = 0, lastCount = progressCount, tempCount;
            Timer timer;

            if (ProgressString != null)
            {
                timer = new Timer(_ =>
                {
                    ProgressString.Report(string.Format(progressFormat, progressCount, newCount));
                    tempCount = progressCount;
                    //CodeSite.Send("progressCount", tempCount - lastCount);
                    lastCount = tempCount;
                }, null, 1000, 1000);
            }
            else
            {
                timer = null;
            }

            try
            {
                ParallelOptions po = new ParallelOptions()
                {
                    CancellationToken = cts.Token, MaxDegreeOfParallelism = MaxDegreeOfParallelism
                };
                //CodeSite.Send("po.MaxDegreeOfParallelism", po.MaxDegreeOfParallelism);
                Parallel.ForEach(listIP, po, (uip) =>
                {
                    Interlocked.Increment(ref progressCount);
                    int time;
                    if ((time = TryPing(uip, Timeout)) != -1)
                    {
                        Interlocked.Increment(ref newCount);
                        bcIPv4SSL.Add(new IPv4SSL(uip)
                        {
                            RoundtripTime = time
                        });
                    }
                });
            }
            catch (OperationCanceledException ex)
            {
                ex.SendCodeSite("OperationCanceledException");
            }
            CodeSite.Send("新增数据", newCount);

            if (timer != null)
            {
                timer.Dispose();
            }
        }
示例#12
0
 void SaveThreadStop()
 {
     CodeSite.EnterMethod(this, "SaveThreadStop");
     try
     {
         ctsSave.Cancel();
         taskSave.Wait();
     }
     finally
     {
         CodeSite.ExitMethod(this, "SaveThreadStop");
     }
 }
示例#13
0
 public Task IPv4SSLAsync()
 {
     cts  = new CancellationTokenSource();
     task = new Task(() =>
     {
         try
         {
             SaveThreadStart();
             Thread.CurrentThread.Name = "Async";
             IPAddress ip = IPAddress.Parse(Properties.Settings.Default.LastIP);
             CodeSite.Send("LastIP", ip.ToString());
             IPNetwork net = IPNetwork.Parse(ip.ToString(), 12);
             uint count    = (uint)net.Total / 256;
             for (uint i = net.Network.ToUInt32() >> 8; i <= uint.MaxValue >> 8; i++)
             {
                 if (cts.IsCancellationRequested)
                 {
                     break;
                 }
                 uint uip = i << 8;
                 if (!IPNetworkSet.IPv4Reserved.Contains(uip) && !IPNetworkSet.IPv4Assigned.Contains(uip) ^ Properties.Settings.Default.IPv4Assigned)
                 {
                     for (uint j = 0; j < 256; j++)
                     {
                         IPSet.Add(uip + j);
                     }
                 }
                 if (i % count == count - 1)//每组
                 {
                     if (IPSet.Count > 0)
                     {
                         CodeSite.Send("MinIP", IPSet.Min.ToIPAddress().ToString());
                         progressFormat = string.Format("{0}-{1},{{0,8}}/{2},New{{1,8}}", IPSet.Min.ToIPAddress(), IPSet.Max.ToIPAddress(), IPSet.Count);
                         Properties.Settings.Default.LastIP = IPSet.Min.ToIPAddress().ToString();
                         Properties.Settings.Default.Save();
                         ExceptDB();
                         CheckList();
                         IPSet.Clear();
                     }
                 }
             }
             SaveThreadStop();
         }
         catch (Exception ex)
         {
             ex.SendCodeSite("IPv4SSLAsync");
         }
     }, cts.Token);
     task.Start();
     return(task);
 }
示例#14
0
        public static void TcpCheck(IPv4SSL ip)
        {
            IPAddress value     = IPAddress.Parse(ip.IP);
            TcpClient tcpClient = new TcpClient {
                ReceiveTimeout = 1000, SendTimeout = 1000
            };

            try
            {
                using (Ping ping = new Ping())
                {
                    PingReply pingReply = ping.Send(value, 1000);
                    if (pingReply.Status == IPStatus.Success)
                    {
                        tcpClient.Connect(value, 443);
                        SslStream sslStream = new SslStream(tcpClient.GetStream(), false,
                                                            (sender, certificate, chain, sslPolicyErrors) =>
                                                            Encoding.UTF8.GetString(certificate.GetRawCertData()).IndexOf("google") != -1);
                        sslStream.AuthenticateAsClient("");
                        StreamReader streamReader = new StreamReader(sslStream);
                        StreamWriter streamWriter = new StreamWriter(sslStream);
                        streamWriter.Write("HEAD / HTTP/1.1\r\nHost:www.google.com\r\nConnection:Close\r\n\r\n");
                        streamWriter.Flush();
                        string text = streamReader.ReadToEnd();
                        CodeSite.Send("text", text);
                        tcpClient.Close();
                        object[] array = new object[8];
                        if (text.IndexOf("Server: gvs 1.0") != -1)
                        {
                            array[2] = "GVS";
                        }
                        else if (text.IndexOf("Server: gws") != -1)
                        {
                            array[2] = "gws";
                        }

                        string text2 = sslStream.RemoteCertificate.Subject.Split(new char[] { ',' })[0].Substring(3);
                        array[0] = value.ToString();
                        array[1] = "_OK " + pingReply.RoundtripTime.ToString().PadLeft(4, '0');
                        array[3] = text2;
                        array[4] = "001";
                        array[5] = "";
                    }
                }
            }
            catch (Exception ex)
            {
                ex.SendCodeSite("TcpCheck");
            }
            tcpClient.Close();
        }
示例#15
0
        bool WebCheck(uint value)
        {
            IPv4SSL ip = new IPv4SSL(value);

            WebCheck(ip);
            if (ip.RoundtripTime != -1)
            {
                if (Properties.Settings.Default.IPv4Assigned)
                {
                    CodeSite.Send("IP", ip.IP);
                }
                IPStack.Push(ip);
            }
            return(ip.RoundtripTime != -1);
        }
示例#16
0
 private void buttonServernull_Click(object sender, EventArgs e)
 {
     using (IPv4DataContext db = new IPv4DataContext())
     {
         //var q = from g in db.Google
         //        join i in db.IPv4SSL on g.Address equals i.Address
         //        where i.Server == null
         //        select i.Address;
         foreach (var ip in db.IPv4SSL.Join(db.Google, i => i.Address, g => g.Address, (i, g) => i).Where(f => f.Server == null).OrderBy(f => f.Address))
         {
             CodeSite.Send("IP", ip.IP);
             Search.WebCheck(ip);
             Search.TcpCheck(ip);
             db.SubmitChanges();
         }
     }
 }
示例#17
0
 void ExceptDB()
 {
     using (IPv4DataContext db = new IPv4DataContext())
     {
         var listDB = (from item in db.IPv4SSL
                       where item.Address >= listIP.First() && item.Address <= listIP.Last()
                       select item.Address).ToList().Select(f => (uint)f);
         progressCount  = listIP.Count;
         listIP         = listIP.Except(listDB).ToList();
         progressCount -= listIP.Count;
     }
     if (progressCount > 0)
     {
         CodeSite.Send("SkipCount", progressCount);
     }
     Shuffle();
 }
示例#18
0
 static bool SaveDB(IEnumerable <IPv4SSL> ipa)
 {
     using (IPv4DataContext db = new IPv4DataContext())
     {
         try
         {
             db.IPv4SSL.InsertAllOnSubmit(ipa);
             db.SubmitChanges();
             return(true);
         }
         catch (Exception ex)
         {
             ex.SendCodeSite("SaveDB");
             CodeSite.Send("ipa.First().IP", ipa.First().IP);
         }
     }
     return(false);
 }
示例#19
0
 protected override bool SaveDB(IEnumerable <IPv4SSL> ipa)
 {
     using (IPv4DataContext db = new IPv4DataContext())
     {
         try
         {
             db.IPv4SSL.InsertAllOnSubmit(ipa);
             db.SubmitChanges();
             return(true);
         }
         catch (Exception ex)
         {
             ex.SendCodeSite("SaveDB");
             CodeSite.Send("Count = {0}, FirstIP = {1}", ipa.Count(), ipa.First().IP);
         }
     }
     return(false);
 }
示例#20
0
 public bool Cancel()
 {
     if (cts != null && !cts.IsCancellationRequested)
     {
         CodeSite.EnterMethod(this, "Cancel");
         try
         {
             cts.Cancel();
             CodeSite.SendNote("cts.Cancel();");
             task.Wait();
             return(true);
         }
         finally
         {
             CodeSite.ExitMethod(this, "Cancel");
         }
     }
     return(false);
 }
示例#21
0
 void SaveThreadStart()
 {
     CodeSite.EnterMethod(this, "SaveThreadStart");
     try
     {
         ctsSave  = new CancellationTokenSource();
         taskSave = Task.Factory.StartNew(() =>
         {
             IPv4SSL[] ipa = new IPv4SSL[100];
             int c;
             while (!ctsSave.IsCancellationRequested || !IPStack.IsEmpty)
             {
                 if ((c = IPStack.TryPopRange(ipa)) > 0)
                 {
                     var ip = ipa.Take(c);
                     if (ProgressIP != null)
                     {
                         foreach (var item in ip)
                         {
                             if (item.Isgws)
                             {
                                 ProgressIP.Report(item);
                             }
                         }
                     }
                     while (!SaveDB(ip) && !ctsSave.IsCancellationRequested)
                     {
                         Thread.Sleep(1000);
                     }
                 }
                 if (IPStack.IsEmpty)
                 {
                     Thread.Sleep(1000);
                 }
             }
         }, ctsSave.Token);
     }
     finally
     {
         CodeSite.ExitMethod(this, "SaveThreadStart");
     }
 }
示例#22
0
 public Task GlobalAsync()
 {
     CodeSite.EnterMethod(this, "GlobalAsync");
     try
     {
         CheckTask();
         cts  = new CancellationTokenSource();
         Task = Task.Run(() =>
         {
             try
             {
                 var save = SaveAsync();
                 Thread.CurrentThread.Name = "GlobalAsync";
                 IPAddress ip = IPAddress.Parse(LastProgress.Ssl);
                 CodeSite.Send("LastIP", ip.ToString());
                 while (SelectDB(ip) && !cts.IsCancellationRequested)
                 {
                     listIP           = listIPv4SSL.Select(f => (uint)f.Address).ToList();
                     LastProgress.Ssl = ip.ToString();
                     CodeSite.Send("StartIP", string.Format("{0} - {1}", LastProgress.Ssl, listIP.Count));
                     progressFormat = string.Format("{0}-{1},{{0,8}}/{2},新增{{1,8}}", listIP.First().ToIPAddress(), listIP.Last().ToIPAddress(), listIP.Count);
                     ip             = (listIP.Last() + 1).ToIPAddress();
                     Shuffle();
                     CheckList();
                     listIP.Clear();
                 }
                 bcIPv4SSL.CompleteAdding();
                 save.Wait();
             }
             catch (Exception ex)
             {
                 ex.SendCodeSite("GlobalAsync");
             }
         }, cts.Token);
         return(Task);
     }
     finally
     {
         CodeSite.ExitMethod(this, "GlobalAsync");
     }
 }
示例#23
0
        private void buttonGoogleIPHunter_Click(object sender, EventArgs e)
        {
            var files = Directory.GetFiles(@"G:\DxgWork\GitHub\GoogleIPHunter\trunk", "*.txt", SearchOption.AllDirectories);

            using (IPv4DataContext db = new IPv4DataContext())
            {
                foreach (var file in files)
                {
                    foreach (var s in File.ReadAllLines(file))
                    {
                        try
                        {
                            IPNetwork network = IPNetwork.Parse(s);
                            if (network.ToString() == s)
                            {
                                uint address = network.Network.ToUInt32();
                                if (!db.GoogleIPHunter.Any(f => f.Address == (long)address))
                                {
                                    db.GoogleIPHunter.InsertOnSubmit(new GoogleIPHunter()
                                    {
                                        Address = address, IPBlock = s
                                    });
                                    db.SubmitChanges();
                                    CodeSite.SendNote("新增 = {0}", s);
                                }
                            }
                            else
                            {
                                CodeSite.SendError(s);
                            }
                        }
                        catch
                        {
                            //CodeSite.SendException(s, ex);
                        }
                    }
                }
            }
        }
示例#24
0
        private void buttonGoogleIP_Click(object sender, EventArgs e)
        {
            var sa = File.ReadAllLines("google ip duan.txt");

            using (IPv4DataContext db = new IPv4DataContext())
            {
                var list = db.GoogleIPDuan.ToList().Select(f => (uint)f.Address);
                foreach (var s in sa)
                {
                    try
                    {
                        IPNetwork network = IPNetwork.Parse(s);
                        if (network.ToString() == s)
                        {
                            uint address = network.Network.ToUInt32();
                            if (!list.Contains(address))
                            {
                                db.GoogleIPDuan.InsertOnSubmit(new GoogleIPDuan()
                                {
                                    Address = address, IPBlock = s
                                });
                                CodeSite.SendNote("新增 = {0}", s);
                            }
                        }
                        else
                        {
                            CodeSite.SendError(s);
                        }
                    }
                    catch (Exception ex)
                    {
                        CodeSite.SendException(s, ex);
                    }
                }
                db.SubmitChanges();
            }
        }
示例#25
0
        public void Add(string networks)
        {
            var ips = networks.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).AsEnumerable();

            foreach (var ip in ips)
            {
                try
                {
                    IPNetwork network = IPNetwork.Parse(ip);
                    if (ip.StartsWith(network.ToString()))
                    {
                        Add(network);
                    }
                    else
                    {
                        CodeSite.Send(ip, network);
                    }
                }
                catch (Exception ex)
                {
                    CodeSite.SendException(ip, ex);
                }
            }
        }
示例#26
0
 protected override bool SaveDB(IEnumerable <IPv4SSL> ipa)
 {
     //CodeSite.Send("ipa.Count()", ipa.Count());
     using (IPv4DataContext db = new IPv4DataContext())
     {
         try
         {
             foreach (var ip in ipa)
             {
                 if (ip.Isgws && ProgressIPv4SSL != null)
                 {
                     ProgressIPv4SSL.Report(ip);
                 }
                 int i = listIPv4SSL.BinarySearch(ip);
                 if (i > -1)
                 {
                     //CodeSite.Send(listIPv4SSL[i].IP, listIPv4SSL[i]);
                     //CodeSite.Send(ip.IP, ip);
                     db.IPv4SSL.Attach(ip, listIPv4SSL[i]);
                 }
                 else
                 {
                     CodeSite.SendError(string.Format("{0}-{1}", i, ip.IP));
                 }
             }
             db.SubmitChanges();
             return(true);
         }
         catch (Exception ex)
         {
             ex.SendCodeSite("SaveDB");
             CodeSite.Send("ipa.First().IP", ipa.First().IP);
         }
     }
     return(false);
 }
示例#27
0
        public static bool SetMinThreads(int threadCount)
        {
            int workerMin, workerMax, workerA, completionPort;

            ThreadPool.GetAvailableThreads(out workerA, out completionPort);
            ThreadPool.GetMaxThreads(out workerMax, out completionPort);
            ThreadPool.GetMinThreads(out workerMin, out completionPort);
            //CodeSite.Send("workerA", workerA);
            //CodeSite.Send("workerMax", workerMax);
            //CodeSite.Send("workerMin", workerMin);
            int worker = workerMax - workerA + threadCount + 200;

            //CodeSite.Send("worker", worker);
            if (worker > workerMin)
            {
                worker = Math.Min(worker, workerMax);
                CodeSite.Send("worker", worker);
                return(ThreadPool.SetMinThreads(worker, completionPort));
            }
            else
            {
                return(true);
            }
        }
示例#28
0
        /// <summary>
        /// Gets tags from the raw MP3 files and generates a List of MP3Node data structures with
        /// necessary info to construct our MP3 folder structure
        ///
        /// Not sure whether I understand why we are moving this into a separate structure
        /// </summary>
        /// <param name="mp3FilePaths">List of string paths to the MP3 files</param>
        /// <returns>List of MP3 representations</returns>
        public IList <Mp3Node> RetrieveTagsFromMp3Files(IList <string> mp3FilePaths)
        {
            var mp3FileList = new List <Mp3Node>();
            var count       = 0;

            foreach (var currentMp3FilePath in mp3FilePaths)
            {
                // filtering out these two types.  Don't know why
                string extension = Path.GetExtension(currentMp3FilePath);

                if ((extension != ".cue") && (extension != ".db"))
                {
                    try
                    {
                        // Codesite is a logging tool. Where is this going???
                        CodeSite.Send("Processing file " + count++ + " from " + mp3FilePaths.Count);

                        TagLib.File tagLibFile = null;

                        try
                        {
                            // hydrate TagLib File data structure from raw mp3 file
                            tagLibFile = TagLib.File.Create(currentMp3FilePath);
                        }
                        catch (Exception exception)
                        {
                            _filesWithMissingTags.Add(currentMp3FilePath);
                            CodeSite.Send(currentMp3FilePath);
                            CodeSite.SendException(exception);
                        }

                        string artist      = "Unknown artist";
                        string album       = "Unknown album";
                        string title       = "Unknown title";
                        string trackNumber = "00";
                        int    bitrate     = 1;

                        // if we have a tag library, we'll go through it and create an MP3Node to represent it
                        // TODO: Not sure the justification of moving from this tagLib format to our own custom format
                        if (tagLibFile != null && tagLibFile.Tag != null)
                        {
                            // set artist
                            if (tagLibFile.Tag.AlbumArtists.Length > 0)
                            {
                                artist = tagLibFile.Tag.AlbumArtists[0];
                            }
                            // this property is obsoleted so only check as a fallback
                            else if (tagLibFile.Tag.Artists.Length > 0)
                            {
                                artist = tagLibFile.Tag.Artists[0];
                            }
                            else
                            {
                                _filesWithMissingTags.Add(currentMp3FilePath);
                            }

                            // set album
                            if (tagLibFile.Tag.Album.Length > 0)
                            {
                                album = tagLibFile.Tag.Album;
                            }
                            else if (tagLibFile.Tag.AlbumArtists.Length > 0)
                            {
                                album = tagLibFile.Tag.AlbumArtists[0];
                            }
                            else
                            {
                                _filesWithMissingTags.Add(currentMp3FilePath);
                            }

                            // set trackName
                            if (tagLibFile.Tag.Title.Length > 0)
                            {
                                title = tagLibFile.Tag.Title;
                            }
                            else
                            {
                                title = currentMp3FilePath;
                            }

                            // set track number
                            trackNumber = tagLibFile.Tag.Track.ToString();

                            if (string.IsNullOrEmpty(trackNumber))
                            {
                                trackNumber = "00";
                            }

                            bitrate = tagLibFile.Properties.AudioBitrate;
                        }
                        else
                        {
                            _filesWithMissingTags.Add(currentMp3FilePath);
                        }

                        // create new MP3 Node in the list
                        var Mp3Node1 = new Mp3Node()
                        {
                            AlbumName   = album,
                            ArtistName  = artist,
                            FileName    = currentMp3FilePath,
                            Title       = title,
                            Bitrate     = bitrate,
                            TrackNumber = trackNumber
                        };

                        mp3FileList.Add(Mp3Node1);
                    }
                    catch (Exception ex)
                    {
                        CodeSite.Send(currentMp3FilePath);
                        CodeSite.SendException(ex);
                        _filesWithMissingTags.Add(currentMp3FilePath);
                    }
                }
            }

            return(mp3FileList);
        }
示例#29
0
        public static void WebCheck(IPv4SSL ip)
        {
            IPAddress value = IPAddress.Parse(ip.IP);

            try
            {
                using (Ping ping = new Ping())
                {
                    PingReply pr = ping.Send(value, 1000);
                    if (pr.Status == IPStatus.Success)
                    {
                        ip.RoundtripTime = (int)pr.RoundtripTime;
                        //ip = new IPv4SSL(value.ToUInt32(), pr.RoundtripTime);
                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format("https://{0}", value));
                        request.Timeout                             = 5000;
                        request.AllowAutoRedirect                   = false;
                        request.AllowWriteStreamBuffering           = false;
                        request.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
                        {
                            chain.Dispose();
                            if (certificate == null)
                            {
                                return(false);
                            }
                            ip.Issuer  = ((X509Certificate2)certificate).GetNameInfo(X509NameType.SimpleName, true);
                            ip.Subject = ((X509Certificate2)certificate).GetNameInfo(X509NameType.SimpleName, false);
                            if (ip.IsGoogle)
                            {
                                CodeSite.Send("IP", value.ToString());
                            }
                            certificate.Dispose();
                            return(ip.IsGoogle);
                        };
                        request.Method    = "HEAD";
                        request.KeepAlive = false;
                        try
                        {
                            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                            {
                                ip.Server = response.Server;
                                CodeSite.Send("response", response);
                            }
                        }
                        finally
                        {
                            request.Abort();
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                if (ip.IsGoogle)
                {
                    HttpWebResponse response = ex.Response as HttpWebResponse;
                    if (response != null)
                    {
                        ip.Server = response.Server;
                        CodeSite.Send("response", response);
                    }
                    else
                    {
                        ex.SendCodeSite("IsGoogle");
                    }
                }
            }
            catch (Exception ex)
            {
                if (ip.IsGoogle)
                {
                    ex.SendCodeSite("WebCheck");
                }
            }
        }