Skip to content

Commit

Permalink
Server: Use a traffic-less way to get external IP
Browse files Browse the repository at this point in the history
Previously, the code for finding the current machine's external
IP address created a TCP connection to Cloudflare's DNS. This is unnecessary.
Instead, just creating a socket to a non-private IP suffices to find
that IP. We still use Cloudflare's IP, but we will no longer send any traffic
there as we use an UDP socket without actually sending any data.

Credit for finding this approach goes to @atsampson.
Fixes #633.

Signed-off-by: Christian Hoffmann <mail@hoffmann-christian.info>
  • Loading branch information
hoffie committed Feb 25, 2021
1 parent f95d2e9 commit 2e2bce6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@

- bug fix: fix crash if settings are changed in ASIO4ALL during a connection (contained in #796). Reverts #727 for Windows

- stop sending traffic to CloudFlare as part of the external IP detection
logic (#633, #1092), suggested by atsampson, coded by hoffie




Expand Down
10 changes: 6 additions & 4 deletions src/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@ LED bar: lbr
#define SOFTWARE_MANUAL_URL "https://jamulus.io/wiki/Software-Manual"

// determining server internal address uses well-known host and port
// (You can change the service used here to something like Cloudflare (1.1.1.1), Google DNS (8.8.8.8), or something else reliable)
#define WELL_KNOWN_HOST "1.1.1.1" // CloudFlare
#define WELL_KNOWN_PORT 53 // DNS
#define IP_LOOKUP_TIMEOUT 500 // ms
// We just need a valid, public Internet IP here. We will not send any
// traffic there as we will only set up an UDP socket without sending any
// data.
#define WELL_KNOWN_HOST "1.1.1.1" // CloudFlare
#define WELL_KNOWN_PORT DEFAULT_PORT_NUMBER
#define IP_LOOKUP_TIMEOUT 500 // ms

// system sample rate (the sound card and audio coder works on this sample rate)
#define SYSTEM_SAMPLE_RATE_HZ 48000 // Hz
Expand Down
1 change: 1 addition & 0 deletions src/serverlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ CServerListManager::CServerListManager ( const quint16 iNPortNum,
// User-supplied --serverpublicip
qhaServerPublicIP = QHostAddress ( strServerPublicIP );
}
qDebug() << "Using" << qhaServerPublicIP.toString() << "as external IP.";
SlaveCurLocalHostAddress = CHostAddress ( qhaServerPublicIP, iNPortNum );

// prepare the server info information
Expand Down
5 changes: 4 additions & 1 deletion src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,10 @@ bool NetworkUtil::ParseNetworkAddress ( QString strAddress,

CHostAddress NetworkUtil::GetLocalAddress()
{
QTcpSocket socket;
QUdpSocket socket;
// As we are using UDP, the connectToHost() does not generate any traffic at all.
// We just require a socket which is pointed towards the Internet in
// order to find out the IP of our own external interface:
socket.connectToHost ( WELL_KNOWN_HOST, WELL_KNOWN_PORT );

if ( socket.waitForConnected ( IP_LOOKUP_TIMEOUT ) )
Expand Down
2 changes: 1 addition & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#pragma once

#include <QCoreApplication>
#include <QTcpSocket>
#include <QUdpSocket>
#include <QHostAddress>
#include <QHostInfo>
#ifndef HEADLESS
Expand Down

0 comments on commit 2e2bce6

Please sign in to comment.