Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 14 additions & 1 deletion src/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,19 @@ func shouldExitForUnixSendCode(goos string, codeFlagSet, classicInsecureMode boo
return goos != "windows" && codeFlagSet && !classicInsecureMode && envSecret == ""
}

// parseRelayPorts splits a comma-separated --ports value, trimming whitespace
// around each entry and dropping empties. This keeps "9009, 9010," working the
// same as "9009,9010" instead of producing invalid port strings like " 9010".
func parseRelayPorts(portsFlag string) []string {
var ports []string
for _, p := range strings.Split(portsFlag, ",") {
if p = strings.TrimSpace(p); p != "" {
ports = append(ports, p)
}
}
return ports
}

func send(c *cli.Context) (err error) {
setDebugLevel(c)
comm.Socks5Proxy = c.String("socks5")
Expand Down Expand Up @@ -793,7 +806,7 @@ func relay(c *cli.Context) (err error) {
var ports []string

if c.IsSet("ports") {
ports = strings.Split(c.String("ports"), ",")
ports = parseRelayPorts(c.String("ports"))
} else {
portString := c.Int("port")
if portString == 0 {
Expand Down
48 changes: 47 additions & 1 deletion src/cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,52 @@
package cli

import "testing"
import (
"reflect"
"testing"
)

func TestParseRelayPorts(t *testing.T) {
tests := []struct {
name string
in string
want []string
}{
{
name: "plain comma separated",
in: "9009,9010,9011",
want: []string{"9009", "9010", "9011"},
},
{
name: "spaces after commas are trimmed",
in: "9009, 9010, 9011",
want: []string{"9009", "9010", "9011"},
},
{
name: "surrounding and trailing empties are dropped",
in: " 9009 ,, 9010 ,",
want: []string{"9009", "9010"},
},
{
name: "single port",
in: "9009",
want: []string{"9009"},
},
{
name: "empty string yields no ports",
in: "",
want: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := parseRelayPorts(tt.in)
if !reflect.DeepEqual(got, tt.want) {
t.Fatalf("parseRelayPorts(%q) = %#v, want %#v", tt.in, got, tt.want)
}
})
}
}

func TestResolveSendSharedSecret(t *testing.T) {
t.Run("uses env secret", func(t *testing.T) {
Expand Down