Repository navigation
Expand file tree
/
Copy pathdialer.go
More file actions
85 lines (74 loc) · 2.22 KB
/
Copy pathdialer.go
File metadata and controls
85 lines (74 loc) · 2.22 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
package chained
import (
"bufio"
"fmt"
"net"
"net/http"
"strings"
)
// Dialer is an implementation of proxy.Dialer that proxies traffic via an
// upstream server proxy. Its Dial function uses DialServer to dial the server
// proxy and then issues a CONNECT request to instruct the server to connect to
// the destination at the specified network and addr.
type Dialer struct {
// DialServer: function that dials the upstream server proxy
DialServer func() (net.Conn, error)
// OnRequest: optional function that gets called on every CONNECT request to
// the server and is allowed to modify the http.Request before it passes to
// the server.
OnRequest func(req *http.Request)
}
// Dial implements the method from proxy.Dialer
func (d *Dialer) Dial(network, addr string) (net.Conn, error) {
conn, err := d.DialServer()
if err != nil {
return nil, fmt.Errorf("Unable to dial server: %s", err)
}
err = d.sendCONNECT(network, addr, conn)
if err != nil {
conn.Close()
return nil, err
}
return conn, nil
}
// Close implements the method from proxy.Dialer
func (d *Dialer) Close() error {
return nil
}
func (d *Dialer) sendCONNECT(network, addr string, conn net.Conn) error {
if !strings.Contains(network, "tcp") {
return fmt.Errorf("%s connections are not supported, only tcp is supported", network)
}
req, err := buildCONNECTRequest(addr, d.OnRequest)
if err != nil {
return fmt.Errorf("Unable to construct CONNECT request: %s", err)
}
err = req.Write(conn)
if err != nil {
return fmt.Errorf("Unable to write CONNECT request: %s", err)
}
r := bufio.NewReader(conn)
err = checkCONNECTResponse(r, req)
return err
}
func buildCONNECTRequest(addr string, onRequest func(req *http.Request)) (*http.Request, error) {
req, err := http.NewRequest(CONNECT, addr, nil)
if err != nil {
return nil, err
}
req.Host = addr
if onRequest != nil {
onRequest(req)
}
return req, nil
}
func checkCONNECTResponse(r *bufio.Reader, req *http.Request) error {
resp, err := http.ReadResponse(r, req)
if err != nil {
return fmt.Errorf("Error reading CONNECT response: %s", err)
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return fmt.Errorf("Bad status code on CONNECT response: %d", resp.StatusCode)
}
return nil
}