[triggers/usb] add UsbDisconnect Trigger

This commit is contained in:
2023-10-28 11:40:17 +02:00
parent cec34477c0
commit d6f09d7c84
2 changed files with 97 additions and 0 deletions

View File

@@ -30,6 +30,7 @@ func GetAllTriggers() []DocumentedTrigger {
return []DocumentedTrigger{
TimeOut{},
EthernetDisconnect{},
UsbDisconnect{},
}
}

96
triggers/usb.go Normal file
View File

@@ -0,0 +1,96 @@
package triggers
import (
"encoding/json"
"errors"
"fmt"
"os"
"time"
"unknown.com/gokill/actions"
"unknown.com/gokill/internal"
)
type UsbDisconnect struct {
WaitTillConnected bool `json:"waitTillConnected"`
DeviceName string `json:"deviceName"`
action actions.Action
}
func isUsbConnected(deviceName string) bool {
devicePath := "/dev/disk/by-id/" + deviceName
_, err := os.Open(devicePath)
if errors.Is(err, os.ErrNotExist) {
return false
}
return true
}
func (t UsbDisconnect) Listen() {
if t.WaitTillConnected {
for !isUsbConnected(t.DeviceName) {
time.Sleep(1 * time.Second)
}
fmt.Sprintln("Device %s detected.", t.DeviceName)
fmt.Println("UsbDisconnect Trigger is Armed")
}
for {
if !isUsbConnected(t.DeviceName) {
break
}
time.Sleep(1 * time.Second)
}
actions.Fire(t.action)
}
func CreateUsbDisconnect(config internal.KillSwitchConfig) (UsbDisconnect, error) {
result := UsbDisconnect{
WaitTillConnected: true,
}
err := json.Unmarshal(config.Options, &result)
if err != nil {
return UsbDisconnect{}, err
}
if result.DeviceName == "" {
return UsbDisconnect{}, internal.OptionMissingError{"deviceName"}
}
action, err := actions.NewAction(config.Actions)
if err != nil {
return UsbDisconnect{}, err
}
result.action = action
return result, nil
}
func (e UsbDisconnect) Create(config internal.KillSwitchConfig) (Trigger, error) {
return CreateUsbDisconnect(config)
}
func (p UsbDisconnect) GetName() string {
return "UsbDisconnect"
}
func (p UsbDisconnect) GetDescription() string {
return "Triggers when given usb drive is disconnected"
}
func (p UsbDisconnect) GetOptions() []internal.ConfigOption {
return []internal.ConfigOption{
{"waitTillConnected", "bool", "Only trigger when device was connected before", "true"},
{"deviceId", "string", "Name of device under /dev/disk/by-id/", "\"\""},
}
}