You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
1.7 KiB
89 lines
1.7 KiB
1 year ago
|
package cluster
|
||
1 year ago
|
|
||
|
import (
|
||
|
"context"
|
||
|
"net/netip"
|
||
1 year ago
|
"strconv"
|
||
1 year ago
|
"strings"
|
||
1 year ago
|
"time"
|
||
1 year ago
|
|
||
|
"darvaza.org/core"
|
||
1 year ago
|
)
|
||
|
|
||
1 year ago
|
// LookupNetIP uses the DNS Resolver to get the public addresses associated
|
||
|
// to a Machine
|
||
|
func (m *Machine) LookupNetIP(timeout time.Duration) ([]netip.Addr, error) {
|
||
1 year ago
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||
|
|
||
|
defer cancel()
|
||
|
|
||
|
return m.zone.zones.resolver.LookupNetIP(ctx, "ip", m.FullName())
|
||
|
}
|
||
|
|
||
1 year ago
|
// UpdatePublicAddresses uses the DNS Resolver to set Machine.PublicAddresses
|
||
|
func (m *Machine) UpdatePublicAddresses() error {
|
||
|
addrs, err := m.LookupNetIP(2 * time.Second)
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
m.PublicAddresses = addrs
|
||
|
return nil
|
||
|
}
|
||
1 year ago
|
|
||
1 year ago
|
func (m *Machine) init() error {
|
||
1 year ago
|
if err := m.setID(); err != nil {
|
||
1 year ago
|
return core.Wrap(err, m.Name)
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
for i := 0; i < RingsCount; i++ {
|
||
|
if err := m.tryReadWireguardKeys(i); err != nil {
|
||
1 year ago
|
return core.Wrap(err, m.Name)
|
||
1 year ago
|
}
|
||
|
}
|
||
1 year ago
|
|
||
1 year ago
|
return nil
|
||
|
}
|
||
|
|
||
1 year ago
|
func (m *Machine) setID() error {
|
||
|
zoneName := m.zone.Name
|
||
|
|
||
1 year ago
|
l := len(zoneName)
|
||
|
switch {
|
||
|
case len(m.Name) < l+2:
|
||
|
return ErrInvalidName
|
||
|
case !strings.HasPrefix(m.Name, zoneName):
|
||
|
return ErrInvalidName
|
||
|
case m.Name[l] != '-':
|
||
|
return ErrInvalidName
|
||
|
}
|
||
|
|
||
|
suffix := m.Name[l+1:]
|
||
1 year ago
|
id, err := strconv.ParseInt(suffix, 10, 8)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
m.ID = int(id)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
1 year ago
|
func (m *Machine) scan(opts *ScanOptions) error {
|
||
1 year ago
|
for i := 0; i < RingsCount; i++ {
|
||
|
if err := m.tryApplyWireguardConfig(i); err != nil {
|
||
1 year ago
|
m.error(err).
|
||
|
WithField("subsystem", "wireguard").
|
||
|
WithField("node", m.Name).
|
||
|
WithField("ring", i).
|
||
|
Print()
|
||
1 year ago
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
if !opts.DontResolvePublicAddresses {
|
||
1 year ago
|
return m.UpdatePublicAddresses()
|
||
1 year ago
|
}
|
||
|
|
||
|
return nil
|
||
1 year ago
|
}
|