gpt4 book ai didi

c#- HTTPS 代理

转载 作者:太空宇宙 更新时间:2023-11-03 13:37:12 28 4
gpt4 key购买 nike

我试图在 C# 中创建一个 HTTPS 代理服务器。这里有人发布了解决方案:

        string host = "encrypted.google.com";
string proxy = "127.0.0.1";//host;
int proxyPort = 8888;//443;

byte[] buffer = new byte[2048];
int bytes;

// Connect socket
TcpClient client = new TcpClient(proxy, proxyPort);
NetworkStream stream = client.GetStream();

// Establish Tcp tunnel
byte[] tunnelRequest = Encoding.UTF8.GetBytes(String.Format("CONNECT {0}:443 HTTP/1.1\r\nHost: {0}\r\n\r\n", host));
stream.Write(tunnelRequest , 0, tunnelRequest.Length);
stream.Flush();

// Read response to CONNECT request
// There should be loop that reads multiple packets
bytes = stream.Read(buffer, 0, buffer.Length);
Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));

// Wrap in SSL stream
SslStream sslStream = new SslStream(stream);
sslStream.AuthenticateAsClient(host);

// Send request
byte[] request = Encoding.UTF8.GetBytes(String.Format("GET https://{0}/ HTTP/1.1\r\nHost: {0}\r\n\r\n", host));
sslStream.Write(request, 0, request.Length);
sslStream.Flush();

// Read response
do
{
bytes = sslStream.Read(buffer, 0, buffer.Length);
Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));
} while (bytes != 0);

client.Close();
Console.ReadKey();

但是,这不是服务器的代码。它打印响应(作为纯文本),但客户端套接字(浏览器)没有收到响应。

感谢您的帮助,谢谢。

最佳答案

我不是 C# 专家,但不会将 Console.Write 替换为写操作回到原始 CONNECT 请求来自的 TCP 连接来解决这个问题吗?

看起来缺少一些代码,就像在这一切发生之前,您必须通过 TCP 连接监听客户端。例如,您正在监听 IP 地址 192.168.1.1 端口 8080。然后客户端发送类似

HTTP/1.1 www.example.com:443

然后你去做所有这些事情,然后你想要做的不是 Console.write,而是 (192.168.1.1:8080).write() 或 C# 等价物。

关于c#- HTTPS 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36199239/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com