/
proxy.go
224 lines (193 loc) · 5.52 KB
/
proxy.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package app
import (
"context"
"crypto/tls"
"fmt"
"io"
"net"
"os"
"reflect"
"syscall"
"time"
"unsafe"
"golang.org/x/sys/unix"
)
// Copies data between a remote TCP network connection (possibly with TLS) and
// a local unix socket.
//
// The function will return if one of the following events occurs:
//
// - the other end of the remote network socket closes the connection
// - the other end of the local unix socket closes the connection
// - the context is cancelled
// - an error occurs when writing or reading data
//
// In case of errors, details are returned.
func proxy(ctx context.Context, remote net.Conn, local net.Conn, config *tls.Config) error {
tcp, err := tryExtractTCPConn(remote)
if err == nil {
if err := setKeepalive(tcp); err != nil {
return err
}
}
if config != nil {
if config.ClientCAs != nil {
remote = tls.Server(remote, config)
} else {
remote = tls.Client(remote, config)
}
}
remoteToLocal := make(chan error, 0)
localToRemote := make(chan error, 0)
// Start copying data back and forth until either the client or the
// server get closed or hit an error.
go func() {
_, err := io.Copy(local, remote)
remoteToLocal <- err
}()
go func() {
_, err := io.Copy(remote, local)
localToRemote <- err
}()
errs := make([]error, 2)
select {
case <-ctx.Done():
// Force closing, ignore errors.
remote.Close()
local.Close()
<-remoteToLocal
<-localToRemote
case err := <-remoteToLocal:
if err != nil {
errs[0] = fmt.Errorf("remote -> local: %v", err)
}
local.(*net.UnixConn).CloseRead()
if err := <-localToRemote; err != nil {
errs[1] = fmt.Errorf("local -> remote: %v", err)
}
remote.Close()
local.Close()
case err := <-localToRemote:
if err != nil {
errs[0] = fmt.Errorf("local -> remote: %v", err)
}
if tcp != nil {
tcp.CloseRead()
}
if err := <-remoteToLocal; err != nil {
errs[1] = fmt.Errorf("remote -> local: %v", err)
}
remote.Close()
local.Close()
}
if errs[0] != nil || errs[1] != nil {
return proxyError{first: errs[0], second: errs[1]}
}
return nil
}
// tryExtractTCPConn tries to extract the underlying net.TCPConn, potentially from a tls.Conn.
func tryExtractTCPConn(conn net.Conn) (*net.TCPConn, error) {
tcp, ok := conn.(*net.TCPConn)
if ok {
return tcp, nil
}
// Go doesn't currently expose the underlying TCP connection of a TLS connection, but we need it in order
// to set timeout properties on the connection. We use some reflect/unsafe magic to extract the private
// remote.conn field, which is indeed the underlying TCP connection.
tlsConn, ok := conn.(*tls.Conn)
if !ok {
return nil, fmt.Errorf("connection is not a tls.Conn")
}
field := reflect.ValueOf(tlsConn).Elem().FieldByName("conn")
field = reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem()
c := field.Interface()
tcpConn, ok := c.(*net.TCPConn)
if !ok {
return nil, fmt.Errorf("connection is not a net.TCPConn")
}
return tcpConn, nil
}
// Set TCP_USER_TIMEOUT and TCP keepalive with 3 seconds idle time, 3 seconds
// retry interval with at most 3 retries.
//
// See https://thenotexpert.com/golang-tcp-keepalive/.
func setKeepalive(conn *net.TCPConn) error {
err := conn.SetKeepAlive(true)
if err != nil {
return err
}
err = conn.SetKeepAlivePeriod(time.Second * 3)
if err != nil {
return err
}
raw, err := conn.SyscallConn()
if err != nil {
return err
}
raw.Control(
func(ptr uintptr) {
fd := int(ptr)
// Number of probes.
err = syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, _TCP_KEEPCNT, 3)
if err != nil {
return
}
// Wait time after an unsuccessful probe.
err = syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, _TCP_KEEPINTVL, 3)
if err != nil {
return
}
// Set TCP_USER_TIMEOUT option to limit the maximum amount of time in ms that transmitted data may remain
// unacknowledged before TCP will forcefully close the corresponding connection and return ETIMEDOUT to the
// application. This combined with the TCP keepalive options on the socket will ensure that should the
// remote side of the connection disappear abruptly that dqlite will detect this and close the socket quickly.
// Decreasing the user timeouts allows applications to "fail fast" if so desired. Otherwise it may take
// up to 20 minutes with the current system defaults in a normal WAN environment if there are packets in
// the send queue that will prevent the keepalive timer from working as the retransmission timers kick in.
// See https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=dca43c75e7e545694a9dd6288553f55c53e2a3a3
err = syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(30*time.Microsecond))
if err != nil {
return
}
})
return err
}
// Returns a pair of connected unix sockets.
func socketpair() (net.Conn, net.Conn, error) {
fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM, 0)
if err != nil {
return nil, nil, err
}
c1, err := fdToFileConn(fds[0])
if err != nil {
return nil, nil, err
}
c2, err := fdToFileConn(fds[1])
if err != nil {
c1.Close()
return nil, nil, err
}
return c1, c2, err
}
func fdToFileConn(fd int) (net.Conn, error) {
f := os.NewFile(uintptr(fd), "")
defer f.Close()
return net.FileConn(f)
}
type proxyError struct {
first error
second error
}
func (e proxyError) Error() string {
msg := ""
if e.first != nil {
msg += "first: " + e.first.Error()
}
if e.second != nil {
if e.first != nil {
msg += " "
}
msg += "second: " + e.second.Error()
}
return msg
}