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CAAC,CAAC;AAC/B,MAAI,CAAC,KAAK,CAAC,UAAU,CAAC,EACpB,KAAK,CAAC,UAAU,CAAC,GAAG,MAAM,CAAC;AAC7B,QAAM,CAAC,KAAK,CAAC,UAAU,CAAC,KAAK,MAAM,iCACC,MAAM,YAAO,KAAK,CAAC,UAAU,CAAC,CAAG,CAAC;;;AAGtE,iBAAe,EAAE,CAAC;CACnB,CAAC","file":"reroute.js","sourcesContent":["// Domain routing and port forwarding\n//\n// Used for mapping hosts and domains to localhost, so you can open TCP\n// connections with friendly hostnames to test against the local server.\n//\n// Can also map any source port to any destination port, so you can use port 80\n// to access localhost server running on unprivileged port.\n\n\nconst assert  = require('assert');\nconst Net     = require('net');\n\n\n// Routing table.\n//\n// key   - Source host name or wildcard (e.g. \"example.com\", \"*.example.com\")\n// value - Object that maps source port to target port\nconst routing = new Map();\n\n// Flip this from enableRerouting() so we only inject our code into\n// Socket.connect once.\nlet   enabled = false;\n\n\n// If there's a route for host/port, returns destination port number.\n//\n// Called recursively to handle wildcards.  Starting with the host\n// www.example.com, it will attempt to match routes from most to least specific:\n//\n//   www.example.com\n// *.www.example.com\n//     *.example.com\n//             *.com\nfunction findTargetPort(hostname, port) {\n  if (!hostname)\n    return null;\n\n  const route = routing.get(hostname);\n  if (route)\n    return route[port];\n\n  // This will first expand www.hostname.com to *.www.hostname.com,\n  // then contract it to *.hostname.com, *.com and finally *.\n  const wildcard = hostname.replace(/^(\\*\\.[^.]+(\\.|$))?/, '*.');\n  if (wildcard !== '*.')\n    return findTargetPort(wildcard, port);\n}\n\n\n// Called once to hack Socket.connect\nfunction enableRerouting() {\n  if (enabled)\n    return;\n  enabled = true;\n\n  const connect = Net.Socket.prototype.connect;\n  Net.Socket.prototype.connect = function(options, callback) {\n    if (Array.isArray(options) && options.some(opts => findTargetPort(opts.host, opts.port))) {\n      // we hackily update the array in place, because the array has already been extended with\n      // extra methods\n      options.forEach((opts, i) => {\n        const port = findTargetPort(opts.host, opts.port);\n        if (port)\n          options[i] = Object.assign({}, opts, { host: 'localhost', port });\n      });\n      return connect.call(this, options, callback);\n    }\n    if (options && typeof options === 'object' && !Array.isArray(options)) {\n      const port = findTargetPort(options.host, options.port);\n      if (port) {\n        options = Object.assign({}, options, { host: 'localhost', port });\n        return connect.call(this, options, callback);\n      }\n    }\n    return connect.apply(this, arguments);\n  };\n}\n\n\n// source - Hostname or host:port (default to port 80)\n// target - Target port number\nmodule.exports = function addRoute(source, target) {\n  assert(source, 'Expected source address of the form \"host:port\" or just \"host\"');\n  const sourceHost    = source.split(':')[0];\n  const sourcePort    = source.split(':')[1] || 80;\n  const route         = routing.get(sourceHost) || {};\n  routing.set(sourceHost, route);\n  if (!route[sourcePort])\n    route[sourcePort] = target;\n  assert(route[sourcePort] === target,\n         `Already have routing from ${source} to ${route[sourcePort]}`);\n\n  // Enable Socket.connect routing\n  enableRerouting();\n};\n"]}