@@ -62,35 +62,62 @@ public class ZeroconfAuthenticator implements Closeable {
6262 private final Session .Inner session ;
6363 private final DiffieHellman keys ;
6464
65- ZeroconfAuthenticator (Session .Inner session ) throws IOException {
65+ ZeroconfAuthenticator (Session .Inner session , Configuration conf ) throws IOException {
6666 this .session = session ;
6767 this .keys = new DiffieHellman (session .random );
6868 this .mDnsService = new MulticastDNSService ();
6969
7070 int port = session .random .nextInt ((MAX_PORT - MIN_PORT ) + 1 ) + MIN_PORT ;
7171 new Thread (this .runner = new HttpRunner (port )).start ();
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- }
72+
73+ InetAddress [] bound ;
74+ if (conf .zeroconfListenAll ()) {
75+ bound = getAllInterfacesAddresses ();
76+ } else {
77+ String [] interfaces = conf .zeroconfInterfaces ();
78+ if (interfaces .length == 0 ) {
79+ bound = new InetAddress []{InetAddress .getLoopbackAddress ()};
80+ } else {
81+ List <InetAddress > list = new ArrayList <>();
82+ for (String str : interfaces ) addAddressForInterfaceName (list , str );
83+ bound = list .toArray (new InetAddress [0 ]);
8384 }
8485 }
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=/" );
86+
87+ LOGGER .debug ("Registering service on " + Arrays .toString (bound ));
88+
89+ ServiceInstance service = new ServiceInstance (new ServiceName ("librespot._spotify-connect._tcp.local." ), 0 , 0 , port , Name .fromString ("local." ), bound , "VERSION=1.0" , "CPath=/" );
8890 spotifyConnectService = mDnsService .register (service );
8991 if (spotifyConnectService == null )
9092 throw new IOException ("Failed registering SpotifyConnect service!" );
9193 LOGGER .info ("SpotifyConnect service registered successfully!" );
9294 }
9395
96+ private static void addAddressForInterfaceName (List <InetAddress > list , @ NotNull String name ) throws SocketException {
97+ NetworkInterface nif = NetworkInterface .getByName (name );
98+ if (nif == null ) {
99+ LOGGER .warn (String .format ("Interface %s doesn't exists." , name ));
100+ return ;
101+ }
102+
103+ addAddressOfInterface (list , nif );
104+ }
105+
106+ private static void addAddressOfInterface (List <InetAddress > list , @ NotNull NetworkInterface nif ) {
107+ LOGGER .trace (String .format ("Adding addresses of %s (displayName: %s)" , nif .getName (), nif .getDisplayName ()));
108+ Enumeration <InetAddress > ias = nif .getInetAddresses ();
109+ while (ias .hasMoreElements ())
110+ list .add (ias .nextElement ());
111+ }
112+
113+ @ NotNull
114+ private static InetAddress [] getAllInterfacesAddresses () throws SocketException {
115+ List <InetAddress > list = new ArrayList <>();
116+ Enumeration <NetworkInterface > is = NetworkInterface .getNetworkInterfaces ();
117+ while (is .hasMoreElements ()) addAddressOfInterface (list , is .nextElement ());
118+ return list .toArray (new InetAddress [0 ]);
119+ }
120+
94121 @ Override
95122 public void close () throws IOException {
96123 mDnsService .unregister (spotifyConnectService );
@@ -204,6 +231,13 @@ Authentication.LoginCredentials lockUntilCredentials() throws InterruptedExcepti
204231 }
205232 }
206233
234+ public interface Configuration {
235+ boolean zeroconfListenAll ();
236+
237+ @ NotNull
238+ String [] zeroconfInterfaces ();
239+ }
240+
207241 private class HttpRunner implements Runnable , Closeable {
208242 private final ServerSocket serverSocket ;
209243 private volatile boolean shouldStop = false ;
0 commit comments