2020-05-07 10:25:58 +02:00
|
|
|
package common
|
2020-05-06 19:05:04 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-05-07 10:25:58 +02:00
|
|
|
// NodeMeta holds metadata sent over the cluster
|
|
|
|
type NodeMeta struct {
|
2020-05-06 19:05:04 +02:00
|
|
|
OverlayAddr net.IPNet
|
|
|
|
PubKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Node holds the memberlist node structure
|
2020-05-07 10:25:58 +02:00
|
|
|
type Node struct {
|
2020-05-06 19:05:04 +02:00
|
|
|
Name string
|
|
|
|
Addr net.IP
|
2020-05-06 19:27:21 +02:00
|
|
|
Meta []byte
|
2020-05-07 10:25:58 +02:00
|
|
|
NodeMeta
|
2020-05-06 19:05:04 +02:00
|
|
|
}
|
|
|
|
|
2020-05-07 10:25:58 +02:00
|
|
|
func (n *Node) String() string {
|
2020-05-06 19:05:04 +02:00
|
|
|
return n.Addr.String()
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:25:58 +02:00
|
|
|
func EncodeNodeMeta(nm NodeMeta, limit int) []byte {
|
2020-05-06 19:05:04 +02:00
|
|
|
buf := &bytes.Buffer{}
|
2020-05-06 19:14:33 +02:00
|
|
|
if err := gob.NewEncoder(buf).Encode(nm); err != nil {
|
2020-05-06 19:05:04 +02:00
|
|
|
logrus.Errorf("could not encode local state: %s", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if buf.Len() > limit {
|
|
|
|
logrus.Errorf("could not fit node metadata into %d bytes", limit)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:25:58 +02:00
|
|
|
func DecodeNodeMeta(b []byte) (NodeMeta, error) {
|
2020-05-06 19:05:04 +02:00
|
|
|
// TODO: we blindly trust the info we get from the peers; We should be more defensive to limit the damage a leaked
|
|
|
|
// PSK can cause.
|
2020-05-07 10:25:58 +02:00
|
|
|
nm := NodeMeta{}
|
2020-05-06 19:05:04 +02:00
|
|
|
if err := gob.NewDecoder(bytes.NewReader(b)).Decode(&nm); err != nil {
|
|
|
|
return nm, errors.Wrap(err, "could not decode node meta")
|
|
|
|
}
|
|
|
|
return nm, nil
|
|
|
|
}
|