add wireguard connectivity between nodes
Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
This commit is contained in:
parent
2e34c8746e
commit
c0515d4802
11 changed files with 640 additions and 180 deletions
187
server/net.go
Normal file
187
server/net.go
Normal file
|
@ -0,0 +1,187 @@
|
|||
/*
|
||||
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"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/gomodule/redigo/redis"
|
||||
)
|
||||
|
||||
type subnetRange struct {
|
||||
Start net.IP
|
||||
End net.IP
|
||||
Subnet *net.IPNet
|
||||
}
|
||||
|
||||
func (s *Server) getIPs(ctx context.Context) (map[string]net.IP, error) {
|
||||
values, err := redis.StringMap(s.local(ctx, "HGETALL", ipsKey))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ips := make(map[string]net.IP, len(values))
|
||||
for id, val := range values {
|
||||
ip := net.ParseIP(string(val))
|
||||
ips[id] = ip
|
||||
}
|
||||
return ips, nil
|
||||
}
|
||||
|
||||
func (s *Server) getIP(ctx context.Context, id string) (net.IP, error) {
|
||||
allIPs, err := s.getIPs(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if ip, exists := allIPs[id]; exists {
|
||||
return ip, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *Server) getOrAllocateIP(ctx context.Context, id string) (net.IP, *net.IPNet, error) {
|
||||
r, err := s.parseSubnetRange(s.cfg.PeerNetwork)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
ip, err := s.getIP(ctx, id)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if ip != nil {
|
||||
return ip, r.Subnet, nil
|
||||
}
|
||||
|
||||
ip, err = s.allocateIP(ctx, id, r)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return ip, r.Subnet, nil
|
||||
}
|
||||
|
||||
func (s *Server) allocateIP(ctx context.Context, id string, r *subnetRange) (net.IP, error) {
|
||||
reservedIPs, err := s.getIPs(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if ip, exists := reservedIPs[id]; exists {
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
lookup := map[string]string{}
|
||||
for id, ip := range reservedIPs {
|
||||
lookup[ip.String()] = id
|
||||
}
|
||||
for ip := r.Start; !ip.Equal(r.End); s.nextIP(ip) {
|
||||
// filter out network, gateway and broadcast
|
||||
if !s.validIP(ip) {
|
||||
continue
|
||||
}
|
||||
if _, exists := lookup[ip.String()]; exists {
|
||||
// ip already reserved
|
||||
continue
|
||||
}
|
||||
|
||||
// save
|
||||
if _, err := s.master(ctx, "HSET", ipsKey, id, ip.String()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("no available IPs")
|
||||
}
|
||||
|
||||
func (s *Server) releaseIP(ctx context.Context, id string) error {
|
||||
ip, err := s.getIP(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ip != nil {
|
||||
if _, err := s.master(ctx, "HDEL", ipsKey, id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) nextIP(ip net.IP) {
|
||||
for j := len(ip) - 1; j >= 0; j-- {
|
||||
ip[j]++
|
||||
if ip[j] > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) validIP(ip net.IP) bool {
|
||||
v := ip[len(ip)-1]
|
||||
switch v {
|
||||
case 0, 1, 255:
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// parseSubnetRange parses the subnet range
|
||||
// format can either be a subnet like 10.0.0.0/8 or range like 10.0.0.100-10.0.0.200/24
|
||||
func (s *Server) parseSubnetRange(subnet string) (*subnetRange, error) {
|
||||
parts := strings.Split(subnet, "-")
|
||||
if len(parts) == 1 {
|
||||
ip, sub, err := net.ParseCIDR(parts[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
end := make(net.IP, len(ip))
|
||||
copy(end, ip)
|
||||
end[len(end)-1] = 254
|
||||
return &subnetRange{
|
||||
Start: ip,
|
||||
End: end,
|
||||
Subnet: sub,
|
||||
}, nil
|
||||
}
|
||||
if len(parts) > 2 || !strings.Contains(subnet, "/") {
|
||||
return nil, fmt.Errorf("invalid range specified; expect format 10.0.0.100-10.0.0.200/24")
|
||||
}
|
||||
start := net.ParseIP(parts[0])
|
||||
end, sub, err := net.ParseCIDR(parts[1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &subnetRange{
|
||||
Start: start,
|
||||
End: end,
|
||||
Subnet: sub,
|
||||
}, nil
|
||||
}
|
129
server/node.go
129
server/node.go
|
@ -23,8 +23,6 @@ package server
|
|||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -33,31 +31,34 @@ import (
|
|||
"github.com/gomodule/redigo/redis"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stellarproject/heimdall"
|
||||
v1 "github.com/stellarproject/heimdall/api/v1"
|
||||
)
|
||||
|
||||
func (s *Server) configureNode() error {
|
||||
ctx := context.Background()
|
||||
nodes, err := redis.Strings(s.local(ctx, "KEYS", s.getNodeKey("*")))
|
||||
nodeKeys, err := redis.Strings(s.local(ctx, "KEYS", s.getNodeKey("*")))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// attempt to connect to existing
|
||||
if len(nodes) > 0 {
|
||||
for _, node := range nodes {
|
||||
addr, err := redis.String(s.local(ctx, "GET", node))
|
||||
if len(nodeKeys) > 0 {
|
||||
for _, nodeKey := range nodeKeys {
|
||||
nodeData, err := redis.Bytes(s.local(ctx, "GET", nodeKey))
|
||||
if err != nil {
|
||||
logrus.Warn(err)
|
||||
continue
|
||||
}
|
||||
var node v1.Node
|
||||
if err := proto.Unmarshal(nodeData, &node); err != nil {
|
||||
return err
|
||||
}
|
||||
// ignore self
|
||||
if addr == s.cfg.GRPCAddress {
|
||||
if node.Addr == s.cfg.GRPCAddress {
|
||||
continue
|
||||
}
|
||||
|
||||
logrus.Infof("attempting to join existing node %s", addr)
|
||||
c, err := s.getClient(addr)
|
||||
logrus.Infof("attempting to join existing node %s", node.Addr)
|
||||
c, err := s.getClient(node.Addr)
|
||||
if err != nil {
|
||||
logrus.Warn(err)
|
||||
continue
|
||||
|
@ -125,9 +126,9 @@ func (s *Server) disableReplica() {
|
|||
}
|
||||
|
||||
func (s *Server) replicaMonitor() {
|
||||
logrus.Debugf("starting replica monitor: ttl=%s", heartbeatInterval)
|
||||
logrus.Debugf("starting replica monitor: ttl=%s", masterHeartbeatInterval)
|
||||
s.replicaCh = make(chan struct{}, 1)
|
||||
t := time.NewTicker(heartbeatInterval)
|
||||
t := time.NewTicker(masterHeartbeatInterval)
|
||||
go func() {
|
||||
for range t.C {
|
||||
if _, err := redis.Bytes(s.local(context.Background(), "GET", masterKey)); err != nil {
|
||||
|
@ -149,9 +150,9 @@ func (s *Server) replicaMonitor() {
|
|||
}
|
||||
|
||||
func (s *Server) masterHeartbeat() {
|
||||
logrus.Debugf("starting master heartbeat: ttl=%s", heartbeatInterval)
|
||||
logrus.Debugf("starting master heartbeat: ttl=%s", masterHeartbeatInterval)
|
||||
// initial update
|
||||
ctx, cancel := context.WithTimeout(context.Background(), heartbeatInterval)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), masterHeartbeatInterval)
|
||||
defer cancel()
|
||||
|
||||
logrus.Infof("cluster master key=%s", s.cfg.ClusterKey)
|
||||
|
@ -159,7 +160,7 @@ func (s *Server) masterHeartbeat() {
|
|||
logrus.Error(err)
|
||||
}
|
||||
|
||||
t := time.NewTicker(heartbeatInterval)
|
||||
t := time.NewTicker(masterHeartbeatInterval)
|
||||
for range t.C {
|
||||
if err := s.updateMasterInfo(ctx); err != nil {
|
||||
logrus.Error(err)
|
||||
|
@ -218,83 +219,37 @@ func (s *Server) updateMasterInfo(ctx context.Context) error {
|
|||
return errors.Wrap(err, "error setting master info")
|
||||
}
|
||||
|
||||
if _, err := s.master(ctx, "EXPIRE", masterKey, int(heartbeatInterval.Seconds())); err != nil {
|
||||
if _, err := s.master(ctx, "EXPIRE", masterKey, int(masterHeartbeatInterval.Seconds())); err != nil {
|
||||
return errors.Wrap(err, "error setting expire for master info")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) updatePeerInfo(ctx context.Context) error {
|
||||
// check for existing key
|
||||
endpoint := fmt.Sprintf("%s:%d", heimdall.GetIP(), s.cfg.WireguardPort)
|
||||
|
||||
peer, err := s.getPeerInfo(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: build allowedIPs from routes and peer network
|
||||
allowedIPs := []string{s.cfg.PeerNetwork}
|
||||
ipHash := hashIPs(allowedIPs)
|
||||
|
||||
// check cached info and validate
|
||||
if peer != nil {
|
||||
peerIPHash := hashIPs(peer.AllowedIPs)
|
||||
// if endpoint is the same assume unchanged
|
||||
if peer.Endpoint == endpoint && peerIPHash == ipHash {
|
||||
logrus.Debugf("peer info: public=%s endpoint=%s", peer.PublicKey, peer.Endpoint)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
privateKey, publicKey, err := generateWireguardKeys(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO: allowed IPs
|
||||
n := &v1.Peer{
|
||||
PrivateKey: privateKey,
|
||||
PublicKey: publicKey,
|
||||
AllowedIPs: allowedIPs,
|
||||
Endpoint: endpoint,
|
||||
}
|
||||
|
||||
logrus.Debugf("peer info: public=%s endpoint=%s", n.PublicKey, n.Endpoint)
|
||||
data, err := proto.Marshal(n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
key := s.getPeerKey(s.cfg.ID)
|
||||
if _, err := s.master(ctx, "SET", key, data); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) getPeerInfo(ctx context.Context) (*v1.Peer, error) {
|
||||
key := s.getPeerKey(s.cfg.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
|
||||
}
|
||||
|
||||
func (s *Server) nodeHeartbeat() {
|
||||
func (s *Server) nodeHeartbeat(ctx context.Context) {
|
||||
logrus.Debugf("starting node heartbeat: ttl=%s", nodeHeartbeatInterval)
|
||||
ctx := context.Background()
|
||||
t := time.NewTicker(nodeHeartbeatInterval)
|
||||
key := s.getNodeKey(s.cfg.ID)
|
||||
for range t.C {
|
||||
if _, err := s.master(ctx, "SET", key, s.cfg.GRPCAddress); err != nil {
|
||||
keyPair, err := s.getOrCreateKeyPair(ctx, s.cfg.ID)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
continue
|
||||
}
|
||||
node := &v1.Node{
|
||||
ID: s.cfg.ID,
|
||||
Addr: s.cfg.GRPCAddress,
|
||||
KeyPair: keyPair,
|
||||
GatewayIP: s.cfg.GatewayIP,
|
||||
GatewayPort: uint64(s.cfg.GatewayPort),
|
||||
}
|
||||
|
||||
data, err := proto.Marshal(node)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
continue
|
||||
}
|
||||
|
||||
if _, err := s.master(ctx, "SET", key, data); err != nil {
|
||||
logrus.Error(err)
|
||||
continue
|
||||
}
|
||||
|
@ -305,11 +260,3 @@ func (s *Server) nodeHeartbeat() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func hashIPs(ips []string) string {
|
||||
h := sha256.New()
|
||||
for _, ip := range ips {
|
||||
h.Write([]byte(ip))
|
||||
}
|
||||
return fmt.Sprintf("%x", h.Sum(nil))
|
||||
}
|
||||
|
|
190
server/peer.go
Normal file
190
server/peer.go
Normal file
|
@ -0,0 +1,190 @@
|
|||
/*
|
||||
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"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/gogo/protobuf/proto"
|
||||
"github.com/gomodule/redigo/redis"
|
||||
"github.com/sirupsen/logrus"
|
||||
v1 "github.com/stellarproject/heimdall/api/v1"
|
||||
)
|
||||
|
||||
func (s *Server) updatePeerInfo(ctx context.Context) error {
|
||||
keypair, err := s.getOrCreateKeyPair(ctx, s.cfg.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
endpoint := fmt.Sprintf("%s:%d", s.cfg.GatewayIP, s.cfg.GatewayPort)
|
||||
|
||||
// TODO: build allowedIPs from routes and peer network
|
||||
allowedIPs := []string{s.cfg.PeerNetwork}
|
||||
|
||||
n := &v1.Peer{
|
||||
ID: s.cfg.ID,
|
||||
KeyPair: keypair,
|
||||
AllowedIPs: allowedIPs,
|
||||
Endpoint: endpoint,
|
||||
}
|
||||
|
||||
data, err := proto.Marshal(n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
key := s.getPeerKey(s.cfg.ID)
|
||||
if _, err := s.master(ctx, "SET", key, data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logrus.Debugf("peer info: endpoint=%s allowedips=%+v", n.Endpoint, n.Endpoint)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (s *Server) updatePeerConfig(ctx context.Context) {
|
||||
logrus.Debugf("starting peer config updater: ttl=%s", peerConfigUpdateInterval)
|
||||
t := time.NewTicker(peerConfigUpdateInterval)
|
||||
|
||||
configHash := ""
|
||||
|
||||
for range t.C {
|
||||
uctx, cancel := context.WithTimeout(ctx, peerConfigUpdateInterval)
|
||||
peerKeys, err := redis.Strings(s.local(uctx, "KEYS", s.getPeerKey("*")))
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
cancel()
|
||||
continue
|
||||
}
|
||||
var peers []*v1.Peer
|
||||
for _, peerKey := range peerKeys {
|
||||
peerData, err := redis.Bytes(s.local(uctx, "GET", peerKey))
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
cancel()
|
||||
continue
|
||||
}
|
||||
var p v1.Peer
|
||||
if err := proto.Unmarshal(peerData, &p); err != nil {
|
||||
logrus.Error(err)
|
||||
cancel()
|
||||
continue
|
||||
}
|
||||
|
||||
// do not add self as a peer
|
||||
if p.ID == s.cfg.ID {
|
||||
continue
|
||||
}
|
||||
|
||||
peers = append(peers, &p)
|
||||
}
|
||||
|
||||
keyPair, err := s.getOrCreateKeyPair(ctx, s.cfg.ID)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
cancel()
|
||||
continue
|
||||
}
|
||||
|
||||
gatewayIP, _, err := s.getOrAllocateIP(ctx, s.cfg.ID)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
cancel()
|
||||
continue
|
||||
}
|
||||
wireguardCfg := &wireguardConfig{
|
||||
Iface: defaultWireguardInterface,
|
||||
PrivateKey: keyPair.PrivateKey,
|
||||
ListenPort: s.cfg.GatewayPort,
|
||||
Address: gatewayIP.String() + "/32",
|
||||
Peers: peers,
|
||||
}
|
||||
|
||||
tmpCfg, err := generateNodeWireguardConfig(wireguardCfg)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
cancel()
|
||||
continue
|
||||
}
|
||||
|
||||
h, err := hashConfig(tmpCfg)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
cancel()
|
||||
continue
|
||||
}
|
||||
|
||||
// if config has not change skip update
|
||||
if h == configHash {
|
||||
continue
|
||||
}
|
||||
|
||||
logrus.Debugf("updating peer config to version %s", h)
|
||||
// update wireguard config
|
||||
if err := os.Rename(tmpCfg, wireguardConfigPath); err != nil {
|
||||
logrus.Error(err)
|
||||
cancel()
|
||||
continue
|
||||
}
|
||||
// reload wireguard
|
||||
if err := restartWireguardTunnel(ctx); err != nil {
|
||||
logrus.Error(err)
|
||||
cancel()
|
||||
continue
|
||||
}
|
||||
configHash = h
|
||||
}
|
||||
}
|
||||
|
||||
func hashConfig(cfgPath string) (string, error) {
|
||||
peerData, err := ioutil.ReadFile(cfgPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
h := sha256.New()
|
||||
h.Write(peerData)
|
||||
return fmt.Sprintf("%x", h.Sum(nil)), nil
|
||||
}
|
|
@ -29,6 +29,7 @@ import (
|
|||
"runtime/pprof"
|
||||
"time"
|
||||
|
||||
"github.com/gogo/protobuf/proto"
|
||||
ptypes "github.com/gogo/protobuf/types"
|
||||
"github.com/gomodule/redigo/redis"
|
||||
"github.com/pkg/errors"
|
||||
|
@ -42,18 +43,21 @@ import (
|
|||
const (
|
||||
masterKey = "heimdall:master"
|
||||
clusterKey = "heimdall:key"
|
||||
keypairsKey = "heimdall:keypairs"
|
||||
nodesKey = "heimdall:nodes"
|
||||
nodeJoinKey = "heimdall:join"
|
||||
peersKey = "heimdall:peers"
|
||||
ipsKey = "heimdall:ips"
|
||||
|
||||
wireguardConfigPath = "/etc/wireguard/darknet.conf"
|
||||
)
|
||||
|
||||
var (
|
||||
empty = &ptypes.Empty{}
|
||||
heartbeatInterval = time.Second * 5
|
||||
nodeHeartbeatInterval = time.Second * 60
|
||||
nodeHeartbeatExpiry = 86400
|
||||
empty = &ptypes.Empty{}
|
||||
masterHeartbeatInterval = time.Second * 5
|
||||
nodeHeartbeatInterval = time.Second * 60
|
||||
nodeHeartbeatExpiry = 86400
|
||||
peerConfigUpdateInterval = time.Second * 10
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
|
@ -122,11 +126,19 @@ func (s *Server) Run() error {
|
|||
}
|
||||
}
|
||||
|
||||
if _, err := s.getOrCreateKeyPair(ctx, s.cfg.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.updatePeerInfo(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
go s.nodeHeartbeat()
|
||||
// start node heartbeat to update in redis
|
||||
go s.nodeHeartbeat(ctx)
|
||||
|
||||
// start peer config updater to configure wireguard as peers join
|
||||
go s.updatePeerConfig(ctx)
|
||||
|
||||
// start listener for pub/sub
|
||||
errCh := make(chan error, 1)
|
||||
|
@ -170,6 +182,39 @@ func getPool(u string) *redis.Pool {
|
|||
return pool
|
||||
}
|
||||
|
||||
func (s *Server) getOrCreateKeyPair(ctx context.Context, id string) (*v1.KeyPair, error) {
|
||||
key := s.getKeyPairKey(id)
|
||||
keyData, err := redis.Bytes(s.master(ctx, "GET", key))
|
||||
if err != nil {
|
||||
if err != redis.ErrNil {
|
||||
return nil, err
|
||||
}
|
||||
logrus.Debugf("generating new keypair for %s", s.cfg.ID)
|
||||
privateKey, publicKey, err := generateWireguardKeys(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keyPair := &v1.KeyPair{
|
||||
PrivateKey: privateKey,
|
||||
PublicKey: publicKey,
|
||||
}
|
||||
data, err := proto.Marshal(keyPair)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := s.master(ctx, "SET", key, data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return keyPair, nil
|
||||
}
|
||||
|
||||
var keyPair v1.KeyPair
|
||||
if err := proto.Unmarshal(keyData, &keyPair); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &keyPair, nil
|
||||
}
|
||||
|
||||
func (s *Server) getNodeKey(id string) string {
|
||||
return fmt.Sprintf("%s:%s", nodesKey, id)
|
||||
}
|
||||
|
@ -178,6 +223,10 @@ func (s *Server) getPeerKey(id string) string {
|
|||
return fmt.Sprintf("%s:%s", peersKey, id)
|
||||
}
|
||||
|
||||
func (s *Server) getKeyPairKey(id string) string {
|
||||
return fmt.Sprintf("%s:%s", keypairsKey, id)
|
||||
}
|
||||
|
||||
func (s *Server) getClient(addr string) (*client.Client, error) {
|
||||
return client.NewClient(s.cfg.ID, addr)
|
||||
}
|
||||
|
|
|
@ -32,12 +32,14 @@ import (
|
|||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
v1 "github.com/stellarproject/heimdall/api/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultInterface = "darknet"
|
||||
wireguardTemplate = `# managed by heimdall
|
||||
defaultWireguardInterface = "darknet"
|
||||
wireguardTemplate = `# managed by heimdall
|
||||
[Interface]
|
||||
PrivateKey = {{ .PrivateKey }}
|
||||
ListenPort = {{ .ListenPort }}
|
||||
|
@ -46,7 +48,7 @@ PostUp = iptables -A FORWARD -i {{ .Iface }} -j ACCEPT; iptables -t nat -A POSTR
|
|||
PostDown = iptables -D FORWARD -i {{ .Iface }} -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE; ip6tables -D FORWARD -i {{ .Iface }} -j ACCEPT; ip6tables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
|
||||
{{ range .Peers }}
|
||||
[Peer]
|
||||
PublicKey = {{ .PublicKey }}
|
||||
PublicKey = {{ .KeyPair.PublicKey }}
|
||||
AllowedIPs = {{ allowedIPs .AllowedIPs }}
|
||||
Endpoint = {{ .Endpoint }}
|
||||
{{ end }}
|
||||
|
@ -65,28 +67,28 @@ type wireguardConfig struct {
|
|||
Peers []*v1.Peer
|
||||
}
|
||||
|
||||
func generateNodeWireguardConfig(cfg *wireguardConfig) (*os.File, error) {
|
||||
func generateNodeWireguardConfig(cfg *wireguardConfig) (string, error) {
|
||||
f, err := ioutil.TempFile("", "heimdall-wireguard-")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", err
|
||||
}
|
||||
t, err := template.New("wireguard").Funcs(template.FuncMap{
|
||||
"allowedIPs": allowedIPs,
|
||||
}).Parse(wireguardTemplate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", err
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(wireguardConfigPath), 0755); err != nil {
|
||||
return nil, err
|
||||
return "", err
|
||||
}
|
||||
|
||||
if err := t.Execute(f, cfg); err != nil {
|
||||
return nil, err
|
||||
return "", err
|
||||
}
|
||||
f.Close()
|
||||
|
||||
return f, nil
|
||||
return f.Name(), nil
|
||||
}
|
||||
|
||||
func generateWireguardKeys(ctx context.Context) (string, string, error) {
|
||||
|
@ -105,6 +107,20 @@ func generateWireguardKeys(ctx context.Context) (string, string, error) {
|
|||
return privateKey, publicKey, nil
|
||||
}
|
||||
|
||||
func restartWireguardTunnel(ctx context.Context) error {
|
||||
tunnelName := strings.Replace(filepath.Base(wireguardConfigPath), filepath.Ext(filepath.Base(wireguardConfigPath)), "", 1)
|
||||
logrus.Infof("restarting tunnel %s", tunnelName)
|
||||
d, err := wgquick(ctx, "down", tunnelName)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, string(d))
|
||||
}
|
||||
u, err := wgquick(ctx, "up", tunnelName)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, string(u))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func wg(ctx context.Context, in io.Reader, args ...string) ([]byte, error) {
|
||||
cmd := exec.CommandContext(ctx, "wg", args...)
|
||||
if in != nil {
|
||||
|
@ -112,3 +128,8 @@ func wg(ctx context.Context, in io.Reader, args ...string) ([]byte, error) {
|
|||
}
|
||||
return cmd.CombinedOutput()
|
||||
}
|
||||
|
||||
func wgquick(ctx context.Context, args ...string) ([]byte, error) {
|
||||
cmd := exec.CommandContext(ctx, "wg-quick", args...)
|
||||
return cmd.CombinedOutput()
|
||||
}
|
||||
|
|
|
@ -45,25 +45,27 @@ Endpoint = 100.100.100.100:10000
|
|||
|
||||
`
|
||||
cfg := &wireguardConfig{
|
||||
Iface: "darknet",
|
||||
Iface: defaultWireguardInterface,
|
||||
PrivateKey: "SERVER-PRIVATE-KEY",
|
||||
ListenPort: 10000,
|
||||
Address: "1.2.3.4:10000",
|
||||
Peers: []*v1.Peer{
|
||||
{
|
||||
PrivateKey: "PEER-PRIVATE-KEY",
|
||||
PublicKey: "PEER-PUBLIC-KEY",
|
||||
KeyPair: &v1.KeyPair{
|
||||
PrivateKey: "PEER-PRIVATE-KEY",
|
||||
PublicKey: "PEER-PUBLIC-KEY",
|
||||
},
|
||||
AllowedIPs: []string{"10.100.0.0/24", "10.254.0.0/16"},
|
||||
Endpoint: "100.100.100.100:10000",
|
||||
},
|
||||
},
|
||||
}
|
||||
f, err := generateWireguardConfig(cfg)
|
||||
configPath, err := generateNodeWireguardConfig(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(f.Name())
|
||||
data, err := ioutil.ReadFile(f.Name())
|
||||
defer os.Remove(configPath)
|
||||
data, err := ioutil.ReadFile(configPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue