request_data.go 5.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. BrowserAge time.Duration `bson:"bage" json:"browser_age,omitempty" form:"browser_age,omitempty" url:"browser_age,omitempty"`
  42. BrowserEra int `bson:"bera" json:"browser_era,omitempty" form:"browser_era,omitempty" url:"browser_era,omitempty"`
  43. }
  44. func (a *RequestData) Less(b btree.Item) bool {
  45. return a.CreatedAt.Before(b.(*RequestData).CreatedAt)
  46. }
  47. func (a *RequestData) ToRequest() *Request {
  48. return &Request{
  49. Url: a.Url,
  50. Reverse: a.Reverse,
  51. IpSrc: a.IpSrc,
  52. IpDst: a.IpDst,
  53. PortSrc: a.PortSrc,
  54. PortDst: a.PortDst,
  55. TcpSeq: a.TcpSeq,
  56. CreatedAt: a.CreatedAt.UnixNano(),
  57. XForwardedFor: a.XForwardedFor,
  58. XRealIP: a.XRealIP,
  59. Method: a.Method,
  60. Origin: a.Origin,
  61. Referer: a.Referer,
  62. UserAgent: a.UserAgent,
  63. Source: a.Source,
  64. Host: a.Host,
  65. Protocol: a.Protocol,
  66. Connection: a.Connection,
  67. XRequestedWith: a.XRequestedWith,
  68. AcceptEncoding: a.AcceptEncoding,
  69. AcceptLanguage: a.AcceptLanguage,
  70. Accept: a.Accept,
  71. Cookie: a.Cookie,
  72. Via: a.Via,
  73. Customer: a.Customer,
  74. }
  75. }
  76. // Path returns the Url without any query string
  77. func (a *RequestData) Path() string {
  78. idx := strings.Index(a.Url, "?")
  79. if idx >= 0 {
  80. return a.Url[0:idx]
  81. }
  82. return a.Url
  83. }