![在此输入图像说明] [1]嗨,
我们有一台接受ssl连接的服务器。假如我请求https://gmail.com
,那么服务器首先检查我是否已登录到它,如果是,则它连接到gmail并发回响应。如果我还没有登录,那么它会重定向到我们的登录页面。
我的要求如下:
我必须写一个TCPListener
来监听本地代理(比如127.0.0.1:13000)。
当请求来自浏览器时,我必须创建一个到我们服务器的ssl连接并将请求传递给它。
服务器将以登录URL或成功回复 然后我应该对浏览器做同样的事情。
我编写的守则如下:
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;
}
}
当我运行代码时,我得到了我附加的输出。 (被阻止的文本显示我公司的登录网址)
有人可以告诉我为什么浏览器没有重定向到登录页面。
由于 伊尔凡