using AllBase.Model.Result;
using Fiddler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TemuKJD.BaseForms;
namespace TemuKJD.Helper
{
public class FddlHelper
{
public bool InstallCertificate()
{
//FiddlerApplication.Log.LogString($"安装证书,为了监听https请求");
if (!CertMaker.rootCertExists())
{
if (!CertMaker.createRootCert() || !CertMaker.trustRootCert())
{
BaseMsgForm.ShowError("证书安装失败,请重试!");
return false;
}
}
return true;
}
/// 启动fiddlercore本地设置的代理端口监听
public void StartupFiddlerCore(int port = 9898)
{
//Console.WriteLine("fiddler启动了...listening: port: 127.0.0.1:" + port);
FiddlerApplication.Startup(port, FiddlerCoreStartupFlags.Default | FiddlerCoreStartupFlags.RegisterAsSystemProxy);
//FiddlerApplication.Log.LogString($"Created endpoint listening on port {CONFIG.ListenPort}");
}
public void AttachListening()
{
//FiddlerApplication.OnNotification += (o, nea) => Console.WriteLine($"** NotifyUser: {nea.NotifyString}");
//FiddlerApplication.Log.OnLogString += (o, lea) => Console.WriteLine($"** LogString: {lea.LogString}");
FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest;
FiddlerApplication.BeforeResponse += FiddlerApplication_BeforeResponse;
}
/// 拦截请求返回Request信息
private void FiddlerApplication_BeforeRequest(Session oSession)
{
//Debug.WriteLine(oSession.fullUrl);
// In order to enable response tampering, buffering mode MUST
// be enabled; this allows FiddlerCore to permit modification of
// the response in the BeforeResponse handler rather than streaming
// the response to the client as the response comes in.
oSession.bBufferResponse = false;
// Set this property if you want FiddlerCore to automatically authenticate by
// answering Digest/Negotiate/NTLM/Kerberos challenges itself
//session["X-AutoAuth"] = "(default)";
//FiddlerApplication.Log.LogString($" {oSession.fullUrl }");
}
/// 拦截请求返回Response信息
private void FiddlerApplication_BeforeResponse(Session oSession)
{
}
/// 关闭fiddlercore设置的代理
public void UninstallFiddler()
{
if (FiddlerApplication.IsStarted())
{
FiddlerApplication.Shutdown();
}
}
/// 卸载证书
public bool UninstallCertificate()
{
if (CertMaker.rootCertExists())
{
if (!CertMaker.removeFiddlerGeneratedCerts(true))
return false;
}
return true;
}
}
}