.NET原生应用相当于修改标题的Firefox插件的插件、标题、NET、Firefox

2023-09-04 02:50:40 作者:毕竟我那么帅

我想开发C#中的袜子隧道应用程序,它能够通过使用添加和修改后的头请求(类似的修改标题的Firefox插件)和隧道通过代理(袜子preferable数据)。请任何人都可以指定我可能需要这个任何资源?或者有什么替代方案,可以实现同样的功能,开源,我可以建立在也许,等谢谢!

I am trying to develop a socks tunneling application in c# that is able to open a website by using add and modified header requests (something similar to Modify Header Firefox Addon) and tunnel the data through a proxy (socks preferable). Please can anyone specify any resources I might need for this? Or any alternative that can perform the same function, open source that I can build on maybe, etc. Thanks!

PS:该应用程序也应当是能够打开的https和其他常见的网络协议

ps: the applications should also be able to open https and other common network protocols

推荐答案

一种方法是使用 HttpSys ,并创建一个本地代理服务器来处理请求的环回 127.0.0.1 地址。你会改变系统代理到这个地址/端口,客户端和服务器之间坐。

One way is to use HttpSys and create a local proxy server to handle request on the loopback 127.0.0.1 address. You would be change the system proxy to this address/port and sit between client and server.

这将允许你修改请求/响应包和放大器;头。这里有一个例如这里在C#这种方法,而且我已经修正这个下面展示它是如何工作的。

This would allow you to modify request/response packets & headers. There's an example here of this approach in C#, and I've amended this below to show how it would work.

public class MyProxy
{
    private readonly HttpListener listener;

    public MyProxy()
    {
        listener = new HttpListener();
    }

    public void Start()
    {
        listener.Prefixes.Add("http://*:8888/");
        listener.Prefixes.Add("https://*:8889/");
        listener.Start();
        Console.WriteLine("Proxy started, hit enter to stop");
        listener.BeginGetContext(GetContextCallback, null);
        Console.ReadLine();
        listener.Stop();
    }

    public void GetContextCallback(IAsyncResult result)
    {
        var context = listener.EndGetContext(result);
        listener.BeginGetContext(GetContextCallback, null);

        var request = context.Request;
        var response = context.Response;
        var url = request.Url;

        UriBuilder builder = new UriBuilder(url);
        builder.Port = url.Port == 8888 ? 80 : 443;
        url = builder.Uri;

        WebRequest webRequest = WebRequest.Create(url);

        webRequest.Proxy = GlobalProxySelection.GetEmptyWebProxy();
        WebResponse webResponse = webRequest.GetResponse();
        using (Stream reader = webResponse.GetResponseStream())
        {
            using (Stream writer = response.OutputStream)
            {
                reader.CopyTo(writer);
            }
        }
    }
}

这样做的缺点的方法是这是相当低的水平,并且影响用户机,它可能不是所希望的所有流量。你将不得不处理SSL请求,并且它也将影响任何现有的配置的代理。

The downside to this approach is it's quite low level, and affects all traffic on the user machine which may not be desirable. You would have to handle SSL requests, and it would also impact any existing configured proxies.

另一种方法是使用 Microsoft Internet控制 COM组件,延长 web浏览器类。有一个SO question这里,显示方式。不幸的是 web浏览器在.NET命名空间中的版本不执行请求响应的对象。有趣的位下方。

Another alternative approach is to use the Microsoft Internet Controls COM component and to extend the WebBrowser class. There's a SO question here that shows the approach. Unfortunately the version of WebBrowser in the .NET namespace does not implement the request response objects. The interesting bits are below.

public class ExtendedWebBrowser : WebBrowser
{
    ...

    void BeforeNavigate(object pDisp, ref object url, ref object flags,
                       ref object targetFrameName, ref object postData, 
                       ref object headers, ref bool cancel)
    {
         if (!headers.Contains("X-RequestFlag")
         {
             headers += "X-RequestFlag: true\r\n";

             // append custom header here

             // cancel current request
             cancel = true;

             // re-request with amended details
             Navigate((string)url, (string)targetFrameName, (byte[])postData, 
                      (string)headers);
         }
         else
         {
             base.BeforeNavigate(...);
         }
    }
}