我写了MITM HTTPS代理。但是,当我尝试AuthenticateAsServer时,它停止并在几次程序写入“连接已关闭”之后。这可能有什么问题?
static readonly TcpListener Listener = new TcpListener(IPAddress.Any, 4502);
Listener.Start();
var client = Listener.AcceptTcpClient();
Socket client = (Socket)sclient.Client;
NetworkStream ns = new NetworkStream(client);
byte[] buffer = new byte[BUFFER_SIZE];
int clientBytes = 0;
string data = "";
do
{
clientBytes = ns.Read(buffer, 0, BUFFER_SIZE);
data += Encoding.ASCII.GetString(buffer, 0, clientBytes);
} while (clientBytes == BUFFER_SIZE);
Console.WriteLine(ASCIIEncoding.ASCII.GetString(buffer));
var clientStream = new SslStream(sclient.GetStream(), false, SslValidationCallback, null);
clientStream.AuthenticateAsServer(cert, false, System.Security.Authentication.SslProtocols.Default, false);
这里的证书是下一步创建的
makecert -n CN=MyCA -cy authority -a sha1 -sv “MyCA.pvk” -r MyCA.cer
certmgr -add -all -c MyCA.cer -s -r LocalMachine Root
makecert -n CN=*mail.ru -ic MyCA.cer -iv MyCA.pvk -a sha1 -sky exchange -pe -sr currentuser -ss my SslServer.cer