.NET Native 应用程序相当于修改标头 Firefox Addon

发布于 2024-12-02 17:00:46 字数 279 浏览 1 评论 0原文

我正在尝试用 C# 开发一个袜子隧道应用程序,该应用程序能够通过使用添加和修改的标头请求来打开网站(类似于 修改标头 Firefox 插件)并通过代理(最好是袜子)传输数据。请问有人可以指定我可能需要的任何资源吗?或者任何可以执行相同功能的替代方案,我可以构建的开源,等等。谢谢!

ps:应用程序还应该能够打开https和其他常见的网络协议

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: the applications should also be able to open https and other common network protocols

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人事已非 2024-12-09 17:00:46

一种方法是使用 HttpSys 并创建本地代理服务器来处理环回 127.0.0.1 地址上的请求。您可以将系统代理更改为该地址/端口,并位于客户端和服务器之间。

这将允许您修改请求/响应数据包和标头。 这里有一个 C# 方法的示例,我下面对此进行了修改以展示其工作原理。

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 请求,并且它还会影响任何现有的已配置代理。

另一种替代方法是使用 Microsoft Internet Controls COM 组件并扩展 WebBrowser 类。这里有一个问题展示了这种方法。不幸的是,.NET 命名空间中的 WebBrowser 版本没有实现请求响应对象。有趣的部分如下。

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(...);
         }
    }
}

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.

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);
            }
        }
    }
}

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.

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(...);
         }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文