保存本地文件
static function OnBeforeResponse(oSession: Session) {
// ......
// 拦截 xxx 域名 https://xxx/api/admin/user/me
if (oSession.HostnameIs("xxx") && oSession.uriContains("/api/admin/user/me") && oSession.oRequest.headers.Exists("X-Auth-Token")) {
SaveSession(oSession);
}
}
// 保存header
static function SaveSession(oSession: Session) {
FiddlerApplication.Log.LogString("拦截【headers】并写入【"+ fileName + "】 => \n" + oSession.RequestHeaders);
var fso = new ActiveXObject("Scripting.FileSystemObject");
var dirPath = "C:\\Users\\xxx\\.demo\\fiddler";
if(!fso.FolderExists(dirPath)){
FiddlerApplication.Log.LogString("创建目录 => " + dirPath);
fso.CreateFolder(dirPath);
}
var fileName = oSession.host + ".txt";
// 采用 UTF-16LE with bom
var file = fso.OpenTextFile(fso.BuildPath(dirPath,fileName), 2 ,true, -1);
file.writeLine("[request_header]:\n"+oSession.oRequest.headers);
file.writeLine("[request_body]:\n"+oSession.GetRequestBodyAsString());
file.writeLine("[response_header]:\n"+oSession.oResponse.headers);
file.writeLine("[response_body]:\n"+oSession.GetResponseBodyAsString());
file.close();
}
二次转发
static function OnBeforeResponse(oSession: Session) {
// ......
// 拦截 xxx 域名 https://xxx/api/admin/user/me
if (oSession.HostnameIs("xxx") && oSession.uriContains("/api/admin/user/me") && oSession.oRequest.headers.Exists("X-Auth-Token")) {
SaveSession(oSession);
}
}
static function SaveSession(oSession: Session) {
FiddlerApplication.Log.LogString("拦截【headers】并发送 => \n" + oSession.oRequest.headers);
SendRequest(oSession, "localhost:8080", "/push");
SendRequest(oSession, "public:8081", "/push");
}
// 发送到第三方接口
static function SendRequest(oSession: Session,sHost: String, sApi:String){
var requestHeader = encodeHexString(""+oSession.oRequest.headers)
var payload = encodeHexString(oSession.GetRequestBodyAsString())
var responseHeader = encodeHexString(""+oSession.oResponse.headers)
var response = encodeHexString(oSession.GetResponseBodyAsString())
var body = "{\n";
body += "requestHeader: \""+requestHeader+"\",\n";
body += "payload: \""+payload+"\",\n";
body += "responseHeader: \""+responseHeader+"\",\n";
body += "response: \""+response+"\"\n";
body += "}";
var sContent = System.Text.Encoding.UTF8.GetBytes(body);
var sDir = new System.Collections.Specialized.StringDictionary();
var sRequest: HTTPRequestHeaders = new HTTPRequestHeaders(sApi,[]);
sRequest.HTTPMethod = "POST";
sRequest.Add("Host",sHost);
sRequest.Add("Content-Length",sContent.length.ToString());
sRequest.Add("Accept","application/json, text/plain, */*");
sRequest.Add("Accept-Language","zh-cn");
sRequest.Add("Content-Type","application/json;charset=UTF-8");
try{
var sSession = FiddlerApplication.oProxy.SendRequestAndWait(sRequest, sContent, sDir, null);
var sResponse = sSession.GetResponseBodyAsString();
FiddlerApplication.Log.LogString("推送结果 => " + sResponse);
sSession.utilSetResponseBody(sResponse);
} catch(e) {
FiddlerApplication.Log.LogString("推送失败 => " + e.ToString());
}
}
// utf8编码+十六进制编码
static function encodeHexString(str) {
var bytes = System.Text.Encoding.UTF8.GetBytes(str);
var res = [];
for ( var i=0; i< bytes.length; i++ ) {
res[i] = bytes[i].toString(16);
if (res[i].Length % 2 != 0){
res[i]= "0" + res[i];
}
}
var tar = res.join("");
return tar;
}