C#:sslStream和本地代理
![在此处输入图片描述] [1]嗨,
我们有一台接受ssl连接的服务器。假设我请求“ 0”,那么服务器首先检查我是否已登录,如果是,则它连接到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;
}
}
当我运行代码时,我得到的是我附加的输出。 (被阻止的文本显示了我公司的登录网址)
有人可以告诉我为什么浏览器没有重定向到登录页面。
谢谢
尔凡
没有找到相关结果
已邀请:
0 个回复