9bb4b09a04
CI / Lint + build + test (push) Has been cancelled
Post-repair hardware validation pipeline for Proxmox cluster hosts. Go orchestrator + in-image agent + mkosi live image + bundled dnsmasq PXE + SQLite + HTMX/SSE UI + notify registry + janitor + full docs.
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package orchestrator
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
"net"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// SendWoL sends a Wake-on-LAN magic packet to broadcastIP:port for the
|
|
// given MAC (aa:bb:cc:dd:ee:ff). The packet is 6 bytes of 0xFF followed
|
|
// by the MAC repeated 16 times.
|
|
func SendWoL(mac, broadcastIP string, port int) error {
|
|
macBytes, err := parseMAC(mac)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
packet := make([]byte, 6+16*6)
|
|
for i := 0; i < 6; i++ {
|
|
packet[i] = 0xff
|
|
}
|
|
for i := 0; i < 16; i++ {
|
|
copy(packet[6+i*6:], macBytes)
|
|
}
|
|
|
|
conn, err := net.Dial("udp", net.JoinHostPort(broadcastIP, strconv.Itoa(port)))
|
|
if err != nil {
|
|
return fmt.Errorf("dial wol: %w", err)
|
|
}
|
|
defer conn.Close()
|
|
|
|
if _, err := conn.Write(packet); err != nil {
|
|
return fmt.Errorf("write wol: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func parseMAC(s string) ([]byte, error) {
|
|
s = strings.ToLower(strings.TrimSpace(s))
|
|
parts := strings.Split(s, ":")
|
|
if len(parts) != 6 {
|
|
return nil, fmt.Errorf("invalid MAC %q", s)
|
|
}
|
|
out := make([]byte, 6)
|
|
for i, p := range parts {
|
|
if len(p) != 2 {
|
|
return nil, fmt.Errorf("invalid MAC octet %q", p)
|
|
}
|
|
b, err := hex.DecodeString(p)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid MAC %q: %w", s, err)
|
|
}
|
|
out[i] = b[0]
|
|
}
|
|
return out, nil
|
|
}
|