add support for connecting over tor, remove seedlist class (for now)
This commit is contained in:
parent
5cc98a4595
commit
b23f3c8088
|
@ -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));
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
47
SeedList.js
47
SeedList.js
|
@ -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);
|
|
@ -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');
|
||||
});
|
|
@ -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);
|
||||
});
|
Loading…
Reference in New Issue