C# 重定向所有网络流量

C# 重定向所有网络流量,c#,network-programming,C#,Network Programming,可能重复: 我正在编写一个代理应用程序,我想处理所有网络连接。 我已经对服务器和客户端应用程序进行了编程,所以现在我只需要将网络连接重定向到它 我搜索了很多,我发现了: //For sniffing the socket to capture the packets has to be a raw socket, with the //address family being of type internetwork, and protocol being IP mainSocket = ne

可能重复:

我正在编写一个代理应用程序,我想处理所有网络连接。 我已经对服务器和客户端应用程序进行了编程,所以现在我只需要将网络连接重定向到它

我搜索了很多,我发现了:

//For sniffing the socket to capture the packets has to be a raw socket, with the
//address family being of type internetwork, and protocol being IP
mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);

//Bind the socket to the selected IP address
mainSocket.Bind(new IPEndPoint(IPAddress.Parse(cmbInterfaces.Text), 0));

//Set the socket  options
mainSocket.SetSocketOption(SocketOptionLevel.IP,    //Applies only to IP packets
SocketOptionName.HeaderIncluded,                    //Set the include the header
true);                                              //option to true

byte[] byTrue = new byte[4] {1, 0, 0, 0};
byte[] byOut = new byte[4]{1, 0, 0, 0}; //Capture outgoing packets

我不确定这是否对我有帮助,因为我不能取消最初的请求。要做到这一点,最简单的方法是什么?

要透明地拦截所有网络流量,唯一的方法是使用驱动程序,而在c#中,由于明显的原因,这是无法做到的。也就是说,它可能被用作内核模式组件,并用c#编写应用程序逻辑。winpcap已经有了一些可供使用的书面文档,尽管我还没有使用过,所以无法说明支持的稳定性和/或完整性。

不,我已经这样做了,这不是问题所在,我不想配置浏览器向我的应用程序发送流量,我不想缓存所有连接。因此,这就是如何在与C#相关的: