Skip to content

Commit

Permalink
Improve LAN preview network to detect local ip properly (#7170)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementPasteau authored Nov 18, 2024
1 parent dda85cf commit 13aab9a
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions newIDE/electron-app/app/Utils/LocalNetworkIpFinder.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
const os = require('os');

/**
* As defined in RFC 1918, the following IPv4 address ranges are reserved for private networks:
* https://datatracker.ietf.org/doc/html/rfc1918#section-3
*
* @param {string} ip
* @returns {boolean}
*/
const isPrivateIp = ip => {
const [first, second] = ip.split('.').map(Number);
return (
first === 10 ||
(first === 172 && second >= 16 && second <= 31) ||
(first === 192 && second === 168)
);
};

/** @returns {string[]} */
const getLocalNetworkIps = () => {
return Object.entries(os.networkInterfaces())
.flatMap(([name, interfaces]) =>
name.match(/^(VirtualBox|VMware)/) ? [] : interfaces
)
.filter(({ family, internal }) => family === 'IPv4' && !internal)
.filter(
({ family, internal, address }) =>
family === 'IPv4' && !internal && isPrivateIp(address)
)
.map(({ address }) => address);
};

Expand All @@ -19,14 +38,6 @@ const findLocalIp = () => {

if (!ipAddresses.length) return null;

let firstLocalIp = ipAddresses.find(
ipAddress => ipAddress.indexOf('192.168') === 0
);
if (firstLocalIp) return firstLocalIp;

firstLocalIp = ipAddresses.find(ipAddress => ipAddress.indexOf('192') === 0);
if (firstLocalIp) return firstLocalIp;

return ipAddresses[0];
};

Expand Down

1 comment on commit 13aab9a

@arthuro555
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another prefix that is occasionally used is the 100.64.0.0/10. It is meant for NAT technically, but in practice it is reserved for private networks and used at the discretion of the ISP, and when you've got a funky ISP, like for example my university network which is its own ISP, they can use them as normal local addresses (speaking from experience, writing this comment from a network where my computer's local IP is in this range 😅)... it's also the IP range used by VPNs like tailscale, which some users might be using to put devices on the same (virtual) private network when they wouldn't normally be able to access each other!

Another thing is IPv6, it maybe shouldn't be thrown out the window like that.... Another funky network I've had experience with is the one my workstation is connected to, which only assigns IPv6 addresses locally, no local IPv4... 😓

Please sign in to comment.