Skip to content

Commit

Permalink
Merge pull request #50 from OutSystems/fix/RDGRS-53
Browse files Browse the repository at this point in the history
fix: add validation to prevent duplicated local ports
  • Loading branch information
dnlopes authored Feb 15, 2024
2 parents 29616a8 + 47032aa commit e2a4152
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

chclient "github.com/jpillora/chisel/client"
"github.com/jpillora/chisel/share/cos"
"github.com/jpillora/chisel/share/settings"
)

var (
Expand Down Expand Up @@ -140,7 +141,13 @@ func client(args []string) {
log.Fatalf("A server and least one remote is required")
}
config.Server = args[0]

if err := validateRemotes(args[1:]); err != nil {
log.Fatal(err)
}

config.Remotes = args[1:]

//default auth
if config.Auth == "" {
config.Auth = os.Getenv("AUTH")
Expand All @@ -167,3 +174,36 @@ func client(args []string) {
log.Fatal(err)
}
}

// validate the provided Remotes configuration is valid
func validateRemotes(remotes []string) error {
uniqueRemotes := []string{}

for _, newRemote := range remotes {

// iterate all remotes already in the unique list, if duplicate is found return error
for _, unique := range uniqueRemotes {
firstRemote, err := settings.DecodeRemote(unique)
if err != nil {
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)
}
}

uniqueRemotes = append(uniqueRemotes, newRemote)
}

return nil
}

func isDuplicatedRemote(first, second *settings.Remote) bool {
return first.LocalPort == second.LocalPort
}

0 comments on commit e2a4152

Please sign in to comment.