Skip to content

Commit

Permalink
fixed go versioning. Pending issue with ui
Browse files Browse the repository at this point in the history
  • Loading branch information
RicYaben committed Jan 3, 2024
1 parent ecc8acd commit 6d13c90
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 130 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.18 AS builder
FROM golang:1.21 AS builder

# Set the working directory to golang working space
WORKDIR /riotpot
Expand Down Expand Up @@ -27,7 +27,7 @@ COPY build build/
ADD Makefile .
RUN make compile

FROM golang:1.18 AS release
FROM golang:1.21 AS release

#ENV GIN_MODE=release

Expand Down
6 changes: 3 additions & 3 deletions pkg/api/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func NewProxy(px proxy.Proxy) *GetProxy {
ID: px.GetID(),
Port: px.GetPort(),
Network: px.GetNetwork().String(),
Status: px.GetStatus().String(),
Status: px.IsRunning().String(),
Service: serv,
}
}
Expand Down Expand Up @@ -232,7 +232,7 @@ func changeProxyStatus(ctx *gin.Context) {
case utils.RunningStatus:
err = pe.Start()
case utils.StoppedStatus:
err = pe.Stop()
pe.Stop()
default:
err = fmt.Errorf("status not allowed")
}
Expand All @@ -243,7 +243,7 @@ func changeProxyStatus(ctx *gin.Context) {
}

// Serialize the status and send it as the response
ctx.JSON(http.StatusOK, gin.H{"status": pe.GetStatus().String()})
ctx.JSON(http.StatusOK, gin.H{"status": pe.IsRunning().String()})
}

// POST request to change the port of the proxy
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func newServiceAndProxy(ctx *gin.Context) {
ID: pe.GetID(),
Port: pe.GetPort(),
Network: pe.GetNetwork().String(),
Status: pe.GetStatus().String(),
Status: pe.IsRunning().String(),
Service: NewService(sv),
}

Expand Down
52 changes: 10 additions & 42 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package proxy

