@@ -12,6 +12,8 @@ import (
1212 "strings"
1313 "time"
1414
15+ "math/rand"
16+
1517 chclient "github.com/jpillora/chisel/client"
1618 "github.com/jpillora/chisel/share/cos"
1719 "github.com/jpillora/chisel/share/settings"
@@ -140,12 +142,15 @@ func client(args []string) {
140142 if len (args ) < 2 {
141143 log .Fatalf ("A server and least one remote is required" )
142144 }
143- config .Server = args [0 ]
144145
145- if err := validateRemotes (args [1 :]); err != nil {
146+ localPorts , err := validateRemotes (args [1 :])
147+ if err != nil {
146148 log .Fatal (err )
147149 }
148150
151+ queryParams := generateQueryParameters (localPorts )
152+
153+ config .Server = fmt .Sprintf ("%s%s" , args [0 ], queryParams )
149154 config .Remotes = args [1 :]
150155
151156 //default auth
@@ -175,33 +180,39 @@ func client(args []string) {
175180 }
176181}
177182
183+ func generateQueryParameters (localPorts string ) string {
184+ return fmt .Sprintf ("?id=%v&ports=%v" , rand .Intn (999999999 - 100000000 )+ 100000000 , localPorts )
185+ }
186+
178187// validate the provided Remotes configuration is valid
179- func validateRemotes (remotes []string ) error {
188+ func validateRemotes (remotes []string ) ( string , error ) {
180189 uniqueRemotes := []string {}
190+ localPorts := []string {}
181191
182192 for _ , newRemote := range remotes {
183193
194+ remote , err := settings .DecodeRemote (newRemote )
195+ if err != nil {
196+ return "" , fmt .Errorf ("failed to decode remote '%s': %s" , newRemote , err )
197+ }
198+
184199 // iterate all remotes already in the unique list, if duplicate is found return error
185200 for _ , unique := range uniqueRemotes {
186- firstRemote , err := settings .DecodeRemote (unique )
201+ validatedRemote , err := settings .DecodeRemote (unique )
187202 if err != nil {
188- return fmt .Errorf ("failed to decode remote '%s': %s" , unique , err )
203+ return "" , fmt .Errorf ("failed to decode remote '%s': %s" , unique , err )
189204 }
190205
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 )
206+ if isDuplicatedRemote (validatedRemote , remote ) {
207+ return "" , fmt .Errorf ("invalid Remote configuration: local port '%s' is duplicated" , remote .LocalPort )
198208 }
199209 }
200210
201211 uniqueRemotes = append (uniqueRemotes , newRemote )
212+ localPorts = append (localPorts , remote .LocalPort )
202213 }
203214
204- return nil
215+ return strings . Join ( localPorts , "," ), nil
205216}
206217
207218func isDuplicatedRemote (first , second * settings.Remote ) bool {
0 commit comments