2019-01-02 10:23:25 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-19 05:46:39 +01:00
|
|
|
"crypto/tls"
|
2021-01-28 19:43:47 +00:00
|
|
|
"errors"
|
2019-01-02 10:23:25 +00:00
|
|
|
"net"
|
2021-01-28 19:43:47 +00:00
|
|
|
"os"
|
|
|
|
"runtime"
|
2019-09-07 01:02:38 +01:00
|
|
|
"sync"
|
2021-01-28 19:43:47 +00:00
|
|
|
"syscall"
|
2019-01-02 10:23:25 +00:00
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
"github.com/zeebo/errs"
|
2019-07-31 13:09:45 +01:00
|
|
|
"go.uber.org/zap"
|
2019-02-04 14:50:55 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2019-01-02 10:23:25 +00:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/identity"
|
|
|
|
"storj.io/common/peertls/tlsopts"
|
|
|
|
"storj.io/common/rpc"
|
2020-03-30 16:22:32 +01:00
|
|
|
"storj.io/common/rpc/rpctracing"
|
2021-03-23 15:17:51 +00:00
|
|
|
"storj.io/drpc/drpcmigrate"
|
2020-03-24 17:49:20 +00:00
|
|
|
"storj.io/drpc/drpcmux"
|
2019-09-07 01:02:38 +01:00
|
|
|
"storj.io/drpc/drpcserver"
|
2020-03-30 16:22:32 +01:00
|
|
|
jaeger "storj.io/monkit-jaeger"
|
2021-04-23 14:13:51 +01:00
|
|
|
"storj.io/storj/private/quic"
|
2019-01-02 10:23:25 +00:00
|
|
|
)
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Config holds server specific configuration parameters.
|
2020-03-24 16:28:19 +00:00
|
|
|
type Config struct {
|
|
|
|
tlsopts.Config
|
2020-05-11 06:26:32 +01:00
|
|
|
Address string `user:"true" help:"public address to listen on" default:":7777"`
|
|
|
|
PrivateAddress string `user:"true" help:"private address to listen on" default:"127.0.0.1:7778"`
|
2021-01-28 23:24:35 +00:00
|
|
|
DisableQUIC bool `help:"disable QUIC listener on a server" hidden:"true" default:"false"`
|
2020-05-11 06:26:32 +01:00
|
|
|
|
2021-01-28 23:24:35 +00:00
|
|
|
DisableTCPTLS bool `help:"disable TCP/TLS listener on a server" internal:"true"`
|
2020-05-11 06:26:32 +01:00
|
|
|
DebugLogTraffic bool `hidden:"true" default:"false"` // Deprecated
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
type public struct {
|
2021-01-28 23:24:35 +00:00
|
|
|
tcpListener net.Listener
|
|
|
|
udpConn *net.UDPConn
|
|
|
|
quicListener net.Listener
|
|
|
|
addr net.Addr
|
|
|
|
disableTCPTLS bool
|
|
|
|
disableQUIC bool
|
|
|
|
|
|
|
|
drpc *drpcserver.Server
|
|
|
|
mux *drpcmux.Mux
|
2019-03-07 18:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type private struct {
|
|
|
|
listener net.Listener
|
2019-09-07 01:02:38 +01:00
|
|
|
drpc *drpcserver.Server
|
2020-03-24 17:49:20 +00:00
|
|
|
mux *drpcmux.Mux
|
2019-03-07 18:19:37 +00:00
|
|
|
}
|
|
|
|
|
2019-01-02 10:23:25 +00:00
|
|
|
// Server represents a bundle of services defined by a specific ID.
|
|
|
|
// Examples of servers are the satellite, the storagenode, and the uplink.
|
|
|
|
type Server struct {
|
2019-09-19 05:46:39 +01:00
|
|
|
log *zap.Logger
|
|
|
|
public public
|
|
|
|
private private
|
|
|
|
tlsOptions *tlsopts.Options
|
2019-09-07 01:02:38 +01:00
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
wg sync.WaitGroup
|
|
|
|
once sync.Once
|
|
|
|
done chan struct{}
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 15:04:53 +00:00
|
|
|
// New creates a Server out of an Identity, a net.Listener,
|
2020-03-25 19:38:08 +00:00
|
|
|
// and interceptors.
|
2021-01-28 23:24:35 +00:00
|
|
|
func New(log *zap.Logger, tlsOptions *tlsopts.Options, config Config) (_ *Server, err error) {
|
2019-07-31 13:09:45 +01:00
|
|
|
server := &Server{
|
2019-09-19 05:46:39 +01:00
|
|
|
log: log,
|
|
|
|
tlsOptions: tlsOptions,
|
|
|
|
done: make(chan struct{}),
|
2019-07-31 13:09:45 +01:00
|
|
|
}
|
|
|
|
|
2021-01-28 23:24:35 +00:00
|
|
|
server.public, err = newPublic(config.Address, config.DisableTCPTLS, config.DisableQUIC)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
2021-01-19 16:33:50 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 23:24:35 +00:00
|
|
|
serverOptions := drpcserver.Options{
|
|
|
|
Manager: rpc.NewDefaultManagerOptions(),
|
2019-03-07 18:19:37 +00:00
|
|
|
}
|
2021-01-28 23:24:35 +00:00
|
|
|
privateListener, err := net.Listen("tcp", config.PrivateAddress)
|
2019-03-07 18:19:37 +00:00
|
|
|
if err != nil {
|
2021-01-28 23:24:35 +00:00
|
|
|
return nil, errs.Combine(err, server.public.Close())
|
2019-03-07 18:19:37 +00:00
|
|
|
}
|
2020-03-24 17:49:20 +00:00
|
|
|
privateMux := drpcmux.New()
|
2020-03-30 16:22:32 +01:00
|
|
|
privateTracingHandler := rpctracing.NewHandler(privateMux, jaeger.RemoteTraceHandler)
|
2019-07-31 13:09:45 +01:00
|
|
|
server.private = private{
|
2020-01-03 20:21:34 +00:00
|
|
|
listener: wrapListener(privateListener),
|
2020-03-30 16:22:32 +01:00
|
|
|
drpc: drpcserver.NewWithOptions(privateTracingHandler, serverOptions),
|
2020-03-24 17:49:20 +00:00
|
|
|
mux: privateMux,
|
2019-03-07 18:19:37 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:09:45 +01:00
|
|
|
return server, nil
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Identity returns the server's identity.
|
2019-09-19 05:46:39 +01:00
|
|
|
func (p *Server) Identity() *identity.FullIdentity { return p.tlsOptions.Ident }
|
2019-01-02 10:23:25 +00:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Addr returns the server's public listener address.
|
2021-01-28 23:24:35 +00:00
|
|
|
func (p *Server) Addr() net.Addr { return p.public.addr }
|
2019-03-07 18:19:37 +00:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// PrivateAddr returns the server's private listener address.
|
2019-03-07 18:19:37 +00:00
|
|
|
func (p *Server) PrivateAddr() net.Addr { return p.private.listener.Addr() }
|
2019-01-02 10:23:25 +00:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// DRPC returns the server's dRPC mux for registration purposes.
|
2020-03-24 17:49:20 +00:00
|
|
|
func (p *Server) DRPC() *drpcmux.Mux { return p.public.mux }
|
2019-09-07 01:02:38 +01:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// PrivateDRPC returns the server's dRPC mux for registration purposes.
|
2020-03-24 17:49:20 +00:00
|
|
|
func (p *Server) PrivateDRPC() *drpcmux.Mux { return p.private.mux }
|
2019-09-07 01:02:38 +01:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Close shuts down the server.
|
2019-01-02 10:23:25 +00:00
|
|
|
func (p *Server) Close() error {
|
2019-09-07 01:02:38 +01:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
|
|
|
// Close done and wait for any Runs to exit.
|
|
|
|
p.once.Do(func() { close(p.done) })
|
|
|
|
p.wg.Wait()
|
|
|
|
|
|
|
|
// Ensure the listeners are closed in case Run was never called.
|
|
|
|
// We ignore these errors because there's not really anything to do
|
|
|
|
// even if they happen, and they'll just be errors due to duplicate
|
|
|
|
// closes anyway.
|
2021-01-28 23:24:35 +00:00
|
|
|
_ = p.public.Close()
|
2019-09-07 01:02:38 +01:00
|
|
|
_ = p.private.listener.Close()
|
2019-01-02 10:23:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Run will run the server and all of its services.
|
2019-01-02 10:23:25 +00:00
|
|
|
func (p *Server) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-09-07 01:02:38 +01:00
|
|
|
// Make sure the server isn't already closed. If it is, register
|
|
|
|
// ourselves in the wait group so that Close can wait on it.
|
|
|
|
p.mu.Lock()
|
|
|
|
select {
|
|
|
|
case <-p.done:
|
|
|
|
p.mu.Unlock()
|
|
|
|
return errs.New("server closed")
|
|
|
|
default:
|
|
|
|
p.wg.Add(1)
|
|
|
|
defer p.wg.Done()
|
|
|
|
}
|
|
|
|
p.mu.Unlock()
|
|
|
|
|
|
|
|
// We want to launch the muxes in a different group so that they are
|
|
|
|
// only closed after we're sure that p.Close is called. The reason why
|
|
|
|
// is so that we don't get "listener closed" errors because the
|
|
|
|
// Run call exits and closes the listeners before the servers have had
|
|
|
|
// a chance to be notified that they're done running.
|
|
|
|
|
2021-01-28 23:24:35 +00:00
|
|
|
var (
|
2021-03-23 15:17:51 +00:00
|
|
|
publicMux *drpcmigrate.ListenMux
|
2021-01-28 23:24:35 +00:00
|
|
|
publicDRPCListener net.Listener
|
|
|
|
)
|
|
|
|
if p.public.tcpListener != nil {
|
2021-03-23 15:17:51 +00:00
|
|
|
publicMux = drpcmigrate.NewListenMux(p.public.tcpListener, len(drpcmigrate.DRPCHeader))
|
|
|
|
publicDRPCListener = tls.NewListener(publicMux.Route(drpcmigrate.DRPCHeader), p.tlsOptions.ServerTLSConfig())
|
2021-01-28 23:24:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.public.udpConn != nil {
|
2021-03-02 16:56:41 +00:00
|
|
|
p.public.quicListener, err = quic.NewListener(p.public.udpConn, p.tlsOptions.ServerTLSConfig(), defaultQUICConfig())
|
2021-01-28 23:24:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-09-07 01:02:38 +01:00
|
|
|
|
2021-03-23 15:17:51 +00:00
|
|
|
privateMux := drpcmigrate.NewListenMux(p.private.listener, len(drpcmigrate.DRPCHeader))
|
|
|
|
privateDRPCListener := privateMux.Route(drpcmigrate.DRPCHeader)
|
2019-09-07 01:02:38 +01:00
|
|
|
|
|
|
|
// We need a new context chain because we require this context to be
|
2020-05-06 11:54:14 +01:00
|
|
|
// canceled only after all of the upcoming drpc servers have
|
2019-09-07 01:02:38 +01:00
|
|
|
// fully exited. The reason why is because Run closes listener for
|
|
|
|
// the mux when it exits, and we can only do that after all of the
|
|
|
|
// Servers are no longer accepting.
|
|
|
|
muxCtx, muxCancel := context.WithCancel(context.Background())
|
|
|
|
defer muxCancel()
|
|
|
|
|
|
|
|
var muxGroup errgroup.Group
|
2021-01-28 23:24:35 +00:00
|
|
|
if publicMux != nil {
|
|
|
|
muxGroup.Go(func() error {
|
|
|
|
return publicMux.Run(muxCtx)
|
|
|
|
})
|
|
|
|
}
|
2019-09-07 01:02:38 +01:00
|
|
|
muxGroup.Go(func() error {
|
|
|
|
return privateMux.Run(muxCtx)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Now we launch all the stuff that uses the listeners.
|
2019-02-04 14:50:55 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2019-09-07 01:00:42 +01:00
|
|
|
defer cancel()
|
|
|
|
|
2019-02-04 14:50:55 +00:00
|
|
|
var group errgroup.Group
|
2019-09-07 01:00:42 +01:00
|
|
|
group.Go(func() error {
|
2019-09-07 01:02:38 +01:00
|
|
|
select {
|
|
|
|
case <-p.done:
|
|
|
|
cancel()
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-09-07 01:00:42 +01:00
|
|
|
})
|
2019-09-07 01:02:38 +01:00
|
|
|
|
2021-01-28 23:24:35 +00:00
|
|
|
if publicDRPCListener != nil {
|
|
|
|
group.Go(func() error {
|
|
|
|
defer cancel()
|
|
|
|
return p.public.drpc.Serve(ctx, publicDRPCListener)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.public.quicListener != nil {
|
|
|
|
group.Go(func() error {
|
|
|
|
defer cancel()
|
|
|
|
return p.public.drpc.Serve(ctx, wrapListener(p.public.quicListener))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
group.Go(func() error {
|
|
|
|
defer cancel()
|
2019-09-07 01:02:38 +01:00
|
|
|
return p.private.drpc.Serve(ctx, privateDRPCListener)
|
2019-02-04 14:50:55 +00:00
|
|
|
})
|
|
|
|
|
2019-09-07 01:02:38 +01:00
|
|
|
// Now we wait for all the stuff using the listeners to exit.
|
|
|
|
err = group.Wait()
|
|
|
|
|
|
|
|
// Now we close down our listeners.
|
|
|
|
muxCancel()
|
|
|
|
return errs.Combine(err, muxGroup.Wait())
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|
2021-01-28 19:43:47 +00:00
|
|
|
|
2021-01-28 23:24:35 +00:00
|
|
|
func newPublic(publicAddr string, disableTCPTLS, disableQUIC bool) (public, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
publicTCPListener net.Listener
|
|
|
|
publicUDPConn *net.UDPConn
|
|
|
|
)
|
|
|
|
|
|
|
|
for retry := 0; ; retry++ {
|
|
|
|
addr := publicAddr
|
|
|
|
if !disableTCPTLS {
|
|
|
|
publicTCPListener, err = net.Listen("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return public{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = publicTCPListener.Addr().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !disableQUIC {
|
|
|
|
udpAddr, err := net.ResolveUDPAddr("udp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return public{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
publicUDPConn, err = net.ListenUDP("udp", udpAddr)
|
|
|
|
if err != nil {
|
|
|
|
_, port, _ := net.SplitHostPort(publicAddr)
|
|
|
|
if port == "0" && retry < 10 && isErrorAddressAlreadyInUse(err) {
|
|
|
|
// from here, we know for sure that the tcp port chosen by the
|
|
|
|
// os is available, but we don't know if the same port number
|
|
|
|
// for udp is also available.
|
|
|
|
// if a udp port is already in use, we will close the tcp port and retry
|
|
|
|
// to find one that is available for both udp and tcp.
|
|
|
|
_ = publicTCPListener.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return public{}, errs.Combine(err, publicTCPListener.Close())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
publicMux := drpcmux.New()
|
|
|
|
publicTracingHandler := rpctracing.NewHandler(publicMux, jaeger.RemoteTraceHandler)
|
|
|
|
serverOptions := drpcserver.Options{
|
|
|
|
Manager: rpc.NewDefaultManagerOptions(),
|
|
|
|
}
|
|
|
|
|
|
|
|
var netAddr net.Addr
|
|
|
|
if publicTCPListener != nil {
|
|
|
|
netAddr = publicTCPListener.Addr()
|
|
|
|
}
|
|
|
|
|
|
|
|
if publicUDPConn != nil && netAddr == nil {
|
|
|
|
netAddr = publicUDPConn.LocalAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
return public{
|
|
|
|
tcpListener: wrapListener(publicTCPListener),
|
|
|
|
udpConn: publicUDPConn,
|
|
|
|
addr: netAddr,
|
|
|
|
drpc: drpcserver.NewWithOptions(publicTracingHandler, serverOptions),
|
|
|
|
mux: publicMux,
|
|
|
|
disableTCPTLS: disableTCPTLS,
|
|
|
|
disableQUIC: disableQUIC,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p public) Close() (err error) {
|
|
|
|
if p.quicListener != nil {
|
|
|
|
err = p.quicListener.Close()
|
|
|
|
}
|
|
|
|
if p.udpConn != nil {
|
|
|
|
err = errs.Combine(err, p.udpConn.Close())
|
|
|
|
}
|
|
|
|
if p.tcpListener != nil {
|
|
|
|
err = errs.Combine(err, p.tcpListener.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-28 19:43:47 +00:00
|
|
|
// isErrorAddressAlreadyInUse checks whether the error is corresponding to
|
|
|
|
// EADDRINUSE. Taken from https://stackoverflow.com/a/65865898.
|
|
|
|
func isErrorAddressAlreadyInUse(err error) bool {
|
|
|
|
var eOsSyscall *os.SyscallError
|
|
|
|
if !errors.As(err, &eOsSyscall) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var errErrno syscall.Errno
|
|
|
|
if !errors.As(eOsSyscall.Err, &errErrno) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if errErrno == syscall.EADDRINUSE {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
const WSAEADDRINUSE = 10048
|
|
|
|
if runtime.GOOS == "windows" && errErrno == WSAEADDRINUSE {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|