Skip to content

Commit 2fcd013

Browse files
committed
Fix proxies to set the listeners properly
1 parent d79ae26 commit 2fcd013

4 files changed

Lines changed: 23 additions & 2 deletions

File tree

internal/proxy/manager.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type ProxyManager interface {
2424

2525
// Methods for proxies the using ID field
2626
GetProxy(id string) (Proxy, error)
27+
SetProxy(pe Proxy) (Proxy, error)
2728
DeleteProxy(id string) error
2829

2930
// Wrapper method to find a proxy using the port and protocol
@@ -81,6 +82,23 @@ func (pm *ProxyManagerItem) GetProxy(id string) (pe Proxy, err error) {
8182
return
8283
}
8384

85+
func (pm *ProxyManagerItem) SetProxy(px Proxy) (pe Proxy, err error) {
86+
// Get all the proxies registered
87+
proxies := pm.GetProxies()
88+
89+
for ind, proxy := range proxies {
90+
if proxy.GetID() == px.GetID() {
91+
// Replace the index of the proxy with the new one
92+
proxies[ind] = px
93+
pm.proxies = proxies
94+
return
95+
}
96+
}
97+
98+
err = fmt.Errorf("proxy not found")
99+
return
100+
}
101+
84102
// Delete a proxy from teh registered list using the ID
85103
func (pm *ProxyManagerItem) DeleteProxy(id string) (err error) {
86104
// Get all the proxies registered

internal/proxy/proxy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ type AbstractProxy struct {
5757
wg sync.WaitGroup
5858

5959
// Generic listener
60-
listener interface{ Close() }
60+
listener interface{ Close() error }
6161
}
6262

6363
// Function to stop the proxy from runing
6464
func (pe *AbstractProxy) Stop() (err error) {
6565
// Stop the proxy if it is still alive
66-
if pe.GetStatus() != globals.StoppedStatus {
66+
if pe.GetStatus() == globals.RunningStatus {
6767
close(pe.stop)
6868
pe.listener.Close()
6969
// Wait for all the connections and the server to stop

internal/proxy/tcpproxy.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func (tcpProxy *TCPProxy) Start() (err error) {
2828

2929
// Get the listener or create a new one
3030
listener, err := tcpProxy.GetListener()
31+
3132
if err != nil {
3233
return
3334
}
@@ -98,6 +99,7 @@ func (tcpProxy *TCPProxy) GetListener() (listener net.Listener, err error) {
9899

99100
func (tcpProxy *TCPProxy) NewListener() (listener net.Listener, err error) {
100101
listener, err = net.Listen(tcpProxy.GetNetwork().String(), fmt.Sprintf(":%d", tcpProxy.GetPort()))
102+
tcpProxy.AbstractProxy.listener = listener
101103
return
102104
}
103105

internal/proxy/udpproxy.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func (udpProxy *UDPProxy) GetListener() (listener *net.UDPConn, err error) {
9393
return
9494
}
9595
udpProxy.listener = listener
96+
udpProxy.AbstractProxy.listener = listener
9697
}
9798

9899
return

0 commit comments

Comments
 (0)