Skip to content

Commit 5766289

Browse files
committed
Fixed #151 + intelligent hostname detection
1 parent d81cee0 commit 5766289

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
<dependency>
6868
<groupId>xyz.gianlu.zeroconf</groupId>
6969
<artifactId>zeroconf</artifactId>
70-
<version>1.1.1</version>
70+
<version>1.1.2</version>
7171
</dependency>
7272

7373
<!-- H2 (cache) -->

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
import java.io.DataInputStream;
2424
import java.io.IOException;
2525
import java.io.OutputStream;
26+
import java.math.BigInteger;
2627
import java.net.*;
2728
import java.security.GeneralSecurityException;
2829
import java.security.MessageDigest;
2930
import java.util.*;
3031
import java.util.concurrent.ExecutorService;
3132
import java.util.concurrent.Executors;
33+
import java.util.concurrent.ThreadLocalRandom;
3234

3335
/**
3436
* @author Gianlu
@@ -90,8 +92,8 @@ public class ZeroconfServer implements Closeable {
9092
private final Session.Inner inner;
9193
private final DiffieHellman keys;
9294
private final List<SessionListener> sessionListeners;
93-
private volatile Session session;
9495
private final Zeroconf zeroconf;
96+
private volatile Session session;
9597

9698
private ZeroconfServer(Session.Inner inner, Configuration conf) throws IOException {
9799
this.inner = inner;
@@ -133,6 +135,7 @@ private ZeroconfServer(Session.Inner inner, Configuration conf) throws IOExcepti
133135
}
134136

135137
zeroconf = new Zeroconf();
138+
zeroconf.setLocalHostName(getUsefulHostname());
136139
zeroconf.setUseIpv4(true).setUseIpv6(false);
137140
zeroconf.addNetworkInterfaces(nics);
138141

@@ -146,6 +149,18 @@ private ZeroconfServer(Session.Inner inner, Configuration conf) throws IOExcepti
146149
zeroconf.announce(service);
147150
}
148151

152+
@NotNull
153+
public static String getUsefulHostname() throws UnknownHostException {
154+
String host = InetAddress.getLocalHost().getHostName();
155+
if (host.equals("localhost")) {
156+
host = Base64.getEncoder().encodeToString(BigInteger.valueOf(ThreadLocalRandom.current().nextLong()).toByteArray()) + ".local";
157+
LOGGER.warn("Hostname cannot be `localhost`, temporary hostname: " + host);
158+
return host;
159+
}
160+
161+
return host;
162+
}
163+
149164
@NotNull
150165
public static ZeroconfServer create(@NotNull AbsConfiguration conf) throws IOException {
151166
ApResolver.fillPool();

0 commit comments

Comments
 (0)