HTTPS Proxy server in C#
我正在使用HTTPS代理服务器。它应该是一个控制台应用程序。我想找一本手册或是一个例子。我发现了很多零件或是非工作样品。我尝试使用MSND中的SSL流示例,但没有成功。有没有人有经验或工作榜样?
假设你在一个正常的HTPS代理服务器(不是MITM代理服务器),你不需要任何SSL/TLS代码。
所有这一切都需要能够解释http://EDOCX1>〔0〕方法和从
看心理代理源代码http://www.mentalis.org/soft/projects/proxy/。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | using System; using System.Text; using System.Net.Sockets; using System.Net.Security; namespace SslTcpClient { public class SslTcpClient { public static void Main(string[] args) { string host ="encrypted.google.com"; string proxy ="127.0.0.1";//host; int proxyPort = 8888;//443; byte[] buffer = new byte[2048]; int bytes; // Connect socket TcpClient client = new TcpClient(proxy, proxyPort); NetworkStream stream = client.GetStream(); // Establish Tcp tunnel byte[] tunnelRequest = Encoding.UTF8.GetBytes(String.Format("CONNECT {0}:443 HTTP/1.1 Host: {0} ", host)); stream.Write(tunnelRequest , 0, tunnelRequest.Length); stream.Flush(); // Read response to CONNECT request // There should be loop that reads multiple packets bytes = stream.Read(buffer, 0, buffer.Length); Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes)); // Wrap in SSL stream SslStream sslStream = new SslStream(stream); sslStream.AuthenticateAsClient(host); // Send request byte[] request = Encoding.UTF8.GetBytes(String.Format("GET https://{0}/ HTTP/1.1 Host: {0} ", host)); sslStream.Write(request, 0, request.Length); sslStream.Flush(); // Read response do { bytes = sslStream.Read(buffer, 0, buffer.Length); Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes)); } while (bytes != 0); client.Close(); Console.ReadKey(); } } } |
Distr.