2022-07-03 18:22:56 +08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2022-11-27 11:09:56 +08:00
|
|
|
"context"
|
2022-07-03 18:22:56 +08:00
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SplitHostPort(hostport string) (string, uint16, error) {
|
|
|
|
host, port, err := net.SplitHostPort(hostport)
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, err
|
|
|
|
}
|
|
|
|
portUint, err := strconv.ParseUint(port, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, err
|
|
|
|
}
|
|
|
|
return host, uint16(portUint), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseIPZone(s string) (net.IP, string) {
|
|
|
|
s, zone := splitHostZone(s)
|
|
|
|
return net.ParseIP(s), zone
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitHostZone(s string) (host, zone string) {
|
|
|
|
if i := last(s, '%'); i > 0 {
|
|
|
|
host, zone = s[:i], s[i+1:]
|
|
|
|
} else {
|
|
|
|
host = s
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func last(s string, b byte) int {
|
|
|
|
i := len(s)
|
|
|
|
for i--; i >= 0; i-- {
|
|
|
|
if s[i] == b {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
2022-11-27 11:09:56 +08:00
|
|
|
|
|
|
|
type PacketDialer interface {
|
2022-12-11 13:41:44 +08:00
|
|
|
ListenPacket(rAddr net.Addr) (net.PacketConn, error)
|
2022-11-27 11:09:56 +08:00
|
|
|
Context() context.Context
|
|
|
|
RemoteAddr(host string) (net.Addr, error)
|
|
|
|
}
|