Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(RDGRS-662): added ID to CC execution and show the local ports #77

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
37 changes: 24 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
37 changes: 37 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}