| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | |
| 10 | |
| 11 | func parseFlagP(s string) (string, error) { |
| 12 | split := strings.Split(s, ":") |
| 13 | switch len(split) { |
| 14 | case 1: |
| 15 | port, err := strconv.Atoi(split[0]) |
| 16 | if err != nil { |
| 17 | return "", fmt.Errorf("invalid port %q", split[0]) |
| 18 | } |
| 19 | return fmt.Sprintf("0.0.0.0:%d:localhost:%d", port, port), nil |
| 20 | case 2: |
| 21 | localPort, err := strconv.Atoi(split[0]) |
| 22 | if err != nil { |
| 23 | return "", fmt.Errorf("invalid port %q", split[0]) |
| 24 | } |
| 25 | remotePort, err := strconv.Atoi(split[1]) |
| 26 | if err != nil { |
| 27 | return "", fmt.Errorf("invalid port %q", split[1]) |
| 28 | } |
| 29 | return fmt.Sprintf("0.0.0.0:%d:localhost:%d", localPort, remotePort), nil |
| 30 | case 3: |
| 31 | localIP := split[0] |
| 32 | localPort, err := strconv.Atoi(split[1]) |
| 33 | if err != nil { |
| 34 | return "", fmt.Errorf("invalid port %q", split[1]) |
| 35 | } |
| 36 | remotePort, err := strconv.Atoi(split[2]) |
| 37 | if err != nil { |
| 38 | return "", fmt.Errorf("invalid port %q", split[2]) |
| 39 | } |
| 40 | return fmt.Sprintf("%s:%d:localhost:%d", localIP, localPort, remotePort), nil |
| 41 | } |
| 42 | return "", fmt.Errorf("cannot parse %q, should be [[LOCALIP:]LOCALPORT:]REMOTEPORT", s) |
| 43 | } |