home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 December / PCWELT_12_2006.ISO / pcwsoft / FiddlerBetaSetup.exe / Scripts / SampleRules.js
Encoding:
Text File  |  2006-08-31  |  9.6 KB  |  286 lines

  1. ∩╗┐import System;
  2. import System.Windows.Forms;
  3. import Fiddler;
  4.  
  5. // GLOBALIZATION NOTE:
  6. // Be sure to save this file with UTF-8 Encoding if using any non-ASCII characters
  7. // in strings, etc.
  8. //
  9. // JScript Reference
  10. // http://msdn.microsoft.com/library/en-us/jscript7/html/jsoriProgrammingWithJScriptNET.asp
  11. //
  12. // FiddlerScript Reference
  13. // http://www.fiddlertool.com/fiddler/dev/scriptsamples.asp
  14. //
  15. // FiddlerScript Editor: 
  16. // http://www.fiddlertool.com/dl/fsesetup.exe
  17.  
  18. class Handlers
  19. {
  20.     public static RulesOption("Hide all 200s")
  21.     var m_Hide200s: boolean = false;
  22.  
  23.     public static RulesOption("Simple &ad removal")
  24.     var m_NoAD: boolean = false;
  25.  
  26.     public static RulesOption("Netscape &3", "&User-Agents", true)
  27.     var m_NS3: boolean = false;
  28.  
  29.     public static RulesOption("&Firefox 1.5", "&User-Agents", true)
  30.     var m_FFox: boolean = false;
  31.     
  32.     public static RulesOption("IE &6 (XPSP2)", "&User-Agents", true)
  33.     var m_IE6: boolean = false;
  34.     
  35.     public static RulesOption("IE &7 (Vista)", "&User-Agents", true)
  36.     var m_IE7: boolean = false;
  37.  
  38.     public static RulesOption("IE &8 (Win2k3)", "&User-Agents", true, true)
  39.     var m_IE8: boolean = false;
  40.  
  41.     public static RulesOption("&Disabled", "&User-Agents", true)
  42.     var m_UANONE: boolean = true;
  43.  
  44.     public static RulesOption("Request &Japanese Content")
  45.     var m_Japanese: boolean = false;
  46.     
  47.     public static RulesOption("Simulate &Modem speeds")
  48.     var m_SimulateModem: boolean = false;
  49.  
  50.     // Force a manual reload of the script file.  Resets all
  51.     // RulesOption variables to their defaults.
  52.     public static ToolsAction("Reset Script")
  53.     function DoManualReload(){ 
  54.         FiddlerObject.ReloadScript();
  55.     }
  56.  
  57.     public static ToolsAction("Enumerate Selected Sessions")
  58.     function DoRunSessions(oSessions: Fiddler.Session[]){
  59.         if (null != oSessions){
  60.             MessageBox.Show("Number of selected sessions: " + oSessions.Length);
  61.         }
  62.     
  63.         for (var x = 0; x < oSessions.Length; x++){
  64.             MessageBox.Show("Result: " + oSessions[x].responseCode);
  65.             Clipboard.SetDataObject(oSessions[x].responseCode.ToString());
  66.         }
  67.     }
  68.  
  69.     public static ContextAction("Reissue Selected Requests")
  70.     function ReRunSessions(oSessions: Fiddler.Session[]){
  71.         if (null == oSessions){
  72.             MessageBox.Show("Please select sessions to reissue.", "Nothing to Do");
  73.             return;
  74.         }
  75.     
  76.         for (var x = 0; x < oSessions.Length; x++){
  77.             FiddlerObject.utilIssueRequest(oSessions[x].oRequest.headers.ToString(true, true) +
  78.                                             System.Text.Encoding.UTF8.GetString(oSessions[x].requestBodyBytes));
  79.         }
  80.     }
  81.  
  82.     static function OnBoot(){
  83. //        MessageBox.Show("Fiddler has finished booting");
  84. //        System.Diagnostics.Process.Start("iexplore.exe");
  85.  
  86.         // FiddlerObject.UI.miManipulateIgnoreImages.Checked = false;
  87.  
  88. //        FiddlerObject.UI.ActivateRequestInspector("HEADERS");
  89. //        FiddlerObject.UI.ActivateResponseInspector("HEADERS");
  90.     }
  91.     
  92.     static function OnShutdown(){
  93. //        MessageBox.Show("Fiddler has shutdown");
  94.     }
  95.     
  96.     static function OnAttach(){
  97. //        MessageBox.Show("Fiddler is now the system proxy");
  98. //        System.Diagnostics.Process.Start("proxycfg.exe", "-u");    // Notify WinHTTP of proxy change
  99.     }
  100.  
  101.     static function OnDetach(){
  102. //        MessageBox.Show("Fiddler is no longer the system proxy");
  103. //        System.Diagnostics.Process.Start("proxycfg.exe", "-u");    // Notify WinHTTP of proxy change
  104.     }
  105.  
  106.     static function OnBeforeRequest(oSession:Fiddler.Session)
  107.     {
  108.         if ((null != gs_ReplaceToken) && (oSession.url.indexOf(gs_ReplaceToken)>-1)){    
  109.             oSession.url = oSession.url.Replace(gs_ReplaceToken, gs_ReplaceTokenWith); 
  110.         }
  111.  
  112.         if ((null!=bpRequestURI) && (oSession.url.indexOf(bpRequestURI)>-1)){
  113.             oSession["x-breakrequest"]="uri";
  114.         }        
  115.         
  116.         if ((null!=bpMethod) && (oSession.oRequest.headers.HTTPMethod == bpMethod)){
  117.             oSession["x-breakrequest"]="method";
  118.         }        
  119.  
  120.         if (m_SimulateModem){
  121.             // Delay sends by 300ms per KB uploaded.
  122.             oSession["request-trickle-delay"] = "300"; 
  123.         }
  124.  
  125.         if (m_NoAD &&
  126.             (oSession.host.indexOf("ad.")>-1 || 
  127.             oSession.host.indexOf("ad2")>-1 ||  
  128.             oSession.url.indexOf("/ad")>-1)){
  129.             oSession.oRequest.FailSession(403, "Advertisement", "This looks like an ad");
  130.         }
  131.  
  132.         // User-Agent Overrides
  133.         if (m_NS3){
  134.             oSession.oRequest["User-Agent"] = "Mozilla/3.0 (Win95; I)"; 
  135.         }
  136.         else
  137.         if (m_FFox){
  138.             oSession.oRequest["User-Agent"] = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.0.5) Gecko/20060719 Firefox/1.5.0.5)"; 
  139.         }
  140.         else
  141.         if (m_IE6){
  142.             oSession.oRequest["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"; 
  143.         }
  144.         else
  145.         if (m_IE7){
  146.             oSession.oRequest["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1)"; 
  147.         }
  148.         else
  149.         if (m_IE8){
  150.             oSession.oRequest["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; WOW64)"; 
  151.         }
  152.         
  153.  
  154.         if (m_Japanese){
  155.             oSession.oRequest["Accept-Language"] = "ja";
  156.         }
  157.  
  158.         //if (oSession.url.toLowerCase().indexOf("/sandbox/")>-1){
  159.         //    oSession.oFlags["x-breakrequest"] = "yup";        // The very existence of this Flag triggers breakpoint; value is unimportant.
  160.         //}
  161.  
  162.     }
  163.     
  164.     static function OnBeforeResponse(oSession:Fiddler.Session)
  165.        {
  166.         if (m_SimulateModem){
  167.             // Delay receives by 150ms per KB downloaded.
  168.             oSession["response-trickle-delay"] = "150"; 
  169.         }
  170.  
  171.         if (m_Hide200s && oSession.responseCode == 200){
  172.             oSession["ui-hide"] = "true";
  173.         }
  174.         
  175.         if ((bpStatus>0) && (oSession.responseCode == bpStatus)){
  176.             oSession["x-breakresponse"]="status";
  177.         }
  178.  
  179.         // Uncomment to reduce incidence of "unexpected socket closure" exceptions in .NET code.  
  180.         // Note that you really should also fix your .NET code to gracefully handle unexpected connection closure.
  181.         //
  182.         // if ((oSession.responseCode != 401) && (oSession.responseCode != 407)){
  183.         //   oSession.oResponse["Connection"] = "close";
  184.         // }
  185.  
  186.     }
  187.         
  188.     static function Main()
  189.     {
  190.           var today: Date = new Date();
  191.         FiddlerObject.StatusText = " CustomRules.js was loaded at: " + today;
  192.     }
  193.  
  194.  
  195.     // These static variables are used for simple breakpointing    
  196.     static var bpRequestURI:String = null;
  197.     static var bpStatus:int = -1;
  198.     static var bpMethod: String = null;
  199.     static var gs_ReplaceToken: String = null;
  200.     static var gs_ReplaceTokenWith: String = null;
  201.     
  202.     // The ExecAction function is called by either the QuickExec box in the Fiddler window,
  203.     // or by the ExecAction.exe command line utility.
  204.     static function OnExecAction(sParams: String[]){
  205.     FiddlerObject.StatusText = "ExecAction: " + sParams[0];
  206.         
  207.     var sAction = sParams[0].toLowerCase();
  208.     switch (sAction){
  209.     
  210.     case "bp":
  211.             FiddlerObject.alert("bpu = breakpoint request uri\nbpm = breakpoint request method\nbps=breakpoint response status");
  212.         break;
  213.     case "bps":
  214.         if (sParams.Length<2) {bpStatus=-1; FiddlerObject.StatusText="Response Status breakpoint cleared"; return;}
  215.         bpStatus = parseInt(sParams[1]); FiddlerObject.StatusText="Response status breakpoint for "+sParams[1];
  216.         break;
  217.     case "bpv":
  218.     case "bpm":
  219.         if (sParams.Length<2) {bpMethod=null; FiddlerObject.StatusText="Request Method breakpoint cleared"; return;}
  220.         bpMethod = sParams[1].toUpperCase(); FiddlerObject.StatusText="Request Method breakpoint for "+sParams[1].toUpperCase();
  221.         break;
  222.     case "bpu":
  223.         if (sParams.Length<2) {bpRequestURI=null; FiddlerObject.StatusText="RequestURI breakpoint cleared"; return;}
  224.         if (sParams[1].toLowerCase().StartsWith("http://")){sParams[1] = sParams[1].Substring(7);} 
  225.         bpRequestURI = sParams[1]; 
  226.         FiddlerObject.StatusText="RequestURI breakpoint for "+sParams[1];
  227.     break;
  228.     case "urlreplace":
  229.         if (sParams.Length<3) {gs_ReplaceToken=null; FiddlerObject.StatusText="URL Replacement cleared"; return;}
  230.         gs_ReplaceToken = sParams[1];
  231.         gs_ReplaceTokenWith = sParams[2].Replace(" ", "%20");  // Simple helper
  232.         FiddlerObject.StatusText="Replacing [" + sParams[1] + "] in URIs with [" + sParams[2] + "]";
  233.         break;
  234.     case "select":
  235.         if (sParams.Length<2) { FiddlerObject.StatusText="Please specify Content-Type to select."; return;}
  236.         FiddlerObject.UI.actSelectSessionsWithResponseHeaderValue("Content-Type", sParams[1]);
  237.         FiddlerObject.StatusText="Searching for Content-Type: " + sParams[1];
  238.         break;
  239.     case "allbut":
  240.         if (sParams.Length<2) { FiddlerObject.StatusText="Please specify Content-Type to retain during wipe."; return;}
  241.         FiddlerObject.UI.actSelectSessionsWithResponseHeaderValue("Content-Type", sParams[1]);
  242.         FiddlerObject.UI.actRemoveUnselectedSessions();
  243.         FiddlerObject.UI.lvSessions.SelectedItems.Clear();
  244.         FiddlerObject.StatusText="Removed all but Content-Type: " + sParams[1];
  245.         break;
  246.     case "stop":
  247.             FiddlerObject.UI.actDetachProxy();
  248.             break;
  249.         case "start":
  250.             FiddlerObject.UI.actAttachProxy();
  251.             break;
  252.         case "cls":
  253.         case "clear":
  254.             FiddlerObject.UI.actRemoveAllSessions();
  255.             break;
  256.     case "g":
  257.     case "go":
  258.         FiddlerObject.UI.actResumeAllSessions();
  259.         break;
  260.         case "help":
  261.         System.Diagnostics.Process.Start("http://www.fiddlertool.com/fiddler/help/quickexec.asp");
  262.             break;
  263.         case "hide":
  264.             FiddlerObject.UI.actMinimizeToTray();
  265.             break;
  266.         case "show":
  267.             FiddlerObject.UI.actRestoreWindow();
  268.             break;
  269.         case "quit":
  270.             FiddlerObject.UI.actExit();
  271.             break;
  272.         case "dump":
  273.             FiddlerObject.UI.actSelectAll();
  274.             FiddlerObject.UI.actSaveSessionsToZip("C:\\dump.zip");
  275.             FiddlerObject.UI.actRemoveAllSessions();
  276.             break;
  277.         
  278.         default:
  279.             if (sAction.StartsWith("http") || sAction.StartsWith("www")){
  280.                 System.Diagnostics.Process.Start(sAction);
  281.             }
  282.             else
  283.             FiddlerObject.StatusText = "Requested action: " + sAction + " not found.";
  284.         }
  285.     }
  286. }