-
Notifications
You must be signed in to change notification settings - Fork 51
/
connproc.go
228 lines (195 loc) · 4.89 KB
/
connproc.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
225
226
227
228
// +build linux
package connproc
import (
"context"
"fmt"
"net"
"os"
"sync"
"syscall"
"time"
"golang.org/x/sys/unix"
"go.aporeto.io/trireme-lib/controller/internal/enforcer/applicationproxy/markedconn"
"go.uber.org/zap"
)
// GetInterfaces retrieves all the local interfaces.
func GetInterfaces() map[string]struct{} {
ipmap := map[string]struct{}{}
ifaces, _ := net.Interfaces()
for _, intf := range ifaces {
addrs, _ := intf.Addrs()
for _, addr := range addrs {
ip, _, _ := net.ParseCIDR(addr.String())
if ip.To4() != nil {
ipmap[ip.String()] = struct{}{}
}
}
}
return ipmap
}
// Fd returns the Fd of a connection
func Fd(c net.Conn) (*os.File, int, error) {
var inTCP *net.TCPConn
switch c.(type) {
case *net.TCPConn:
inTCP = c.(*net.TCPConn)
case *markedconn.ProxiedConnection:
inTCP = c.(*markedconn.ProxiedConnection).GetTCPConnection()
default:
return nil, 0, fmt.Errorf("Unprocessable connection")
}
inFile, err := inTCP.File()
if err != nil {
return nil, 0, fmt.Errorf("Failed to open file: %s", err)
}
return inFile, int(inFile.Fd()), nil
}
// WriteMsg writes a message to the provided Fd
func WriteMsg(fd int, data []byte) error {
addr, err := syscall.Getpeername(fd)
if err != nil {
return fmt.Errorf("Cannot retrieve peer name %s %+v", err, addr)
}
return syscall.Sendto(fd, data, 0, addr)
}
// ReadMsg reads a message from the provided Fd
func ReadMsg(fd int) (int, []byte, error) {
msg := make([]byte, 2048)
n, _, err := syscall.Recvfrom(fd, msg, 0)
return n, msg, err
}
// Pipe proxies data bi-directionally between in and out.
func Pipe(ctx context.Context, inConn, outConn net.Conn) error {
inFile, inFd, err := Fd(inConn)
if err != nil {
return err
}
defer inFile.Close() // nolint
outFile, outFd, err := Fd(outConn)
if err != nil {
return err
}
defer outFile.Close() // nolint
tcpIn, err := tcpConnection(inConn)
if err != nil {
return err
}
tcpOut, err := tcpConnection(outConn)
if err != nil {
return err
}
if err := tcpIn.SetKeepAlive(true); err != nil {
return err
}
if err := tcpOut.SetKeepAlive(true); err != nil {
return err
}
if err := tcpIn.SetKeepAlivePeriod(5 * time.Second); err != nil {
return err
}
if err := tcpOut.SetKeepAlivePeriod(5 * time.Second); err != nil {
return err
}
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer func() {
if err := outConn.Close(); err != nil {
zap.L().Error("Failed to close in connection", zap.Error(err))
}
wg.Done()
}()
copyBytes(ctx, false, inFd, outFd, tcpIn, tcpOut)
}()
go func() {
defer func() {
if err := inConn.Close(); err != nil {
zap.L().Error("Failed to close out connection", zap.Error(err))
}
wg.Done()
}()
copyBytes(ctx, true, outFd, inFd, tcpOut, tcpIn)
}()
wg.Wait()
return nil
}
func tcpConnection(c net.Conn) (*net.TCPConn, error) {
switch c.(type) {
case *net.TCPConn:
return c.(*net.TCPConn), nil
case *markedconn.ProxiedConnection:
return c.(*markedconn.ProxiedConnection).GetTCPConnection(), nil
default:
return nil, fmt.Errorf("Uknown connection type")
}
}
func copyBytes(ctx context.Context, downstream bool, destFd, srcFd int, destConn, srcCon *net.TCPConn) {
var total int64
var nwrote int64
pipe := []int{0, 0}
err := syscall.Pipe2(pipe, syscall.O_CLOEXEC)
if err != nil {
syscall.Shutdown(destFd, syscall.SHUT_WR) // nolint errcheck
zap.L().Error("error creating splicing:", zap.Bool("Downstream", downstream), zap.Error(err))
return
}
defer func() {
if err = syscall.Shutdown(destFd, syscall.SHUT_WR); err != nil {
if er, ok := err.(syscall.Errno); ok {
if er != syscall.ENOTCONN {
zap.L().Warn("closing connection failed:", zap.Bool("Downstream", downstream), zap.Error(err))
}
}
}
if err = syscall.Close(pipe[0]); err != nil {
zap.L().Warn("Failed to close pipe", zap.Error(err))
}
if err = syscall.Close(pipe[1]); err != nil {
zap.L().Warn("Failed to close pipe", zap.Error(err))
}
}()
var nread int64
for {
select {
case <-ctx.Done():
break
default:
nread, err = syscall.Splice(srcFd, nil, pipe[1], nil, 2*8192, unix.SPLICE_F_NONBLOCK)
if err != nil {
if isTemporary(err) && !downstream {
continue
}
return
}
if nread == 0 {
return
}
for total = 0; total < nread; {
if nwrote, err = syscall.Splice(pipe[0], nil, destFd, nil, int(nread-total), 0); err != nil {
if !isTemporary(err) {
return
}
return
}
total += nwrote
}
}
}
}
func isTemporary(err error) bool {
switch t := err.(type) {
case net.Error:
if t.Timeout() {
fmt.Println("Timeout error")
return true
}
case syscall.Errno:
if t == syscall.ECONNRESET || t == syscall.ECONNABORTED || t == syscall.ENOTCONN || t == syscall.EPIPE {
return false
}
zap.L().Error("Connection error to destination", zap.Error(err))
default:
zap.L().Error("Connection terminated", zap.Error(err))
}
return false
}