Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net;
- using System.IO;
- using System.Security;
- using System.Text.RegularExpressions;
- namespace Jerl.GelbooruActionablesLibrary
- {
- /// <summary>
- /// This class enables the user to log in to the site.
- /// </summary>
- public static class Account
- {
- //provide access to the cookies generated
- private static CookieContainer cookieContainer;
- //backing field
- private static Uri loginUri = new Uri("http://gelbooru.com/index.php?page=account&s=login&code=00");
- /// <summary>
- /// provides external visibility to the login URI should it be needed
- /// </summary>
- public static Uri LoginUri
- {
- get { return loginUri; }
- }
- /// <summary>
- /// Logs the user in to the site using the console.
- /// </summary>
- /// <returns>A CookieContainer with the user's login credentials</returns>
- public static CookieContainer LoginUi()
- {
- bool loginSuccess = false;
- do
- {
- //prompt the user for login information
- Console.WriteLine("Input username");
- string username;
- do
- username = Console.ReadLine();
- while (username == "");
- Console.WriteLine("Input password");
- string password;
- do
- password = GetPassword();
- while (password == "");
- //clear the console once we have what we need
- Console.Clear();
- Login(username, password, out loginSuccess);
- if (!loginSuccess)
- Console.WriteLine("Login failed!");
- } while (!loginSuccess);
- return cookieContainer;
- }
- /// <summary>
- /// Logs in to the site using the provided credentials and verifies that the login succeeded.
- /// </summary>
- /// <param name="username">The username to log in with.</param>
- /// <param name="password">The password to log in with.</param>
- /// <param name="loginSuccess">A boolean that will be set to true if login succeeds or false if login fails.</param>
- /// <returns>A CookieContainer containing successful login credentials.</returns>
- public static CookieContainer Login(string username, string password, out bool loginSuccess)
- {
- //create a byte array out of our request
- byte[] post = new ASCIIEncoding().GetBytes(string.Format("user={0}&pass={1}&submit=Log+in", username, password));
- //create the login request
- HttpWebRequest loginRequest = (HttpWebRequest)WebRequest.Create(LoginUri);
- loginRequest.Proxy = null;
- loginRequest.Method = "POST";
- loginRequest.AllowAutoRedirect = false;
- loginRequest.ContentType = "application/x-www-form-urlencoded";
- loginRequest.ContentLength = post.Length;
- //post the data
- Stream postStream = loginRequest.GetRequestStream();
- postStream.Write(post, 0, post.Length);
- postStream.Close();
- //create the cookie container
- loginRequest.CookieContainer = new CookieContainer();
- loginRequest.Referer = "http://gelbooru.com/index.php?page=account&s=login&code=00";
- //load the page
- try
- {
- HttpWebResponse loginResponse = (HttpWebResponse)loginRequest.GetResponse();
- Stream dumpData = loginResponse.GetResponseStream();
- Regex loginCheckRegex = new Regex("s=home");
- using (StreamReader dumpDataReader = new StreamReader(dumpData))
- {
- //string page = dumpDataReader.ReadToEnd();
- //loginSuccess = loginCheckRegex.IsMatch(page);
- dumpDataReader.ReadToEnd();
- }
- loginSuccess = (loginResponse.Headers["Location"] == "index.php?page=account&s=home");
- //update our field with the cookies we acquired
- cookieContainer = loginRequest.CookieContainer;
- }
- catch (WebException)
- {
- loginSuccess = false;
- }
- return cookieContainer;
- }
- /// <summary>
- /// Fetches the user's password from the console
- /// </summary>
- /// <returns>The user's password</returns>
- public static string GetPassword()
- {
- string pwd = "";
- while (true)
- {
- ConsoleKeyInfo i = Console.ReadKey(true);
- if (i.Key == ConsoleKey.Enter)
- {
- break;
- }
- else if (i.Key == ConsoleKey.Backspace)
- {
- if (pwd.Length > 0)
- {
- pwd.Remove(pwd.Length - 1);
- Console.Write("\b \b");
- }
- }
- else
- {
- pwd = pwd.Insert(pwd.Length, i.KeyChar.ToString());
- Console.Write("*");
- }
- }
- return pwd;
- }
- /// <summary>
- /// Verifies that a CookieContainer contains valid login credentials.
- /// </summary>
- /// <param name="cookies">A CookieContainer to check for valid Gelbooru login credentials.</param>
- /// <returns>true if the credentials are valid, false if they are not.</returns>
- public static bool LoginVerified(CookieContainer cookies)
- {
- Regex loggedInRegex = new Regex("index.php?page=account&s=profile");
- HttpWebRequest loginVerificationRequest = (HttpWebRequest)WebRequest.Create("http://gelbooru.com/index.php?page=account&s=home");
- loginVerificationRequest.CookieContainer = cookies;
- try
- {
- HttpWebResponse loginVerificationResponse = (HttpWebResponse)loginVerificationRequest.GetResponse();
- string page;
- using (StreamReader sr = new StreamReader(loginVerificationResponse.GetResponseStream()))
- {
- page = sr.ReadToEnd();
- }
- if (Regex.IsMatch(page, "Logout"))
- return true;
- return false;
- }
- catch (WebException)
- {
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement