gpt4 book ai didi

c# - 在 C# 中创建的 Web 代理需要提示进行身份验证

转载 作者:行者123 更新时间:2023-12-03 12:06:49 25 4
gpt4 key购买 nike

我编写了一个使用 TcpListener 的小型 Web 代理程序,它在特定端口上监听 Web 调用。一旦收到调用,它就会创建一个套接字,获取调用的 header ,将其转发到请求的网页上,读取响应并将其返回给执行请求的客户端。代理很好用,但现在我想通过隐含用户名来限制谁可以通过代理访问网络。我如何将身份验证合并到我的代理上?

这是我的代码:

使用系统;
使用 System.Net;
使用 System.Net.Sockets;
使用 System.Text;
使用 System.IO;
使用 System.Threading;
使用 System.Windows.Forms;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;

命名空间 WebProxy2
{
公共(public)部分类Form1:表格
{
公共(public) bool 代理=假;

    public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

public void log(string logText)
{
Application.DoEvents();
textBox1.Text += logText + "\r\n";
StreamWriter sw = new StreamWriter(@"c:\proxyLog.txt",true);
sw.WriteLine(logText);
sw.Close();
Application.DoEvents();
}

class WebProxy2
{
Socket clientSocket;
Form1 proxyGui;
Byte[] read = new byte[1024];
Byte[] Buffer = null;
Encoding ASCII = Encoding.ASCII;
const string HTTP_VERSION = "HTTP/1.0";
const string CRLF = "\r\n";
Byte[] RecvBytes = new Byte[4096];

public WebProxy2(Socket socket, Form1 form)
{
this.clientSocket = socket;
this.proxyGui = form;
}

public void run()
{
Application.DoEvents();
String clientmessage = " ", sURL = " ";

int bytes = readmessage(read, ref clientSocket, ref clientmessage);
if (bytes == 0)
{
return;
}
int index1 = clientmessage.IndexOf(' ');
int index2 = clientmessage.IndexOf(' ', index1 + 1);
if ((index1 == -1) || (index2 == -1))
{
throw new IOException();
}
proxyGui.log("Connecting to Site: " + clientmessage.Substring(index1 + 1, index2 - index1));
proxyGui.log("Connection from " + clientSocket.RemoteEndPoint);
string part1 = clientmessage.Substring(index1 + 1, index2 - index1);
int index3 = part1.IndexOf('/', index1 + 8);
int index4 = part1.IndexOf(' ', index1 + 8);
int index5 = index4 - index3;
sURL = part1.Substring(index1 + 4, (part1.Length - index5) - 8);
try
{


IPHostEntry IPHost = Dns.Resolve(sURL);
proxyGui.log("Request resolved: " + IPHost.HostName);
string[] aliases = IPHost.Aliases;
IPAddress[] address = IPHost.AddressList;
proxyGui.log(address[0].ToString());
IPEndPoint sEndpoint = new IPEndPoint(address[0], 80);
Socket IPsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

IPsocket.Connect(sEndpoint);
if (IPsocket.Connected)
proxyGui.log("Socket connect OK");
string GET = clientmessage;
Byte[] ByteGet = ASCII.GetBytes(GET);
IPsocket.Send(ByteGet, ByteGet.Length, 0);
Int32 rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
proxyGui.log("Recieved " + rBytes);
//Buffer = RecvBytes;
String strRetPage = null;
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, rBytes);
while (rBytes > 0)
{
rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, rBytes);
}
IPsocket.Shutdown(SocketShutdown.Both);
IPsocket.Close();
sendmessage(clientSocket, strRetPage);
}
catch (Exception exc2)
{
proxyGui.log(exc2.ToString());
}
}

private int readmessage(byte[] ByteArray, ref Socket s, ref String clientmessage)
{

int bytes = s.Receive(ByteArray, 1024, 0);
string messagefromclient = Encoding.ASCII.GetString(ByteArray);
clientmessage = (String)messagefromclient;
return bytes;
}
private void sendmessage(Socket s, string message)
{
Buffer = new Byte[message.Length + 1];
int length = ASCII.GetBytes(message, 0, message.Length, Buffer, 0);
s.Send(Buffer, length, 0);
}

}

const int port = 8080;
TcpListener tcplistener = new TcpListener(port);
private void btnProxy_Click_1(object sender, EventArgs e)
{
proxyOn = true;
log("Listening on port " + port + " " + DateTime.Now.ToString());

tcplistener.Start();
while (proxyOn)
{
Application.DoEvents();
if (tcplistener.Pending())
{
Socket socket = tcplistener.AcceptSocket();
WebProxy2 webproxy = new WebProxy2(socket, this);
webproxy.run();
}
Application.DoEvents();
}
}

private void btnStop_Click_1(object sender, EventArgs e)
{
Application.DoEvents();
proxyOn = false;
tcplistener.Stop();
log("Proxy stopped !!! " + DateTime.Now.ToString());
Application.DoEvents();
}

}

}

最佳答案

不清楚您是实现了自己的 HTTP 代理协议(protocol)(或多或少是基于 Host header 的简单 TCP 重定向)还是使用标准 HTTP 代理协议(protocol)。

如果您选择标准代理(这可能更好,除非您有充分的理由不这样做),您应该可以使用 Proxy-Authenticate , Proxy-AuthorizationProxy-Authentication-Info标题而不是 WWW-Authenticate , AuthorizationAuthentication-Info (分别)例如用于 HTTP Basic 或 Digest 身份验证。

关于c# - 在 C# 中创建的 Web 代理需要提示进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3196118/

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