func (sc *ServiceCommand) waitForListeningPorts(ports []int, cancel <-chan struct{}, command *exec.Cmd) error { for true { time.Sleep(100 * time.Millisecond) select { case <-cancel: return nil default: } var matchedPorts = make(map[int]struct{}) connections, err := net.Connections("all") if err != nil { return errors.WithStack(err) } for _, connection := range connections { if connection.Status == "LISTEN" { matchedPorts[int(connection.Laddr.Port)] = struct{}{} } } allMatched := true for _, port := range ports { if _, ok := matchedPorts[port]; !ok { allMatched = false } } if allMatched { return nil } } return errors.New("exited check loop unexpectedly") }
func (sc *ServiceCommand) waitForAnyPort(cancel <-chan struct{}, command *exec.Cmd) error { for true { time.Sleep(100 * time.Millisecond) select { case <-cancel: return nil default: } connections, err := net.Connections("all") if err != nil { return errors.WithStack(err) } proc, err := process.NewProcess(int32(command.Process.Pid)) if err != nil { return errors.WithStack(err) } if hasPort(proc, connections) { return nil } } return errors.New("exited check loop unexpectedly") }
func (sc *ServiceCommand) areAnyListeningPortsOpen(ports []int) (bool, error) { var matchedPorts = make(map[int]struct{}) for _, port := range ports { matchedPorts[port] = struct{}{} } connections, err := net.Connections("all") if err != nil { return false, errors.WithStack(err) } for _, connection := range connections { if connection.Status == "LISTEN" { if _, ok := matchedPorts[int(connection.Laddr.Port)]; ok { return true, nil } } } return false, nil }
func (sc *ServiceConfig) doGetPorts(proc *process.Process) ([]string, error) { var err error if len(connectionsCache) == 0 { connectionsCache, err = net.Connections("all") if err != nil { return nil, errors.WithStack(err) } } var ports []string var knownPorts = make(map[int]struct{}) if sc.LaunchChecks != nil { for _, port := range sc.LaunchChecks.Ports { knownPorts[port] = struct{}{} } } for _, connection := range connectionsCache { if connection.Status == "LISTEN" { if _, ok := knownPorts[int(connection.Laddr.Port)]; connection.Pid == proc.Pid && !ok { ports = append(ports, strconv.Itoa(int(connection.Laddr.Port))) } } } children, err := proc.Children() // This will error out if the process has finished or has no children if err != nil { return ports, nil } for _, child := range children { childPorts, err := sc.doGetPorts(child) if err == nil { ports = append(ports, childPorts...) } } return ports, nil }
func (s *systemPS) NetConnections() ([]net.ConnectionStat, error) { return net.Connections("all") }