Skip to content

Commit f645600

Browse files
funtaxma5309devgianlu
authored
Make device id configurable (#178)
* Allow settings of deviceId for blob-authentication * Use hex string for device ID + minor changes Co-authored-by: ma5309 <[email protected]> Co-authored-by: devgianlu <[email protected]>
1 parent d9b2c8e commit f645600

5 files changed

Lines changed: 20 additions & 1 deletion

File tree

common/src/main/java/xyz/gianlu/librespot/common/Utils.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ public final class Utils {
3434
private Utils() {
3535
}
3636

37+
@NotNull
38+
public static String randomHexString(@NotNull Random random, int length) {
39+
byte[] bytes = new byte[length / 2];
40+
random.nextBytes(bytes);
41+
return bytesToHex(bytes, 0, bytes.length, false, length);
42+
}
43+
3744
@NotNull
3845
public static String randomString(@NotNull Random random, int length) {
3946
char[] chars = new char[length];

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
*/
1818
public abstract class AbsConfiguration implements ApiConfiguration, Session.ProxyConfiguration, TimeProvider.Configuration, Player.Configuration, CacheManager.Configuration, AuthConfiguration, ZeroconfServer.Configuration {
1919

20+
@Nullable
21+
public abstract String deviceId();
22+
2023
@Nullable
2124
public abstract String deviceName();
2225

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ public boolean stopPlaybackOnChunkError() {
304304
return config.get("player.stopPlaybackOnChunkError");
305305
}
306306

307+
@Override
308+
public @Nullable String deviceId() {
309+
return config.get("deviceId");
310+
}
311+
307312
@Override
308313
public @Nullable String deviceName() {
309314
return config.get("deviceName");

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,10 @@ private Inner(Connect.DeviceType deviceType, String deviceName, AbsConfiguration
667667
this.deviceName = deviceName;
668668
this.configuration = configuration;
669669
this.random = new SecureRandom();
670-
this.deviceId = Utils.randomString(random, 40);
670+
671+
String configuredDeviceId = configuration.deviceId();
672+
this.deviceId = (configuredDeviceId == null || configuredDeviceId.isEmpty()) ?
673+
Utils.randomHexString(random, 40).toLowerCase() : configuredDeviceId;
671674
}
672675

673676
@NotNull

core/src/main/resources/default.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
deviceId = "" ### Device ID (40 chars, leave empty for random) ###
12
deviceName = "librespot-java" ### Device name ###
23
deviceType = "COMPUTER" ### Device type (COMPUTER, TABLET, SMARTPHONE, SPEAKER, TV, AVR, STB, AUDIO_DONGLE, GAME_CONSOLE, CAST_VIDEO, CAST_AUDIO, AUTOMOBILE, WEARABLE, UNKNOWN_SPOTIFY, CAR_THING, UNKNOWN) ###
34
preferredLocale = "en" ### Preferred locale ###

0 commit comments

Comments
 (0)