diff --git a/.gitignore b/.gitignore index b48644b64..791448c05 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ stcli.exe *.zip *.asc *.sublime* +discosrv +stpidx diff --git a/build.sh b/build.sh index 437cd9705..dd3704306 100755 --- a/build.sh +++ b/build.sh @@ -112,6 +112,10 @@ case "$1" in test || exit 1 assets + godep go build ./discover/cmd/discosrv + godep go build ./cmd/stpidx + godep go build ./cmd/stcli + for os in darwin-amd64 linux-386 linux-amd64 freebsd-amd64 windows-amd64 windows-386 ; do export GOOS=${os%-*} export GOARCH=${os#*-} diff --git a/discover/cmd/discosrv/main.go b/discover/cmd/discosrv/main.go index 208209938..490ed28ee 100644 --- a/discover/cmd/discosrv/main.go +++ b/discover/cmd/discosrv/main.go @@ -5,6 +5,7 @@ import ( "encoding/hex" "flag" "fmt" + "io" "log" "net" "os" @@ -16,18 +17,18 @@ import ( "github.com/juju/ratelimit" ) -type Node struct { - Addresses []Address - Updated time.Time +type node struct { + addresses []address + updated time.Time } -type Address struct { - IP []byte - Port uint16 +type address struct { + ip []byte + port uint16 } var ( - nodes = make(map[string]Node) + nodes = make(map[string]node) lock sync.Mutex queries = 0 announces = 0 @@ -134,7 +135,7 @@ func limit(addr *net.UDPAddr) bool { func handleAnnounceV2(addr *net.UDPAddr, buf []byte) { var pkt discover.AnnounceV2 err := pkt.UnmarshalXDR(buf) - if err != nil { + if err != nil && err != io.EOF { log.Println("AnnounceV2 Unmarshal:", err) log.Println(hex.Dump(buf)) return @@ -152,25 +153,25 @@ func handleAnnounceV2(addr *net.UDPAddr, buf []byte) { ip = addr.IP.To16() } - var addrs []Address - for _, addr := range pkt.Addresses { + var addrs []address + for _, addr := range pkt.This.Addresses { tip := addr.IP if len(tip) == 0 { tip = ip } - addrs = append(addrs, Address{ - IP: tip, - Port: addr.Port, + addrs = append(addrs, address{ + ip: tip, + port: addr.Port, }) } - node := Node{ - Addresses: addrs, - Updated: time.Now(), + node := node{ + addresses: addrs, + updated: time.Now(), } lock.Lock() - nodes[pkt.NodeID] = node + nodes[pkt.This.ID] = node lock.Unlock() } @@ -191,19 +192,21 @@ func handleQueryV2(conn *net.UDPConn, addr *net.UDPAddr, buf []byte) { queries++ lock.Unlock() - if ok && len(node.Addresses) > 0 { - pkt := discover.AnnounceV2{ - Magic: discover.AnnouncementMagicV2, - NodeID: pkt.NodeID, + if ok && len(node.addresses) > 0 { + ann := discover.AnnounceV2{ + Magic: discover.AnnouncementMagicV2, + This: discover.Node{ + ID: pkt.NodeID, + }, } - for _, addr := range node.Addresses { - pkt.Addresses = append(pkt.Addresses, discover.Address{IP: addr.IP, Port: addr.Port}) + for _, addr := range node.addresses { + ann.This.Addresses = append(ann.This.Addresses, discover.Address{IP: addr.ip, Port: addr.port}) } if debug { log.Printf("-> %v %#v", addr, pkt) } - tb := pkt.MarshalXDR() + tb := ann.MarshalXDR() _, _, err = conn.WriteMsgUDP(tb, nil, addr) if err != nil { log.Println("QueryV2 response write:", err) @@ -235,7 +238,7 @@ func logStats(file string, intv int) { var deleted = 0 for id, node := range nodes { - if time.Since(node.Updated) > 60*time.Minute { + if time.Since(node.updated) > 60*time.Minute { delete(nodes, id) deleted++ }