mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-11-27 15:53:57 +00:00
working sample implementation of tls or non-tls
This commit is contained in:
parent
bbde5e5397
commit
7e1b3705bb
@ -42,11 +42,13 @@ func NewNetAdapter(cfg *config.Config) (*NetAdapter, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
// p2pServer is a gRCP server for internodes communication
|
||||||
p2pServer, err := grpcserver.NewP2PServer(cfg.Listeners)
|
p2pServer, err := grpcserver.NewP2PServer(cfg.Listeners)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
rpcServer, err := grpcserver.NewRPCServer(cfg.RPCListeners, cfg.RPCMaxClients)
|
// rpcServer is for gRCP miner, wallet and certain kaspactl utility communications
|
||||||
|
rpcServer, err := grpcserver.NewRPCServer(cfg.RPCListeners, cfg.RPCMaxClients, cfg.RPCAuth, cfg.RPCCert, cfg.RPCKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,21 +3,29 @@ package grpcserver
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/server"
|
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/server"
|
||||||
"github.com/kaspanet/kaspad/util/panics"
|
"github.com/kaspanet/kaspad/util/panics"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/credentials"
|
||||||
"google.golang.org/grpc/peer"
|
"google.golang.org/grpc/peer"
|
||||||
"net"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// RequestModifier can modify the http request
|
||||||
|
type RequestModifier func(r *grpc.Server)
|
||||||
|
|
||||||
type gRPCServer struct {
|
type gRPCServer struct {
|
||||||
|
// modifiers are applied before any request
|
||||||
|
//modifiers []RequestModifier
|
||||||
onConnectedHandler server.OnConnectedHandler
|
onConnectedHandler server.OnConnectedHandler
|
||||||
listeningAddresses []string
|
listeningAddresses []string
|
||||||
server *grpc.Server
|
server *grpc.Server
|
||||||
name string
|
name string
|
||||||
|
auth string
|
||||||
|
|
||||||
maxInboundConnections int
|
maxInboundConnections int
|
||||||
inboundConnectionCount int
|
inboundConnectionCount int
|
||||||
@ -25,15 +33,30 @@ type gRPCServer struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// newGRPCServer creates a gRPC server
|
// newGRPCServer creates a gRPC server
|
||||||
func newGRPCServer(listeningAddresses []string, maxMessageSize int, maxInboundConnections int, name string) *gRPCServer {
|
func newGRPCServer(listeningAddresses []string, maxMessageSize int, maxInboundConnections int, name string, auth string, certFile string, keyFile string) *gRPCServer {
|
||||||
log.Debugf("Created new %s GRPC server with maxMessageSize %d and maxInboundConnections %d", name, maxMessageSize, maxInboundConnections)
|
log.Debugf("Created new %s GRPC server with maxMessageSize %d and maxInboundConnections %d", name, maxMessageSize, maxInboundConnections)
|
||||||
return &gRPCServer{
|
log.Warnf("Name: %s for grpc auth type: %s", name, auth)
|
||||||
server: grpc.NewServer(grpc.MaxRecvMsgSize(maxMessageSize), grpc.MaxSendMsgSize(maxMessageSize)),
|
if auth == "tls" {
|
||||||
listeningAddresses: listeningAddresses,
|
creds, _ := credentials.NewServerTLSFromFile(certFile, keyFile)
|
||||||
name: name,
|
return &gRPCServer{
|
||||||
maxInboundConnections: maxInboundConnections,
|
server: grpc.NewServer(grpc.Creds(creds), grpc.MaxRecvMsgSize(maxMessageSize), grpc.MaxSendMsgSize(maxMessageSize)),
|
||||||
inboundConnectionCount: 0,
|
listeningAddresses: listeningAddresses,
|
||||||
inboundConnectionCountLock: &sync.Mutex{},
|
name: name,
|
||||||
|
auth: auth,
|
||||||
|
maxInboundConnections: maxInboundConnections,
|
||||||
|
inboundConnectionCount: 0,
|
||||||
|
inboundConnectionCountLock: &sync.Mutex{},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return &gRPCServer{
|
||||||
|
server: grpc.NewServer(grpc.MaxRecvMsgSize(maxMessageSize), grpc.MaxSendMsgSize(maxMessageSize)),
|
||||||
|
listeningAddresses: listeningAddresses,
|
||||||
|
name: name,
|
||||||
|
auth: auth,
|
||||||
|
maxInboundConnections: maxInboundConnections,
|
||||||
|
inboundConnectionCount: 0,
|
||||||
|
inboundConnectionCountLock: &sync.Mutex{},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,9 @@ package grpcserver
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/server"
|
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/server"
|
||||||
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/server/grpcserver/protowire"
|
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/server/grpcserver/protowire"
|
||||||
"github.com/kaspanet/kaspad/util/panics"
|
"github.com/kaspanet/kaspad/util/panics"
|
||||||
@ -9,10 +12,10 @@ import (
|
|||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/encoding/gzip"
|
"google.golang.org/grpc/encoding/gzip"
|
||||||
"google.golang.org/grpc/peer"
|
"google.golang.org/grpc/peer"
|
||||||
"net"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// p2pServer is a gRCP server for internodes communication
|
||||||
|
|
||||||
type p2pServer struct {
|
type p2pServer struct {
|
||||||
protowire.UnimplementedP2PServer
|
protowire.UnimplementedP2PServer
|
||||||
gRPCServer
|
gRPCServer
|
||||||
@ -28,7 +31,7 @@ const p2pMaxInboundConnections = 0
|
|||||||
|
|
||||||
// NewP2PServer creates a new P2PServer
|
// NewP2PServer creates a new P2PServer
|
||||||
func NewP2PServer(listeningAddresses []string) (server.P2PServer, error) {
|
func NewP2PServer(listeningAddresses []string) (server.P2PServer, error) {
|
||||||
gRPCServer := newGRPCServer(listeningAddresses, p2pMaxMessageSize, p2pMaxInboundConnections, "P2P")
|
gRPCServer := newGRPCServer(listeningAddresses, p2pMaxMessageSize, p2pMaxInboundConnections, "P2P", "none", "", "")
|
||||||
p2pServer := &p2pServer{gRPCServer: *gRPCServer}
|
p2pServer := &p2pServer{gRPCServer: *gRPCServer}
|
||||||
protowire.RegisterP2PServer(gRPCServer.server, p2pServer)
|
protowire.RegisterP2PServer(gRPCServer.server, p2pServer)
|
||||||
return p2pServer, nil
|
return p2pServer, nil
|
||||||
|
|||||||
@ -6,6 +6,8 @@ import (
|
|||||||
"github.com/kaspanet/kaspad/util/panics"
|
"github.com/kaspanet/kaspad/util/panics"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// rpcServer is for gRCP miner, wallet and certain kaspactl utility communications
|
||||||
|
// This is for outside querying of the node's state.
|
||||||
type rpcServer struct {
|
type rpcServer struct {
|
||||||
protowire.UnimplementedRPCServer
|
protowire.UnimplementedRPCServer
|
||||||
gRPCServer
|
gRPCServer
|
||||||
@ -15,8 +17,9 @@ type rpcServer struct {
|
|||||||
const RPCMaxMessageSize = 1024 * 1024 * 1024 // 1 GB
|
const RPCMaxMessageSize = 1024 * 1024 * 1024 // 1 GB
|
||||||
|
|
||||||
// NewRPCServer creates a new RPCServer
|
// NewRPCServer creates a new RPCServer
|
||||||
func NewRPCServer(listeningAddresses []string, rpcMaxInboundConnections int) (server.Server, error) {
|
// @TODO make this a variadic function for better middleware and number of variable args passed in
|
||||||
gRPCServer := newGRPCServer(listeningAddresses, RPCMaxMessageSize, rpcMaxInboundConnections, "RPC")
|
func NewRPCServer(listeningAddresses []string, rpcMaxInboundConnections int, rpcAuth string, rpcCert string, rpcKey string) (server.Server, error) {
|
||||||
|
gRPCServer := newGRPCServer(listeningAddresses, RPCMaxMessageSize, rpcMaxInboundConnections, "RPC", rpcAuth, rpcCert, rpcKey)
|
||||||
rpcServer := &rpcServer{gRPCServer: *gRPCServer}
|
rpcServer := &rpcServer{gRPCServer: *gRPCServer}
|
||||||
protowire.RegisterRPCServer(gRPCServer.server, rpcServer)
|
protowire.RegisterRPCServer(gRPCServer.server, rpcServer)
|
||||||
return rpcServer, nil
|
return rpcServer, nil
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user