Advertisement
Guest User

Untitled

a guest
Dec 18th, 2016
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. using MailKit.Net.Imap;
  2. using MailKit.Search;
  3. using Org.BouncyCastle.X509;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.Security;
  9. using System.Security.Cryptography.X509Certificates;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Threading.Tasks;
  13. using UltimateTeam.Toolkit.Exceptions;
  14. using UltimateTeam.Toolkit.Services;
  15.  
  16. namespace FUT17_AB_Pro
  17. {
  18. class ImapTFCProvider : ITwoFactorCodeProvider
  19. {
  20. private readonly string _username;
  21. private readonly string _password;
  22. private readonly string _hostName;
  23. private readonly int _port;
  24. private readonly bool _useUseSsl;
  25.  
  26. public ImapTFCProvider(string username, string password, string hostname, int port, bool useSsl)
  27. {
  28. _username = username;
  29. _password = password;
  30. _hostName = hostname;
  31. _port = port;
  32. _useUseSsl = useSsl;
  33. }
  34.  
  35. public Task<string> GetTwoFactorCodeAsync()
  36. {
  37. return Task.Run(async () =>
  38. {
  39. using (var client = new ImapClient())
  40. {
  41. var regex = new Regex(@"\d{6}");
  42. var query = SearchQuery.NotSeen.And(SearchQuery.FromContains("EA").And(SearchQuery.ToContains(_username)).And(SearchQuery.YoungerThan(120)));
  43.  
  44. ServicePointManager.ServerCertificateValidationCallback = delegate (object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
  45.  
  46. await Task.Delay(5000);
  47.  
  48. try
  49. {
  50. client.Connect(_hostName, _port, _useUseSsl);
  51. client.AuthenticationMechanisms.Remove("XOAUTH2");
  52. client.Authenticate(_username, _password);
  53. } catch
  54. {
  55. return GetCodeFromForms();
  56. }
  57.  
  58.  
  59. for (int i = 0; i < 6; i++)
  60. {
  61. var inbox = client.Inbox;
  62. inbox.Open(MailKit.FolderAccess.ReadOnly);
  63.  
  64. foreach (var uid in inbox.Search(query))
  65. {
  66. var msg = inbox.GetMessage(uid);
  67. var match = regex.Match(msg.Subject);
  68.  
  69. return match.Value;
  70. }
  71. await Task.Delay(5000);
  72. }
  73. }
  74. throw new FutException("Unable to find security code.");
  75. });
  76. }
  77.  
  78. public string GetCodeFromForms()
  79. {
  80. var form = new FormTFC(_username);
  81. if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  82. {
  83. return form.Code;
  84. }
  85. else
  86. return string.Empty;
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement