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: add validation to prevent duplicated local ports #50

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Changes from 1 commit
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
41 changes: 40 additions & 1 deletion 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]
config.Remotes = args[1:]

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

config.Remotes = args[1:]
}

//default auth
if config.Auth == "" {
config.Auth = os.Getenv("AUTH")
Expand All @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
}