forked from openairplay/airplay2-receiver
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathap2-receiver.py
756 lines (660 loc) · 27.7 KB
/
ap2-receiver.py
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
import os
import sys
import time
import struct
import socket
import argparse
import tempfile
import multiprocessing
import pprint
import http.server
import socketserver
import netifaces as ni
from hexdump import hexdump
from Crypto.Cipher import ChaCha20_Poly1305, AES
from zeroconf import IPVersion, ServiceInfo, Zeroconf
from biplist import readPlistFromString, writePlistToString
from ap2.connections.audio import RTPBuffer
from ap2.playfair import PlayFair
from ap2.utils import get_volume, set_volume
from ap2.pairing.hap import Hap, HAPSocket
from ap2.connections.event import Event
from ap2.connections.stream import Stream
# No Auth - coreutils, PairSetupMfi
# MFi Verify fail error after pair-setup[2/5]
FEATURES = 0x88340405f8a00
# No Auth - HK and coreutils
# Stops after pairing (setup [5/5] verify [2/2])with no supported auth error
FEATURES = 0xc340405f8a00
# No Auth = HK, coreutils, PairSetupMFi
# MFi Verify fail error after pair-setup[2/5]
FEATURES = 0x8c340405f8a00
# Mfi Auth - HK and coreutils
# All encrypt after pairing (setup [5/5] verify [2/2])
FEATURES = 0xc340445f8a00
# FairPlay - HK and coreutils
# Stops after pairing (setup [5/5] verify [2/2])with no supported auth error
FEATURES = 0xc340405fca00
# FairPlay - HK and coreutils and transient
# fp-setup after pair-setup[2/5]
FEATURES = 0x1c340405fca00
# MFi - HK and coreutils and transient
# auth-setup after pair-setup[2/5]
FEATURES = 0x1c340445f8a00
# No Auth - No enc - PairSetupMFi
# Works!!
FEATURES = 0x8030040780a00
# No Auth - No enc
# No supported authentication types.
# FEATURES = 0x30040780a00
# FEATURES = 0x8030040780a00 | (1 << 27)
FEATURES = 0x1c340405fca00
DEVICE_ID = None
IPV4 = None
IPV6 = None
SERVER_VERSION = "366.0"
HTTP_CT_BPLIST = "application/x-apple-binary-plist"
HTTP_CT_OCTET = "application/octet-stream"
HTTP_CT_PARAM = "text/parameters"
HTTP_CT_IMAGE = "image/jpeg"
HTTP_CT_DMAP = "application/x-dmap-tagged"
def setup_global_structs(args):
global sonos_one_info
global sonos_one_setup
global sonos_one_setup_data
global second_stage_info
global mdns_props
sonos_one_info = {
# 'OSInfo': 'Linux 3.10.53',
# 'PTPInfo': 'OpenAVNU ArtAndLogic-aPTP-changes a5d7f94-0.0.1',
'audioLatencies': [ { 'inputLatencyMicros': 0,
'outputLatencyMicros': 400000,
'type': 100},
{ 'audioType': 'default',
'inputLatencyMicros': 0,
'outputLatencyMicros': 400000,
'type': 100},
{ 'audioType': 'media',
'inputLatencyMicros': 0,
'outputLatencyMicros': 400000,
'type': 100},
{ 'audioType': 'media',
'inputLatencyMicros': 0,
'outputLatencyMicros': 400000,
'type': 102}],
# 'build': '16.0',
'deviceID': DEVICE_ID,
'features': FEATURES,
# 'features': 496155769145856, # Sonos One
# 'firmwareBuildDate': 'Nov 5 2019',
# 'firmwareRevision': '53.3-71050',
# 'hardwareRevision': '1.21.1.8-2',
'keepAliveLowPower': True,
'keepAliveSendStatsAsBody': True,
'manufacturer': 'Sonos',
'model': 'One',
'name': 'Camera da letto',
'nameIsFactoryDefault': False,
'pi': 'ba5cb8df-7f14-4249-901a-5e748ce57a93', # UUID generated casually..
'protocolVersion': '1.1',
'sdk': 'AirPlay;2.0.2',
'sourceVersion': '366.0',
'statusFlags': 4,
# 'statusFlags': 0x404 # Sonos One
}
if DISABLE_VM:
volume = 0
else:
volume = get_volume()
second_stage_info = {
"initialVolume": volume,
}
sonos_one_setup = {
'eventPort': 0, # AP2 receiver event server
'timingPort': 0,
'timingPeerInfo': {
'Addresses': [
IPV4, IPV6],
'ID': IPV4}
}
sonos_one_setup_data = {
'streams': [
{
'type': 96,
'dataPort': 0, # AP2 receiver data server
'controlPort': 0 # AP2 receiver control server
}
]
}
mdns_props = {
"srcvers": SERVER_VERSION,
"deviceid": DEVICE_ID,
"features": "%s,%s" % (hex(FEATURES & 0xffffffff), hex(FEATURES >> 32 & 0xffffffff)),
"flags": "0x4",
# "name": "GINO", # random
"model": "Airplay2-Receiver", # random
# "manufacturer": "Pino", # random
# "serialNumber": "01234xX321", # random
"protovers": "1.1",
"acl": "0",
"rsf": "0x0",
"fv": "p20.78000.12",
"pi": "5dccfd20-b166-49cc-a593-6abd5f724ddb", # UUID generated casually
"gid": "5dccfd20-b166-49cc-a593-6abd5f724ddb", # UUID generated casually
"gcgl": "0",
# "vn": "65537",
"pk": "de352b0df39042e201d31564049023af58a106c6d904b74a68aa65012852997f"
}
class AP2Handler(http.server.BaseHTTPRequestHandler):
pp = pprint.PrettyPrinter()
def parse_request(self):
self.raw_requestline = self.raw_requestline.replace(b"RTSP/1.0", b"HTTP/1.1")
r = http.server.BaseHTTPRequestHandler.parse_request(self)
self.protocol_version = "RTSP/1.0"
self.close_connection = 0
return r
def send_response(self, code, message=None):
if message is None:
if code in self.responses:
message = self.responses[code][0]
else:
message = b''
response = "%s %d %s\r\n" % (self.protocol_version, code, message)
self.wfile.write(response.encode())
def version_string(self):
return "AirTunes/%s" % SERVER_VERSION
def do_GET(self):
print(self.headers)
if self.path == "/info":
print("GET /info")
self.handle_info()
else:
print("GET %s Not implemented!" % self.path)
self.send_error(404)
def do_OPTIONS(self):
print(self.headers)
self.send_response(200)
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.send_header("Public", "ANNOUNCE, SETUP, RECORD, PAUSE, FLUSH, FLUSHBUFFERED, TEARDOWN, OPTIONS, POST, GET, PUT")
self.end_headers()
def do_FLUSHBUFFERED(self):
print("FLUSHBUFFERED")
self.send_response(200)
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.end_headers()
if self.headers["Content-Type"] == HTTP_CT_BPLIST:
content_len = int(self.headers["Content-Length"])
if content_len > 0:
body = self.rfile.read(content_len)
plist = readPlistFromString(body)
flush_from_seq = 0
if "flushFromSeq" in plist:
flush_from_seq = plist["flushFromSeq"]
if "flushUntilSeq" in plist:
flush_until_seq = plist["flushUntilSeq"]
self.server.streams[0].audio_connection.send("flush_from_until_seq-%i-%i" % (flush_from_seq, flush_until_seq))
self.pp.pprint(plist)
def do_POST(self):
if self.path == "/command":
print(self.headers)
print("POST /command")
self.handle_command()
elif self.path == "/feedback":
# debug logs disabled for feedback
self.handle_feedback()
elif self.path == "/audioMode":
print(self.headers)
print("POST /audioMode")
self.handle_audiomode()
elif self.path == "/auth-setup":
print(self.headers)
print("POST /auth-setup")
self.handle_auth_setup()
elif self.path == "/fp-setup":
print(self.headers)
print("POST /fp-setup")
self.handle_fp_setup()
elif self.path == "/fp-setup2":
print(self.headers)
print("POST /fp-setup2")
self.handle_auth_setup()
elif self.path == "/pair-setup":
print(self.headers)
print("POST /pair-setup")
self.handle_pair_setup()
elif self.path == "/pair-verify":
print(self.headers)
print("POST /pair-verify")
self.handle_pair_verify()
else:
print("POST %s Not implemented!" % self.path)
self.send_error(404)
def do_SETUP(self):
dacp_id = self.headers.get("DACP-ID")
active_remote = self.headers.get("Active-Remote")
ua = self.headers.get("User-Agent")
print("SETUP %s" % self.path)
print(self.headers)
if self.headers["Content-Type"] == HTTP_CT_BPLIST:
content_len = int(self.headers["Content-Length"])
if content_len > 0:
body = self.rfile.read(content_len)
plist = readPlistFromString(body)
self.pp.pprint(plist)
if "streams" not in plist:
print("Sending EVENT:")
event_port, self.event_proc = Event.spawn()
sonos_one_setup["eventPort"] = event_port
print("[+] eventPort=%d" % event_port)
self.pp.pprint(sonos_one_setup)
res = writePlistToString(sonos_one_setup)
self.send_response(200)
self.send_header("Content-Length", len(res))
self.send_header("Content-Type", HTTP_CT_BPLIST)
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.end_headers()
self.wfile.write(res)
else:
print("Sending CONTROL/DATA:")
stream = Stream(plist["streams"][0])
self.server.streams.append(stream)
sonos_one_setup_data["streams"][0]["controlPort"] = stream.control_port
sonos_one_setup_data["streams"][0]["dataPort"] = stream.data_port
print("[+] controlPort=%d dataPort=%d" % (stream.control_port, stream.data_port))
if stream.type == Stream.BUFFERED:
sonos_one_setup_data["streams"][0]["type"] = stream.type
sonos_one_setup_data["streams"][0]["audioBufferSize"] = 8388608
self.pp.pprint(sonos_one_setup_data)
res = writePlistToString(sonos_one_setup_data)
self.send_response(200)
self.send_header("Content-Length", len(res))
self.send_header("Content-Type", HTTP_CT_BPLIST)
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.end_headers()
self.wfile.write(res)
return
self.send_error(404)
def do_GET_PARAMETER(self):
print("GET_PARAMETER %s" % self.path)
print(self.headers)
params_res = {}
content_len = int(self.headers["Content-Length"])
if content_len > 0:
body = self.rfile.read(content_len)
params = body.splitlines()
for p in params:
if p == b"volume":
print("GET_PARAMETER: %s" % p)
if not DISABLE_VM:
params_res[p] = str(get_volume()).encode()
else:
print("Volume Management is disabled")
else:
print("Ops GET_PARAMETER: %s" % p)
if DISABLE_VM:
res = b"volume: 0" + b"\r\n"
else:
res = b"\r\n".join(b"%s: %s" % (k, v) for k, v in params_res.items()) + b"\r\n"
self.send_response(200)
self.send_header("Content-Length", len(res))
self.send_header("Content-Type", HTTP_CT_PARAM)
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.end_headers()
hexdump(res);
self.wfile.write(res)
def do_SET_PARAMETER(self):
print("SET_PARAMETER %s" % self.path)
print(self.headers)
params_res = {}
content_type = self.headers["Content-Type"]
content_len = int(self.headers["Content-Length"])
if content_type == HTTP_CT_PARAM:
if content_len > 0:
body = self.rfile.read(content_len)
params = body.splitlines()
for p in params:
pp = p.split(b":")
if pp[0] == b"volume":
print("SET_PARAMETER: %s => %s" % (pp[0], pp[1]))
if not DISABLE_VM:
set_volume(float(pp[1]))
else:
print("Volume Management is disabled")
elif pp[0] == b"progress":
print("SET_PARAMETER: %s => %s" % (pp[0], pp[1]))
else:
print("Ops SET_PARAMETER: %s" % p)
elif content_type == HTTP_CT_IMAGE:
if content_len > 0:
fname = None
with tempfile.NamedTemporaryFile(prefix="artwork", dir=".", delete=False) as f:
f.write(self.rfile.read(content_len))
fname = f.name
print("Artwork saved to %s" % fname)
elif content_type == HTTP_CT_DMAP:
if content_len > 0:
self.rfile.read(content_len)
print("Now plaing DAAP info. (need a daap parser here)")
self.send_response(200)
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.end_headers()
def do_RECORD(self):
print("RECORD %s" % self.path)
print(self.headers)
if self.headers["Content-Type"] == HTTP_CT_BPLIST:
content_len = int(self.headers["Content-Length"])
if content_len > 0:
body = self.rfile.read(content_len)
plist = readPlistFromString(body)
self.pp.pprint(plist)
self.send_response(200)
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.end_headers()
def do_SETRATEANCHORTIME(self):
print("SETRATEANCHORTIME %s" % self.path)
print(self.headers)
if self.headers["Content-Type"] == HTTP_CT_BPLIST:
content_len = int(self.headers["Content-Length"])
if content_len > 0:
body = self.rfile.read(content_len)
plist = readPlistFromString(body)
if plist["rate"] == 1:
self.server.streams[0].audio_connection.send("play-%i" % plist["rtpTime"])
if plist["rate"] == 0:
self.server.streams[0].audio_connection.send("pause")
self.pp.pprint(plist)
self.send_response(200)
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.end_headers()
def do_TEARDOWN(self):
print("TEARDOWN %s" % self.path)
print(self.headers)
if self.headers["Content-Type"] == HTTP_CT_BPLIST:
content_len = int(self.headers["Content-Length"])
if content_len > 0:
body = self.rfile.read(content_len)
plist = readPlistFromString(body)
if "streams" in plist:
stream_id = plist["streams"][0]["streamID"]
stream = self.server.streams[stream_id]
stream.teardown()
del self.server.streams[stream_id]
else:
for stream in self.server.streams:
stream.teardown()
self.server.streams.clear()
self.pp.pprint(plist)
self.send_response(200)
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.end_headers()
# Erase the hap() instance, otherwise reconnects fail
self.server.hap = None
# terminate the forked event_proc, otherwise a zombie process consumes 100% cpu
self.event_proc.terminate()
def do_SETPEERS(self):
print("SETPEERS %s" % self.path)
print(self.headers)
content_len = int(self.headers["Content-Length"])
if content_len > 0:
body = self.rfile.read(content_len)
plist = readPlistFromString(body)
self.pp.pprint(plist)
self.send_response(200)
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.end_headers()
def do_FLUSH(self):
print("FLUSH %s" % self.path)
print(self.headers)
self.send_response(200)
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.end_headers()
def handle_command(self):
if self.headers["Content-Type"] == HTTP_CT_BPLIST:
content_len = int(self.headers["Content-Length"])
if content_len > 0:
body = self.rfile.read(content_len)
plist = readPlistFromString(body)
newin = []
if "mrSupportedCommandsFromSender" in plist["params"]:
for p in plist["params"]["mrSupportedCommandsFromSender"]:
iplist = readPlistFromString(p)
newin.append(iplist)
plist["params"]["mrSupportedCommandsFromSender"] = newin
if "params" in plist["params"] and "kMRMediaRemoteNowPlayingInfoArtworkData" in plist["params"]["params"]:
plist["params"]["params"]["kMRMediaRemoteNowPlayingInfoArtworkData"] = "<redacted ..too long>"
self.pp.pprint(plist)
self.send_response(200)
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.end_headers()
def handle_feedback(self):
if self.headers["Content-Type"] == HTTP_CT_BPLIST:
content_len = int(self.headers["Content-Length"])
if content_len > 0:
body = self.rfile.read(content_len)
plist = readPlistFromString(body)
# feedback logs are pretty much noise...
#self.pp.pprint(plist)
self.send_response(200)
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.end_headers()
def handle_audiomode(self):
if self.headers["Content-Type"] == HTTP_CT_BPLIST:
content_len = int(self.headers["Content-Length"])
if content_len > 0:
body = self.rfile.read(content_len)
plist = readPlistFromString(body)
self.pp.pprint(plist)
self.send_response(200)
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.end_headers()
def handle_auth_setup(self):
content_len = int(self.headers["Content-Length"])
if content_len > 0:
body = self.rfile.read(content_len)
hexdump(body)
self.send_response(200)
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.end_headers()
def handle_fp_setup(self):
content_len = int(self.headers["Content-Length"])
if content_len > 0:
body = self.rfile.read(content_len)
pf = PlayFair()
pf_info = PlayFair.fairplay_s()
response = pf.fairplay_setup(pf_info, body)
hexdump(body)
self.send_response(200)
self.send_header("Content-Length", len(response))
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.end_headers()
self.wfile.write(response)
def handle_pair_setup(self):
content_len = int(self.headers["Content-Length"])
body = self.rfile.read(content_len)
hexdump(body)
if not self.server.hap:
self.server.hap = Hap()
res = self.server.hap.pair_setup(body)
self.send_response(200)
self.send_header("Content-Length", len(res))
self.send_header("Content-Type", HTTP_CT_BPLIST)
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.end_headers()
self.wfile.write(res)
if self.server.hap.encrypted:
hexdump(self.server.hap.accessory_shared_key)
self.upgrade_to_encrypted(self.server.hap.accessory_shared_key)
def handle_pair_verify(self):
content_len = int(self.headers["Content-Length"])
body = self.rfile.read(content_len)
if not self.server.hap:
self.server.hap = Hap()
res = self.server.hap.pair_verify(body)
self.send_response(200)
self.send_header("Content-Length", len(res))
self.send_header("Content-Type", HTTP_CT_OCTET)
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.end_headers()
self.wfile.write(res)
if self.server.hap.encrypted:
hexdump(self.server.hap.accessory_shared_key)
self.upgrade_to_encrypted(self.server.hap.accessory_shared_key)
def handle_info(self):
if "Content-Type" in self.headers:
if self.headers["Content-Type"] == HTTP_CT_BPLIST:
content_len = int(self.headers["Content-Length"])
if content_len > 0:
body = self.rfile.read(content_len)
plist = readPlistFromString(body)
self.pp.pprint(plist)
if "qualifier" in plist and "txtAirPlay" in plist["qualifier"]:
print("Sending:")
self.pp.pprint(sonos_one_info)
res = writePlistToString(sonos_one_info)
self.send_response(200)
self.send_header("Content-Length", len(res))
self.send_header("Content-Type", HTTP_CT_BPLIST)
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.end_headers()
self.wfile.write(res)
else:
print("No txtAirPlay")
self.send_error(404)
return
else:
print("No content")
self.send_error(404)
return
else:
print("Content-Type: %s | Not implemented" % self.headers["Content-Type"])
self.send_error(404)
else:
res = writePlistToString(sonos_one_info)
self.send_response(200)
self.send_header("Content-Length", len(res))
self.send_header("Content-Type", HTTP_CT_BPLIST)
self.send_header("Server", self.version_string())
self.send_header("CSeq", self.headers["CSeq"])
self.end_headers()
self.wfile.write(res)
def upgrade_to_encrypted(self, shared_key):
self.request = self.server.upgrade_to_encrypted(
self.client_address,
shared_key)
self.connection = self.request
self.rfile = self.connection.makefile('rb', self.rbufsize)
self.wfile = self.connection.makefile('wb')
self.is_encrypted = True
print("----- ENCRYPTED CHANNEL -----")
def register_mdns(receiver_name):
addresses = []
for ifen in ni.interfaces():
ifenaddr = ni.ifaddresses(ifen)
if ni.AF_INET in ifenaddr:
addresses.append(socket.inet_pton(ni.AF_INET,
ifenaddr[ni.AF_INET][0]["addr"]))
if ni.AF_INET6 in ifenaddr:
addresses.append(socket.inet_pton(ni.AF_INET6,
ifenaddr[ni.AF_INET6][0]["addr"].split("%")[0]))
info = ServiceInfo(
"_airplay._tcp.local.",
"%s._airplay._tcp.local." % receiver_name,
# addresses=[socket.inet_aton("127.0.0.1")],
addresses=addresses,
port=7000,
properties=mdns_props,
server="%s.local." % receiver_name,
)
zeroconf = Zeroconf(ip_version=IPVersion.V4Only)
zeroconf.register_service(info)
print("mDNS service registered")
return (zeroconf, info)
def unregister_mdns(zeroconf, info):
print("Unregistering...")
zeroconf.unregister_service(info)
zeroconf.close()
def get_free_port():
free_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
free_socket.bind(('0.0.0.0', 0))
free_socket.listen(5)
port = free_socket.getsockname()[1]
free_socket.close()
return port
class AP2Server(socketserver.TCPServer):
def __init__(self, addr_port, handler):
super().__init__(addr_port, handler)
self.connections = {}
self.hap = None
self.enc_layer = False
self.streams = []
#Override
def get_request(self):
client_socket, client_addr = super().get_request()
print("Got connection with %s:%d" % client_addr)
self.connections[client_addr] = client_socket
return (client_socket, client_addr)
def upgrade_to_encrypted(self, client_address, shared_key):
client_socket = self.connections[client_address]
hap_socket = HAPSocket(client_socket, shared_key)
self.connections[client_address] = hap_socket
return hap_socket
if __name__ == "__main__":
multiprocessing.set_start_method("spawn")
parser = argparse.ArgumentParser(prog='AirPlay 2 receiver')
parser.add_argument("-m", "--mdns", required=True, help="mDNS name to announce")
parser.add_argument("-n", "--netiface", required=True, help="Network interface to bind to")
parser.add_argument("-nv", "--no-volume-management", required=False, help="Disable volume management", action='store_true')
parser.add_argument("-f", "--features", required=False, help="Features")
args = parser.parse_args()
try:
IFEN = args.netiface
ifen = ni.ifaddresses(IFEN)
DISABLE_VM = args.no_volume_management
if args.features:
try:
FEATURES = int(args.features, 16)
except Exception:
print("[!] Error with feature arg - hex format required")
exit(-1)
except Exception:
print("[!] Network interface not found")
exit(-1)
DEVICE_ID = ifen[ni.AF_LINK][0]["addr"]
IPV4 = ifen[ni.AF_INET][0]["addr"]
IPV6 = ifen[ni.AF_INET6][0]["addr"].split("%")[0]
setup_global_structs(args)
print("Interface: %s" % IFEN)
print("IPv4: %s" % IPV4)
print("IPv6: %s" % IPV6)
print()
mdns = register_mdns(args.mdns)
print("Starting RSTP server, press Ctrl-C to exit...")
try:
PORT = 7000
with AP2Server(("0.0.0.0", PORT), AP2Handler) as httpd:
print("serving at port", PORT)
httpd.serve_forever()
except KeyboardInterrupt:
pass
finally:
print("Shutting down mDNS...")
unregister_mdns(*mdns)