Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Romain Goyet
executable file 75 lines (65 sloc) 2.169 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
#!/usr/bin/env ruby
require 'rubygems'
require 'eventmachine'
require 'zlib'
require 'cfpropertylist'
require 'pp'

class String
  def remove_leading_hex(hex_string)
    length = hex_string.length/2
    return self[length..-1] if self[0...length].unpack('H*').first == hex_string
    self
  end
end

module SiriServer
  include EventMachine::Protocols::LineText2
  def ssl_handshake_completed
    puts "SSL proxy layer established !"
    @zstream = Zlib::Inflate.new
    @stream = ""
  end

  def post_init
    start_tls(:cert_chain_file => "./server.passless.crt",
              :private_key_file => "./server.passless.key",
              :verify_peer => false)
  end

  def receive_binary_data(data)
    #puts data.bytes.to_a.map{|i| i.to_s(16).rjust(2, '0')}.join(" ")
    data = data.remove_leading_hex('0d0a') # Remove heading newline
    data = data.remove_leading_hex('aaccee02') # Remove ACE header
    @stream << @zstream.inflate(data)
    parse
  end

  def unbind
    #@zstream.finish
    @zstream.close
  end

  def receive_line(line)
    puts line
    set_binary_mode if line.match(/X-Ace-Host/)
  end

  def parse

    if @stream[0...5].unpack('H*').first.match(/030000..../) # Ignore 030000xxxx commands
      puts "#####################################################"
      puts "* PING ? : #{@stream[0...5].unpack('H*').first.match(/030000(....)/)[1].to_i(16)}"
      @stream = @stream[5..-1]
    end

    chunk_size = @stream[0...5].unpack('H*').first.match(/0200(......)/)[1].to_i(16) rescue 1000000
    if (chunk_size < @stream.length+5)
      plist_data = @stream[5...5+chunk_size]
      plist = CFPropertyList::List.new(:data => plist_data)
      puts "#####################################################"
      plist_object = CFPropertyList.native_types(plist.value)
      pp plist_object
      (plist_object["properties"]["packets"] || []).each do |packet|
        puts packet.length
        File.open("data.spx", "a"){|f| f.write(packet)}
      end
      #self.send_data "Received an audio chunk !"
      @stream = @stream[chunk_size+5..-1]
    end
  end
end

EventMachine.run do
  EventMachine::start_server '0.0.0.0', 443, SiriServer
end
Something went wrong with that request. Please try again.