-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathToPython.cs
131 lines (112 loc) · 5.03 KB
/
ToPython.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Net;
using System.Text;
using System.Windows.Forms;
using Fiddler;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
[assembly: Fiddler.RequiredVersion("5.0.0.0")]
[assembly: AssemblyVersion("0.0.2")]
[assembly: AssemblyTitle("Fiddler Request To Python Code")]
[assembly: AssemblyDescription("将 Fiddler 的抓包请求转换为 Python 代码")]
namespace ToPython
{
public class ToPython : IFiddlerExtension
{
private TabPage _page;
private RichTextBox codeBox;
private void TextBox_LinkClicked(object sender, LinkClickedEventArgs e){
Process.Start(e.LinkText);
}
private void TextBox_DragEnter(object sender, DragEventArgs e){
e.Effect = DragDropEffects.Copy;
}
private void TextBox_DragDrop(object sender, DragEventArgs e){
DataObject dataObject = (DataObject)e.Data;
if (!dataObject.GetDataPresent("Fiddler.Session[]"))
return;
Session[] sessions = (Session[])dataObject.GetData("Fiddler.Session[]");
if (sessions.Length == 0)
return;
Session session = sessions[0];
Uri url = new Uri(session.fullUrl);
string query = url.Query;
string req_method = "get";
string RequestBody = Encoding.UTF8.GetString(session.RequestBody);
// 生成Python代码
StringBuilder sb = new StringBuilder();
StringBuilder sb1 = new StringBuilder();
sb.Append("import requests\n\n");
sb.AppendFormat("url = '{0}'\n\n", url.GetLeftPart(UriPartial.Path));
sb.Append("headers = {\n");
var header_arr = session.oRequest.headers.ToArray();
foreach (HTTPHeaderItem header in header_arr) {
if (header.Name != "Cookie") {
sb.AppendFormat(" '{0}': '{1}',\n", header.Name, header.Value);
} else {
sb.Append("}\n\ncookies = {\n");
string[] cookie_pairs = header.Value.Split(';');
foreach (string pair in cookie_pairs) {
string[] parts = pair.Trim().Split('=');
sb.AppendFormat(" '{0}': '{1}',\n", parts[0], WebUtility.UrlDecode(parts[1]));
}
sb1.Append(", cookies=cookies");
}
}
sb.Append("}\n\n");
if (!String.IsNullOrEmpty(query)) {
sb.Append("params = {\n");
string[] query_pairs = query.TrimStart('?').Split('&');
foreach (string pair in query_pairs) {
string[] parts = pair.Split('=');
sb.AppendFormat(" '{0}': '{1}',\n", parts[0], WebUtility.UrlDecode(parts[1]));
}
sb.Append("}\n\n");
sb1.Append(", params=params");
}
if (session.RequestMethod == "POST") {
req_method = "post";
if (!String.IsNullOrEmpty(RequestBody)) {
if (RequestBody.Contains("&") && RequestBody.Contains("=")) {
sb.Append("data = {\n");
string[] body_pairs = RequestBody.Split('&');
foreach (string pair in body_pairs) {
string[] parts = pair.Split('=');
sb.AppendFormat(" '{0}': '{1}',\n", parts[0], WebUtility.UrlDecode(parts[1]));
}
sb.Append("}\n\n");
} else {
sb.AppendFormat("data = '{0}'\n\n", WebUtility.UrlDecode(RequestBody));
}
sb1.Append(", data=data");
}
}
sb.AppendFormat("response = requests.{0}(url, headers=headers{1})\n", req_method, sb1.ToString());
sb.Append("print(response.text)\n");
// 显示Python代码
codeBox.DetectUrls = false;
codeBox.Text = sb.ToString();
}
public void OnLoad(){
// 设置代码框
codeBox = new RichTextBox();
codeBox.AutoSize = true;
codeBox.ReadOnly = true;
codeBox.Multiline = true;
codeBox.Dock = DockStyle.Fill;
codeBox.Text = "\n\tDrag and drop the request here\n\t将请求拖放到此处\n\n\tby Courser\n\thttps://github.com/GitCourser/fiddler2py\n";
// 拖放与点击
codeBox.AllowDrop = true;
codeBox.DragDrop += new DragEventHandler(TextBox_DragDrop);
codeBox.DragEnter += new DragEventHandler(TextBox_DragEnter);
codeBox.LinkClicked += new LinkClickedEventHandler(TextBox_LinkClicked);
// 添加选项卡
_page = new TabPage("Python");
_page.Controls.Add(codeBox);
FiddlerApplication.UI.tabsViews.TabPages.Add(_page);
}
public void OnBeforeUnload(){}
}
}