fix small bugs - working version with turn server of relay.metered.ca

This commit is contained in:
schlagmichdoch 2022-12-23 02:38:56 +01:00
parent ecc29b39d7
commit 72f3bb0e7c
4 changed files with 125 additions and 29 deletions

View file

@ -1,4 +1,5 @@
var process = require('process')
var net = require('net')
// Handle SIGINT
process.on('SIGINT', () => {
console.info("SIGINT Received, exiting...")
@ -43,10 +44,10 @@ class SnapdropServer {
});
}
_onHeaders(headers, response) {
if (response.headers.cookie && response.headers.cookie.indexOf('peerid=') > -1) return;
response.peerId = Peer.uuid();
headers.push('Set-Cookie: peerid=' + response.peerId + "; SameSite=Strict; Secure");
_onHeaders(headers, request) {
if (request.headers.cookie && request.headers.cookie.indexOf('peerid=') > -1) return;
request.peerId = Peer.uuid();
headers.push('Set-Cookie: peerid=' + request.peerId + "; SameSite=Strict; Secure");
}
_onMessage(sender, message) {
@ -181,6 +182,7 @@ class Peer {
// for keepalive
this.timerId = 0;
this.lastBeat = Date.now();
console.debug(this.name.displayName)
}
_setIP(request) {
@ -192,18 +194,48 @@ 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') {
this.ip = '127.0.0.1';
}
// put all peers on the same network as the server into the same room
if (this.ipIsPrivate(this.ip)) {
// 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)) {
this.ip = '127.0.0.1';
}
console.debug(this.ip)
}
ipIsPrivate(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)
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) {