|
|
|
package zones
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/netip"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LookupNetIP uses the DNS Resolver to get the public addresses associated
|
|
|
|
// to a Machine
|
|
|
|
func (m *Machine) LookupNetIP(timeout time.Duration) ([]netip.Addr, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
return m.zone.zones.resolver.LookupNetIP(ctx, "ip", m.FullName())
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdatePublicAddresses uses the DNS Resolver to set Machine.PublicAddresses
|
|
|
|
func (m *Machine) UpdatePublicAddresses() error {
|
|
|
|
addrs, err := m.LookupNetIP(2 * time.Second)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.PublicAddresses = addrs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Machine) init() error {
|
|
|
|
if err := m.setID(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < RingsCount; i++ {
|
|
|
|
if err := m.tryReadWireguardKeys(i); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Machine) setID() error {
|
|
|
|
zoneName := m.zone.Name
|
|
|
|
suffix := m.Name[len(zoneName)+1:]
|
|
|
|
|
|
|
|
id, err := strconv.ParseInt(suffix, 10, 8)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.ID = int(id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Machine) scan(opts *ScanOptions) error {
|
|
|
|
for i := 0; i < RingsCount; i++ {
|
|
|
|
if err := m.tryApplyWireguardConfig(i); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !opts.DontResolvePublicAddresses {
|
|
|
|
return m.UpdatePublicAddresses()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|