2021-11-16 18:04:01 +00:00
|
|
|
//go:build linux
|
|
|
|
// +build linux
|
2019-08-27 21:16:41 +00:00
|
|
|
|
|
|
|
package freeport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-10-24 16:36:13 +00:00
|
|
|
"io/ioutil"
|
2019-08-27 21:16:41 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2020-10-24 16:36:13 +00:00
|
|
|
const ephemeralPortRangeProcFile = "/proc/sys/net/ipv4/ip_local_port_range"
|
2019-08-27 21:16:41 +00:00
|
|
|
|
|
|
|
var ephemeralPortRangePatt = regexp.MustCompile(`^\s*(\d+)\s+(\d+)\s*$`)
|
|
|
|
|
|
|
|
func getEphemeralPortRange() (int, int, error) {
|
2020-10-24 16:36:13 +00:00
|
|
|
out, err := ioutil.ReadFile(ephemeralPortRangeProcFile)
|
2019-08-27 21:16:41 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 16:36:13 +00:00
|
|
|
return 0, 0, fmt.Errorf("unexpected sysctl value %q for key %q", val, ephemeralPortRangeProcFile)
|
2019-08-27 21:16:41 +00:00
|
|
|
}
|