New detail in getMulticastInterfaces admin endpoint

This commit is contained in:
Neil Alexander 2024-11-23 14:49:48 +00:00
parent d3b4de46ea
commit 7790a19e4c
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
4 changed files with 57 additions and 16 deletions

View File

@ -281,9 +281,21 @@ func run() int {
if err := json.Unmarshal(recv.Response, &resp); err != nil { if err := json.Unmarshal(recv.Response, &resp); err != nil {
panic(err) panic(err)
} }
table.SetHeader([]string{"Interface"}) fmtBool := func(b bool) string {
if b {
return "Yes"
}
return "-"
}
table.SetHeader([]string{"Name", "Listen Address", "Beacon", "Listen", "Password"})
for _, p := range resp.Interfaces { for _, p := range resp.Interfaces {
table.Append([]string{p}) table.Append([]string{
p.Name,
p.Address,
fmtBool(p.Beacon),
fmtBool(p.Listen),
fmtBool(p.Password),
})
} }
table.Render() table.Render()

View File

@ -8,7 +8,7 @@ package config
func getDefaults() platformDefaultParameters { func getDefaults() platformDefaultParameters {
return platformDefaultParameters{ return platformDefaultParameters{
// Admin // Admin
DefaultAdminListen: "tcp://localhost:9001", DefaultAdminListen: "unix:///var/run/yggdrasil.sock",
// Configuration (used for yggdrasilctl) // Configuration (used for yggdrasilctl)
DefaultConfigFile: "/etc/yggdrasil.conf", DefaultConfigFile: "/etc/yggdrasil.conf",

View File

@ -2,20 +2,47 @@ package multicast
import ( import (
"encoding/json" "encoding/json"
"slices"
"strings"
"github.com/Arceliar/phony"
"github.com/yggdrasil-network/yggdrasil-go/src/admin" "github.com/yggdrasil-network/yggdrasil-go/src/admin"
) )
type GetMulticastInterfacesRequest struct{} type GetMulticastInterfacesRequest struct{}
type GetMulticastInterfacesResponse struct { type GetMulticastInterfacesResponse struct {
Interfaces []string `json:"multicast_interfaces"` Interfaces []MulticastInterfaceState `json:"multicast_interfaces"`
}
type MulticastInterfaceState struct {
Name string `json:"name"`
Address string `json:"address"`
Beacon bool `json:"beacon"`
Listen bool `json:"listen"`
Password bool `json:"password"`
} }
func (m *Multicast) getMulticastInterfacesHandler(_ *GetMulticastInterfacesRequest, res *GetMulticastInterfacesResponse) error { func (m *Multicast) getMulticastInterfacesHandler(_ *GetMulticastInterfacesRequest, res *GetMulticastInterfacesResponse) error {
res.Interfaces = []string{} res.Interfaces = []MulticastInterfaceState{}
for _, v := range m.Interfaces() { phony.Block(m, func() {
res.Interfaces = append(res.Interfaces, v.Name) for name, intf := range m._interfaces {
} is := MulticastInterfaceState{
Name: intf.iface.Name,
Beacon: intf.beacon,
Listen: intf.listen,
Password: len(intf.password) > 0,
}
if li := m._listeners[name]; li != nil && li.listener != nil {
is.Address = li.listener.Addr().String()
} else {
is.Address = "-"
}
res.Interfaces = append(res.Interfaces, is)
}
})
slices.SortStableFunc(res.Interfaces, func(a, b MulticastInterfaceState) int {
return strings.Compare(a.Name, b.Name)
})
return nil return nil
} }

View File

@ -156,7 +156,13 @@ func (m *Multicast) _updateInterfaces() {
delete(interfaces, name) delete(interfaces, name)
continue continue
} }
info.addrs = addrs for _, addr := range addrs {
addrIP, _, err := net.ParseCIDR(addr.String())
if err != nil || addrIP.To4() != nil || !addrIP.IsLinkLocalUnicast() {
continue
}
info.addrs = append(info.addrs, addr)
}
interfaces[name] = info interfaces[name] = info
m.log.Debugf("Discovered addresses for interface %s: %s", name, addrs) m.log.Debugf("Discovered addresses for interface %s: %s", name, addrs)
} }
@ -299,13 +305,9 @@ func (m *Multicast) _announce() {
for _, info := range m._interfaces { for _, info := range m._interfaces {
iface := info.iface iface := info.iface
for _, addr := range info.addrs { for _, addr := range info.addrs {
addrIP, _, _ := net.ParseCIDR(addr.String()) addrIP, _, err := net.ParseCIDR(addr.String())
// Ignore IPv4 addresses // Ignore IPv4 addresses or non-link-local addresses
if addrIP.To4() != nil { if err != nil || addrIP.To4() != nil || !addrIP.IsLinkLocalUnicast() {
continue
}
// Ignore non-link-local addresses
if !addrIP.IsLinkLocalUnicast() {
continue continue
} }
if info.listen { if info.listen {