ports-mapping flag now reimplemented as a StringSliceFlag and renamed to port-map.

This commit is contained in:
onyx-and-iris 2026-02-18 14:32:38 +00:00
parent 775979bfc8
commit 9be4344144

View File

@ -61,32 +61,47 @@ func main() {
Usage: "Target host address",
Sources: cli.EnvVars("Q3RCON_TARGET_HOST"),
},
&cli.StringFlag{
Name: "ports-mapping",
Usage: "Proxy and target ports (proxy:target)",
Sources: cli.EnvVars("Q3RCON_PORTS_MAPPING"),
&cli.StringSliceFlag{
Name: "port-map",
Usage: "Ports mapping in the format proxyPort:targetPort (e.g., 27950:27960)",
Sources: cli.EnvVars("Q3RCON_PORT_MAP"),
Required: true,
Action: func(_ context.Context, _ *cli.Command, v string) error {
// Validate the ports mapping
for mapping := range strings.SplitSeq(v, ";") {
ports := strings.Split(mapping, ":")
if len(ports) != 2 {
return fmt.Errorf("invalid ports mapping: %s", mapping)
Action: func(_ context.Context, cmd *cli.Command, v []string) error {
// Validate the ports mapping format and values
for _, mapping := range v {
src, dst := func(m string) (string, string) {
parts := strings.Split(m, ":")
if len(parts) != 2 {
return "", ""
}
proxyPort, err := strconv.Atoi(ports[0])
if err != nil || proxyPort < 1 || proxyPort > 65535 {
return fmt.Errorf("invalid proxy port: %s", ports[0])
}
targetPort, err := strconv.Atoi(ports[1])
if err != nil || targetPort < 1 || targetPort > 65535 {
return fmt.Errorf("invalid target port: %s", ports[1])
}
if proxyPort == targetPort {
return parts[0], parts[1]
}(mapping)
if src == "" || dst == "" {
return fmt.Errorf(
"proxy and target ports cannot be the same: %s",
"invalid ports mapping: %s (expected format: proxyPort:targetPort)",
mapping,
)
}
n, err := strconv.Atoi(src)
if err != nil || n <= 0 || n > 65535 {
return fmt.Errorf("invalid proxy port: %s", src)
}
n, err = strconv.Atoi(dst)
if err != nil || n <= 0 || n > 65535 {
return fmt.Errorf("invalid target port: %s", dst)
}
if src == dst {
return fmt.Errorf(
"proxy port and target port cannot be the same: %s",
src,
)
}
log.Debugf(
"Validated ports mapping: proxy port %s -> target port %s",
src,
dst,
)
}
return nil
},
@ -115,7 +130,7 @@ func main() {
Action: func(_ context.Context, cmd *cli.Command) error {
errChan := make(chan error)
for mapping := range strings.SplitSeq(cmd.String("ports-mapping"), ";") {
for _, mapping := range cmd.StringSlice("port-map") {
cfg := proxyConfig{
proxyHost: cmd.String("proxy-host"),
targetHost: cmd.String("target-host"),