I'm working on my own proxy, and I have problems with https. Connecting to HTTP sites through my proxy server is working, but if I try to connect to https, i get error. Explain me how i must do it better please
// HTTPS
if (http.Method == HTTP.Parser.MethodsList.CONNECT)
{
TcpClient client = new TcpClient(http.Host, 443);
Console.WriteLine("Client connected.");
// Create an SSL stream that will close the client's stream.
SslStream sslStream = new SslStream(
client.GetStream(),
false,
new RemoteCertificateValidationCallback(ValidateServerCertificate),
null
);
// The server name must match the name on the server certificate.
try
{
sslStream.AuthenticateAsClient(http.Host);
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}", e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
}
Console.WriteLine("Authentication failed - closing the connection.");
client.Close();
return;
}
// Encode a test message into a byte array.
// Signal the end of the message using the "<EOF>".
// Send hello message to the server.
sslStream.Write(httpRequest);
sslStream.Flush();
// sslStream.Read(ans, 0,Convert.ToInt32(sslStream.Length));
// Read message from the server.
string serverMessage = ReadMessage(sslStream);
byte[] ans = Encoding.UTF8.GetBytes(serverMessage);
Console.WriteLine("Server says: {0}", serverMessage);
HTTP.Parser httpResponse = new HTTP.Parser(ans);
myClient.Send(httpResponse.Source, httpResponse.Source.Length, SocketFlags.None);
// Close the client connection.
client.Close();
myClient.Close();
Console.WriteLine("Client closed.");
} // HTTP.Parser.MethodsList.CONNECT
else //http
{
// rerouting request to host
using (Socket myRerouting = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
myRerouting.Connect(http.Host,http.Port);
if (myRerouting.Send(httpRequest, httpRequest.Length, SocketFlags.None) != httpRequest.Length)
{
WriteLog("Cant send data to {0} ...", http.Host);
}
else
{
// Get response
HTTP.Parser httpResponse = new HTTP.Parser(ReadToEnd(myRerouting));
if (httpResponse.Source != null && httpResponse.Source.Length > 0)
{
response = httpResponse.Source;
}
else
{
WriteLog("Response is empty");
}
}
myRerouting.Close();
}
} // HTTP.Parser.MethodsList.CONNECT
// Send response to client
if (response != null)
myClient.Send(response, response.Length, SocketFlags.None);
}
}
Aucun commentaire:
Enregistrer un commentaire