Advertisement
Guest User

Untitled

a guest
Jan 10th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.Text.RegularExpressions;
  7. using System.IO;
  8. using System.IO.Compression;
  9. using System.Net.Sockets;
  10. using Flurl;
  11. using Flurl.Http;
  12.  
  13.  
  14. namespace goodwrap
  15. {
  16.     class wrapper
  17.     {
  18.         private TcpClient client;
  19.         IDictionary<string, string> colCookies = new Dictionary<string, string>();
  20.         public string strCookies;
  21.         public string LastPage;
  22.  
  23.         public string Request(string Method, string URL, string Referer)
  24.         {
  25.             string Host = null;
  26.             string strFile = null;
  27.             string strPost = null;
  28.             int pos = 0;
  29.  
  30.             if (Referer == null)
  31.             {
  32.                 Referer = LastPage;
  33.             }
  34.             if (URL.Contains("http://"))
  35.             {
  36.                 Host = URL.Substring(7);
  37.             }
  38.             else
  39.             {
  40.                 Host = URL;
  41.             }
  42.             if (Host.Contains("/"))
  43.             {
  44.                 pos = Host.IndexOf("/", 0);
  45.                 strFile = Host.Substring(pos);
  46.                 Host = Host.Substring(0, pos);
  47.             }
  48.             else
  49.             {
  50.                 strFile = "/";
  51.             }
  52.             if (Method == "POST")
  53.             {
  54.                 pos = strFile.IndexOf("?");
  55.                 if (pos != -1)
  56.                 {
  57.                     strPost = strFile.Substring(pos + 1);
  58.                     strFile = strFile.Substring(0, pos);
  59.                 }
  60.                 else
  61.                 {
  62.                     strPost = null;
  63.                 }
  64.             }
  65.             LastPage = URL;
  66.  
  67.             string ReqHeaders = null;
  68.             if (Method == "GET" || Method == "PIC")
  69.             {
  70.                 ReqHeaders = "GET" + " " + strFile + " HTTP/1.1" + "\r\n"
  71.                 + "Host: " + Host + "\r\n"
  72.                 + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 (.NET CLR 3.5.30729)" + "\r\n"
  73.                 + "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/ *;q=0.5" + "\r\n"
  74.                 + "Accept-Language: en-us,en;q=0.5" + "\r\n"
  75.                 + "Accept-Encoding: gzip, deflate" + "\r\n"
  76.                 + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" + "\r\n"
  77.                 + "Keep-Alive: 300" + "\r\n"
  78.                 + "Connection: keep-alive" + "\r\n"
  79.                 + "Referer: " + Referer + "\r\n"
  80.                 + "Cookie: " + strCookies + "\r\n" + "\r\n";
  81.             }
  82.             else
  83.             {
  84.                 ReqHeaders = "POST " + strFile + " HTTP/1.1" + "\r\n"
  85.                 + "Host: " + Host + "\r\n"
  86.                 + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 (.NET CLR 3.5.30729)" + "\r\n"
  87.                 + "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/ *;q=0.5" + "\r\n"
  88.                 + "Accept-Language: en-us,en;q=0.5" + "\r\n"
  89.                 + "Accept-Encoding: gzip, deflate" + "\r\n"
  90.                 + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" + "\r\n"
  91.                 + "Keep-Alive: 300" + "\r\n"
  92.                 + "Connection: keep-alive" + "\r\n"
  93.                 + "Referer: " + Referer + "\r\n"
  94.                 + "Cookie: " + strCookies + "\r\n"
  95.                 + "Content-Type: application/x-www-form-urlencoded" + "\r\n"
  96.                 + "Content-Length: " + strPost.Length.ToString() + "\r\n"
  97.                 + "Connection: close" + "\r\n" + "\r\n"
  98.                 + strPost;
  99.             }
  100.             if (Method == "PIC") ReqHeaders.Replace("Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/ *;q=0.5", "Accept: image/png,*/*;q=0.5");
  101.  
  102.             client = new TcpClient(Host, 80);
  103.             Byte[] headers = System.Text.Encoding.ASCII.GetBytes(ReqHeaders);
  104.             NetworkStream ns = client.GetStream();
  105.             ns.Write(headers, 0, headers.Length);
  106.             StreamReader sr = new StreamReader(ns, Encoding.Default);
  107.             String strHTML = sr.ReadToEnd();
  108.  
  109.             string[] strParts = Regex.Split(strHTML, Environment.NewLine + Environment.NewLine);
  110.             strCookies = ParseCookies(strParts[0]);
  111.             if (strParts[0].Contains("Content-Encoding"))
  112.             {
  113.                 strParts[1] = DecompressGzip(strParts[1]);
  114.             }
  115.             return strParts[0] + Environment.NewLine + Environment.NewLine + strParts[1];
  116.         }
  117.  
  118.         public string DecompressGzip(string compressed)
  119.         {
  120.             MemoryStream memStream = new MemoryStream(System.Text.Encoding.Default.GetBytes(compressed));
  121.             GZipStream decompressStream = new GZipStream(memStream, CompressionMode.Decompress);
  122.  
  123.             byte[] endBytes = new byte[4];
  124.             int position = (int)memStream.Length - 4;
  125.             memStream.Position = position;
  126.             memStream.Read(endBytes, 0, 4);
  127.             memStream.Position = 0;
  128.             byte[] buffer = new byte[BitConverter.ToInt32(endBytes, 0) + 100];
  129.             int offset = 0;
  130.             int total = 0;
  131.             while (true)
  132.             {
  133.                 int o = decompressStream.Read(buffer, offset, 100);
  134.                 if (o == 0) break;
  135.                 offset += o;
  136.                 total += o;
  137.             }
  138.             return Encoding.ASCII.GetString(buffer);
  139.         }
  140.  
  141.         public string NeoLogin(string user, string pass, ref bool loggedIn)
  142.         {
  143.             string strHTML = null;
  144.             Request("GET", "http://neopets.com/loginpage.phtml", "http://google.com");
  145.             Pause(1);
  146.             Request("POST", "http://www.neopets.com/hi.phtml?destination=%2Fpetcentral.phtml&username=" + user, "http://neopets.com/loginpage.phtml");
  147.             Pause(1);
  148.             strHTML = Request("POST", "http://www.neopets.com/login.phtml?username=" + user + "&password=" + pass + "&destination=%2Fpetcentral.phtml", "http://neopets.com/hi.phtml");
  149.             if (strHTML.Contains("Set-Cookie: neologin="))
  150.             {
  151.                 loggedIn = true;
  152.                 return "Logged In";
  153.             }
  154.             else if (strHTML.Contains("too many times"))
  155.             {
  156.                 loggedIn = false;
  157.                 return "To Many Login Attempts";
  158.             }
  159.             else if (strHTML.Contains("badpassword"))
  160.             {
  161.                 loggedIn = false;
  162.                 return "Wrong Password";
  163.             }
  164.             else if (strHTML.Contains("frozen"))
  165.             {
  166.                 loggedIn = false;
  167.                 return "Account Frozen";
  168.             }
  169.             else if (strHTML.Contains("just a technical problem"))
  170.             {
  171.                 loggedIn = false;
  172.                 return "Neopets is down for maintenance.";
  173.             }
  174.             else
  175.             {
  176.                 loggedIn = false;
  177.                 return strHTML;
  178.             }
  179.         }
  180.  
  181.         private static void Pause(double seconds)
  182.         {
  183.             double num = seconds * 1000;
  184.             DateTime t1 = DateTime.Now, t2 = DateTime.Now;
  185.             TimeSpan tmDiff = t2 - t1;
  186.             while (Convert.ToDouble(tmDiff.TotalMilliseconds.ToString()) < num)
  187.             {
  188.                 t2 = DateTime.Now;
  189.                 tmDiff = t2 - t1;
  190.                 Application.DoEvents();
  191.             }
  192.         }
  193.  
  194.         public string StripHeaders(string strSource)
  195.         {
  196.             string[] strParts = Regex.Split(strSource, Environment.NewLine + Environment.NewLine);
  197.             return strParts[1];
  198.         }
  199.  
  200.         public Bitmap GrabPic(string strURL)
  201.         {
  202.             MemoryStream memStream = new MemoryStream(System.Text.Encoding.Default.GetBytes(StripHeaders(Request(" GET", strURL, LastPage))));
  203.             Bitmap bitmap = new Bitmap(memStream);
  204.             return bitmap;
  205.         }
  206.  
  207.         public void ClearCookies()
  208.         {
  209.             colCookies.Clear();
  210.             strCookies = null;
  211.         }
  212.  
  213.         public string ParseCookies(string Headers)
  214.         {//Credit's to Mystical for RegEx
  215.             string ParseCookies = null;
  216.             MatchCollection matches;
  217.             Regex reg = new Regex(@"Set-Cookie:\s*(.+?)=(.+?);", RegexOptions.IgnoreCase);
  218.             if (reg.IsMatch(Headers))
  219.             {
  220.                 matches = reg.Matches(Headers);
  221.                 foreach (Match m in matches)
  222.                 {
  223.                     if (colCookies.ContainsKey(m.Groups[1].ToString()))
  224.                     {
  225.                         colCookies.Remove(m.Groups[1].ToString());
  226.                         colCookies.Add(m.Groups[1].ToString(), m.Groups[1].ToString() + "=" + m.Groups[2].ToString());
  227.                     }
  228.                     else
  229.                     {
  230.                         colCookies.Add(m.Groups[1].ToString(), m.Groups[1].ToString() + "=" + m.Groups[2].ToString());
  231.                     }
  232.                 }
  233.             }
  234.             foreach (KeyValuePair<string, string> item in colCookies)
  235.             {
  236.                 ParseCookies = ParseCookies + item.Value.ToString() + "; ";
  237.             }
  238.             return ParseCookies;
  239.  
  240.         }
  241.     }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement