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 ( @@ -10,19 +10,19 @@ import (
"github.com/btcsuite/btcd/wire"
)
type CrawlError struct {
type crawlError struct {
errLoc string
Err error
}
// 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()
}
// crawlTwistee runs in a goroutine, crawls the remote ip and updates the master
// list of currently active addresses
func crawlTwistee(tw *Twistee) {
func crawlTwistee(tw *twistee) {
tw.crawlActive = true
tw.crawlStart = time.Now()
@ -118,14 +118,14 @@ func crawlTwistee(tw *Twistee) { @@ -118,14 +118,14 @@ func crawlTwistee(tw *Twistee) {
}
// 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
// FIXME - scan for long term crawl active twistees. Dial timeout is 10 seconds
// so should be done in under 5 minutes
}
// 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()
port := strconv.Itoa(int(tw.na.Port))
@ -137,7 +137,7 @@ func crawlIP(tw *Twistee) ([]*wire.NetAddress, *CrawlError) { @@ -137,7 +137,7 @@ func crawlIP(tw *Twistee) ([]*wire.NetAddress, *CrawlError) {
if config.debug {
log.Printf("debug - Could not connect to %s - %v\n", ip, err)
}
return nil, &CrawlError{"", err}
return nil, &crawlError{"", err}
}
defer conn.Close()
@ -146,26 +146,26 @@ func crawlIP(tw *Twistee) ([]*wire.NetAddress, *CrawlError) { @@ -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
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
// last parameter is lastblock
msgver, err := wire.NewMsgVersionFromConn(conn, NOUNCE, 0)
msgver, err := wire.NewMsgVersionFromConn(conn, nounce, 0)
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 {
// 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
msg, _, err := wire.ReadMessage(conn, PVER, TWISTNET)
msg, _, err := wire.ReadMessage(conn, pver, twistNet)
if err != nil {
// 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) {
@ -184,21 +184,21 @@ func crawlIP(tw *Twistee) ([]*wire.NetAddress, *CrawlError) { @@ -184,21 +184,21 @@ func crawlIP(tw *Twistee) ([]*wire.NetAddress, *CrawlError) {
tw.strVersion = msg.UserAgent
}
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
msgverack := wire.NewMsgVerAck()
err = wire.WriteMessage(conn, msgverack, PVER, TWISTNET)
err = wire.WriteMessage(conn, msgverack, pver, twistNet)
if err != nil {
return nil, &CrawlError{"writing message VerAck", err}
return nil, &crawlError{"writing message VerAck", err}
}
// second message received should be verack
msg, _, err = wire.ReadMessage(conn, PVER, TWISTNET)
msg, _, err = wire.ReadMessage(conn, pver, twistNet)
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) {
@ -207,15 +207,15 @@ func crawlIP(tw *Twistee) ([]*wire.NetAddress, *CrawlError) { @@ -207,15 +207,15 @@ func crawlIP(tw *Twistee) ([]*wire.NetAddress, *CrawlError) {
log.Printf("%s - received Version Ack\n", ip)
}
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
msgGetAddr := wire.NewMsgGetAddr()
err = wire.WriteMessage(conn, msgGetAddr, PVER, TWISTNET)
err = wire.WriteMessage(conn, msgGetAddr, pver, twistNet)
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
@ -225,7 +225,7 @@ func crawlIP(tw *Twistee) ([]*wire.NetAddress, *CrawlError) { @@ -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
// of the commands and will error. We can ignore these as we are only
// interested in the addr message and its content.
msgaddr, _, _ := wire.ReadMessage(conn, PVER, TWISTNET)
msgaddr, _, _ := wire.ReadMessage(conn, pver, twistNet)
if msgaddr != nil {
switch msg := msgaddr.(type) {
case *wire.MsgAddr:
@ -248,7 +248,7 @@ func crawlIP(tw *Twistee) ([]*wire.NetAddress, *CrawlError) { @@ -248,7 +248,7 @@ func crawlIP(tw *Twistee) ([]*wire.NetAddress, *CrawlError) {
}
// 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 } @@ -27,14 +27,14 @@ func getv6nonRR() []dns.RR { return latest.ipv6non }
// updateDNS updates the current slices of dns.RR so incoming requests get a
// fast answer
func updateDNS(s *Seeder) {
func updateDNS(s *dnsseeder) {
var rr4std, rr4non, rr6std, rr6non []dns.RR
s.mtx.RLock()
// 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
@ -48,8 +48,8 @@ func updateDNS(s *Seeder) { @@ -48,8 +48,8 @@ func updateDNS(s *Seeder) {
continue
}
if t == DNSV4STD || t == DNSV4NON {
if t == DNSV4STD && tw.dnsType == DNSV4STD {
if t == dnsV4Std || t == dnsV4Non {
if t == dnsV4Std && tw.dnsType == dnsV4Std {
r := new(dns.A)
r.Hdr = dns.RR_Header{Name: config.host + ".", Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60}
r.A = tw.na.IP
@ -58,7 +58,7 @@ func updateDNS(s *Seeder) { @@ -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 t == DNSV4NON && tw.dnsType == DNSV4NON {
if t == dnsV4Non && tw.dnsType == dnsV4Non {
r := new(dns.A)
r.Hdr = dns.RR_Header{Name: "nonstd." + config.host + ".", Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60}
r.A = tw.na.IP
@ -71,8 +71,8 @@ func updateDNS(s *Seeder) { @@ -71,8 +71,8 @@ func updateDNS(s *Seeder) {
numRR++
}
}
if t == DNSV6STD || t == DNSV6NON {
if t == DNSV6STD && tw.dnsType == DNSV6STD {
if t == dnsV6Std || t == dnsV6Non {
if t == dnsV6Std && tw.dnsType == dnsV6Std {
r := new(dns.AAAA)
r.Hdr = dns.RR_Header{Name: config.host + ".", Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: 60}
r.AAAA = tw.na.IP
@ -80,7 +80,7 @@ func updateDNS(s *Seeder) { @@ -80,7 +80,7 @@ func updateDNS(s *Seeder) {
numRR++
}
// 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.Hdr = dns.RR_Header{Name: "nonstd." + config.host + ".", Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: 60}
r.AAAA = tw.na.IP
@ -102,15 +102,15 @@ func updateDNS(s *Seeder) { @@ -102,15 +102,15 @@ func updateDNS(s *Seeder) {
latest.mtx.Lock()
for t := range []int{DNSV4STD, DNSV4NON, DNSV6STD, DNSV6NON} {
for t := range []int{dnsV4Std, dnsV4Non, dnsV6Std, dnsV6Non} {
switch t {
case DNSV4STD:
case dnsV4Std:
latest.ipv4std = rr4std
case DNSV4NON:
case dnsV4Non:
latest.ipv4non = rr4non
case DNSV6STD:
case dnsV6Std:
latest.ipv6std = rr6std
case DNSV6NON:
case dnsV6Non:
latest.ipv6non = rr6non
}
}

10
main.go

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

64
seeder.go

@ -12,26 +12,25 @@ import ( @@ -12,26 +12,25 @@ import (
const (
// Twister Magic number to make it incompatible with the Bitcoin network
TWISTNET = 0xd2bbdaf0
// nounce is used to check if we connect to ourselves
// TWISTNET Magic number to make it incompatible with the Bitcoin network
twistNet = 0xd2bbdaf0
// NOUNCE is used to check if we connect to ourselves
// as we don't listen we can use a fixed value
NOUNCE = 0x0539a019ca550825
PVER = 60000
MINPORT = 0
MAXPORT = 65535
nounce = 0x0539a019ca550825
pver = 60000
minPort = 0
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
DNSV4NON = 2
DNSV6STD = 3
DNSV6NON = 4
dnsV4Std = 1
dnsV4Non = 2
dnsV6Std = 3
dnsV6Non = 4
// twistee status
statusRG = 1 // reported good status. A remote twistee has reported this ip but we have not connected
@ -53,9 +52,10 @@ type twCounts struct { @@ -53,9 +52,10 @@ type twCounts struct {
Total int
mtx sync.RWMutex
}
type Seeder struct {
type dnsseeder struct {
uptime time.Time
theList map[string]*Twistee
theList map[string]*twistee
mtx sync.RWMutex
}
@ -91,7 +91,7 @@ func initCrawlers() { @@ -91,7 +91,7 @@ func initCrawlers() {
// startCrawlers is called on a time basis to start maxcrawlers new
// goroutines if there are spare goroutine slots available
func (s *Seeder) startCrawlers() {
func (s *dnsseeder) startCrawlers() {
tcount := len(s.theList)
if tcount == 0 {
@ -176,7 +176,7 @@ func (s *Seeder) startCrawlers() { @@ -176,7 +176,7 @@ func (s *Seeder) startCrawlers() {
}
// 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()
_, dup := s.theList[ipport]
s.mtx.RUnlock()
@ -184,17 +184,17 @@ func (s *Seeder) isDup(ipport string) bool { @@ -184,17 +184,17 @@ func (s *Seeder) isDup(ipport string) bool {
}
// 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))))
}
// 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 {
return false
}
if nNa.Port <= MINPORT || nNa.Port >= MAXPORT {
if nNa.Port <= minPort || nNa.Port >= maxPort {
return false
}
@ -204,31 +204,31 @@ func (s *Seeder) addNa(nNa *wire.NetAddress) bool { @@ -204,31 +204,31 @@ func (s *Seeder) addNa(nNa *wire.NetAddress) bool {
return false
}
nt := Twistee{
nt := twistee{
na: nNa,
lastConnect: time.Now(),
version: 0,
status: statusRG,
statusTime: time.Now(),
dnsType: DNSV4STD,
dnsType: dnsV4Std,
}
// select the dns type based on the remote address type and port
if x := nt.na.IP.To4(); x == nil {
// not ipv4
if nNa.Port != TWSTDPORT {
nt.dnsType = DNSV6NON
if nNa.Port != twStdPort {
nt.dnsType = dnsV6Non
// produce the nonstdIP
nt.nonstdIP = getNonStdIP(nt.na.IP, nt.na.Port)
} else {
nt.dnsType = DNSV6STD
nt.dnsType = dnsV6Std
}
} else {
// ipv4
if nNa.Port != TWSTDPORT {
nt.dnsType = DNSV4NON
if nNa.Port != twStdPort {
nt.dnsType = dnsV4Non
// force ipv4 address into a 4 byte buffer
nt.na.IP = nt.na.IP.To4()
@ -284,7 +284,7 @@ func crc16(bs []byte) uint16 { @@ -284,7 +284,7 @@ func crc16(bs []byte) uint16 {
return crc
}
func (s *Seeder) auditTwistees() {
func (s *dnsseeder) auditTwistees() {
c := 0
log.Printf("status - Audit start. System Uptime: %s\n", time.Since(s.uptime).String())
@ -318,7 +318,7 @@ func (s *Seeder) auditTwistees() { @@ -318,7 +318,7 @@ func (s *Seeder) auditTwistees() {
}
// 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 {
log.Printf("status - purging twistee %s after %v failed connections\n", k, tw.connectFails)
}
@ -338,7 +338,7 @@ func (s *Seeder) auditTwistees() { @@ -338,7 +338,7 @@ func (s *Seeder) auditTwistees() {
}
// teatload loads the dns records with time based test data
func (s *Seeder) loadDNS() {
func (s *dnsseeder) loadDNS() {
updateDNS(s)
}

14
twistee.go

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

Loading…
Cancel
Save