2019-01-18 13:54:08 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2019-03-02 15:22:20 +00:00
|
|
|
"net/mail"
|
|
|
|
"net/smtp"
|
2019-01-23 19:58:44 +00:00
|
|
|
"os"
|
2019-01-18 13:54:08 +00:00
|
|
|
"path/filepath"
|
2019-03-28 20:09:23 +00:00
|
|
|
"time"
|
2019-01-18 13:54:08 +00:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2019-04-17 11:09:44 +01:00
|
|
|
"storj.io/storj/internal/errs2"
|
2019-03-02 15:22:20 +00:00
|
|
|
"storj.io/storj/internal/post"
|
|
|
|
"storj.io/storj/internal/post/oauth2"
|
2019-04-03 20:13:39 +01:00
|
|
|
"storj.io/storj/internal/version"
|
2019-01-18 13:54:08 +00:00
|
|
|
"storj.io/storj/pkg/accounting"
|
2019-05-10 02:39:21 +01:00
|
|
|
"storj.io/storj/pkg/accounting/live"
|
2019-01-23 19:58:44 +00:00
|
|
|
"storj.io/storj/pkg/accounting/rollup"
|
|
|
|
"storj.io/storj/pkg/accounting/tally"
|
|
|
|
"storj.io/storj/pkg/audit"
|
|
|
|
"storj.io/storj/pkg/auth/grpcauth"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/pkg/auth/signing"
|
2019-01-18 13:54:08 +00:00
|
|
|
"storj.io/storj/pkg/bwagreement"
|
2019-02-07 19:22:49 +00:00
|
|
|
"storj.io/storj/pkg/certdb"
|
2019-01-18 13:54:08 +00:00
|
|
|
"storj.io/storj/pkg/datarepair/checker"
|
|
|
|
"storj.io/storj/pkg/datarepair/irreparable"
|
|
|
|
"storj.io/storj/pkg/datarepair/queue"
|
|
|
|
"storj.io/storj/pkg/datarepair/repairer"
|
|
|
|
"storj.io/storj/pkg/discovery"
|
|
|
|
"storj.io/storj/pkg/identity"
|
|
|
|
"storj.io/storj/pkg/kademlia"
|
|
|
|
"storj.io/storj/pkg/overlay"
|
|
|
|
"storj.io/storj/pkg/pb"
|
2019-02-11 11:17:32 +00:00
|
|
|
"storj.io/storj/pkg/peertls/tlsopts"
|
2019-01-18 13:54:08 +00:00
|
|
|
"storj.io/storj/pkg/server"
|
|
|
|
"storj.io/storj/pkg/storj"
|
2019-01-23 19:58:44 +00:00
|
|
|
"storj.io/storj/pkg/transport"
|
2019-01-18 13:54:08 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2019-01-24 16:26:36 +00:00
|
|
|
"storj.io/storj/satellite/console/consoleauth"
|
|
|
|
"storj.io/storj/satellite/console/consoleweb"
|
2019-04-02 15:55:58 +01:00
|
|
|
"storj.io/storj/satellite/inspector"
|
2019-03-02 15:22:20 +00:00
|
|
|
"storj.io/storj/satellite/mailservice"
|
|
|
|
"storj.io/storj/satellite/mailservice/simulate"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/satellite/metainfo"
|
2019-03-27 10:24:35 +00:00
|
|
|
"storj.io/storj/satellite/orders"
|
2019-01-18 13:54:08 +00:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
"storj.io/storj/storage/boltdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DB is the master database for the satellite
|
|
|
|
type DB interface {
|
|
|
|
// CreateTables initializes the database
|
|
|
|
CreateTables() error
|
|
|
|
// Close closes the database
|
|
|
|
Close() error
|
|
|
|
|
2019-02-04 20:37:46 +00:00
|
|
|
// CreateSchema sets the schema
|
|
|
|
CreateSchema(schema string) error
|
2019-01-31 19:17:12 +00:00
|
|
|
// DropSchema drops the schema
|
|
|
|
DropSchema(schema string) error
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
// BandwidthAgreement returns database for storing bandwidth agreements
|
|
|
|
BandwidthAgreement() bwagreement.DB
|
2019-02-07 19:22:49 +00:00
|
|
|
// CertDB returns database for storing uplink's public key & ID
|
|
|
|
CertDB() certdb.DB
|
2019-01-18 13:54:08 +00:00
|
|
|
// OverlayCache returns database for caching overlay information
|
|
|
|
OverlayCache() overlay.DB
|
2019-05-10 20:05:42 +01:00
|
|
|
// StoragenodeAccounting returns database for storing information about storagenode use
|
|
|
|
StoragenodeAccounting() accounting.StoragenodeAccounting
|
|
|
|
// ProjectAccounting returns database for storing information about project data use
|
|
|
|
ProjectAccounting() accounting.ProjectAccounting
|
2019-01-18 13:54:08 +00:00
|
|
|
// RepairQueue returns queue for segments that need repairing
|
|
|
|
RepairQueue() queue.RepairQueue
|
|
|
|
// Irreparable returns database for failed repairs
|
|
|
|
Irreparable() irreparable.DB
|
|
|
|
// Console returns database for satellite console
|
|
|
|
Console() console.DB
|
2019-03-27 10:24:35 +00:00
|
|
|
// Orders returns database for orders
|
|
|
|
Orders() orders.DB
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config is the global config satellite
|
|
|
|
type Config struct {
|
|
|
|
Identity identity.Config
|
|
|
|
|
|
|
|
// TODO: switch to using server.Config when Identity has been removed from it
|
2019-01-23 19:58:44 +00:00
|
|
|
Server server.Config
|
2019-01-18 13:54:08 +00:00
|
|
|
|
|
|
|
Kademlia kademlia.Config
|
|
|
|
Overlay overlay.Config
|
|
|
|
Discovery discovery.Config
|
|
|
|
|
2019-04-25 09:46:32 +01:00
|
|
|
Metainfo metainfo.Config
|
2019-01-23 19:58:44 +00:00
|
|
|
BwAgreement bwagreement.Config // TODO: decide whether to keep empty configs for consistency
|
2019-01-18 13:54:08 +00:00
|
|
|
|
|
|
|
Checker checker.Config
|
|
|
|
Repairer repairer.Config
|
2019-01-23 19:58:44 +00:00
|
|
|
Audit audit.Config
|
|
|
|
|
2019-05-10 02:39:21 +01:00
|
|
|
Tally tally.Config
|
|
|
|
Rollup rollup.Config
|
|
|
|
LiveAccounting live.Config
|
2019-01-24 16:26:36 +00:00
|
|
|
|
2019-03-02 15:22:20 +00:00
|
|
|
Mail mailservice.Config
|
2019-01-24 16:26:36 +00:00
|
|
|
Console consoleweb.Config
|
2019-04-03 20:13:39 +01:00
|
|
|
|
|
|
|
Version version.Config
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Peer is the satellite
|
|
|
|
type Peer struct {
|
|
|
|
// core dependencies
|
|
|
|
Log *zap.Logger
|
|
|
|
Identity *identity.FullIdentity
|
|
|
|
DB DB
|
|
|
|
|
2019-02-06 12:37:17 +00:00
|
|
|
Transport transport.Client
|
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
Server *server.Server
|
2019-01-18 13:54:08 +00:00
|
|
|
|
2019-04-03 20:13:39 +01:00
|
|
|
Version *version.Service
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
// services and endpoints
|
|
|
|
Kademlia struct {
|
2019-01-23 19:58:44 +00:00
|
|
|
kdb, ndb storage.KeyValueStore // TODO: move these into DB
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
RoutingTable *kademlia.RoutingTable
|
|
|
|
Service *kademlia.Kademlia
|
2019-02-06 12:37:17 +00:00
|
|
|
Endpoint *kademlia.Endpoint
|
2019-01-23 19:58:44 +00:00
|
|
|
Inspector *kademlia.Inspector
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Overlay struct {
|
2019-01-23 19:58:44 +00:00
|
|
|
Service *overlay.Cache
|
|
|
|
Inspector *overlay.Inspector
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Discovery struct {
|
|
|
|
Service *discovery.Discovery
|
|
|
|
}
|
|
|
|
|
|
|
|
Metainfo struct {
|
2019-03-30 11:21:49 +00:00
|
|
|
Database storage.KeyValueStore // TODO: move into pointerDB
|
2019-04-25 09:46:32 +01:00
|
|
|
Service *metainfo.Service
|
2019-03-30 11:21:49 +00:00
|
|
|
Endpoint2 *metainfo.Endpoint
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 15:55:58 +01:00
|
|
|
Inspector struct {
|
|
|
|
Endpoint *inspector.Endpoint
|
|
|
|
}
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
Agreements struct {
|
|
|
|
Endpoint *bwagreement.Server
|
|
|
|
}
|
|
|
|
|
2019-03-27 10:24:35 +00:00
|
|
|
Orders struct {
|
|
|
|
Endpoint *orders.Endpoint
|
2019-03-28 20:09:23 +00:00
|
|
|
Service *orders.Service
|
2019-03-27 10:24:35 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
Repair struct {
|
2019-03-15 20:21:52 +00:00
|
|
|
Checker *checker.Checker
|
|
|
|
Repairer *repairer.Service
|
|
|
|
Inspector *irreparable.Inspector
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
Audit struct {
|
2019-01-23 19:58:44 +00:00
|
|
|
Service *audit.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
Accounting struct {
|
2019-04-04 16:20:59 +01:00
|
|
|
Tally *tally.Service
|
|
|
|
Rollup *rollup.Service
|
2019-01-23 19:58:44 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 02:39:21 +01:00
|
|
|
LiveAccounting struct {
|
|
|
|
Service live.Service
|
|
|
|
}
|
|
|
|
|
2019-03-02 15:22:20 +00:00
|
|
|
Mail struct {
|
|
|
|
Service *mailservice.Service
|
|
|
|
}
|
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
Console struct {
|
|
|
|
Listener net.Listener
|
|
|
|
Service *console.Service
|
|
|
|
Endpoint *consoleweb.Server
|
|
|
|
}
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new satellite
|
2019-04-03 20:13:39 +01:00
|
|
|
func New(log *zap.Logger, full *identity.FullIdentity, db DB, config *Config, versionInfo version.Info) (*Peer, error) {
|
2019-01-18 13:54:08 +00:00
|
|
|
peer := &Peer{
|
2019-02-11 11:17:32 +00:00
|
|
|
Log: log,
|
|
|
|
Identity: full,
|
|
|
|
DB: db,
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
2019-04-03 20:13:39 +01:00
|
|
|
{
|
|
|
|
test := version.Info{}
|
|
|
|
if test != versionInfo {
|
|
|
|
peer.Log.Sugar().Debugf("Binary Version: %s with CommitHash %s, built at %s as Release %v",
|
|
|
|
versionInfo.Version.String(), versionInfo.CommitHash, versionInfo.Timestamp.String(), versionInfo.Release)
|
|
|
|
}
|
2019-04-04 16:40:07 +01:00
|
|
|
peer.Version = version.NewService(config.Version, versionInfo, "Satellite")
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
{ // setup listener and server
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Starting listener and server")
|
2019-03-07 18:19:37 +00:00
|
|
|
sc := config.Server
|
|
|
|
options, err := tlsopts.NewOptions(peer.Identity, sc.Config)
|
2019-01-18 13:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
peer.Transport = transport.NewClient(options)
|
2019-01-18 13:54:08 +00:00
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
peer.Server, err = server.New(options, sc.Address, sc.PrivateAddress, grpcauth.NewAPIKeyInterceptor())
|
2019-01-18 13:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 09:25:13 +00:00
|
|
|
{ // setup overlay
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Starting overlay")
|
2019-02-08 09:25:13 +00:00
|
|
|
config := config.Overlay
|
|
|
|
|
2019-04-10 07:04:24 +01:00
|
|
|
peer.Overlay.Service = overlay.NewCache(peer.Log.Named("overlay"), peer.DB.OverlayCache(), config.Node)
|
2019-03-23 08:06:11 +00:00
|
|
|
peer.Transport = peer.Transport.WithObservers(peer.Overlay.Service)
|
2019-02-08 09:25:13 +00:00
|
|
|
|
|
|
|
peer.Overlay.Inspector = overlay.NewInspector(peer.Overlay.Service)
|
2019-03-07 18:19:37 +00:00
|
|
|
pb.RegisterOverlayInspectorServer(peer.Server.PrivateGRPC(), peer.Overlay.Inspector)
|
2019-02-08 09:25:13 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
{ // setup kademlia
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Setting up Kademlia")
|
2019-01-18 13:54:08 +00:00
|
|
|
config := config.Kademlia
|
|
|
|
// TODO: move this setup logic into kademlia package
|
|
|
|
if config.ExternalAddress == "" {
|
2019-03-07 18:19:37 +00:00
|
|
|
config.ExternalAddress = peer.Addr()
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 07:04:24 +01:00
|
|
|
pbVersion, err := versionInfo.Proto()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
2019-04-22 10:07:50 +01:00
|
|
|
self := &overlay.NodeDossier{
|
|
|
|
Node: pb.Node{
|
|
|
|
Id: peer.ID(),
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Address: config.ExternalAddress,
|
|
|
|
},
|
2019-01-18 13:54:08 +00:00
|
|
|
},
|
2019-04-22 10:07:50 +01:00
|
|
|
Type: pb.NodeType_SATELLITE,
|
|
|
|
Operator: pb.NodeOperator{
|
2019-01-18 13:54:08 +00:00
|
|
|
Wallet: config.Operator.Wallet,
|
|
|
|
},
|
2019-04-22 10:07:50 +01:00
|
|
|
Version: *pbVersion,
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup routing table
|
2019-01-23 19:58:44 +00:00
|
|
|
// TODO: clean this up, should be part of database
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Setting up routing table")
|
2019-01-18 13:54:08 +00:00
|
|
|
bucketIdentifier := peer.ID().String()[:5] // need a way to differentiate between nodes if running more than one simultaneously
|
|
|
|
dbpath := filepath.Join(config.DBPath, fmt.Sprintf("kademlia_%s.db", bucketIdentifier))
|
|
|
|
|
2019-01-23 19:58:44 +00:00
|
|
|
if err := os.MkdirAll(config.DBPath, 0777); err != nil && !os.IsExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
dbs, err := boltdb.NewShared(dbpath, kademlia.KademliaBucket, kademlia.NodeBucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
2019-01-23 19:58:44 +00:00
|
|
|
peer.Kademlia.kdb, peer.Kademlia.ndb = dbs[0], dbs[1]
|
2019-01-18 13:54:08 +00:00
|
|
|
|
2019-01-29 06:51:07 +00:00
|
|
|
peer.Kademlia.RoutingTable, err = kademlia.NewRoutingTable(peer.Log.Named("routing"), self, peer.Kademlia.kdb, peer.Kademlia.ndb, &config.RoutingTableConfig)
|
2019-01-18 13:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
2019-02-08 09:25:13 +00:00
|
|
|
|
|
|
|
peer.Transport = peer.Transport.WithObservers(peer.Kademlia.RoutingTable)
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
2019-04-22 10:07:50 +01:00
|
|
|
peer.Kademlia.Service, err = kademlia.NewService(peer.Log.Named("kademlia"), peer.Transport, peer.Kademlia.RoutingTable, config)
|
2019-01-18 13:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
2019-03-22 20:06:57 +00:00
|
|
|
peer.Kademlia.Endpoint = kademlia.NewEndpoint(peer.Log.Named("kademlia:endpoint"), peer.Kademlia.Service, peer.Kademlia.RoutingTable)
|
2019-03-07 18:19:37 +00:00
|
|
|
pb.RegisterNodesServer(peer.Server.GRPC(), peer.Kademlia.Endpoint)
|
2019-01-23 19:58:44 +00:00
|
|
|
|
|
|
|
peer.Kademlia.Inspector = kademlia.NewInspector(peer.Kademlia.Service, peer.Identity)
|
2019-03-07 18:19:37 +00:00
|
|
|
pb.RegisterKadInspectorServer(peer.Server.PrivateGRPC(), peer.Kademlia.Inspector)
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup discovery
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Setting up discovery")
|
2019-01-18 13:54:08 +00:00
|
|
|
config := config.Discovery
|
2019-03-25 22:25:09 +00:00
|
|
|
peer.Discovery.Service = discovery.New(peer.Log.Named("discovery"), peer.Overlay.Service, peer.Kademlia.Service, config)
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 02:39:21 +01:00
|
|
|
{ // setup live accounting
|
|
|
|
log.Debug("Setting up live accounting")
|
|
|
|
config := config.LiveAccounting
|
|
|
|
liveAccountingService, err := live.New(peer.Log.Named("live-accounting"), config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
peer.LiveAccounting.Service = liveAccountingService
|
|
|
|
}
|
|
|
|
|
2019-03-28 20:09:23 +00:00
|
|
|
{ // setup orders
|
|
|
|
log.Debug("Setting up orders")
|
|
|
|
satelliteSignee := signing.SigneeFromPeerIdentity(peer.Identity.PeerIdentity())
|
|
|
|
peer.Orders.Endpoint = orders.NewEndpoint(
|
|
|
|
peer.Log.Named("orders:endpoint"),
|
|
|
|
satelliteSignee,
|
|
|
|
peer.DB.Orders(),
|
|
|
|
peer.DB.CertDB(),
|
|
|
|
)
|
|
|
|
peer.Orders.Service = orders.NewService(
|
|
|
|
peer.Log.Named("orders:service"),
|
|
|
|
signing.SignerFromFullIdentity(peer.Identity),
|
|
|
|
peer.Overlay.Service,
|
|
|
|
peer.DB.CertDB(),
|
|
|
|
peer.DB.Orders(),
|
|
|
|
45*24*time.Hour, // TODO: make it configurable?
|
|
|
|
)
|
|
|
|
pb.RegisterOrdersServer(peer.Server.GRPC(), peer.Orders.Endpoint)
|
|
|
|
}
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
{ // setup metainfo
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Setting up metainfo")
|
2019-05-14 16:13:18 +01:00
|
|
|
db, err := metainfo.NewStore(peer.Log.Named("metainfo:store"), config.Metainfo.DatabaseURL)
|
2019-01-18 13:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
2019-04-19 14:30:55 +01:00
|
|
|
peer.Metainfo.Database = db // for logging: storelogger.New(peer.Log.Named("pdb"), db)
|
2019-04-25 09:46:32 +01:00
|
|
|
peer.Metainfo.Service = metainfo.NewService(peer.Log.Named("metainfo:service"), peer.Metainfo.Database)
|
2019-02-05 17:22:17 +00:00
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
peer.Metainfo.Endpoint2 = metainfo.NewEndpoint(
|
|
|
|
peer.Log.Named("metainfo:endpoint"),
|
|
|
|
peer.Metainfo.Service,
|
2019-03-28 20:09:23 +00:00
|
|
|
peer.Orders.Service,
|
2019-03-18 10:55:06 +00:00
|
|
|
peer.Overlay.Service,
|
|
|
|
peer.DB.Console().APIKeys(),
|
2019-05-10 20:05:42 +01:00
|
|
|
peer.DB.StoragenodeAccounting(),
|
|
|
|
peer.DB.ProjectAccounting(),
|
2019-05-10 02:39:21 +01:00
|
|
|
peer.LiveAccounting.Service,
|
2019-04-02 19:21:18 +01:00
|
|
|
config.Rollup.MaxAlphaUsage,
|
2019-03-23 08:06:11 +00:00
|
|
|
)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
pb.RegisterMetainfoServer(peer.Server.GRPC(), peer.Metainfo.Endpoint2)
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup agreements
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Setting up agreements")
|
2019-02-07 19:22:49 +00:00
|
|
|
bwServer := bwagreement.NewServer(peer.DB.BandwidthAgreement(), peer.DB.CertDB(), peer.Identity.Leaf.PublicKey, peer.Log.Named("agreements"), peer.Identity.ID)
|
2019-01-25 18:05:21 +00:00
|
|
|
peer.Agreements.Endpoint = bwServer
|
2019-03-07 18:19:37 +00:00
|
|
|
pb.RegisterBandwidthServer(peer.Server.GRPC(), peer.Agreements.Endpoint)
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup datarepair
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Setting up datarepair")
|
2019-01-18 13:54:08 +00:00
|
|
|
// TODO: simplify argument list somehow
|
|
|
|
peer.Repair.Checker = checker.NewChecker(
|
2019-01-19 18:58:53 +00:00
|
|
|
peer.Metainfo.Service,
|
2019-03-25 22:25:09 +00:00
|
|
|
peer.DB.RepairQueue(),
|
2019-03-23 08:06:11 +00:00
|
|
|
peer.Overlay.Service, peer.DB.Irreparable(),
|
2019-01-18 13:54:08 +00:00
|
|
|
0, peer.Log.Named("checker"),
|
|
|
|
config.Checker.Interval)
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
peer.Repair.Repairer = repairer.NewService(
|
|
|
|
peer.DB.RepairQueue(),
|
|
|
|
&config.Repairer,
|
|
|
|
config.Repairer.Interval,
|
|
|
|
config.Repairer.MaxRepair,
|
|
|
|
peer.Transport,
|
|
|
|
peer.Metainfo.Service,
|
2019-03-28 20:09:23 +00:00
|
|
|
peer.Orders.Service,
|
2019-03-18 10:55:06 +00:00
|
|
|
peer.Overlay.Service,
|
|
|
|
)
|
|
|
|
|
2019-03-15 20:21:52 +00:00
|
|
|
peer.Repair.Inspector = irreparable.NewInspector(peer.DB.Irreparable())
|
|
|
|
pb.RegisterIrreparableInspectorServer(peer.Server.PrivateGRPC(), peer.Repair.Inspector)
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup audit
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Setting up audits")
|
2019-01-23 19:58:44 +00:00
|
|
|
config := config.Audit
|
|
|
|
|
|
|
|
peer.Audit.Service, err = audit.NewService(peer.Log.Named("audit"),
|
2019-03-19 17:37:26 +00:00
|
|
|
config,
|
2019-03-28 20:09:23 +00:00
|
|
|
peer.Metainfo.Service,
|
|
|
|
peer.Orders.Service,
|
|
|
|
peer.Transport,
|
|
|
|
peer.Overlay.Service,
|
2019-01-23 19:58:44 +00:00
|
|
|
peer.Identity,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup accounting
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Setting up accounting")
|
2019-05-10 20:05:42 +01:00
|
|
|
peer.Accounting.Tally = tally.New(peer.Log.Named("tally"), peer.DB.StoragenodeAccounting(), peer.DB.ProjectAccounting(), peer.LiveAccounting.Service, peer.Metainfo.Service, peer.Overlay.Service, 0, config.Tally.Interval)
|
|
|
|
peer.Accounting.Rollup = rollup.New(peer.Log.Named("rollup"), peer.DB.StoragenodeAccounting(), config.Rollup.Interval, config.Rollup.DeleteTallies)
|
2019-01-23 19:58:44 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 15:55:58 +01:00
|
|
|
{ // setup inspector
|
|
|
|
log.Debug("Setting up inspector")
|
|
|
|
peer.Inspector.Endpoint = inspector.NewEndpoint(
|
|
|
|
peer.Log.Named("inspector"),
|
|
|
|
peer.Overlay.Service,
|
|
|
|
peer.Metainfo.Service,
|
|
|
|
)
|
|
|
|
|
|
|
|
pb.RegisterHealthInspectorServer(peer.Server.PrivateGRPC(), peer.Inspector.Endpoint)
|
|
|
|
}
|
|
|
|
|
2019-03-02 15:22:20 +00:00
|
|
|
{ // setup mailservice
|
|
|
|
log.Debug("Setting up mail service")
|
|
|
|
// TODO(yar): test multiple satellites using same OAUTH credentials
|
|
|
|
mailConfig := config.Mail
|
|
|
|
|
|
|
|
// validate from mail address
|
|
|
|
from, err := mail.ParseAddress(mailConfig.From)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate smtp server address
|
|
|
|
host, _, err := net.SplitHostPort(mailConfig.SMTPServerAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
var sender mailservice.Sender
|
|
|
|
switch mailConfig.AuthType {
|
|
|
|
case "oauth2":
|
|
|
|
creds := oauth2.Credentials{
|
|
|
|
ClientID: mailConfig.ClientID,
|
|
|
|
ClientSecret: mailConfig.ClientSecret,
|
|
|
|
TokenURI: mailConfig.TokenURI,
|
|
|
|
}
|
|
|
|
token, err := oauth2.RefreshToken(creds, mailConfig.RefreshToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
sender = &post.SMTPSender{
|
|
|
|
From: *from,
|
|
|
|
Auth: &oauth2.Auth{
|
|
|
|
UserEmail: from.Address,
|
|
|
|
Storage: oauth2.NewTokenStore(creds, *token),
|
|
|
|
},
|
|
|
|
ServerAddress: mailConfig.SMTPServerAddress,
|
|
|
|
}
|
|
|
|
case "plain":
|
|
|
|
sender = &post.SMTPSender{
|
|
|
|
From: *from,
|
2019-03-24 00:08:41 +00:00
|
|
|
Auth: smtp.PlainAuth("", mailConfig.Login, mailConfig.Password, host),
|
|
|
|
ServerAddress: mailConfig.SMTPServerAddress,
|
|
|
|
}
|
|
|
|
case "login":
|
|
|
|
sender = &post.SMTPSender{
|
|
|
|
From: *from,
|
|
|
|
Auth: post.LoginAuth{
|
|
|
|
Username: mailConfig.Login,
|
|
|
|
Password: mailConfig.Password,
|
|
|
|
},
|
2019-03-02 15:22:20 +00:00
|
|
|
ServerAddress: mailConfig.SMTPServerAddress,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
sender = &simulate.LinkClicker{}
|
|
|
|
}
|
|
|
|
|
|
|
|
peer.Mail.Service, err = mailservice.New(
|
2019-03-26 15:56:16 +00:00
|
|
|
peer.Log.Named("mail:service"),
|
2019-03-02 15:22:20 +00:00
|
|
|
sender,
|
|
|
|
mailConfig.TemplatePath,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
{ // setup console
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Setting up console")
|
2019-03-02 15:22:20 +00:00
|
|
|
consoleConfig := config.Console
|
2019-01-24 16:26:36 +00:00
|
|
|
|
2019-03-02 15:22:20 +00:00
|
|
|
peer.Console.Listener, err = net.Listen("tcp", consoleConfig.Address)
|
2019-01-24 16:26:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
2019-03-02 15:22:20 +00:00
|
|
|
peer.Console.Service, err = console.NewService(
|
|
|
|
peer.Log.Named("console:service"),
|
|
|
|
// TODO(yar): use satellite key
|
2019-01-24 16:26:36 +00:00
|
|
|
&consoleauth.Hmac{Secret: []byte("my-suppa-secret-key")},
|
2019-02-05 17:31:53 +00:00
|
|
|
peer.DB.Console(),
|
2019-03-02 15:22:20 +00:00
|
|
|
consoleConfig.PasswordCost,
|
2019-02-05 17:31:53 +00:00
|
|
|
)
|
2019-01-24 16:26:36 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
2019-03-02 15:22:20 +00:00
|
|
|
peer.Console.Endpoint = consoleweb.NewServer(
|
|
|
|
peer.Log.Named("console:endpoint"),
|
|
|
|
consoleConfig,
|
2019-01-24 16:26:36 +00:00
|
|
|
peer.Console.Service,
|
2019-03-02 15:22:20 +00:00
|
|
|
peer.Mail.Service,
|
|
|
|
peer.Console.Listener,
|
|
|
|
)
|
2019-01-24 16:26:36 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
return peer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs storage node until it's either closed or it errors.
|
|
|
|
func (peer *Peer) Run(ctx context.Context) error {
|
2019-02-06 13:19:14 +00:00
|
|
|
group, ctx := errgroup.WithContext(ctx)
|
2019-01-18 13:54:08 +00:00
|
|
|
|
|
|
|
group.Go(func() error {
|
2019-04-17 11:09:44 +01:00
|
|
|
return errs2.IgnoreCanceled(peer.Version.Run(ctx))
|
2019-04-03 20:13:39 +01:00
|
|
|
})
|
|
|
|
group.Go(func() error {
|
2019-04-17 11:09:44 +01:00
|
|
|
return errs2.IgnoreCanceled(peer.Kademlia.Service.Bootstrap(ctx))
|
2019-01-18 13:54:08 +00:00
|
|
|
})
|
|
|
|
group.Go(func() error {
|
2019-04-17 11:09:44 +01:00
|
|
|
return errs2.IgnoreCanceled(peer.Kademlia.Service.Run(ctx))
|
2019-01-18 13:54:08 +00:00
|
|
|
})
|
|
|
|
group.Go(func() error {
|
2019-04-17 11:09:44 +01:00
|
|
|
return errs2.IgnoreCanceled(peer.Discovery.Service.Run(ctx))
|
2019-01-18 13:54:08 +00:00
|
|
|
})
|
|
|
|
group.Go(func() error {
|
2019-04-17 11:09:44 +01:00
|
|
|
return errs2.IgnoreCanceled(peer.Repair.Checker.Run(ctx))
|
2019-01-18 13:54:08 +00:00
|
|
|
})
|
|
|
|
group.Go(func() error {
|
2019-04-17 11:09:44 +01:00
|
|
|
return errs2.IgnoreCanceled(peer.Repair.Repairer.Run(ctx))
|
2019-01-18 13:54:08 +00:00
|
|
|
})
|
2019-01-24 18:44:14 +00:00
|
|
|
group.Go(func() error {
|
2019-04-17 11:09:44 +01:00
|
|
|
return errs2.IgnoreCanceled(peer.Accounting.Tally.Run(ctx))
|
2019-01-24 18:44:14 +00:00
|
|
|
})
|
|
|
|
group.Go(func() error {
|
2019-04-17 11:09:44 +01:00
|
|
|
return errs2.IgnoreCanceled(peer.Accounting.Rollup.Run(ctx))
|
2019-01-24 18:44:14 +00:00
|
|
|
})
|
2019-01-24 19:52:16 +00:00
|
|
|
group.Go(func() error {
|
2019-04-17 11:09:44 +01:00
|
|
|
return errs2.IgnoreCanceled(peer.Audit.Service.Run(ctx))
|
2019-01-24 19:52:16 +00:00
|
|
|
})
|
2019-01-18 13:54:08 +00:00
|
|
|
group.Go(func() error {
|
2019-01-23 19:58:44 +00:00
|
|
|
// TODO: move the message into Server instead
|
2019-03-07 18:19:37 +00:00
|
|
|
// Don't change the format of this comment, it is used to figure out the node id.
|
|
|
|
peer.Log.Sugar().Infof("Node %s started", peer.Identity.ID)
|
2019-03-18 17:02:37 +00:00
|
|
|
peer.Log.Sugar().Infof("Public server started on %s", peer.Addr())
|
|
|
|
peer.Log.Sugar().Infof("Private server started on %s", peer.PrivateAddr())
|
2019-04-17 11:09:44 +01:00
|
|
|
return errs2.IgnoreCanceled(peer.Server.Run(ctx))
|
2019-01-18 13:54:08 +00:00
|
|
|
})
|
2019-01-24 16:26:36 +00:00
|
|
|
group.Go(func() error {
|
2019-04-17 11:09:44 +01:00
|
|
|
return errs2.IgnoreCanceled(peer.Console.Endpoint.Run(ctx))
|
2019-01-24 16:26:36 +00:00
|
|
|
})
|
2019-01-18 13:54:08 +00:00
|
|
|
|
|
|
|
return group.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes all the resources.
|
|
|
|
func (peer *Peer) Close() error {
|
|
|
|
var errlist errs.Group
|
|
|
|
|
|
|
|
// TODO: ensure that Close can be called on nil-s that way this code won't need the checks.
|
|
|
|
|
2019-01-28 19:04:42 +00:00
|
|
|
// close servers, to avoid new connections to closing subsystems
|
2019-03-07 18:19:37 +00:00
|
|
|
if peer.Server != nil {
|
|
|
|
errlist.Add(peer.Server.Close())
|
2019-01-28 19:04:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
if peer.Console.Endpoint != nil {
|
|
|
|
errlist.Add(peer.Console.Endpoint.Close())
|
|
|
|
} else {
|
2019-03-05 10:38:21 +00:00
|
|
|
if peer.Console.Listener != nil {
|
|
|
|
errlist.Add(peer.Console.Listener.Close())
|
2019-01-24 16:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 19:04:42 +00:00
|
|
|
// close services in reverse initialization order
|
2019-01-18 13:54:08 +00:00
|
|
|
if peer.Repair.Repairer != nil {
|
|
|
|
errlist.Add(peer.Repair.Repairer.Close())
|
|
|
|
}
|
|
|
|
if peer.Repair.Checker != nil {
|
|
|
|
errlist.Add(peer.Repair.Checker.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
if peer.Agreements.Endpoint != nil {
|
|
|
|
errlist.Add(peer.Agreements.Endpoint.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
if peer.Metainfo.Database != nil {
|
|
|
|
errlist.Add(peer.Metainfo.Database.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
if peer.Discovery.Service != nil {
|
|
|
|
errlist.Add(peer.Discovery.Service.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add kademlia.Endpoint for consistency
|
|
|
|
if peer.Kademlia.Service != nil {
|
|
|
|
errlist.Add(peer.Kademlia.Service.Close())
|
|
|
|
}
|
|
|
|
if peer.Kademlia.RoutingTable != nil {
|
2019-01-29 06:51:07 +00:00
|
|
|
errlist.Add(peer.Kademlia.RoutingTable.Close())
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 09:25:13 +00:00
|
|
|
if peer.Overlay.Service != nil {
|
|
|
|
errlist.Add(peer.Overlay.Service.Close())
|
|
|
|
}
|
|
|
|
|
2019-01-25 22:33:20 +00:00
|
|
|
if peer.Kademlia.ndb != nil || peer.Kademlia.kdb != nil {
|
|
|
|
errlist.Add(peer.Kademlia.kdb.Close())
|
|
|
|
errlist.Add(peer.Kademlia.ndb.Close())
|
|
|
|
}
|
2019-01-23 19:58:44 +00:00
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
return errlist.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the peer ID.
|
|
|
|
func (peer *Peer) ID() storj.NodeID { return peer.Identity.ID }
|
|
|
|
|
|
|
|
// Local returns the peer local node info.
|
2019-04-22 10:07:50 +01:00
|
|
|
func (peer *Peer) Local() overlay.NodeDossier { return peer.Kademlia.RoutingTable.Local() }
|
2019-01-18 13:54:08 +00:00
|
|
|
|
|
|
|
// Addr returns the public address.
|
2019-03-07 18:19:37 +00:00
|
|
|
func (peer *Peer) Addr() string { return peer.Server.Addr().String() }
|
|
|
|
|
|
|
|
// PrivateAddr returns the private address.
|
|
|
|
func (peer *Peer) PrivateAddr() string { return peer.Server.PrivateAddr().String() }
|