-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
258 lines (225 loc) · 8.77 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// BSD 3-Clause License
//
// Copyright (c) 2024, Xendit
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package main
import (
"context"
"fmt"
"net"
"sync"
)
type namedChaoticListener struct {
name string
listener *ChaoticListener
events chan ListenerEvent
cancelEventForwarding context.CancelFunc
stopChan chan struct{}
}
type ProxyEvent interface {
}
// A chaotic proxy manages a collection of chaotic listeners, each of which listens on a specific address and forwards
// connections to a target address. The proxy can be configured with a new set of listeners, and will start, stop, or
// update existing listeners as needed. The proxy will emit events when listeners are started, stopped, or encounter
// errors.
type ChaoticProxy struct {
listeners map[string]namedChaoticListener
events chan ProxyEvent
lock sync.Mutex
}
type NamedListenerStartedEvent struct {
Name string
Listener *ChaoticListener
}
type NamedListenerFailedEvent struct {
Name string
Listener *ChaoticListener
Error error
}
type NamedListenerStoppedEvent struct {
Name string
Listener *ChaoticListener
}
type NamedListenerNewConnectionEvent struct {
Name string
Listener *ChaoticListener
ConnectionName string
Connection *Connection
}
type NamedListenerConnectionClosedEvent struct {
Name string
Listener *ChaoticListener
ConnectionName string
Connection *Connection
}
type NamedListenerConnectionFailedEvent struct {
Name string
Listener *ChaoticListener
ConnectionName string
Connection *Connection
Error error
}
type NamedListenerNewConnectionErrorEvent struct {
Name string
Listener *ChaoticListener
Error error
}
type NamedListenerConfigUpdatedEvent struct {
Name string
Listener *ChaoticListener
}
// Create a new chaotic proxy that emits events on the given channel. The initial state of the proxy is empty, and
// listeners must be added by calling ApplyConfig. This function returns immediately.
func NewChaoticProxy(eventChannel chan ProxyEvent) *ChaoticProxy {
proxy := &ChaoticProxy{
listeners: make(map[string]namedChaoticListener),
events: eventChannel,
}
return proxy
}
// Apply a new configuration to the proxy. The proxy will start, stop, or update listeners as needed to match the new
// configuration. The function returns immediately.
func (p *ChaoticProxy) ApplyConfig(config Config) error {
p.lock.Lock()
defer p.lock.Unlock()
// Keep track of all our existing listeners so we can remove any that are no longer needed.
toRemove := make(map[string]struct{})
for name := range p.listeners {
toRemove[name] = struct{}{}
}
// Go through the new configuration and start, stop, or update listeners as needed.
for _, listenerConfig := range config.Listeners {
// We don't need to remove this listener, since it's still in the configuration.
delete(toRemove, listenerConfig.Name)
existing, exists := p.listeners[listenerConfig.Name]
if exists {
// Update the listener if the configuration has changed.
existingConfig := existing.listener.GetConfig()
if existingConfig != listenerConfig {
if existingConfig.ListenAddress != listenerConfig.ListenAddress {
// We need to recreate the listener if the address has changed.
name := existing.name
// Stop the existing listener.
_ = existing.listener.Close()
p.listeners[name].cancelEventForwarding()
<-p.listeners[name].stopChan
delete(p.listeners, name)
p.events <- NamedListenerStoppedEvent{Name: name, Listener: p.listeners[name].listener}
// Pretend the listener doesn't exist so we can start a new one.
exists = false
} else {
existing.listener.SetConfig(listenerConfig)
p.events <- NamedListenerConfigUpdatedEvent{Name: listenerConfig.Name, Listener: existing.listener}
}
}
}
if !exists {
// Start a new listener. First create the net.Listener.
netListener, netListenerErr := net.Listen("tcp", listenerConfig.ListenAddress)
if netListenerErr != nil {
return fmt.Errorf("failed to listen on %v: %w", listenerConfig.ListenAddress, netListenerErr)
}
// All is good, start the listener.
events := make(chan ListenerEvent, 100)
listener := NewChaoticListener(listenerConfig, netListener, events)
ctx, cancel := context.WithCancel(context.Background())
stopChan := make(chan struct{})
// Process events asynchronously. We'll forward them to the proxy's event channel.
go func() {
defer close(stopChan)
for {
select {
case event := <-events:
switch e := event.(type) {
case NewConnectionEvent:
p.events <- NamedListenerNewConnectionEvent{
Name: listenerConfig.Name, Listener: listener, ConnectionName: e.Name, Connection: e.Connection}
case ConnectionClosedEvent:
p.events <- NamedListenerConnectionClosedEvent{
Name: listenerConfig.Name, Listener: listener, ConnectionName: e.Name, Connection: e.Connection}
case ConnectionFailedEvent:
p.events <- NamedListenerConnectionFailedEvent{
Name: listenerConfig.Name, Listener: listener, ConnectionName: e.Name, Connection: e.Connection, Error: e.Error}
case NewConnectionErrorEvent:
p.events <- NamedListenerNewConnectionErrorEvent{
Name: listenerConfig.Name, Listener: listener, Error: e.Error}
case ListenerFailedEvent:
p.events <- NamedListenerFailedEvent{
Name: listenerConfig.Name, Listener: listener, Error: e.Error}
}
case <-ctx.Done():
if len(events) == 0 {
return
}
}
}
}()
// Add the listener to the proxy, and report that it has started.
p.listeners[listenerConfig.Name] = namedChaoticListener{
name: listenerConfig.Name,
listener: listener,
events: events,
cancelEventForwarding: cancel,
stopChan: stopChan,
}
p.events <- NamedListenerStartedEvent{Name: listenerConfig.Name, Listener: listener}
}
}
// So by now, whatever is left in toRemove is no longer in the configuration. We should remove them.
for name := range toRemove {
_ = p.listeners[name].listener.Close()
p.listeners[name].cancelEventForwarding()
<-p.listeners[name].stopChan
delete(p.listeners, name)
p.events <- NamedListenerStoppedEvent{Name: name, Listener: p.listeners[name].listener}
}
return nil
}
// Get a map of all the listeners currently managed by the proxy. The map is indexed by listener name.
// The map is a copy of the proxy's internal state, so any further configuration changes will not be reflected in the
// map.
func (p *ChaoticProxy) Listeners() map[string]*ChaoticListener {
p.lock.Lock()
defer p.lock.Unlock()
listeners := make(map[string]*ChaoticListener)
for name, namedListener := range p.listeners {
listeners[name] = namedListener.listener
}
return listeners
}
// Close the proxy and all its listeners. This will stop all listeners and emit events for each listener that is stopped.
func (p *ChaoticProxy) Close() {
p.lock.Lock()
defer p.lock.Unlock()
for _, namedListener := range p.listeners {
_ = namedListener.listener.Close()
namedListener.cancelEventForwarding()
<-namedListener.stopChan
p.events <- NamedListenerStoppedEvent{Name: namedListener.name, Listener: namedListener.listener}
}
p.listeners = make(map[string]namedChaoticListener)
}