From 51e873ee464a75f93cb1895225b7d25e1bb728e3 Mon Sep 17 00:00:00 2001
From: Robert Martin-Legene <robert@nic.ar>
Date: Tue, 11 Jun 2019 16:18:03 -0300
Subject: [PATCH] Trying to make faster initial connects using DNS

---
 bin/monitor.js    | 54 +++++++++++++++++++++++++++++++++++++++++++++++
 package-lock.json |  5 +++++
 package.json      |  3 ++-
 3 files changed, 61 insertions(+), 1 deletion(-)

diff --git a/bin/monitor.js b/bin/monitor.js
index b5cf795..58bdfbd 100755
--- a/bin/monitor.js
+++ b/bin/monitor.js
@@ -5,9 +5,11 @@
 "use strict"
 
 const   Libbfa                  =   require( process.env.BFAHOME + '/bin/libbfa.js');
+const   XMLHttpRequest          =   require("xmlhttprequest-ssl").XMLHttpRequest;
 var     bfa                     =   new Libbfa();
 var     web3                    =   bfa.newweb3();
 var     lastUnlock              =   0;
+var     netid                   =   0;
 
 function    readPeersCache()
 {
@@ -37,6 +39,53 @@ function    writePeersCache( peers )
     bfa.fs.writeFileSync( filename, txt, { mode: 0o644 } );
 }
 
+function    dnspeercachelookup()
+{
+    if ( netid == 0 )
+        return;
+    var     dnsquery            =   new XMLHttpRequest();
+    // onreadystatechange is called when TCP things happen to the socket.
+    // We set up the state before we send the query (such that they are
+    // registered in the object)
+    dnsquery.onreadystatechange = function() {
+        // readyStates: 0=UNSENT, 1=OPEN, 2=SENT, 3=LOADING, 4=DONE
+        if ( this.readyState == 4 )
+        {
+            if ( this.status == 200 ) {
+                var     json    =   JSON.parse(this.responseText);
+                if ( json.Status != 0 ) // 0 = NOERROR
+                    return;
+                var     i       =   Math.floor( Math.random() * json.Answer.length );
+                if ( json.Answer[i].type != 16 ) // 16 = TXT
+                    return;
+                var     enode   =   json.Answer[i].data;
+                // strip quotes
+                if ( enode.substring(0,1) == '"' && enode.substring(enode.length-1) == '"' )
+                    enode       =   enode.substring(1,enode.length-2);
+                console.log(
+                    "We have no peers, so will try to connect to "
+                    + enode
+                    + " found via DNS."
+                );
+                // Q: Can bad DNS data create a problem here?
+                // A: Geth checks data input validity.
+                web3.bfa.admin.addPeer( enode );
+            }
+        }
+        // Can we cleanup / commit suicide like this?
+        if ( this.readyState > 3 )
+            this                =   null;
+    };
+    // Robert runs a private little cheat registry on his private domain
+    // for fast update times. Can easily be moved to a hints.bfa.ar zone
+    // if desired needed.
+    // There are no new security aspects to consider, as this information
+    // is public already via bootnodes.
+    dnsquery.open( 'GET', 'https://cloudflare-dns.com/dns-query?name=hints.'+netid+'.bfa.martin-legene.dk&type=TXT' );
+    dnsquery.setRequestHeader( 'accept', 'application/dns-json' );
+    dnsquery.send();
+}
+
 function    writeStatus( peers )
 {
     // write network/status
@@ -134,6 +183,9 @@ function gotAdminPeers( nodelist )
             );
             web3.bfa.admin.addPeer( enode );
         }
+        else
+            if ( nowpeers.length == 0 )
+                dnspeercachelookup();
     }
 }
 
@@ -252,6 +304,8 @@ function    unlock()
 
 function    timer()
 {
+    if ( netid == 0 )
+        web3.eth.net.getId().then( x => {netid = x} );
     peerlist();
     mayseal();
     unlock();
diff --git a/package-lock.json b/package-lock.json
index d8557b7..d1f0721 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1508,6 +1508,11 @@
       "resolved": "https://registry.npmjs.org/xmlhttprequest/-/xmlhttprequest-1.8.0.tgz",
       "integrity": "sha1-Z/4HXFwk/vOfnWX197f+dRcZaPw="
     },
+    "xmlhttprequest-ssl": {
+      "version": "1.5.5",
+      "resolved": "https://registry.npmjs.org/xmlhttprequest-ssl/-/xmlhttprequest-ssl-1.5.5.tgz",
+      "integrity": "sha1-wodrBhaKrcQOV9l+gRkayPQ5iz4="
+    },
     "xtend": {
       "version": "4.0.1",
       "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz",
diff --git a/package.json b/package.json
index cf24738..e881964 100644
--- a/package.json
+++ b/package.json
@@ -6,7 +6,8 @@
     "big-integer": "^1.6.43",
     "request": "^2.88.0",
     "require": "^2.4.20",
-    "web3": "^1.0.0-beta.55"
+    "web3": "^1.0.0-beta.55",
+    "xmlhttprequest-ssl": "^1.5.5"
   },
   "repository": {
     "type": "git",
-- 
GitLab