From bec65c5e140f7c1d4ae199d5c137c83fa8e98f1c Mon Sep 17 00:00:00 2001 From: David Lopes Date: Thu, 15 Feb 2024 10:58:51 +0000 Subject: [PATCH 1/2] fix: add validation to prevent duplicated remote ports --- main.go | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index e49f61e..39b7004 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( chclient "github.com/jpillora/chisel/client" "github.com/jpillora/chisel/share/cos" + "github.com/jpillora/chisel/share/settings" ) var ( @@ -140,7 +141,13 @@ func client(args []string) { log.Fatalf("A server and least one remote is required") } config.Server = args[0] - config.Remotes = args[1:] + + if err := validateRemotes(args[1:]); err != nil { + log.Fatal(err) + } else { + config.Remotes = args[1:] + } + //default auth if config.Auth == "" { config.Auth = os.Getenv("AUTH") @@ -167,3 +174,35 @@ 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: remote port '%s' is duplicated", secondRemote.RemotePort) + } + } + + uniqueRemotes = append(uniqueRemotes, newRemote) + } + + return nil +} + +func isDuplicatedRemote(first, second *settings.Remote) bool { + return first.RemotePort == second.RemotePort +} From 47032aa5c80645fb54627f52e9ac89aceb816e97 Mon Sep 17 00:00:00 2001 From: David Lopes Date: Thu, 15 Feb 2024 15:29:05 +0000 Subject: [PATCH 2/2] address PR feedback --- main.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 39b7004..af0a5f7 100644 --- a/main.go +++ b/main.go @@ -144,10 +144,10 @@ func client(args []string) { if err := validateRemotes(args[1:]); err != nil { log.Fatal(err) - } else { - config.Remotes = args[1:] } + config.Remotes = args[1:] + //default auth if config.Auth == "" { config.Auth = os.Getenv("AUTH") @@ -180,6 +180,7 @@ 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) @@ -193,7 +194,7 @@ func validateRemotes(remotes []string) error { } if isDuplicatedRemote(firstRemote, secondRemote) { - return fmt.Errorf("invalid Remote configuration: remote port '%s' is duplicated", secondRemote.RemotePort) + return fmt.Errorf("invalid Remote configuration: local port '%s' is duplicated", secondRemote.LocalPort) } } @@ -204,5 +205,5 @@ func validateRemotes(remotes []string) error { } func isDuplicatedRemote(first, second *settings.Remote) bool { - return first.RemotePort == second.RemotePort + return first.LocalPort == second.LocalPort }