usb-ks

USB Killswitch
git clone git://git.laack.co/usb-ks.git
Log | Files | Refs | README | LICENSE

filter.go (866B)


      1 package usbmon
      2 
      3 type Filter interface {
      4 	Matches(*Device) bool
      5 }
      6 
      7 type ActionEvent string
      8 
      9 const (
     10 	ActionAdd    ActionEvent = "add"
     11 	ActionRemove ActionEvent = "remove"
     12 	ActionAll    ActionEvent = "all"
     13 )
     14 
     15 type ActionFilter struct {
     16 	Action ActionEvent
     17 }
     18 
     19 func (f *ActionFilter) Matches(dev *Device) bool {
     20 	action := ActionEvent(dev.Properties()["ACTION"])
     21 
     22 	if f.Action == ActionAll && (action == ActionAdd || action == ActionRemove) {
     23 		return true
     24 	}
     25 
     26 	return action == f.Action
     27 }
     28 
     29 type SerialFilter struct {
     30 	Serial string
     31 }
     32 
     33 func (f *SerialFilter) Matches(dev *Device) bool {
     34 	action := ActionEvent(dev.Action())
     35 
     36 	return f.Serial == dev.Serial() && (action == ActionAdd || action == ActionRemove)
     37 }
     38 
     39 type PartitionFilter struct {
     40 	Serial string
     41 }
     42 
     43 func (f *PartitionFilter) Matches(dev *Device) bool {
     44 	return dev.Properties()["DEVICETYPE"] == "partition"
     45 }