How to use Proxy with TcpClient.ConnectAsync()?

后端 未结 2 938
不思量自难忘°
不思量自难忘° 2021-01-03 13:09

HTTP proxy support in .NET does not actually support the lower level classes like TcpClient or Socket. But I want to connect a TCPServer (ip, port) through HTTP proxy that s

相关标签:
2条回答
  • 2021-01-03 13:36

    I find a solution base on .NET: Connecting a TcpClient through an HTTP proxy with authentication and Bypass the proxy using TcpClient

    TcpClient _client;
    NetworkStream _stream;
    
    public TcpClient ProxyTcpClient(string targetHost, int targetPort, string httpProxyHost, int httpProxyPort, string proxyUserName, string proxyPassword)
    {
            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 async Task<bool> ConnectAsync(string hostname, int port)
    {
            _client = ProxyTcpClient("IPTargetHost", 1234, "IPProxyHost", 5678, "Userproxy", "Userppwd");
            _stream = conn._client.GetStream();
    
            ..... Do some stuff
    
            // Connexion OK
            return true;
    }
    
    0 讨论(0)
  • 2021-01-03 13:38

    We have managed to implement it using .Net's Socket. Nuget package is called Filemail.ProxiedTcpClient. The code is pretty simple:

    public static TcpClient CreateProxied(Uri proxy, Uri destination)
    {
        var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    
        socket.Connect(proxy.Host, proxy.Port);
    
        var connectMessage = Encoding.UTF8.GetBytes($"CONNECT {destination.Host}:{destination.Port} HTTP/1.1{Environment.NewLine}{Environment.NewLine}");
        socket.Send(connectMessage);
    
        byte[] receiveBuffer = new byte[1024];
        var received = socket.Receive(receiveBuffer);
    
        var response = ASCIIEncoding.ASCII.GetString(receiveBuffer, 0, received);
    
        if (!response.Contains("200 OK"))
        {
            throw new Exception($"Error connecting to proxy server {destination.Host}:{destination.Port}. Response: {response}");
        }
    
        return new TcpClient
        {
            Client = socket
        };
    }
    

    Contribute here: https://github.com/filemail/ProxiedTcpClient

    0 讨论(0)
提交回复
热议问题