Move ip prefix removal outside ipIsPrivate method. Remove 'net' dependency.
This commit is contained in:
parent
8eab6b5ae0
commit
0d47bf176a
1 changed files with 9 additions and 7 deletions
14
index.js
14
index.js
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue