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"
|
storagenode: accept HTTP calls on public port, listening for monitoring requests
Today each storagenode should have a port which is opened for the internet, and handles DRPC protocol calls.
When we do a HTTP call on the DRPC endpoint, it hangs until a timeout.
This patch changes the behavior: the main DRPC port of the storagenodes can accept HTTP requests and can be used to monitor the status of the node:
* if returns with HTTP 200 only if the storagnode is healthy (not suspended / disqualified + online score > 0.9)
* it CAN include information about the current status (per satellite). It's opt-in, you should configure it so.
In this way it becomes extremely easy to monitor storagenodes with external uptime services.
Note: this patch exposes some information which was not easily available before (especially the node status, and used satellites). I think it should be acceptable:
* Until having more community satellites, all storagenodes are connected to the main Storj satellites.
* With community satellites, it's good thing to have more transparency (easy way to check who is connected to which satellites)
The implementation is based on this line:
```
http.Serve(NewPrefixedListener([]byte("GET / HT"), publicMux.Route("GET / HT")), p.public.http)
```
This line answers to the TCP requests with `GET / HT...` (GET HTTP request to the route), but puts back the removed prefix.
Change-Id: I3700c7e24524850825ecdf75a4bcc3b4afcb3a74
2022-08-23 11:28:41 +01:00
|
|
|
"net/http"
|
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
|
|
|
|
storagenode: accept HTTP calls on public port, listening for monitoring requests
Today each storagenode should have a port which is opened for the internet, and handles DRPC protocol calls.
When we do a HTTP call on the DRPC endpoint, it hangs until a timeout.
This patch changes the behavior: the main DRPC port of the storagenodes can accept HTTP requests and can be used to monitor the status of the node:
* if returns with HTTP 200 only if the storagnode is healthy (not suspended / disqualified + online score > 0.9)
* it CAN include information about the current status (per satellite). It's opt-in, you should configure it so.
In this way it becomes extremely easy to monitor storagenodes with external uptime services.
Note: this patch exposes some information which was not easily available before (especially the node status, and used satellites). I think it should be acceptable:
* Until having more community satellites, all storagenodes are connected to the main Storj satellites.
* With community satellites, it's good thing to have more transparency (easy way to check who is connected to which satellites)
The implementation is based on this line:
```
http.Serve(NewPrefixedListener([]byte("GET / HT"), publicMux.Route("GET / HT")), p.public.http)
```
This line answers to the TCP requests with `GET / HT...` (GET HTTP request to the route), but puts back the removed prefix.
Change-Id: I3700c7e24524850825ecdf75a4bcc3b4afcb3a74
2022-08-23 11:28:41 +01:00
|
|
|
"storj.io/common/errs2"
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/identity"
|
|
|
|
"storj.io/common/peertls/tlsopts"
|
|
|
|
"storj.io/common/rpc"
|
2021-08-03 04:25:41 +01:00
|
|
|
"storj.io/common/rpc/quic"
|
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"
|
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
|
storagenode: accept HTTP calls on public port, listening for monitoring requests
Today each storagenode should have a port which is opened for the internet, and handles DRPC protocol calls.
When we do a HTTP call on the DRPC endpoint, it hangs until a timeout.
This patch changes the behavior: the main DRPC port of the storagenodes can accept HTTP requests and can be used to monitor the status of the node:
* if returns with HTTP 200 only if the storagnode is healthy (not suspended / disqualified + online score > 0.9)
* it CAN include information about the current status (per satellite). It's opt-in, you should configure it so.
In this way it becomes extremely easy to monitor storagenodes with external uptime services.
Note: this patch exposes some information which was not easily available before (especially the node status, and used satellites). I think it should be acceptable:
* Until having more community satellites, all storagenodes are connected to the main Storj satellites.
* With community satellites, it's good thing to have more transparency (easy way to check who is connected to which satellites)
The implementation is based on this line:
```
http.Serve(NewPrefixedListener([]byte("GET / HT"), publicMux.Route("GET / HT")), p.public.http)
```
This line answers to the TCP requests with `GET / HT...` (GET HTTP request to the route), but puts back the removed prefix.
Change-Id: I3700c7e24524850825ecdf75a4bcc3b4afcb3a74
2022-08-23 11:28:41 +01:00
|
|
|
// http fallback for the public endpoint
|
|
|
|
http http.HandlerFunc
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-01-12 14:34:32 +00:00
|
|
|
// IsQUICEnabled checks if QUIC is enabled by config and udp port is open.
|
|
|
|
func (p *Server) IsQUICEnabled() bool { return !p.public.disableQUIC && p.public.udpConn != nil }
|
2021-12-21 05:33:57 +00: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
|
|
|
|
}
|
|
|
|
|
storagenode: accept HTTP calls on public port, listening for monitoring requests
Today each storagenode should have a port which is opened for the internet, and handles DRPC protocol calls.
When we do a HTTP call on the DRPC endpoint, it hangs until a timeout.
This patch changes the behavior: the main DRPC port of the storagenodes can accept HTTP requests and can be used to monitor the status of the node:
* if returns with HTTP 200 only if the storagnode is healthy (not suspended / disqualified + online score > 0.9)
* it CAN include information about the current status (per satellite). It's opt-in, you should configure it so.
In this way it becomes extremely easy to monitor storagenodes with external uptime services.
Note: this patch exposes some information which was not easily available before (especially the node status, and used satellites). I think it should be acceptable:
* Until having more community satellites, all storagenodes are connected to the main Storj satellites.
* With community satellites, it's good thing to have more transparency (easy way to check who is connected to which satellites)
The implementation is based on this line:
```
http.Serve(NewPrefixedListener([]byte("GET / HT"), publicMux.Route("GET / HT")), p.public.http)
```
This line answers to the TCP requests with `GET / HT...` (GET HTTP request to the route), but puts back the removed prefix.
Change-Id: I3700c7e24524850825ecdf75a4bcc3b4afcb3a74
2022-08-23 11:28:41 +01:00
|
|
|
// AddHTTPFallback adds http fallback to the drpc endpoint.
|
|
|
|
func (p *Server) AddHTTPFallback(httpHandler http.HandlerFunc) {
|
|
|
|
p.public.http = httpHandler
|
|
|
|
}
|
|
|
|
|
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
|
storagenode: accept HTTP calls on public port, listening for monitoring requests
Today each storagenode should have a port which is opened for the internet, and handles DRPC protocol calls.
When we do a HTTP call on the DRPC endpoint, it hangs until a timeout.
This patch changes the behavior: the main DRPC port of the storagenodes can accept HTTP requests and can be used to monitor the status of the node:
* if returns with HTTP 200 only if the storagnode is healthy (not suspended / disqualified + online score > 0.9)
* it CAN include information about the current status (per satellite). It's opt-in, you should configure it so.
In this way it becomes extremely easy to monitor storagenodes with external uptime services.
Note: this patch exposes some information which was not easily available before (especially the node status, and used satellites). I think it should be acceptable:
* Until having more community satellites, all storagenodes are connected to the main Storj satellites.
* With community satellites, it's good thing to have more transparency (easy way to check who is connected to which satellites)
The implementation is based on this line:
```
http.Serve(NewPrefixedListener([]byte("GET / HT"), publicMux.Route("GET / HT")), p.public.http)
```
This line answers to the TCP requests with `GET / HT...` (GET HTTP request to the route), but puts back the removed prefix.
Change-Id: I3700c7e24524850825ecdf75a4bcc3b4afcb3a74
2022-08-23 11:28:41 +01:00
|
|
|
publicHTTPListener net.Listener
|
2021-01-28 23:24:35 +00:00
|
|
|
)
|
|
|
|
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())
|
storagenode: accept HTTP calls on public port, listening for monitoring requests
Today each storagenode should have a port which is opened for the internet, and handles DRPC protocol calls.
When we do a HTTP call on the DRPC endpoint, it hangs until a timeout.
This patch changes the behavior: the main DRPC port of the storagenodes can accept HTTP requests and can be used to monitor the status of the node:
* if returns with HTTP 200 only if the storagnode is healthy (not suspended / disqualified + online score > 0.9)
* it CAN include information about the current status (per satellite). It's opt-in, you should configure it so.
In this way it becomes extremely easy to monitor storagenodes with external uptime services.
Note: this patch exposes some information which was not easily available before (especially the node status, and used satellites). I think it should be acceptable:
* Until having more community satellites, all storagenodes are connected to the main Storj satellites.
* With community satellites, it's good thing to have more transparency (easy way to check who is connected to which satellites)
The implementation is based on this line:
```
http.Serve(NewPrefixedListener([]byte("GET / HT"), publicMux.Route("GET / HT")), p.public.http)
```
This line answers to the TCP requests with `GET / HT...` (GET HTTP request to the route), but puts back the removed prefix.
Change-Id: I3700c7e24524850825ecdf75a4bcc3b4afcb3a74
2022-08-23 11:28:41 +01:00
|
|
|
|
|
|
|
if p.public.http != nil {
|
|
|
|
publicHTTPListener = NewPrefixedListener([]byte("GET / HT"), publicMux.Route("GET / HT"))
|
|
|
|
}
|
2021-01-28 23:24:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.public.udpConn != nil {
|
2021-08-03 04:25:41 +01:00
|
|
|
p.public.quicListener, err = quic.NewListener(p.public.udpConn, p.tlsOptions.ServerTLSConfig(), nil)
|
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))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
storagenode: accept HTTP calls on public port, listening for monitoring requests
Today each storagenode should have a port which is opened for the internet, and handles DRPC protocol calls.
When we do a HTTP call on the DRPC endpoint, it hangs until a timeout.
This patch changes the behavior: the main DRPC port of the storagenodes can accept HTTP requests and can be used to monitor the status of the node:
* if returns with HTTP 200 only if the storagnode is healthy (not suspended / disqualified + online score > 0.9)
* it CAN include information about the current status (per satellite). It's opt-in, you should configure it so.
In this way it becomes extremely easy to monitor storagenodes with external uptime services.
Note: this patch exposes some information which was not easily available before (especially the node status, and used satellites). I think it should be acceptable:
* Until having more community satellites, all storagenodes are connected to the main Storj satellites.
* With community satellites, it's good thing to have more transparency (easy way to check who is connected to which satellites)
The implementation is based on this line:
```
http.Serve(NewPrefixedListener([]byte("GET / HT"), publicMux.Route("GET / HT")), p.public.http)
```
This line answers to the TCP requests with `GET / HT...` (GET HTTP request to the route), but puts back the removed prefix.
Change-Id: I3700c7e24524850825ecdf75a4bcc3b4afcb3a74
2022-08-23 11:28:41 +01:00
|
|
|
if publicHTTPListener != nil {
|
|
|
|
// this http server listens on the filtered messages of the incoming DRPC port, instead of a separated port
|
|
|
|
httpServer := http.Server{
|
|
|
|
Handler: p.public.http,
|
|
|
|
}
|
|
|
|
|
|
|
|
group.Go(func() error {
|
|
|
|
<-ctx.Done()
|
|
|
|
return httpServer.Shutdown(context.Background())
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
defer cancel()
|
|
|
|
err := httpServer.Serve(publicHTTPListener)
|
|
|
|
if errs2.IsCanceled(err) || errors.Is(err, http.ErrServerClosed) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|