2018-05-11 19:43:07 +00:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package mailserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2018-05-21 11:30:37 +00:00
|
|
|
"errors"
|
2018-05-11 19:43:07 +00:00
|
|
|
"fmt"
|
2018-05-21 11:30:37 +00:00
|
|
|
|
2018-05-17 11:21:04 +00:00
|
|
|
"time"
|
2018-05-11 19:43:07 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-05-30 12:31:47 +00:00
|
|
|
"github.com/ethereum/go-ethereum/metrics"
|
2018-05-11 19:43:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2018-05-11 19:43:07 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/util"
|
|
|
|
)
|
|
|
|
|
2018-05-17 11:21:04 +00:00
|
|
|
const (
|
|
|
|
maxQueryRange = 24 * time.Hour
|
|
|
|
)
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
var (
|
|
|
|
errDirectoryNotProvided = errors.New("data directory not provided")
|
|
|
|
errPasswordNotProvided = errors.New("password is not specified")
|
2018-05-30 12:31:47 +00:00
|
|
|
// By default go-ethereum/metrics creates dummy metrics that don't register anything.
|
|
|
|
// Real metrics are collected only if -metrics flag is set
|
|
|
|
requestProcessTimer = metrics.NewRegisteredTimer("mailserver/processTime", nil)
|
|
|
|
requestsCounter = metrics.NewRegisteredCounter("mailserver/requests", nil)
|
|
|
|
requestErrorsCounter = metrics.NewRegisteredCounter("mailserver/requestErrors", nil)
|
|
|
|
sentEnvelopesMeter = metrics.NewRegisteredMeter("mailserver/envelopes", nil)
|
|
|
|
sentEnvelopesSizeMeter = metrics.NewRegisteredMeter("mailserver/envelopesSize", nil)
|
2018-05-21 11:30:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// WMailServer whisper mailserver.
|
2018-05-11 19:43:07 +00:00
|
|
|
type WMailServer struct {
|
2018-05-17 11:21:04 +00:00
|
|
|
db *leveldb.DB
|
|
|
|
w *whisper.Whisper
|
|
|
|
pow float64
|
|
|
|
key []byte
|
|
|
|
limit *limiter
|
|
|
|
tick *ticker
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// DBKey key to be stored on db.
|
2018-05-11 19:43:07 +00:00
|
|
|
type DBKey struct {
|
|
|
|
timestamp uint32
|
|
|
|
hash common.Hash
|
|
|
|
raw []byte
|
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// NewDbKey creates a new DBKey with the given values.
|
2018-05-11 19:43:07 +00:00
|
|
|
func NewDbKey(t uint32, h common.Hash) *DBKey {
|
|
|
|
const sz = common.HashLength + 4
|
|
|
|
var k DBKey
|
|
|
|
k.timestamp = t
|
|
|
|
k.hash = h
|
|
|
|
k.raw = make([]byte, sz)
|
|
|
|
binary.BigEndian.PutUint32(k.raw, k.timestamp)
|
|
|
|
copy(k.raw[4:], k.hash[:])
|
|
|
|
return &k
|
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// Init initializes mailServer.
|
2018-05-17 11:21:04 +00:00
|
|
|
func (s *WMailServer) Init(shh *whisper.Whisper, config *params.WhisperConfig) error {
|
2018-05-11 19:43:07 +00:00
|
|
|
var err error
|
2018-05-17 11:21:04 +00:00
|
|
|
|
|
|
|
if len(config.DataDir) == 0 {
|
2018-05-21 11:30:37 +00:00
|
|
|
return errDirectoryNotProvided
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 11:21:04 +00:00
|
|
|
if len(config.Password) == 0 {
|
2018-05-21 11:30:37 +00:00
|
|
|
return errPasswordNotProvided
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 14:47:50 +00:00
|
|
|
s.db, err = leveldb.OpenFile(config.DataDir, nil)
|
2018-05-11 19:43:07 +00:00
|
|
|
if err != nil {
|
2018-05-17 11:21:04 +00:00
|
|
|
return fmt.Errorf("open DB: %s", err)
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.w = shh
|
2018-05-17 11:21:04 +00:00
|
|
|
s.pow = config.MinimumPoW
|
2018-05-11 19:43:07 +00:00
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
if err := s.setupWhisperIdentity(config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.setupLimiter(time.Duration(config.MailServerRateLimit) * time.Second)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setupLimiter in case limit is bigger than 0 it will setup an automated
|
|
|
|
// limit db cleanup.
|
2018-05-22 15:51:05 +00:00
|
|
|
func (s *WMailServer) setupLimiter(limit time.Duration) {
|
2018-05-21 11:30:37 +00:00
|
|
|
if limit > 0 {
|
|
|
|
s.limit = newLimiter(limit)
|
|
|
|
s.setupMailServerCleanup(limit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setupWhisperIdentity setup the whisper identity (symkey) for the current mail
|
|
|
|
// server.
|
|
|
|
func (s *WMailServer) setupWhisperIdentity(config *params.WhisperConfig) error {
|
2018-05-17 11:21:04 +00:00
|
|
|
MailServerKeyID, err := s.w.AddSymKeyFromPassword(config.Password)
|
2018-05-11 19:43:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create symmetric key: %s", err)
|
|
|
|
}
|
2018-05-21 11:30:37 +00:00
|
|
|
|
2018-05-11 19:43:07 +00:00
|
|
|
s.key, err = s.w.GetSymKey(MailServerKeyID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("save symmetric key: %s", err)
|
|
|
|
}
|
2018-05-17 11:21:04 +00:00
|
|
|
|
2018-05-11 19:43:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// setupMailServerCleanup periodically runs an expired entries deleteion for
|
|
|
|
// stored limits.
|
2018-05-17 11:21:04 +00:00
|
|
|
func (s *WMailServer) setupMailServerCleanup(period time.Duration) {
|
|
|
|
if s.tick == nil {
|
|
|
|
s.tick = &ticker{}
|
|
|
|
}
|
|
|
|
go s.tick.run(period, s.limit.deleteExpired)
|
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// Close the mailserver and its associated db connection.
|
2018-05-11 19:43:07 +00:00
|
|
|
func (s *WMailServer) Close() {
|
|
|
|
if s.db != nil {
|
2018-05-17 11:21:04 +00:00
|
|
|
if err := s.db.Close(); err != nil {
|
|
|
|
log.Error(fmt.Sprintf("s.db.Close failed: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if s.tick != nil {
|
|
|
|
s.tick.stop()
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// Archive a whisper envelope.
|
2018-05-11 19:43:07 +00:00
|
|
|
func (s *WMailServer) Archive(env *whisper.Envelope) {
|
|
|
|
key := NewDbKey(env.Expiry-env.TTL, env.Hash())
|
|
|
|
rawEnvelope, err := rlp.EncodeToBytes(env)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Sprintf("rlp.EncodeToBytes failed: %s", err))
|
|
|
|
} else {
|
2018-05-21 11:30:37 +00:00
|
|
|
if err = s.db.Put(key.raw, rawEnvelope, nil); err != nil {
|
2018-05-11 19:43:07 +00:00
|
|
|
log.Error(fmt.Sprintf("Writing to DB failed: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// DeliverMail sends mail to specified whisper peer.
|
2018-05-11 19:43:07 +00:00
|
|
|
func (s *WMailServer) DeliverMail(peer *whisper.Peer, request *whisper.Envelope) {
|
2018-05-30 12:31:47 +00:00
|
|
|
requestsCounter.Inc(1)
|
2018-05-11 19:43:07 +00:00
|
|
|
if peer == nil {
|
2018-05-30 12:31:47 +00:00
|
|
|
requestErrorsCounter.Inc(1)
|
2018-05-11 19:43:07 +00:00
|
|
|
log.Error("Whisper peer is nil")
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 15:51:05 +00:00
|
|
|
if s.exceedsPeerRequests(peer.ID()) {
|
2018-05-30 12:31:47 +00:00
|
|
|
requestErrorsCounter.Inc(1)
|
2018-05-22 15:51:05 +00:00
|
|
|
return
|
|
|
|
}
|
2018-05-21 11:30:37 +00:00
|
|
|
|
|
|
|
if ok, lower, upper, bloom := s.validateRequest(peer.ID(), request); ok {
|
|
|
|
s.processRequest(peer, lower, upper, bloom)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:51:05 +00:00
|
|
|
// exceedsPeerRequests in case limit its been setup on the current server and limit
|
2018-05-21 11:30:37 +00:00
|
|
|
// allows the query, it will store/update new query time for the current peer.
|
2018-05-22 15:51:05 +00:00
|
|
|
func (s *WMailServer) exceedsPeerRequests(peer []byte) bool {
|
2018-05-17 11:21:04 +00:00
|
|
|
if s.limit != nil {
|
2018-05-21 11:30:37 +00:00
|
|
|
peerID := string(peer)
|
2018-05-17 11:21:04 +00:00
|
|
|
if !s.limit.isAllowed(peerID) {
|
|
|
|
log.Info("peerID exceeded the number of requests per second")
|
2018-05-22 15:51:05 +00:00
|
|
|
return true
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|
|
|
|
s.limit.add(peerID)
|
|
|
|
}
|
2018-05-22 15:51:05 +00:00
|
|
|
return false
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// processRequest processes the current request and re-sends all stored messages
|
|
|
|
// accomplishing lower and upper limits.
|
2018-05-11 19:43:07 +00:00
|
|
|
func (s *WMailServer) processRequest(peer *whisper.Peer, lower, upper uint32, bloom []byte) []*whisper.Envelope {
|
|
|
|
ret := make([]*whisper.Envelope, 0)
|
|
|
|
var err error
|
|
|
|
var zero common.Hash
|
|
|
|
kl := NewDbKey(lower, zero)
|
|
|
|
ku := NewDbKey(upper, zero)
|
|
|
|
i := s.db.NewIterator(&util.Range{Start: kl.raw, Limit: ku.raw}, nil)
|
|
|
|
defer i.Release()
|
|
|
|
|
2018-05-30 12:31:47 +00:00
|
|
|
var (
|
|
|
|
sentEnvelopes int64
|
|
|
|
sentEnvelopesSize int64
|
|
|
|
)
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
2018-05-11 19:43:07 +00:00
|
|
|
for i.Next() {
|
|
|
|
var envelope whisper.Envelope
|
|
|
|
err = rlp.DecodeBytes(i.Value(), &envelope)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Sprintf("RLP decoding failed: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if whisper.BloomFilterMatch(bloom, envelope.Bloom()) {
|
|
|
|
if peer == nil {
|
|
|
|
// used for test purposes
|
|
|
|
ret = append(ret, &envelope)
|
|
|
|
} else {
|
|
|
|
err = s.w.SendP2PDirect(peer, &envelope)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Failed to send direct message to peer: %s", err))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2018-05-30 12:31:47 +00:00
|
|
|
sentEnvelopes++
|
|
|
|
sentEnvelopesSize += whisper.EnvelopeHeaderLength + int64(len(envelope.Data))
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 12:31:47 +00:00
|
|
|
requestProcessTimer.UpdateSince(start)
|
|
|
|
sentEnvelopesMeter.Mark(sentEnvelopes)
|
|
|
|
sentEnvelopesSizeMeter.Mark(sentEnvelopesSize)
|
|
|
|
|
2018-05-11 19:43:07 +00:00
|
|
|
err = i.Error()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Level DB iterator error: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// validateRequest runs different validations on the current request.
|
2018-05-11 19:43:07 +00:00
|
|
|
func (s *WMailServer) validateRequest(peerID []byte, request *whisper.Envelope) (bool, uint32, uint32, []byte) {
|
|
|
|
if s.pow > 0.0 && request.PoW() < s.pow {
|
|
|
|
return false, 0, 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f := whisper.Filter{KeySym: s.key}
|
|
|
|
decrypted := request.Open(&f)
|
|
|
|
if decrypted == nil {
|
|
|
|
log.Warn(fmt.Sprintf("Failed to decrypt p2p request"))
|
|
|
|
return false, 0, 0, nil
|
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
if err := s.checkMsgSignature(decrypted, peerID); err != nil {
|
|
|
|
log.Warn(err.Error())
|
2018-05-11 19:43:07 +00:00
|
|
|
return false, 0, 0, nil
|
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
bloom, err := s.bloomFromReceivedMessage(decrypted)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err.Error())
|
2018-05-11 19:43:07 +00:00
|
|
|
return false, 0, 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lower := binary.BigEndian.Uint32(decrypted.Payload[:4])
|
|
|
|
upper := binary.BigEndian.Uint32(decrypted.Payload[4:8])
|
2018-05-17 11:21:04 +00:00
|
|
|
|
|
|
|
lowerTime := time.Unix(int64(lower), 0)
|
|
|
|
upperTime := time.Unix(int64(upper), 0)
|
|
|
|
if upperTime.Sub(lowerTime) > maxQueryRange {
|
|
|
|
log.Warn(fmt.Sprintf("Query range too big for peer %s", string(peerID)))
|
|
|
|
return false, 0, 0, nil
|
|
|
|
}
|
|
|
|
|
2018-05-11 19:43:07 +00:00
|
|
|
return true, lower, upper, bloom
|
|
|
|
}
|
2018-05-17 11:21:04 +00:00
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// checkMsgSignature returns an error in case the message is not correcly signed
|
|
|
|
func (s *WMailServer) checkMsgSignature(msg *whisper.ReceivedMessage, id []byte) error {
|
|
|
|
src := crypto.FromECDSAPub(msg.Src)
|
|
|
|
if len(src)-len(id) == 1 {
|
|
|
|
src = src[1:]
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// if you want to check the signature, you can do it here. e.g.:
|
|
|
|
// if !bytes.Equal(peerID, src) {
|
|
|
|
if src == nil {
|
|
|
|
return errors.New("Wrong signature of p2p request")
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
return nil
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
// bloomFromReceivedMessage gor a given whisper.ReceivedMessage it extracts the
|
|
|
|
// used bloom filter
|
|
|
|
func (s *WMailServer) bloomFromReceivedMessage(msg *whisper.ReceivedMessage) ([]byte, error) {
|
|
|
|
payloadSize := len(msg.Payload)
|
2018-05-17 11:21:04 +00:00
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
if payloadSize < 8 {
|
|
|
|
return nil, errors.New("Undersized p2p request")
|
|
|
|
} else if payloadSize == 8 {
|
|
|
|
return whisper.MakeFullNodeBloom(), nil
|
|
|
|
} else if payloadSize < 8+whisper.BloomFilterSize {
|
|
|
|
return nil, errors.New("Undersized bloom filter in p2p request")
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|
2018-05-21 11:30:37 +00:00
|
|
|
|
|
|
|
return msg.Payload[8 : 8+whisper.BloomFilterSize], nil
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|