'use strict';
var url = require('url');
var tunnelAgent = require('tunnel-agent');

var FIDDLER_URL = 'http://127.0.0.1:8888';

var allowedHeaders = [
  'accept',
  'accept-charset',
  'accept-encoding',
  'accept-language',
  'accept-ranges',
  'cache-control',
  'content-encoding',
  'content-language',
  // 'content-length',
  'content-location',
  'content-md5',
  'content-range',
  'content-type',
  'connection',
  'date',
  'expect',
  'max-forwards',
  'pragma',
  'referer',
  'te',
  'transfer-encoding',
  'user-agent',
  'via'
].reduce(function(set, header) {
  set[header] = true;
  return set;
}, {});

var onlyHeaders = [
  'proxy-authorization'
];

var proxy = url.parse(FIDDLER_URL);

function makeProxyHeaders(headers) {
  return Object.keys(headers)
    .filter(function(header) {
      return allowedHeaders[header.toLowerCase()];
    })
    .reduce(function(set, header) {
      set[header] = headers[header];
      return set;
    }, {});
}


var agentFactories = {
  'https:https:': tunnelAgent.httpsOverHttps,
  'https:http:': tunnelAgent.httpsOverHttp,
  'http:https:': tunnelAgent.httpOverHttps,
  'http:http:': tunnelAgent.httpOverHttp
};
function getAgentFactory(targetProtocol, proxyProtocol) {
  return agentFactories[targetProtocol + proxyProtocol].bind(tunnelAgent);
}

function addFiddlerProxy(conf) {
  if (process.env.USE_FIDDLER) {
    conf.agent = getAgentFactory(conf.port === 443 ? 'https:' : 'http:', proxy.protocol)({
      proxy: {
        host: proxy.hostname,
        port: proxy.port,
        headers: makeProxyHeaders(conf.headers)
      },
      headers: conf.headers,
      rejectUnauthorized: false
    });
  }
  return conf;
}

module.exports = function FiddlerProxyPlugin(client) {
  var previous = client.requestTransform || function identity(x) { return x; };
  client.requestTransform = function(conf) {
    return addFiddlerProxy(previous(conf));
  };
};
@@ -81,16 +81,24 @@
 );%0A%0Avar 
+DEFAULT_
 FIDDLER_
@@ -128,16 +128,16 @@
 :8888';%0A
-
 %0Avar all
@@ -673,44 +673,8 @@
 %5D;%0A%0A
-var proxy = url.parse(FIDDLER_URL);%0A
 %0Afun
@@ -1295,24 +1295,31 @@
 erProxy(conf
+, proxy
 ) %7B%0A  if (pr
@@ -1669,16 +1669,127 @@
 xports =
+ function(opts) %7B%0A  opts = opts %7C%7C %7B url: DEFAULT_FIDDLER_URL %7D;%0A  var proxy = url.parse(opts.url);%0A  %0A  return
  functio
@@ -1819,16 +1819,18 @@
 ient) %7B%0A
+  
   var pr
@@ -1901,16 +1901,18 @@
 n x; %7D;%0A
+  
   client
@@ -1948,24 +1948,26 @@
 conf) %7B%0A    
+  
 return addFi
@@ -1991,17 +1991,31 @@
 us(conf)
-)
+, proxy);%0A    %7D
 ;%0A  %7D;%0A%7D

Add configuration object to the FiddlerProxy plugin to be able to set custom proxy URL