forked from Coccodrillo/apns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpconnectproxy.go
89 lines (77 loc) · 1.72 KB
/
httpconnectproxy.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
80
81
82
83
84
85
86
87
88
89
package apns
import (
"bufio"
"fmt"
"net"
"net/http"
"net/url"
"golang.org/x/net/proxy"
)
// httpConnectProxy is a HTTP/HTTPS connect proxy.
type httpConnectProxy struct {
host string
haveAuth bool
username string
password string
forward proxy.Dialer
}
func (s *httpConnectProxy) Dial(network, addr string) (net.Conn, error) {
// Dial and create the https client connection.
c, err := s.forward.Dial("tcp", s.host)
if err != nil {
return nil, err
}
// HACK. http.ReadRequest also does this.
reqURL, err := url.Parse("http://" + addr)
if err != nil {
c.Close()
return nil, err
}
reqURL.Scheme = ""
req, err := http.NewRequest("CONNECT", reqURL.String(), nil)
if err != nil {
c.Close()
return nil, err
}
req.Close = false
if s.haveAuth {
req.SetBasicAuth(s.username, s.password)
}
req.Header.Set("User-Agent", "Powerby Gota")
err = req.Write(c)
if err != nil {
c.Close()
return nil, err
}
resp, err := http.ReadResponse(bufio.NewReader(c), req)
if err != nil {
// TODO close resp body ?
resp.Body.Close()
c.Close()
return nil, err
}
resp.Body.Close()
if resp.StatusCode != 200 {
c.Close()
err = fmt.Errorf("Connect server using proxy error, StatusCode [%d]", resp.StatusCode)
return nil, err
}
return c, nil
}
func FromURL(u *url.URL, forward proxy.Dialer) (proxy.Dialer, error) {
return proxy.FromURL(u, forward)
}
func FromEnvironment() proxy.Dialer {
return proxy.FromEnvironment()
}
func newHTTPConnectProxy(uri *url.URL, forward proxy.Dialer) (proxy.Dialer, error) {
s := new(httpConnectProxy)
s.host = uri.Host
s.forward = forward
if uri.User != nil {
s.haveAuth = true
s.username = uri.User.Username()
s.password, _ = uri.User.Password()
}
return s, nil
}