1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
package sws
import (
"fmt"
"net"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"time"
)
type Hit struct {
ID *int `json:"id"`
SiteID *int `json:"site_id" db:"site_id"`
Addr string `json:"addr"`
// URL components
Scheme string `json:"scheme"`
Host string `json:"host"`
Path string `json:"path"`
Query *string `json:"query,omitempty"`
Title *string `json:"title,omitempty"`
Referrer *string `json:"referrer,omitempty"`
UserAgentHash *string `json:"user_agent_hash,omitempty" db:"user_agent_hash"`
ViewPort *string `json:"view_port,omitempty" db:"view_port"`
CountryCode *string `json:"country_code" db:"country_code"`
NoScript bool `json:"no_script" db:"no_script"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
//Features map[string]string `json:"features,omitempty"`
// TODO
//Site *Site `db:"s"`
UserAgent *UserAgent `db:"ua"`
}
type Hitter interface {
//Filter(FilterFunc) *HitSet
Hits() []*Hit
Begin() time.Time
End() time.Time
Duration() time.Duration
//Location() *time.Location // TODO Time zone
}
func (h Hit) String() string {
var out strings.Builder
for _, sp := range []*string{&h.Scheme, &h.Host, &h.Path, h.Query} {
if sp != nil {
out.WriteString(*sp)
}
}
return out.String()
}
// SortHits in ascending order by time.
func SortHits(hits []*Hit) {
sort.Slice(hits, func(i, j int) bool {
return hits[i].CreatedAt.Before(hits[j].CreatedAt)
})
}
func HitFromRequest(r *http.Request) (*Hit, error) {
// Strip port from remote address
addr := r.RemoteAddr
if strings.Contains(r.RemoteAddr, ":") {
addr, _, _ = net.SplitHostPort(r.RemoteAddr)
}
out := &Hit{
CreatedAt: time.Now(),
Addr: addr,
}
q := r.URL.Query()
siteIDs := q.Get("id")
if siteIDs == "" {
if siteIDs = q.Get("site"); siteIDs == "" {
return nil, fmt.Errorf("missing site")
}
}
siteID, err := strconv.Atoi(siteIDs)
if err != nil {
return nil, fmt.Errorf("invalid site")
}
out.SiteID = &siteID
// Host and referrer
ref, err := url.ParseRequestURI(r.Referer())
if err != nil || ref == nil {
ref = new(url.URL)
}
host := q.Get("h")
if host == "" {
host = ref.Host
}
out.Host = host
if h := r.Header.Get("HTTP_X_REQUESTED_WITH"); h == "" {
out.NoScript = true
}
scheme := q.Get("s")
if scheme != "" {
out.Scheme = strings.TrimSuffix(scheme, ":")
} else {
out.Scheme = ref.Scheme
}
path := q.Get("p")
if path != "" {
out.Path = path
} else {
out.Path = ref.RawPath
}
query := q.Get("q")
if query != "" {
out.Query = &query
} else {
if ref.RawQuery != "" {
out.Query = ptrString("?" + ref.RawQuery)
}
}
if title := q.Get("t"); title != "" {
out.Title = &title
}
referrer := q.Get("r")
if referrer != "" {
out.Referrer = &referrer
} else {
s := ref.String()
out.Referrer = &s
}
out.UserAgent = UserAgentFromRequest(r)
out.UserAgentHash = &out.UserAgent.Hash
if view := q.Get("v"); view != "" {
out.ViewPort = &view
}
return out, nil
}
|