[英]Why does Visual Studio compile my HTTP server app to run on port 5001 when I set it to 443/80?
[英]Transparent proxy - from port 80 to 443
我正在嘗試開發一個透明的代理,該代理將來自客戶端的不安全的HTTP流量作為安全的https流量轉發到服務器,然后再次返回。 為了更好地說明我的觀點,請看以下圖片。
讓我們假設由於各種原因,客戶端將僅使用HTTP,而不能將端口443用於HTTPS。 由於某些服務器不接受來自端口80的流量,因此我的代理服務器需要將它們重新路由到端口443。這是一種可能的情況:
由於這是一個透明的代理,因此客戶端(在我的情況下,其中很多)不需要任何額外的配置。 網絡已經配置好,因此他們的流量可以通過我的節點。 目前,我的節點只是簡單地重新路由數據,並在其中包含病毒的情況下阻止某些數據。 這是使用WinPcap來訪問較低的網絡層來完成的,但是如果很難使用原始數據包(主要是關於握手)來完成,我願意改變我的方法。
我嘗試過的操作:注意:www.google.com可以是網絡上的任何網站。 它僅用作示例。
開發此類代理的最佳方法是什么?
編輯:有任何方式TcpListener或HttpListener可以充當透明代理嗎? (在客戶端計算機上未配置)。 HttpListener何時准確識別出客戶端正在嘗試連接?
我真的不明白為什么您需要從SslStream
讀取加密的數據。 如果我正確閱讀了您的描述,則只需要:
NetworkStream
包裝在SslStream
和AuthenticateAsClient
SslStream
SslStream
讀取數據並將其寫入客戶端 在此過程中,我看不到您需要查看來自SslStream
的加密數據。
以下是一個非常基本的示例(盡管完全未經測試):
static void Main(string[] args)
{
var listener = new TcpListener(IPAddress.Any, 11180);
var clientConnection = listener.AcceptTcpClient();
// When we get here, the client has connected, initiate the server connection
var serverConnection = new TcpClient("your.server.name", 443);
var serverStream = serverConnection.GetStream();
var secureStream = new SslStream(serverStream);
secureStream.AuthenticateAsClient("your.server.name");
ConnectStreams(clientConnection.GetStream(), secureStream);
}
private static void ConnectStreams(Stream streamA, Stream streamB)
{
ForwardStream(streamA, streamB, new byte[1024]);
ForwardStream(streamB, streamA, new byte[1024]);
}
private static void ForwardStream(Stream source, Stream destination, byte[] buffer)
{
source.BeginRead(buffer, 0, buffer.Length, r => Forward(source, destination, r, buffer), null);
}
private static void Forward(Stream source, Stream destination, IAsyncResult asyncResult, byte[] buffer)
{
var bytesRead = source.EndRead(asyncResult);
if (bytesRead == 0)
{
destination.Close();
return;
}
destination.Write(buffer, 0, bytesRead);
ForwardStream(source, destination, buffer);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.