-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote-pause.go
68 lines (61 loc) · 1.28 KB
/
remote-pause.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
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
_ "embed"
"fmt"
"io"
"net/http"
"os/exec"
"regexp"
"strings"
)
func click(q string) bool {
err := exec.Command("./winclick.exe", q).Run()
if err != nil {
fmt.Println(err)
}
return err == nil
}
func printIP() {
// resolver from "net" pkg uses cgo, so just exec `ipconfig`
out, err := exec.Command("ipconfig").Output()
if err != nil {
fmt.Println(err)
}
re := regexp.MustCompile(`IPv[46] ?Address[ .]+([0-9.:]+)`)
matches := re.FindAllSubmatch(out, -1)
switch len(matches) {
case 0:
return
case 1:
fmt.Println("Open following page on your smartphone:")
default:
fmt.Println("Open one of following pages on your smartphone:")
}
for _, m := range matches {
fmt.Printf("\thttp://%s:20133 \r\n", string(m[1]))
}
}
func main() {
var failCnt int
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
q := r.URL.RawQuery
q = strings.ReplaceAll(q, "_", " ")
q = strings.ReplaceAll(q, "=", "")
if q != "" {
if !click(q) {
failCnt += 1
}
}
io.WriteString(w, pageBegin)
if failCnt != 0 {
fmt.Fprintf(w, "Error counter: %v\n", failCnt)
}
io.WriteString(w, pageEnd)
})
printIP()
http.ListenAndServe("0.0.0.0:20133", nil)
}
//go:embed html/begin.html
var pageBegin string
//go:embed html/end.html
var pageEnd string