11package xyz .gianlu .librespot .core ;
22
33import com .google .gson .JsonObject ;
4- import net .posick .mdns .MulticastDNSService ;
5- import net .posick .mdns .ServiceInstance ;
6- import net .posick .mdns .ServiceName ;
74import org .apache .log4j .Logger ;
85import org .jetbrains .annotations .NotNull ;
9- import org .xbill .DNS .Name ;
106import xyz .gianlu .librespot .AbsConfiguration ;
117import xyz .gianlu .librespot .Version ;
128import xyz .gianlu .librespot .common .Utils ;
1915import javax .crypto .Mac ;
2016import javax .crypto .spec .IvParameterSpec ;
2117import javax .crypto .spec .SecretKeySpec ;
18+ import javax .jmdns .JmDNS ;
19+ import javax .jmdns .ServiceInfo ;
2220import java .io .Closeable ;
2321import java .io .DataInputStream ;
2422import java .io .IOException ;
@@ -77,16 +75,14 @@ public class ZeroconfServer implements Closeable {
7775 }
7876
7977 private final HttpRunner runner ;
80- private final MulticastDNSService mDnsService ;
81- private final ServiceInstance spotifyConnectService ;
8278 private final Session .Inner inner ;
8379 private final DiffieHellman keys ;
8480 private Session session ;
81+ private final JmDNS [] instances ;
8582
8683 private ZeroconfServer (Session .Inner inner , Configuration conf ) throws IOException {
8784 this .inner = inner ;
8885 this .keys = new DiffieHellman (inner .random );
89- this .mDnsService = new MulticastDNSService ();
9086
9187 int port = inner .random .nextInt ((MAX_PORT - MIN_PORT ) + 1 ) + MIN_PORT ;
9288 new Thread (this .runner = new HttpRunner (port ), "zeroconf-http-server" ).start ();
@@ -107,10 +103,17 @@ private ZeroconfServer(Session.Inner inner, Configuration conf) throws IOExcepti
107103
108104 LOGGER .debug ("Registering service on " + Arrays .toString (bound ));
109105
110- ServiceInstance service = new ServiceInstance (new ServiceName ("librespot-java._spotify-connect._tcp.local." ), 0 , 0 , port , Name .fromString ("local." ), bound , "VERSION=1.0" , "CPath=/" );
111- spotifyConnectService = mDnsService .register (service );
112- if (spotifyConnectService == null )
113- throw new IOException ("Failed registering SpotifyConnect service!" );
106+ Map <String , String > txt = new HashMap <>();
107+ txt .put ("CPath" , "/" );
108+ txt .put ("VERSION" , "1.0" );
109+
110+ instances = new JmDNS [bound .length ];
111+ for (int i = 0 ; i < instances .length ; i ++) {
112+ instances [i ] = JmDNS .create (bound [i ], bound [i ].getHostName ());
113+ ServiceInfo serviceInfo = ServiceInfo .create ("_spotify-connect._tcp.local." , "librespot-java" , port , 0 , 0 , txt );
114+ instances [i ].registerService (serviceInfo );
115+ }
116+
114117 LOGGER .info ("SpotifyConnect service registered successfully!" );
115118 }
116119
@@ -150,7 +153,8 @@ private static void addAddressOfInterface(List<InetAddress> list, @NotNull Netwo
150153 if (nif .isLoopback ()) return ;
151154
152155 if (isVirtual (nif )) {
153- if (checkVirtual ) return ;
156+ if (checkVirtual )
157+ return ;
154158 else
155159 LOGGER .warn (String .format ("Interface %s is suspected to be virtual, mac: %s" , nif .getName (), Utils .bytesToHex (nif .getHardwareAddress ())));
156160 }
@@ -171,10 +175,10 @@ private static InetAddress[] getAllInterfacesAddresses() throws SocketException
171175
172176 @ Override
173177 public void close () throws IOException {
174- mDnsService . unregister ( spotifyConnectService );
175- LOGGER . trace ( "SpotifyConnect service unregistered successfully." );
178+ for ( JmDNS instance : instances )
179+ instance . unregisterAllServices ( );
176180
177- mDnsService . close ( );
181+ LOGGER . trace ( "SpotifyConnect service unregistered successfully." );
178182 runner .close ();
179183 }
180184
0 commit comments