-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
209 lines (176 loc) · 4.47 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"encoding/binary"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
"unicode/utf16"
"github.com/bitcynth/cynhid"
)
var colorData2 = []byte{
0x56, 0x83, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0xc1, 0x00, 0x00, 0x00, 0x00,
0xaa, 0xff, 0xff, // Color
0x33, // Brightness
}
var colorData1 = []byte{
0x56, 0x81, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa,
}
var colorData3 = []byte{
0x51, 0x28, 0x00, 0x00,
0xff, // Pattern
}
var changeSettingMaybe = []byte{
0x41, 0x01,
}
type DuckyKB struct {
Device *cynhid.Device
}
type alertManagerAlertJSON struct {
Annotations struct {
Description string `json:"description"`
Summary string `json:"summary"`
} `json:"annotations"`
EndsAt string `json:"endsAt"`
GeneratorURL string `json:"generatorURL"`
Labels map[string]string `json:"labels"`
StartsAt string `json:"startsAt"`
Status string `json:"status"`
}
type alertManagerJSON struct {
Alerts []alertManagerAlertJSON `json:"alerts"`
CommonAnnotations struct {
Summary string `json:"summary"`
} `json:"commonAnnotations"`
CommonLabels struct {
Alertname string `json:"alertname"`
} `json:"commonLabels"`
ExternalURL string `json:"externalURL"`
GroupKey string `json:"groupKey"`
GroupLabels struct {
Alertname string `json:"alertname"`
} `json:"groupLabels"`
Receiver string `json:"receiver"`
Status string `json:"status"`
Version string `json:"version"`
}
var currentAlerts int
func main() {
listenAddr := flag.String("listen", ":9095", "the listen address for http")
flag.Parse()
err := cynhid.Init()
if err != nil {
log.Fatalf("failed to initialize hidapi: %v", err)
}
defer cynhid.Exit()
devInfos, err := cynhid.Enumerate(0x04d9, 0x0348)
if err != nil {
log.Fatal(err)
}
var devPath string
for _, devInfo := range devInfos {
if devInfo.InterfaceNumber == 1 {
devPath = devInfo.Path
}
}
dev, err := cynhid.OpenPath(devPath)
if err != nil {
log.Fatal(err)
}
defer dev.Close()
dev.SetNonblocking(true)
kb := &DuckyKB{
Device: dev,
}
fmt.Printf("Firmware Version: %s\n", kb.GetFirmwareVersion())
currentAlerts = 0
go func() {
lastAlerts := -1
for {
if currentAlerts > 0 {
lastAlerts = currentAlerts
kb.SetBacklight(0x00, 0x00, 0x00, 0x00, DuckyKBPatternStatic)
time.Sleep(time.Millisecond * 500)
kb.SetBacklight(0xff, 0x00, 0x00, 0xff, DuckyKBPatternStatic)
time.Sleep(time.Millisecond * 500)
} else {
if lastAlerts != 0 {
kb.SetBacklight(0xff, 0xff, 0xff, 0xff, DuckyKBPatternRainbow)
}
lastAlerts = currentAlerts
time.Sleep(time.Millisecond * 500)
}
}
}()
http.HandleFunc("/webhook/am", func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println(err)
}
var am alertManagerJSON
err = json.Unmarshal(b, &am)
if err != nil {
log.Println(err)
}
for _, alert := range am.Alerts {
log.Printf("%s: %s", alert.Status, alert.Annotations.Summary)
if alert.Status == "resolved" {
currentAlerts--
if currentAlerts < 0 {
currentAlerts = 0
}
} else if alert.Status == "firing" {
currentAlerts++
}
}
})
http.ListenAndServe(*listenAddr, nil)
}
func (kb *DuckyKB) WriteToDev(data []byte) (int, error) {
dataLen := len(data)
for i := 0; i < 64-dataLen; i++ {
data = append(data, 0x00)
}
n, err := kb.Device.Write(data, 64)
return n, err
}
func (kb *DuckyKB) ReadFromDev() ([]byte, error) {
buf, err := kb.Device.Read(64)
if err != nil {
return nil, nil
}
//log.Printf("read: %x\n", buf)
return buf, nil
}
func (kb *DuckyKB) GetFirmwareVersion() string {
_, err := kb.WriteToDev(usbPacketGetVersion)
if err != nil {
log.Fatal(err)
}
time.Sleep(time.Millisecond * 5)
resp, err := kb.ReadFromDev()
if err != nil {
log.Fatal(err)
}
// The version string is UTF-16LE and starts at pos 9
var tmp []uint16
d := resp[8:]
for i := 0; i < len(d)/2; i++ {
j := binary.LittleEndian.Uint16([]byte{d[i*2], d[i*2+1]})
if j == 0 {
break
}
tmp = append(tmp, j)
}
runes := utf16.Decode(tmp)
return string(runes)
}
func (kb *DuckyKB) SetBacklight(r int, g int, b int, brightness int, pattern int) {
kb.WriteToDev(changeSettingMaybe)
kb.WriteToDev(colorData1)
kb.WriteToDev(append(usbPacketSetColorPrefix, byte(r), byte(g), byte(b), byte(brightness)))
kb.WriteToDev(append(usbPacketSetPatternPrefix, byte(pattern)))
}