-
Notifications
You must be signed in to change notification settings - Fork 13
/
dialer.go
169 lines (155 loc) · 4.4 KB
/
dialer.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
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
168
169
package util
import (
"bufio"
"context"
"encoding/base64"
"fmt"
"net"
"net/http"
"net/url"
"time"
"github.com/Axway/agent-sdk/pkg/util/log"
"golang.org/x/net/proxy"
)
const (
// DefaultKeepAliveInterval - default duration to send keep alive pings
DefaultKeepAliveInterval = 50 * time.Second
// DefaultKeepAliveTimeout - default keepalive timeout
DefaultKeepAliveTimeout = 10 * time.Second
)
// Dialer - interface for http dialer for proxy and single entry point
type Dialer interface {
// Dial - interface used by libbeat for tcp network dial
Dial(network string, addr string) (net.Conn, error)
// DialContext - interface used by http transport
DialContext(ctx context.Context, network string, addr string) (net.Conn, error)
// GetProxyScheme() string
GetProxyScheme() string
}
type dialer struct {
singleEntryHostMap map[string]string
proxyScheme string
proxyAddress string
userName string
password string
}
// NewDialer - creates a new dialer
func NewDialer(proxyURL *url.URL, singleEntryHostMap map[string]string) Dialer {
dialer := &dialer{
singleEntryHostMap: singleEntryHostMap,
}
if proxyURL != nil {
dialer.proxyScheme = proxyURL.Scheme
dialer.proxyAddress = proxyURL.Host
if user := proxyURL.User; user != nil {
dialer.userName = user.Username()
dialer.password, _ = user.Password()
}
}
if dialer.singleEntryHostMap == nil {
dialer.singleEntryHostMap = map[string]string{}
}
return dialer
}
// Dial- manages the connections to proxy and single entry point for tcp transports
func (d *dialer) Dial(network string, addr string) (net.Conn, error) {
conn, err := d.DialContext(context.Background(), network, addr)
if err == nil && len(d.singleEntryHostMap) > 0 && addr != conn.RemoteAddr().String() {
log.Tracef("routing the traffic for %s via %s", addr, conn.RemoteAddr().String())
}
return conn, err
}
// DialContext - manages the connections to proxy and single entry point
func (d *dialer) DialContext(ctx context.Context, network string, addr string) (net.Conn, error) {
originalAddr := addr
singleEntryHost, ok := d.singleEntryHostMap[addr]
if ok {
addr = singleEntryHost
}
if d.proxyAddress != "" {
switch d.proxyScheme {
case "socks5", "socks5h":
return d.socksConnect(network, originalAddr, singleEntryHost)
case "http", "https":
default:
return nil, fmt.Errorf("could not setup proxy, unsupported proxy scheme %s", d.proxyScheme)
}
addr = d.proxyAddress
}
conn, err := (&net.Dialer{
Timeout: DefaultKeepAliveTimeout,
KeepAlive: DefaultKeepAliveInterval,
DualStack: true}).DialContext(ctx, network, addr)
if err != nil {
return nil, err
}
if d.proxyAddress != "" {
switch d.proxyScheme {
case "http", "https":
err = d.httpConnect(ctx, conn, originalAddr, singleEntryHost)
if err != nil {
conn.Close()
return nil, err
}
}
}
return conn, nil
}
func (d *dialer) GetProxyScheme() string {
if d.proxyAddress != "" {
return d.proxyScheme
}
return ""
}
func (d *dialer) socksConnect(network, addr, singleEntryHost string) (net.Conn, error) {
var auth *proxy.Auth
if d.userName != "" {
auth = new(proxy.Auth)
auth.User = d.userName
if d.password != "" {
auth.Password = d.password
}
}
socksDialer, err := proxy.SOCKS5(network, d.proxyAddress, auth, nil)
if err != nil {
return nil, err
}
targetAddr := addr
if singleEntryHost != "" {
targetAddr = singleEntryHost
}
return socksDialer.Dial(network, targetAddr)
}
func (d *dialer) httpConnect(ctx context.Context, conn net.Conn, targetAddr, sniHost string) error {
req := d.createConnectRequest(ctx, targetAddr, sniHost)
if err := req.Write(conn); err != nil {
return err
}
r := bufio.NewReader(conn)
resp, err := http.ReadResponse(r, req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to connect proxy, status : %s", resp.Status)
}
return nil
}
func (d *dialer) createConnectRequest(ctx context.Context, targetAddress, sniHost string) *http.Request {
req := &http.Request{
Method: http.MethodConnect,
URL: &url.URL{Opaque: targetAddress},
Host: targetAddress,
}
if sniHost != "" {
req.URL = &url.URL{Opaque: sniHost}
}
if d.userName != "" {
token := base64.StdEncoding.EncodeToString([]byte(d.userName + ":" + d.password))
req.Header = map[string][]string{
"Proxy-Authorization": {"Basic " + token},
}
}
return req.WithContext(ctx)
}