Check for CF-Connecting-IP and decreased timeout

When hosting Snapdrop behind Cloudflare, X-Forwarded-For is useless. Furthermore, I'm hosting my installation behind another internal proxy, so, in the end, X-Forwarder-For is set to the internal proxy's IP for all the peer, which can see each other regardless being on different networks. To fix this I made _setIP check if the header "cf-connecting-ip" exists, then sets the IP accordingly.

Lastly, when a peer changes network (e.g. a phone switches from wifi to mobile data), the function _leaveRoom is not called and the peer stays visible to others connected to the same room for a whole minute: I adjusted the timeout to what I think is a more reasonable value (500ms, so _leaveRoom is called after 1s).
This commit is contained in:
Leonardo Scoppitto 2022-11-24 13:51:39 +01:00 committed by GitHub
parent f769a76605
commit 0cd73f10ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -137,7 +137,7 @@ class SnapdropServer {
_keepAlive(peer) { _keepAlive(peer) {
this._cancelKeepAlive(peer); this._cancelKeepAlive(peer);
var timeout = 30000; var timeout = 500;
if (!peer.lastBeat) { if (!peer.lastBeat) {
peer.lastBeat = Date.now(); peer.lastBeat = Date.now();
} }
@ -182,7 +182,9 @@ class Peer {
} }
_setIP(request) { _setIP(request) {
if (request.headers['x-forwarded-for']) { if (request.headers['cf-connecting-ip']) {
this.ip = request.headers['cf-connecting-ip'].split(/\s*,\s*/)[0];
} else if (request.headers['x-forwarded-for']) {
this.ip = request.headers['x-forwarded-for'].split(/\s*,\s*/)[0]; this.ip = request.headers['x-forwarded-for'].split(/\s*,\s*/)[0];
} else { } else {
this.ip = request.connection.remoteAddress; this.ip = request.connection.remoteAddress;