2019-08-27 21:16:41 +00:00
|
|
|
//+build linux
|
|
|
|
|
|
|
|
package freeport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
const ephemeralPortRangeSysctlKey = "net.ipv4.ip_local_port_range"
|
|
|
|
|
|
|
|
var ephemeralPortRangePatt = regexp.MustCompile(`^\s*(\d+)\s+(\d+)\s*$`)
|
|
|
|
|
|
|
|
func getEphemeralPortRange() (int, int, error) {
|
2019-11-15 22:28:13 +00:00
|
|
|
cmd := exec.Command("/sbin/sysctl", "-n", ephemeralPortRangeSysctlKey)
|
2019-08-27 21:16:41 +00:00
|
|
|
out, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
val := string(out)
|
|
|
|
|
|
|
|
m := ephemeralPortRangePatt.FindStringSubmatch(val)
|
|
|
|
if m != nil {
|
|
|
|
min, err1 := strconv.Atoi(m[1])
|
|
|
|
max, err2 := strconv.Atoi(m[2])
|
|
|
|
|
|
|
|
if err1 == nil && err2 == nil {
|
|
|
|
return min, max, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, 0, fmt.Errorf("unexpected sysctl value %q for key %q", val, ephemeralPortRangeSysctlKey)
|
|
|
|
}
|