Gordon Hall
11 years ago
5 changed files with 51 additions and 84 deletions
@ -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