Class: SiriProxy::Dns

Inherits:
Object
  • Object
show all
Defined in:
lib/siriproxy/dns.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDns

Returns a new instance of Dns.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/siriproxy/dns.rb', line 6

def initialize
  @interfaces = [
    [:tcp, "0.0.0.0", 53],
    [:udp, "0.0.0.0", 53]
  ]

  servers = []

  $APP_CONFIG.upstream_dns.each { |dns_addr|
    servers << [:udp, dns_addr, 53]
    servers << [:tcp, dns_addr, 53]
  }

  @upstream = RubyDNS::Resolver.new(servers)
end

Instance Attribute Details

#interfacesObject

Returns the value of attribute interfaces.



4
5
6
# File 'lib/siriproxy/dns.rb', line 4

def interfaces
  @interfaces
end

#threadObject

Returns the value of attribute thread.



4
5
6
# File 'lib/siriproxy/dns.rb', line 4

def thread
  @thread
end

#upstreamObject

Returns the value of attribute upstream.



4
5
6
# File 'lib/siriproxy/dns.rb', line 4

def upstream
  @upstream
end

Instance Method Details

#run(log_level = Logger::WARN, server_ip = $APP_CONFIG.server_ip) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/siriproxy/dns.rb', line 48

def run(log_level=Logger::WARN,server_ip=$APP_CONFIG.server_ip)
  if server_ip
    upstream = @upstream
      
    # Start the RubyDNS server
    RubyDNS::run_server(:listen => @interfaces) do
      @logger.level = log_level

      match(/guzzoni.apple.com/, Resolv::DNS::Resource::IN::A) do |transaction|
        transaction.respond!(server_ip)
      end

      # Default DNS handler
      otherwise do |transaction|
        transaction.passthrough!(upstream)
      end
    end

    puts "[Info - Server] DNS Server started, tainting 'guzzoni.apple.com' with #{server_ip}"
  end
end

#start(log_level = Logger::WARN) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/siriproxy/dns.rb', line 22

def start(log_level=Logger::WARN)
  @thread = Thread.new {
    begin
      self.run(log_level)
      $SP_DNS_STARTED = true
    rescue RuntimeError => e
      if e.message.match /^no acceptor/
        puts "[Error - Server] Either you're not root or tcp/udp port 53 is in use. DNS server is disabled"
        $SP_DNS_STARTED = true #Yeah, it didn't start, but we don't want to sit around and wait for it.
      else
        puts "[Error - Server] DNS Error: #{e.message}"
        puts "[Error - Server] DNS Server has crashed. Terminating SiriProxy"
        exit 1
      end
    rescue Exception => e
      puts "[Error - Server] DNS Error: #{e.message}"
      puts "[Error - Server] DNS Server has crashed. Terminating SiriProxy"
      exit 1
    end
  }
end

#stopObject



44
45
46
# File 'lib/siriproxy/dns.rb', line 44

def stop
  Thread.kill(@thread)
end