-
Notifications
You must be signed in to change notification settings - Fork 3
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: add validation to prevent duplicated local ports #50
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't we create a list/slice with ports and use the "unique" package and check the size ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For future proof scenarios, in case we want to extend the validations, having it behind a method is easier. |
||
// 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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.