Skip to content

Commit

Permalink
fix(usebruno#124): Improve localhost handling, add cache for ipv6 & i…
Browse files Browse the repository at this point in the history
…pv4 check
  • Loading branch information
BrandonGillis committed Dec 4, 2023
1 parent 318036a commit e24b75e
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions packages/bruno-electron/src/ipc/network/axios-instance.js
@@ -1,6 +1,11 @@
const URL = require('url');
const Socket = require('net').Socket;
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) => {
if (!hostname) {
Expand All @@ -12,19 +17,28 @@ const getTld = (hostname) => {

const checkConnection = (host, port) =>
new Promise((resolve) => {
const socket = new Socket();
const key = `${host}:${port}`;
const cachedResult = connectionCache.get(key);

if (cachedResult !== undefined) {
resolve(cachedResult);
} else {
const socket = new Socket();

socket.once('connect', () => {
socket.end();
resolve(true);
});
socket.once('connect', () => {
socket.end();
connectionCache.set(key, true); // Cache successful connection
resolve(true);
});

socket.once('error', () => {
resolve(false);
});
socket.once('error', () => {
connectionCache.set(key, false); // Cache failed connection
resolve(false);
});

// Try to connect to the host and port
socket.connect(port, host);
// Try to connect to the host and port
socket.connect(port, host);
}
});

/**
Expand All @@ -43,16 +57,16 @@ function makeAxiosInstance() {
// 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)
// @see https://github.com/usebruno/bruno/issues/124
// temporarily disabling the fix (- Anoop)
// if (getTld(url.hostname) === 'localhost') {
// config.headers.Host = url.hostname; // Put original hostname in Host

// const portNumber = Number(url.port) || (url.protocol.includes('https') ? 443 : 80);
// const useIpv6 = await checkConnection('::1', portNumber);
// url.hostname = useIpv6 ? '::1' : '127.0.0.1';
// delete url.host; // Clear hostname cache
// config.url = URL.format(url);
// }
if (getTld(url.hostname) === LOCALHOST || url.hostname === LOCAL_IPV4 || url.hostname === LOCAL_IPV6) {
// use custom DNS lookup for localhost
config.lookup = (hostname, options, callback) => {
const portNumber = Number(url.port) || (url.protocol.includes('https') ? 443 : 80);
checkConnection(LOCAL_IPV6, portNumber).then((useIpv6) => {
const ip = useIpv6 ? LOCAL_IPV6 : LOCAL_IPV4;
callback(null, ip, useIpv6 ? 6 : 4);
});
};
}

config.headers['request-start-time'] = Date.now();
return config;
Expand Down

0 comments on commit e24b75e

Please sign in to comment.