From ec1cbdfe6097919e7567141519b3543322258de4 Mon Sep 17 00:00:00 2001 From: Lyndsay Roger Date: Thu, 10 Sep 2015 15:47:32 +1200 Subject: [PATCH] Add extra fields to json config to support remote crawlers --- network.go | 76 ++++++++++++++++++++++++++++++++++++++---------------- seeder.go | 46 +++++++++++++++++---------------- 2 files changed, 78 insertions(+), 44 deletions(-) diff --git a/network.go b/network.go index d480816..7e7cf7c 100644 --- a/network.go +++ b/network.go @@ -12,16 +12,19 @@ import ( // JNetwork is the exported struct that is read from the network file type JNetwork struct { - Name string - Desc string - Id string - Port uint16 - Pver uint32 - DNSName string - TTL uint32 - Seeder1 string - Seeder2 string - Seeder3 string + Name string + Desc string + SeederType string + Secret string + Remote string + Id string + Port uint16 + Pver uint32 + DNSName string + TTL uint32 + Seeder1 string + Seeder2 string + Seeder3 string } func createNetFile() { @@ -29,16 +32,19 @@ func createNetFile() { // create a struct to encode with json jnw := &JNetwork{ - Id: "0xabcdef01", - Port: 1234, - Pver: 70001, - TTL: 600, - DNSName: "seeder.example.com", - Name: "SeederNet", - Desc: "Description of SeederNet", - Seeder1: "seeder1.example.com", - Seeder2: "seed1.bob.com", - Seeder3: "seed2.example.com", + Id: "0xabcdef01", + Port: 1234, + Pver: 70001, + TTL: 600, + DNSName: "seeder.example.com", + Name: "SeederNet", + Desc: "Description of SeederNet", + SeederType: "Combined", + Secret: "32bYTesoFSECretThAtiSASecrET!!", + Remote: "ipofdnsserver.example.com:1234", + Seeder1: "seeder1.example.com", + Seeder2: "seed1.bob.com", + Seeder3: "seed2.example.com", } f, err := os.Create("dnsseeder.json") @@ -72,10 +78,10 @@ func loadNetwork(fName string) (*dnsseeder, error) { return nil, errors.New(fmt.Sprintf("Error decoding network file: %v", err)) } - return initNetwork(jnw, jnw.Name) + return initNetwork(jnw) } -func initNetwork(jnw JNetwork, name string) (*dnsseeder, error) { +func initNetwork(jnw JNetwork) (*dnsseeder, error) { if jnw.Port == 0 { return nil, errors.New(fmt.Sprintf("Invalid port supplied: %v", jnw.Port)) @@ -85,6 +91,10 @@ func initNetwork(jnw JNetwork, name string) (*dnsseeder, error) { return nil, errors.New(fmt.Sprintf("No DNS Hostname supplied")) } + if ok := checkBlockSize(jnw.Secret); ok != true { + return nil, errors.New(fmt.Sprintf("shared secret must be either 16, 24 or 32 bytes long. currently: %v", len(jnw.Secret))) + } + if _, ok := config.seeders[jnw.Name]; ok { return nil, errors.New(fmt.Sprintf("Name already exists from previous file - %s", jnw.Name)) } @@ -98,6 +108,9 @@ func initNetwork(jnw JNetwork, name string) (*dnsseeder, error) { seeder.name = jnw.Name seeder.desc = jnw.Desc seeder.dnsHost = jnw.DNSName + seeder.seederType = convertSeederType(jnw.SeederType) + seeder.secret = jnw.Secret + seeder.remote = jnw.Remote // conver the network magic number to a Uint32 t1, err := strconv.ParseUint(jnw.Id, 0, 32) @@ -139,6 +152,25 @@ func initNetwork(jnw JNetwork, name string) (*dnsseeder, error) { return seeder, nil } +func convertSeederType(seederType string) uint32 { + switch seederType { + case "Crawl": + return typeCrawl + case "DNS": + return typeDNS + default: + return typeCombined + } +} + +func checkBlockSize(secret string) bool { + s := len(secret) + if s == 16 || s == 24 || s == 32 { + return true + } + return false +} + /* */ diff --git a/seeder.go b/seeder.go index 3ae3dfe..ab96f62 100644 --- a/seeder.go +++ b/seeder.go @@ -45,29 +45,31 @@ const ( maxStatusTypes // used in main to allocate slice ) -/* NdCounts holds various statistics about the running system -type NdCounts struct { - NdStatus []uint32 - NdStarts []uint32 - DNSCounts []uint32 - mtx sync.RWMutex -} -*/ +const ( + // seederType + typeCombined = iota // This seeder both crawls and provides dns responses + typeCrawl // this seeder just crawls the network and sends ip info to a remote dns server + typeDNS // this seeder just provides dns data to clients and does no crawling itself +) + type dnsseeder struct { - id wire.BitcoinNet // Magic number - Unique ID for this network. Sent in header of all messages - theList map[string]*node // the list of current nodes - mtx sync.RWMutex // protect thelist - maxSize int // max number of clients before we start restricting new entries - port uint16 // default network port this seeder uses - pver uint32 // minimum block height for the seeder - ttl uint32 // DNS TTL to use for this seeder - dnsHost string // dns host we will serve results for this domain - name string // Short name for the network - desc string // Long description for the network - seeders []string // slice of seeders to pull ip addresses when starting this seeder - maxStart []uint32 // max number of goroutines to start each run for each status type - delay []int64 // number of seconds to wait before we connect to a known client for each status - counts NodeCounts // structure to hold stats for this seeder + id wire.BitcoinNet // Magic number - Unique ID for this network. Sent in header of all messages + theList map[string]*node // the list of current nodes + mtx sync.RWMutex // protect thelist + maxSize int // max number of clients before we start restricting new entries + port uint16 // default network port this seeder uses + pver uint32 // minimum block height for the seeder + ttl uint32 // DNS TTL to use for this seeder + dnsHost string // dns host we will serve results for this domain + name string // Short name for the network + desc string // Long description for the network + seederType uint32 // Combined, crawl or DNS only + secret string // Shared secret with remote seeder to encrypt messages + remote string // Remote host and port to send dns info if we are a crawler + seeders []string // slice of seeders to pull ip addresses when starting this seeder + maxStart []uint32 // max number of goroutines to start each run for each status type + delay []int64 // number of seconds to wait before we connect to a known client for each status + counts NodeCounts // structure to hold stats for this seeder } // initCrawlers needs to be run before the startCrawlers so it can get