assign ipv6 address to each machine derived from their public keys

This commit is contained in:
Pavel Sviderski
2024-08-29 22:37:49 +10:00
parent eed2c8809c
commit 41098d00b8
8 changed files with 77 additions and 43 deletions
+20 -8
View File
@@ -24,6 +24,22 @@ type Cluster struct {
config *config.Config
}
func (c *Cluster) User() (*User, error) {
userKey := c.config.Clusters[c.Name].UserKey
if userKey == nil {
return nil, errors.New("cluster user_key must be set in the config")
}
return NewUser(userKey)
}
func (c *Cluster) Machines() []config.MachineConnection {
cfg, ok := c.config.Clusters[c.Name]
if !ok {
return nil
}
return cfg.Machines
}
func (c *Cluster) toConfig() *config.Cluster {
return &config.Cluster{
Name: c.Name,
@@ -126,17 +142,13 @@ func (c *Cluster) AddMachine(ctx context.Context, name, user, host string, port
_ = exec.Close()
}()
wgUserKey := c.config.Clusters[c.Name].UserKey
if wgUserKey == nil {
return "", errors.New("cluster user_key must be set in the config")
}
wgUser, err := NewUser(wgUserKey)
clusterUser, err := c.User()
if err != nil {
return "", fmt.Errorf("create user from key: %w", err)
return "", err
}
userPeerCfg := network.PeerConfig{
Subnet: netip.PrefixFrom(wgUser.Address(), 128),
PublicKey: wgUser.PublicKey(),
Subnet: netip.PrefixFrom(clusterUser.IPv6(), 128),
PublicKey: clusterUser.PublicKey(),
}
mcfg, err := machine.NewBootstrapConfig(name, netip.Prefix{}, userPeerCfg)
if err != nil {
+3 -1
View File
@@ -1,6 +1,8 @@
package config
import "uncloud/internal/secret"
import (
"uncloud/internal/secret"
)
type MachineConnection struct {
User string `toml:"user,omitempty"`
+3 -5
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"net/netip"
"uncloud/internal/machine/network"
"uncloud/internal/secret"
)
@@ -43,9 +44,6 @@ func (u *User) PublicKey() secret.Secret {
return pubKey[:]
}
func (u *User) Address() netip.Addr {
pubKey := u.PublicKey()
bytes := [16]byte{0xfd, 0xcc}
copy(bytes[2:], pubKey[:14])
return netip.AddrFrom16(bytes)
func (u *User) IPv6() netip.Addr {
return network.PeerIPv6(u.PublicKey())
}