问题
![enter image description here][1]Hi,
We have a server which accepts ssl connection. Say if I request for https://gmail.com
then the server first checks if i have logged in to it, if yes then it connects to gmail and sends the response back. If I have not logged in then it redirects to our login page.
my requirement is the following:
I have to write a TCPListener
which listens to the local proxy (say 127.0.0.1:13000).
When ever a request comes from browser, I have to create a ssl connection to our server and pass the request to it.
The server will either reply with login url or success Then I should do the same with the browser.
The Code I have written is as follows:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Collections.Generic;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
class MyTcpListener
{
public static void Main()
{
TcpListener server = null;
try
{
// Set the TcpListener on port 13000.
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Buffer for reading data
Byte[] bytes1 = new Byte[256];
String data1 = null;
string serverName = "xxxxxxxxxxxxxxxx";//This is the server of my company
TcpClient sslClient = new TcpClient();
sslClient.Connect(serverName, 8080);
SslStream sslStream = new SslStream(sslClient.GetStream(), false, new RemoteCertificateValidationCallback(CertificateValidationCallback));
sslStream.AuthenticateAsClient(serverName);
// Enter the listening loop.
while (true)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
int j;
bool firstTime = true;
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
Console.WriteLine("----------------------------------------------");
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
byte[] msg1 = null;
sslStream.Write(msg, 0, msg.Length);
Console.WriteLine("written to sslchannel: {0}", msg.Length);
if (firstTime)
{
j = sslStream.Read(bytes1, 0, bytes1.Length);
firstTime = false;
// Translate data bytes to a ASCII string.
data1 = System.Text.Encoding.ASCII.GetString(bytes1, 0, j);
Console.WriteLine("Received from ssl: {0}", data1);
Console.WriteLine("----------------------------------------------");
// Process the data sent by the client.
data1 = data1.ToUpper();
msg1 = System.Text.Encoding.ASCII.GetBytes(data1);
Console.WriteLine("Sent from ssl: {0}", data1);
Console.WriteLine("----------------------------------------------");
sslStream.Write(msg1, 0, msg1.Length);
// stream.Write(msg1, 0, msg1.Length);
}
// Send back a response.
stream.Write(msg1, 0, msg1.Length);
Console.WriteLine("Sent: {0}", data);
Console.WriteLine("----------------------------------------------");
}
Console.WriteLine("sslStream Block");
// Shutdown and end connection
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
static bool CertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors != SslPolicyErrors.None)
{
Console.WriteLine("SSL Certificate Validation Error!");
Console.WriteLine(sslPolicyErrors.ToString());
return false;
}
else
return true;
}
}
When I run the code I get the output that I have attached. (The blocked text shows the login url of my company)
Can someone tell me why the browser is not redirecting to login page.
Thanks Irfan
来源:https://stackoverflow.com/questions/6356070/c-sslstream-and-local-proxy