Skip to content
Snippets Groups Projects
Commit 51e873ee authored by Robert Martin-Legene's avatar Robert Martin-Legene
Browse files

Trying to make faster initial connects using DNS

parent 6d1799d9
No related branches found
No related tags found
No related merge requests found
...@@ -5,9 +5,11 @@ ...@@ -5,9 +5,11 @@
"use strict" "use strict"
const Libbfa = require( process.env.BFAHOME + '/bin/libbfa.js'); const Libbfa = require( process.env.BFAHOME + '/bin/libbfa.js');
const XMLHttpRequest = require("xmlhttprequest-ssl").XMLHttpRequest;
var bfa = new Libbfa(); var bfa = new Libbfa();
var web3 = bfa.newweb3(); var web3 = bfa.newweb3();
var lastUnlock = 0; var lastUnlock = 0;
var netid = 0;
function readPeersCache() function readPeersCache()
{ {
...@@ -37,6 +39,53 @@ function writePeersCache( peers ) ...@@ -37,6 +39,53 @@ function writePeersCache( peers )
bfa.fs.writeFileSync( filename, txt, { mode: 0o644 } ); 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 ) function writeStatus( peers )
{ {
// write network/status // write network/status
...@@ -134,6 +183,9 @@ function gotAdminPeers( nodelist ) ...@@ -134,6 +183,9 @@ function gotAdminPeers( nodelist )
); );
web3.bfa.admin.addPeer( enode ); web3.bfa.admin.addPeer( enode );
} }
else
if ( nowpeers.length == 0 )
dnspeercachelookup();
} }
} }
...@@ -252,6 +304,8 @@ function unlock() ...@@ -252,6 +304,8 @@ function unlock()
function timer() function timer()
{ {
if ( netid == 0 )
web3.eth.net.getId().then( x => {netid = x} );
peerlist(); peerlist();
mayseal(); mayseal();
unlock(); unlock();
......
...@@ -1508,6 +1508,11 @@ ...@@ -1508,6 +1508,11 @@
"resolved": "https://registry.npmjs.org/xmlhttprequest/-/xmlhttprequest-1.8.0.tgz", "resolved": "https://registry.npmjs.org/xmlhttprequest/-/xmlhttprequest-1.8.0.tgz",
"integrity": "sha1-Z/4HXFwk/vOfnWX197f+dRcZaPw=" "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": { "xtend": {
"version": "4.0.1", "version": "4.0.1",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz",
......
...@@ -6,7 +6,8 @@ ...@@ -6,7 +6,8 @@
"big-integer": "^1.6.43", "big-integer": "^1.6.43",
"request": "^2.88.0", "request": "^2.88.0",
"require": "^2.4.20", "require": "^2.4.20",
"web3": "^1.0.0-beta.55" "web3": "^1.0.0-beta.55",
"xmlhttprequest-ssl": "^1.5.5"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment