2017-04-17 18:08:24 -04:00
|
|
|
package nat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ParsePortRange parses and validates the specified string as a port-range (8000-9000)
|
|
|
|
func ParsePortRange(ports string) (uint64, uint64, error) {
|
|
|
|
if ports == "" {
|
2023-11-10 16:32:30 -05:00
|
|
|
return 0, 0, fmt.Errorf("empty string specified for ports")
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
if !strings.Contains(ports, "-") {
|
|
|
|
start, err := strconv.ParseUint(ports, 10, 16)
|
|
|
|
end := start
|
|
|
|
return start, end, err
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(ports, "-")
|
|
|
|
start, err := strconv.ParseUint(parts[0], 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
end, err := strconv.ParseUint(parts[1], 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
if end < start {
|
2023-11-10 16:32:30 -05:00
|
|
|
return 0, 0, fmt.Errorf("invalid range specified for port: %s", ports)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
return start, end, nil
|
|
|
|
}
|