2018-04-11 15:41:51 +00:00
|
|
|
package shhext
|
|
|
|
|
|
|
|
import (
|
2018-04-26 05:56:19 +00:00
|
|
|
"crypto/ecdsa"
|
2018-09-24 18:07:34 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"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-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-01-17 12:56:22 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2018-09-24 18:07:34 +00:00
|
|
|
"github.com/status-im/status-go/services/shhext/chat"
|
2019-05-17 11:06:56 +00:00
|
|
|
appDB "github.com/status-im/status-go/services/shhext/chat/db"
|
|
|
|
"github.com/status-im/status-go/services/shhext/chat/topic"
|
2018-04-20 11:26:54 +00:00
|
|
|
"github.com/status-im/status-go/services/shhext/dedup"
|
2019-05-17 11:06:56 +00:00
|
|
|
"github.com/status-im/status-go/services/shhext/filter"
|
2018-12-05 13:57:05 +00:00
|
|
|
"github.com/status-im/status-go/services/shhext/mailservers"
|
2019-05-17 11:06:56 +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-01-16 14:42:00 +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
|
2018-04-11 15:41:51 +00:00
|
|
|
)
|
|
|
|
|
2019-06-12 09:16:00 +00:00
|
|
|
var errProtocolNotInitialized = errors.New("protocol is not initialized")
|
2018-12-05 13:57:05 +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-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
|
|
|
|
protocol *chat.ProtocolService
|
|
|
|
dataDir string
|
|
|
|
installationID string
|
|
|
|
pfsEnabled bool
|
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)
|
|
|
|
|
2018-09-24 18:07:34 +00:00
|
|
|
// New returns a new Service. dataDir is a folder path to a network-independent location
|
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)
|
2018-04-11 15:41:51 +00:00
|
|
|
return &Service{
|
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
|
|
|
dataDir: config.BackupDisabledDataDir,
|
|
|
|
installationID: config.InstallationID,
|
|
|
|
pfsEnabled: config.PFSEnabled,
|
|
|
|
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{}
|
|
|
|
}
|
|
|
|
|
2019-01-25 10:31:51 +00:00
|
|
|
// InitProtocolWithPassword creates an instance of ProtocolService given an address and password used to generate an encryption key.
|
|
|
|
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 {
|
2018-09-24 18:07:34 +00:00
|
|
|
if !s.pfsEnabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(filepath.Clean(s.dataDir), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-10 13:11:19 +00:00
|
|
|
v0Path := filepath.Join(s.dataDir, fmt.Sprintf("%x.db", address))
|
|
|
|
v1Path := filepath.Join(s.dataDir, fmt.Sprintf("%s.db", s.installationID))
|
|
|
|
v2Path := filepath.Join(s.dataDir, fmt.Sprintf("%s.v2.db", s.installationID))
|
2019-01-11 10:12:23 +00:00
|
|
|
v3Path := filepath.Join(s.dataDir, fmt.Sprintf("%s.v3.db", s.installationID))
|
2019-01-11 10:55:18 +00:00
|
|
|
v4Path := filepath.Join(s.dataDir, fmt.Sprintf("%s.v4.db", s.installationID))
|
2018-11-28 11:34:39 +00:00
|
|
|
|
2019-01-25 10:31:51 +00:00
|
|
|
if password != "" {
|
2019-05-17 11:06:56 +00:00
|
|
|
if err := appDB.MigrateDBFile(v0Path, v1Path, "ON", password); err != nil {
|
2019-01-25 10:31:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-17 11:06:56 +00:00
|
|
|
if err := appDB.MigrateDBFile(v1Path, v2Path, password, encKey); err != nil {
|
2019-01-25 10:31: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)
|
|
|
|
}
|
2018-12-10 13:11:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 11:06:56 +00:00
|
|
|
if err := appDB.MigrateDBKeyKdfIterations(v2Path, v3Path, encKey); err != nil {
|
2019-01-11 10:12:23 +00:00
|
|
|
os.Remove(v2Path)
|
|
|
|
os.Remove(v3Path)
|
|
|
|
}
|
|
|
|
|
2019-01-11 10:55:18 +00:00
|
|
|
// Fix IOS not encrypting database
|
2019-05-17 11:06:56 +00:00
|
|
|
if err := appDB.EncryptDatabase(v3Path, v4Path, encKey); err != nil {
|
2019-01-11 10:55:18 +00:00
|
|
|
os.Remove(v3Path)
|
|
|
|
os.Remove(v4Path)
|
|
|
|
}
|
|
|
|
|
2019-02-25 07:51:25 +00:00
|
|
|
// 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(s.dataDir, "ethereum", "mainnet_rpc", fmt.Sprintf("%s.v4.db", s.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
|
|
|
|
}
|
|
|
|
|
2019-01-25 10:31:51 +00:00
|
|
|
persistence, err := chat.NewSQLLitePersistence(v4Path, encKey)
|
2018-09-24 18:07:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-05 08:22:49 +00:00
|
|
|
|
|
|
|
addedBundlesHandler := func(addedBundles []chat.IdentityAndIDPair) {
|
|
|
|
handler := EnvelopeSignalHandler{}
|
|
|
|
for _, bundle := range addedBundles {
|
|
|
|
handler.BundleAdded(bundle[0], bundle[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 11:06:56 +00:00
|
|
|
// Initialize topics
|
|
|
|
topicService := topic.NewService(persistence.GetTopicStorage())
|
|
|
|
filterService := filter.New(s.config.AsymKeyID, s.w, topicService)
|
|
|
|
s.filter = filterService
|
|
|
|
|
|
|
|
s.protocol = chat.NewProtocolService(
|
|
|
|
chat.NewEncryptionService(
|
|
|
|
persistence,
|
|
|
|
chat.DefaultEncryptionServiceConfig(s.installationID)),
|
|
|
|
topicService,
|
|
|
|
addedBundlesHandler,
|
|
|
|
s.onNewTopicHandler)
|
2018-09-24 18:07:34 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-16 10:31:05 +00:00
|
|
|
func (s *Service) ProcessPublicBundle(myIdentityKey *ecdsa.PrivateKey, bundle *chat.Bundle) ([]chat.IdentityAndIDPair, error) {
|
2018-09-24 18:07:34 +00:00
|
|
|
if s.protocol == nil {
|
2018-10-16 10:31:05 +00:00
|
|
|
return nil, errProtocolNotInitialized
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.protocol.ProcessPublicBundle(myIdentityKey, bundle)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) GetBundle(myIdentityKey *ecdsa.PrivateKey) (*chat.Bundle, error) {
|
|
|
|
if s.protocol == nil {
|
|
|
|
return nil, errProtocolNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.protocol.GetBundle(myIdentityKey)
|
|
|
|
}
|
|
|
|
|
2018-11-06 08:05:32 +00:00
|
|
|
// EnableInstallation enables an installation for multi-device sync.
|
|
|
|
func (s *Service) EnableInstallation(myIdentityKey *ecdsa.PublicKey, installationID string) error {
|
|
|
|
if s.protocol == nil {
|
|
|
|
return errProtocolNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.protocol.EnableInstallation(myIdentityKey, installationID)
|
|
|
|
}
|
|
|
|
|
2019-02-12 11:07:13 +00:00
|
|
|
func (s *Service) GetPublicBundle(identityKey *ecdsa.PublicKey) (*chat.Bundle, error) {
|
|
|
|
if s.protocol == nil {
|
|
|
|
return nil, errProtocolNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.protocol.GetPublicBundle(identityKey)
|
|
|
|
}
|
|
|
|
|
2018-11-06 08:05:32 +00:00
|
|
|
// DisableInstallation disables an installation for multi-device sync.
|
|
|
|
func (s *Service) DisableInstallation(myIdentityKey *ecdsa.PublicKey, installationID string) error {
|
|
|
|
if s.protocol == nil {
|
|
|
|
return errProtocolNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.protocol.DisableInstallation(myIdentityKey, installationID)
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:41:51 +00:00
|
|
|
// 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
|
2018-04-11 15:41:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop is run when a service is stopped.
|
|
|
|
// It does nothing in this case but is required by `node.Service` interface.
|
|
|
|
func (s *Service) Stop() error {
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:41:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-05-17 11:06:56 +00:00
|
|
|
|
|
|
|
func (s *Service) GetNegotiatedChat(identity *ecdsa.PublicKey) *filter.Chat {
|
|
|
|
return s.filter.Get(identity)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) LoadFilters(chats []*filter.Chat) error {
|
|
|
|
return s.filter.Init(chats)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) RemoveFilter(chat *filter.Chat) {
|
|
|
|
// remove filter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) onNewTopicHandler(sharedSecrets []*topic.Secret) {
|
|
|
|
var filters []*signal.Filter
|
|
|
|
log.Info("NEW TOPIC HANDLER", "secrets", sharedSecrets)
|
|
|
|
for _, sharedSecret := range sharedSecrets {
|
|
|
|
err := s.filter.ProcessNegotiatedSecret(sharedSecret)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to process negotiated secret", "err", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// TODO: send back chat filter
|
|
|
|
log.Info("FILTER IDS", "filter", filters)
|
|
|
|
if len(filters) != 0 {
|
|
|
|
log.Info("SENDING FILTERS")
|
|
|
|
handler := EnvelopeSignalHandler{}
|
|
|
|
handler.WhisperFilterAdded(filters)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|