Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Net;
- using System.IO;
- using System.Net.Cache;
- using System.Security.Authentication;
- public sealed class RuneScape
- {
- /// <summary>
- /// Username or Email address
- /// </summary>
- public String UserName { get; set; }
- /// <summary>
- /// Password
- /// </summary>
- public String Password { get; set; }
- /// <summary>
- /// Nickname
- /// </summary>
- public String NickName { get; protected set; }
- /// <summary>
- /// Member status
- /// </summary>
- public Boolean IsMember { get; protected set; }
- /// <summary>
- /// Account Id
- /// </summary>
- public Int32 AccountId { get; protected set; }
- /// <summary>
- /// Is authenticated
- /// </summary>
- public Boolean IsAuthenticated { get; protected set; }
- /// <summary>
- /// Session data
- /// </summary>
- public CookieContainer Cookies { get; protected set; }
- /// <summary>
- /// Proxy
- /// </summary>
- public WebProxy Proxy { get; set; }
- /// <summary>
- /// Base uri
- /// </summary>
- private const String Base = "https://secure.runescape.com";
- /// <summary>
- /// Constructor
- /// </summary>
- public RuneScape()
- {
- SslProtocols tlsToken = (SslProtocols)0xC00;
- ServicePointManager.SecurityProtocol = (SecurityProtocolType)tlsToken;
- ServicePointManager.MaxServicePoints = ushort.MaxValue;
- ServicePointManager.Expect100Continue = false;
- ServicePointManager.MaxServicePointIdleTime = 0;
- Refresh();
- }
- /// <summary>
- /// Refresh session
- /// </summary>
- /// <returns></returns>
- public Boolean Refresh()
- {
- IsAuthenticated = false;
- Cookies = new CookieContainer();
- return true;
- }
- /// <summary>
- /// Authenticate with RuneScape
- /// </summary>
- public Boolean Authenticate()
- {
- if (String.IsNullOrEmpty(UserName) || String.IsNullOrEmpty(Password))
- return false;
- if (CanAuthenticate())
- {
- Encoding encoding = Encoding.GetEncoding("iso-8859-15");
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Base + "/m=weblogin/login.ws");
- request.Method = "POST";
- request.CookieContainer = Cookies;
- request.Proxy = Proxy;
- request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
- request.UseDefaultCredentials = false;
- request.Headers.Set("Accept-Language", "en-US,en;q=0.5");
- request.Headers.Set("Accept-Encoding", "gzip, deflate, br");
- request.Headers.Set("Upgrade-Insecure-Requests", "1");
- request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0";
- request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
- request.AllowAutoRedirect = true;
- request.CachePolicy = new RequestCachePolicy(level: RequestCacheLevel.BypassCache);
- request.KeepAlive = true;
- String postData = string.Format("username={0}&password={1}&mod=www&ssl=1&expired=0&dest={2}", Uri.EscapeDataString(UserName), Uri.EscapeDataString(Password), Uri.EscapeDataString("account_settings.ws"));
- Byte[] httpPostData = Encoding.UTF8.GetBytes(postData);
- request.Referer = Path.Combine(Base, "/m=weblogin/loginform.ws?mod=www&ssl=1&expired=0");
- request.ContentType = "application/www-form-urlencoded";
- request.ContentLength = httpPostData.Length;
- using (Stream requestStream = request.GetRequestStream())
- {
- requestStream.Write(httpPostData, 0, httpPostData.Length);
- }
- HttpWebResponse response = default(HttpWebResponse);
- try
- {
- response = (HttpWebResponse)request.GetResponse();
- }
- catch (WebException exception)
- {
- response = (HttpWebResponse)exception.Response;
- }
- if (response != null)
- {
- if (((Int32)response.StatusCode / 100) == 2)
- {
- Boolean result = response.ResponseUri.ToString().IndexOf("account_settings.ws") != -1;
- if (result)
- {
- using (Stream responseStream = response.GetResponseStream())
- {
- using (StreamReader reader = new StreamReader(responseStream, encoding))
- {
- String htmlSource = reader.ReadToEnd();
- NickName = GetNickname(htmlSource);
- if (NickName == null)
- NickName = UserName;
- IsMember = GetMemberStatus(htmlSource);
- AccountId = GetAccountId(htmlSource);
- }
- }
- IsAuthenticated = result;
- return IsAuthenticated;
- }
- }
- response.Close();
- }
- else {
- throw new WebException("Could not connect to the RuneScape server");
- }
- }
- return false;
- }
- /// <summary>
- /// Check if can authenticate
- /// </summary>
- public Boolean CanAuthenticate()
- {
- Encoding encoding = Encoding.GetEncoding("iso-8859-15");
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Base + "/m=weblogin/loginform.ws?mod=www&ssl=1&expired=0");
- request.Method = "GET";
- request.CookieContainer = Cookies;
- request.Proxy = Proxy;
- request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
- request.UseDefaultCredentials = false;
- request.Headers.Set("Accept-Language", "en-US,en;q=0.5");
- request.Headers.Set("Accept-Encoding", "gzip, deflate, br");
- request.Headers.Set("Upgrade-Insecure-Requests", "1");
- request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0";
- request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
- request.AllowAutoRedirect = true;
- request.CachePolicy = new RequestCachePolicy(level: RequestCacheLevel.BypassCache);
- request.KeepAlive = true;
- request.Referer = Base;
- HttpWebResponse response = default(HttpWebResponse);
- try
- {
- response = (HttpWebResponse)request.GetResponse();
- }
- catch (WebException exception)
- {
- response = (HttpWebResponse)exception.Response;
- }
- if (response != null)
- {
- if (((Int32)response.StatusCode / 100) == 2)
- {
- using (Stream responseStream = response.GetResponseStream())
- {
- using (StreamReader reader = new StreamReader(responseStream, encoding))
- {
- String responseHtml = reader.ReadToEnd();
- return (responseHtml.IndexOf("captcha") == -1);
- }
- }
- }
- response.Close();
- }
- else
- {
- throw new WebException("Could not connect to the RuneScape server");
- }
- return false;
- }
- /// <summary>
- /// Extract nickname from HTML
- /// </summary>
- private String GetNickname(String htmlSource)
- {
- Int32 startIndex = htmlSource.IndexOf("name: ");
- Int32 endIndex = -1;
- if (startIndex == -1)
- return null;
- startIndex = htmlSource.IndexOf("'", startIndex);
- if (startIndex == 1)
- return null;
- endIndex = htmlSource.IndexOf("'", startIndex + 1);
- if (endIndex == -1 || startIndex == -1)
- return null;
- return htmlSource.Substring(startIndex + 1, (endIndex - startIndex - 1));
- }
- /// <summary>
- /// Extract membership status from HTML
- /// </summary>
- private Boolean GetMemberStatus(String htmlSource)
- {
- Int32 startIndex = htmlSource.IndexOf("member:");
- Int32 endIndex = -1;
- if (startIndex == -1)
- return false;
- startIndex = htmlSource.IndexOf(" ", startIndex);
- if (startIndex == 1)
- return false;
- endIndex = htmlSource.IndexOf(",", startIndex + 1);
- if (endIndex == -1 || startIndex == -1)
- return false;
- return htmlSource.Substring(startIndex + 1, (endIndex - startIndex - 1)) == "true";
- }
- /// <summary>
- /// Extract account id from HTML
- /// </summary>
- private Int32 GetAccountId(String htmlSource)
- {
- Int32 startIndex = htmlSource.IndexOf("id:");
- Int32 endIndex = -1;
- if (startIndex == -1)
- return -1;
- startIndex = htmlSource.IndexOf(" ", startIndex);
- if (startIndex == 1)
- return -1;
- endIndex = htmlSource.IndexOf(",", startIndex + 1);
- if (endIndex == -1 || startIndex == -1)
- return -1;
- return Int32.Parse(htmlSource.Substring(startIndex + 1, (endIndex - startIndex - 1)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement