diff --git a/bin/monitor.js b/bin/monitor.js index b5cf79563918547530ac4fe2628861ca6ca52b0a..58bdfbd68badffc170c60fcfae7f583c26ec6e07 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 d8557b74ea86f6f78274468094327a25b35a5975..d1f07214a1e6e77ae0bbd9e73a126ad808b7fcec 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 cf247387cb53e24d067e9461570ef8c253a9486c..e8819647f4d34c41f2fd61540a8e7ed598229068 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",