Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,53 @@ class Peer {
} else {
this.ip = request.connection.remoteAddress;
}

// remove the prefix used for IPv4-translated addresses
if (this.ip.substring(0,7) === "::ffff:")
this.ip = this.ip.substring(7);

// IPv4 and IPv6 use different values to refer to localhost
if (this.ip == '::1' || this.ip == '::ffff:127.0.0.1') {
// put all peers that are on the same network as the server into the same room as well
if (this.ip === '::1' || this.ipIsPrivate(this.ip)) {
this.ip = '127.0.0.1';
}
}

ipIsPrivate(ip) {
// if ip is IPv4
if (!ip.includes(":")) {
// 10.0.0.0 - 10.255.255.255 || 172.16.0.0 - 172.31.255.255 || 192.168.0.0 - 192.168.255.255
return /^(10)\.(.*)\.(.*)\.(.*)$/.test(ip) || /^(172)\.(1[6-9]|2[0-9]|3[0-1])\.(.*)\.(.*)$/.test(ip) || /^(192)\.(168)\.(.*)\.(.*)$/.test(ip)
}

// else: ip is IPv6
const firstWord = ip.split(":").find(el => !!el); //get first not empty word

// The original IPv6 Site Local addresses (fec0::/10) are deprecated. Range: fec0 - feff
if (/^fe[c-f][0-f]$/.test(firstWord))
return true;

// These days Unique Local Addresses (ULA) are used in place of Site Local.
// Range: fc00 - fcff
else if (/^fc[0-f]{2}$/.test(firstWord))
return true;

// Range: fd00 - fcff
else if (/^fd[0-f]{2}$/.test(firstWord))
return true;

// Link local addresses (prefixed with fe80) are not routable
else if (firstWord === "fe80")
return true;

// Discard Prefix
else if (firstWord === "100")
return true;

// Any other IP address is not Unique Local Address (ULA)
return false;
}

_setPeerId(request) {
if (request.peerId) {
this.id = request.peerId;
Expand Down