mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-10-14 00:59:33 +00:00

* [NOD-1123] Bubble bad-message errors up to the protocol level. * [NOD-1123] Implement Banning. * [NOD-1123] Properly use &stopped. * [NOD-1123] Ban by IP rather than IP and port. * [NOD-1123] Don't initiate connections to banned peers. * [NOD-1123] Fix infinite loop in checkOutgoingConnections. * [NOD-1123] Fix bannedAddresses key. * [NOD-1123] Rename onBadMessageHandler to onInvalidMessageHandler.
50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package grpcserver
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/netadapter/server/grpcserver/protowire"
|
|
"github.com/kaspanet/kaspad/util/panics"
|
|
"github.com/pkg/errors"
|
|
"google.golang.org/grpc/peer"
|
|
"net"
|
|
)
|
|
|
|
type p2pServer struct {
|
|
protowire.UnimplementedP2PServer
|
|
server *gRPCServer
|
|
}
|
|
|
|
func newP2PServer(s *gRPCServer) *p2pServer {
|
|
return &p2pServer{server: s}
|
|
}
|
|
|
|
func (p *p2pServer) MessageStream(stream protowire.P2P_MessageStreamServer) error {
|
|
defer panics.HandlePanic(log, "p2pServer.MessageStream", nil)
|
|
|
|
peerInfo, ok := peer.FromContext(stream.Context())
|
|
if !ok {
|
|
return errors.Errorf("Error getting stream peer info from context")
|
|
}
|
|
tcpAddress, ok := peerInfo.Addr.(*net.TCPAddr)
|
|
if !ok {
|
|
return errors.Errorf("non-tcp connections are not supported")
|
|
}
|
|
|
|
if p.server.IsBanned(tcpAddress) {
|
|
log.Debugf("received connection attempt from banned peer %s", peerInfo.Addr)
|
|
return nil
|
|
}
|
|
|
|
connection := newConnection(p.server, tcpAddress, false, stream)
|
|
|
|
err := p.server.onConnectedHandler(connection)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Infof("Incoming connection from %s", peerInfo.Addr)
|
|
|
|
<-connection.stopChan
|
|
|
|
return nil
|
|
}
|