Class: DACPClient::PairingServer

Inherits:
GServer
  • Object
show all
Defined in:
lib/dacpclient/pairingserver.rb

Overview

The pairingserver handles pairing with iTunes

Constant Summary collapse

MDNS_TYPE =
'_touch-remote._tcp'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, guid, host = '0.0.0.0', port = 1024) ⇒ PairingServer

Returns a new instance of PairingServer.



14
15
16
17
18
19
20
21
22
23
# File 'lib/dacpclient/pairingserver.rb', line 14

def initialize(name, guid, host = '0.0.0.0', port = 1024)
  @name = name
  @port = port
  @host = host
  @pair = guid
  @pin = [0, 0, 0, 0]
  @peer = nil
  @device_type = 'iPod'
  super port, host
end

Instance Attribute Details

#device_typeObject

Returns the value of attribute device_type.



9
10
11
# File 'lib/dacpclient/pairingserver.rb', line 9

def device_type
  @device_type
end

#peerObject (readonly)

Returns the value of attribute peer.



10
11
12
# File 'lib/dacpclient/pairingserver.rb', line 10

def peer
  @peer
end

#pinObject

Returns the value of attribute pin.



9
10
11
# File 'lib/dacpclient/pairingserver.rb', line 9

def pin
  @pin
end

Class Method Details

.generate_pin_challenge(pair, pin) ⇒ Object



40
41
42
43
# File 'lib/dacpclient/pairingserver.rb', line 40

def self.generate_pin_challenge(pair, pin)
  pin_string = pin.map { |i| "#{i}\x00" }.join
  Digest::MD5.hexdigest(pair.upcase + pin_string).upcase
end

Instance Method Details

#serve(client) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dacpclient/pairingserver.rb', line 45

def serve(client)
  data = client.gets
  peer_addr = client.peeraddr[2]
  browser = DACPClient::Browser.new
  browser.browse
  @peer = browser.devices.find do |device|
    device.host == peer_addr
  end
  if data =~ /pairingcode=#{@expected}/i && @peer
    client.print "HTTP/1.1 200 OK\n" \
                 "Content-Length: #{@pairing_string.length}\n\n"
    client.print @pairing_string
    client.close
    stop
  else
    client.print "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"
    client.close
  end
end

#startObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dacpclient/pairingserver.rb', line 25

def start
  @pairing_string = generate_pairing_string(@pair, @name, @device_type)
  @expected = PairingServer.generate_pin_challenge(@pair, @pin)
  @service = DNSSD.register!(@name, MDNS_TYPE, 'local', @port, text_record)

  PairInfo.new(DMAPParser::Parser.parse(@pairing_string))
  super
  join

  @service.stop

  sleep 0.5 # sleep so iTunes accepts our login
  peer
end