-
Notifications
You must be signed in to change notification settings - Fork 19
/
upstream.go
58 lines (48 loc) · 1.01 KB
/
upstream.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
package h2s
import (
"bufio"
"crypto/tls"
"errors"
"net"
"net/http"
"net/url"
)
func (s *Server) dialUpstream() (conn net.Conn, u *internalUpstream, err error) {
ok := false
for tried := 0; tried <= s.retries; tried++ {
u, ok = <-s.next
if !ok {
err = errors.New("h2s is already closed")
return
}
if u.tlsConfig != nil {
conn, err = tls.DialWithDialer(s.dialer, "tcp", u.address, u.tlsConfig)
} else {
conn, err = s.dialer.Dial("tcp", u.address)
}
if err == nil {
return
}
}
err = errors.New("max retry exceeded: " + err.Error())
return
}
func (s *Server) handshakeUpstream(conn net.Conn, u *internalUpstream, target string) error {
req := &http.Request{
Method: "CONNECT",
URL: &url.URL{Opaque: target},
Host: target,
Header: u.header,
}
if err := req.Write(conn); err != nil {
return err
}
res, err := http.ReadResponse(bufio.NewReader(conn), req)
if err != nil {
return err
}
if res.StatusCode != 200 {
return errors.New(res.Status)
}
return nil
}