Advertisement
Guest User

RuneScape.cs

a guest
Mar 4th, 2018
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.50 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Net;
  4. using System.IO;
  5. using System.Net.Cache;
  6. using System.Security.Authentication;
  7.  
  8. public sealed class RuneScape
  9. {
  10.     /// <summary>
  11.     /// Username or Email address
  12.     /// </summary>
  13.     public String UserName { get; set; }
  14.  
  15.     /// <summary>
  16.     /// Password
  17.     /// </summary>
  18.     public String Password { get; set; }
  19.  
  20.     /// <summary>
  21.     /// Nickname
  22.     /// </summary>
  23.     public String NickName { get; protected set; }
  24.  
  25.     /// <summary>
  26.     /// Member status
  27.     /// </summary>
  28.     public Boolean IsMember { get; protected set; }
  29.  
  30.     /// <summary>
  31.     /// Account Id
  32.     /// </summary>
  33.     public Int32 AccountId { get; protected set; }
  34.  
  35.     /// <summary>
  36.     /// Is authenticated
  37.     /// </summary>
  38.     public Boolean IsAuthenticated { get; protected set; }
  39.  
  40.     /// <summary>
  41.     /// Session data
  42.     /// </summary>
  43.     public CookieContainer Cookies { get; protected set; }
  44.  
  45.     /// <summary>
  46.     /// Proxy
  47.     /// </summary>
  48.     public WebProxy Proxy { get; set; }
  49.  
  50.     /// <summary>
  51.     /// Base uri
  52.     /// </summary>
  53.     private const String Base = "https://secure.runescape.com";
  54.  
  55.     /// <summary>
  56.     /// Constructor
  57.     /// </summary>
  58.     public RuneScape()
  59.     {
  60.         SslProtocols tlsToken = (SslProtocols)0xC00;
  61.         ServicePointManager.SecurityProtocol = (SecurityProtocolType)tlsToken;
  62.         ServicePointManager.MaxServicePoints = ushort.MaxValue;
  63.         ServicePointManager.Expect100Continue = false;
  64.         ServicePointManager.MaxServicePointIdleTime = 0;
  65.  
  66.         Refresh();
  67.     }
  68.  
  69.     /// <summary>
  70.     /// Refresh session
  71.     /// </summary>
  72.     /// <returns></returns>
  73.     public Boolean Refresh()
  74.     {
  75.         IsAuthenticated = false;
  76.         Cookies = new CookieContainer();
  77.         return true;
  78.     }
  79.  
  80.     /// <summary>
  81.     /// Authenticate with RuneScape
  82.     /// </summary>
  83.     public Boolean Authenticate()
  84.     {
  85.         if (String.IsNullOrEmpty(UserName) || String.IsNullOrEmpty(Password))
  86.             return false;
  87.  
  88.         if (CanAuthenticate())
  89.         {
  90.             Encoding encoding = Encoding.GetEncoding("iso-8859-15");
  91.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Base + "/m=weblogin/login.ws");
  92.             request.Method = "POST";
  93.  
  94.             request.CookieContainer = Cookies;
  95.             request.Proxy = Proxy;
  96.             request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
  97.             request.UseDefaultCredentials = false;
  98.  
  99.             request.Headers.Set("Accept-Language", "en-US,en;q=0.5");
  100.             request.Headers.Set("Accept-Encoding", "gzip, deflate, br");
  101.             request.Headers.Set("Upgrade-Insecure-Requests", "1");
  102.             request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0";
  103.             request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
  104.             request.AllowAutoRedirect = true;
  105.             request.CachePolicy = new RequestCachePolicy(level: RequestCacheLevel.BypassCache);
  106.             request.KeepAlive = true;
  107.  
  108.             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"));
  109.             Byte[] httpPostData = Encoding.UTF8.GetBytes(postData);
  110.  
  111.             request.Referer = Path.Combine(Base, "/m=weblogin/loginform.ws?mod=www&ssl=1&expired=0");
  112.             request.ContentType = "application/www-form-urlencoded";
  113.             request.ContentLength = httpPostData.Length;
  114.  
  115.             using (Stream requestStream = request.GetRequestStream())
  116.             {
  117.                 requestStream.Write(httpPostData, 0, httpPostData.Length);
  118.             }
  119.  
  120.             HttpWebResponse response = default(HttpWebResponse);
  121.             try
  122.             {
  123.                 response = (HttpWebResponse)request.GetResponse();
  124.             }
  125.             catch (WebException exception)
  126.             {
  127.                 response = (HttpWebResponse)exception.Response;
  128.             }
  129.  
  130.             if (response != null)
  131.             {
  132.                 if (((Int32)response.StatusCode / 100) == 2)
  133.                 {
  134.                     Boolean result = response.ResponseUri.ToString().IndexOf("account_settings.ws") != -1;
  135.                     if (result)
  136.                     {
  137.                         using (Stream responseStream = response.GetResponseStream())
  138.                         {
  139.                             using (StreamReader reader = new StreamReader(responseStream, encoding))
  140.                             {
  141.                                 String htmlSource = reader.ReadToEnd();
  142.  
  143.                                 NickName = GetNickname(htmlSource);
  144.                                 if (NickName == null)
  145.                                     NickName = UserName;
  146.  
  147.                                 IsMember = GetMemberStatus(htmlSource);
  148.                                 AccountId = GetAccountId(htmlSource);
  149.                             }
  150.                         }
  151.  
  152.                         IsAuthenticated = result;
  153.                         return IsAuthenticated;
  154.                     }
  155.                 }
  156.  
  157.                 response.Close();
  158.             }
  159.             else {
  160.                 throw new WebException("Could not connect to the RuneScape server");
  161.             }
  162.         }
  163.  
  164.         return false;
  165.     }
  166.  
  167.     /// <summary>
  168.     /// Check if can authenticate
  169.     /// </summary>
  170.     public Boolean CanAuthenticate()
  171.     {
  172.         Encoding encoding = Encoding.GetEncoding("iso-8859-15");
  173.         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Base + "/m=weblogin/loginform.ws?mod=www&ssl=1&expired=0");
  174.         request.Method = "GET";
  175.  
  176.         request.CookieContainer = Cookies;
  177.         request.Proxy = Proxy;
  178.         request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
  179.         request.UseDefaultCredentials = false;
  180.  
  181.         request.Headers.Set("Accept-Language", "en-US,en;q=0.5");
  182.         request.Headers.Set("Accept-Encoding", "gzip, deflate, br");
  183.         request.Headers.Set("Upgrade-Insecure-Requests", "1");
  184.         request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0";
  185.         request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
  186.         request.AllowAutoRedirect = true;
  187.         request.CachePolicy = new RequestCachePolicy(level: RequestCacheLevel.BypassCache);
  188.         request.KeepAlive = true;
  189.  
  190.         request.Referer = Base;
  191.  
  192.         HttpWebResponse response = default(HttpWebResponse);
  193.         try
  194.         {
  195.             response = (HttpWebResponse)request.GetResponse();
  196.         }
  197.         catch (WebException exception)
  198.         {
  199.             response = (HttpWebResponse)exception.Response;
  200.         }
  201.  
  202.         if (response != null)
  203.         {
  204.             if (((Int32)response.StatusCode / 100) == 2)
  205.             {
  206.                 using (Stream responseStream = response.GetResponseStream())
  207.                 {
  208.                     using (StreamReader reader = new StreamReader(responseStream, encoding))
  209.                     {
  210.                         String responseHtml = reader.ReadToEnd();
  211.                         return (responseHtml.IndexOf("captcha") == -1);
  212.                     }
  213.                 }
  214.             }
  215.  
  216.             response.Close();
  217.         }
  218.         else
  219.         {
  220.             throw new WebException("Could not connect to the RuneScape server");
  221.         }
  222.  
  223.         return false;
  224.     }
  225.  
  226.     /// <summary>
  227.     /// Extract nickname from HTML
  228.     /// </summary>
  229.     private String GetNickname(String htmlSource)
  230.     {
  231.         Int32 startIndex = htmlSource.IndexOf("name: ");
  232.         Int32 endIndex = -1;
  233.  
  234.         if (startIndex == -1)
  235.             return null;
  236.  
  237.         startIndex = htmlSource.IndexOf("'", startIndex);
  238.  
  239.         if (startIndex == 1)
  240.             return null;
  241.  
  242.         endIndex = htmlSource.IndexOf("'", startIndex + 1);
  243.  
  244.         if (endIndex == -1 || startIndex == -1)
  245.             return null;
  246.  
  247.         return htmlSource.Substring(startIndex + 1, (endIndex - startIndex - 1));
  248.     }
  249.  
  250.     /// <summary>
  251.     /// Extract membership status from HTML
  252.     /// </summary>
  253.     private Boolean GetMemberStatus(String htmlSource)
  254.     {
  255.         Int32 startIndex = htmlSource.IndexOf("member:");
  256.         Int32 endIndex = -1;
  257.  
  258.         if (startIndex == -1)
  259.             return false;
  260.  
  261.         startIndex = htmlSource.IndexOf(" ", startIndex);
  262.  
  263.         if (startIndex == 1)
  264.             return false;
  265.  
  266.         endIndex = htmlSource.IndexOf(",", startIndex + 1);
  267.  
  268.         if (endIndex == -1 || startIndex == -1)
  269.             return false;
  270.  
  271.         return htmlSource.Substring(startIndex + 1, (endIndex - startIndex - 1)) == "true";
  272.     }
  273.  
  274.     /// <summary>
  275.     /// Extract account id from HTML
  276.     /// </summary>
  277.     private Int32 GetAccountId(String htmlSource)
  278.     {
  279.         Int32 startIndex = htmlSource.IndexOf("id:");
  280.         Int32 endIndex = -1;
  281.  
  282.         if (startIndex == -1)
  283.             return -1;
  284.  
  285.         startIndex = htmlSource.IndexOf(" ", startIndex);
  286.  
  287.         if (startIndex == 1)
  288.             return -1;
  289.  
  290.         endIndex = htmlSource.IndexOf(",", startIndex + 1);
  291.  
  292.         if (endIndex == -1 || startIndex == -1)
  293.             return -1;
  294.  
  295.         return Int32.Parse(htmlSource.Substring(startIndex + 1, (endIndex - startIndex - 1)));
  296.     }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement