github.com/metacubex/mihomo@v1.18.5/transport/hysteria/utils/misc.go (about)

     1  package utils
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"strconv"
     7  )
     8  
     9  func SplitHostPort(hostport string) (string, uint16, error) {
    10  	host, port, err := net.SplitHostPort(hostport)
    11  	if err != nil {
    12  		return "", 0, err
    13  	}
    14  	portUint, err := strconv.ParseUint(port, 10, 16)
    15  	if err != nil {
    16  		return "", 0, err
    17  	}
    18  	return host, uint16(portUint), err
    19  }
    20  
    21  func ParseIPZone(s string) (net.IP, string) {
    22  	s, zone := splitHostZone(s)
    23  	return net.ParseIP(s), zone
    24  }
    25  
    26  func splitHostZone(s string) (host, zone string) {
    27  	if i := last(s, '%'); i > 0 {
    28  		host, zone = s[:i], s[i+1:]
    29  	} else {
    30  		host = s
    31  	}
    32  	return
    33  }
    34  
    35  func last(s string, b byte) int {
    36  	i := len(s)
    37  	for i--; i >= 0; i-- {
    38  		if s[i] == b {
    39  			break
    40  		}
    41  	}
    42  	return i
    43  }
    44  
    45  type PacketDialer interface {
    46  	ListenPacket(rAddr net.Addr) (net.PacketConn, error)
    47  	Context() context.Context
    48  	RemoteAddr(host string) (net.Addr, error)
    49  }