mirror of
https://github.com/crazy-max/diun.git
synced 2024-12-23 03:48:00 +00:00
34 lines
748 B
Go
34 lines
748 B
Go
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 == "" {
|
|
return 0, 0, fmt.Errorf("empty string specified for ports")
|
|
}
|
|
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 {
|
|
return 0, 0, fmt.Errorf("invalid range specified for port: %s", ports)
|
|
}
|
|
return start, end, nil
|
|
}
|