mirror of
https://github.com/go-i2p/go-sam-go.git
synced 2025-07-01 05:21:59 -04:00
60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
![]() |
package common
|
||
|
|
||
|
import (
|
||
|
"math/rand"
|
||
|
"net"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
"github.com/sirupsen/logrus"
|
||
|
)
|
||
|
|
||
|
func IgnorePortError(err error) error {
|
||
|
if err == nil {
|
||
|
return nil
|
||
|
}
|
||
|
if strings.Contains(err.Error(), "missing port in address") {
|
||
|
log.Debug("Ignoring 'missing port in address' error")
|
||
|
err = nil
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func SplitHostPort(hostport string) (string, string, error) {
|
||
|
host, port, err := net.SplitHostPort(hostport)
|
||
|
if err != nil {
|
||
|
if IgnorePortError(err) == nil {
|
||
|
log.WithField("host", hostport).Debug("Using full string as host, port set to 0")
|
||
|
host = hostport
|
||
|
port = "0"
|
||
|
}
|
||
|
}
|
||
|
log.WithFields(logrus.Fields{
|
||
|
"host": host,
|
||
|
"port": port,
|
||
|
}).Debug("Split host and port")
|
||
|
return host, port, nil
|
||
|
}
|
||
|
|
||
|
func RandPort() string {
|
||
|
for {
|
||
|
s := rand.NewSource(time.Now().UnixNano())
|
||
|
r := rand.New(s)
|
||
|
p := r.Intn(55534) + 10000
|
||
|
port := strconv.Itoa(p)
|
||
|
if l, e := net.Listen("tcp", net.JoinHostPort("localhost", port)); e != nil {
|
||
|
continue
|
||
|
} else {
|
||
|
defer l.Close()
|
||
|
if l, e := net.Listen("udp", net.JoinHostPort("localhost", port)); e != nil {
|
||
|
continue
|
||
|
} else {
|
||
|
defer l.Close()
|
||
|
return strconv.Itoa(l.Addr().(*net.UDPAddr).Port)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|