-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
80 lines (67 loc) · 1.36 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
80
package main
import (
"bufio"
"fmt"
"net"
"net/http"
"net/url"
"golang.org/x/net/proxy"
)
type httpProxy struct {
host string
username string
password string
forward proxy.Dialer
}
func newHTTPProxy(dst *url.URL, forward proxy.Dialer) (proxy.Dialer, error) {
s := new(httpProxy)
s.host = dst.Host
s.forward = forward
if dst.User != nil {
s.username = dst.User.Username()
s.password, _ = dst.User.Password()
}
return s, nil
}
func (p *httpProxy) Dial(network, addr string) (net.Conn, error) {
reqURL, err := url.Parse("http://" + addr)
if err != nil {
return nil, err
}
reqURL.Scheme = ""
req, err := http.NewRequest("CONNECT", reqURL.String(), nil)
if err != nil {
return nil, err
}
req.Close = false
if p.username != "" {
req.SetBasicAuth(p.username, p.password)
}
req.Header.Set("User-Agent", "smssh")
conn, err := p.forward.Dial("tcp", p.host)
if err != nil {
return nil, err
}
err = req.Write(conn)
if err != nil {
return nil, err
}
defer func() {
if err != nil {
conn.Close()
}
}()
resp, err := http.ReadResponse(bufio.NewReader(conn), req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
err = fmt.Errorf("unexpected proxy reponse status: %d", resp.StatusCode)
return nil, err
}
return conn, nil
}
func init() {
proxy.RegisterDialerType("http", newHTTPProxy)
}