/
proxy.go
188 lines (156 loc) · 5.05 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
package proxy
import (
"fmt"
"io"
"net"
"strconv"
"sync"
"github.com/eko/monday/pkg/hostfile"
)
const (
// ProxyPortStart is the first port that will be allocated by the proxy component.
// Others will be incremented by 1 each time
ProxyPortStart = "9400"
)
type ProxyInterface interface {
Listen() error
Stop() error
AddProxyForward(name string, proxyForward *ProxyForward)
}
// Proxy represents the proxy component instance
type Proxy struct {
ProxyForwards map[string][]*ProxyForward
hostfile hostfile.HostfileInterface
listeners map[string]net.Listener
listening bool
addProxyForwardMux sync.Mutex
listenerMux sync.Mutex
latestPort string
attributedIPs map[string]net.IP
ipLastByte int
}
// NewProxy initializes a new proxy component instance
func NewProxy(hostfile hostfile.HostfileInterface) *Proxy {
return &Proxy{
ProxyForwards: make(map[string][]*ProxyForward, 0),
hostfile: hostfile,
listeners: make(map[string]net.Listener),
listening: true,
latestPort: ProxyPortStart,
attributedIPs: make(map[string]net.IP, 0),
ipLastByte: 1,
}
}
// Listen opens a TCP proxy for each ProxyForward instance
func (p *Proxy) Listen() error {
for name, pfs := range p.ProxyForwards {
for _, pf := range pfs {
if pf.LocalPort == "" {
// In case no local port is specified: don't handle connections
continue
}
key := fmt.Sprintf("%s_%s", name, pf.LocalPort)
// We already have a listening port
if _, ok := p.listeners[key]; ok {
return nil
}
fmt.Printf("🔌 Proxifying %s locally (%s:%s) <-> forwarding to %s:%s\n", pf.GetHostname(), pf.LocalIP, pf.LocalPort, pf.GetProxyHostname(), pf.ProxyPort)
go p.handleConnections(pf, key)
}
}
return nil
}
// Stop stops all currently active proxy listeners
func (p *Proxy) Stop() error {
p.listening = false
for name, listener := range p.listeners {
err := listener.Close()
if err != nil {
fmt.Printf("❌ An error has occured while stopping proxy listener '%s': %v\n", name, err)
}
}
return nil
}
func (p *Proxy) handleConnections(pf *ProxyForward, key string) {
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%s", pf.LocalIP, pf.LocalPort))
if err != nil {
fmt.Printf("❌ Could not create proxy listener for '%s:%s' (%s): %v\n", pf.LocalIP, pf.LocalPort, pf.GetHostname(), err)
}
p.listenerMux.Lock()
p.listeners[key] = listener
p.listenerMux.Unlock()
// Accept clients and proxify calls
for {
client, err := listener.Accept()
if !p.listening {
break
}
if err != nil {
fmt.Printf("❌ Could not accept client connection for '%s:%s' (%s): %v\n", pf.LocalIP, pf.LocalPort, pf.GetHostname(), err)
}
target, err := net.Dial("tcp", fmt.Sprintf("%s:%s", pf.GetProxyHostname(), pf.ProxyPort))
if err != nil {
fmt.Printf("❌ Error when dialing with forwarder for '%s:%s' (%s): %v\n", pf.LocalIP, pf.LocalPort, pf.GetHostname(), err)
}
go func() {
defer client.Close()
defer target.Close()
io.Copy(client, target)
}()
go func() {
defer client.Close()
defer target.Close()
io.Copy(target, client)
}()
}
}
// AddProxyForward creates a new ProxyForward instance and attributes an IP address and a proxy port to it
func (p *Proxy) AddProxyForward(name string, proxyForward *ProxyForward) {
p.addProxyForwardMux.Lock()
defer p.addProxyForwardMux.Unlock()
err := p.generateIP(proxyForward)
if err != nil {
fmt.Printf("❌ An error has occured while generating IP address for '%s': %v\n", proxyForward.Name, err)
}
if proxyForward.ProxyPort == "" {
p.generateProxyPort(proxyForward)
}
err = p.hostfile.AddHost(proxyForward.LocalIP, proxyForward.GetHostname())
if err != nil {
fmt.Printf("❌ An error has occured while trying to write host file for application '%s' (ip: %s): %v\n", proxyForward.Name, proxyForward.LocalIP, err)
}
if proxyForward.LocalPort != "" {
fmt.Printf("✅ Successfully mapped hostname '%s' with IP '%s' and port %s\n", proxyForward.GetHostname(), proxyForward.LocalIP, proxyForward.ProxyPort)
} else {
fmt.Printf("✅ Successfully mapped hostname '%s' with IP '%s'\n", proxyForward.GetHostname(), proxyForward.LocalIP)
}
if pfs, ok := p.ProxyForwards[name]; ok {
p.ProxyForwards[name] = append(pfs, proxyForward)
} else {
p.ProxyForwards[name] = append(pfs, proxyForward)
}
}
func (p *Proxy) generateIP(pf *ProxyForward) error {
// Create new listener on a dedicated IP address if the service
// does not already have an IP address attributed, elsewhere give the already
// attributed address
var localIP net.IP
var err error
if v, ok := p.attributedIPs[pf.Name]; ok {
localIP = v
} else {
localIP, err = generateIP(127, 1, 2, p.ipLastByte, pf.LocalPort)
p.ipLastByte = p.ipLastByte + 1
if err != nil {
return err
}
}
p.attributedIPs[pf.Name] = localIP
pf.SetLocalIP(localIP.String())
return nil
}
func (p *Proxy) generateProxyPort(proxyForward *ProxyForward) {
integerPort, _ := strconv.Atoi(p.latestPort)
p.latestPort = strconv.Itoa(integerPort + 1)
proxyForward.SetProxyPort(p.latestPort)
}