2019-09-19 20:56:34 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package contact
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/identity"
|
|
|
|
"storj.io/storj/pkg/pb"
|
2019-09-19 05:46:39 +01:00
|
|
|
"storj.io/storj/pkg/rpc/rpcstatus"
|
2019-09-19 20:56:34 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SatelliteIDVerifier checks if the connection is from a trusted satellite
|
|
|
|
type SatelliteIDVerifier interface {
|
|
|
|
VerifySatelliteID(ctx context.Context, id storj.NodeID) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// KademliaEndpoint implements the NodesServer Interface for backwards compatibility
|
|
|
|
type KademliaEndpoint struct {
|
|
|
|
log *zap.Logger
|
|
|
|
service *Service
|
|
|
|
trust SatelliteIDVerifier
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewKademliaEndpoint returns a new endpoint
|
|
|
|
func NewKademliaEndpoint(log *zap.Logger, service *Service, trust SatelliteIDVerifier) *KademliaEndpoint {
|
|
|
|
return &KademliaEndpoint{
|
|
|
|
log: log,
|
|
|
|
service: service,
|
|
|
|
trust: trust,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query is a node to node communication query
|
|
|
|
func (endpoint *KademliaEndpoint) Query(ctx context.Context, req *pb.QueryRequest) (_ *pb.QueryResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return &pb.QueryResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ping provides an easy way to verify a node is online and accepting requests
|
|
|
|
func (endpoint *KademliaEndpoint) Ping(ctx context.Context, req *pb.PingRequest) (_ *pb.PingResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return &pb.PingResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequestInfo returns the node info
|
|
|
|
func (endpoint *KademliaEndpoint) RequestInfo(ctx context.Context, req *pb.InfoRequest) (_ *pb.InfoResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
self := endpoint.service.Local()
|
|
|
|
|
|
|
|
if endpoint.trust == nil {
|
2019-09-19 05:46:39 +01:00
|
|
|
return nil, rpcstatus.Error(rpcstatus.Internal, "missing trust")
|
2019-09-19 20:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
peer, err := identity.PeerIdentityFromContext(ctx)
|
|
|
|
if err != nil {
|
2019-09-19 05:46:39 +01:00
|
|
|
return nil, rpcstatus.Error(rpcstatus.Unauthenticated, err.Error())
|
2019-09-19 20:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = endpoint.trust.VerifySatelliteID(ctx, peer.ID)
|
|
|
|
if err != nil {
|
2019-09-19 05:46:39 +01:00
|
|
|
return nil, rpcstatus.Errorf(rpcstatus.PermissionDenied, "untrusted peer %v", peer.ID)
|
2019-09-19 20:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.InfoResponse{
|
|
|
|
Type: self.Type,
|
|
|
|
Operator: &self.Operator,
|
|
|
|
Capacity: &self.Capacity,
|
|
|
|
Version: &self.Version,
|
|
|
|
}, nil
|
|
|
|
}
|