record.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package db
  2. import (
  3. "fmt"
  4. "strings"
  5. fp "github.com/cloudflare/mitmengine/fputil"
  6. )
  7. // A Record is represents a software signature.
  8. type Record struct {
  9. RequestSignature fp.RequestSignature
  10. UASignature fp.UASignature
  11. MitmInfo fp.MitmInfo
  12. }
  13. // Parse a record from a string, returning an error on failure.
  14. func (a *Record) Parse(s string) error {
  15. split := strings.Split(s, "|")
  16. if len(split) != 3 {
  17. return fmt.Errorf("invalid record format: '%s'", s)
  18. }
  19. if err := a.UASignature.Parse(split[0]); err != nil {
  20. return err
  21. }
  22. if err := a.RequestSignature.Parse(split[1]); err != nil {
  23. return err
  24. }
  25. if err := a.MitmInfo.Parse(split[2]); err != nil {
  26. return err
  27. }
  28. return nil
  29. }
  30. // Return a string representation of a record.
  31. func (a Record) String() string {
  32. return fmt.Sprintf("%s|%s|%s", a.UASignature, a.RequestSignature, a.MitmInfo)
  33. }
  34. // Merge two records into one.
  35. func (a Record) Merge(b Record) (merged Record) {
  36. merged.RequestSignature = a.RequestSignature.Merge(b.RequestSignature)
  37. merged.UASignature = a.UASignature.Merge(b.UASignature)
  38. merged.MitmInfo = a.MitmInfo.Merge(b.MitmInfo)
  39. return merged
  40. }