new: HID devices are now exported by the api.rest module

This commit is contained in:
evilsocket 2019-02-21 11:43:48 +01:00
commit e0e1f4e6df
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
5 changed files with 64 additions and 7 deletions

View file

@ -1,6 +1,7 @@
package network
import (
"encoding/json"
"fmt"
"sort"
"strings"
@ -45,6 +46,13 @@ type HIDDevice struct {
payloadsSz uint64
}
type hidDeviceJSON struct {
LastSeen time.Time `json:"last_seen"`
Type string `json:"type"`
Address string `json:"address"`
Channels []string `json:"channels"`
}
func NormalizeHIDAddress(address string) string {
parts := strings.Split(address, ":")
for i, p := range parts {
@ -81,6 +89,16 @@ func NewHIDDevice(address []byte, channel int, payload []byte) *HIDDevice {
return dev
}
func (dev *HIDDevice) MarshalJSON() ([]byte, error) {
doc := hidDeviceJSON{
LastSeen: dev.LastSeen,
Type: dev.Type.String(),
Address: dev.Address,
Channels: dev.ChannelsList(),
}
return json.Marshal(doc)
}
func (dev *HIDDevice) AddChannel(ch int) {
dev.Lock()
defer dev.Unlock()
@ -88,7 +106,7 @@ func (dev *HIDDevice) AddChannel(ch int) {
dev.channels[ch] = true
}
func (dev *HIDDevice) Channels() string {
func (dev *HIDDevice) ChannelsList() []string {
dev.Lock()
defer dev.Unlock()
@ -98,7 +116,12 @@ func (dev *HIDDevice) Channels() string {
}
sort.Strings(chans)
return strings.Join(chans, ",")
return chans
}
func (dev *HIDDevice) Channels() string {
return strings.Join(dev.ChannelsList(), ",")
}
// credits to https://github.com/insecurityofthings/jackit/tree/master/jackit/plugins