文章目錄
本文目標
爲fiddler做一個插件,按要求截獲所感興趣的關鍵字url,保存到文件中
下載安裝最新版本fiddler
https://www.telerik.com/fiddler
選擇安裝路徑到d盤中(非必須)
關於接口的描述,請參考:https://www.cnblogs.com/weekend001/archive/2013/12/13/3473632.html
配置工程
新建一個工程並引入類庫
新建一個C#,Windows類庫
添加前面安裝的fiddler.exe作爲引用
因爲要加入一個帶窗體的應用程序,因此還需要引入System.Windows.Forms
新建一個頁面控件
在AssemblyInfo.cs中聲明fiddler的版本
一般嘛使用4.5.1.2即可解決大多數版本問題
[assembly: Fiddler.RequiredVersion("4.5.1.2")]
設計頁面
使用fiddler提供的接口,把頁面加入到fiddler中
using Fiddler;
using System.Windows.Forms;
namespace FiddlerExtension
{
public class Class1: IAutoTamper
{
private TabPage tabPage; //創建插件的選項卡頁
private UserControl1 myCtrl; //MyControl自定義控件
public Class1()
{
//構造函數中實例化對象
this.tabPage = new TabPage("MyFiddlerExtension");//選項卡的名字爲Test
this.myCtrl = new UserControl1();
}
public void OnLoad()
{
//將用戶控件添加到選項卡中
this.tabPage.Controls.Add(this.myCtrl);
//爲選項卡添加icon圖標,這裏使用Fiddler 自帶的
this.tabPage.ImageIndex = (int)Fiddler.SessionIcons.Timeline;
//將tabTage選項卡添加到Fidder UI的Tab 頁集合中
FiddlerApplication.UI.tabsViews.TabPages.Add(this.tabPage);
}
public void OnBeforeUnload()
{
}
// Called before the user can edit a request using the Fiddler Inspectors
public 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
public void AutoTamperRequestAfter(Session oSession) { }
// Called before the user can edit a response using the Fiddler Inspectors, unless streaming.
public void AutoTamperResponseBefore(Session oSession) { }
// Called after the user edited a response using the Fiddler Inspectors. Not called when streaming.
public void AutoTamperResponseAfter(Session oSession) { }
// Called Fiddler returns a self-generated HTTP error (for instance DNS lookup failed, etc)
public void OnBeforeReturningError(Session oSession) { }
}
}
爲按鈕加入一個響應事件
using System;
using System.Windows.Forms;
namespace FiddlerExtension
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("you clicked me");
}
}
}
生成並測試
把生成後得到的FiddlerExtension.dll,拷貝到fiddler目錄下的Scripts文件夾中
=================================================
可以通過修改生成事件,讓其自動複製到指定文件夾中
copy "$(TargetPath)" "D:\Fiddler\Scripts$(TargetFilename)"
=================================================
運行fiddler,點擊頁面中的按鈕,可以看到彈出提示,證明代碼已經正確
完成目標功能
修改頁面控件內容
補充頁面控件的代碼
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace FiddlerExtension
{
public partial class UserControl1 : UserControl
{
public string filter = "";
private bool isBeginFilter = false;
public UserControl1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
if (!isBeginFilter) // beginFilter
{
isBeginFilter = !isBeginFilter;
filter = this.textBox2.Text;
this.button1.Text = "停止捕獲";
}
else
{
isBeginFilter = !isBeginFilter;
filter = "";
this.button1.Text = "開始捕獲";
}
}
private void button2_Click(object sender, EventArgs e)
{
// 把當前listview中的數據保存到output.txt中
if (this.listView1.Items.Count == 0)
{
MessageBox.Show("列表爲空!");
}
else
{
List<string> list = new List<string>();
foreach (ListViewItem item in this.listView1.Items)
{
string temp = item.SubItems[2].Text; // 取出url的數據進行保存
list.Add(temp);
}
Thread thexp = new Thread(() => export(list)) { IsBackground = true };
thexp.Start();
}
}
private void export(List<string> list)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "url_" + Guid.NewGuid().ToString() + ".txt";
StringBuilder sb = new StringBuilder();
foreach (string urlString in list)
{
sb.AppendLine(urlString);
}
System.IO.File.WriteAllText(path, sb.ToString(), Encoding.UTF8);
MessageBox.Show("已保存完畢,路徑:" + path);
}
}
}
對應項目的代碼如下(Class1.cs)
using Fiddler;
using System.Windows.Forms;
namespace FiddlerExtension
{
public class Class1 : IAutoTamper
{
private TabPage tabPage; //創建插件的選項卡頁
private UserControl1 myCtrl; //MyControl自定義控件
public Class1()
{
//構造函數中實例化對象
this.tabPage = new TabPage("MyFiddlerExtension");//選項卡的名字爲Test
this.myCtrl = new UserControl1();
}
public void OnLoad()
{
//將用戶控件添加到選項卡中
this.tabPage.Controls.Add(this.myCtrl);
//爲選項卡添加icon圖標,這裏使用Fiddler 自帶的
this.tabPage.ImageIndex = (int)Fiddler.SessionIcons.Timeline;
//將tabTage選項卡添加到Fidder UI的Tab 頁集合中
FiddlerApplication.UI.tabsViews.TabPages.Add(this.tabPage);
}
public void OnBeforeUnload()
{
}
// Called before the user can edit a request using the Fiddler Inspectors
public void AutoTamperRequestBefore(Session oSession)
{
// this.myCtrl.textBox1.Text += oSession.fullUrl + "\r\n";
// 過濾滿足條件的host
if (this.myCtrl.filter != "")
{
string url = oSession.fullUrl;
if (url.Contains(this.myCtrl.filter))
{
ListViewItem item = new ListViewItem(this.myCtrl.listView1.Items.Count + "");
item.SubItems.Add(oSession.host); // 加入host
item.SubItems.Add(oSession.fullUrl); // 加入fullUrl
//item.SubItems.Add(""); // 加入request參數
//item.SubItems.Add(""); // 加入response參數
this.myCtrl.listView1.Items.Add(item);
}
}
}
// Called after the user has had the chance to edit the request using the Fiddler Inspectors, but before the request is sent
public void AutoTamperRequestAfter(Session oSession) { }
// Called before the user can edit a response using the Fiddler Inspectors, unless streaming.
public void AutoTamperResponseBefore(Session oSession) { }
// Called after the user edited a response using the Fiddler Inspectors. Not called when streaming.
public void AutoTamperResponseAfter(Session oSession) { }
// Called Fiddler returns a self-generated HTTP error (for instance DNS lookup failed, etc)
public void OnBeforeReturningError(Session oSession) { }
}
}
生成並測試
開始捕獲
保存到文件中
點擊後如下
打開查看內容如下
以上說明功能完成
示例代碼下載
https://download.csdn.net/download/zengraoli/12120294
已在demo上做了修改,url寫出到固定文件中,只需要替換本文相應代碼即可
參考的鏈接
- https://www.cnblogs.com/weekend001/archive/2013/12/13/3473632.html
- https://www.cnblogs.com/rufus-hua/p/5275980.html
- https://www.qxqzx.com/contents/623.html
- https://www.sohu.com/a/190260237_741445
- https://blog.csdn.net/qqaiqqaiww/article/details/92800318
- https://www.cnblogs.com/weekend001/p/3472912.html
- https://blog.csdn.net/x333vxhl/article/details/55188371
- https://www.qxqzx.com/contents/623.html