'use strict';

var dns = require('dns-socket');

class DnsServer {
    constructor(options, dnsStore) {
        this.options = options;
        this.dnsStore = dnsStore;
        this.socket = dns();
        
        this.socket.on('query', (query, port, host) => {
            if (query.questions.length === 1) {
                this.query(query.questions[0], (err, results) => {
                    if (err) {
                        return;
                    }

                    let reply = {
                        questions: query.questions,
                        answers: results
                    };
                    this.socket.response(query, reply, port, host);
                });
            }
        });
    }
    
    listen(port, cb) {
        this.socket.bind(port, cb);
    }
    
    close(cb) {
        this.socket.destroy(cb);
    }
    
    query(question, cb) {
        this.dnsStore.kvGet(question.name, 'utf8', (err, n, value) => {
            if (value === null) {
                this.socket.query({
                    questions: [question]
                }, 53, this.options.dnsRootIp, (err, res) => {
                    if (err) {
                        cb('not found');
                        return;
                    }
                    
                    cb(null, res.answers);
                });
                return;
            }
            
            let remote = value.split(':');
            let ip = remote[0];

            cb(null, [{
                type: question.type,
                name: question.name,
                ttl: 0,
                data: ip
            }]);
        });
    }
}

module.exports = DnsServer;
@@ -302,24 +302,64 @@
 th === 1) %7B%0A
+                let queryId = query.id;%0A
             
@@ -382,21 +382,8 @@
 uery
-.questions%5B0%5D
 , (e
@@ -486,16 +486,57 @@
      %7D%0A%0A
+                    query.id = queryId;%0A%0A
         
@@ -653,16 +653,140 @@
  results
+.answers,%0A                        authorities: results.authorities,%0A                        additionals: results.additionals
 %0A       
@@ -1067,23 +1067,16 @@
  query(q
-uestion
 , cb) %7B%0A
@@ -1103,24 +1103,26 @@
 e.kvGet(
+q.
 question
 .name, '
@@ -1109,24 +1109,28 @@
 t(q.question
+s%5B0%5D
 .name, 'utf8
@@ -1225,69 +1225,9 @@
 ery(
-%7B%0A                    questions: %5Bquestion%5D%0A                %7D
+q
 , 53
@@ -1447,24 +1447,16 @@
 ull, res
-.answers
 );%0A     
@@ -1617,16 +1617,43 @@
 cb(null,
+ %7B%0A                answers:
  %5B%7B%0A    
@@ -1668,22 +1668,28 @@
     
+    
 type: 
+q.
 question
 .typ
@@ -1688,15 +1688,23 @@
 tion
+s%5B0%5D
 .type,%0A
+    
     
@@ -1721,24 +1721,26 @@
   name: 
+q.
 question
 .name,%0A 
@@ -1735,15 +1735,23 @@
 tion
+s%5B0%5D
 .name,%0A
+    
     
@@ -1786,16 +1786,20 @@
         
+  
+  
 data: ip
@@ -1815,10 +1815,94 @@
     
+    %7D%5D,%0A                authorities: %5B%5D,%0A                additionals: %5B%5D%0A            
 %7D
-%5D
 );%0A 

add authorities and additionals