mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-05-10 12:08:24 +08:00
wol 幻数据包发送测试
This commit is contained in:
parent
eacb63befc
commit
876da28fe8
10
services/wol/wol.go
Normal file
10
services/wol/wol.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package wol
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
var Services *Service
|
||||||
|
|
||||||
|
func InitService() {
|
||||||
|
Services = &Service{}
|
||||||
|
}
|
66
services/wol/wol_test.go
Normal file
66
services/wol/wol_test.go
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
package wol
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/hex"
|
||||||
|
"net"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestName(t *testing.T) {
|
||||||
|
udpAddr, err := net.ResolveUDPAddr("udp", "255.255.255.255:9")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Logf("%v", udpAddr)
|
||||||
|
|
||||||
|
interfaces, err := net.Interfaces()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mac := "00-E0-4C-84-50-EB"
|
||||||
|
mac = strings.Replace(strings.Replace(mac, ":", "", -1), "-", "", -1)
|
||||||
|
macHex, _ := hex.DecodeString(mac)
|
||||||
|
// 广播MAC地址 FF:FF:FF:FF:FF:FF
|
||||||
|
var broadcast = []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
buffer.Write(broadcast)
|
||||||
|
for i := 0; i < 16; i++ {
|
||||||
|
buffer.Write(macHex)
|
||||||
|
}
|
||||||
|
wolPackage := buffer.Bytes()
|
||||||
|
|
||||||
|
for _, i := range interfaces {
|
||||||
|
if i.Flags&net.FlagUp == 0 {
|
||||||
|
t.Logf("%s 未启用", i.Name)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("%s 已启用", i.Name)
|
||||||
|
addrs, err := i.Addrs()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, addr := range addrs {
|
||||||
|
if ip, ok := addr.(*net.IPNet); ok {
|
||||||
|
if ipv4 := ip.IP.To4(); ipv4 != nil {
|
||||||
|
t.Logf("\t ipv4: %s", ipv4.String())
|
||||||
|
|
||||||
|
conn, err := net.DialUDP("udp", &net.UDPAddr{IP: ipv4}, udpAddr)
|
||||||
|
defer func() {
|
||||||
|
_ = conn.Close()
|
||||||
|
t.Logf("wol 幻数据包发送成功, %s %s %s", i.Name, ipv4.String(), mac)
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("wol 幻数据包发送失败 %s", err)
|
||||||
|
}
|
||||||
|
_, _ = conn.Write(wolPackage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user