From ebec07c050a0dee3587e336a89637b81980d88f0 Mon Sep 17 00:00:00 2001 From: Gordon Hall Date: Wed, 19 Mar 2014 16:11:47 -0400 Subject: [PATCH] added a seedlist class and example for connecting over tor --- SeedList.js | 47 ++++++++++++++++++++++++++++++++++++++++++++ config.js | 2 +- examples/TorProxy.js | 33 +++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 SeedList.js create mode 100644 examples/TorProxy.js diff --git a/SeedList.js b/SeedList.js new file mode 100644 index 0000000..b90ad88 --- /dev/null +++ b/SeedList.js @@ -0,0 +1,47 @@ +'use strict'; +var imports = require('soop').imports(); +var parent = imports.parent || require('events').EventEmitter; +var EventEmitter = require('events').EventEmitter; +var dns = require('dns'); +var inherits = require('util').inherits; + +function SeedList(options) { + this.options = options || {}; + this.sources = [ + 'dnsseed.bluematt.me', + 'dnsseed.bitcoin.dashjr.org', + 'seed.bitcoin.sipa.be', + 'seed.bitcoinstats.com', + 'bitseed.xf2.org' + ]; + this.source = this.options.source || this.sources[0]; + this.seeds = []; + this.find() +}; + +inherits(SeedList, EventEmitter); + +SeedList.prototype.find = function() { + var self = this; + dns.resolve(self.source, function(err, seeds) { + if (err) { + var index = self.sources.indexOf(self.source); + if (index !== -1) { + index++; + if (!self.sources[index]) { + return self.emit('seedsNotFound'); + } + else { + self.source = self.sources[index]; + } + self.find(); + } + return self.emit('error', err); + } + self.seeds = self.seeds.concat(seeds); + self.emit('seedsFound', seeds); + }); + return self; +}; + +module.exports = require('soop')(SeedList); diff --git a/config.js b/config.js index aea295c..5833536 100644 --- a/config.js +++ b/config.js @@ -1,4 +1,4 @@ module.exports = { network: 'livenet', - logger: 'normal' // none, normal, debug + logger: 'debug' // none, normal, debug }; diff --git a/examples/TorProxy.js b/examples/TorProxy.js new file mode 100644 index 0000000..2603da9 --- /dev/null +++ b/examples/TorProxy.js @@ -0,0 +1,33 @@ +var Socks5Client = require('socks5-client'); +var Peer = require('../Peer'); +var Connection = require('../Connection'); +var SeedList = require('../SeedList') + +// start looking for a seed +var seedlist = new SeedList(); +// create a client socket proxied through +// tor's socks5 proxy +var client = new Socks5Client('127.0.0.1', 9050); + +// when we have a list of seeds... +seedlist.on('seedsFound', function(seeds) { + // use the first seed in list + var peer = new Peer(seeds[0], 8333); + var connection = new Connection(client, peer); + // open the connection to the seed + client.connect(peer.port, peer.host); + // always handle errors + connection.on('error', function(err) { + console.log(err); + }); +}); + +// failboat +seedlist.on('seedsNotFound', function() { + console.log('failed to find seeds :('); +}); + +// double failboat +seedlist.on('error', function(err) { + console.log('error:', err); +});