Skip to content

Commit 3fe06df

Browse files
committed
Update tunnel.go
1 parent 746260c commit 3fe06df

1 file changed

Lines changed: 34 additions & 4 deletions

File tree

mobilegonc/tunnel.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,40 @@ func StartP2PLinkAgent(password string, useUDP bool, extraArgs string, cb Callba
7171
return start(args, cb, "linkagent"), nil
7272
}
7373

74+
// splitExtraArgs tokenizes a command-line string shell-style: whitespace
75+
// separates tokens, but single or double quotes group a run (including spaces)
76+
// into one argument, with the quote characters removed. Adjacent quoted and
77+
// unquoted runs join (a"b c"d -> "ab cd"). Backslash escaping is not supported;
78+
// use quotes for values that contain spaces, e.g. -x "aaa bbb ccc".
7479
func splitExtraArgs(extraArgs string) []string {
75-
fields := strings.Fields(extraArgs)
76-
if len(fields) == 0 {
77-
return nil
80+
var args []string
81+
var current strings.Builder
82+
inToken := false
83+
var quote rune // 0 when not inside quotes, otherwise '\'' or '"'
84+
for _, r := range extraArgs {
85+
switch {
86+
case quote != 0:
87+
if r == quote {
88+
quote = 0
89+
} else {
90+
current.WriteRune(r)
91+
}
92+
case r == '\'' || r == '"':
93+
quote = r
94+
inToken = true
95+
case r == ' ' || r == '\t' || r == '\n' || r == '\r':
96+
if inToken {
97+
args = append(args, current.String())
98+
current.Reset()
99+
inToken = false
100+
}
101+
default:
102+
current.WriteRune(r)
103+
inToken = true
104+
}
105+
}
106+
if inToken {
107+
args = append(args, current.String())
78108
}
79-
return fields
109+
return args
80110
}

0 commit comments

Comments
 (0)