From b23f3c808811978753e2ce2617330179419e41fd Mon Sep 17 00:00:00 2001 From: Gordon Hall Date: Fri, 4 Apr 2014 11:46:57 -0400 Subject: [PATCH] add support for connecting over tor, remove seedlist class (for now) --- Connection.js | 19 ++++++++++++++-- PeerManager.js | 4 ++-- SeedList.js | 47 --------------------------------------- examples/ConnectionTor.js | 32 ++++++++++++++++++++++++++ examples/TorProxy.js | 33 --------------------------- 5 files changed, 51 insertions(+), 84 deletions(-) delete mode 100644 SeedList.js create mode 100644 examples/ConnectionTor.js delete mode 100644 examples/TorProxy.js diff --git a/Connection.js b/Connection.js index f273ec3..8c6c589 100644 --- a/Connection.js +++ b/Connection.js @@ -10,6 +10,7 @@ var PROTOCOL_VERSION = 70000; var Binary = imports.Binary || require('binary'); var Put = imports.Put || require('bufferput'); var Buffers = imports.Buffers || require('buffers'); +var Socks5Client = imports.Socks5Client || require('socks5-client') require('./Buffers.monkey').patch(Buffers); var Block = require('./Block'); @@ -22,11 +23,19 @@ var nonce = util.generateNonce(); var BIP0031_VERSION = 60000; -function Connection(socket, peer) { +function Connection(socket, peer, opts) { Connection.super(this, arguments); + + this.options = opts || {}; + this.socket = socket; this.peer = peer; + // check for socks5 proxy options and construct a proxied socket + if (this.options.proxy) { + this.socket = new Socks5Client(opts.proxy.host, opts.proxy.port); + } + // A connection is considered "active" once we have received verack this.active = false; // The version incoming packages are interpreted as @@ -36,7 +45,7 @@ function Connection(socket, peer) { // The (claimed) height of the remote peer's block chain this.bestHeight = 0; // Is this an inbound connection? - this.inbound = !!socket.server; + this.inbound = !!this.socket.server; // Have we sent a getaddr on this connection? this.getaddr = false; @@ -54,6 +63,12 @@ function Connection(socket, peer) { } Connection.parent = imports.parent || require('events').EventEmitter; +Connection.prototype.open = function(callback) { + if (typeof callback === 'function') this.once('connect', callback); + this.socket.connect(this.peer.port, this.peer.host); + return this; +}; + Connection.prototype.setupHandlers = function () { this.socket.addListener('connect', this.handleConnect.bind(this)); this.socket.addListener('error', this.handleError.bind(this)); diff --git a/PeerManager.js b/PeerManager.js index c6e2029..67b6d43 100644 --- a/PeerManager.js +++ b/PeerManager.js @@ -95,8 +95,8 @@ PeerManager.prototype.connectTo = function(peer) { } }; -PeerManager.prototype.addConnection = function(socketConn, peer) { - var conn = new Connection(socketConn, peer); +PeerManager.prototype.addConnection = function(socketConn, peer, opts) { + var conn = new Connection(socketConn, peer, opts); this.connections.push(conn); this.emit('connection', conn); diff --git a/SeedList.js b/SeedList.js deleted file mode 100644 index 05ea63a..0000000 --- a/SeedList.js +++ /dev/null @@ -1,47 +0,0 @@ -'use strict'; -var imports = require('soop').imports(); -var parent = imports.parent || require('events').EventEmitter; -var EventEmitter = require('events').EventEmitter; -var dns = require('dns'); - -function SeedList(options) { - SeedList.super(this, arguments); - 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() -}; - -SeedList.parent = imports.parent || 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/examples/ConnectionTor.js b/examples/ConnectionTor.js new file mode 100644 index 0000000..f03bf9e --- /dev/null +++ b/examples/ConnectionTor.js @@ -0,0 +1,32 @@ +var Peer = require('../Peer'); +var Connection = require('../Connection'); + +// create a peer instance from a know peer +// (later we can use built-in peer discovery) +// to get a peer to connect to you can run: +// +// ~# dig dnsseed.bluematt.me +// +// (or use a different dns seed) +var peer = new Peer('108.13.10.109', 8333); + +// create a connection without an existing socket +// but specify a socks5 proxy to create a socket +// that's bound to that proxy in it's place +var connection = new Connection(null, peer, { + proxy: { host: '127.0.0.1', port: 9050 } +}); + +// open the connection +connection.open(); + +// you can listen for the connect event +connection.on('connect', function(data) { + // we are connected! + console.log('connected'); +}); + +connection.on('error', function(err) { + // boo! :( + console.log('poop'); +}); diff --git a/examples/TorProxy.js b/examples/TorProxy.js deleted file mode 100644 index 2603da9..0000000 --- a/examples/TorProxy.js +++ /dev/null @@ -1,33 +0,0 @@ -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); -});