Пример #1
0
        private static async System.Threading.Tasks.Task InstallBrowserAsync()
        {
            // Установка и обновление браузера chromium
            var browserFetcher = new BrowserFetcher();
            var localVersions  = browserFetcher.LocalRevisions();

            if (!localVersions.Any() || BrowserFetcher.DefaultRevision != localVersions.Max())
            {
                Log.Information("Downloading chromium...");
                browserFetcher.DownloadProgressChanged += (_, e) => { Console.Write("\r" + e.ProgressPercentage + "%"); };
                await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);

                Console.WriteLine(); // Перевод курсора на следующую строку(чтобы небыло "100%Успешно")
            }
        }
Пример #2
0
        /// <summary>
        /// Downloads the browser.
        /// </summary>
        /// <param name="logger">The logger.</param>
        /// <exception cref="TimeoutException">Timeout waiting for exclusive access.</exception>
        internal static void DownloadBrowser(ILogger logger)
        {
            string mutexId = $@"Global\{Directory.GetCurrentDirectory().Replace('\\', '_')}";

            var allowEveryoneRule =
                new MutexAccessRule(
                    new SecurityIdentifier(WellKnownSidType.WorldSid, null),
                    MutexRights.FullControl,
                    AccessControlType.Allow);
            var securitySettings = new MutexSecurity();

            securitySettings.AddAccessRule(allowEveryoneRule);

            using (var mutex = new Mutex(false, mutexId, out bool createdNew, securitySettings))
            {
                var hasHandle = mutex.WaitOne(Timeout.Infinite, false);
                if (hasHandle == false)
                {
                    throw new TimeoutException("Timeout waiting for exclusive access");
                }

                try
                {
                    BrowserFetcher browserFetcher = new BrowserFetcher();
                    if (browserFetcher.LocalRevisions().Any(x => x == BrowserFetcher.DefaultChromiumRevision))
                    {
                        // local revision already exists
                        return;
                    }

                    logger.LogDebug("Downloading browser...");
                    Task <RevisionInfo> task = browserFetcher.DownloadAsync(BrowserFetcher.DefaultChromiumRevision);
                    task.Wait();

                    RevisionInfo revisionInfo = task.Result;
                    if (!revisionInfo.Downloaded)
                    {
                        throw new Exception($"Browser could not be downlaoded from URL \"{revisionInfo.Url}\".");
                    }

                    logger.LogDebug($"Browser downloaded to \"{revisionInfo.FolderPath}\".");
                }
                finally
                {
                    mutex.ReleaseMutex();
                }
            }
        }
Пример #3
0
        public async Task StartBrowserAsync()
        {
            var fetcher   = new BrowserFetcher();
            var revisions = fetcher.LocalRevisions();

            if (!revisions.Any())
            {
                _logger.LogInformation("Downloading Chromium browser");
                await fetcher.DownloadAsync(BrowserFetcher.DefaultRevision);
            }

            _logger.LogInformation("Launching Chromium browser");

            Browser = await Puppeteer.LaunchAsync(new LaunchOptions
            {
                DefaultViewport = new ViewPortOptions()
                {
                    Width  = 1366,
                    Height = 768
                },
                Headless = !Debugger.IsAttached
            });
        }
Пример #4
0
 public static IEnumerable <int> ValidLocalRevisions()
 {
     return(BrowserFetcher.LocalRevisions().Where(r => IsValidRevision(RevisionInfo(r))));
 }
    public async Task <bool> DoAsync()
    {
        try
        {
            CancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(60));

            BrowserFetcher browserFetcher = new BrowserFetcher();

            if (!browserFetcher.LocalRevisions().Contains(BrowserFetcher.DefaultChromiumRevision))
            {
                Logger.Warn($"Downloading browser... First time it can take a while, depending on your internet connection.");
                RevisionInfo revisionInfo = await browserFetcher.DownloadAsync(BrowserFetcher.DefaultChromiumRevision);

                Logger.Warn($"Downloaded browser. Downloaded: {revisionInfo.Downloaded}, Platform: {revisionInfo.Platform}, Revision: {revisionInfo.Revision}, Path: {revisionInfo.FolderPath}");
            }

            Logger.Debug($"Creating browser...");

            PuppeteerExtra puppeteerExtra = new PuppeteerExtra();

            // Use stealth plugin (needed for Cloudflare / hCaptcha)
            puppeteerExtra.Use(new StealthPlugin());

            using (Browser = await puppeteerExtra.LaunchAsync(new LaunchOptions
            {
                Headless = false,
                Args = new[] { "--no-sandbox", "--disable-setuid-sandbox", $"--user-agent=\"{Constants.UserAgent.Chrome}\"" },
                DefaultViewport = null,
                IgnoreHTTPSErrors = true
            }))
            {
                Logger.Info($"Started browser with PID {Browser.Process.Id}");

                Browser.Closed          += Browser_Closed;
                Browser.Disconnected    += Browser_Disconnected;
                Browser.TargetChanged   += Browser_TargetChanged;
                Browser.TargetCreated   += Browser_TargetCreated;
                Browser.TargetDestroyed += Browser_TargetDestroyed;

                Logger.Debug($"Created browser.");

                Logger.Debug($"Creating page...");

                using (Page = (await Browser.PagesAsync())[0])
                {
                    Page.Close                  += Page_Close;
                    Page.Console                += Page_Console;
                    Page.Dialog                 += Page_Dialog;
                    Page.DOMContentLoaded       += Page_DOMContentLoaded;
                    Page.Error                  += Page_Error;
                    Page.FrameAttached          += Page_FrameAttached;
                    Page.FrameDetached          += Page_FrameDetached;
                    Page.FrameNavigated         += Page_FrameNavigated;
                    Page.Load                   += Page_Load;
                    Page.Metrics                += Page_Metrics;
                    Page.PageError              += Page_PageError;
                    Page.Popup                  += Page_Popup;
                    Page.Request                += Page_Request;
                    Page.RequestFailed          += Page_RequestFailed;
                    Page.RequestFinished        += Page_RequestFinished;
                    Page.RequestServedFromCache += Page_RequestServedFromCache;
                    Page.Response               += Page_Response;
                    Page.WorkerCreated          += Page_WorkerCreated;
                    Page.WorkerDestroyed        += Page_WorkerDestroyed;

                    Logger.Debug($"Created page.");

                    Stopwatch stopwatch = Stopwatch.StartNew();

                    Logger.Debug($"Navigating to {Url}..");

                    await Page.GoToAsync(Url);

                    await Task.Delay(TimeSpan.FromSeconds(60), CancellationTokenSource.Token);

                    Logger.Debug($"Navigation done in {stopwatch.ElapsedMilliseconds}ms");
                }
            }

            Logger.Debug("Finished with browser!");
        }
        catch (OperationCanceledException ex)
        {
            if (!OK)
            {
                Logger.Error(ex, "Looks like Cloudflare protection wasn't solved in time.");
            }
        }
        catch (Exception ex)
        {
            Logger.Error(ex, "Error with browser");
        }
        finally
        {
            Logger.Debug("Closing browser");
            await Browser.CloseAsync();

            Logger.Debug("Closed browser");
        }

        return(OK);
    }