From 585f1ba2ec968f58af571a1aba92efa4fab16a54 Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Fri, 6 Jan 2023 16:49:37 +0100 Subject: [PATCH 1/3] put all devices that are on the same network as the server into the same room to make it possible to run snapdrop on the local network --- server/index.js | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/server/index.js b/server/index.js index c47feb1e..fc0fb020 100644 --- a/server/index.js +++ b/server/index.js @@ -188,11 +188,49 @@ class Peer { this.ip = request.connection.remoteAddress; } // 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.ip === '::ffff:127.0.0.1' || this.ip === '::1' || this.ipIsPrivate(this.ip)) { this.ip = '127.0.0.1'; } } + ipIsPrivate(ip) { + if (ip.substring(0,7) === "::ffff:") + ip = ip.substring(7); + + if (net.isIPv4(ip)) { + // 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; From 82533c0017b8f20c9bf6d536c2aa2dfdc45f07da Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Sat, 28 Jan 2023 15:30:04 +0100 Subject: [PATCH 2/3] add 'net' import needed to put all local devices in the same room --- server/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/server/index.js b/server/index.js index fc0fb020..a6a30ac0 100644 --- a/server/index.js +++ b/server/index.js @@ -1,3 +1,4 @@ +const net = require('net'); var process = require('process') // Handle SIGINT process.on('SIGINT', () => { From f823d851c0fb29473cfdbf759cebd25915905ddf Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Sat, 4 Feb 2023 17:34:20 +0100 Subject: [PATCH 3/3] Move ip prefix removal outside ipIsPrivate method. Remove 'net' dependency. --- server/index.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/server/index.js b/server/index.js index a6a30ac0..4c4249a1 100644 --- a/server/index.js +++ b/server/index.js @@ -1,4 +1,3 @@ -const net = require('net'); var process = require('process') // Handle SIGINT process.on('SIGINT', () => { @@ -188,18 +187,21 @@ 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 // put all peers that are on the same network as the server into the same room as well - if (this.ip === '::1' || this.ip === '::ffff:127.0.0.1' || this.ip === '::1' || this.ipIsPrivate(this.ip)) { + if (this.ip === '::1' || this.ipIsPrivate(this.ip)) { this.ip = '127.0.0.1'; } } ipIsPrivate(ip) { - if (ip.substring(0,7) === "::ffff:") - ip = ip.substring(7); - - if (net.isIPv4(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) }