-
Notifications
You must be signed in to change notification settings - Fork 7
/
goshut.go
executable file
·55 lines (46 loc) · 1.27 KB
/
goshut.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
51
52
53
54
55
package main
import (
"fmt"
"net/http"
"os"
"syscall"
"time"
)
/*
* See https://docs.microsoft.com/en-us/windows/console/handlerroutine
*/
const CTRL_SHUTDOWN_EVENT uint = 0x6
func eventHandler(controlType uint) uint {
fmt.Printf("consoleControlHandler called with %v\n", controlType)
if controlType == CTRL_SHUTDOWN_EVENT {
fmt.Printf("Received CTRL_SHUTDOWN_EVENT (%d)\n", CTRL_SHUTDOWN_EVENT)
fmt.Printf("Looping forever\n")
start := time.Now()
for {
fmt.Printf("IN LOOP: Elapsed time=%s\n", time.Now().Sub(start))
time.Sleep(500 * time.Millisecond)
}
}
return 0
}
func main() {
fmt.Println("Starting goshut")
kernel32 := syscall.NewLazyDLL("kernel32.dll")
setConsoleCtrlHandler := kernel32.NewProc("SetConsoleCtrlHandler")
/*
* Calls the systemcall to set a ConsoleHandler
*/
r1, r2, lastErr := setConsoleCtrlHandler.Call(syscall.NewCallback(eventHandler), 1)
if r1 == 0 {
fmt.Fprintf(os.Stderr, "setConsoleCtrlHandler failed! Oops\n")
os.Exit(1)
}
fmt.Printf("Setting Handler done. Call result %v %v %v\n", r1, r2, lastErr)
/*
* This is a trivial http server just to keep CF happy
*/
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love you!\n")
})
http.ListenAndServe(":8080", nil)
}