/
fronted.go
40 lines (34 loc) · 1 KB
/
fronted.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
package proxied
import (
"net/http"
"time"
"github.com/getlantern/errors"
"github.com/getlantern/fronted"
)
const DefaultMasqueradeTimeout = 5 * time.Minute
// Fronted creates an http.RoundTripper that proxies request using domain
// fronting.
//
// Leave masqueradeTimeout as 0 to use a default value.
func Fronted(masqueradeTimeout time.Duration) http.RoundTripper {
if masqueradeTimeout == 0 {
masqueradeTimeout = DefaultMasqueradeTimeout
}
return frontedRoundTripper{masqueradeTimeout: masqueradeTimeout}
}
type frontedRoundTripper struct {
masqueradeTimeout time.Duration
}
// Use a wrapper for fronted.NewDirect to avoid blocking
// `dualFetcher.RoundTrip` when fronted is not yet available, especially when
// the application is starting up
func (f frontedRoundTripper) RoundTrip(
req *http.Request,
) (*http.Response, error) {
rt, ok := fronted.NewDirect(f.masqueradeTimeout)
if !ok {
return nil, errors.New("Unable to obtain direct fronter")
}
changeUserAgent(req)
return rt.RoundTrip(req)
}