mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-06-25 18:17:12 +08:00
Conflicts: Gopkg.lock pkg/appsrv/appsrv.go pkg/cloudcommon/options.go pkg/compute/models/hosts.go pkg/compute/models/quotas.go pkg/compute/service/service.go pkg/mcclient/mcclient.go
45 lines
748 B
Go
45 lines
748 B
Go
package missinggo
|
|
|
|
import (
|
|
"net"
|
|
"strconv"
|
|
)
|
|
|
|
// Extracts the port as an integer from an address string.
|
|
func AddrPort(addr net.Addr) int {
|
|
switch raw := addr.(type) {
|
|
case *net.UDPAddr:
|
|
return raw.Port
|
|
case *net.TCPAddr:
|
|
return raw.Port
|
|
default:
|
|
_, port, err := net.SplitHostPort(addr.String())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
i64, err := strconv.ParseInt(port, 0, 0)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return int(i64)
|
|
}
|
|
}
|
|
|
|
func AddrIP(addr net.Addr) net.IP {
|
|
if addr == nil {
|
|
return nil
|
|
}
|
|
switch raw := addr.(type) {
|
|
case *net.UDPAddr:
|
|
return raw.IP
|
|
case *net.TCPAddr:
|
|
return raw.IP
|
|
default:
|
|
host, _, err := net.SplitHostPort(addr.String())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return net.ParseIP(host)
|
|
}
|
|
}
|