fix: find free port for torrent

This commit is contained in:
Richard Ramos 2023-11-22 14:50:19 -04:00 committed by richΛrd
parent 40c9ba82a5
commit d198ac63fc
1 changed files with 34 additions and 31 deletions

View File

@ -513,39 +513,42 @@ func (m *Manager) getTCPandUDPport(portNumber int) (int, error) {
// Find free port
for i := 0; i < 10; i++ {
port := func() int {
tcpAddr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort("localhost", "0"))
if err != nil {
m.logger.Warn("unable to resolve tcp addr: %v", zap.Error(err))
continue
return 0
}
tcpListener, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
tcpListener.Close()
m.logger.Warn("unable to listen on addr", zap.Stringer("addr", tcpAddr), zap.Error(err))
continue
return 0
}
defer tcpListener.Close()
port := tcpListener.Addr().(*net.TCPAddr).Port
tcpListener.Close()
udpAddr, err := net.ResolveUDPAddr("udp", net.JoinHostPort("localhost", fmt.Sprintf("%d", port)))
if err != nil {
m.logger.Warn("unable to resolve udp addr: %v", zap.Error(err))
continue
return 0
}
udpListener, err := net.ListenUDP("udp", udpAddr)
if err != nil {
udpListener.Close()
m.logger.Warn("unable to listen on addr", zap.Stringer("addr", udpAddr), zap.Error(err))
continue
return 0
}
defer udpListener.Close()
udpListener.Close()
return port
}()
if port != 0 {
return port, nil
}
}
return 0, fmt.Errorf("no free port found")
}