-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
79 lines (70 loc) · 1.78 KB
/
http.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
package liltunnel
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"time"
"github.com/pkg/errors"
)
const (
flushPeriod = 10 * time.Second
)
// NewHTTPTunnler returns an HTTP only Tunneler. You can configure the HTTP
// tunneler to cache HTTP responses out to disk. Which can be _pretty_
// handy in the right scenario.
func NewHTTPTunnler(dialer Dialer, localPort string, remotePort string, cache bool, ttl time.Duration, serveStale bool, log logger) (Tunneler, error) {
h := &httpTunnel{
dialer: dialer,
localPort: localPort,
remotePort: remotePort,
log: log,
cache: cache,
cacheTTL: ttl,
cacheServeStale: serveStale,
}
return h, nil
}
type httpTunnel struct {
dialer Dialer
log logger
localPort string
remotePort string
cache bool
cacheTTL time.Duration
cacheServeStale bool
}
func (h *httpTunnel) Tunnel() error {
url, err := url.Parse(
fmt.Sprintf("http://localhost%v", h.localPort),
)
if err != nil {
return errors.Wrap(err, "could not parse url")
}
rp := httputil.NewSingleHostReverseProxy(url)
t := &http.Transport{
Proxy: http.ProxyFromEnvironment,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
DialContext: h.dialer.DialContext,
}
rp.Transport = t
// Cast ReverseProxy as an HTTP handler so we can wrap it with the cache
handler := http.Handler(rp)
if h.cache {
h.log.Println("initializing http cache")
c, err := newHTTPCache(
"",
h.cacheTTL,
h.cacheServeStale,
h.log,
)
if err != nil {
return err
}
handler = c.Handle(handler)
}
return http.ListenAndServe(h.localPort, handler)
}