-
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathZeroconfServer.java
More file actions
584 lines (497 loc) · 22.4 KB
/
ZeroconfServer.java
File metadata and controls
584 lines (497 loc) · 22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
* Copyright 2021 devgianlu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.gianlu.librespot;
import com.google.gson.JsonObject;
import com.spotify.connectstate.Connect;
import okhttp3.HttpUrl;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.gianlu.librespot.common.NameThreadFactory;
import xyz.gianlu.librespot.common.Utils;
import xyz.gianlu.librespot.core.Session;
import xyz.gianlu.librespot.core.TokenProvider;
import xyz.gianlu.librespot.crypto.DiffieHellman;
import xyz.gianlu.zeroconf.Service;
import xyz.gianlu.zeroconf.Zeroconf;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.Closeable;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.net.*;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
/**
* @author Gianlu
*/
public class ZeroconfServer implements Closeable {
public static final String SERVICE = "spotify-connect";
private final static int MAX_PORT = 65536;
private final static int MIN_PORT = 1024;
private static final Logger LOGGER = LoggerFactory.getLogger(ZeroconfServer.class);
private static final byte[] EOL = new byte[]{'\r', '\n'};
private static final JsonObject DEFAULT_GET_INFO_FIELDS = new JsonObject();
private static final JsonObject DEFAULT_SUCCESSFUL_ADD_USER = new JsonObject();
private static final byte[][] VIRTUAL_INTERFACES = new byte[][]{
new byte[]{(byte) 0x00, (byte) 0x0F, (byte) 0x4B}, // Virtual Iron Software, Inc.
new byte[]{(byte) 0x00, (byte) 0x13, (byte) 0x07}, // Paravirtual Corporation
new byte[]{(byte) 0x00, (byte) 0x13, (byte) 0xBE}, // Virtual Conexions
new byte[]{(byte) 0x00, (byte) 0x21, (byte) 0xF6}, // Virtual Iron Software
new byte[]{(byte) 0x00, (byte) 0x24, (byte) 0x0B}, // Virtual Computer Inc.
new byte[]{(byte) 0x00, (byte) 0xA0, (byte) 0xB1}, // First Virtual Corporation
new byte[]{(byte) 0x00, (byte) 0xE0, (byte) 0xC8}, // Virtual access, ltd.
new byte[]{(byte) 0x54, (byte) 0x52, (byte) 0x00}, // Linux kernel virtual machine (kvm)
new byte[]{(byte) 0x00, (byte) 0x21, (byte) 0xF6}, // Oracle Corporation
new byte[]{(byte) 0x18, (byte) 0x92, (byte) 0x2C}, // Virtual Instruments
new byte[]{(byte) 0x3C, (byte) 0xF3, (byte) 0x92}, // VirtualTek. Co. Ltd.
new byte[]{(byte) 0x00, (byte) 0x05, (byte) 0x69}, // VMWare 1
new byte[]{(byte) 0x00, (byte) 0x0C, (byte) 0x29}, // VMWare 2
new byte[]{(byte) 0x00, (byte) 0x50, (byte) 0x56}, // VMWare 3
new byte[]{(byte) 0x00, (byte) 0x1C, (byte) 0x42}, // Parallels
new byte[]{(byte) 0x00, (byte) 0x03, (byte) 0xFF}, // Microsoft Virtual PC
new byte[]{(byte) 0x00, (byte) 0x16, (byte) 0x3E}, // Red Hat Xen, Oracle VM, Xen Source, Novell Xen
new byte[]{(byte) 0x08, (byte) 0x00, (byte) 0x27}, // VirtualBox
new byte[]{(byte) 0x00, (byte) 0x15, (byte) 0x5D}, // Hyper-V
};
static {
DEFAULT_GET_INFO_FIELDS.addProperty("status", 101);
DEFAULT_GET_INFO_FIELDS.addProperty("statusString", "OK");
DEFAULT_GET_INFO_FIELDS.addProperty("spotifyError", 0);
DEFAULT_GET_INFO_FIELDS.addProperty("version", "2.7.1");
DEFAULT_GET_INFO_FIELDS.addProperty("libraryVersion", Version.versionNumber());
DEFAULT_GET_INFO_FIELDS.addProperty("accountReq", "PREMIUM");
DEFAULT_GET_INFO_FIELDS.addProperty("brandDisplayName", "librespot-org");
DEFAULT_GET_INFO_FIELDS.addProperty("modelDisplayName", "librespot-java");
DEFAULT_GET_INFO_FIELDS.addProperty("voiceSupport", "NO");
DEFAULT_GET_INFO_FIELDS.addProperty("availability", "");
DEFAULT_GET_INFO_FIELDS.addProperty("productID", 0);
DEFAULT_GET_INFO_FIELDS.addProperty("tokenType", "default");
DEFAULT_GET_INFO_FIELDS.addProperty("groupStatus", "NONE");
DEFAULT_GET_INFO_FIELDS.addProperty("resolverVersion", "0");
DEFAULT_GET_INFO_FIELDS.addProperty("scope", "streaming,client-authorization-universal");
DEFAULT_SUCCESSFUL_ADD_USER.addProperty("status", 101);
DEFAULT_SUCCESSFUL_ADD_USER.addProperty("spotifyError", 0);
DEFAULT_SUCCESSFUL_ADD_USER.addProperty("statusString", "OK");
Utils.removeCryptographyRestrictions();
}
private final HttpRunner runner;
private final DiffieHellman keys;
private final List<SessionListener> sessionListeners;
private final Zeroconf zeroconf;
private final Object connectionLock = new Object();
private final Inner inner;
private volatile Session session;
private String connectingUsername = null;
private ZeroconfServer(@NotNull Inner inner, int listenPort, boolean listenAllInterfaces, String[] interfacesList) throws IOException {
this.inner = inner;
this.keys = new DiffieHellman(inner.random);
this.sessionListeners = new ArrayList<>();
if (listenPort == -1)
listenPort = inner.random.nextInt((MAX_PORT - MIN_PORT) + 1) + MIN_PORT;
new Thread(this.runner = new HttpRunner(listenPort), "zeroconf-http-server").start();
List<NetworkInterface> nics;
if (listenAllInterfaces) {
nics = getAllInterfaces();
} else {
if (interfacesList == null || interfacesList.length == 0) {
nics = getAllInterfaces();
} else {
nics = new ArrayList<>();
for (String str : interfacesList) {
NetworkInterface nif = NetworkInterface.getByName(str);
if (nif == null) {
LOGGER.warn("Interface {} doesn't exists.", str);
continue;
}
checkInterface(nics, nif);
}
}
}
zeroconf = new Zeroconf();
zeroconf.setLocalHostName(getUsefulHostname());
zeroconf.setUseIpv4(true).setUseIpv6(false);
zeroconf.addNetworkInterfaces(nics);
Map<String, String> txt = new HashMap<>();
txt.put("CPath", "/");
txt.put("VERSION", "1.0");
txt.put("Stack", "SP");
Service service = new Service(inner.deviceName, SERVICE, listenPort);
service.setText(txt);
zeroconf.announce(service);
}
@NotNull
public static String getUsefulHostname() throws UnknownHostException {
String host = InetAddress.getLocalHost().getHostName();
if (Objects.equals(host, "localhost")) {
host = Utils.toBase64(BigInteger.valueOf(ThreadLocalRandom.current().nextLong()).toByteArray()) + ".local";
LOGGER.warn("Hostname cannot be `localhost`, temporary hostname: " + host);
return host;
}
return host;
}
private static boolean isVirtual(@NotNull NetworkInterface nif) throws SocketException {
byte[] mac = nif.getHardwareAddress();
if (mac == null) return true;
outer:
for (byte[] virtual : VIRTUAL_INTERFACES) {
for (int i = 0; i < Math.min(virtual.length, mac.length); i++) {
if (virtual[i] != mac[i])
continue outer;
}
return true;
}
return false;
}
private static void checkInterface(List<NetworkInterface> list, @NotNull NetworkInterface nif) throws SocketException {
if (nif.isLoopback() || isVirtual(nif)) return;
list.add(nif);
}
@NotNull
private static List<NetworkInterface> getAllInterfaces() throws SocketException {
List<NetworkInterface> list = new ArrayList<>();
Enumeration<NetworkInterface> is = NetworkInterface.getNetworkInterfaces();
while (is.hasMoreElements()) checkInterface(list, is.nextElement());
return list;
}
@NotNull
private static Map<String, String> parsePath(@NotNull String path) {
HttpUrl url = HttpUrl.get("http://host" + path);
Map<String, String> map = new HashMap<>();
for (String name : url.queryParameterNames()) map.put(name, url.queryParameter(name));
return map;
}
@Override
public void close() throws IOException {
zeroconf.close();
runner.close();
}
public void closeSession() throws IOException {
if (session == null) return;
sessionListeners.forEach(l -> l.sessionClosing(session));
session.close();
session = null;
}
private boolean hasValidSession() {
try {
boolean valid = session != null && session.isValid();
if (!valid) session = null;
return valid;
} catch (IllegalStateException ex) {
session = null;
return false;
}
}
private void handleGetInfo(OutputStream out, String httpVersion) throws IOException {
JsonObject info = DEFAULT_GET_INFO_FIELDS.deepCopy();
info.addProperty("deviceID", inner.deviceId);
info.addProperty("remoteName", inner.deviceName);
info.addProperty("publicKey", Utils.toBase64(keys.publicKeyArray()));
info.addProperty("deviceType", inner.deviceType.name().toUpperCase());
synchronized (connectionLock) {
info.addProperty("activeUser", connectingUsername != null ? connectingUsername : (hasValidSession() ? session.username() : ""));
}
out.write(httpVersion.getBytes());
out.write(" 200 OK".getBytes());
out.write(EOL);
out.flush();
out.write("Content-Type: application/json".getBytes());
out.write(EOL);
out.flush();
out.write(EOL);
out.write(info.toString().getBytes());
out.flush();
}
private void handleAddUser(OutputStream out, Map<String, String> params, String httpVersion) throws GeneralSecurityException, IOException {
String username = params.get("userName");
if (username == null || username.isEmpty()) {
LOGGER.error("Missing userName!");
return;
}
String blobStr = params.get("blob");
if (blobStr == null || blobStr.isEmpty()) {
LOGGER.error("Missing blob!");
return;
}
String clientKeyStr = params.get("clientKey");
if (clientKeyStr == null || clientKeyStr.isEmpty()) {
LOGGER.error("Missing clientKey!");
return;
}
synchronized (connectionLock) {
if (username.equals(connectingUsername)) {
LOGGER.info("{} is already trying to connect.", username);
out.write(httpVersion.getBytes());
out.write(" 403 Forbidden".getBytes()); // I don't think this is the Spotify way
out.write(EOL);
out.write(EOL);
out.flush();
return;
}
}
byte[] sharedKey = Utils.toByteArray(keys.computeSharedKey(Utils.fromBase64(clientKeyStr)));
byte[] blobBytes = Utils.fromBase64(blobStr);
byte[] iv = Arrays.copyOfRange(blobBytes, 0, 16);
byte[] encrypted = Arrays.copyOfRange(blobBytes, 16, blobBytes.length - 20);
byte[] checksum = Arrays.copyOfRange(blobBytes, blobBytes.length - 20, blobBytes.length);
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
sha1.update(sharedKey);
byte[] baseKey = Arrays.copyOfRange(sha1.digest(), 0, 16);
Mac hmac = Mac.getInstance("HmacSHA1");
hmac.init(new SecretKeySpec(baseKey, "HmacSHA1"));
hmac.update("checksum".getBytes());
byte[] checksumKey = hmac.doFinal();
hmac.init(new SecretKeySpec(baseKey, "HmacSHA1"));
hmac.update("encryption".getBytes());
byte[] encryptionKey = hmac.doFinal();
hmac.init(new SecretKeySpec(checksumKey, "HmacSHA1"));
hmac.update(encrypted);
byte[] mac = hmac.doFinal();
if (!Arrays.equals(mac, checksum)) {
LOGGER.error("Mac and checksum don't match!");
out.write(httpVersion.getBytes());
out.write(" 400 Bad Request".getBytes()); // I don't think this is the Spotify way
out.write(EOL);
out.write(EOL);
out.flush();
return;
}
Cipher aes = Cipher.getInstance("AES/CTR/NoPadding");
aes.init(Cipher.DECRYPT_MODE, new SecretKeySpec(Arrays.copyOfRange(encryptionKey, 0, 16), "AES"), new IvParameterSpec(iv));
byte[] decrypted = aes.doFinal(encrypted);
try {
closeSession();
} catch (IOException ex) {
LOGGER.warn("Failed closing previous session.", ex);
}
try {
synchronized (connectionLock) {
connectingUsername = username;
}
LOGGER.info("Accepted new user from {}. {deviceId: {}}", params.get("deviceName"), inner.deviceId);
// Sending response
String resp = DEFAULT_SUCCESSFUL_ADD_USER.toString();
out.write(httpVersion.getBytes());
out.write(" 200 OK".getBytes());
out.write(EOL);
out.write("Content-Length: ".getBytes());
out.write(String.valueOf(resp.length()).getBytes());
out.write(EOL);
out.flush();
out.write(EOL);
out.write(resp.getBytes());
out.flush();
session = new Session.Builder(inner.conf)
.setDeviceId(inner.deviceId)
.setDeviceName(inner.deviceName)
.setDeviceType(inner.deviceType)
.setPreferredLocale(inner.preferredLocale)
.blob(username, decrypted)
.create();
synchronized (connectionLock) {
connectingUsername = null;
}
sessionListeners.forEach(l -> l.sessionChanged(session));
} catch (Session.SpotifyAuthenticationException | TokenProvider.TokenException | IOException | GeneralSecurityException ex) {
LOGGER.error("Couldn't establish a new session.", ex);
synchronized (connectionLock) {
connectingUsername = null;
}
out.write(httpVersion.getBytes());
out.write(" 500 Internal Server Error".getBytes()); // I don't think this is the Spotify way
out.write(EOL);
out.write(EOL);
out.flush();
}
}
public void addSessionListener(@NotNull SessionListener listener) {
sessionListeners.add(listener);
}
public void removeSessionListener(@NotNull SessionListener listener) {
sessionListeners.remove(listener);
}
public interface SessionListener {
/**
* The session instance is going to be closed after this call.
*
* @param session The old {@link Session}
*/
void sessionClosing(@NotNull Session session);
/**
* The session instance changed. {@link #sessionClosing(Session)} has been already called.
*
* @param session The new {@link Session}
*/
void sessionChanged(@NotNull Session session);
}
public static class Builder extends Session.AbsBuilder<Builder> {
private boolean listenAll = true;
private int listenPort = -1;
private String[] listenInterfaces = null;
public Builder(Session.@NotNull Configuration conf) {
super(conf);
}
public Builder() {
}
public Builder setListenAll(boolean listenAll) {
this.listenAll = listenAll;
this.listenInterfaces = null;
return this;
}
public Builder setListenPort(int listenPort) {
this.listenPort = listenPort;
return this;
}
public Builder setListenInterfaces(@NotNull String[] listenInterfaces) {
this.listenAll = false;
this.listenInterfaces = listenInterfaces;
return this;
}
@NonNls
public ZeroconfServer create() throws IOException {
return new ZeroconfServer(new Inner(deviceType, deviceName, deviceId, preferredLocale, conf), listenPort, listenAll, listenInterfaces);
}
}
private static class Inner {
final Random random = new SecureRandom();
final Connect.DeviceType deviceType;
final String deviceName;
final String deviceId;
final String preferredLocale;
final Session.Configuration conf;
Inner(@NotNull Connect.DeviceType deviceType, @NotNull String deviceName, @Nullable String deviceId, @NotNull String preferredLocale, @NotNull Session.Configuration conf) {
this.deviceType = deviceType;
this.deviceName = deviceName;
this.preferredLocale = preferredLocale;
this.conf = conf;
this.deviceId = (deviceId == null || deviceId.isEmpty()) ? Utils.randomHexString(random, 40).toLowerCase() : deviceId;
}
}
private class HttpRunner implements Runnable, Closeable {
private final ServerSocket serverSocket;
private final ExecutorService executorService = Executors.newCachedThreadPool(new NameThreadFactory((r) -> "zeroconf-client-" + r.hashCode()));
private volatile boolean shouldStop = false;
HttpRunner(int port) throws IOException {
serverSocket = new ServerSocket(port);
LOGGER.info("Zeroconf HTTP server started successfully on port {}!", port);
}
@Override
public void run() {
while (!shouldStop) {
try {
Socket socket = serverSocket.accept();
executorService.execute(() -> {
try {
handle(socket);
socket.close();
} catch (IOException ex) {
LOGGER.error("Failed handling request!", ex);
}
});
} catch (IOException ex) {
if (!shouldStop) LOGGER.error("Failed handling connection!", ex);
}
}
}
private void handleRequest(@NotNull OutputStream out, @NotNull String httpVersion, @NotNull String action, @Nullable Map<String, String> params) {
if (Objects.equals(action, "addUser")) {
if (params == null) throw new IllegalArgumentException();
try {
handleAddUser(out, params, httpVersion);
} catch (GeneralSecurityException | IOException ex) {
LOGGER.error("Failed handling addUser!", ex);
}
} else if (Objects.equals(action, "getInfo")) {
try {
handleGetInfo(out, httpVersion);
} catch (IOException ex) {
LOGGER.error("Failed handling getInfo!", ex);
}
} else {
LOGGER.warn("Unknown action: " + action);
}
}
private void handle(@NotNull Socket socket) throws IOException {
DataInputStream in = new DataInputStream(socket.getInputStream());
OutputStream out = socket.getOutputStream();
String[] requestLine = Utils.split(Utils.readLine(in), ' ');
if (requestLine.length != 3) {
LOGGER.warn("Unexpected request line: " + Arrays.toString(requestLine));
return;
}
String method = requestLine[0];
String path = requestLine[1];
String httpVersion = requestLine[2];
Map<String, String> headers = new HashMap<>();
String header;
while (!(header = Utils.readLine(in)).isEmpty()) {
String[] split = Utils.split(header, ':');
headers.put(split[0], split[1].trim());
}
if (!hasValidSession())
LOGGER.trace("Handling request: {} {} {}, headers: {}", method, path, httpVersion, headers);
Map<String, String> params;
if (Objects.equals(method, "POST")) {
String contentType = headers.get("Content-Type");
if (!Objects.equals(contentType, "application/x-www-form-urlencoded")) {
LOGGER.error("Bad Content-Type: " + contentType);
return;
}
String contentLengthStr = headers.get("Content-Length");
if (contentLengthStr == null) {
LOGGER.error("Missing Content-Length header!");
return;
}
int contentLength = Integer.parseInt(contentLengthStr);
byte[] body = new byte[contentLength];
in.readFully(body);
String bodyStr = new String(body);
String[] pairs = Utils.split(bodyStr, '&');
params = new HashMap<>(pairs.length);
for (String pair : pairs) {
String[] split = Utils.split(pair, '=');
params.put(URLDecoder.decode(split[0], "UTF-8"),
URLDecoder.decode(split[1], "UTF-8"));
}
} else {
params = parsePath(path);
}
String action = params.get("action");
if (action == null) {
LOGGER.debug("Request is missing action.");
return;
}
handleRequest(out, httpVersion, action, params);
}
@Override
public void close() throws IOException {
shouldStop = true;
serverSocket.close();
executorService.shutdown();
}
}
}