UNPKG

mockttp

Version:

Mock HTTP server for testing HTTP clients and stubbing webservices

235 lines 11.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.requireSocketResetSupport = exports.isSocketLoop = exports.isLocalIPv6Available = void 0; exports.isLocalPortActive = isLocalPortActive; exports.getParentSocket = getParentSocket; exports.resetOrDestroy = resetOrDestroy; exports.buildRawSocketEventData = buildRawSocketEventData; exports.buildTlsSocketEventData = buildTlsSocketEventData; exports.buildSocketTimingInfo = buildSocketTimingInfo; exports.buildSocketErrorRequestTimings = buildSocketErrorRequestTimings; const _ = require("lodash"); const os = require("os"); const net = require("net"); const tls = require("tls"); const http2 = require("http2"); const util_1 = require("./util"); const now = () => performance.now(); const socket_extensions_1 = require("./socket-extensions"); const socket_metadata_1 = require("./socket-metadata"); const ip_utils_1 = require("./ip-utils"); // Test if a local port for a given interface (IPv4/6) is currently in use, by attempting // to bind to it. We reuse a single server instance and cache results to avoid creating // a new server on every request. Concurrent checks for the same port share one probe. const probeServer = net.createServer(); const portActiveCache = new Map(); const portActiveInFlight = new Map(); const PORT_ACTIVE_CACHE_TTL = 5000; // 5 seconds async function isLocalPortActive(interfaceIp, port) { if (interfaceIp === '::1' && !exports.isLocalIPv6Available) return false; const cacheKey = `${interfaceIp}:${port}`; const cached = portActiveCache.get(cacheKey); if (cached && cached.expires > Date.now()) { return cached.result; } // Deduplicate concurrent checks for the same address:port let probe = portActiveInFlight.get(cacheKey); if (!probe) { probe = probePort(interfaceIp, port); portActiveInFlight.set(cacheKey, probe); probe.then((result) => { portActiveCache.set(cacheKey, { result, expires: Date.now() + PORT_ACTIVE_CACHE_TTL }); portActiveInFlight.delete(cacheKey); }); } return probe; } // Serialized via the in-flight map above — only one probe runs at a time per key, // and different keys won't collide because they share the single probeServer sequentially // via the listen/close cycle. let probeServerReady = Promise.resolve(); function probePort(interfaceIp, port) { const result = probeServerReady.then(() => new Promise((resolve) => { probeServer.listen({ host: interfaceIp, port, ipv6Only: interfaceIp === '::1' }); probeServer.once('listening', () => { probeServer.close(() => resolve(false)); }); probeServer.once('error', () => { resolve(true); }); })); // Chain so the next probe waits for this one to fully complete probeServerReady = result.then(() => { }); return result; } // This file imported in browsers etc as it's used in handlers, but none of these methods are used // directly. It is useful though to guard sections that immediately perform actions: exports.isLocalIPv6Available = util_1.isNode ? _.some(os.networkInterfaces(), (addresses) => _.some(addresses, a => a.address === '::1')) : true; // Check whether an incoming socket is the other end of one of our outgoing sockets: const isSocketLoop = (outgoingSockets, incomingSocket) => // We effectively just compare the address & port: if they match, we've almost certainly got a loop. // I don't think it's generally possible to see the same ip on different interfaces from one process (you need // ip-netns network namespaces), but if it is, then there's a tiny chance of false positives here. If we have ip X, // and on another interface somebody else has ip X, and they send a request with the same incoming port as an // outgoing request we have on the other interface, we'll assume it's a loop. Extremely unlikely imo. _.some([...outgoingSockets], (outgoingSocket) => { if (!outgoingSocket.localAddress || !outgoingSocket.localPort) { // It's possible for sockets in outgoingSockets to be closed, in which case these properties // will be undefined. If so, we know they're not relevant to loops, so skip entirely. return false; } else { return (0, ip_utils_1.normalizeIP)(outgoingSocket.localAddress) === (0, ip_utils_1.normalizeIP)(incomingSocket.remoteAddress) && outgoingSocket.localPort === incomingSocket.remotePort; } }); exports.isSocketLoop = isSocketLoop; function getParentSocket(socket) { return socket._parent || // TLS wrapper socket.stream || // SocketWrapper socket._handle?._parentWrap?.stream; // HTTP/2 CONNECT'd TLS wrapper } const isSocketResetSupported = util_1.isNode ? !!net.Socket.prototype.resetAndDestroy : false; // Avoid errors in browsers const requireSocketResetSupport = () => { if (!isSocketResetSupported) { throw new Error('Connection reset is only supported in Node v16.17+, v18.3.0+, or later'); } }; exports.requireSocketResetSupport = requireSocketResetSupport; const isHttp2Stream = (maybeStream) => 'httpVersion' in maybeStream && maybeStream.httpVersion?.startsWith('2'); /** * Reset the socket where possible, or at least destroy it where that's not possible. * * This has a few cases for different layers of socket & tunneling, designed to * simulate a real connection reset as closely as possible. That means, in general, * we unwrap the connection as far as possible whilst still only affecting a single * request. * * In practice, we unwrap HTTP/1 & TLS back as far as we can, until we hit either an * HTTP/2 stream or a raw TCP connection. We then either send a RST_FRAME or a TCP RST * to kill that connection. */ function resetOrDestroy(requestOrSocket) { let primarySocket = (isHttp2Stream(requestOrSocket) && requestOrSocket.stream) ? requestOrSocket.stream : ('socket' in requestOrSocket && requestOrSocket.socket) ? requestOrSocket.socket : requestOrSocket; let socket = primarySocket; while (socket instanceof tls.TLSSocket) { const parent = getParentSocket(socket); if (!parent) break; // Not clear why, but it seems in some cases we run out of parents here socket = parent; } if ('rstCode' in socket) { // It's an HTTP/2 stream instance - let's kill it here. // If it's the innermost stream, i.e. this is the stream of the request we're // resetting, then we want to send an internal error. If it's a tunneling // stream, then we want to send a CONNECT error: const isOuterSocket = socket === requestOrSocket.stream; const errorCode = isOuterSocket ? http2.constants.NGHTTP2_INTERNAL_ERROR : http2.constants.NGHTTP2_CONNECT_ERROR; const h2Stream = socket; h2Stream.close(errorCode); } else { // Must be a net.Socket then, so we let's reset it for real: if (isSocketResetSupported) { try { socket.resetAndDestroy(); } catch (error) { // This could fail in funky ways if the socket is not just the right kind // of socket. We should still fail in that case, but it's useful to log // some extra data first beforehand, so we can fix this if it ever happens: console.warn(`Failed to reset on socket of type ${socket.constructor.name} with parent of type ${getParentSocket(socket)?.constructor.name}`); throw error; } } else { socket.destroy(); } } // Explicitly mark the top-level socket as destroyed too. This isn't always required, but // is good for backwards compat (<v20) as it fixes some issues where the 'destroyed' // states can end up out of sync in older Node versions. primarySocket.destroy(); } ; function buildRawSocketEventData(socket) { const timingInfo = socket[socket_extensions_1.SocketTimingInfo] || socket._parent?.[socket_extensions_1.SocketTimingInfo] || buildSocketTimingInfo(); return { remoteIpAddress: socket.remoteAddress || // Normal case socket._parent?.remoteAddress || // Pre-certCB TLS error, e.g. timeout socket[socket_extensions_1.InitialRemoteAddress], // Post-certcb, recorded by monkeypatch remotePort: socket.remotePort || socket._parent?.remotePort || socket[socket_extensions_1.InitialRemotePort], tags: (0, socket_metadata_1.getSocketMetadataTags)(socket[socket_extensions_1.SocketMetadata]), timingEvents: { startTime: timingInfo.initialSocket, connectTimestamp: timingInfo.initialSocketTimestamp, tunnelTimestamp: timingInfo.tunnelSetupTimestamp } }; } function buildTlsSocketEventData(socket) { const rawSocketData = buildRawSocketEventData(socket); const timingInfo = socket[socket_extensions_1.SocketTimingInfo] || socket._parent?.[socket_extensions_1.SocketTimingInfo] || buildSocketTimingInfo(); rawSocketData.timingEvents.handshakeTimestamp = timingInfo.tlsConnectedTimestamp; // Attached in passThroughMatchingTls TLS sniffing logic in http-combo-server: rawSocketData.tlsMetadata = socket[socket_extensions_1.TlsMetadata] || socket._parent?.[socket_extensions_1.TlsMetadata] || {}; return rawSocketData; } function buildSocketTimingInfo() { return { initialSocket: Date.now(), initialSocketTimestamp: now() }; } // If we get an error linked to a socket, we want to calculate a rough estimate of when the // request was started, using the socket timing data: function buildSocketErrorRequestTimings(socket) { if (socket?.[socket_extensions_1.SocketTimingInfo]) { if (socket[socket_extensions_1.SocketTimingInfo].lastRequestTimestamp) { // If this isn't the first request (or was partially received) we use the // most recent received request time as the start of this request: const startTimestamp = socket[socket_extensions_1.SocketTimingInfo].lastRequestTimestamp; const startTime = socket[socket_extensions_1.SocketTimingInfo].initialSocket + (startTimestamp - socket[socket_extensions_1.SocketTimingInfo].initialSocketTimestamp); return { startTime, startTimestamp }; } else { // If this is the first request, treat socket creation as the start: return { startTime: socket[socket_extensions_1.SocketTimingInfo].initialSocket, startTimestamp: socket[socket_extensions_1.SocketTimingInfo].initialSocketTimestamp }; } } else { return { startTime: Date.now(), startTimestamp: now() }; } } //# sourceMappingURL=socket-util.js.map