2323import java .io .DataInputStream ;
2424import java .io .IOException ;
2525import java .io .OutputStream ;
26+ import java .math .BigInteger ;
2627import java .net .*;
2728import java .security .GeneralSecurityException ;
2829import java .security .MessageDigest ;
2930import java .util .*;
3031import java .util .concurrent .ExecutorService ;
3132import 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