-
Notifications
You must be signed in to change notification settings - Fork 2
/
httpse.go
162 lines (143 loc) · 3.41 KB
/
httpse.go
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package httpseverywhere
import (
"net/url"
"sync"
"sync/atomic"
"time"
"github.com/armon/go-radix"
"github.com/getlantern/golog"
"github.com/getlantern/mtime"
)
// Rewrite changes an HTTP URL to rewrite.
type Rewrite func(url *url.URL) (string, bool)
type httpse struct {
log golog.Logger
initOnce sync.Once
wildcardTargets atomic.Value // *radix.Tree
plainTargets atomic.Value // map[string]*ruleset
stats *httpseStats
statsCh chan *timing
}
type httpseStats struct {
runs int64
totalTime int64
max int64
maxHost string
}
type timing struct {
host string
dur time.Duration
}
// Default returns a lazily-initialized Rewrite using the default rules
func Default() Rewrite {
h := newEmpty()
h.initAsync()
return h.rewrite
}
// Eager returns an eagerly-initialized Rewrite using the default rules
func Eager() Rewrite {
h := newEmpty()
h.init()
return h.rewrite
}
func newEmpty() *httpse {
h := &httpse{
log: golog.LoggerFor("httpse"),
stats: &httpseStats{},
statsCh: make(chan *timing, 100),
}
go h.readTimings()
h.wildcardTargets.Store(radix.New())
h.plainTargets.Store(make(map[string]*ruleset))
return h
}
func (h *httpse) init() {
d := newDeserializer()
plain, wildcard, err := d.newRulesets()
if err != nil {
return
}
h.plainTargets.Store(plain)
h.wildcardTargets.Store(wildcard)
}
func (h *httpse) initAsync() {
h.initOnce.Do(func() {
go h.init()
})
}
func (h *httpse) rewrite(url *url.URL) (string, bool) {
if url.Scheme != "http" {
return "", false
}
start := mtime.Now()
defer func() {
h.statsCh <- &timing{
dur: mtime.Now().Sub(start),
host: url.String(),
}
}()
if val, ok := h.plainTargets.Load().(map[string]*ruleset)[url.Host]; ok {
if r, hit := h.rewriteWithRuleset(url, val); hit {
return r, hit
}
}
// Check prefixes (with reversing the URL host)
if _, val, match := h.wildcardTargets.Load().(*radix.Tree).LongestPrefix(reverse(url.Host)); match {
if r, hit := h.rewriteWithRuleset(url, val.(*ruleset)); hit {
return r, hit
}
}
// Check suffixes last because there are far fewer suffix rules.
if _, val, match := h.wildcardTargets.Load().(*radix.Tree).LongestPrefix(url.Host); match {
return h.rewriteWithRuleset(url, val.(*ruleset))
}
return "", false
}
// rewriteWithRuleset converts the given URL to HTTPS if there is an associated
// rule for it.
func (h *httpse) rewriteWithRuleset(fullURL *url.URL, r *ruleset) (string, bool) {
url := fullURL.String()
for _, exclude := range r.exclusion {
if exclude.pattern.MatchString(url) {
return "", false
}
}
for _, rule := range r.rule {
if rule.from.MatchString(url) {
return rule.from.ReplaceAllString(url, rule.to), true
}
}
return "", false
}
func reverse(input string) string {
n := 0
runes := make([]rune, len(input)+1)
// Add a dot prefix to make sure we're only operating on subdomains
runes[0] = '.'
runes = runes[1:]
for _, r := range input {
runes[n] = r
n++
}
runes = runes[0:n]
// Reverse
for i := 0; i < n/2; i++ {
runes[i], runes[n-1-i] = runes[n-1-i], runes[i]
}
// Convert back to UTF-8.
return string(runes)
}
func (h *httpse) readTimings() {
for t := range h.statsCh {
h.addTiming(t)
}
}
func (h *httpse) addTiming(t *timing) {
ms := t.dur.Nanoseconds() / int64(time.Millisecond)
h.stats.runs++
h.stats.totalTime += ms
if ms > h.stats.max {
h.stats.max = ms
h.stats.maxHost = t.host
}
}