node.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package dht
  2. import (
  3. "time"
  4. "github.com/anacrolix/dht/v2/int160"
  5. "github.com/anacrolix/dht/v2/krpc"
  6. )
  7. type nodeKey struct {
  8. Addr Addr
  9. Id int160.T
  10. }
  11. type node struct {
  12. nodeKey
  13. lastGotQuery time.Time // From the remote node
  14. lastGotResponse time.Time // From the remote node
  15. numReceivesFrom int
  16. failedLastQuestionablePing bool
  17. }
  18. func (s *Server) IsQuestionable(n *node) bool {
  19. return !s.IsGood(n) && !s.nodeIsBad(n)
  20. }
  21. func (n *node) hasAddrAndID(addr Addr, id int160.T) bool {
  22. return id == n.Id && n.Addr.String() == addr.String()
  23. }
  24. func (n *node) IsSecure() bool {
  25. return NodeIdSecure(n.Id.AsByteArray(), n.Addr.IP())
  26. }
  27. func (n *node) idString() string {
  28. return n.Id.ByteString()
  29. }
  30. func (n *node) NodeInfo() (ret krpc.NodeInfo) {
  31. ret.Addr = n.Addr.KRPC()
  32. if n := copy(ret.ID[:], n.idString()); n != 20 {
  33. panic(n)
  34. }
  35. return
  36. }
  37. // Per the spec in BEP 5.
  38. func (s *Server) IsGood(n *node) bool {
  39. if s.nodeIsBad(n) {
  40. return false
  41. }
  42. return time.Since(n.lastGotResponse) < 15*time.Minute ||
  43. !n.lastGotResponse.IsZero() && time.Since(n.lastGotQuery) < 15*time.Minute
  44. }