forked from DSTMK/shairport_plugin
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPlugin.pm
448 lines (356 loc) · 14.7 KB
/
Plugin.pm
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
use strict;
package Plugins::ShairTunes::Plugin;
use base qw(Slim::Plugin::OPMLBased);
use Digest::MD5 qw(md5 md5_hex);
use MIME::Base64;
use Slim::Utils::Log;
use Slim::Utils::Prefs;
use IO::Socket::INET6;
use Crypt::OpenSSL::RSA;
use Net::SDP;
use IPC::Open2;
use Plugins::ShairTunes::AIRPLAY;
# create log categogy before loading other modules
my $log = Slim::Utils::Log->addLogCategory({
'category' => 'plugin.shairtunes',
# 'defaultLevel' => 'ERROR',
'defaultLevel' => 'INFO',
'description' => getDisplayName(),
});
use Slim::Utils::Misc;
my $prefs = preferences('plugin.shairtunes');
my $airport_pem = join '', <DATA>;
my $rsa = Crypt::OpenSSL::RSA->new_private_key($airport_pem) || die "RSA private key import failed";
my $hairtunes_cli = "shairport_helper";
my $pipepath = "/tmp/pipe";
my %clients = ();
my %sockets = ();
my %players = ();
my %connections = ();
sub initPlugin
{
my $class = shift;
$log->info("Initialising " . $class->_pluginDataFor('version'));
# Subscribe to player connect/disconnect messages
Slim::Control::Request::subscribe(
\&playerSubscriptionChange,
[['client'],['new','reconnect','disconnect']]
);
# Slim::Control::Request::subscribe( \&pauseCallback, [['pause']] );
return 1;
}
sub playerSubscriptionChange {
my $request = shift;
my $client = $request->client;
my $reqstr = $request->getRequestString();
my $clientname = $client->name();
$log->debug("request=$reqstr client=$clientname");
if ( ($reqstr eq "client new") || ($reqstr eq "client reconnect") ) {
$sockets{$client} = createListenPort();
$players{$sockets{$client}} = $client;
if ($sockets{$client}) {
# Add us to the select loop so we get notified
Slim::Networking::Select::addRead($sockets{$client}, \&handleSocketConnect);
$clients{$client} = publishPlayer($clientname, "", $sockets{$client}->sockport());
} else {
$log->error("could not create ShairTunes socket for $clientname");
delete $sockets{$client}
}
} elsif ($reqstr eq "client disconnect") {
$log->debug("publisher for $clientname PID $clients{$client} will be terminated.");
system "kill $clients{$client}";
Slim::Networking::Select::removeRead($sockets{$client});
}
}
sub shutdownPlugin
{
# Slim::Control::Request::unsubscribe(\&pauseCallback);
return;
}
sub getDisplayName()
{
return('PLUGIN_SHAIRTUNES')
}
sub createListenPort()
{
my $port = 5123;
my $listen;
$listen = new IO::Socket::INET6(Listen => 1,
Domain => AF_INET6,
ReuseAddr => 1,
Proto => 'tcp' );
$listen ||= new IO::Socket::INET(Listen => 1,
ReuseAddr => 1,
Proto => 'tcp' );
return $listen
}
sub publishPlayer()
{
my ($apname, $password, $port) = @_;
my $pid = fork();
my $pw_clause = (length $password) ? "pw=true" : "pw=false";
my @hw_addr = +(map(ord, split(//, md5($apname))))[0..5];
if ($pid==0) {
{ exec 'avahi-publish-service',
join('', map { sprintf "%02X", $_ } @hw_addr) . "\@$apname",
"_raop._tcp",
$port,
"tp=UDP","sm=false","sv=false","ek=1","et=0,1","cn=0,1","ch=2","ss=16","sr=44100",$pw_clause,"vn=3","txtvers=1"; };
{ exec 'dns-sd', '-R',
join('', map { sprintf "%02X", $_ } @hw_addr) . "\@$apname",
"_raop._tcp",
".",
$port,
"tp=UDP","sm=false","sv=false","ek=1","et=0,1","cn=0,1","ch=2","ss=16","sr=44100",$pw_clause,"vn=3","txtvers=1"; };
{ exec 'mDNSPublish',
join('', map { sprintf "%02X", $_ } @hw_addr) . "\@$apname",
"_raop._tcp",
$port,
"tp=UDP","sm=false","sv=false","ek=1","et=0,1","cn=0,1","ch=2","ss=16","sr=44100",$pw_clause,"vn=3","txtvers=1"; };
die "could not run avahi-publish-service nor dns-sd nor mDNSPublish";
}
return $pid
}
sub ip6bin {
my $ip = shift;
$ip =~ /((.*)::)?(.+)/;
my @left = split /:/, $2;
my @right = split /:/, $3;
my @mid;
my $pad = 8 - ($#left + $#right + 2);
if ($pad > 0) {
@mid = (0) x $pad;
}
pack('S>*', map { hex } (@left, @mid, @right));
}
sub handleSocketConnect()
{
my $socket = shift;
my $player = $players{$socket};
my $new = $socket->accept;
$log->info("New connection from".$new->peerhost);
$new->blocking(0);
$connections{$new} = {socket => $socket, player => $player};
# Add us to the select loop so we get notified
Slim::Networking::Select::addRead($new, \&handleSocketRead);
}
sub handleSocketRead()
{
my $socket = shift;
if (eof($socket)) {
$log->debug("Closed: ".$socket);
Slim::Networking::Select::removeRead($socket);
close $socket;
delete $connections{$socket}
} else {
conn_handle_data($socket);
}
}
sub conn_handle_data {
my $socket = shift;
my $conn = $connections{$socket};
$log->debug("handle data 1");
if ($conn->{req_need}) {
if (length($conn->{data}) >= $conn->{req_need}) {
$conn->{req}->content(substr($conn->{data}, 0, $conn->{req_need}, ''));
conn_handle_request($socket, $conn);
}
undef $conn->{req_need};
return;
}
read $socket, my $data, 4096;
$conn->{data} .= $data;
if ($conn->{data} =~ /(\r\n\r\n|\n\n|\r\r)/) {
my $req_data = substr($conn->{data}, 0, $+[0], '');
$conn->{req} = HTTP::Request->parse($req_data);
$log->debug("REQ: ".$conn->{req}->method);
conn_handle_request($socket, $conn);
conn_handle_data($socket) if length($conn->{data});
}
}
sub digest_ok {
my ($req, $conn) = @_;
my $authz = $req->header('Authorization');
return 0 unless $authz =~ s/^Digest\s+//i;
return 0 unless length $conn->{nonce};
my @authz = split /,\s*/, $authz;
my %authz = map { /(.+)="(.+)"/; ($1, $2) } @authz;
# not a standard digest - uses capital hex digits, in conflict with the RFC
my $digest = uc md5_hex (
uc(md5_hex($authz{username} . ':' . $authz{realm} . ':' . $conn->{password}))
. ':' . $authz{nonce} . ':' .
uc(md5_hex($req->method . ':' . $authz{uri}))
);
return $digest eq $authz{response};
}
sub conn_handle_request {
my ($socket, $conn) = @_;
my $req = $conn->{req};
my $clen = $req->header('content-length') // 0;
if ($clen > 0 && !length($req->content)) {
$conn->{req_need} = $clen;
return; # need more!
}
my $resp = HTTP::Response->new(200);
$resp->request($req);
$resp->protocol($req->protocol);
$resp->header('CSeq', $req->header('CSeq'));
$resp->header('Audio-Jack-Status', 'connected; type=analog');
if (my $chall = $req->header('Apple-Challenge')) {
my $data = decode_base64($chall);
my $ip = $socket->sockhost;
if ($ip =~ /((\d+\.){3}\d+)$/) { # IPv4
$data .= join '', map { chr } split(/\./, $1);
} else {
$data .= ip6bin($ip);
}
my @hw_addr = +(map(ord, split(//, md5($conn->{player}->name()))))[0..5];
$data .= join '', map { chr } @hw_addr;
$data .= chr(0) x (0x20-length($data));
$rsa->use_pkcs1_padding; # this isn't hashed before signing
my $signature = encode_base64 $rsa->private_encrypt($data), '';
$signature =~ s/=*$//;
$resp->header('Apple-Response', $signature);
}
if (length $conn->{password}) {
if (!digest_ok($req, $conn)) {
my $nonce = md5_hex(map { rand } 1..20);
$conn->{nonce} = $nonce;
my $apname = $conn->{player}->name();
$resp->header('WWW-Authenticate', "Digest realm=\"$apname\", nonce=\"$nonce\"");
$resp->code(401);
$req->method('DENIED');
}
}
for ($req->method) {
/^OPTIONS$/ && do {
$resp->header('Public', 'ANNOUNCE, SETUP, RECORD, PAUSE, FLUSH, TEARDOWN, OPTIONS, GET_PARAMETER, SET_PARAMETER');
last;
};
/^ANNOUNCE$/ && do {
my $sdp = Net::SDP->new($req->content);
my $audio = $sdp->media_desc_of_type('audio');
print $audio->as_string();
print $audio->attribute('aesiv');
die("no AESIV") unless my $aesiv = decode_base64($audio->attribute('aesiv'));
die("no AESKEY") unless my $rsaaeskey = decode_base64($audio->attribute('rsaaeskey'));
$rsa->use_pkcs1_oaep_padding;
my $aeskey = $rsa->decrypt($rsaaeskey) || die "RSA decrypt failed";
$conn->{aesiv} = $aesiv;
$conn->{aeskey} = $aeskey;
$conn->{fmtp} = $audio->attribute('fmtp');
last;
};
/^SETUP$/ && do {
my $transport = $req->header('Transport');
$transport =~ s/;control_port=(\d+)//;
my $cport = $1;
$transport =~ s/;timing_port=(\d+)//;
my $tport = $1;
$transport =~ s/;server_port=(\d+)//;
my $dport = $1;
$resp->header('Session', 'DEADBEEF');
my %dec_args = (
iv => unpack('H*', $conn->{aesiv}),
key => unpack('H*', $conn->{aeskey}),
fmtp => $conn->{fmtp},
cport => $cport,
tport => $tport,
dport => $dport,
);
$dec_args{pipe} = $pipepath if defined $pipepath;
my $dec = '"' . $hairtunes_cli . '"' . join(' ', '', map { sprintf "%s '%s'", $_, $dec_args{$_} } keys(%dec_args));
$log->debug("decode command: $dec");
my $decoder = open2(my $dec_out, my $dec_in, $dec);
$conn->{decoder_pid} = $decoder;
$conn->{decoder_fh} = $dec_in;
my $portdesc = <$dec_out>;
die("Expected port number from decoder; got $portdesc") unless $portdesc =~ /^port: (\d+)/;
my $port = $1;
my $portdesc = <$dec_out>;
die("Expected cport number from decoder; got $portdesc") unless $portdesc =~ /^cport: (\d+)/;
my $cport = $1;
my $portdesc = <$dec_out>;
die("Expected hport number from decoder; got $portdesc") unless $portdesc =~ /^hport: (\d+)/;
my $hport = $1;
$log->info("launched decoder: $decoder on ports: $port/$cport/$hport");
$resp->header('Transport', $req->header('Transport') . ";server_port=$port");
my $host = Slim::Utils::Network::serverAddr();
my $url = "airplay://$host:$hport/stream.wav";
my $client = $conn->{player};
my @otherclients = grep { $_->name ne $client->name and $_->power }
$client->syncGroupActiveMembers();
foreach my $otherclient (@otherclients)
{
$log->info('turning off: ' . $otherclient->name);
$otherclient->display->showBriefly({line => ['AirPlay streaming to ' . $client->name . ':', 'Turning this player off']});
$otherclient->execute(['power', 0]);
}
$conn->{player}->execute( [ 'playlist', 'play', $url ] );
last;
};
/^RECORD$/ && last;
/^FLUSH$/ && do {
my $dfh = $conn->{decoder_fh};
print $dfh "flush\n";
last;
};
/^TEARDOWN$/ && do {
$resp->header('Connection', 'close');
close $conn->{decoder_fh};
$conn->{player}->execute( [ 'stop' ] );
last;
};
/^SET_PARAMETER$/ && do {
my @lines = split /[\r\n]+/, $req->content;
$log->debug("SET_PARAMETER req: " . $req->content);
my %content = map { /^(\S+): (.+)/; (lc $1, $2) } @lines;
my $cfh = $conn->{decoder_fh};
if (exists $content{volume}) {
my $volume = $content{volume};
my $percent = 100 + ($volume * 3.35);
$conn->{player}->execute( [ 'mixer', 'volume', $percent ] );
$log->debug("sending-> vol: ". $percent);
} else {
$log->error("unable to perform content for req: " . $req->content);
}
last;
};
/^GET_PARAMETER$/ && do {
my @lines = split /[\r\n]+/, $req->content;
$log->debug("GET_PARAMETER req: " . $req->content);
my %content = map { /^(\S+): (.+)/; (lc $1, $2) } @lines;
last;
};
/^DENIED$/ && last;
die("Unknown method: $_");
}
$log->debug($resp->as_string("\r\n"));
print $socket $resp->as_string("\r\n");
$socket->flush;
}
1;
__DATA__
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEA59dE8qLieItsH1WgjrcFRKj6eUWqi+bGLOX1HL3U3GhC/j0Qg90u3sG/1CUt
wC5vOYvfDmFI6oSFXi5ELabWJmT2dKHzBJKa3k9ok+8t9ucRqMd6DZHJ2YCCLlDRKSKv6kDqnw4U
wPdpOMXziC/AMj3Z/lUVX1G7WSHCAWKf1zNS1eLvqr+boEjXuBOitnZ/bDzPHrTOZz0Dew0uowxf
/+sG+NCK3eQJVxqcaJ/vEHKIVd2M+5qL71yJQ+87X6oV3eaYvt3zWZYD6z5vYTcrtij2VZ9Zmni/
UAaHqn9JdsBWLUEpVviYnhimNVvYFZeCXg/IdTQ+x4IRdiXNv5hEewIDAQABAoIBAQDl8Axy9XfW
BLmkzkEiqoSwF0PsmVrPzH9KsnwLGH+QZlvjWd8SWYGN7u1507HvhF5N3drJoVU3O14nDY4TFQAa
LlJ9VM35AApXaLyY1ERrN7u9ALKd2LUwYhM7Km539O4yUFYikE2nIPscEsA5ltpxOgUGCY7b7ez5
NtD6nL1ZKauw7aNXmVAvmJTcuPxWmoktF3gDJKK2wxZuNGcJE0uFQEG4Z3BrWP7yoNuSK3dii2jm
lpPHr0O/KnPQtzI3eguhe0TwUem/eYSdyzMyVx/YpwkzwtYL3sR5k0o9rKQLtvLzfAqdBxBurciz
aaA/L0HIgAmOit1GJA2saMxTVPNhAoGBAPfgv1oeZxgxmotiCcMXFEQEWflzhWYTsXrhUIuz5jFu
a39GLS99ZEErhLdrwj8rDDViRVJ5skOp9zFvlYAHs0xh92ji1E7V/ysnKBfsMrPkk5KSKPrnjndM
oPdevWnVkgJ5jxFuNgxkOLMuG9i53B4yMvDTCRiIPMQ++N2iLDaRAoGBAO9v//mU8eVkQaoANf0Z
oMjW8CN4xwWA2cSEIHkd9AfFkftuv8oyLDCG3ZAf0vrhrrtkrfa7ef+AUb69DNggq4mHQAYBp7L+
k5DKzJrKuO0r+R0YbY9pZD1+/g9dVt91d6LQNepUE/yY2PP5CNoFmjedpLHMOPFdVgqDzDFxU8hL
AoGBANDrr7xAJbqBjHVwIzQ4To9pb4BNeqDndk5Qe7fT3+/H1njGaC0/rXE0Qb7q5ySgnsCb3DvA
cJyRM9SJ7OKlGt0FMSdJD5KG0XPIpAVNwgpXXH5MDJg09KHeh0kXo+QA6viFBi21y340NonnEfdf
54PX4ZGS/Xac1UK+pLkBB+zRAoGAf0AY3H3qKS2lMEI4bzEFoHeK3G895pDaK3TFBVmD7fV0Zhov
17fegFPMwOII8MisYm9ZfT2Z0s5Ro3s5rkt+nvLAdfC/PYPKzTLalpGSwomSNYJcB9HNMlmhkGzc
1JnLYT4iyUyx6pcZBmCd8bD0iwY/FzcgNDaUmbX9+XDvRA0CgYEAkE7pIPlE71qvfJQgoA9em0gI
LAuE4Pu13aKiJnfft7hIjbK+5kyb3TysZvoyDnb3HOKvInK7vXbKuU4ISgxB2bB3HcYzQMGsz1qJ
2gG0N5hvJpzwwhbhXqFKA4zaaSrw622wDniAK5MlIE0tIAKKP4yxNGjoD2QYjhBGuhvkWKY=
-----END RSA PRIVATE KEY-----