You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/agent/peermigration/endpoints.go

165 lines
4.2 KiB
Go

package peermigration
import (
"errors"
"fmt"
"net"
"net/url"
"sort"
"strconv"
)
const MaxEndpoints = 16
// DiscoverEndpoints publishes only literal, locally assigned unicast IPs. The
// peer validates the pinned host identity, so neither DNS nor a public CA is
// required. Private and public candidates are both retained for automatic
// physical-LAN preference with public fallback.
func DiscoverEndpoints(port int) ([]string, error) {
if port < 1 || port > 65535 {
return nil, errors.New("peer migration port is invalid")
}
interfaces, err := net.Interfaces()
if err != nil {
return nil, fmt.Errorf("inspect peer migration interfaces: %w", err)
}
type candidate struct {
endpoint string
private bool
}
candidates := make([]candidate, 0, len(interfaces))
seen := make(map[string]struct{})
for _, iface := range interfaces {
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
continue
}
addresses, addrErr := iface.Addrs()
if addrErr != nil {
continue
}
for _, address := range addresses {
ip, _, parseErr := net.ParseCIDR(address.String())
if parseErr != nil || ip == nil || !ip.IsGlobalUnicast() {
continue
}
ip = canonicalIP(ip)
if ip == nil {
continue
}
endpoint := "https://" + net.JoinHostPort(ip.String(), strconv.Itoa(port))
if _, duplicate := seen[endpoint]; duplicate {
continue
}
seen[endpoint] = struct{}{}
candidates = append(candidates, candidate{endpoint: endpoint, private: ip.IsPrivate()})
}
}
sort.Slice(candidates, func(left, right int) bool {
if candidates[left].private != candidates[right].private {
// Keep a directly reachable public fallback when a host has many
// container/libvirt private interfaces. The receiver still orders
// same-subnet endpoints ahead of public endpoints at dial time.
return !candidates[left].private
}
return candidates[left].endpoint < candidates[right].endpoint
})
if len(candidates) > MaxEndpoints {
candidates = candidates[:MaxEndpoints]
}
endpoints := make([]string, len(candidates))
for index := range candidates {
endpoints[index] = candidates[index].endpoint
}
if len(endpoints) == 0 {
return nil, errors.New("peer migration has no locally assigned unicast address")
}
return endpoints, nil
}
func ValidateEndpoints(values []string) error {
if len(values) < 1 || len(values) > MaxEndpoints {
return errors.New("peer migration endpoints are missing or exceed the limit")
}
seen := make(map[string]struct{}, len(values))
for _, value := range values {
if err := ValidateEndpoint(value); err != nil {
return err
}
if _, duplicate := seen[value]; duplicate {
return errors.New("peer migration endpoints must be unique")
}
seen[value] = struct{}{}
}
return nil
}
func orderEndpoints(values []string) []string {
ordered := append([]string(nil), values...)
networks := localNetworks()
sort.SliceStable(ordered, func(left, right int) bool {
leftIP := endpointIP(ordered[left])
rightIP := endpointIP(ordered[right])
leftScore := endpointScore(leftIP, networks)
rightScore := endpointScore(rightIP, networks)
if leftScore != rightScore {
return leftScore < rightScore
}
return ordered[left] < ordered[right]
})
return ordered
}
func endpointScore(ip net.IP, networks []*net.IPNet) int {
for _, network := range networks {
if network.Contains(ip) {
return 0
}
}
if ip != nil && !ip.IsPrivate() {
return 1
}
return 2
}
func localNetworks() []*net.IPNet {
interfaces, err := net.Interfaces()
if err != nil {
return nil
}
result := make([]*net.IPNet, 0, len(interfaces))
for _, iface := range interfaces {
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
continue
}
addresses, err := iface.Addrs()
if err != nil {
continue
}
for _, address := range addresses {
ip, network, err := net.ParseCIDR(address.String())
if err == nil && ip != nil && ip.IsGlobalUnicast() {
result = append(result, network)
}
}
}
return result
}
func endpointIP(value string) net.IP {
parsed, err := url.Parse(value)
if err != nil {
return nil
}
return canonicalIP(net.ParseIP(parsed.Hostname()))
}
func canonicalIP(ip net.IP) net.IP {
if ip == nil {
return nil
}
if ipv4 := ip.To4(); ipv4 != nil {
return ipv4
}
return ip.To16()
}