2017-04-14 11:29:00 +03:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
2017-01-31 13:16:20 +03:00
|
|
|
//
|
2017-04-14 11:29:00 +03:00
|
|
|
// 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
|
2017-01-31 13:16:20 +03:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2017-04-14 11:29:00 +03:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2017-01-31 13:16:20 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-04-14 11:29:00 +03:00
|
|
|
// GNU Lesser General Public License for more details.
|
2017-01-31 13:16:20 +03:00
|
|
|
//
|
2017-04-14 11:29:00 +03:00
|
|
|
// 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/>.
|
2017-01-31 13:16:20 +03:00
|
|
|
|
|
|
|
package mailserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2017-02-22 15:10:07 +03:00
|
|
|
"fmt"
|
2017-01-31 13:16:20 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-02-13 15:15:20 +03:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2017-02-22 15:10:07 +03:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2017-01-31 13:16:20 +03:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2018-02-10 16:35:32 +03:00
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
|
2017-01-31 13:16:20 +03:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
type WMailServer struct {
|
|
|
|
db *leveldb.DB
|
|
|
|
w *whisper.Whisper
|
|
|
|
pow float64
|
|
|
|
key []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type DBKey struct {
|
|
|
|
timestamp uint32
|
|
|
|
hash common.Hash
|
|
|
|
raw []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04 12:10:18 +03:00
|
|
|
func (s *WMailServer) Init(shh *whisper.Whisper, path string, password string, pow float64) error {
|
2017-01-31 13:16:20 +03:00
|
|
|
var err error
|
|
|
|
if len(path) == 0 {
|
2018-05-04 12:10:18 +03:00
|
|
|
return fmt.Errorf("DB file is not specified")
|
2017-01-31 13:16:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(password) == 0 {
|
2018-05-04 12:10:18 +03:00
|
|
|
return fmt.Errorf("password is not specified")
|
2017-01-31 13:16:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
s.db, err = leveldb.OpenFile(path, nil)
|
|
|
|
if err != nil {
|
2018-05-04 12:10:18 +03:00
|
|
|
return fmt.Errorf("open DB file: %s", err)
|
2017-01-31 13:16:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
s.w = shh
|
|
|
|
s.pow = pow
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
MailServerKeyID, err := s.w.AddSymKeyFromPassword(password)
|
2017-01-31 13:16:20 +03:00
|
|
|
if err != nil {
|
2018-05-04 12:10:18 +03:00
|
|
|
return fmt.Errorf("create symmetric key: %s", err)
|
2017-01-31 13:16:20 +03:00
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
s.key, err = s.w.GetSymKey(MailServerKeyID)
|
|
|
|
if err != nil {
|
2018-05-04 12:10:18 +03:00
|
|
|
return fmt.Errorf("save symmetric key: %s", err)
|
2017-04-10 00:49:22 +03:00
|
|
|
}
|
2018-05-04 12:10:18 +03:00
|
|
|
return nil
|
2017-01-31 13:16:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WMailServer) Close() {
|
|
|
|
if s.db != nil {
|
|
|
|
s.db.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WMailServer) Archive(env *whisper.Envelope) {
|
|
|
|
key := NewDbKey(env.Expiry-env.TTL, env.Hash())
|
|
|
|
rawEnvelope, err := rlp.EncodeToBytes(env)
|
|
|
|
if err != nil {
|
2017-02-22 15:10:07 +03:00
|
|
|
log.Error(fmt.Sprintf("rlp.EncodeToBytes failed: %s", err))
|
2017-01-31 13:16:20 +03:00
|
|
|
} else {
|
|
|
|
err = s.db.Put(key.raw, rawEnvelope, nil)
|
|
|
|
if err != nil {
|
2017-02-22 15:10:07 +03:00
|
|
|
log.Error(fmt.Sprintf("Writing to DB failed: %s", err))
|
2017-01-31 13:16:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WMailServer) DeliverMail(peer *whisper.Peer, request *whisper.Envelope) {
|
2017-02-13 15:15:20 +03:00
|
|
|
if peer == nil {
|
2017-08-08 14:48:06 +03:00
|
|
|
log.Error("Whisper peer is nil")
|
2017-01-31 13:16:20 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-01 18:04:09 +03:00
|
|
|
ok, lower, upper, bloom := s.validateRequest(peer.ID(), request)
|
2017-02-13 15:15:20 +03:00
|
|
|
if ok {
|
2018-03-01 18:04:09 +03:00
|
|
|
s.processRequest(peer, lower, upper, bloom)
|
2017-02-13 15:15:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 18:04:09 +03:00
|
|
|
func (s *WMailServer) processRequest(peer *whisper.Peer, lower, upper uint32, bloom []byte) []*whisper.Envelope {
|
2017-02-13 15:15:20 +03:00
|
|
|
ret := make([]*whisper.Envelope, 0)
|
2017-01-31 13:16:20 +03:00
|
|
|
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()
|
|
|
|
|
|
|
|
for i.Next() {
|
|
|
|
var envelope whisper.Envelope
|
|
|
|
err = rlp.DecodeBytes(i.Value(), &envelope)
|
|
|
|
if err != nil {
|
2017-02-22 15:10:07 +03:00
|
|
|
log.Error(fmt.Sprintf("RLP decoding failed: %s", err))
|
2017-01-31 13:16:20 +03:00
|
|
|
}
|
|
|
|
|
2018-03-01 18:04:09 +03:00
|
|
|
if whisper.BloomFilterMatch(bloom, envelope.Bloom()) {
|
2017-02-13 15:15:20 +03:00
|
|
|
if peer == nil {
|
|
|
|
// used for test purposes
|
|
|
|
ret = append(ret, &envelope)
|
|
|
|
} else {
|
|
|
|
err = s.w.SendP2PDirect(peer, &envelope)
|
|
|
|
if err != nil {
|
2017-02-22 15:10:07 +03:00
|
|
|
log.Error(fmt.Sprintf("Failed to send direct message to peer: %s", err))
|
2017-02-13 15:15:20 +03:00
|
|
|
return nil
|
|
|
|
}
|
2017-01-31 13:16:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = i.Error()
|
|
|
|
if err != nil {
|
2017-02-22 15:10:07 +03:00
|
|
|
log.Error(fmt.Sprintf("Level DB iterator error: %s", err))
|
2017-01-31 13:16:20 +03:00
|
|
|
}
|
2017-02-13 15:15:20 +03:00
|
|
|
|
|
|
|
return ret
|
2017-01-31 13:16:20 +03:00
|
|
|
}
|
|
|
|
|
2018-03-01 18:04:09 +03:00
|
|
|
func (s *WMailServer) validateRequest(peerID []byte, request *whisper.Envelope) (bool, uint32, uint32, []byte) {
|
2017-01-31 13:16:20 +03:00
|
|
|
if s.pow > 0.0 && request.PoW() < s.pow {
|
2018-03-01 18:04:09 +03:00
|
|
|
return false, 0, 0, nil
|
2017-01-31 13:16:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
f := whisper.Filter{KeySym: s.key}
|
|
|
|
decrypted := request.Open(&f)
|
|
|
|
if decrypted == nil {
|
2017-02-22 15:10:07 +03:00
|
|
|
log.Warn(fmt.Sprintf("Failed to decrypt p2p request"))
|
2018-03-01 18:04:09 +03:00
|
|
|
return false, 0, 0, nil
|
2017-01-31 13:16:20 +03:00
|
|
|
}
|
|
|
|
|
2017-02-13 15:15:20 +03:00
|
|
|
src := crypto.FromECDSAPub(decrypted.Src)
|
|
|
|
if len(src)-len(peerID) == 1 {
|
|
|
|
src = src[1:]
|
|
|
|
}
|
2018-02-27 17:45:00 +03:00
|
|
|
|
|
|
|
// if you want to check the signature, you can do it here. e.g.:
|
|
|
|
// if !bytes.Equal(peerID, src) {
|
|
|
|
if src == nil {
|
2017-02-22 15:10:07 +03:00
|
|
|
log.Warn(fmt.Sprintf("Wrong signature of p2p request"))
|
2018-03-01 18:04:09 +03:00
|
|
|
return false, 0, 0, nil
|
2017-01-31 13:16:20 +03:00
|
|
|
}
|
|
|
|
|
2018-03-01 18:04:09 +03:00
|
|
|
var bloom []byte
|
|
|
|
payloadSize := len(decrypted.Payload)
|
|
|
|
if payloadSize < 8 {
|
|
|
|
log.Warn(fmt.Sprintf("Undersized p2p request"))
|
|
|
|
return false, 0, 0, nil
|
|
|
|
} else if payloadSize == 8 {
|
|
|
|
bloom = whisper.MakeFullNodeBloom()
|
|
|
|
} else if payloadSize < 8+whisper.BloomFilterSize {
|
|
|
|
log.Warn(fmt.Sprintf("Undersized bloom filter in p2p request"))
|
|
|
|
return false, 0, 0, nil
|
|
|
|
} else {
|
|
|
|
bloom = decrypted.Payload[8 : 8+whisper.BloomFilterSize]
|
2017-01-31 13:16:20 +03:00
|
|
|
}
|
|
|
|
|
2018-03-01 18:04:09 +03:00
|
|
|
lower := binary.BigEndian.Uint32(decrypted.Payload[:4])
|
|
|
|
upper := binary.BigEndian.Uint32(decrypted.Payload[4:8])
|
|
|
|
return true, lower, upper, bloom
|
2017-01-31 13:16:20 +03:00
|
|
|
}
|