using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
using Fiddler;
namespace Demo
{
class Program
{
static Proxy oSecureEndpoint;
static string sSecureEndpointHostname = "localhost";
static int iPort = 8877;
private static string Ellipsize(string s, int iLen)
{
if (s.Length <= iLen) return s;
return s.Substring(0, iLen - 3) + "...";
}
private static void WriteSessionList(List<Fiddler.Session> oAllSessions)
{
try
{
Monitor.Enter(oAllSessions);
foreach (Session oS in oAllSessions)
{
string bodyString = oS.GetRequestBodyAsString();
Console.Write(String.Format("{0} {1} {2}\n{3} {4} {5}\n\n", oS.id, oS.oRequest.headers.HTTPMethod, Ellipsize(oS.fullUrl, 60), oS.responseCode, oS.oResponse.MIMEType, bodyString));
}
}
finally
{
Monitor.Exit(oAllSessions);
}
}
static void Main(string[] args)
{
#region AttachEventListeners
Fiddler.FiddlerApplication.BeforeResponse += delegate (Fiddler.Session oS)
{
if (oS.RequestHeaders.HTTPMethod.ToString() == "GET" || oS.RequestHeaders.HTTPMethod.ToString() == "POST")
{
if (oS.responseCode.ToString() == "200")
{
if (oS.oResponse.headers.ExistsAndContains("Content-Type", "text/html"))
{
oS.utilDecodeResponse();
var oBody = System.Text.Encoding.UTF8.GetString(oS.responseBodyBytes);
oBody += "<h1>My PROXY</h1>";
oS.utilSetResponseBody(oBody + oS.oRequest.headers.ToString());
}
}
}
};
#endregion AttachEventListeners
#region AttachEventListeners
Fiddler.FiddlerApplication.BeforeRequest += delegate (Fiddler.Session oS)
{
oS.bBufferResponse = false;
Monitor.Enter(oAllSessions);
if(oS.oRequest.headers.HTTPMethod.ToString() == "POST")
{
Console.WriteLine();
tw.WriteLine(oS);
}
Monitor.Exit(oAllSessions);
if ((oS.oRequest.pipeClient.LocalPort == iPort) && (oS.hostname == sSecureEndpointHostname))
{
oS.utilCreateResponseAndBypassServer();
oS.oResponse.headers.SetStatus(200, "Ok");
oS.oResponse["Content-Type"] = "text/html; charset=UTF-8";
oS.oResponse["Cache-Control"] = "private, max-age=0";
oS.utilSetResponseBody("<html><body>Request for httpS://" + sSecureEndpointHostname + ":" + iPort.ToString() + " received. Your request was:<br /><plaintext>" + oS.oRequest.headers.ToString());
}
};
#endregion AttachEventListeners
Fiddler.CONFIG.IgnoreServerCertErrors = false;
FiddlerApplication.Prefs.SetBoolPref("fiddler.network.streaming.abortifclientaborts", true);
FiddlerCoreStartupFlags oFCSF = FiddlerCoreStartupFlags.Default;
Fiddler.FiddlerApplication.Startup(iPort, oFCSF);
oSecureEndpoint = FiddlerApplication.CreateProxyEndpoint(iPort, true, sSecureEndpointHostname);
if (null != oSecureEndpoint)
{
FiddlerApplication.Log.LogFormat("Created secure endpoint listening on port {0}, using a HTTPS certificate for '{1}'", iPort, sSecureEndpointHostname);
}
bool bDone = false;
while (!bDone)
{
}
}
}
}