Skip to content

Commit ceac759

Browse files
committed
Fix for #32 - correctly advertise IP Addresses for multiple network
interfaces, without including loopback addresses.
1 parent d031eb5 commit ceac759

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

core/src/main/java/xyz/gianlu/librespot/core/ZeroconfAuthenticator.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@
2020
import java.io.DataInputStream;
2121
import java.io.IOException;
2222
import java.io.OutputStream;
23-
import java.net.InetAddress;
24-
import java.net.ServerSocket;
25-
import java.net.Socket;
26-
import java.net.URLDecoder;
23+
import java.net.*;
2724
import java.security.GeneralSecurityException;
2825
import java.security.MessageDigest;
2926
import java.util.*;
@@ -72,12 +69,25 @@ public class ZeroconfAuthenticator implements Closeable {
7269

7370
int port = session.random.nextInt((MAX_PORT - MIN_PORT) + 1) + MIN_PORT;
7471
new Thread(this.runner = new HttpRunner(port)).start();
75-
76-
ServiceInstance service = new ServiceInstance(new ServiceName("librespot._spotify-connect._tcp.local."), 0, 0, port, Name.fromString("local."), InetAddress.getAllByName("localhost"), "VERSION=1.0", "CPath=/");
72+
ArrayList<InetAddress> addressArrayList = new ArrayList<>();
73+
Enumeration<NetworkInterface> networkInterfaces= NetworkInterface.getNetworkInterfaces();
74+
while(networkInterfaces.hasMoreElements()){
75+
NetworkInterface networkInterface = networkInterfaces.nextElement();
76+
Enumeration<InetAddress> interfaceAddresses = networkInterface.getInetAddresses();
77+
if(!networkInterface.isLoopback()) {
78+
while (interfaceAddresses.hasMoreElements()) {
79+
InetAddress address = interfaceAddresses.nextElement();
80+
if(!address.isLoopbackAddress())
81+
addressArrayList.add(address);
82+
}
83+
}
84+
}
85+
InetAddress[] addressArray = addressArrayList.toArray(new InetAddress[addressArrayList.size()]);
86+
addressArrayList.add(InetAddress.getByName(InetAddress.getLocalHost().getCanonicalHostName()));
87+
ServiceInstance service = new ServiceInstance(new ServiceName("librespot._spotify-connect._tcp.local."), 0, 0, port, Name.fromString("local."), addressArray, "VERSION=1.0", "CPath=/");
7788
spotifyConnectService = mDnsService.register(service);
7889
if (spotifyConnectService == null)
7990
throw new IOException("Failed registering SpotifyConnect service!");
80-
8191
LOGGER.info("SpotifyConnect service registered successfully!");
8292
}
8393

@@ -297,4 +307,4 @@ public void close() throws IOException {
297307
serverSocket.close();
298308
}
299309
}
300-
}
310+
}

0 commit comments

Comments
 (0)