Skip to content

Commit e2a4152

Browse files
authored
Merge pull request #50 from OutSystems/fix/RDGRS-53
fix: add validation to prevent duplicated local ports
2 parents 29616a8 + 47032aa commit e2a4152

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

main.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
chclient "github.com/jpillora/chisel/client"
1616
"github.com/jpillora/chisel/share/cos"
17+
"github.com/jpillora/chisel/share/settings"
1718
)
1819

1920
var (
@@ -140,7 +141,13 @@ func client(args []string) {
140141
log.Fatalf("A server and least one remote is required")
141142
}
142143
config.Server = args[0]
144+
145+
if err := validateRemotes(args[1:]); err != nil {
146+
log.Fatal(err)
147+
}
148+
143149
config.Remotes = args[1:]
150+
144151
//default auth
145152
if config.Auth == "" {
146153
config.Auth = os.Getenv("AUTH")
@@ -167,3 +174,36 @@ func client(args []string) {
167174
log.Fatal(err)
168175
}
169176
}
177+
178+
// validate the provided Remotes configuration is valid
179+
func validateRemotes(remotes []string) error {
180+
uniqueRemotes := []string{}
181+
182+
for _, newRemote := range remotes {
183+
184+
// iterate all remotes already in the unique list, if duplicate is found return error
185+
for _, unique := range uniqueRemotes {
186+
firstRemote, err := settings.DecodeRemote(unique)
187+
if err != nil {
188+
return fmt.Errorf("failed to decode remote '%s': %s", unique, err)
189+
}
190+
191+
secondRemote, err := settings.DecodeRemote(newRemote)
192+
if err != nil {
193+
return fmt.Errorf("failed to decode remote '%s': %s", newRemote, err)
194+
}
195+
196+
if isDuplicatedRemote(firstRemote, secondRemote) {
197+
return fmt.Errorf("invalid Remote configuration: local port '%s' is duplicated", secondRemote.LocalPort)
198+
}
199+
}
200+
201+
uniqueRemotes = append(uniqueRemotes, newRemote)
202+
}
203+
204+
return nil
205+
}
206+
207+
func isDuplicatedRemote(first, second *settings.Remote) bool {
208+
return first.LocalPort == second.LocalPort
209+
}

0 commit comments

Comments
 (0)