fix(#124): Improve localhost handling, add cache for ipv6 & ipv4 check

This commit is contained in:
Brandon Gillis 2023-12-04 13:36:32 +01:00
parent 318036a279
commit e24b75e7d8

View File

@ -1,6 +1,11 @@
const URL = require('url'); const URL = require('url');
const Socket = require('net').Socket; const Socket = require('net').Socket;
const axios = require('axios'); const axios = require('axios');
const connectionCache = new Map(); // Cache to store checkConnection() results
const LOCAL_IPV6 = '::1';
const LOCAL_IPV4 = '127.0.0.1';
const LOCALHOST = 'localhost';
const getTld = (hostname) => { const getTld = (hostname) => {
if (!hostname) { if (!hostname) {
@ -12,19 +17,28 @@ const getTld = (hostname) => {
const checkConnection = (host, port) => const checkConnection = (host, port) =>
new Promise((resolve) => { new Promise((resolve) => {
const socket = new Socket(); const key = `${host}:${port}`;
const cachedResult = connectionCache.get(key);
socket.once('connect', () => { if (cachedResult !== undefined) {
socket.end(); resolve(cachedResult);
resolve(true); } else {
}); const socket = new Socket();
socket.once('error', () => { socket.once('connect', () => {
resolve(false); socket.end();
}); connectionCache.set(key, true); // Cache successful connection
resolve(true);
});
// Try to connect to the host and port socket.once('error', () => {
socket.connect(port, host); connectionCache.set(key, false); // Cache failed connection
resolve(false);
});
// Try to connect to the host and port
socket.connect(port, host);
}
}); });
/** /**
@ -43,16 +57,16 @@ function makeAxiosInstance() {
// Resolve all *.localhost to localhost and check if it should use IPv6 or IPv4 // Resolve all *.localhost to localhost and check if it should use IPv6 or IPv4
// RFC: 6761 section 6.3 (https://tools.ietf.org/html/rfc6761#section-6.3) // RFC: 6761 section 6.3 (https://tools.ietf.org/html/rfc6761#section-6.3)
// @see https://github.com/usebruno/bruno/issues/124 // @see https://github.com/usebruno/bruno/issues/124
// temporarily disabling the fix (- Anoop) if (getTld(url.hostname) === LOCALHOST || url.hostname === LOCAL_IPV4 || url.hostname === LOCAL_IPV6) {
// if (getTld(url.hostname) === 'localhost') { // use custom DNS lookup for localhost
// config.headers.Host = url.hostname; // Put original hostname in Host config.lookup = (hostname, options, callback) => {
const portNumber = Number(url.port) || (url.protocol.includes('https') ? 443 : 80);
// const portNumber = Number(url.port) || (url.protocol.includes('https') ? 443 : 80); checkConnection(LOCAL_IPV6, portNumber).then((useIpv6) => {
// const useIpv6 = await checkConnection('::1', portNumber); const ip = useIpv6 ? LOCAL_IPV6 : LOCAL_IPV4;
// url.hostname = useIpv6 ? '::1' : '127.0.0.1'; callback(null, ip, useIpv6 ? 6 : 4);
// delete url.host; // Clear hostname cache });
// config.url = URL.format(url); };
// } }
config.headers['request-start-time'] = Date.now(); config.headers['request-start-time'] = Date.now();
return config; return config;