• 用.NET代码扩展Fiddler[转]


    转自:http://www.cnblogs.com/AlexLiu/archive/2008/11/25/1340832.html


    介绍

    如何在.net的代码中扩展Fiddler

    Fiddler2 是一个多方面的通过脚本和.net代码扩展的。通过扩展的机制,你能够加到FiddlerUI上去,不假思索的修改请求和回应,并且自定义观察使得场景特效表现,手动的修改请求和回应。

    预先的准备

    Fiddler2扩展需要vs2005或者2.0以上的Framework命令行编译。而且必须安装最新的Fiddler2才可以。如果你从前已经开发过了,你可能需要阅读下Updating Fiddler Extensions for v2.1.

    设置你的扩展assemblies路径

    Fiddler 的扩展在这个%Program Files%\Fiddler2\Scripts and %USERPROFILE%\My Documents\Fiddler2\Scripts 文件夹读取DLL.  安装到%Program Files%位置才能是这个机器的所用用户使用扩展,%file% 文件加的话就只能是当前的用户使用了。

    设置 Fiddler.RequiredVersion 属性 在你的AssemblyInfo.cs 文件中 (或者代码的其它地方), 如下:


    using Fiddler;

    // This Assembly requires Fiddler v2.1.0.1 or later because it uses types introduced in v2.1.
    [assembly: Fiddler.RequiredVersion("2.1.0.1")]

    如果Fiddler 发现 RequiredVersion 属性 that 暗示了需要新的版本, 用户就必须使用比这个更新的一个Fiddler版本了。Assemblies 没有包涵一个 RequiredVersion 属性的话就默认的忽略了.

    IFiddlerExtension 接口

    Public classes in your assembly that implement the IFiddlerExtension interface will be loaded by Fiddler during startup.


    public interface IFiddlerExtension
    {
      // Called when Fiddler User Interface is fully available
      void OnLoad();

      // Called when Fiddler is shutting down
      void OnBeforeUnload();
    }

    OnLoad 函数将要被调用, Fiddler 读取UI完全OK了之后,这么看来,你可以安全的加菜单项,记录项,或者其他的元素到你的UI上面了。

    OnBeforeUnload
    函数在Fiddler关闭并且卸载了所有的扩展的时候将被调用。

    IAutoTamper 接口

    实现了IAutoTamper 接口的那些(继承了IFiddlerExtension 接口) 将被每一个 HTTP/HTTPS 发送和接收请求调用, 可修改, 记录, 或者其他的操作。 


    public interface IAutoTamper : IFiddlerExtension
    {
      // Called before the user can edit a request using the Fiddler Inspectors
      void AutoTamperRequestBefore(Session oSession);

      // Called after the user has had the chance to edit the request using the Fiddler Inspectors, but before the request is sent
      void AutoTamperRequestAfter(Session oSession);

      // Called before the user can edit a response using the Fiddler Inspectors
      void AutoTamperResponseBefore(Session oSession);

      // Called before the user can edit a response using the Fiddler Inspectors
      void AutoTamperResponseAfter(Session oSession);

      // Called Fiddler returns a self-generated HTTP error (for instance DNS lookup failed, etc)
      void OnBeforeReturningError(Session oSession);
    }

    IHandleExecAction 接口

    实现了IHandleExecAction 接口的扩展会在往QuickExec box写入指令的时候被调用如你你希望你的扩展能够对指令做出反映的话(并且阻止进一步的扩展处理的话) 从这个方法返回 true


    public interface IHandleExecAction
    {
      // return TRUE if handled.
      bool OnExecAction(string sCommand);
    }

    注意Fiddler.Utilities 类包括了一个帮助函数, 你会在解释指令的时候发现他非常的有用
    [CodeDescription("Tokenize a string into tokens. Delimits on whitespace; Quotation marks are dropped unless preceded by a \ character.")]
    public static string[] Parameterize(string sCommand)

    一步一步学习样例

    这有一个琐碎个扩展示例。它修改了发送请求的User-Agent字符串。

    1. 启动 Visual Studio 2005.
    2. 建立一个Visual C# Class Library
    3. 右键点击References 文件夹。
    4. 选择Browse tab 并且在这个文件夹下C:\Program Files\Fiddler2 找到Fiddler.exe
    5. 单击 Ok 加到引用当中去。
    6. 如果你打算增加Fiddler'sUI
      1. 再次右键点击 References 属性文件夹 Solution Explorer again
      2. .NET tab, 选择 System.Windows.Winforms.
      3. 单击 Ok 加到引用中去。
    7. Solution Explorer, 右键点项目选择属性。
    8. Build Events tab中, 加入如下的事件命令行 Post-build event command line:

          copy $(TargetPath) "%userprofile%\My Documents\Fiddler2\Scripts\$(TargetFilename)"
    9. 修改默认的class1.cs (活着建立一个新的clsss) 在你的工程中,如下:

    using System;
    using System.Windows.Forms;
    using Fiddler;

    [assembly: Fiddler.RequiredVersion("2.1.8.1")]

    public class Violin : IAutoTamper       // Make sure the class is public, or Fiddler won't find it!
    {
      string sUserAgent = "";

      public Violin(){
      /* NOTE: It's possible that Fiddler UI isn't fully loaded yet, so don't add any UI in the constructor.

         But it's also possible that AutoTamper* methods are called before OnLoad (below), so be
         sure any needed data structures are initialized to safe values here in this constructor */
       
        string sUserAgent = "Violin";
      }

      public void OnLoad(){ /* Load your UI here */ }
      public void OnBeforeUnload() { }

      public void AutoTamperRequestBefore(Session oSession){
        oSession.oRequest["User-Agent"] = sUserAgent;
      }
      public void AutoTamperRequestAfter(Session oSession){}
      public void AutoTamperResponseBefore(Session oSession){}
      public void AutoTamperResponseAfter(Session oSession){}
      public void OnBeforeReturningError(Session oSession){}
    }

    在你的Extension's tab加入一个图标

    这些图标是从ImageList 叫做 imglSessionIcons 的附件中被选出来的到FiddlerApplication.UI.

    为了使用一个存在的图标, 你可以简单的设置ImageIndex 属性,如下:

    public void OnLoad()
    {
    oPage = new TabPage("Timeline");
    oPage.ImageIndex = (int)Fiddler.SessionIcons.Timeline;
    oView = new TimelineView();
    oPage.Controls.Add(oView);
    oView.Dock = DockStyle.Fill;
    FiddlerApplication.UI.tabsViews.TabPages.Add(oPage);

    }

    如果你希望加上自定义的图标, 你必须首先加入到imglSessionIcons.

    在早期版本的Fiddler中,这个成员并不是公开的, 所以当你在就的版本操作imglSessionIcons的时候会抛出一个visibility异常. 解决的方法如下:

    1> 只用在ImageIndex 属性中存在的, 或者

    2> 使用[assembly: Fiddler.RequiredVersion("2.1.3.1")] 属性去获取那个用户更新当前版本的Fiddler

    其他的例子或者是信息


    差不多完成了...

    <!--[if !supportLists]-->·         <!--[endif]-->编译你的项目。

    <!--[if !supportLists]-->·         <!--[endif]-->丢掉.DLL 这个 \My Documents\Fiddler2\Scripts 文件加下面的assembly (或者使用\Program Files\Fiddler2\Scripts 这个而文件夹能够让所有的用户使用)

    <!--[if !supportLists]-->·         <!--[endif]-->重启 Fiddler2.

    如果你需要帮助或者起他信息,email我使用上面的Contact 链接。


     翻译的原文:

    http://www.fiddler2.com/Fiddler/dev/IFiddlerExtension.asp

  • 相关阅读:
    常量池 栈 堆
    函数的调用
    字符、数组
    整理
    JavaScript深入之——原型与原型链
    虚拟机(VMware)中文破解版,及创建虚拟机
    小程序之mpvue使用
    报错整理及解决办法
    js 关于时间方面的通用函数
    iview Cascader级联选择省市区问题
  • 原文地址:https://www.cnblogs.com/luoweihua7/p/create_fiddler2_plugin.html
Copyright © 2020-2023  润新知