Я пытаюсь подключиться к www.google.com:443 через прокси SOCKS5 через TcpClient, но не понимаю, почему мой запрос CONNECT зависает при прокси.Приведенный ниже код будет работать без проблем, пока соединение не будет проксировано через SOCKS5 (если используется обычный HTTP-прокси, он будет работать нормально).
Public Sub SocksProxyTest(proxy As String, proxyPort As String)
Const host As String = "www.google.com"
Const hostPort As Integer = 443
Dim buffer As Byte() = New Byte(2047) {}
Dim bytes As Integer
Dim client As TcpClient = New TcpClient(proxy, proxyPort)
Dim stream As NetworkStream = client.GetStream()
Debug.Print("negotiating socks5 connection")
Using br = New BinaryReader(stream, Encoding.[Default], True)
Dim ip = Dns.GetHostAddresses(host)(0)
Dim auth As Byte() = New Byte() {&H5, &H1, &H0}
Debug.Print(">> " & String.Join(" ", auth.Select(Function(b) "&H" & b.ToString("X2"))))
stream.Write(auth, 0, auth.Length)
Dim result As Byte() = br.ReadBytes(2)
Debug.Print("<< &H" & result(1).ToString("X2"))
Dim data As Byte() = {&H5, &H1, &H0, &H1, &H0, &H0, &H0, &H0, get_port_bytes(hostPort)(0), get_port_bytes(hostPort)(1)}
For i As Integer = 0 To 4 - 1
data(i + 4) = ip.GetAddressBytes()(i)
Next
Debug.Print(">> " & String.Join(" ", data.Select(Function(b) "&H" & b.ToString("X2"))))
stream.Write(data, 0, data.Length)
stream.Flush()
result = br.ReadBytes(10)
Debug.Print("<< &H" & result(1).ToString("X2"))
br.Close()
End Using
Debug.Print("negotiated socks5 connection")
Debug.Print("sending tunnel request")
Dim tunnelRequest As Byte() = Encoding.UTF8.GetBytes(String.Format("CONNECT {0}:{1} HTTP/1.1{2}Host: {0}{2}{2}", host, hostPort, vbCrLf))
stream.Write(tunnelRequest, 0, tunnelRequest.Length)
stream.Flush()
bytes = stream.Read(buffer, 0, buffer.Length)
Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes))
Dim sslStream As SslStream = New SslStream(stream)
sslStream.AuthenticateAsClient(host)
Debug.Print("sending web request")
Dim request As Byte() = Encoding.UTF8.GetBytes(String.Format("GET https://{0}/ HTTP/1.1" & vbCrLf & "Host: {0}" & vbCrLf & vbCrLf, host))
sslStream.Write(request, 0, request.Length)
sslStream.Flush()
Do
bytes = sslStream.Read(buffer, 0, buffer.Length)
Debug.Print(Encoding.UTF8.GetString(buffer, 0, bytes))
Loop While bytes <> 0
client.Close()
End Sub