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"
|
2019-07-01 09:39:51 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-12-12 09:39:00 +00:00
|
|
|
"time"
|
2018-04-11 15:41:51 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-07-01 10:00:46 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
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-07-01 09:39:51 +00:00
|
|
|
|
2019-04-30 06:46:12 +00:00
|
|
|
"github.com/status-im/status-go/db"
|
2019-07-01 09:39:51 +00:00
|
|
|
"github.com/status-im/status-go/messaging/chat"
|
2019-07-03 19:13:11 +00:00
|
|
|
msgdb "github.com/status-im/status-go/messaging/db"
|
2019-06-26 18:17:41 +00:00
|
|
|
"github.com/status-im/status-go/messaging/filter"
|
2019-07-03 19:13:11 +00:00
|
|
|
"github.com/status-im/status-go/messaging/multidevice"
|
2019-06-26 18:17:41 +00:00
|
|
|
"github.com/status-im/status-go/messaging/publisher"
|
2019-07-03 19:13:11 +00:00
|
|
|
"github.com/status-im/status-go/messaging/sharedsecret"
|
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"
|
2019-07-01 09:39:51 +00:00
|
|
|
"github.com/status-im/status-go/signal"
|
|
|
|
|
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"
|
2019-07-01 09:39:51 +00:00
|
|
|
"golang.org/x/crypto/sha3"
|
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
|
2019-07-01 09:39:51 +00:00
|
|
|
// maxInstallations is a maximum number of supported devices for one account.
|
|
|
|
maxInstallations = 3
|
2019-07-01 10:00:46 +00:00
|
|
|
// filterCheckIntervalMs is a how often we should check whisper filters for new messages
|
|
|
|
filterCheckIntervalMs = 300
|
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-07-01 09:39:51 +00:00
|
|
|
*publisher.Publisher
|
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-07-01 09:39:51 +00:00
|
|
|
publisher := publisher.New(w, publisher.Config{PFSEnabled: config.PFSEnabled})
|
2018-04-11 15:41:51 +00:00
|
|
|
return &Service{
|
2019-07-01 09:39:51 +00:00
|
|
|
Publisher: publisher,
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-01 09:39:51 +00:00
|
|
|
func (s *Service) InitProtocolWithPassword(address string, password string) error {
|
|
|
|
digest := sha3.Sum256([]byte(password))
|
|
|
|
encKey := fmt.Sprintf("%x", digest)
|
|
|
|
return s.initProtocol(address, encKey, password)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitProtocolWithEncyptionKey creates an instance of ProtocolService given an address and encryption key.
|
|
|
|
func (s *Service) InitProtocolWithEncyptionKey(address string, encKey string) error {
|
|
|
|
return s.initProtocol(address, encKey, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) initProtocol(address, encKey, password string) error {
|
|
|
|
if !s.config.PFSEnabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dataDir := filepath.Clean(s.config.BackupDisabledDataDir)
|
|
|
|
|
|
|
|
if err := os.MkdirAll(dataDir, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v0Path := filepath.Join(dataDir, fmt.Sprintf("%x.db", address))
|
|
|
|
v1Path := filepath.Join(dataDir, fmt.Sprintf("%s.db", s.config.InstallationID))
|
|
|
|
v2Path := filepath.Join(dataDir, fmt.Sprintf("%s.v2.db", s.config.InstallationID))
|
|
|
|
v3Path := filepath.Join(dataDir, fmt.Sprintf("%s.v3.db", s.config.InstallationID))
|
|
|
|
v4Path := filepath.Join(dataDir, fmt.Sprintf("%s.v4.db", s.config.InstallationID))
|
|
|
|
|
|
|
|
if password != "" {
|
2019-07-03 19:13:11 +00:00
|
|
|
if err := msgdb.MigrateDBFile(v0Path, v1Path, "ON", password); err != nil {
|
2019-07-01 09:39:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-03 19:13:11 +00:00
|
|
|
if err := msgdb.MigrateDBFile(v1Path, v2Path, password, encKey); err != nil {
|
2019-07-01 09:39:51 +00:00
|
|
|
// Remove db file as created with a blank password and never used,
|
|
|
|
// and there's no need to rekey in this case
|
|
|
|
os.Remove(v1Path)
|
|
|
|
os.Remove(v2Path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 19:13:11 +00:00
|
|
|
if err := msgdb.MigrateDBKeyKdfIterations(v2Path, v3Path, encKey); err != nil {
|
2019-07-01 09:39:51 +00:00
|
|
|
os.Remove(v2Path)
|
|
|
|
os.Remove(v3Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix IOS not encrypting database
|
2019-07-03 19:13:11 +00:00
|
|
|
if err := msgdb.EncryptDatabase(v3Path, v4Path, encKey); err != nil {
|
2019-07-01 09:39:51 +00:00
|
|
|
os.Remove(v3Path)
|
|
|
|
os.Remove(v4Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Desktop was passing a network dependent directory, which meant that
|
|
|
|
// if running on testnet it would not access the right db. This copies
|
|
|
|
// the db from mainnet to the root location.
|
|
|
|
networkDependentPath := filepath.Join(dataDir, "ethereum", "mainnet_rpc", fmt.Sprintf("%s.v4.db", s.config.InstallationID))
|
|
|
|
if _, err := os.Stat(networkDependentPath); err == nil {
|
|
|
|
if err := os.Rename(networkDependentPath, v4Path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
persistence, err := chat.NewSQLLitePersistence(v4Path, encKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize sharedsecret
|
|
|
|
sharedSecretService := sharedsecret.NewService(persistence.GetSharedSecretStorage())
|
|
|
|
|
|
|
|
// Initialize filter
|
2019-07-01 10:00:46 +00:00
|
|
|
onNewMessagesHandler := func(messages []*filter.Messages) {
|
|
|
|
var signalMessages []*signal.Messages
|
|
|
|
handler := PublisherSignalHandler{}
|
|
|
|
for _, chatMessages := range messages {
|
|
|
|
signalMessage := &signal.Messages{
|
|
|
|
Error: chatMessages.Error,
|
|
|
|
Chat: chatMessages.Chat,
|
|
|
|
}
|
|
|
|
signalMessages = append(signalMessages, signalMessage)
|
|
|
|
dedupMessages, err := s.processReceivedMessages(chatMessages.Messages)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("could not process messages", "err", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
signalMessage.Messages = dedupMessages
|
|
|
|
}
|
|
|
|
handler.NewMessages(signalMessages)
|
|
|
|
}
|
|
|
|
|
|
|
|
filterService := filter.New(s.w, filter.NewSQLLitePersistence(persistence.DB), sharedSecretService, onNewMessagesHandler)
|
|
|
|
go filterService.Start(filterCheckIntervalMs * time.Millisecond)
|
2019-07-01 09:39:51 +00:00
|
|
|
|
|
|
|
// Initialize multidevice
|
|
|
|
multideviceConfig := &multidevice.Config{
|
|
|
|
InstallationID: s.config.InstallationID,
|
|
|
|
ProtocolVersion: chat.ProtocolVersion,
|
|
|
|
MaxInstallations: maxInstallations,
|
|
|
|
}
|
|
|
|
multideviceService := multidevice.New(multideviceConfig, persistence.GetMultideviceStorage())
|
|
|
|
|
|
|
|
addedBundlesHandler := func(addedBundles []*multidevice.Installation) {
|
|
|
|
handler := PublisherSignalHandler{}
|
|
|
|
for _, bundle := range addedBundles {
|
|
|
|
handler.BundleAdded(bundle.Identity, bundle.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protocolService := chat.NewProtocolService(
|
|
|
|
chat.NewEncryptionService(
|
|
|
|
persistence,
|
|
|
|
chat.DefaultEncryptionServiceConfig(s.config.InstallationID)),
|
|
|
|
sharedSecretService,
|
|
|
|
multideviceService,
|
|
|
|
addedBundlesHandler,
|
|
|
|
s.newSharedSecretHandler(filterService))
|
|
|
|
|
|
|
|
s.Publisher.Init(persistence.DB, protocolService, filterService)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-01 10:00:46 +00:00
|
|
|
func (s *Service) processReceivedMessages(messages []*whisper.Message) ([]dedup.DeduplicateMessage, error) {
|
|
|
|
dedupMessages := s.deduplicator.Deduplicate(messages)
|
|
|
|
|
|
|
|
// Attempt to decrypt message, otherwise leave unchanged
|
|
|
|
for _, dedupMessage := range dedupMessages {
|
|
|
|
err := s.ProcessMessage(dedupMessage.Message, dedupMessage.DedupID)
|
|
|
|
switch err {
|
|
|
|
case chat.ErrNotPairedDevice:
|
|
|
|
log.Info("Received a message from non-paired device", "err", err)
|
|
|
|
case chat.ErrDeviceNotFound:
|
|
|
|
log.Warn("Device not found, sending signal", "err", err)
|
|
|
|
|
|
|
|
publicKey, err := crypto.UnmarshalPubkey(dedupMessage.Message.Sig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to handler chat.ErrDeviceNotFound: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
keyString := fmt.Sprintf("%#x", crypto.FromECDSAPub(publicKey))
|
|
|
|
handler := PublisherSignalHandler{}
|
|
|
|
handler.DecryptMessageFailed(keyString)
|
|
|
|
default:
|
|
|
|
log.Error("Failed handling message with error", "err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dedupMessages, nil
|
|
|
|
}
|
|
|
|
|
2019-07-01 09:39:51 +00:00
|
|
|
func (s *Service) newSharedSecretHandler(filterService *filter.Service) func([]*sharedsecret.Secret) {
|
|
|
|
return func(sharedSecrets []*sharedsecret.Secret) {
|
|
|
|
var filters []*signal.Filter
|
|
|
|
for _, sharedSecret := range sharedSecrets {
|
|
|
|
chat, err := filterService.ProcessNegotiatedSecret(sharedSecret)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to process negotiated secret", "err", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
filter := &signal.Filter{
|
|
|
|
ChatID: chat.ChatID,
|
|
|
|
SymKeyID: chat.SymKeyID,
|
|
|
|
Listen: chat.Listen,
|
|
|
|
FilterID: chat.FilterID,
|
|
|
|
Identity: chat.Identity,
|
|
|
|
Topic: chat.Topic,
|
|
|
|
}
|
|
|
|
|
|
|
|
filters = append(filters, filter)
|
|
|
|
}
|
|
|
|
if len(filters) != 0 {
|
|
|
|
handler := PublisherSignalHandler{}
|
|
|
|
handler.WhisperFilterAdded(filters)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-01 09:39:51 +00:00
|
|
|
return s.Publisher.Start(s.online, true)
|
2019-06-03 14:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-07-01 09:39:51 +00:00
|
|
|
return s.Publisher.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|