HTTPS代理服务器在C#中
我正在开发HTTPS代理服务器。它应该是一个控制台应用程序。 我想找一本手册或范例。我发现很多件或非工作样品。 我尝试从MSND的SSLStream示例,但未成功。 有没有人有一些经验或工作的例子?
假设您使用的是正常的HTTPS代理服务器(而不是MITM代理服务器),则根本不需要任何SSL / TLS代码。
它所需要的只是能够解释HTTP CONNECT
方法,并将流量按原样从CONNECT
请求中使用的主机和端口(例如, CONNECT host.example.org:443
)中继转发。
看看mentalis代理的源代码
http://www.mentalis.org/soft/projects/proxy/
码:
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.1rnHost: {0}rnrn", 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.1rnHost: {0}rnrn", 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();
}
}
}
;)
链接地址: http://www.djcxy.com/p/96415.html