github.com/psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/quic/gquic-go/internal/utils/host.go (about) 1 package utils 2 3 import ( 4 "net/url" 5 "strings" 6 ) 7 8 // HostnameFromAddr determines the hostname in an address string 9 func HostnameFromAddr(addr string) (string, error) { 10 p, err := url.Parse(addr) 11 if err != nil { 12 return "", err 13 } 14 h := p.Host 15 16 // copied from https://golang.org/src/net/http/transport.go 17 if hasPort(h) { 18 h = h[:strings.LastIndex(h, ":")] 19 } 20 21 return h, nil 22 } 23 24 // copied from https://golang.org/src/net/http/http.go 25 func hasPort(s string) bool { 26 return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") 27 }