Skip to content

Commit 5a0ea7a

Browse files
committed
Configurable Zeroconf listen port
1 parent e4d80fc commit 5a0ea7a

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

conf.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ auth.password=
1212
# Spotify authentication blob (BLOB only)
1313
auth.blob=
1414
### Zeroconf ###
15+
# Listen on this port (`-1` for random)
16+
zeroconf.listenPort=-1
1517
# Listen on all interfaces (overrides `zeroconf.interfaces`)
1618
zeroconf.listenAll=true
1719
# Listen on these interfaces (comma separated list of names)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ public boolean zeroconfListenAll() {
115115
return true;
116116
}
117117

118+
@Override
119+
public int zeroconfListenPort() {
120+
return -1;
121+
}
122+
118123
@Override
119124
public @NotNull String[] zeroconfInterfaces() {
120125
return new String[0];

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.jetbrains.annotations.Nullable;
77
import xyz.gianlu.librespot.common.Utils;
88
import xyz.gianlu.librespot.core.Session;
9+
import xyz.gianlu.librespot.core.ZeroconfServer;
910
import xyz.gianlu.librespot.player.PlayerRunner;
1011
import xyz.gianlu.librespot.player.StreamFeeder;
1112

@@ -187,6 +188,17 @@ public boolean zeroconfListenAll() {
187188
return getBoolean("zeroconf.listenAll", defaults.zeroconfListenAll());
188189
}
189190

191+
@Override
192+
public int zeroconfListenPort() {
193+
int val = getInt("zeroconf.listenPort", defaults.zeroconfListenPort());
194+
if (val == -1) return val;
195+
196+
if (val < ZeroconfServer.MIN_PORT || val > ZeroconfServer.MAX_PORT)
197+
throw new IllegalArgumentException("Illegal port number: " + val);
198+
199+
return 0;
200+
}
201+
190202
@NotNull
191203
@Override
192204
public String[] zeroconfInterfaces() {

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
* @author Gianlu
3131
*/
3232
public class ZeroconfServer implements Closeable {
33-
private final static int MAX_PORT = 65536;
34-
private final static int MIN_PORT = 1024;
33+
public final static int MAX_PORT = 65536;
34+
public final static int MIN_PORT = 1024;
3535
private static final Logger LOGGER = Logger.getLogger(ZeroconfServer.class);
3636
private static final byte[] EOL = new byte[]{'\r', '\n'};
3737
private static final JsonObject DEFAULT_GET_INFO_FIELDS = new JsonObject();
@@ -77,14 +77,17 @@ public class ZeroconfServer implements Closeable {
7777
private final HttpRunner runner;
7878
private final Session.Inner inner;
7979
private final DiffieHellman keys;
80-
private Session session;
8180
private final JmDNS[] instances;
81+
private Session session;
8282

8383
private ZeroconfServer(Session.Inner inner, Configuration conf) throws IOException {
8484
this.inner = inner;
8585
this.keys = new DiffieHellman(inner.random);
8686

87-
int port = inner.random.nextInt((MAX_PORT - MIN_PORT) + 1) + MIN_PORT;
87+
int port = conf.zeroconfListenPort();
88+
if (port == -1)
89+
port = inner.random.nextInt((MAX_PORT - MIN_PORT) + 1) + MIN_PORT;
90+
8891
new Thread(this.runner = new HttpRunner(port), "zeroconf-http-server").start();
8992

9093
InetAddress[] bound;
@@ -300,6 +303,8 @@ private void handleAddUser(OutputStream out, Map<String, String> params, String
300303
public interface Configuration {
301304
boolean zeroconfListenAll();
302305

306+
int zeroconfListenPort();
307+
303308
@NotNull
304309
String[] zeroconfInterfaces();
305310
}

0 commit comments

Comments
 (0)