2018-04-11 15:41:51 +00:00
|
|
|
package shhext
|
|
|
|
|
|
|
|
import (
|
2019-06-26 16:17:41 +00:00
|
|
|
"context"
|
2018-04-26 05:56:19 +00:00
|
|
|
"crypto/ecdsa"
|
2019-06-26 16:17:41 +00:00
|
|
|
"fmt"
|
2018-12-12 09:39:00 +00:00
|
|
|
"time"
|
2018-04-11 15:41:51 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-05-17 11:06:56 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-04-11 15:41:51 +00:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2018-11-21 10:22:30 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2018-04-11 15:41:51 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2019-04-30 06:46:12 +00:00
|
|
|
"github.com/status-im/status-go/db"
|
2019-06-26 18:17:41 +00:00
|
|
|
"github.com/status-im/status-go/messaging/filter"
|
|
|
|
"github.com/status-im/status-go/messaging/publisher"
|
2019-01-17 12:56:22 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2018-04-20 11:26:54 +00:00
|
|
|
"github.com/status-im/status-go/services/shhext/dedup"
|
2018-12-05 13:57:05 +00:00
|
|
|
"github.com/status-im/status-go/services/shhext/mailservers"
|
2018-09-25 07:05:38 +00:00
|
|
|
whisper "github.com/status-im/whisper/whisperv6"
|
2018-05-02 12:14:08 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
2018-04-11 15:41:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-12-05 13:57:05 +00:00
|
|
|
// defaultConnectionsTarget used in Service.Start if configured connection target is 0.
|
|
|
|
defaultConnectionsTarget = 1
|
2018-12-12 09:39:00 +00:00
|
|
|
// defaultTimeoutWaitAdded is a timeout to use to establish initial connections.
|
|
|
|
defaultTimeoutWaitAdded = 5 * time.Second
|
2018-04-11 15:41:51 +00:00
|
|
|
)
|
|
|
|
|
2018-04-13 05:52:22 +00:00
|
|
|
// EnvelopeEventsHandler used for two different event types.
|
|
|
|
type EnvelopeEventsHandler interface {
|
|
|
|
EnvelopeSent(common.Hash)
|
2019-04-02 10:40:45 +00:00
|
|
|
EnvelopeExpired(common.Hash, error)
|
2018-10-18 10:25:00 +00:00
|
|
|
MailServerRequestCompleted(common.Hash, common.Hash, []byte, error)
|
2018-06-15 15:12:31 +00:00
|
|
|
MailServerRequestExpired(common.Hash)
|
2018-04-13 05:52:22 +00:00
|
|
|
}
|
2018-04-11 15:41:51 +00:00
|
|
|
|
|
|
|
// Service is a service that provides some additional Whisper API.
|
|
|
|
type Service struct {
|
2019-05-23 08:47:20 +00:00
|
|
|
*publisher.Service
|
2019-05-06 06:33:19 +00:00
|
|
|
storage db.TransactionalStorage
|
2019-01-15 09:21:33 +00:00
|
|
|
w *whisper.Whisper
|
|
|
|
config params.ShhextConfig
|
2019-02-20 06:57:57 +00:00
|
|
|
envelopesMonitor *EnvelopesMonitor
|
|
|
|
mailMonitor *MailRequestMonitor
|
2019-01-15 09:21:33 +00:00
|
|
|
requestsRegistry *RequestsRegistry
|
2019-04-30 06:46:12 +00:00
|
|
|
historyUpdates *HistoryUpdateReactor
|
2019-01-15 09:21:33 +00:00
|
|
|
server *p2p.Server
|
|
|
|
nodeID *ecdsa.PrivateKey
|
|
|
|
deduplicator *dedup.Deduplicator
|
2019-04-30 06:46:12 +00:00
|
|
|
peerStore *mailservers.PeerStore
|
|
|
|
cache *mailservers.Cache
|
|
|
|
connManager *mailservers.ConnectionManager
|
|
|
|
lastUsedMonitor *mailservers.LastUsedConnectionMonitor
|
2019-05-17 11:06:56 +00:00
|
|
|
filter *filter.Service
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-11 15:41:51 +00:00
|
|
|
// Make sure that Service implements node.Service interface.
|
|
|
|
var _ node.Service = (*Service)(nil)
|
|
|
|
|
2019-05-23 08:47:20 +00:00
|
|
|
// New returns a new Service.
|
2019-04-30 06:46:12 +00:00
|
|
|
func New(w *whisper.Whisper, handler EnvelopeEventsHandler, ldb *leveldb.DB, config params.ShhextConfig) *Service {
|
|
|
|
cache := mailservers.NewCache(ldb)
|
2018-12-12 09:39:00 +00:00
|
|
|
ps := mailservers.NewPeerStore(cache)
|
2019-01-15 09:21:33 +00:00
|
|
|
delay := defaultRequestsDelay
|
|
|
|
if config.RequestsDelay != 0 {
|
|
|
|
delay = config.RequestsDelay
|
|
|
|
}
|
|
|
|
requestsRegistry := NewRequestsRegistry(delay)
|
2019-05-06 06:33:19 +00:00
|
|
|
historyUpdates := NewHistoryUpdateReactor()
|
2019-02-20 06:57:57 +00:00
|
|
|
mailMonitor := &MailRequestMonitor{
|
|
|
|
w: w,
|
|
|
|
handler: handler,
|
|
|
|
cache: map[common.Hash]EnvelopeState{},
|
|
|
|
requestsRegistry: requestsRegistry,
|
|
|
|
}
|
2019-03-01 13:36:21 +00:00
|
|
|
envelopesMonitor := NewEnvelopesMonitor(w, handler, config.MailServerConfirmations, ps, config.MaxMessageDeliveryAttempts)
|
2019-05-23 08:47:20 +00:00
|
|
|
publisherConfig := &publisher.Config{
|
|
|
|
PfsEnabled: config.PFSEnabled,
|
|
|
|
DataDir: config.BackupDisabledDataDir,
|
|
|
|
InstallationID: config.InstallationID,
|
|
|
|
}
|
|
|
|
publisherService := publisher.New(publisherConfig, w)
|
2018-04-11 15:41:51 +00:00
|
|
|
return &Service{
|
2019-05-23 08:47:20 +00:00
|
|
|
Service: publisherService,
|
2019-05-06 06:33:19 +00:00
|
|
|
storage: db.NewLevelDBStorage(ldb),
|
2019-01-15 09:21:33 +00:00
|
|
|
w: w,
|
|
|
|
config: config,
|
2019-02-20 06:57:57 +00:00
|
|
|
envelopesMonitor: envelopesMonitor,
|
|
|
|
mailMonitor: mailMonitor,
|
2019-01-15 09:21:33 +00:00
|
|
|
requestsRegistry: requestsRegistry,
|
2019-04-30 06:46:12 +00:00
|
|
|
historyUpdates: historyUpdates,
|
|
|
|
deduplicator: dedup.NewDeduplicator(w, ldb),
|
2019-01-15 09:21:33 +00:00
|
|
|
peerStore: ps,
|
|
|
|
cache: cache,
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 10:22:30 +00:00
|
|
|
// UpdateMailservers updates information about selected mail servers.
|
2018-12-12 09:39:00 +00:00
|
|
|
func (s *Service) UpdateMailservers(nodes []*enode.Node) error {
|
|
|
|
if err := s.peerStore.Update(nodes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-05 13:57:05 +00:00
|
|
|
if s.connManager != nil {
|
|
|
|
s.connManager.Notify(nodes)
|
|
|
|
}
|
2018-12-12 09:39:00 +00:00
|
|
|
return nil
|
2018-11-21 10:22:30 +00:00
|
|
|
}
|
|
|
|
|
2018-04-11 15:41:51 +00:00
|
|
|
// Protocols returns a new protocols list. In this case, there are none.
|
|
|
|
func (s *Service) Protocols() []p2p.Protocol {
|
|
|
|
return []p2p.Protocol{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// APIs returns a list of new APIs.
|
|
|
|
func (s *Service) APIs() []rpc.API {
|
2018-06-25 13:27:17 +00:00
|
|
|
apis := []rpc.API{
|
2018-04-11 15:41:51 +00:00
|
|
|
{
|
|
|
|
Namespace: "shhext",
|
|
|
|
Version: "1.0",
|
2018-04-26 05:56:19 +00:00
|
|
|
Service: NewPublicAPI(s),
|
2018-04-11 15:41:51 +00:00
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
}
|
2018-06-25 13:27:17 +00:00
|
|
|
return apis
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start is run when a service is started.
|
|
|
|
// It does nothing in this case but is required by `node.Service` interface.
|
|
|
|
func (s *Service) Start(server *p2p.Server) error {
|
2018-12-05 13:57:05 +00:00
|
|
|
if s.config.EnableConnectionManager {
|
|
|
|
connectionsTarget := s.config.ConnectionTarget
|
|
|
|
if connectionsTarget == 0 {
|
|
|
|
connectionsTarget = defaultConnectionsTarget
|
|
|
|
}
|
2019-01-21 14:00:10 +00:00
|
|
|
maxFailures := s.config.MaxServerFailures
|
|
|
|
// if not defined change server on first expired event
|
|
|
|
if maxFailures == 0 {
|
|
|
|
maxFailures = 1
|
|
|
|
}
|
|
|
|
s.connManager = mailservers.NewConnectionManager(server, s.w, connectionsTarget, maxFailures, defaultTimeoutWaitAdded)
|
2018-12-05 13:57:05 +00:00
|
|
|
s.connManager.Start()
|
2018-12-12 09:39:00 +00:00
|
|
|
if err := mailservers.EnsureUsedRecordsAddedFirst(s.peerStore, s.connManager); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if s.config.EnableLastUsedMonitor {
|
|
|
|
s.lastUsedMonitor = mailservers.NewLastUsedConnectionMonitor(s.peerStore, s.cache, s.w)
|
|
|
|
s.lastUsedMonitor.Start()
|
2018-12-05 13:57:05 +00:00
|
|
|
}
|
2019-02-20 06:57:57 +00:00
|
|
|
s.envelopesMonitor.Start()
|
|
|
|
s.mailMonitor.Start()
|
2018-04-26 05:56:19 +00:00
|
|
|
s.nodeID = server.PrivateKey
|
2018-12-05 13:57:05 +00:00
|
|
|
s.server = server
|
2019-06-03 14:29:14 +00:00
|
|
|
return s.Service.Start(s.online, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) online() bool {
|
|
|
|
return s.server.PeerCount() != 0
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop is run when a service is stopped.
|
|
|
|
func (s *Service) Stop() error {
|
2019-06-03 14:29:14 +00:00
|
|
|
log.Info("Stopping shhext service")
|
2018-12-05 13:57:05 +00:00
|
|
|
if s.config.EnableConnectionManager {
|
|
|
|
s.connManager.Stop()
|
|
|
|
}
|
2018-12-12 09:39:00 +00:00
|
|
|
if s.config.EnableLastUsedMonitor {
|
|
|
|
s.lastUsedMonitor.Stop()
|
|
|
|
}
|
2019-02-26 12:55:01 +00:00
|
|
|
s.requestsRegistry.Clear()
|
2019-02-20 06:57:57 +00:00
|
|
|
s.envelopesMonitor.Stop()
|
|
|
|
s.mailMonitor.Stop()
|
2019-05-23 07:54:28 +00:00
|
|
|
if s.filter != nil {
|
|
|
|
if err := s.filter.Stop(); err != nil {
|
|
|
|
log.Error("Failed to stop filter service with error", "err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 08:47:20 +00:00
|
|
|
return s.Service.Stop()
|
2019-05-17 11:06:56 +00:00
|
|
|
}
|
2019-06-26 16:17:41 +00:00
|
|
|
|
|
|
|
func (s *Service) syncMessages(ctx context.Context, mailServerID []byte, r whisper.SyncMailRequest) (resp whisper.SyncEventResponse, err error) {
|
|
|
|
err = s.w.SyncMessages(mailServerID, r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the response which is received asynchronously as a p2p packet.
|
|
|
|
// This packet handler will send an event which contains the response payload.
|
|
|
|
events := make(chan whisper.EnvelopeEvent, 1024)
|
|
|
|
sub := s.w.SubscribeEnvelopeEvents(events)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
// Add explicit timeout context, otherwise the request
|
|
|
|
// can hang indefinitely if not specified by the sender.
|
|
|
|
// Sender is usually through netcat or some bash tool
|
|
|
|
// so it's not really possible to specify the timeout.
|
|
|
|
timeoutCtx, cancel := context.WithTimeout(ctx, time.Second*30)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-events:
|
|
|
|
if event.Event != whisper.EventMailServerSyncFinished {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("received EventMailServerSyncFinished event", "data", event.Data)
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
resp, ok = event.Data.(whisper.SyncEventResponse)
|
|
|
|
if !ok {
|
|
|
|
err = fmt.Errorf("did not understand the response event data")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
case <-timeoutCtx.Done():
|
|
|
|
err = timeoutCtx.Err()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|