2019-10-04 13:46:03 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 Stellar Project
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in the
|
|
|
|
Software without restriction, including without limitation the rights to use, copy,
|
|
|
|
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
and to permit persons to whom the Software is furnished to do so, subject to the
|
|
|
|
following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all copies
|
|
|
|
or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
|
|
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
|
|
|
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
|
|
|
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/gomodule/redigo/redis"
|
|
|
|
"github.com/sirupsen/logrus"
|
2019-10-07 14:06:12 +00:00
|
|
|
"github.com/stellarproject/heimdall"
|
2019-10-04 13:46:03 +00:00
|
|
|
v1 "github.com/stellarproject/heimdall/api/v1"
|
2019-10-07 14:06:12 +00:00
|
|
|
"github.com/stellarproject/heimdall/wg"
|
2019-10-04 13:46:03 +00:00
|
|
|
)
|
|
|
|
|
2019-10-05 02:56:46 +00:00
|
|
|
// Peers returns a list of known peers
|
|
|
|
func (s *Server) Peers(ctx context.Context, req *v1.PeersRequest) (*v1.PeersResponse, error) {
|
|
|
|
peers, err := s.getPeers(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &v1.PeersResponse{
|
|
|
|
Peers: peers,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getPeers(ctx context.Context) ([]*v1.Peer, error) {
|
|
|
|
peerKeys, err := redis.Strings(s.local(ctx, "KEYS", s.getPeerKey("*")))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var peers []*v1.Peer
|
|
|
|
for _, peerKey := range peerKeys {
|
|
|
|
data, err := redis.Bytes(s.local(ctx, "GET", peerKey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var peer v1.Peer
|
|
|
|
if err := proto.Unmarshal(data, &peer); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
peers = append(peers, &peer)
|
|
|
|
}
|
|
|
|
return peers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) peerUpdater(ctx context.Context) {
|
|
|
|
logrus.Debugf("starting peer config updater: ttl=%s", peerConfigUpdateInterval)
|
|
|
|
t := time.NewTicker(peerConfigUpdateInterval)
|
|
|
|
for range t.C {
|
|
|
|
uctx, cancel := context.WithTimeout(ctx, peerConfigUpdateInterval)
|
2019-10-07 14:06:12 +00:00
|
|
|
if err := s.updatePeerInfo(uctx, s.cfg.ID); err != nil {
|
|
|
|
logrus.Errorf("updateLocalPeerInfo: %s", err)
|
2019-10-05 02:56:46 +00:00
|
|
|
cancel()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-10-07 17:52:47 +00:00
|
|
|
peers, err := s.getPeers(ctx)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
cancel()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
node, err := s.getNode(ctx, s.cfg.ID)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
cancel()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.updatePeerConfig(uctx, node, peers); err != nil {
|
2019-10-05 02:56:46 +00:00
|
|
|
logrus.Errorf("updatePeerConfig: %s", err)
|
|
|
|
cancel()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 14:06:12 +00:00
|
|
|
func (s *Server) updatePeerInfo(ctx context.Context, id string) error {
|
|
|
|
keypair, err := s.getOrCreateKeyPair(ctx, id)
|
2019-10-04 13:46:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-07 14:06:12 +00:00
|
|
|
endpoint, err := s.getPeerEndpoint(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-04 13:46:03 +00:00
|
|
|
|
2019-10-05 02:56:46 +00:00
|
|
|
// build allowedIPs from routes and peer network
|
2019-10-06 06:28:58 +00:00
|
|
|
allowedIPs := []string{}
|
2019-10-07 14:06:12 +00:00
|
|
|
|
|
|
|
// add peer net
|
|
|
|
if endpoint == "" {
|
|
|
|
allowedIPs = append(allowedIPs, s.cfg.PeerNetwork)
|
|
|
|
}
|
2019-10-06 06:28:58 +00:00
|
|
|
nodes, err := s.getNodes(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range nodes {
|
|
|
|
// only add the route if a peer to prevent route duplicate
|
2019-10-07 14:06:12 +00:00
|
|
|
if node.ID != id {
|
2019-10-06 06:28:58 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, gatewayNet, err := s.getNodeIP(ctx, node.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
allowedIPs = append(allowedIPs, gatewayNet.String())
|
|
|
|
}
|
|
|
|
|
2019-10-05 02:56:46 +00:00
|
|
|
routes, err := s.getRoutes(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, route := range routes {
|
2019-10-07 14:06:12 +00:00
|
|
|
// only add the route if a peer to prevent route blackhole
|
|
|
|
if route.NodeID != id {
|
2019-10-05 02:56:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
allowedIPs = append(allowedIPs, route.Network)
|
|
|
|
}
|
2019-10-04 13:46:03 +00:00
|
|
|
|
|
|
|
n := &v1.Peer{
|
2019-10-07 14:06:12 +00:00
|
|
|
ID: id,
|
2019-10-04 13:46:03 +00:00
|
|
|
KeyPair: keypair,
|
|
|
|
AllowedIPs: allowedIPs,
|
|
|
|
Endpoint: endpoint,
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := proto.Marshal(n)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-07 14:06:12 +00:00
|
|
|
pHash := heimdall.HashData(data)
|
2019-10-05 02:56:46 +00:00
|
|
|
|
2019-10-07 14:06:12 +00:00
|
|
|
key := s.getPeerKey(id)
|
2019-10-05 02:56:46 +00:00
|
|
|
peerData, err := redis.Bytes(s.local(ctx, "GET", key))
|
|
|
|
if err != nil {
|
|
|
|
if err != redis.ErrNil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 14:06:12 +00:00
|
|
|
eHash := heimdall.HashData(peerData)
|
2019-10-05 02:56:46 +00:00
|
|
|
|
|
|
|
// skip update if same
|
|
|
|
if pHash == eHash {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-04 13:46:03 +00:00
|
|
|
if _, err := s.master(ctx, "SET", key, data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-07 14:06:12 +00:00
|
|
|
logrus.Debugf("peer info updated: id=%s", id)
|
2019-10-04 13:46:03 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-07 14:06:12 +00:00
|
|
|
func (s *Server) getPeerEndpoint(ctx context.Context, id string) (string, error) {
|
|
|
|
node, err := s.getNode(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
if err == redis.ErrNil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if node == nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s:%d", node.EndpointIP, node.EndpointPort), nil
|
|
|
|
}
|
|
|
|
|
2019-10-04 13:46:03 +00:00
|
|
|
func (s *Server) getPeerInfo(ctx context.Context, id string) (*v1.Peer, error) {
|
|
|
|
key := s.getPeerKey(id)
|
|
|
|
data, err := redis.Bytes(s.local(ctx, "GET", key))
|
|
|
|
if err != nil {
|
|
|
|
if err == redis.ErrNil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var peer v1.Peer
|
|
|
|
if err := proto.Unmarshal(data, &peer); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &peer, nil
|
|
|
|
}
|
|
|
|
|
2019-10-07 17:52:47 +00:00
|
|
|
func (s *Server) updatePeerConfig(ctx context.Context, node *v1.Node, peers []*v1.Peer) error {
|
|
|
|
var nodePeers []*v1.Peer
|
|
|
|
for _, peer := range peers {
|
2019-10-05 02:56:46 +00:00
|
|
|
// do not add self as a peer
|
2019-10-07 17:52:47 +00:00
|
|
|
if peer.ID == node.ID {
|
2019-10-04 13:46:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-10-07 17:52:47 +00:00
|
|
|
nodePeers = append(nodePeers, peer)
|
2019-10-05 02:56:46 +00:00
|
|
|
}
|
2019-10-04 13:46:03 +00:00
|
|
|
|
2019-10-07 17:52:47 +00:00
|
|
|
//size, _ := gatewayNet.Mask.Size()
|
2019-10-07 14:06:12 +00:00
|
|
|
wireguardCfg := &wg.Config{
|
2019-10-07 17:52:47 +00:00
|
|
|
Iface: node.InterfaceName,
|
2019-10-08 03:37:13 +00:00
|
|
|
PrivateKey: node.KeyPair.PrivateKey,
|
2019-10-07 17:52:47 +00:00
|
|
|
ListenPort: int(node.EndpointPort),
|
|
|
|
Address: fmt.Sprintf("%s/%d", node.GatewayIP, 16),
|
|
|
|
Peers: nodePeers,
|
2019-10-05 02:56:46 +00:00
|
|
|
}
|
2019-10-04 13:46:03 +00:00
|
|
|
|
2019-10-07 14:06:12 +00:00
|
|
|
wireguardConfigPath := s.getWireguardConfigPath()
|
|
|
|
tmpCfg, err := wg.GenerateNodeConfig(wireguardCfg, wireguardConfigPath)
|
2019-10-05 02:56:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-04 13:46:03 +00:00
|
|
|
|
2019-10-07 14:06:12 +00:00
|
|
|
h, err := heimdall.HashConfig(tmpCfg)
|
2019-10-05 02:56:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-07 14:06:12 +00:00
|
|
|
e, err := heimdall.HashConfig(wireguardConfigPath)
|
2019-10-05 02:56:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-10-04 13:46:03 +00:00
|
|
|
}
|
2019-10-05 02:56:46 +00:00
|
|
|
|
|
|
|
// if config has not change skip update
|
|
|
|
if h == e {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("updating peer config to version %s", h)
|
|
|
|
// update wireguard config
|
|
|
|
if err := os.Rename(tmpCfg, wireguardConfigPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// reload wireguard
|
2019-10-07 14:06:12 +00:00
|
|
|
if err := wg.RestartTunnel(ctx, s.getTunnelName()); err != nil {
|
2019-10-05 02:56:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|