import (
"fmt"
"sync"

"github.com/google/uuid"
"github.com/riotpot/pkg/service"
"github.com/riotpot/pkg/utils"
Expand All @@ -13,13 +10,13 @@ import (
type Proxy interface {
// Start and stop
Start() error
Stop() error
Stop()

// Getters
GetID() string
GetPort() int
GetNetwork() utils.Network
GetStatus() utils.Status
IsRunning() utils.Status
GetService() service.Service

// Setters
Expand All @@ -42,7 +39,7 @@ type baseProxy struct {

// Create a channel to stop the proxy gracefully
// This channel is also used to guess if the proxy is running
stop chan struct{}
quit chan struct{}

// Pointer to the slice of middlewares for the proxies
// All the proxies should apply and share the same middlewares
Expand All @@ -52,54 +49,26 @@ type baseProxy struct {
// Service to proxy
service service.Service

// Waiting group for the server
wg sync.WaitGroup

// Generic listener
listener interface{ Close() error }
}

// Function to stop the proxy from runing
func (pe *baseProxy) Stop() (err error) {
// Stop the proxy if it is still alive
if pe.GetStatus() == utils.RunningStatus {
close(pe.stop)

if pe.listener != nil {
pe.listener.Close()
}

// Wait for all the connections and the server to stop
pe.wg.Wait()
return
}

err = fmt.Errorf("proxy not running")
return
func (pe *baseProxy) Stop() {
close(pe.quit)
}

// Simple function to check if the proxy is running
func (pe *baseProxy) GetStatus() (alive utils.Status) {
// When the proxy is instantiated, the stop channel is nil;
// therefore, the proxy is not running
if pe.stop == nil {
func (b *baseProxy) IsRunning() (alive utils.Status) {
if b.quit == nil {
return
}

// [7/4/2022] NOTE: The logic of this block is difficult to read.
// However, the select block will only give the default value when there is nothing
// to read from the channel while the channel is still open.
// When the channel is closed, the first case is not blocked, so we can not
// read "anything else" from the channel
select {
// Return if the channel is closed
case <-pe.stop:
// Return if the channel is open
case <-b.quit:
return
default:
alive = utils.RunningStatus
return utils.RunningStatus
}

return
}

func (pe *baseProxy) GetID() string {
Expand Down Expand Up @@ -140,6 +109,5 @@ func newProxy(port int, network utils.Network) (px *baseProxy) {
port: port,
network: network,
middlewares: Middlewares,
wg: sync.WaitGroup{},
}
}
80 changes: 43 additions & 37 deletions pkg/proxy/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,57 +26,63 @@ func (px *tcpProxy) Start() (err error) {
return fmt.Errorf("service not set")
}

// Set stop channel
px.quit = make(chan struct{})

// Get the listener or create a new one
listener, err := px.GetListener()
if err != nil {
return
}

// Set stop channel
px.stop = make(chan struct{})

// Add a waiting task
px.wg.Add(1)
var wg sync.WaitGroup
wg.Add(1)

go func() {
defer px.wg.Done()
defer wg.Done()

for {
// Accept the next connection
// This goes first as it is the method we have to check if the proxy is running
// There is no need to continue if it is not
client, err := listener.Accept()
if err != nil {
return
}
defer client.Close()

// Apply the middlewares to the connection before dialing the server
_, err = px.middlewares.Apply(client)
if err != nil {
select {
case <-px.quit:
return
default:
// Accept the next connection
// This goes first as it is the method we have to check if the proxy is running
// There is no need to continue if it is not
client, err := listener.Accept()
if err != nil {
return
}
defer client.Close()

// Apply the middlewares to the connection before dialing the server
_, err = px.middlewares.Apply(client)
if err != nil {
return
}

// Get a connection to the server for each new connection with the client
server, servErr := net.DialTimeout(utils.TCP.String(), px.service.GetAddress(), 1*time.Second)

// If there was an error, close the connection to the server and return
if servErr != nil {
return
}
defer server.Close()

// Add a waiting task
wg.Add(1)

go func() {
defer wg.Done()
// Handle the connection between the client and the server
// NOTE: The handlers will defer the connections
px.handle(client, server)
}()
}

// Get a connection to the server for each new connection with the client
server, servErr := net.DialTimeout(utils.TCP.String(), px.service.GetAddress(), 1*time.Second)

// If there was an error, close the connection to the server and return
if servErr != nil {
return
}
defer server.Close()

// Add a waiting task
px.wg.Add(1)

go func() {
// Handle the connection between the client and the server
// NOTE: The handlers will defer the connections
px.handle(client, server)

// Finish the task
px.wg.Done()
}()
}
}()

Expand All @@ -87,7 +93,7 @@ func (px *tcpProxy) GetListener() (listener net.Listener, err error) {
listener = px.listener

// Get the listener only
if listener == nil || px.GetStatus() != utils.RunningStatus {
if listener == nil || px.IsRunning() != utils.RunningStatus {
listener, err = px.NewListener()
if err != nil {
return
Expand Down
81 changes: 38 additions & 43 deletions pkg/proxy/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,63 +21,58 @@ func (px *udpProxy) Start() (err error) {
return
}

// Create a channel to stop the proxy
px.quit = make(chan struct{})

// Get the listener or create a new one
client, err := px.GetListener()
if err != nil {
return
}
defer client.Close()

// Create a channel to stop the proxy
px.stop = make(chan struct{})

// Add a waiting task
px.wg.Add(1)
var wg sync.WaitGroup
wg.Add(1)

srvAddr := net.UDPAddr{
Port: px.service.GetPort(),
}

for {
// Get a connection to the server for each new connection with the client
server, servErr := net.DialUDP(utils.UDP.String(), nil, &srvAddr)
// If there was an error, close the connection to the server and return
if servErr != nil {
return
go func() {
defer wg.Done()

for {
select {
case <-px.quit:
return

default:
// Get a connection to the server for each new connection with the client
server, servErr := net.DialUDP(utils.UDP.String(), nil, &srvAddr)
// If there was an error, close the connection to the server and return
if servErr != nil {
return
}
defer server.Close()

// Add a waiting task
wg.Add(1)

go func() {
defer wg.Done()
// TODO: Handle the middlewares! they only accept TCP connections
// Apply the middlewares to the connection
//udpProxy.middlewares.Apply(listener)

// Handle the connection between the client and the server
// NOTE: The handlers will defer the connections
px.handle(client, server)
}()
}
}
defer server.Close()

go func() {
// TODO: Handle the middlewares! they only accept TCP connections
// Apply the middlewares to the connection
//udpProxy.middlewares.Apply(listener)

// Handle the connection between the client and the server
// NOTE: The handlers will defer the connections
px.handle(client, server)

// Finish the task
px.wg.Done()
}()
}
}

// Function to stop the proxy from runing
func (px *udpProxy) Stop() (err error) {
// Stop the proxy if it is still alive
if px.GetStatus() != utils.StoppedStatus {
close(px.stop)

if px.listener != nil {
px.listener.Close()
}

// Wait for all the connections and the server to stop
px.wg.Wait()
return
}
}()

err = fmt.Errorf("proxy not running")
return
}

Expand All @@ -86,7 +81,7 @@ func (px *udpProxy) GetListener() (listener *net.UDPConn, err error) {
listener = px.listener

// Check if there is a listener
if listener == nil || px.GetStatus() != utils.RunningStatus {
if listener == nil || px.IsRunning() != utils.RunningStatus {
// Get the address of the UDP server
addr := net.UDPAddr{
Port: px.service.GetPort(),
Expand Down
4 changes: 2 additions & 2 deletions test/internal/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ func TestStopProxy(t *testing.T) {
pr.Start()

// Give the proxy some time to start
alive := pr.GetStatus()
alive := pr.IsRunning()
assert.Equal(alive, true, "The proxy is running")

// Stop the service
pr.Stop()
alive = pr.GetStatus()
alive = pr.IsRunning()
assert.Equal(alive, false, "The proxy is stop")
}

Expand Down

0 comments on commit 6d13c90

Please sign in to comment.