status-go/vendor/github.com/waku-org/go-libp2p-rendezvous/svc.go

201 lines
5.3 KiB
Go
Raw Normal View History

2023-03-27 13:51:55 +00:00
package rendezvous
import (
"github.com/libp2p/go-libp2p/core/host"
inet "github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
2023-06-07 20:46:50 +00:00
"github.com/libp2p/go-msgio/pbio"
2023-03-27 13:51:55 +00:00
2023-06-07 20:46:50 +00:00
db "github.com/waku-org/go-libp2p-rendezvous/db"
pb "github.com/waku-org/go-libp2p-rendezvous/pb"
2023-03-27 13:51:55 +00:00
)
const (
MaxTTL = 72 * 3600 // 72hr
MaxNamespaceLength = 256
MaxPeerAddressLength = 2048
MaxRegistrations = 1000
MaxDiscoverLimit = 1000
)
type RendezvousService struct {
2023-06-07 20:46:50 +00:00
DB db.DB
2023-03-27 13:51:55 +00:00
}
2023-06-07 20:46:50 +00:00
func NewRendezvousService(host host.Host, db db.DB) *RendezvousService {
rz := &RendezvousService{DB: db}
2023-03-27 13:51:55 +00:00
host.SetStreamHandler(RendezvousProto, rz.handleStream)
return rz
}
func (rz *RendezvousService) handleStream(s inet.Stream) {
defer s.Reset()
pid := s.Conn().RemotePeer()
log.Debugf("New stream from %s", pid.Pretty())
2023-06-07 20:46:50 +00:00
r := pbio.NewDelimitedReader(s, inet.MessageSizeMax)
w := pbio.NewDelimitedWriter(s)
2023-03-27 13:51:55 +00:00
for {
var req pb.Message
var res pb.Message
err := r.ReadMsg(&req)
if err != nil {
return
}
t := req.GetType()
switch t {
case pb.Message_REGISTER:
r := rz.handleRegister(pid, req.GetRegister())
2023-06-07 20:46:50 +00:00
res.Type = pb.Message_REGISTER_RESPONSE.Enum()
2023-03-27 13:51:55 +00:00
res.RegisterResponse = r
err = w.WriteMsg(&res)
if err != nil {
log.Debugf("Error writing response: %s", err.Error())
return
}
case pb.Message_UNREGISTER:
err := rz.handleUnregister(pid, req.GetUnregister())
if err != nil {
log.Debugf("Error unregistering peer: %s", err.Error())
}
case pb.Message_DISCOVER:
r := rz.handleDiscover(pid, req.GetDiscover())
2023-06-07 20:46:50 +00:00
res.Type = pb.Message_DISCOVER_RESPONSE.Enum()
2023-03-27 13:51:55 +00:00
res.DiscoverResponse = r
err = w.WriteMsg(&res)
if err != nil {
log.Debugf("Error writing response: %s", err.Error())
return
}
default:
log.Debugf("Unexpected message: %s", t.String())
return
}
}
}
func (rz *RendezvousService) handleRegister(p peer.ID, m *pb.Message_Register) *pb.Message_RegisterResponse {
ns := m.GetNs()
if ns == "" {
return newRegisterResponseError(pb.Message_E_INVALID_NAMESPACE, "unspecified namespace")
}
if len(ns) > MaxNamespaceLength {
return newRegisterResponseError(pb.Message_E_INVALID_NAMESPACE, "namespace too long")
}
2023-06-07 20:46:50 +00:00
signedPeerRecord := m.GetSignedPeerRecord()
if signedPeerRecord == nil {
return newRegisterResponseError(pb.Message_E_INVALID_SIGNED_PEER_RECORD, "missing signed peer record")
2023-03-27 13:51:55 +00:00
}
2023-06-07 20:46:50 +00:00
peerRecord, err := pbToPeerRecord(signedPeerRecord)
if err != nil {
return newRegisterResponseError(pb.Message_E_INVALID_SIGNED_PEER_RECORD, "invalid peer record")
}
2023-03-27 13:51:55 +00:00
2023-06-07 20:46:50 +00:00
if peerRecord.ID != p {
return newRegisterResponseError(pb.Message_E_INVALID_SIGNED_PEER_RECORD, "peer id mismatch")
2023-03-27 13:51:55 +00:00
}
2023-06-07 20:46:50 +00:00
if len(peerRecord.Addrs) == 0 {
return newRegisterResponseError(pb.Message_E_INVALID_SIGNED_PEER_RECORD, "missing peer addresses")
2023-03-27 13:51:55 +00:00
}
mlen := 0
2023-06-07 20:46:50 +00:00
for _, maddr := range peerRecord.Addrs {
mlen += len(maddr.Bytes())
2023-03-27 13:51:55 +00:00
}
if mlen > MaxPeerAddressLength {
2023-06-07 20:46:50 +00:00
return newRegisterResponseError(pb.Message_E_INVALID_SIGNED_PEER_RECORD, "peer info too long")
2023-03-27 13:51:55 +00:00
}
// Note:
// We don't validate the addresses, because they could include protocols we don't understand
// Perhaps we should though.
mttl := m.GetTtl()
2023-06-07 20:46:50 +00:00
if mttl > MaxTTL {
2023-03-27 13:51:55 +00:00
return newRegisterResponseError(pb.Message_E_INVALID_TTL, "bad ttl")
}
ttl := DefaultTTL
if mttl > 0 {
ttl = int(mttl)
}
// now check how many registrations we have for this peer -- simple limit to defend
// against trivial DoS attacks (eg a peer connects and keeps registering until it
// fills our db)
rcount, err := rz.DB.CountRegistrations(p)
if err != nil {
log.Errorf("Error counting registrations: %s", err.Error())
return newRegisterResponseError(pb.Message_E_INTERNAL_ERROR, "database error")
}
if rcount > MaxRegistrations {
log.Warningf("Too many registrations for %s", p)
return newRegisterResponseError(pb.Message_E_NOT_AUTHORIZED, "too many registrations")
}
// ok, seems like we can register
2023-06-07 20:46:50 +00:00
_, err = rz.DB.Register(p, ns, signedPeerRecord, ttl)
2023-03-27 13:51:55 +00:00
if err != nil {
log.Errorf("Error registering: %s", err.Error())
return newRegisterResponseError(pb.Message_E_INTERNAL_ERROR, "database error")
}
log.Infof("registered peer %s %s (%d)", p, ns, ttl)
return newRegisterResponse(ttl)
}
func (rz *RendezvousService) handleUnregister(p peer.ID, m *pb.Message_Unregister) error {
ns := m.GetNs()
err := rz.DB.Unregister(p, ns)
if err != nil {
return err
}
log.Infof("unregistered peer %s %s", p, ns)
return nil
}
func (rz *RendezvousService) handleDiscover(p peer.ID, m *pb.Message_Discover) *pb.Message_DiscoverResponse {
ns := m.GetNs()
if len(ns) > MaxNamespaceLength {
return newDiscoverResponseError(pb.Message_E_INVALID_NAMESPACE, "namespace too long")
}
limit := MaxDiscoverLimit
mlimit := m.GetLimit()
2023-06-07 20:46:50 +00:00
if mlimit > 0 && mlimit < uint64(limit) {
2023-03-27 13:51:55 +00:00
limit = int(mlimit)
}
cookie := m.GetCookie()
if cookie != nil && !rz.DB.ValidCookie(ns, cookie) {
return newDiscoverResponseError(pb.Message_E_INVALID_COOKIE, "bad cookie")
}
regs, cookie, err := rz.DB.Discover(ns, cookie, limit)
if err != nil {
log.Errorf("Error in query: %s", err.Error())
return newDiscoverResponseError(pb.Message_E_INTERNAL_ERROR, "database error")
}
2023-06-07 20:46:50 +00:00
log.Debugf("discover query: %s %s -> %d", p, ns, len(regs))
2023-03-27 13:51:55 +00:00
return newDiscoverResponse(regs, cookie)
}