M
M
martensit2019-09-01 15:56:59
C++ / C#
martensit, 2019-09-01 15:56:59

How to properly run AuthenticateAsClient through http proxy?

var client = new TcpClient("192.168.56.2", 3128);
var stream = new SslStream(client.GetStream());
stream.AuthenticateAsClient("2ip.ru");
string request = "GET https://2ip.ru HTTP/1.1" + Environment.NewLine +
"Host: 2ip.ru" + Environment.NewLine +
"Connection: keep-alive" + Environment.NewLine + Environment.NewLine;
byte[] requestBytes = Encoding.ASCII.GetBytes(request);
stream.Write(requestBytes, 0, requestBytes.Length);
byte[] buffer = new byte[2048];
int bytes;
do
{
    bytes = stream.Read(buffer, 0, buffer.Length);
    Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));
} while (bytes != 0);

Gets stuck on AuthenticateAsClient just right.
If you ask ("2ip.ru", 443) instead of ("192.168.56.2", 3128), he answers normally.
AuthenticateAsClient through a proxy needs to be organized correctly somehow

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
martensit, 2019-09-02
@martensit

public static TcpClient ProxyTcpClient(string targetHost, int targetPort, string httpProxyHost, int httpProxyPort)
{
    const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Instance;
    Uri proxyUri = new UriBuilder
    {
        Scheme = Uri.UriSchemeHttp,
        Host = httpProxyHost,
        Port = httpProxyPort
    }.Uri;
    Uri targetUri = new UriBuilder
    {
        Scheme = Uri.UriSchemeHttp,
        Host = targetHost,
        Port = targetPort
    }.Uri;
    WebProxy webProxy = new WebProxy(proxyUri, true);
    //webProxy.Credentials = new NetworkCredential(proxyUserName, proxyPassword);
    WebRequest request = WebRequest.Create(targetUri);
    request.Proxy = webProxy;
    request.Method = "CONNECT";
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream responseStream = response.GetResponseStream();
    Type responseType = responseStream.GetType();
    PropertyInfo connectionProperty = responseType.GetProperty("Connection", Flags);
    var connection = connectionProperty.GetValue(responseStream, null);
    Type connectionType = connection.GetType();
    PropertyInfo networkStreamProperty = connectionType.GetProperty("NetworkStream", Flags);
    NetworkStream networkStream = (NetworkStream)networkStreamProperty.GetValue(connection, null);
    Type nsType = networkStream.GetType();
    PropertyInfo socketProperty = nsType.GetProperty("Socket", Flags);
    Socket socket = (Socket)socketProperty.GetValue(networkStream, null);
    return new TcpClient { Client = socket };
}

public static void Connect()
{
    TcpClient client = ProxyTcpClient("2ip.ru", 443, "192.168.56.2", 3128);
    var stream = new SslStream(client.GetStream());
    stream.AuthenticateAsClient("2ip.ru");
    string request = "GET https://2ip.ru HTTP/1.1" + Environment.NewLine +
    "Host: 2ip.ru" + Environment.NewLine +
    "Connection: keep-alive" + Environment.NewLine + Environment.NewLine;
    byte[] requestBytes = Encoding.ASCII.GetBytes(request);
    stream.Write(requestBytes, 0, requestBytes.Length);
    stream.Flush();
    var reader = new StreamReader(stream, Encoding.UTF8);
}

Here is the solution

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question