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 }