Advertisement
Guest User

Untitled

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