2020-08-21 15:01:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-11-30 10:07:46 +00:00
|
|
|
"crypto/tls"
|
2022-10-25 06:48:02 +00:00
|
|
|
"errors"
|
2020-08-22 11:03:45 +00:00
|
|
|
"fmt"
|
2020-08-21 15:01:59 +00:00
|
|
|
"net"
|
2022-03-25 10:28:07 +00:00
|
|
|
"time"
|
2020-08-21 15:01:59 +00:00
|
|
|
|
2021-06-01 18:53:31 +00:00
|
|
|
grpcconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/grpc"
|
2021-01-18 08:56:14 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
|
|
|
"go.uber.org/zap"
|
2020-08-21 15:01:59 +00:00
|
|
|
"google.golang.org/grpc"
|
2021-05-14 09:11:49 +00:00
|
|
|
"google.golang.org/grpc/credentials"
|
2020-08-21 15:01:59 +00:00
|
|
|
)
|
|
|
|
|
2020-08-24 09:40:32 +00:00
|
|
|
func initGRPC(c *cfg) {
|
2022-10-25 06:48:02 +00:00
|
|
|
var successCount int
|
2021-06-22 17:25:18 +00:00
|
|
|
grpcconfig.IterateEndpoints(c.appCfg, func(sc *grpcconfig.Config) {
|
|
|
|
serverOpts := []grpc.ServerOption{
|
|
|
|
grpc.MaxSendMsgSize(maxMsgSize),
|
|
|
|
}
|
2021-05-14 09:11:49 +00:00
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
tlsCfg := sc.TLS()
|
2021-05-14 09:11:49 +00:00
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
if tlsCfg != nil {
|
2021-11-30 10:07:46 +00:00
|
|
|
cert, err := tls.LoadX509KeyPair(tlsCfg.CertificateFile(), tlsCfg.KeyFile())
|
2022-10-25 06:48:02 +00:00
|
|
|
if err != nil {
|
|
|
|
c.log.Error("could not read certificate from file", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
2021-11-30 10:07:46 +00:00
|
|
|
|
|
|
|
var cipherSuites []uint16
|
|
|
|
if !tlsCfg.UseInsecureCrypto() {
|
|
|
|
// This more or less follows the list in https://wiki.mozilla.org/Security/Server_Side_TLS
|
|
|
|
// excluding:
|
|
|
|
// 1. TLS 1.3 suites need not be specified here.
|
|
|
|
// 2. Suites that use DH key exchange are not implemented by stdlib.
|
|
|
|
cipherSuites = []uint16{
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
creds := credentials.NewTLS(&tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
CipherSuites: cipherSuites,
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
})
|
2021-06-22 17:25:18 +00:00
|
|
|
|
|
|
|
serverOpts = append(serverOpts, grpc.Creds(creds))
|
|
|
|
}
|
|
|
|
|
2022-10-25 06:48:02 +00:00
|
|
|
lis, err := net.Listen("tcp", sc.Endpoint())
|
|
|
|
if err != nil {
|
|
|
|
c.log.Error("can't listen gRPC endpoint", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.cfgGRPC.listeners = append(c.cfgGRPC.listeners, lis)
|
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
srv := grpc.NewServer(serverOpts...)
|
2021-05-14 09:11:49 +00:00
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
c.onShutdown(func() {
|
|
|
|
stopGRPC("NeoFS Public API", srv, c.log)
|
|
|
|
})
|
2021-01-18 08:56:14 +00:00
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
c.cfgGRPC.servers = append(c.cfgGRPC.servers, srv)
|
2022-10-25 06:48:02 +00:00
|
|
|
successCount++
|
2021-01-18 08:56:14 +00:00
|
|
|
})
|
2022-10-25 06:48:02 +00:00
|
|
|
|
|
|
|
if successCount == 0 {
|
|
|
|
fatalOnErr(errors.New("could not listen to any gRPC endpoints"))
|
|
|
|
}
|
2020-08-24 09:40:32 +00:00
|
|
|
}
|
2020-08-22 11:03:45 +00:00
|
|
|
|
2020-08-24 09:40:32 +00:00
|
|
|
func serveGRPC(c *cfg) {
|
2021-06-22 17:25:18 +00:00
|
|
|
for i := range c.cfgGRPC.servers {
|
|
|
|
c.wg.Add(1)
|
2020-12-29 15:50:41 +00:00
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
srv := c.cfgGRPC.servers[i]
|
|
|
|
lis := c.cfgGRPC.listeners[i]
|
2020-08-22 11:03:45 +00:00
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
go func() {
|
|
|
|
defer func() {
|
2021-08-04 14:44:37 +00:00
|
|
|
c.log.Info("stop listening gRPC endpoint",
|
|
|
|
zap.String("endpoint", lis.Addr().String()),
|
|
|
|
)
|
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
c.wg.Done()
|
|
|
|
}()
|
|
|
|
|
2021-08-04 14:44:37 +00:00
|
|
|
c.log.Info("start listening gRPC endpoint",
|
|
|
|
zap.String("endpoint", lis.Addr().String()),
|
|
|
|
)
|
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
if err := srv.Serve(lis); err != nil {
|
|
|
|
fmt.Println("gRPC server error", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2020-08-21 15:01:59 +00:00
|
|
|
}
|
2021-01-18 08:56:14 +00:00
|
|
|
|
|
|
|
func stopGRPC(name string, s *grpc.Server, l *logger.Logger) {
|
2022-09-28 07:41:01 +00:00
|
|
|
l = &logger.Logger{Logger: l.With(zap.String("name", name))}
|
2021-01-18 08:56:14 +00:00
|
|
|
|
|
|
|
l.Info("stopping gRPC server...")
|
|
|
|
|
2022-03-25 10:28:07 +00:00
|
|
|
// GracefulStop() may freeze forever, see #1270
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
s.GracefulStop()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(1 * time.Minute):
|
|
|
|
l.Info("gRPC cannot shutdown gracefully, forcing stop")
|
|
|
|
s.Stop()
|
|
|
|
}
|
2021-01-18 08:56:14 +00:00
|
|
|
|
|
|
|
l.Info("gRPC server stopped successfully")
|
|
|
|
}
|