this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(network): add function to broadcast Wake-on-LAN magic packet

Khue Doan 98bc72bb 94d482c6

+28 -1
+28 -1
internal/network/wol.go
··· 1 1 package network 2 2 3 - import "net" 3 + import ( 4 + "fmt" 5 + "net" 6 + ) 4 7 5 8 func buildMacicPacket(mac net.HardwareAddr) []byte { 6 9 // A physical WakeOnLAN (Magic Packet) will look like this: ··· 28 31 29 32 return packet 30 33 } 34 + 35 + func SendWakeOnLAN(mac net.HardwareAddr) error { 36 + // https://superuser.com/questions/295325/does-it-matter-what-udp-port-a-wol-signal-is-sent-to 37 + // UDP is recommended because it can be generated without raw sockets which come with security restrictions, 38 + // and port 9 is recommended because it maps to the old well-known discard protocol 39 + const wolPort = 9 40 + magicPacket := buildMacicPacket(mac) 41 + 42 + // TODO no IPv6 for now 43 + conn, err := net.DialUDP("udp4", nil, &net.UDPAddr{ 44 + IP: net.IPv4bcast, 45 + Port: wolPort, 46 + }) 47 + if err != nil { 48 + return fmt.Errorf("failed to dial UDP broadcast: %w", err) 49 + } 50 + defer conn.Close() 51 + 52 + if _, err = conn.Write(magicPacket); err != nil { 53 + return fmt.Errorf("failed to send magic packet: %w", err) 54 + } 55 + 56 + return nil 57 + }