-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandle.go
94 lines (85 loc) · 2.21 KB
/
handle.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"fmt"
"log"
"net/http"
"os/exec"
"strings"
"github.com/fishioon/wxgo/webwx"
qrcode "github.com/skip2/go-qrcode"
)
func msgHandle(wx *webwx.Wechat, msgs []*webwx.Msg) error {
for _, msg := range msgs {
fromUser := wx.GetUser(msg.FromUserName)
toUser := wx.GetUser(msg.ToUserName)
if fromUser != nil {
log.Printf("FromUserName:%s %s", msg.FromUserName, fromUser.NickName)
}
if toUser != nil {
log.Printf("ToUserName:%s %s", msg.ToUserName, toUser.NickName)
}
log.Printf("Content:%s", msg.Content)
log.Printf("MsgType:%d", msg.MsgType)
if strings.HasPrefix(msg.FromUserName, "@@") {
} else {
if msg.FromUserName != msg.ToUserName {
wx.SendMsg(msg.FromUserName, msg.Content)
}
}
}
return nil
}
// WebwxRun login webwx and run
func WebwxRun(w http.ResponseWriter, _ *http.Request) {
png, err := webwxRun()
if err != nil {
w.Write([]byte(err.Error()))
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("content-type", "image/jpeg")
w.Write(png)
}
func webwxRun() ([]byte, error) {
codeURL, err := webwx.NewLoginCodeURL()
if err != nil {
return nil, err
}
return qrcode.Encode(codeURL, qrcode.Medium, 256)
// return c.Blob(http.StatusOK, "image/jpeg", png)
}
func handleScript(w http.ResponseWriter, r *http.Request) {
res, err := runShellCommand(r.URL.Query().Get("script"))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(res))
}
func handleLocation(w http.ResponseWriter, r *http.Request) {
location := r.URL.Query().Get("location")
if location == "" {
location = "Automatic"
}
res, err := changeNetworkLocation(location)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(res))
}
func runShellCommand(script string) (string, error) {
cmd := exec.Command("sh", "-c", script)
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
return string(out), nil
}
func changeNetworkLocation(location string) (string, error) {
cmd := fmt.Sprintf("networksetup -switchtolocation \"%s\"", location)
return runShellCommand(cmd)
}