NF Auth

Discussion in 'Code Snippets and Tutorials' started by Fexxel, Mar 19, 2009.

  1. Fexxel

    Fexxel Level IV

    Joined:
    Jan 28, 2009
    Messages:
    959
    Likes Received:
    26
    So I'm using that Fix that Tharoux made, but I can't really get it to work..
    I copy+pasted the entire code and put it on the top of my code for my Avvie Grabber.
    This is the code I copy+pasted:

    Code (Text):
    1. Imports System.Text.Regularexpressions
    2. Imports System.Net
    3. Imports System.IO
    4. Imports System.Text
    5. Imports System.IO.Compression
    6. Imports System.Runtime.Serialization.Formatters.Binary
    7.  
    8. Public Class frmAuth
    9.     Dim progID = 0 'The Id of the prog in NF
    10.     Private Wrapper As New TCPWrapperAuth
    11.     Private Sub cmdQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdQuit.Click
    12.         Me.Close()
    13.     End Sub
    14.  
    15.     Private Sub cmdAuth_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAuth.Click
    16.         If txtUserAuth.Text <> "" And txtPassAuth.Text <> "" Then
    17.             cmdAuth.Enabled = False
    18.             Dim bAuth As Boolean = False
    19.             Dim bNewVersion As Boolean = False
    20.             Dim ProgName As String = "Auto-Adopter V1.0"
    21.             Dim strReturn As String = Wrapper.Request("GET", "http://www.neofriends.net/auth.php?username=" + txtUserAuth.Text + "&password=" + txtPassAuth.Text + "&program=" + progID, "http://www.neofriends.net/")
    22.             If strReturn.Contains("Not Authorized to use the program") Then
    23.                 bAuth = False
    24.             ElseIf strReturn.Contains("Authorized to use the program") Then
    25.                 bAuth = True
    26.                 If strReturn.Contains(ProgName) <> True Then
    27.                     bNewVersion = True
    28.                 End If
    29.             Else
    30.                 bAuth = False
    31.             End If
    32.             If bAuth = True Then
    33.                 If bNewVersion = True Then
    34.                     MsgBox("New version available. Please download the latest one!", MsgBoxStyle.Exclamation, "www.neofriends.net")
    35.                     cmdAuth.Enabled = True
    36.                 Else
    37.                     'Me.Hide()
    38.                     'frmMain.Show()
    39.                 End If
    40.             Else
    41.                 MsgBox("You are not authorized to use this program.", MsgBoxStyle.Exclamation, "www.neofriends.net")
    42.                 cmdAuth.Enabled = True
    43.             End If
    44.         Else
    45.             MsgBox("Username and/or password cannot be blank")
    46.         End If
    47.     End Sub
    48.  
    49.     Private Sub frmAuth_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    50.  
    51.     End Sub
    52. End Class
    53.  
    54. Public Class TCPWrapperAuth
    55.     Inherits System.Windows.Forms.UserControl
    56. #Region " Windows Form Designer generated code "
    57.     Public Sub New()
    58.         MyBase.New()
    59.         'This call is required by the Windows Form Designer.
    60.         InitializeComponent()
    61.         'Add any initialization after the InitializeComponent() call
    62.     End Sub
    63.     'UserControl overrides dispose to clean up the component list.
    64.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    65.         If disposing Then
    66.             If Not (components Is Nothing) Then
    67.                 components.Dispose()
    68.             End If
    69.         End If
    70.         MyBase.Dispose(disposing)
    71.     End Sub
    72.     'Required by the Windows Form Designer
    73.     Private components As System.ComponentModel.IContainer
    74.     'NOTE: The following procedure is required by the Windows Form Designer
    75.     'It can be modified using the Windows Form Designer.
    76.     'Do not modify it using the code editor.
    77.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    78.         components = New System.ComponentModel.Container
    79.     End Sub
    80. #End Region
    81.     Private colCookies As New Collection
    82.     Dim strCookies As String
    83.     Public LastPage As String
    84.     Public bGzip As Boolean
    85.     Public Function Request(ByVal Method As String, ByVal URL As String, ByVal Referer As String) As String
    86.         Dim Host As String = Nothing
    87.         Dim strFile As String = Nothing
    88.         Dim strPost As String = Nothing
    89.         Dim pos As Integer = 0
    90.         If Referer Is Nothing Then
    91.             Referer = LastPage
    92.         End If
    93.         If URL.Contains("http://") Then
    94.             Host = URL.Substring(7)
    95.         Else
    96.             Host = URL
    97.         End If
    98.         If Host.Contains("/") Then
    99.             pos = Host.IndexOf("/", 0)
    100.             strFile = Host.Substring(pos)
    101.             Host = Host.Substring(0, pos)
    102.         Else
    103.             strFile = "/"
    104.         End If
    105.         If Method = "POST" Then
    106.             pos = strFile.IndexOf("?")
    107.             If Not (pos = -1) Then
    108.                 strPost = strFile.Substring(pos + 1)
    109.                 strFile = strFile.Substring(0, pos)
    110.             Else
    111.                 strPost = Nothing
    112.             End If
    113.         End If
    114.         LastPage = URL
    115.         Dim ReqHeaders As String = Nothing
    116.         If bGzip = True Then
    117.             If Method = "GET" OrElse Method = "PIC" Then
    118.                 ReqHeaders = "GET" + " " + strFile + " HTTP/1.1" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Host: " + Host + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Language: en-us,en;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Encoding: gzip, deflate" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Keep-Alive: 300" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Connection: keep-alive" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Referer: " + Referer + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Cookie: " + strCookies + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & ""
    119.             Else
    120.                 ReqHeaders = "POST " + strFile + " HTTP/1.1" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Host: " + Host + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Language: en-us,en;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Encoding: gzip, deflate" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Keep-Alive: 300" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Connection: keep-alive" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Referer: " + Referer + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Cookie: " + strCookies + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Content-Type: application/x-www-form-urlencoded" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Content-Length: " + strPost.Length.ToString + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Connection: close" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + strPost
    121.             End If
    122.             If Method = "PIC" Then
    123.                 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")
    124.             End If
    125.         Else
    126.             If Method = "GET" OrElse Method = "PIC" Then
    127.                 ReqHeaders = "GET" + " " + strFile + " HTTP/1.1" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Host: " + Host + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Language: en-us,en;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" & "" + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Keep-Alive: 300" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Connection: keep-alive" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Referer: " + Referer + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Cookie: " + strCookies + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & ""
    128.             Else
    129.                 ReqHeaders = "POST " + strFile + " HTTP/1.1" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Host: " + Host + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Language: en-us,en;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Keep-Alive: 300" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Connection: keep-alive" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Referer: " + Referer + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Cookie: " + strCookies + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Content-Type: application/x-www-form-urlencoded" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Content-Length: " + strPost.Length.ToString + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Connection: close" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + strPost
    130.             End If
    131.             If Method = "PIC" Then
    132.                 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")
    133.             End If
    134.         End If
    135.  
    136.         Dim tcp As New System.Net.Sockets.TcpClient
    137.         Dim netstream As System.Net.Sockets.NetworkStream
    138.         Dim TN(1) As Long
    139.         If Referer = "" Then Referer = LastPage
    140.         If InStr(1, URL, "http://") <> 0 Then Host = Mid$(URL, 8)
    141.         If InStr(1, Host, "/") <> 0 Then Host = Mid$(Host, 1, InStr(1, Host, "/") - 1)
    142.         If InStr(1, Host, "?") <> 0 Then Host = Mid$(Host, 1, InStr(1, Host, "?") - 1)
    143.         If Mid$(Host, 1, 4) = "www." Then Host = Mid$(Host, 5)
    144.         LastPage = URL
    145.         Try
    146.             tcp.Connect(Host, 80)
    147.         Catch ex As Exception
    148.             Return ex.Message
    149.         End Try
    150.  
    151.         Dim sendbytes As Byte()
    152.         sendbytes = System.Text.Encoding.ASCII.GetBytes(ReqHeaders)
    153.         netstream = tcp.GetStream()
    154.         netstream.Write(sendbytes, 0, sendbytes.Length)
    155.         Dim sr As StreamReader = New StreamReader(netstream, Encoding.Default)
    156.         Dim strHTML As String = sr.ReadToEnd
    157.         Dim strParts As String() = Regex.Split(strHTML, Environment.NewLine + Environment.NewLine)
    158.         strCookies = ParseCookies(strParts(0))
    159.         If strParts(0).Contains("Content-Encoding") Then
    160.             strParts(1) = DecompressGzip(strParts(1))
    161.         End If
    162.         Return strParts(1)
    163.     End Function
    164.     Public Function DecompressGzip(ByVal compressed As String) As String
    165.         Dim memStream As MemoryStream = New MemoryStream(System.Text.Encoding.Default.GetBytes(compressed))
    166.         Dim decompressStream As GZipStream = New GZipStream(memStream, CompressionMode.Decompress)
    167.         Dim endBytes(4) As Byte
    168.         Dim position As Integer = CType(memStream.Length, Integer) - 4
    169.         memStream.Position = position
    170.         memStream.Read(endBytes, 0, 4)
    171.         memStream.Position = 0
    172.         Dim buffer(BitConverter.ToInt32(endBytes, 0) + 100) As Byte
    173.         Dim offset As Integer = 0
    174.         Dim total As Integer = 0
    175.         While True
    176.             Dim bytesRead As Integer = decompressStream.Read(buffer, offset, 100)
    177.             If bytesRead = 0 Then
    178.                 Exit While
    179.             End If
    180.             offset += bytesRead
    181.             total += bytesRead
    182.         End While
    183.         Return Encoding.ASCII.GetString(buffer)
    184.     End Function
    185.     Public Function ParseCookies(ByVal Headers As String) As String
    186.         ParseCookies = ""
    187.         Dim reg As Regex
    188.         Dim matches As MatchCollection
    189.         Dim match As Match
    190.         reg = New Regex("set-cookie:\s*([^=]+)=([^;]+);", RegexOptions.IgnoreCase)
    191.         If reg.IsMatch(Headers) Then
    192.             matches = reg.Matches(Headers)
    193.             For Each match In matches
    194.                 Try
    195.                     colCookies.Add(match.Groups(1).ToString & "=" & match.Groups(2).ToString, match.Groups(1).ToString)
    196.                 Catch ex As Exception
    197.                     colCookies.Remove(match.Groups(1).ToString)
    198.                     colCookies.Add(match.Groups(1).ToString & "=" & match.Groups(2).ToString, match.Groups(1).ToString)
    199.                 End Try
    200.             Next
    201.         End If
    202.         Dim i As Long
    203.         For i = 1 To colCookies.Count Step 1
    204.             ParseCookies = ParseCookies & colCookies.Item(i).ToString & ";"
    205.         Next
    206.     End Function
    207.     Public Function StripHeaders(ByVal strSource As String) As String
    208.         Dim strParts() As String = Regex.Split(strSource, Environment.NewLine + Environment.NewLine)
    209.         Return strParts(1)
    210.     End Function
    211.     Private Shared Sub Pause(ByVal seconds As Double)
    212.         Dim num As Double = seconds * 1000
    213.         Dim t1 As DateTime = DateTime.Now
    214.         Dim t2 As DateTime = DateTime.Now
    215.         Dim tmDiff As TimeSpan = t2 - t1
    216.         While Convert.ToDouble(tmDiff.TotalMilliseconds.ToString) < num
    217.             t2 = DateTime.Now
    218.             tmDiff = t2 - t1
    219.             Application.DoEvents()
    220.         End While
    221.     End Sub
    222.     Public Function GrabPic(ByVal strURL As String) As System.Drawing.Image
    223.         Dim memStream As New MemoryStream(System.Text.Encoding.Default.GetBytes(StripHeaders(Request("GET", strURL, LastPage))))
    224.         GrabPic = Image.FromStream(memStream)
    225.         Return GrabPic
    226.     End Function
    227.     Public Function DownloadImage(ByVal URL As String) As System.Drawing.Image
    228.         Dim imageFile As New WebClient
    229.         Dim bytes() As Byte = imageFile.DownloadData(URL)
    230.  
    231.         Dim imageStream As New MemoryStream(bytes)
    232.         Dim downloadedImage As System.Drawing.Image = Image.FromStream(imageStream)
    233.  
    234.         DownloadImage = downloadedImage
    235.     End Function
    236.     Public Sub ClearCookies()
    237.         colCookies.Clear()
    238.         strCookies = Nothing
    239.     End Sub
    240. End Class
    I realized that I have things like 2 imports and stuff.
    So I got 23 errors, which is a problem.
    Any ideas?