From 3ae34f72af912201f07684589603f1da4d7e28a2 Mon Sep 17 00:00:00 2001 From: "Bruno Martins (bjm)" <59831763+bmartins-unit@users.noreply.github.com> Date: Tue, 3 Sep 2024 16:36:08 +0100 Subject: [PATCH] fix(RDGRS-662): added ID to CC execution and show the local ports (#77) * added ID to CC execution and show the local ports * new versions --- go.mod | 4 ++-- go.sum | 12 ++++++------ main.go | 37 ++++++++++++++++++++++++------------- main_test.go | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 main_test.go diff --git a/go.mod b/go.mod index 3dccd66..4732ef1 100644 --- a/go.mod +++ b/go.mod @@ -10,10 +10,10 @@ require ( github.com/jpillora/sizestr v1.0.0 // indirect golang.org/x/crypto v0.26.0 // indirect golang.org/x/net v0.28.0 // indirect - golang.org/x/sync v0.7.0 // indirect + golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.24.0 // indirect ) -replace github.com/jpillora/chisel => github.com/outsystems/chisel v1.9.2-0.20230908114230-bf0e1ad1c3e6 +replace github.com/jpillora/chisel => github.com/outsystems/chisel v1.9.2-os.4 go 1.22 diff --git a/go.sum b/go.sum index 484bc34..6d2c573 100644 --- a/go.sum +++ b/go.sum @@ -8,15 +8,15 @@ github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2E github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/jpillora/sizestr v1.0.0 h1:4tr0FLxs1Mtq3TnsLDV+GYUWG7Q26a6s+tV5Zfw2ygw= github.com/jpillora/sizestr v1.0.0/go.mod h1:bUhLv4ctkknatr6gR42qPxirmd5+ds1u7mzD+MZ33f0= -github.com/outsystems/chisel v1.9.2-0.20230908114230-bf0e1ad1c3e6 h1:+jPXx3ix1uEbwZ4jRz/kICYaYkfcTFZDYnqslBTcB28= -github.com/outsystems/chisel v1.9.2-0.20230908114230-bf0e1ad1c3e6/go.mod h1:QGQAxn4w6IbAHTGxDE/lD4dPUSU4pUzkH44skisW+ag= +github.com/outsystems/chisel v1.9.2-os.4 h1:PplhrOrFlj2d0M/lNuvswSPzAmdlPkx8OZly1l5dyPs= +github.com/outsystems/chisel v1.9.2-os.4/go.mod h1:H90bwoii5ME/1jRRxreLwd43gTmp03mJQa8xKHcCZW0= golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk= -golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4= +golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU= +golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk= diff --git a/main.go b/main.go index af0a5f7..3ccb67e 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,8 @@ import ( "strings" "time" + "math/rand" + chclient "github.com/jpillora/chisel/client" "github.com/jpillora/chisel/share/cos" "github.com/jpillora/chisel/share/settings" @@ -140,12 +142,15 @@ func client(args []string) { if len(args) < 2 { log.Fatalf("A server and least one remote is required") } - config.Server = args[0] - if err := validateRemotes(args[1:]); err != nil { + localPorts, err := validateRemotes(args[1:]) + if err != nil { log.Fatal(err) } + queryParams := generateQueryParameters(localPorts) + + config.Server = fmt.Sprintf("%s%s", args[0], queryParams) config.Remotes = args[1:] //default auth @@ -175,33 +180,39 @@ func client(args []string) { } } +func generateQueryParameters(localPorts string) string { + return fmt.Sprintf("?id=%v&ports=%v", rand.Intn(999999999-100000000)+100000000, localPorts) +} + // validate the provided Remotes configuration is valid -func validateRemotes(remotes []string) error { +func validateRemotes(remotes []string) (string, error) { uniqueRemotes := []string{} + localPorts := []string{} for _, newRemote := range remotes { + remote, err := settings.DecodeRemote(newRemote) + if err != nil { + return "", fmt.Errorf("failed to decode remote '%s': %s", newRemote, err) + } + // iterate all remotes already in the unique list, if duplicate is found return error for _, unique := range uniqueRemotes { - firstRemote, err := settings.DecodeRemote(unique) + validatedRemote, err := settings.DecodeRemote(unique) if err != nil { - return fmt.Errorf("failed to decode remote '%s': %s", unique, err) + return "", fmt.Errorf("failed to decode remote '%s': %s", unique, err) } - secondRemote, err := settings.DecodeRemote(newRemote) - if err != nil { - return fmt.Errorf("failed to decode remote '%s': %s", newRemote, err) - } - - if isDuplicatedRemote(firstRemote, secondRemote) { - return fmt.Errorf("invalid Remote configuration: local port '%s' is duplicated", secondRemote.LocalPort) + if isDuplicatedRemote(validatedRemote, remote) { + return "", fmt.Errorf("invalid Remote configuration: local port '%s' is duplicated", remote.LocalPort) } } uniqueRemotes = append(uniqueRemotes, newRemote) + localPorts = append(localPorts, remote.LocalPort) } - return nil + return strings.Join(localPorts, ","), nil } func isDuplicatedRemote(first, second *settings.Remote) bool { diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..c635603 --- /dev/null +++ b/main_test.go @@ -0,0 +1,37 @@ +package main + +import "testing" + +func Test_validateRemotes(t *testing.T) { + + tests := []struct { + name string + remotes []string + want string + wantErr bool + }{ + { + name: "success", + remotes: []string{"R:15800:localhost:7000", "R:15801:localhost:7001"}, + want: "15800,15801", + wantErr: false, + }, + { + name: "error", + remotes: []string{"R:15800:localhost:7000", "R:15800:localhost:7001"}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := validateRemotes(tt.remotes) + if (err != nil) != tt.wantErr { + t.Errorf("validateRemotes() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("validateRemotes() = %v, want %v", got, tt.want) + } + }) + } +}