Browse Source

golint code cleanup

master
Lyndsay Roger 9 years ago
parent
commit
6a18e1aaaf
  1. 46
      crawler.go
  2. 26
      dns.go
  3. 10
      main.go
  4. 64
      seeder.go
  5. 14
      twistee.go

46
crawler.go

@ -10,19 +10,19 @@ import (
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
) )
type CrawlError struct { type crawlError struct {
errLoc string errLoc string
Err error Err error
} }
// Error returns a formatted error about a crawl // Error returns a formatted error about a crawl
func (e *CrawlError) Error() string { func (e *crawlError) Error() string {
return "err: " + e.errLoc + ": " + e.Err.Error() return "err: " + e.errLoc + ": " + e.Err.Error()
} }
// crawlTwistee runs in a goroutine, crawls the remote ip and updates the master // crawlTwistee runs in a goroutine, crawls the remote ip and updates the master
// list of currently active addresses // list of currently active addresses
func crawlTwistee(tw *Twistee) { func crawlTwistee(tw *twistee) {
tw.crawlActive = true tw.crawlActive = true
tw.crawlStart = time.Now() tw.crawlStart = time.Now()
@ -118,14 +118,14 @@ func crawlTwistee(tw *Twistee) {
} }
// crawlEnd is a deffered func to update theList after a crawl is all done // crawlEnd is a deffered func to update theList after a crawl is all done
func crawlEnd(tw *Twistee) { func crawlEnd(tw *twistee) {
tw.crawlActive = false tw.crawlActive = false
// FIXME - scan for long term crawl active twistees. Dial timeout is 10 seconds // FIXME - scan for long term crawl active twistees. Dial timeout is 10 seconds
// so should be done in under 5 minutes // so should be done in under 5 minutes
} }
// crawlIP retrievs a slice of ip addresses from a client // crawlIP retrievs a slice of ip addresses from a client
func crawlIP(tw *Twistee) ([]*wire.NetAddress, *CrawlError) { func crawlIP(tw *twistee) ([]*wire.NetAddress, *crawlError) {
ip := tw.na.IP.String() ip := tw.na.IP.String()
port := strconv.Itoa(int(tw.na.Port)) port := strconv.Itoa(int(tw.na.Port))
@ -137,7 +137,7 @@ func crawlIP(tw *Twistee) ([]*wire.NetAddress, *CrawlError) {
if config.debug { if config.debug {
log.Printf("debug - Could not connect to %s - %v\n", ip, err) log.Printf("debug - Could not connect to %s - %v\n", ip, err)
} }
return nil, &CrawlError{"", err} return nil, &crawlError{"", err}
} }
defer conn.Close() defer conn.Close()
@ -146,26 +146,26 @@ func crawlIP(tw *Twistee) ([]*wire.NetAddress, *CrawlError) {
} }
// set a deadline for all comms to be done by. After this all i/o will error // set a deadline for all comms to be done by. After this all i/o will error
conn.SetDeadline(time.Now().Add(time.Second * MAXTO)) conn.SetDeadline(time.Now().Add(time.Second * maxTo))
// First command to remote end needs to be a version command // First command to remote end needs to be a version command
// last parameter is lastblock // last parameter is lastblock
msgver, err := wire.NewMsgVersionFromConn(conn, NOUNCE, 0) msgver, err := wire.NewMsgVersionFromConn(conn, nounce, 0)
if err != nil { if err != nil {
return nil, &CrawlError{"Create NewMsgVersionFromConn", err} return nil, &crawlError{"Create NewMsgVersionFromConn", err}
} }
err = wire.WriteMessage(conn, msgver, PVER, TWISTNET) err = wire.WriteMessage(conn, msgver, pver, twistNet)
if err != nil { if err != nil {
// Log and handle the error // Log and handle the error
return nil, &CrawlError{"Write Version Message", err} return nil, &crawlError{"Write Version Message", err}
} }
// first message received should be version // first message received should be version
msg, _, err := wire.ReadMessage(conn, PVER, TWISTNET) msg, _, err := wire.ReadMessage(conn, pver, twistNet)
if err != nil { if err != nil {
// Log and handle the error // Log and handle the error
return nil, &CrawlError{"Read message after sending Version", err} return nil, &crawlError{"Read message after sending Version", err}
} }
switch msg := msg.(type) { switch msg := msg.(type) {
@ -184,21 +184,21 @@ func crawlIP(tw *Twistee) ([]*wire.NetAddress, *CrawlError) {
tw.strVersion = msg.UserAgent tw.strVersion = msg.UserAgent
} }
default: default:
return nil, &CrawlError{"Did not receive expected Version message from remote client", errors.New("")} return nil, &crawlError{"Did not receive expected Version message from remote client", errors.New("")}
} }
// send verack command // send verack command
msgverack := wire.NewMsgVerAck() msgverack := wire.NewMsgVerAck()
err = wire.WriteMessage(conn, msgverack, PVER, TWISTNET) err = wire.WriteMessage(conn, msgverack, pver, twistNet)
if err != nil { if err != nil {
return nil, &CrawlError{"writing message VerAck", err} return nil, &crawlError{"writing message VerAck", err}
} }
// second message received should be verack // second message received should be verack
msg, _, err = wire.ReadMessage(conn, PVER, TWISTNET) msg, _, err = wire.ReadMessage(conn, pver, twistNet)
if err != nil { if err != nil {
return nil, &CrawlError{"reading expected Ver Ack from remote client", err} return nil, &crawlError{"reading expected Ver Ack from remote client", err}
} }
switch msg.(type) { switch msg.(type) {
@ -207,15 +207,15 @@ func crawlIP(tw *Twistee) ([]*wire.NetAddress, *CrawlError) {
log.Printf("%s - received Version Ack\n", ip) log.Printf("%s - received Version Ack\n", ip)
} }
default: default:
return nil, &CrawlError{"Did not receive expected Ver Ack message from remote client", errors.New("")} return nil, &crawlError{"Did not receive expected Ver Ack message from remote client", errors.New("")}
} }
// send getaddr command // send getaddr command
msgGetAddr := wire.NewMsgGetAddr() msgGetAddr := wire.NewMsgGetAddr()
err = wire.WriteMessage(conn, msgGetAddr, PVER, TWISTNET) err = wire.WriteMessage(conn, msgGetAddr, pver, twistNet)
if err != nil { if err != nil {
return nil, &CrawlError{"writing Addr message to remote client", err} return nil, &crawlError{"writing Addr message to remote client", err}
} }
c := 0 c := 0
@ -225,7 +225,7 @@ func crawlIP(tw *Twistee) ([]*wire.NetAddress, *CrawlError) {
// Using the Bitcoin lib for the Twister Net means it does not understand some // Using the Bitcoin lib for the Twister Net means it does not understand some
// of the commands and will error. We can ignore these as we are only // of the commands and will error. We can ignore these as we are only
// interested in the addr message and its content. // interested in the addr message and its content.
msgaddr, _, _ := wire.ReadMessage(conn, PVER, TWISTNET) msgaddr, _, _ := wire.ReadMessage(conn, pver, twistNet)
if msgaddr != nil { if msgaddr != nil {
switch msg := msgaddr.(type) { switch msg := msgaddr.(type) {
case *wire.MsgAddr: case *wire.MsgAddr:
@ -248,7 +248,7 @@ func crawlIP(tw *Twistee) ([]*wire.NetAddress, *CrawlError) {
} }
// received too many messages before requested Addr // received too many messages before requested Addr
return nil, &CrawlError{"message loop - did not receive remote addresses in first 25 messages from remote client", errors.New("")} return nil, &crawlError{"message loop - did not receive remote addresses in first 25 messages from remote client", errors.New("")}
} }
/* /*

26
dns.go

@ -27,14 +27,14 @@ func getv6nonRR() []dns.RR { return latest.ipv6non }
// updateDNS updates the current slices of dns.RR so incoming requests get a // updateDNS updates the current slices of dns.RR so incoming requests get a
// fast answer // fast answer
func updateDNS(s *Seeder) { func updateDNS(s *dnsseeder) {
var rr4std, rr4non, rr6std, rr6non []dns.RR var rr4std, rr4non, rr6std, rr6non []dns.RR
s.mtx.RLock() s.mtx.RLock()
// loop over each dns recprd type we need // loop over each dns recprd type we need
for t := range []int{DNSV4STD, DNSV4NON, DNSV6STD, DNSV6NON} { for t := range []int{dnsV4Std, dnsV4Non, dnsV6Std, dnsV6Non} {
numRR := 0 numRR := 0
@ -48,8 +48,8 @@ func updateDNS(s *Seeder) {
continue continue
} }
if t == DNSV4STD || t == DNSV4NON { if t == dnsV4Std || t == dnsV4Non {
if t == DNSV4STD && tw.dnsType == DNSV4STD { if t == dnsV4Std && tw.dnsType == dnsV4Std {
r := new(dns.A) r := new(dns.A)
r.Hdr = dns.RR_Header{Name: config.host + ".", Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60} r.Hdr = dns.RR_Header{Name: config.host + ".", Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60}
r.A = tw.na.IP r.A = tw.na.IP
@ -58,7 +58,7 @@ func updateDNS(s *Seeder) {
} }
// if the twistee is using a non standard port then add the encoded port info to DNS // if the twistee is using a non standard port then add the encoded port info to DNS
if t == DNSV4NON && tw.dnsType == DNSV4NON { if t == dnsV4Non && tw.dnsType == dnsV4Non {
r := new(dns.A) r := new(dns.A)
r.Hdr = dns.RR_Header{Name: "nonstd." + config.host + ".", Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60} r.Hdr = dns.RR_Header{Name: "nonstd." + config.host + ".", Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60}
r.A = tw.na.IP r.A = tw.na.IP
@ -71,8 +71,8 @@ func updateDNS(s *Seeder) {
numRR++ numRR++
} }
} }
if t == DNSV6STD || t == DNSV6NON { if t == dnsV6Std || t == dnsV6Non {
if t == DNSV6STD && tw.dnsType == DNSV6STD { if t == dnsV6Std && tw.dnsType == dnsV6Std {
r := new(dns.AAAA) r := new(dns.AAAA)
r.Hdr = dns.RR_Header{Name: config.host + ".", Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: 60} r.Hdr = dns.RR_Header{Name: config.host + ".", Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: 60}
r.AAAA = tw.na.IP r.AAAA = tw.na.IP
@ -80,7 +80,7 @@ func updateDNS(s *Seeder) {
numRR++ numRR++
} }
// if the twistee is using a non standard port then add the encoded port info to DNS // if the twistee is using a non standard port then add the encoded port info to DNS
if t == DNSV6NON && tw.dnsType == DNSV6NON { if t == dnsV6Non && tw.dnsType == dnsV6Non {
r := new(dns.AAAA) r := new(dns.AAAA)
r.Hdr = dns.RR_Header{Name: "nonstd." + config.host + ".", Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: 60} r.Hdr = dns.RR_Header{Name: "nonstd." + config.host + ".", Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: 60}
r.AAAA = tw.na.IP r.AAAA = tw.na.IP
@ -102,15 +102,15 @@ func updateDNS(s *Seeder) {
latest.mtx.Lock() latest.mtx.Lock()
for t := range []int{DNSV4STD, DNSV4NON, DNSV6STD, DNSV6NON} { for t := range []int{dnsV4Std, dnsV4Non, dnsV6Std, dnsV6Non} {
switch t { switch t {
case DNSV4STD: case dnsV4Std:
latest.ipv4std = rr4std latest.ipv4std = rr4std
case DNSV4NON: case dnsV4Non:
latest.ipv4non = rr4non latest.ipv4non = rr4non
case DNSV6STD: case dnsV6Std:
latest.ipv6std = rr6std latest.ipv6std = rr6std
case DNSV6NON: case dnsV6Non:
latest.ipv6non = rr6non latest.ipv6non = rr6non
} }
} }

10
main.go

@ -15,7 +15,7 @@ import (
"github.com/miekg/dns" "github.com/miekg/dns"
) )
type dnsseeder struct { type configData struct {
host string host string
port string port string
http string http string
@ -23,10 +23,10 @@ type dnsseeder struct {
verbose bool verbose bool
debug bool debug bool
stats bool stats bool
seeder *Seeder seeder *dnsseeder
} }
var config dnsseeder var config configData
var counts twCounts var counts twCounts
func main() { func main() {
@ -66,8 +66,8 @@ func main() {
} }
// init the seeder // init the seeder
config.seeder = &Seeder{} config.seeder = &dnsseeder{}
config.seeder.theList = make(map[string]*Twistee) config.seeder.theList = make(map[string]*twistee)
config.seeder.uptime = time.Now() config.seeder.uptime = time.Now()
// start dns server // start dns server

64
seeder.go

@ -12,26 +12,25 @@ import (
const ( const (
// Twister Magic number to make it incompatible with the Bitcoin network // TWISTNET Magic number to make it incompatible with the Bitcoin network
TWISTNET = 0xd2bbdaf0 twistNet = 0xd2bbdaf0
// nounce is used to check if we connect to ourselves // NOUNCE is used to check if we connect to ourselves
// as we don't listen we can use a fixed value // as we don't listen we can use a fixed value
NOUNCE = 0x0539a019ca550825 nounce = 0x0539a019ca550825
PVER = 60000 pver = 60000
MINPORT = 0 minPort = 0
MAXPORT = 65535 maxPort = 65535
TWSTDPORT = 28333 // standard port twister listens on twStdPort = 28333 // standard port twister listens on
MAXFAILS = 58 // max number of connect fails before we delete a twistee. Just over 24 hours(checked every 33 minutes) maxFails = 58 // max number of connect fails before we delete a twistee. Just over 24 hours(checked every 33 minutes)
MAXTO = 250 // max seconds (4min 10 sec) for all comms to twistee to complete before we timeout maxTo = 250 // max seconds (4min 10 sec) for all comms to twistee to complete before we timeout
// DNS Type. Is this twistee using v4/v6 and standard or non standard ports dnsV4Std = 1
DNSV4STD = 1 dnsV4Non = 2
DNSV4NON = 2 dnsV6Std = 3
DNSV6STD = 3 dnsV6Non = 4
DNSV6NON = 4
// twistee status // twistee status
statusRG = 1 // reported good status. A remote twistee has reported this ip but we have not connected statusRG = 1 // reported good status. A remote twistee has reported this ip but we have not connected
@ -53,9 +52,10 @@ type twCounts struct {
Total int Total int
mtx sync.RWMutex mtx sync.RWMutex
} }
type Seeder struct {
type dnsseeder struct {
uptime time.Time uptime time.Time
theList map[string]*Twistee theList map[string]*twistee
mtx sync.RWMutex mtx sync.RWMutex
} }
@ -91,7 +91,7 @@ func initCrawlers() {
// startCrawlers is called on a time basis to start maxcrawlers new // startCrawlers is called on a time basis to start maxcrawlers new
// goroutines if there are spare goroutine slots available // goroutines if there are spare goroutine slots available
func (s *Seeder) startCrawlers() { func (s *dnsseeder) startCrawlers() {
tcount := len(s.theList) tcount := len(s.theList)
if tcount == 0 { if tcount == 0 {
@ -176,7 +176,7 @@ func (s *Seeder) startCrawlers() {
} }
// isDup will return true or false depending if the ip exists in theList // isDup will return true or false depending if the ip exists in theList
func (s *Seeder) isDup(ipport string) bool { func (s *dnsseeder) isDup(ipport string) bool {
s.mtx.RLock() s.mtx.RLock()
_, dup := s.theList[ipport] _, dup := s.theList[ipport]
s.mtx.RUnlock() s.mtx.RUnlock()
@ -184,17 +184,17 @@ func (s *Seeder) isDup(ipport string) bool {
} }
// isNaDup returns true if this wire.NetAddress is already known to us // isNaDup returns true if this wire.NetAddress is already known to us
func (s *Seeder) isNaDup(na *wire.NetAddress) bool { func (s *dnsseeder) isNaDup(na *wire.NetAddress) bool {
return s.isDup(net.JoinHostPort(na.IP.String(), strconv.Itoa(int(na.Port)))) return s.isDup(net.JoinHostPort(na.IP.String(), strconv.Itoa(int(na.Port))))
} }
// addNa validates and adds a network address to theList // addNa validates and adds a network address to theList
func (s *Seeder) addNa(nNa *wire.NetAddress) bool { func (s *dnsseeder) addNa(nNa *wire.NetAddress) bool {
if dup := s.isNaDup(nNa); dup == true { if dup := s.isNaDup(nNa); dup == true {
return false return false
} }
if nNa.Port <= MINPORT || nNa.Port >= MAXPORT { if nNa.Port <= minPort || nNa.Port >= maxPort {
return false return false
} }
@ -204,31 +204,31 @@ func (s *Seeder) addNa(nNa *wire.NetAddress) bool {
return false return false
} }
nt := Twistee{ nt := twistee{
na: nNa, na: nNa,
lastConnect: time.Now(), lastConnect: time.Now(),
version: 0, version: 0,
status: statusRG, status: statusRG,
statusTime: time.Now(), statusTime: time.Now(),
dnsType: DNSV4STD, dnsType: dnsV4Std,
} }
// select the dns type based on the remote address type and port // select the dns type based on the remote address type and port
if x := nt.na.IP.To4(); x == nil { if x := nt.na.IP.To4(); x == nil {
// not ipv4 // not ipv4
if nNa.Port != TWSTDPORT { if nNa.Port != twStdPort {
nt.dnsType = DNSV6NON nt.dnsType = dnsV6Non
// produce the nonstdIP // produce the nonstdIP
nt.nonstdIP = getNonStdIP(nt.na.IP, nt.na.Port) nt.nonstdIP = getNonStdIP(nt.na.IP, nt.na.Port)
} else { } else {
nt.dnsType = DNSV6STD nt.dnsType = dnsV6Std
} }
} else { } else {
// ipv4 // ipv4
if nNa.Port != TWSTDPORT { if nNa.Port != twStdPort {
nt.dnsType = DNSV4NON nt.dnsType = dnsV4Non
// force ipv4 address into a 4 byte buffer // force ipv4 address into a 4 byte buffer
nt.na.IP = nt.na.IP.To4() nt.na.IP = nt.na.IP.To4()
@ -284,7 +284,7 @@ func crc16(bs []byte) uint16 {
return crc return crc
} }
func (s *Seeder) auditTwistees() { func (s *dnsseeder) auditTwistees() {
c := 0 c := 0
log.Printf("status - Audit start. System Uptime: %s\n", time.Since(s.uptime).String()) log.Printf("status - Audit start. System Uptime: %s\n", time.Since(s.uptime).String())
@ -318,7 +318,7 @@ func (s *Seeder) auditTwistees() {
} }
// last audit task is to remove twistees that we can not connect to // last audit task is to remove twistees that we can not connect to
if tw.status == statusNG && tw.connectFails > MAXFAILS { if tw.status == statusNG && tw.connectFails > maxFails {
if config.verbose { if config.verbose {
log.Printf("status - purging twistee %s after %v failed connections\n", k, tw.connectFails) log.Printf("status - purging twistee %s after %v failed connections\n", k, tw.connectFails)
} }
@ -338,7 +338,7 @@ func (s *Seeder) auditTwistees() {
} }
// teatload loads the dns records with time based test data // teatload loads the dns records with time based test data
func (s *Seeder) loadDNS() { func (s *dnsseeder) loadDNS() {
updateDNS(s) updateDNS(s)
} }

14
twistee.go

@ -8,7 +8,7 @@ import (
) )
// Twistee struct contains details on one twister client // Twistee struct contains details on one twister client
type Twistee struct { type twistee struct {
na *wire.NetAddress na *wire.NetAddress
lastConnect time.Time lastConnect time.Time
lastTry time.Time lastTry time.Time
@ -28,7 +28,7 @@ type Twistee struct {
} }
// status2str will return the string description of the status // status2str will return the string description of the status
func (tw Twistee) status2str() string { func (tw twistee) status2str() string {
switch tw.status { switch tw.status {
case statusRG: case statusRG:
return "statusRG" return "statusRG"
@ -44,15 +44,15 @@ func (tw Twistee) status2str() string {
} }
// dns2str will return the string description of the dns type // dns2str will return the string description of the dns type
func (tw Twistee) dns2str() string { func (tw twistee) dns2str() string {
switch tw.dnsType { switch tw.dnsType {
case DNSV4STD: case dnsV4Std:
return "v4 standard port" return "v4 standard port"
case DNSV4NON: case dnsV4Non:
return "v4 non-standard port" return "v4 non-standard port"
case DNSV6STD: case dnsV6Std:
return "v6 standard port" return "v6 standard port"
case DNSV6NON: case dnsV6Non:
return "v6 non-standard port" return "v6 non-standard port"
default: default:
return "Unknown DNS Type" return "Unknown DNS Type"

Loading…
Cancel
Save