Guest User

Untitled

a guest
Jul 15th, 2019
1,082
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Diagnostics;
  2. using System.Net;
  3. using System.Net.Http;
  4. using System.Text.RegularExpressions;
  5. using System.Threading.Tasks;
  6. using InstagramApiSharp.API;
  7. using InstagramApiSharp.API.Builder;
  8. using InstagramApiSharp.Classes;
  9. using InstagramApiSharp.Classes.SessionHandlers;
  10. using InstaPrimed.Models;
  11.  
  12. namespace InstaPrimed.Login
  13. {
  14. public static class LoginProcessor
  15. {
  16. private static IInstaApi CreateApi(string username, string password, string stateFile, Proxy proxy = null)
  17. {
  18. // TODO Get instagram login from available accounts
  19. var userSession = new UserSessionData()
  20. {
  21. UserName = username,
  22. Password = password
  23. };
  24.  
  25. if (proxy == null)
  26. {
  27. return InstaApiBuilder.CreateBuilder()
  28. .SetUser(userSession)
  29. //.UseHttpClientHandler(GetHttpClientHandler(proxy))
  30. //.SetRequestDelay(RequestDelay.FromSeconds(0, 1))
  31. // Session handler, set a file path to save/load your state/session data
  32. .SetSessionHandler(new FileSessionHandler() { FilePath = stateFile })
  33. .Build();
  34. }
  35. else
  36. {
  37. return InstaApiBuilder.CreateBuilder()
  38. .SetUser(userSession)
  39. .UseHttpClientHandler(GetHttpClientHandler(proxy))
  40. //.SetRequestDelay(RequestDelay.FromSeconds(0, 1))
  41. // Session handler, set a file path to save/load your state/session data
  42. .SetSessionHandler(new FileSessionHandler() { FilePath = stateFile })
  43. .Build();
  44. }
  45. }
  46.  
  47. public static async Task<InstagramLogin> LoginToInstagram(string username, string password,
  48. string stateFile, Proxy proxy = null)
  49. {
  50. var instaApi = CreateApi(username, password, stateFile, proxy);
  51.  
  52. // Load previous sessions if it exists
  53. instaApi?.SessionHandler?.Load();
  54. if (instaApi != null && instaApi.IsUserAuthenticated)
  55. {
  56. // session load logged us in
  57. return new InstagramLogin()
  58. {
  59. InstaApi = instaApi,
  60. InstaLoginResult = InstaLoginResult.Success,
  61. Username = username,
  62. Password = password
  63. };
  64. }
  65.  
  66. if (instaApi != null && !instaApi.IsUserAuthenticated)
  67. {
  68. var logInResult = await instaApi.LoginAsync();
  69. Debug.WriteLine(logInResult.Value);
  70.  
  71. if (logInResult.Succeeded)
  72. {
  73. // We logged in
  74. // lets save the session
  75. instaApi.SessionHandler.Save();
  76. return new InstagramLogin()
  77. {
  78. InstaApi = instaApi,
  79. InstaLoginResult = InstaLoginResult.Success,
  80. Username = username,
  81. Password = password
  82. };
  83. }
  84.  
  85. if (logInResult.Value == InstaLoginResult.TwoFactorRequired)
  86. {
  87. return new InstagramLogin()
  88. {
  89. InstaApi = instaApi,
  90. InstaLoginResult = InstaLoginResult.TwoFactorRequired,
  91. Username = username,
  92. Password = password
  93. };
  94. }
  95.  
  96. // Could not log in, lets see why
  97. if (logInResult.Value == InstaLoginResult.ChallengeRequired)
  98. {
  99. return new InstagramLogin()
  100. {
  101. InstaApi = instaApi,
  102. InstaLoginResult = InstaLoginResult.ChallengeRequired,
  103. Username = username,
  104. Password = password
  105. };
  106. }
  107.  
  108.  
  109. }
  110.  
  111. return new InstagramLogin()
  112. {
  113. InstaApi = instaApi,
  114. InstaLoginResult = InstaLoginResult.Exception,
  115. Username = username,
  116. Password = password
  117. };
  118. }
  119.  
  120. public static HttpClientHandler GetHttpClientHandler(Proxy p)
  121. {
  122. var proxy = new WebProxy()
  123. {
  124. Address = p.Address, //i.e: http://1.2.3.4.5:8080
  125. BypassProxyOnLocal = false,
  126. UseDefaultCredentials = false,
  127.  
  128. // *** These creds are given to the proxy server, not the web server ***
  129. // We have 8,354 proxies available
  130. // The -NUMBER behind the username will choose on of those proxies
  131. // We will choose a random number between 1-8,354 to get a random proxy
  132.  
  133. Credentials = new NetworkCredential(
  134. userName: p.Username,
  135. password: p.Password)
  136. };
  137.  
  138. // Now create a client handler which uses that proxy
  139. var httpClientHandler = new HttpClientHandler()
  140. {
  141. Proxy = proxy,
  142. };
  143. return httpClientHandler;
  144. }
  145.  
  146. public static async Task<bool> SendVerificationCodeToEmail(IInstaApi instaApi)
  147. {
  148. // check login
  149. await instaApi.ResetChallengeRequireVerifyMethodAsync();
  150. var challenge = await instaApi.GetChallengeRequireVerifyMethodAsync();
  151. if (challenge.Value.SubmitPhoneRequired)
  152. {
  153. return false;
  154. }
  155. var emailSend = await instaApi.RequestVerifyCodeToEmailForChallengeRequireAsync();
  156. return emailSend.Succeeded;
  157. }
  158. public static async Task<bool> CheckAndVerifyCode(IInstaApi instaApi, string code)
  159. {
  160. code = code.Trim();
  161. code = code.Replace(" ", "");
  162. var regex = new Regex(@"^-*[0-9,\.]+$");
  163. if (!regex.IsMatch(code) || code.Length != 6)
  164. {
  165. return false;
  166. }
  167.  
  168. if (!regex.IsMatch(code) || code.Length != 6) return false;
  169.  
  170. var verifyLogin = await instaApi.VerifyCodeForChallengeRequireAsync(code);
  171. if (!verifyLogin.Succeeded) return false;
  172.  
  173. // save session
  174. instaApi.SessionHandler.Save();
  175. return true;
  176.  
  177. }
  178.  
  179. public static bool IsAuthenticated(IInstaApi instaApi)
  180. {
  181. return instaApi.IsUserAuthenticated;
  182. }
  183. }
  184. }
RAW Paste Data