-
Notifications
You must be signed in to change notification settings - Fork 51
/
markedconn.go
209 lines (169 loc) · 5.62 KB
/
markedconn.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
// +build linux windows
package markedconn
import (
"context"
"fmt"
"net"
"time"
"go.aporeto.io/trireme-lib/utils/netinterfaces"
"go.uber.org/zap"
)
// DialMarkedWithContext will dial a TCP connection to the provide address and mark the socket
// with the provided mark.
func DialMarkedWithContext(ctx context.Context, network string, addr string, platformData *PlatformData, mark int) (net.Conn, error) {
// platformData is for Windows
if platformData != nil && platformData.postConnectFunc != nil {
defer platformData.postConnectFunc(platformData.handle)
}
d := makeDialer(mark, platformData)
conn, err := d.DialContext(ctx, network, addr)
if err != nil {
zap.L().Error("Failed to dial to downstream node",
zap.Error(err),
zap.String("Address", addr),
zap.String("Network type", network),
)
}
return conn, err
}
// NewSocketListener will create a listener and mark the socket with the provided mark.
func NewSocketListener(ctx context.Context, port string, mark int) (net.Listener, error) {
listenerCfg := makeListenerConfig(mark)
listener, err := listenerCfg.Listen(ctx, "tcp", port)
if err != nil {
return nil, fmt.Errorf("Failed to create listener: %s", err)
}
return ProxiedListener{
netListener: listener,
mark: mark,
platformDataCtrl: NewPlatformDataControl(),
}, nil
}
// ProxiedConnection is a proxied connection where we can recover the
// original destination.
type ProxiedConnection struct {
originalIP net.IP
originalPort int
originalTCPConnection *net.TCPConn
platformData *PlatformData
}
// PlatformData is proxy/socket data (platform-specific)
type PlatformData struct {
handle uintptr
postConnectFunc func(fd uintptr)
}
// GetOriginalDestination sets the original destination of the connection.
func (p *ProxiedConnection) GetOriginalDestination() (net.IP, int) {
return p.originalIP, p.originalPort
}
// GetPlatformData gets the platform-specific socket data (needed for Windows)
func (p *ProxiedConnection) GetPlatformData() *PlatformData {
return p.platformData
}
// GetTCPConnection returns the TCP connection object.
func (p *ProxiedConnection) GetTCPConnection() *net.TCPConn {
return p.originalTCPConnection
}
// LocalAddr implements the corresponding method of net.Conn, but returns the original
// address.
func (p *ProxiedConnection) LocalAddr() net.Addr {
return &net.TCPAddr{
IP: p.originalIP,
Port: p.originalPort,
}
}
// RemoteAddr returns the remote address
func (p *ProxiedConnection) RemoteAddr() net.Addr {
return p.originalTCPConnection.RemoteAddr()
}
// Read reads data from the connection.
func (p *ProxiedConnection) Read(b []byte) (n int, err error) {
return p.originalTCPConnection.Read(b)
}
// Write writes data to the connection.
func (p *ProxiedConnection) Write(b []byte) (n int, err error) {
return p.originalTCPConnection.Write(b)
}
// Close closes the connection.
func (p *ProxiedConnection) Close() error {
return p.originalTCPConnection.Close()
}
// SetDeadline passes the read deadline to the original TCP connection.
func (p *ProxiedConnection) SetDeadline(t time.Time) error {
return p.originalTCPConnection.SetDeadline(t)
}
// SetReadDeadline implements the call by passing it to the original connection.
func (p *ProxiedConnection) SetReadDeadline(t time.Time) error {
return p.originalTCPConnection.SetReadDeadline(t)
}
// SetWriteDeadline implements the call by passing it to the original connection.
func (p *ProxiedConnection) SetWriteDeadline(t time.Time) error {
return p.originalTCPConnection.SetWriteDeadline(t)
}
// ProxiedListener is a proxied listener that uses proxied connections.
type ProxiedListener struct {
netListener net.Listener
mark int
platformDataCtrl *PlatformDataControl
}
type passFD interface {
Control(func(uintptr)) error
}
func getOriginalDestination(conn *net.TCPConn) (net.IP, int, *PlatformData, error) { // nolint interfacer
rawconn, err := conn.SyscallConn()
if err != nil {
return nil, 0, nil, err
}
localIPString, _, err := net.SplitHostPort(conn.LocalAddr().String())
if err != nil {
return nil, 0, nil, err
}
localIP := net.ParseIP(localIPString)
return getOriginalDestPlatform(rawconn, localIP.To4() != nil)
}
// Accept implements the accept method of the interface.
func (l ProxiedListener) Accept() (c net.Conn, err error) {
nc, err := l.netListener.Accept()
if err != nil {
return nil, err
}
tcpConn, ok := nc.(*net.TCPConn)
if !ok {
zap.L().Error("Received a non-TCP connection - this should never happen", zap.Error(err))
return nil, fmt.Errorf("Not a tcp connection - ignoring")
}
ip, port, platformData, err := getOriginalDestination(tcpConn)
if err != nil {
zap.L().Error("Failed to discover original destination - aborting", zap.Error(err))
return nil, err
}
l.platformDataCtrl.StorePlatformData(ip, port, platformData)
return &ProxiedConnection{
originalIP: ip,
originalPort: port,
originalTCPConnection: tcpConn,
platformData: platformData,
}, nil
}
// Addr implements the Addr method of net.Listener.
func (l ProxiedListener) Addr() net.Addr {
return l.netListener.Addr()
}
// Close implements the Close method of the net.Listener.
func (l ProxiedListener) Close() error {
return l.netListener.Close()
}
// GetInterfaces retrieves all the local interfaces.
func GetInterfaces() map[string]struct{} {
ipmap := map[string]struct{}{}
ifaces, err := netinterfaces.GetInterfacesInfo()
if err != nil {
zap.L().Error("Unable to get interfaces info", zap.Error(err))
}
for _, iface := range ifaces {
for _, ip := range iface.IPs {
ipmap[ip.String()] = struct{}{}
}
}
return ipmap
}