zrok/agent/autoListener.go

18 lines
340 B
Go
Raw Normal View History

2024-11-06 23:18:20 +01:00
package agent
import (
"fmt"
"net"
)
func AutoListener(address string, startPort, endPort uint16) (net.Listener, error) {
for i := startPort; i <= endPort; i++ {
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", address, i))
if err != nil {
continue
}
return l, nil
}
return nil, fmt.Errorf("no listener found in range")
}