23
23
import java .io .DataInputStream ;
24
24
import java .io .IOException ;
25
25
import java .io .OutputStream ;
26
+ import java .math .BigInteger ;
26
27
import java .net .*;
27
28
import java .security .GeneralSecurityException ;
28
29
import java .security .MessageDigest ;
29
30
import java .util .*;
30
31
import java .util .concurrent .ExecutorService ;
31
32
import java .util .concurrent .Executors ;
33
+ import java .util .concurrent .ThreadLocalRandom ;
32
34
33
35
/**
34
36
* @author Gianlu
@@ -90,8 +92,8 @@ public class ZeroconfServer implements Closeable {
90
92
private final Session .Inner inner ;
91
93
private final DiffieHellman keys ;
92
94
private final List <SessionListener > sessionListeners ;
93
- private volatile Session session ;
94
95
private final Zeroconf zeroconf ;
96
+ private volatile Session session ;
95
97
96
98
private ZeroconfServer (Session .Inner inner , Configuration conf ) throws IOException {
97
99
this .inner = inner ;
@@ -133,6 +135,7 @@ private ZeroconfServer(Session.Inner inner, Configuration conf) throws IOExcepti
133
135
}
134
136
135
137
zeroconf = new Zeroconf ();
138
+ zeroconf .setLocalHostName (getUsefulHostname ());
136
139
zeroconf .setUseIpv4 (true ).setUseIpv6 (false );
137
140
zeroconf .addNetworkInterfaces (nics );
138
141
@@ -146,6 +149,18 @@ private ZeroconfServer(Session.Inner inner, Configuration conf) throws IOExcepti
146
149
zeroconf .announce (service );
147
150
}
148
151
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
+
149
164
@ NotNull
150
165
public static ZeroconfServer create (@ NotNull AbsConfiguration conf ) throws IOException {
151
166
ApResolver .fillPool ();
0 commit comments