Repository navigation
Expand file tree
/
Copy pathconn_impl.go
More file actions
167 lines (145 loc) · 4.01 KB
/
Copy pathconn_impl.go
File metadata and controls
167 lines (145 loc) · 4.01 KB
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
163
164
165
166
167
package enproxy
import (
"bufio"
"fmt"
"io"
"net/http"
"time"
"code.google.com/p/go-uuid/uuid"
"github.com/getlantern/idletiming"
)
// Connect opens a connection to the proxy and starts processing writes and
// reads to this Conn.
func (c *Conn) Connect() error {
c.id = uuid.NewRandom().String()
c.initDefaults()
c.makeChannels()
c.markActive()
c.initRequestStrategy()
go c.processWrites()
go c.processReads()
// Dial proxy
proxyConn, err := c.dialProxy()
if err != nil {
return fmt.Errorf("Unable to dial proxy: %s", err)
}
go c.processRequests(proxyConn)
return nil
}
func (c *Conn) initDefaults() {
if c.Config.FlushTimeout == 0 {
c.Config.FlushTimeout = defaultWriteFlushTimeout
}
if c.Config.IdleTimeout == 0 {
c.Config.IdleTimeout = defaultIdleTimeoutClient
}
}
func (c *Conn) makeChannels() {
c.initialResponseCh = make(chan hostWithResponse)
c.writeRequestsCh = make(chan []byte)
c.writeResponsesCh = make(chan rwResponse)
c.stopWriteCh = make(chan interface{}, closeChannelDepth)
c.readRequestsCh = make(chan []byte)
c.readResponsesCh = make(chan rwResponse)
c.stopReadCh = make(chan interface{}, closeChannelDepth)
c.requestOutCh = make(chan *request)
c.requestFinishedCh = make(chan error)
c.stopRequestCh = make(chan interface{}, closeChannelDepth)
}
func (c *Conn) initRequestStrategy() {
if c.Config.BufferRequests {
c.rs = &bufferingRequestStrategy{
c: c,
}
} else {
c.rs = &streamingRequestStrategy{
c: c,
}
}
}
func (c *Conn) dialProxy() (*connInfo, error) {
conn, err := c.Config.DialProxy(c.Addr)
if err != nil {
return nil, fmt.Errorf("Unable to dial proxy: %s", err)
}
proxyConn := &connInfo{
bufReader: bufio.NewReader(conn),
}
proxyConn.conn = idletiming.Conn(conn, c.Config.IdleTimeout, func() {
// When the underlying connection times out, mark the connInfo closed
proxyConn.closedMutex.Lock()
defer proxyConn.closedMutex.Unlock()
proxyConn.closed = true
})
return proxyConn, nil
}
func (c *Conn) redialProxyIfNecessary(proxyConn *connInfo) (*connInfo, error) {
proxyConn.closedMutex.Lock()
defer proxyConn.closedMutex.Unlock()
if proxyConn.closed || proxyConn.conn.TimesOutIn() < oneSecond {
proxyConn.conn.Close()
return c.dialProxy()
} else {
return proxyConn, nil
}
}
func (c *Conn) doRequest(proxyConn *connInfo, host string, op string, request *request) (resp *http.Response, err error) {
var body io.Reader
if request != nil {
body = request.body
}
req, err := c.Config.NewRequest(host, "POST", body)
if err != nil {
err = fmt.Errorf("Unable to construct request to proxy: %s", err)
return
}
req.Header.Set(X_ENPROXY_OP, op)
// Always send our connection id
req.Header.Set(X_ENPROXY_ID, c.id)
// Always send the address that we're trying to reach
req.Header.Set(X_ENPROXY_DEST_ADDR, c.Addr)
req.Header.Set("Content-type", "application/octet-stream")
if request != nil && request.length > 0 {
// Force identity encoding to appeas CDNs like Fastly that can't
// handle chunked encoding on requests
req.TransferEncoding = []string{"identity"}
req.ContentLength = int64(request.length)
} else {
req.ContentLength = 0
}
err = req.Write(proxyConn.conn)
if err != nil {
err = fmt.Errorf("Error sending request to proxy: %s", err)
return
}
resp, err = http.ReadResponse(proxyConn.bufReader, req)
if err != nil {
err = fmt.Errorf("Error reading response from proxy: %s", err)
return
}
// Check response status
responseOK := resp.StatusCode >= 200 && resp.StatusCode < 300
if !responseOK {
err = fmt.Errorf("Bad response status for read: %s", resp.Status)
resp.Body.Close()
resp = nil
}
return
}
func (c *Conn) markActive() {
c.lastActivityMutex.Lock()
defer c.lastActivityMutex.Unlock()
c.lastActivityTime = time.Now()
}
func (c *Conn) isIdle() bool {
c.lastActivityMutex.RLock()
defer c.lastActivityMutex.RUnlock()
timeSinceLastActivity := time.Now().Sub(c.lastActivityTime)
return timeSinceLastActivity > c.Config.IdleTimeout
}
type closer struct {
io.Reader
}
func (r *closer) Close() error {
return nil
}