Skip to content

Instantly share code, notes, and snippets.

@mendelgusmao

mendelgusmao/antiwold.go

Last active Dec 14, 2015
Embed
What would you like to do?
A WOL daemon to shutdown the computer
package main
import (
"fmt"
"net"
"bytes"
"os/exec"
)
var (
buffer_size int = 128
buffer [128]byte
packet []byte = []byte{255, 255, 255, 255, 255, 255}
mac []byte = []byte{0, 0, 0, 0, 0, 0}
program string = "%windir%\\system32\\rundll32.exe powrprof.dll,SetSuspendState"
)
func init() {
for i := 1; i <= 16; i++ {
packet = append(packet, mac...)
}
}
func main() {
address, resolveError := net.ResolveUDPAddr("IP4", "0.0.0.0:9")
if resolveError == nil {
socket, listenError := net.ListenUDP("udp", address)
if listenError == nil {
for {
_, readError := socket.Read(buffer[0:buffer_size])
if readError == nil {
if bytes.Equal(buffer[0:buffer_size], packet[0:buffer_size]) {
command := exec.Command(program)
runError := command.Run()
if runError != nil {
fmt.Println(runError)
}
}
} else {
fmt.Println(readError)
}
}
} else {
fmt.Println(listenError)
}
} else {
fmt.Println(resolveError)
}
}
package main
import (
"flag"
"fmt"
"net"
"os"
)
var (
buffer_size int = 102
buffer [102]byte
inAddr string = "0.0.0.0:7"
outAddr string = "192.168.0.255:9"
)
func init() {
flag.StringVar(&inAddr, "listen", inAddr, "listen address")
flag.StringVar(&outAddr, "send", outAddr, "send address")
flag.Parse()
}
func main() {
inAddress, err := net.ResolveUDPAddr("IP4", inAddr)
catch("resolving listening address", err, false)
outAddress, err := net.ResolveUDPAddr("IP4", outAddr)
catch("resolving send address:", err, false)
socketIn, err := net.ListenUDP("udp", inAddress)
catch("listening:", err, false)
for {
read, err := socketIn.Read(buffer[0:buffer_size])
catch("reading:", err, true)
if err == nil && read > 0 {
socketOut, err := net.DialUDP("udp4", nil, outAddress)
catch("opening socket for writing:", err, true)
if err == nil {
written, err := socketOut.Write(buffer[0:buffer_size])
catch("writing to socket:", err, true)
if err == nil && written > 0 {
fmt.Println(written, "bytes written")
}
}
}
}
}
func catch(description string, err error, relax bool) {
if err == nil {
return
}
fmt.Println("Error", description, err)
if !relax {
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment