request_data.go 5.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package data
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/google/btree"
  6. "github.com/scraperwall/ipmeta"
  7. )
  8. type RequestData struct {
  9. ID string `bson:"_id,omitempty" json:"_id,omitempty" form:"_id,omitempty" url:"_id,omitempty"`
  10. Connection string `bson:"connection" json:"connection" form:"connection,omitempty" url:"connection,omitempty"`
  11. IpDst string `bson:"ip_dst" json:"ip_dst" form:"ip_dst,omitempty" url:"ip_dst,omitempty"`
  12. TcpSeq uint32 `bson:"tcp_seq" json:"tcp_seq" form:"tcp_seq,omitempty" url:"tcp_seq,omitempty"`
  13. XForwardedFor string `bson:"x_forwarded_for" json:"x_forwarded_for,omitempty" form:"x_forwarded_for,omitempty" url:"x_forwarded_for,omitempty"`
  14. XRealIP string `bson:"x_real_ip" json:"x_real_ip,omitempty" form:"x_real_ip,omitempty" url:"x_real_ip,omitempty"`
  15. Origin string `bson:"origin" json:"origin,omitempty" form:"origin,omitempty" url:"origin,omitempty"`
  16. PortDst uint32 `bson:"port_dst" json:"port_dst,omitempty" form:"port_dst,omitempty" url:"port_dst,omitempty"`
  17. Referer string `bson:"referer" json:"referer,omitempty" form:"referer,omitempty" url:"referer,omitempty"`
  18. XRequestedWith string `bson:"x_requested_with" json:"x_requested_with,omitempty" form:"x_requested_with.omitempty" url:"x_requested_with,omitempty"`
  19. PortSrc uint32 `bson:"port_src" json:"port_src,omitempty" form:"port_src,omitempty" url:"port_src,omitempty"`
  20. Url string `bson:"url" json:"url,omitempty" form:"url,omitempty" url:"url,omitempty"`
  21. Reverse string `bson:"reverse" json:"reverse,omitempty" form:"reverse,omitempty" url:"reverse,omitempty"`
  22. AcceptEncoding string `bson:"accept_encoding" json:"accept_encoding,omitempty" form:"accept_encoding,omitempty" url:"accept_encoding,omitempty"`
  23. AcceptLanguage string `bson:"accept_language" json:"accept_language,omitempty" form:"accept_language,omitempty" url:"accept_language,omitempty"`
  24. CreatedAt time.Time `bson:"created_at" json:"created_at,omitempty" form:"created_at,omitempty" url:"created_at,omitempty"`
  25. SortTime time.Time `bson:"-" json:"-" form:"-" url:"-"`
  26. IpSrc string `bson:"ip_src" json:"ip_src,omitempty" form:"ip_src,omitempty" url:"ip_src,omitempty"`
  27. Method string `bson:"method" json:"method,omitempty" form:"method,omitempty" url:"method,omitempty"`
  28. UserAgent string `bson:"user_agent" json:"user_agent,omitempty" form:"user_agent,omitempty" url:"user_agent,omitempty"`
  29. Accept string `bson:"accept" json:"accept,omitempty" form:"accept,omitempty" url:"accept,omitempty"`
  30. Cookie string `bson:"cookie" json:"cookie,omitempty" form:"cookie,omitempty" url:"cookie,omitempty"`
  31. Host string `bson:"host" json:"host,omitempty" form:"host,omitempty" url:"host,omitempty"`
  32. HostName string `bson:"hostname" json:"hostname,omitempty" form:"hostname,omitempty" url:"hostname,omitempty"`
  33. Protocol string `bson:"protocol" json:"protocol,omitempty" form:"protocol,omitempty" url:"protocol,omitempty"`
  34. Source string `bson:"source" json:"source,omitempty" form:"source,omitempty" url:"source,omitempty"`
  35. Via string `bson:"via" json:"via,omitempty" form:"via,omitempty" url:"via,omitempty"`
  36. Customer string `bson:"customer" json:"customer,omitempty" form:"customer,omitempty" url:"customer,omitempty"`
  37. DataRaw map[string]interface{} `bson:"data_raw" json:"data_raw,omitempty" form:"data_raw,omitempty" url:"data_raw,omitempty"`
  38. Weight uint64 `bson:"weight" json:"weight,omitempty" form:"weight,omitempty" url:"weight,omitempty"`
  39. Meta *ipmeta.Meta `bson:"meta" json:"meta,omitempty" url:"meta,omitempty" `
  40. Type string `bson:"type" json:"type,omitempty" form:"type:omitempty" url:"type,omitempty"`
  41. }
  42. func (a *RequestData) Less(b btree.Item) bool {
  43. return a.CreatedAt.Before(b.(*RequestData).CreatedAt)
  44. }
  45. func (a *RequestData) ToRequest() *Request {
  46. return &Request{
  47. Url: a.Url,
  48. Reverse: a.Reverse,
  49. IpSrc: a.IpSrc,
  50. IpDst: a.IpDst,
  51. PortSrc: a.PortSrc,
  52. PortDst: a.PortDst,
  53. TcpSeq: a.TcpSeq,
  54. CreatedAt: a.CreatedAt.UnixNano(),
  55. XForwardedFor: a.XForwardedFor,
  56. XRealIP: a.XRealIP,
  57. Method: a.Method,
  58. Origin: a.Origin,
  59. Referer: a.Referer,
  60. UserAgent: a.UserAgent,
  61. Source: a.Source,
  62. Host: a.Host,
  63. Protocol: a.Protocol,
  64. Connection: a.Connection,
  65. XRequestedWith: a.XRequestedWith,
  66. AcceptEncoding: a.AcceptEncoding,
  67. AcceptLanguage: a.AcceptLanguage,
  68. Accept: a.Accept,
  69. Cookie: a.Cookie,
  70. Via: a.Via,
  71. Customer: a.Customer,
  72. }
  73. }
  74. // Path returns the Url without any query string
  75. func (a *RequestData) Path() string {
  76. idx := strings.Index(a.Url, "?")
  77. if idx >= 0 {
  78. return a.Url[0:idx]
  79. }
  80. return a.Url
  81. }