forked from lulianqi/FreeHttp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFiddlerFreeHttp.cs
More file actions
727 lines (682 loc) · 33.3 KB
/
Copy pathFiddlerFreeHttp.cs
File metadata and controls
727 lines (682 loc) · 33.3 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
using Fiddler;
using FreeHttp.AutoTest.RunTimeStaticData;
using FreeHttp.FiddlerHelper;
using FreeHttp.FreeHttpControl;
using FreeHttp.MyHelper;
using FreeHttp.WebService;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
/*******************************************************************************
* Copyright (c) 2018 lulianqi
* All rights reserved.
*
* 文件名称:
* 内容摘要: mycllq@hotmail.com
*
* 历史记录:
* 日 期: 20181103 创建人: lulianqi [mycllq@hotmail.com]
* 描 述: 创建
*
* 历史记录:
* 日 期: 修改:
* 描 述:
*******************************************************************************/
[assembly: Fiddler.RequiredVersion("2.3.5.0")]
namespace FreeHttp
{
public class FiddlerFreeHttp : IAutoTamper, IDisposable
{
private bool isOnLoad = false; //是否已经加载过tab
private bool isCheckedUpdata = false; //是否已经成功完成更新检查,如果检查失败会被重新设置为false
private bool isInFreeHttpTab = false; //是否在正在FreeHttp Tab页中
private Image myIco;
private TabPage tabPage;
private FreeHttpWindow myFreeHttpWindow;
private UpgradeService upgradeService;
private OperationReportService operationReportService;
public bool IsSkipConnectTunnels
{
get
{
if (myFreeHttpWindow != null && myFreeHttpWindow.ModificSettingInfo != null)
return myFreeHttpWindow.ModificSettingInfo.IsSkipConnectTunnels;
else
return true;
}
}
public bool IsSkipUiHide
{
get
{
if (myFreeHttpWindow != null && myFreeHttpWindow.ModificSettingInfo != null)
return myFreeHttpWindow.ModificSettingInfo.IsSkipUiHide;
else
return true;
}
}
public bool IsOnlyMatchFistTamperRule
{
get
{
if (myFreeHttpWindow != null && myFreeHttpWindow.ModificSettingInfo != null)
return myFreeHttpWindow.ModificSettingInfo.IsOnlyMatchFirstTamperRule;
else
return false;
}
}
public bool IsHideFreeHttpSession
{
get
{
return isCheckedUpdata && myFreeHttpWindow.ModificSettingInfo.IsHideSelfSession;
}
}
private void ShowMes(string mes)
{
ShowMes(mes, false);
}
private void ShowMes(string mes , bool isReport = false)
{
if (!isOnLoad)
{
return;
}
if (isReport) _ = RemoteLogService.ReportLogAsync(mes, RemoteLogService.RemoteLogOperation.SessionTamp, RemoteLogService.RemoteLogType.Info);
if (myFreeHttpWindow.InvokeRequired)
{
//BeginInvoke,Invoke will execute in the contol ui thread, but Invoke will with the end in the ui thread
//myFreeHttpWindow.Invoke(new Action(()=>{System.Threading.Thread.Sleep(10000);}) );
myFreeHttpWindow.BeginInvoke(new Action<string>(myFreeHttpWindow.PutInfo), mes);
}
else
{
myFreeHttpWindow.PutInfo(mes);
}
}
private void ShowError(string mes)
{
ShowError(mes, true);
}
private void ShowError(string mes ,bool isReport = true)
{
if (!isOnLoad)
{
return;
}
AddFiddlerObjectLog(mes);
if(isReport) _ = RemoteLogService.ReportLogAsync(mes, RemoteLogService.RemoteLogOperation.SessionTamp, RemoteLogService.RemoteLogType.Error);
if (myFreeHttpWindow.InvokeRequired)
{
myFreeHttpWindow.BeginInvoke(new Action<string>(myFreeHttpWindow.PutError), mes);
}
else
{
myFreeHttpWindow.PutError(mes);
}
}
private void AddFiddlerObjectLog(string mes)
{
FiddlerObject.log(string.Format("【FiddlerFreeHttp】:{0}", mes));
}
private void SetStatusText(string mes)
{
FiddlerObject.StatusText = mes;
}
private void MarkSession(Session oSession)
{
oSession["ui-backcolor"] = "Khaki";
oSession["ui-bold"] = "true";
oSession["ui-color"] = "Indigo";
oSession.RefreshUI();
}
public void OnBeforeUnload()
{
SerializableHelper.SerializeRuleList(myFreeHttpWindow.RequestRuleListView, myFreeHttpWindow.ResponseRuleListView);
SerializableHelper.SerializeData<FiddlerModificSettingInfo>(myFreeHttpWindow.ModificSettingInfo, "FreeHttp\\FreeHttpSetting.xml");
SerializableHelper.SerializeContractData<ActuatorStaticDataCollection>(myFreeHttpWindow.StaticDataCollection, "FreeHttp\\FreeHttpStaticDataCollection.xml");
SerializableHelper.SerializeContractData<FiddlerRuleGroup>(myFreeHttpWindow.ModificRuleGroup, "FreeHttp\\FreeHttpModificRuleGroup.xml");
if (isInFreeHttpTab)
{
operationReportService.OutOperation(DateTime.Now, myFreeHttpWindow.RequestRuleListView.Items.Count, myFreeHttpWindow.ResponseRuleListView.Items.Count);
}
if (operationReportService.HasAnyOperation && IsSkipConnectTunnels)
{
operationReportService.StaticDataCollection = myFreeHttpWindow.StaticDataCollection.IsEmpty? null: myFreeHttpWindow.StaticDataCollection;
operationReportService.RuleGroup = myFreeHttpWindow.ModificRuleGroup.IsEmpty ? null : myFreeHttpWindow.ModificRuleGroup;
operationReportService.FiddlerRequestChangeRuleList = myFreeHttpWindow.FiddlerRequestChangeList;
operationReportService.FiddlerResponseChangeRuleList = myFreeHttpWindow.FiddlerResponseChangeList;
operationReportService.StartReportThread();
}
upgradeService.TrySilentUpgrade();
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)]
public void OnLoad()
{
AddFiddlerObjectLog("OnLoad");
//string workPath = string.Format("{0}\\FreeHttp", System.Windows.Forms.Application.StartupPath);
//System.Threading.Tasks.Task.Run(OnLoad);
string workPath = string.Format("{0}\\FreeHttp", Directory.GetCurrentDirectory());
if (!isOnLoad)
{
tabPage = new TabPage();
tabPage.Text = "Free Http";
if (FiddlerApplication.UI.tabsViews.ImageList != null)
{
myIco = FreeHttp.Resources.MyResource.freehttp;
FiddlerApplication.UI.tabsViews.ImageList.Images.Add(myIco);
tabPage.ImageIndex = FiddlerApplication.UI.tabsViews.ImageList.Images.Count - 1;
}
try
{
if (!Directory.Exists(workPath))
{
AddFiddlerObjectLog(string.Format("Create working directory {0}", workPath));
Directory.CreateDirectory(workPath);
}
AddFiddlerObjectLog(string.Format("load configuration"));
myFreeHttpWindow = new FreeHttpWindow(SerializableHelper.DeserializeRuleList(),
SerializableHelper.DeserializeData<FiddlerModificSettingInfo>("FreeHttp\\FreeHttpSetting.xml"),
SerializableHelper.DeserializeContractData<ActuatorStaticDataCollection>("FreeHttp\\FreeHttpStaticDataCollection.xml"),
SerializableHelper.DeserializeContractData<FiddlerRuleGroup>("FreeHttp\\FreeHttpModificRuleGroup.xml"));
}
catch (Exception ex)
{
AddFiddlerObjectLog(string.Format("load configuration fial ,{0}", ex.Message));
}
finally
{
if (myFreeHttpWindow == null)
{
myFreeHttpWindow = new FreeHttpWindow(null, null, null ,null);
}
}
myFreeHttpWindow.OnUpdataFromSession += myFreeHttpWindow_OnUpdataFromSession;
myFreeHttpWindow.OnGetSessionRawData += myFreeHttpWindow_OnGetSessionRawData;
myFreeHttpWindow.OnGetSessionEventArgs += MyFreeHttpWindow_OnGetSessionEventArgs;
myFreeHttpWindow.OnGetSessionSeekHead += myFreeHttpWindow_OnGetSessionSeekHead;
myFreeHttpWindow.OnShowInIndependentWindow += MyFreeHttpWindow_OnShowInIndependentWindow;
myFreeHttpWindow.Dock = DockStyle.Fill;
myFreeHttpWindow.Enter += myFreeHttpWindow_Enter;
tabPage.Controls.Add(myFreeHttpWindow);
FiddlerApplication.UI.tabsViews.TabPages.Add(tabPage);
Fiddler.FiddlerApplication.UI.Deactivate += UI_Deactivate;
FiddlerApplication.UI.tabsViews.SelectedIndexChanged += tabsViews_SelectedIndexChanged;
FiddlerApplication.OnWebSocketMessage += FiddlerApplication_OnWebSocketMessage;
FiddlerApplication.UI.tabsViews.ParentChanged += TabsViews_ParentChanged;
upgradeService = new UpgradeService();
upgradeService.GetUpgradeMes += upgradeService_GetUpgradeMes;
operationReportService = new OperationReportService();
isOnLoad = true;
}
}
private void TabsViews_ParentChanged(object sender, EventArgs e)
{
myFreeHttpWindow.FreeHttpWindowParentChanged(sender);
}
void tabsViews_SelectedIndexChanged(object sender, EventArgs e)
{
if (isCheckedUpdata == false && FiddlerApplication.UI.tabsViews.SelectedTab == tabPage)
{
isCheckedUpdata = true;
upgradeService.StartCheckUpgrade();
//upgradeService.StartCheckUpgradeThread();
}
//operation report
if (FiddlerApplication.UI.tabsViews.SelectedTab == tabPage)
{
isInFreeHttpTab = true;
operationReportService.InOperation(DateTime.Now);
myFreeHttpWindow.FreeHttpWindowSelectedChanged(true);
}
else if (isInFreeHttpTab)
{
isInFreeHttpTab = false;
operationReportService.OutOperation(DateTime.Now, myFreeHttpWindow.RequestRuleListView.Items.Count, myFreeHttpWindow.ResponseRuleListView.Items.Count);
myFreeHttpWindow.FreeHttpWindowSelectedChanged(false);
}
}
void myFreeHttpWindow_Enter(object sender, EventArgs e)
{
//when myFreeHttpWindow is enter do somethings
}
void UI_Deactivate(object sender, EventArgs e)
{
myFreeHttpWindow.CloseEditRtb();
}
private void upgradeService_GetUpgradeMes(object sender, UpgradeService.UpgradeServiceEventArgs e)
{
Action<string, string> ShowDialogResultBox = (message, title) =>
{
if (string.IsNullOrEmpty(e.UpgradeInfo.url))
{
MessageBox.Show(message, title);
}
else
{
if (MessageBox.Show(message, title,e.UpgradeInfo.isForceEnter? MessageBoxButtons.OK : MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
{
ShowMes(string.Format("enter message details [flag:{0}]", e.UpgradeInfo.messageFlag??"any"),true);
if (string.IsNullOrEmpty(e.UpgradeInfo.url))
{
MessageBox.Show("UpgradeInfo.url is error");
return;
}
try
{
System.Diagnostics.Process.Start(e.UpgradeInfo.url);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("UpgradeMes is error \r\n{0}", ex.Message));
ShowError(string.Format("process start fail [{0}] [{1}] ", e.UpgradeInfo.url , ex.Message));
}
}
else
{
ShowMes(string.Format("cancel enter message details [flag:{0}]", e.UpgradeInfo.messageFlag ?? "any"),true);
}
}
};
if (e.IsSuccess)
{
if(!string.IsNullOrEmpty(e.UpgradeInfo.uuid)&& string.IsNullOrEmpty(myFreeHttpWindow.ModificSettingInfo.UserToken))
{
myFreeHttpWindow.ModificSettingInfo.UserToken = e.UpgradeInfo.uuid;
}
if (e.UpgradeInfo.isNeedUpdata && !e.UpgradeInfo.isSilentUpgrade)
{
//ShowDialogResultBox(string.IsNullOrEmpty(e.UpgradeInfo.message)?"Find new version for [ FreeHttp Plug-in ] \r\nDo you want goto upgrade page to udpade your FreeHttp" : e.UpgradeInfo.message , "find new version");
myFreeHttpWindow.Invoke(new Action(() => {
ShowDialogResultBox(string.IsNullOrEmpty(e.UpgradeInfo.message) ? "Find new version for [ FreeHttp Plug-in ] \r\nDo you want goto upgrade page to udpade your FreeHttp" : e.UpgradeInfo.message, "find new version");
}));
return;
}
else if(e.UpgradeInfo.isNeedUpdata && e.UpgradeInfo.isSilentUpgrade)
{
//Silent Upgrade
}
else if (e.UpgradeInfo.isShowMessage && !string.IsNullOrEmpty(e.UpgradeInfo.message))
{
//show meaasge
if(string.IsNullOrEmpty(e.UpgradeInfo.messageFlag))
{
myFreeHttpWindow.Invoke(new Action(() => {
ShowDialogResultBox(e.UpgradeInfo.message, "new message");
}));
}
else
{
if (!myFreeHttpWindow.ModificSettingInfo.ReadedMessageFlags.Contains(e.UpgradeInfo.messageFlag))
{
myFreeHttpWindow.Invoke(new Action(() =>
{
ShowDialogResultBox(e.UpgradeInfo.message, "new message");
}));
myFreeHttpWindow.ModificSettingInfo.ReadedMessageFlags.Add(e.UpgradeInfo.messageFlag);
}
}
}
else
{
//not any thing
}
}
else
{
isCheckedUpdata = false;
}
}
private void myFreeHttpWindow_OnGetSessionRawData(object sender, FreeHttpWindow.GetSessionRawDataEventArgs e)
{
Session tempSession = Fiddler.FiddlerObject.UI.GetFirstSelectedSession();
if (tempSession == null)
{
Fiddler.FiddlerObject.UI.ShowAlert(new frmAlert("STOP", "please select a session", "OK"));
FreeHttpWindow.MarkWarnControl(Fiddler.FiddlerApplication.UI.lvSessions);
return;
}
switch (e.SessionAction)
{
case FreeHttpWindow.GetSessionAction.ShowShowResponse:
string tempStr = FiddlerSessionTamper.GetSessionRawData(tempSession, true);
ShowMes(tempStr == null ? "error session" : string.Format("Get Raw Data\r\n{0}", tempStr));
myFreeHttpWindow.ShowOwnerWindow(tempSession.fullUrl, tempStr);
break;
case FreeHttpWindow.GetSessionAction.SetCookies:
myFreeHttpWindow.SetClientAddCookies(tempSession.RequestHeaders["Cookie"]);
break;
case FreeHttpWindow.GetSessionAction.DeleteCookies:
myFreeHttpWindow.SetClientDelCookies(tempSession.RequestHeaders["Cookie"]);
break;
default:
Fiddler.FiddlerObject.UI.ShowAlert(new frmAlert("STOP", "Not supported this SessionAction", "OK"));
break;
}
}
private void MyFreeHttpWindow_OnGetSessionEventArgs(object sender, FreeHttpWindow.GetSessionEventArgs e)
{
Session tempSession = Fiddler.FiddlerObject.UI.GetFirstSelectedSession();
if (tempSession == null)
{
e.IsGetSuccess = false;
}
else
{
e.IsGetSuccess = FiddlerSessionTamper.GetSessionData(tempSession, e);
}
}
private void myFreeHttpWindow_OnGetSessionSeekHead(object sender, FreeHttpWindow.GetSessionSeekHeadEventArgs e)
{
Session tempSession = Fiddler.FiddlerObject.UI.GetFirstSelectedSession();
if (tempSession == null)
{
FreeHttpWindow.MarkWarnControl(Fiddler.FiddlerApplication.UI.lvSessions);
}
else
{
if (e != null && e.ResquestHead.Key != null)
{
//HTTPHeaderItem nowHTTPHeaderItem = tempSession.RequestHeaders.First(hTTPHeaderItem => hTTPHeaderItem.Name == e.ResquestHead.Key);
HTTPHeaderItem nowHTTPHeaderItem = tempSession.RequestHeaders.FirstOrDefault(hTTPHeaderItem => hTTPHeaderItem.Name == e.ResquestHead.Key);
if (nowHTTPHeaderItem != null)
{
e.ResquestHead = new KeyValuePair<string, string>(nowHTTPHeaderItem.Name, nowHTTPHeaderItem.Value);
e.SeekUri = tempSession.fullUrl;
}
}
if (e != null && e.ResponseHead.Key != null)
{
HTTPHeaderItem nowHTTPHeaderItem = tempSession.ResponseHeaders.FirstOrDefault(hTTPHeaderItem => hTTPHeaderItem.Name == e.ResponseHead.Key);
if (nowHTTPHeaderItem != null)
{
e.ResponseHead = new KeyValuePair<string, string>(nowHTTPHeaderItem.Name, nowHTTPHeaderItem.Value);
e.SeekUri = tempSession.fullUrl;
}
}
}
}
private void MyFreeHttpWindow_OnShowInIndependentWindow(object sender, bool e)
{
if(e)
{
myFreeHttpWindow.FreeHttpWindowParentChanged(sender);
Form newForm = new Form();
newForm.Icon = FreeHttp.Resources.MyResource.freehttpico;
newForm.Text = "FreeHttp";
newForm.Size = tabPage.Size;
newForm.FormClosing += new FormClosingEventHandler((yourSender, yourE) => {
myFreeHttpWindow.FreeHttpWindowParentChanged(sender);
MyControlHelper.SetControlFreeze(tabPage);
myFreeHttpWindow.independentWindowToolStripMenuItem.Text = "independent window";
tabPage.Controls.Clear();
tabPage.Controls.Add(myFreeHttpWindow);
MyControlHelper.SetControlUnfreeze(tabPage);
});
MyControlHelper.SetControlFreeze(newForm);
Label lb_info = new Label();
lb_info.Text = "closing...";
lb_info.ForeColor = Color.Blue;
lb_info.Location = new Point((newForm.Width - lb_info.Width) / 2, (newForm.Height - lb_info.Height) / 2);
lb_info.Anchor = AnchorStyles.None;
newForm.Controls.Add(lb_info);
newForm.Controls.Add(myFreeHttpWindow);
lb_info.SendToBack();
MyControlHelper.SetControlUnfreeze(newForm);
newForm.Show();
LinkLabel llb_info = new LinkLabel();
llb_info.Text = "FreeHttp is in independent mode";
llb_info.ForeColor = Color.Blue;
llb_info.AutoSize = true;
llb_info.Location = new Point((tabPage.Width - llb_info.Width) / 2, (tabPage.Height - llb_info.Height) / 2);
llb_info.Anchor = AnchorStyles.None;
llb_info.LinkClicked += new LinkLabelLinkClickedEventHandler((yourSender, yourE) => { newForm.Activate(); });
tabPage.Controls.Add(llb_info);
LinkLabel llb_infoRecover = new LinkLabel();
llb_infoRecover.Text = "recover to addin mode";
llb_infoRecover.ForeColor = Color.Blue;
llb_infoRecover.AutoSize = true;
llb_infoRecover.Location = new Point((tabPage.Width - llb_infoRecover.Width) / 2, ((tabPage.Height - llb_infoRecover.Height) / 2) + 20);
llb_infoRecover.Anchor = AnchorStyles.None;
llb_infoRecover.LinkClicked += new LinkLabelLinkClickedEventHandler((yourSender, yourE) => { myFreeHttpWindow.independentWindowToolStripMenuItem_Click(null, null);});
tabPage.Controls.Add(llb_infoRecover);
}
else
{
(myFreeHttpWindow.Parent as Form)?.Close();
}
}
private void myFreeHttpWindow_OnUpdataFromSession(object sender, EventArgs e)
{
Session tempSession = Fiddler.FiddlerObject.UI.GetFirstSelectedSession();
if (tempSession != null)
{
ShowMes(string.Format("Get http session in {0}", tempSession.fullUrl));
((FreeHttpWindow)sender).SetModificSession(tempSession);
}
else
{
Fiddler.FiddlerObject.UI.ShowAlert(new frmAlert("STOP", "please select a session", "OK"));
//((FreeHttpWindow)sender).MarkWarnControl(Fiddler.FiddlerApplication.UI.Controls[0]);
FreeHttpWindow.MarkWarnControl(Fiddler.FiddlerApplication.UI.lvSessions);
}
}
private void FiddlerApplication_OnWebSocketMessage(object sender, WebSocketMessageEventArgs e)
{
//((Bitmap)((Fiddler.Session)sender).ViewItem.ImageList.Images[34]).Save(@"D:\A1.ico", System.Drawing.Imaging.ImageFormat.Icon);
Session oSession = (Session)sender;
WebSocketMessage webSocketMessage = e.oWSM;
if (!isOnLoad)
{
return;
}
if(webSocketMessage==null)
{
AddFiddlerObjectLog("get null WebSocketMessage");
return;
}
if (webSocketMessage.FrameType == WebSocketFrameTypes.Close ||
webSocketMessage.FrameType == WebSocketFrameTypes.Ping ||
webSocketMessage.FrameType == WebSocketFrameTypes.Pong ||
webSocketMessage.FrameType == WebSocketFrameTypes.Reservedx3 ||
webSocketMessage.FrameType == WebSocketFrameTypes.Reservedx4 ||
webSocketMessage.FrameType == WebSocketFrameTypes.Reservedx5 ||
webSocketMessage.FrameType == WebSocketFrameTypes.Reservedx6 ||
webSocketMessage.FrameType == WebSocketFrameTypes.Reservedx7 ||
webSocketMessage.FrameType == WebSocketFrameTypes.ReservedxB ||
webSocketMessage.FrameType == WebSocketFrameTypes.ReservedxC ||
webSocketMessage.FrameType == WebSocketFrameTypes.ReservedxD ||
webSocketMessage.FrameType == WebSocketFrameTypes.ReservedxE ||
webSocketMessage.FrameType == WebSocketFrameTypes.ReservedxF)
{
return;
}
if ((myFreeHttpWindow.IsRequestRuleEnable && webSocketMessage.IsOutbound)|| (myFreeHttpWindow.IsResponseRuleEnable && !webSocketMessage.IsOutbound))
{
if (IsSkipUiHide && oSession["ui-hide"] !=null)
{
return;
}
if (IsSkipConnectTunnels && oSession.RequestMethod == "CONNECT")
{
return;
}
bool isRequest = webSocketMessage.IsOutbound;
List<IFiddlerHttpTamper> matchItems = null;
if(isRequest)
{
matchItems = FiddlerSessionHelper.FindMatchTanperRule(oSession, myFreeHttpWindow.FiddlerRequestChangeList, isRequest, webSocketMessage);
}
else
{
//oSession.WriteResponseToStream(new MemoryStream(new Byte[] { 0x81,0x81,0x01,0x41 }), false);
//WebSocket ws = oSession.__oTunnel as WebSocket;
//ws.listMessages.Add(webSocketMessage);
matchItems = FiddlerSessionHelper.FindMatchTanperRule(oSession, myFreeHttpWindow.FiddlerResponseChangeList, isRequest, webSocketMessage);
}
if (matchItems != null && matchItems.Count > 0)
{
foreach (var matchItem in matchItems)
{
ListViewItem tempListViewItem = myFreeHttpWindow.FindListViewItemFromRule(matchItem);
FreeHttpWindow.MarkMatchRule(tempListViewItem);
MarkSession(oSession);
ShowMes(string.Format("macth the [requst rule {0}] with {1}", tempListViewItem.SubItems[0].Text, oSession.fullUrl));
FiddlerSessionTamper.ModificWebSocketMessage(oSession, webSocketMessage, matchItem ,isRequest, ShowError, ShowMes);
if (!isRequest)
{
FiddlerResponseChange nowFiddlerResponseChange = ((FiddlerResponseChange)matchItem);
if (nowFiddlerResponseChange.ResponseLatency > 0)
{
ShowMes(string.Format("[reponse rule {0}] is modified , now delay {1} ms", tempListViewItem.SubItems[0].Text, nowFiddlerResponseChange.ResponseLatency));
System.Threading.Thread.Sleep(nowFiddlerResponseChange.ResponseLatency);
}
if (IsOnlyMatchFistTamperRule)
{
break;
}
}
}
}
}
}
public void AutoTamperRequestAfter(Session oSession)
{
//throw new NotImplementedException();
}
public void AutoTamperRequestBefore(Session oSession)
{
//if (oSession.HTTPMethodIs("CONNECT") && oSession.HostnameIs("api.map.baidu.com"))
//{
// oSession["x-OverrideSslProtocols"] = "ssl3.0";
//}
//oSession.oRequest["AddOrigin"] = "from lijie PC";
if (!isOnLoad)
{
return;
}
if(IsHideFreeHttpSession && oSession.oRequest.host=="api.lulianqi.com")
{
oSession["ui-hide"] = "true";
}
if (myFreeHttpWindow.IsRequestRuleEnable)
{
//IsRequestRuleEnable is more efficient then string comparison (so if not IsRequestRuleEnable the string comparison will not execute)
if (IsSkipUiHide && oSession["ui-hide"] != null)
{
return;
}
if (IsSkipConnectTunnels && oSession.RequestMethod == "CONNECT")
{
return;
}
List<IFiddlerHttpTamper> matchItems = FiddlerSessionHelper.FindMatchTanperRule(oSession, myFreeHttpWindow.FiddlerRequestChangeList,true);
if (matchItems != null && matchItems.Count>0)
{
foreach (var matchItem in matchItems)
{
FiddlerRequestChange nowFiddlerRequsetChange = ((FiddlerRequestChange)matchItem);
ListViewItem tempListViewItem = myFreeHttpWindow.FindListViewItemFromRule(matchItem);
FreeHttpWindow.MarkMatchRule(tempListViewItem);
MarkSession(oSession);
ShowMes(string.Format("macth the [requst rule {0}] with {1}", tempListViewItem.SubItems[0].Text, oSession.fullUrl));
FiddlerSessionTamper.ModificSessionRequest(oSession, nowFiddlerRequsetChange,ShowError,ShowMes);
if(IsOnlyMatchFistTamperRule)
{
break;
}
}
}
}
if (myFreeHttpWindow.IsResponseRuleEnable)
{
if (IsSkipConnectTunnels && oSession.RequestMethod == "CONNECT")
{
return;
}
List<IFiddlerHttpTamper> matchItems = FiddlerSessionHelper.FindMatchTanperRule(oSession, myFreeHttpWindow.FiddlerResponseChangeList,false);
if (matchItems != null && matchItems.Count>0)
{
oSession.bBufferResponse = true;// if any response rule may match the Session, we should set bBufferResponse true (When streaming is enabled for a response, each block of data read from the server is immediately passed to the client application. )
foreach (var matchItem in matchItems)
{
FiddlerResponseChange nowFiddlerResponseChange = ((FiddlerResponseChange)matchItem);
ListViewItem tempListViewItem = myFreeHttpWindow.FindListViewItemFromRule(matchItem);
if (nowFiddlerResponseChange.IsIsDirectRespons)
{
FreeHttpWindow.MarkMatchRule(tempListViewItem);
MarkSession(oSession);
ShowMes(string.Format("macth the [reponse rule {0}] with {1}", tempListViewItem.SubItems[0].Text, oSession.fullUrl));
FiddlerSessionTamper.ReplaceSessionResponse(oSession, nowFiddlerResponseChange,ShowError,ShowMes);
//oSession.state = SessionStates.Done;
if (IsOnlyMatchFistTamperRule)
{
break;
}
}
}
}
}
}
public void AutoTamperResponseAfter(Session oSession)
{
if (!isOnLoad)
{
return;
}
if (myFreeHttpWindow.IsResponseRuleEnable)
{
if (IsSkipUiHide && oSession["ui-hide"] !=null)
{
return;
}
if (IsSkipConnectTunnels && oSession.RequestMethod == "CONNECT")
{
return;
}
List<IFiddlerHttpTamper> matchItems = FiddlerSessionHelper.FindMatchTanperRule(oSession, myFreeHttpWindow.FiddlerResponseChangeList,false);
if (matchItems != null && matchItems.Count>0)
{
foreach (var matchItem in matchItems)
{
FiddlerResponseChange nowFiddlerResponseChange = ((FiddlerResponseChange)matchItem);
ListViewItem tempListViewItem = myFreeHttpWindow.FindListViewItemFromRule(matchItem);
if (!(nowFiddlerResponseChange.IsRawReplace && nowFiddlerResponseChange.IsIsDirectRespons))
{
FreeHttpWindow.MarkMatchRule(tempListViewItem);
MarkSession(oSession);
ShowMes(string.Format("macth the [reponse rule {0}] with {1}", tempListViewItem.SubItems[0].Text, oSession.fullUrl));
FiddlerSessionTamper.ModificSessionResponse(oSession, nowFiddlerResponseChange,ShowError,ShowMes);
}
if (nowFiddlerResponseChange.ResponseLatency > 0)
{
ShowMes(string.Format("[reponse rule {0}] is modified , now delay {1} ms", tempListViewItem.SubItems[0].Text, nowFiddlerResponseChange.ResponseLatency));
System.Threading.Thread.Sleep(nowFiddlerResponseChange.ResponseLatency);
}
if (IsOnlyMatchFistTamperRule)
{
break;
}
}
}
}
}
public void AutoTamperResponseBefore(Session oSession)
{
//throw new NotImplementedException();
}
public void OnBeforeReturningError(Session oSession)
{
this.AutoTamperResponseAfter(oSession);
}
public void Dispose()
{
tabPage.Dispose();
myFreeHttpWindow.Dispose();
}
}
}