forked from jetkvm/kvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwol.go
50 lines (40 loc) · 1.05 KB
/
wol.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package kvm
import (
"bytes"
"encoding/binary"
"fmt"
"net"
)
// SendWOLMagicPacket sends a Wake-on-LAN magic packet to the specified MAC address
func rpcSendWOLMagicPacket(macAddress string) error {
// Parse the MAC address
mac, err := net.ParseMAC(macAddress)
if err != nil {
return fmt.Errorf("invalid MAC address: %v", err)
}
// Create the magic packet
packet := createMagicPacket(mac)
// Set up UDP connection
conn, err := net.Dial("udp", "255.255.255.255:9")
if err != nil {
return fmt.Errorf("failed to establish UDP connection: %v", err)
}
defer conn.Close()
// Send the packet
_, err = conn.Write(packet)
if err != nil {
return fmt.Errorf("failed to send WOL packet: %v", err)
}
return nil
}
// createMagicPacket creates a Wake-on-LAN magic packet
func createMagicPacket(mac net.HardwareAddr) []byte {
var buf bytes.Buffer
// Write 6 bytes of 0xFF
buf.Write(bytes.Repeat([]byte{0xFF}, 6))
// Write the target MAC address 16 times
for i := 0; i < 16; i++ {
binary.Write(&buf, binary.BigEndian, mac)
}
return buf.Bytes()
}