Move ip prefix removal outside ipIsPrivate method. Remove 'net' dependency.

This commit is contained in:
schlagmichdoch 2023-02-04 17:34:20 +01:00
parent 8eab6b5ae0
commit 0d47bf176a

View file

@ -1,7 +1,6 @@
const process = require('process') const process = require('process')
const crypto = require('crypto') const crypto = require('crypto')
const {spawn} = require('child_process') const {spawn} = require('child_process')
const net = require('net')
// Handle SIGINT // Handle SIGINT
process.on('SIGINT', () => { process.on('SIGINT', () => {
@ -462,18 +461,21 @@ class Peer {
} else { } else {
this.ip = request.connection.remoteAddress; 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 // IPv4 and IPv6 use different values to refer to localhost
// put all peers on the same network as the server into the same room as well // put all peers 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'; this.ip = '127.0.0.1';
} }
} }
ipIsPrivate(ip) { ipIsPrivate(ip) {
if (ip.substring(0,7) === "::ffff:") // if ip is IPv4
ip = ip.substring(7); if (!ip.includes(":")) {
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 // 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) return /^(10)\.(.*)\.(.*)\.(.*)$/.test(ip) || /^(172)\.(1[6-9]|2[0-9]|3[0-1])\.(.*)\.(.*)$/.test(ip) || /^(192)\.(168)\.(.*)\.(.*)$/.test(ip)
} }
@ -485,7 +487,7 @@ class Peer {
if (/^fe[c-f][0-f]$/.test(firstWord)) if (/^fe[c-f][0-f]$/.test(firstWord))
return true; return true;
// These days Unique Local Addresses (ULA) are used in place of Site Local. // These days Unique Local Addresses (ULA) are used in place of Site Local.
// Range: fc00 - fcff // Range: fc00 - fcff
else if (/^fc[0-f]{2}$/.test(firstWord)) else if (/^fc[0-f]{2}$/.test(firstWord))
return true; return true;