Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using MailKit.Net.Imap;
- using MailKit.Search;
- using Org.BouncyCastle.X509;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Security;
- using System.Security.Cryptography.X509Certificates;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using UltimateTeam.Toolkit.Exceptions;
- using UltimateTeam.Toolkit.Services;
- namespace FUT17_AB_Pro
- {
- class ImapTFCProvider : ITwoFactorCodeProvider
- {
- private readonly string _username;
- private readonly string _password;
- private readonly string _hostName;
- private readonly int _port;
- private readonly bool _useUseSsl;
- public ImapTFCProvider(string username, string password, string hostname, int port, bool useSsl)
- {
- _username = username;
- _password = password;
- _hostName = hostname;
- _port = port;
- _useUseSsl = useSsl;
- }
- public Task<string> GetTwoFactorCodeAsync()
- {
- return Task.Run(async () =>
- {
- using (var client = new ImapClient())
- {
- var regex = new Regex(@"\d{6}");
- var query = SearchQuery.NotSeen.And(SearchQuery.FromContains("EA").And(SearchQuery.ToContains(_username)).And(SearchQuery.YoungerThan(120)));
- ServicePointManager.ServerCertificateValidationCallback = delegate (object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
- await Task.Delay(5000);
- try
- {
- client.Connect(_hostName, _port, _useUseSsl);
- client.AuthenticationMechanisms.Remove("XOAUTH2");
- client.Authenticate(_username, _password);
- } catch
- {
- return GetCodeFromForms();
- }
- for (int i = 0; i < 6; i++)
- {
- var inbox = client.Inbox;
- inbox.Open(MailKit.FolderAccess.ReadOnly);
- foreach (var uid in inbox.Search(query))
- {
- var msg = inbox.GetMessage(uid);
- var match = regex.Match(msg.Subject);
- return match.Value;
- }
- await Task.Delay(5000);
- }
- }
- throw new FutException("Unable to find security code.");
- });
- }
- public string GetCodeFromForms()
- {
- var form = new FormTFC(_username);
- if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- return form.Code;
- }
- else
- return string.Empty;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement