Module: Girl::Dns
- Included in:
- ProxyWorker, ProxydWorker
- Defined in:
- lib/girl/dns.rb
Instance Method Summary collapse
- #pack_a_query(domain, type = 1) ⇒ Object
- #pack_domain(domain) ⇒ Object
- #seek_dn(data, offset) ⇒ Object
- #seek_ip(data) ⇒ Object
- #seek_question(data) ⇒ Object
- #seek_question_dn(data) ⇒ Object
- #seek_rr_ip(data, offset) ⇒ Object
Instance Method Details
#pack_a_query(domain, type = 1) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/girl/dns.rb', line 4 def pack_a_query( domain, type = 1 ) # https://www.ietf.org/rfc/rfc1035.txt # https://www.ietf.org/rfc/rfc3596.txt raise "domain may not exceed 255 chars" if domain.bytesize > 255 raise "invalid domain" if domain =~ /[^\w\.\-]/ data = [ rand( 65_535 ), 1, 0, 1, 0, 0, 0 ].pack( 'nCCnnnn' ) data << [ pack_domain( domain ), type, 1 ].pack( 'a*nn' ) data end |
#pack_domain(domain) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/girl/dns.rb', line 14 def pack_domain( domain ) data = '' domain.split( '.' ).each do | label | raise "label may not exceed 63 chars" if label.bytesize > 63 data << [ label.bytesize, label ].pack( 'Ca*' ) end data << [ 0 ].pack( 'C' ) data end |
#seek_dn(data, offset) ⇒ Object
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 |
# File 'lib/girl/dns.rb', line 26 def seek_dn( data, offset ) name = "" datalen = data.bytesize loop do raise "offset is greater than datalen" if datalen < ( offset + 1 ) len = data.unpack( "@#{ offset } C" ).first if len == 0 offset += 1 break elsif ( len & 0xC0 ) == 0xC0 raise "data ended before offset expand" if datalen < ( offset + 2 ) ptr = data.unpack( "@#{ offset } n" ).first ptr &= 0x3FFF name2 = seek_dn( data, ptr ).first raise "data is malformed" if name2.nil? name += name2 offset += 2 break else offset += 1 raise "no expansion found" if datalen < ( offset + len ) elem = data[ offset..offset + len - 1 ] name += "#{ elem }." offset += len end end [ name, offset ] end |
#seek_ip(data) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/girl/dns.rb', line 62 def seek_ip( data ) ip = nil answer_count = data.unpack( "@6 n" ).first # puts "debug answer count #{ answer_count }" offset = seek_question( data ) # puts "debug offset #{ offset }" answer_count.times do ip, offset = seek_rr_ip( data, offset ) if ip then break end end ip end |
#seek_question(data) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/girl/dns.rb', line 80 def seek_question( data ) offset = 12 loop do len = data.unpack( "@#{ offset } C" ).first # puts "debug len #{ len } #{ data[ offset + 1, len ] }" break if len == 0 offset += ( 1 + len ) end offset += 5 offset end |
#seek_question_dn(data) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/girl/dns.rb', line 94 def seek_question_dn( data ) id = data[ 0, 2 ] parts = [] offset = 12 loop do len = data.unpack( "@#{ offset } C" ).first # puts "debug len #{ len } #{ data[ offset + 1, len ] }" break if len == 0 parts << data[ offset + 1, len ] offset += ( 1 + len ) end type = data.unpack( "@#{ offset + 1 } n" ).first # puts "debug id #{ id.inspect } dn #{ parts.join( '.' ).inspect } type #{ type }" [ id, parts.join( '.' ), type ] end |
#seek_rr_ip(data, offset) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/girl/dns.rb', line 112 def seek_rr_ip( data, offset ) ip = nil name, offset = seek_dn( data, offset ) # puts "debug seek_dn #{ name }, #{ offset }" type = data.unpack( "@#{ offset } n" ).first # puts "debug type #{ type }" offset += 8 rdlen = data.unpack( "@#{ offset } n" ).first # puts "debug rdlen #{ rdlen }" offset += 2 if type == 1 then raise "rdlen not 4?" if rdlen != 4 a, b, c, d = data.unpack( "@#{ offset } CCCC" ) ip = "#{ a }.#{ b }.#{ c }.#{ d }" elsif type == 28 then tokens = data.unpack("@#{ offset } n8") ip = format( "%x:%x:%x:%x:%x:%x:%x:%x", *tokens ) end offset += rdlen [ ip, offset ] end |