Exemple #1
0
        /// <summary>
        ///     Authentication process to pixiv web api, which is driven by
        ///     <a href="https://github.com/cefsharp/CefSharp">CefSharp</a>
        ///     This method is for login usage only, USE AT YOUR OWN RISK
        /// </summary>
        /// <param name="name">user name</param>
        /// <param name="pwd">user password</param>
        /// <returns></returns>
        public static async Task WebApiAuthenticate(string name, string pwd)
        {
            // create x.509 certificate object for intercepting https traffic, USE AT YOUR OWN RISK
            var certificate = await CertificateManager.GetFakeServerCertificate();

            // create https proxy server for intercepting and forwarding https traffic
            using var proxyServer = HttpsProxyServer.Create("127.0.0.1", AppContext.ProxyPort, (await new PixivApiDnsResolver().Lookup("pixiv.net"))[0].ToString(), certificate);
            // create pac file server for providing the proxy-auto-configuration file,
            // which is driven by EmbedIO, this is because CefSharp do not accept file uri
            using var pacServer = PacFileServer.Create("127.0.0.1", AppContext.PacPort);
            pacServer.Start();
            var          chrome   = SignIn.Instance.ChromiumWebBrowser;
            const string loginUrl = "https://accounts.pixiv.net/login";

            chrome.Address       = loginUrl;
            chrome.FrameLoadEnd += (sender, args) =>
            {
                // when the login page is loaded, we will execute the following js snippet
                // which is going to fill and submit the form
                if (args.Url == loginUrl)
                {
                    // ReSharper disable once AccessToDisposedClosure
                    chrome.ExecuteScriptAsync($@"
                                var container_login = document.getElementById('container-login');
                                var fields = container_login.getElementsByClassName('input-field');
                                var account = fields[0].getElementsByTagName('input')[0];
                                var password = fields[1].getElementsByTagName('input')[0];
                                account.value = '{name}';
                                password.value = '{pwd}';
                                document.getElementById('container-login').getElementsByClassName('signup-form__submit')[0].click();
                        ");
                }
            };
#pragma warning disable 4014
            var cancellationTokenSource = new CancellationTokenSource();
            Task.Run(async() =>
            {
                // ReSharper disable AccessToDisposedClosure
                while (!cancellationTokenSource.Token.IsCancellationRequested)
                {
                    await Task.Delay(1000, cancellationTokenSource.Token);
                    var src = await chrome.Dispatcher.Invoke(async() =>
                    {
                        if (chrome.WebBrowser != null)
                        {
                            return(await chrome.WebBrowser.GetSourceAsync());
                        }
                        return("");
                    });
                    if (src.Contains("error-msg-list__item"))
                    {
                        cancellationTokenSource.Cancel();
                        chrome.Dispatcher.Invoke(() =>
                        {
                            MessageBox.Show(AkaI18N.ThisLoginSessionRequiresRecaptcha);
                            SignIn.Instance.BrowserDialog.IsOpen = true;
                        });
                    }
                }
            }, cancellationTokenSource.Token);
Exemple #2
0
        /// <summary>
        ///     Authentication process to pixiv web api, which is driven by
        ///     <a href="https://github.com/cefsharp/CefSharp">CefSharp</a>
        ///     This method is for login usage only, USE AT YOUR OWN RISK
        /// </summary>
        /// <param name="name">user name</param>
        /// <param name="pwd">user password</param>
        /// <returns></returns>
        public static async Task WebApiAuthenticate(string name, string pwd)
        {
            // create x.509 certificate object for intercepting https traffic, USE AT YOUR OWN RISK
            var certificate = await CertificateManager.GetFakeServerCertificate();

            // create https reverse proxy server for intercepting and forwarding https traffic,
            // default port is 1234
            using var proxyServer = HttpsProxyServer.Create("127.0.0.1", 1234, (await new PixivApiDnsResolver().Lookup("pixiv.net"))[0].ToString(), certificate);
            // create pac file server for providing the proxy-auto-configuration file,
            // which is driven by EmbedIO, this is because CefSharp do not accept file uri,
            // default port is 4321
            using var pacServer = PacFileServer.Create("127.0.0.1", 4321);
            pacServer.Start();
            const string loginUrl = "https://accounts.pixiv.net/login";
            var          chrome   = new ChromiumWebBrowser(loginUrl)
            {
                RequestHandler = new BypassProxyRequestHandler()
            };

            chrome.FrameLoadEnd += (sender, args) =>
            {
                // when the login page is loaded, we will execute the following js snippet
                // which is going to fill and submit the form
                if (args.Url == loginUrl)
                {
                    // ReSharper disable once AccessToDisposedClosure
                    chrome.ExecuteScriptAsync(
                        $@"
                                var container_login = document.getElementById('container-login');
                                var fields = container_login.getElementsByClassName('input-field');
                                var account = fields[0].getElementsByTagName('input')[0]
                                var password = fields[1].getElementsByTagName('input')[0]
                                account.value = '{name}';
                                password.value = '{pwd}';
                                document.getElementById('container-login').getElementsByClassName('signup-form__submit')[0].click()  
                        ");
                }
            };