implement port+prefix matching

This commit is contained in:
Joe Kralicky 2024-06-20 19:28:53 -04:00
parent e18c04216e
commit a5a0cf4ba8
No known key found for this signature in database
GPG key ID: 75C4875F34A9FB79
11 changed files with 131 additions and 52 deletions

View file

@ -149,6 +149,59 @@ func GetDomainsForURL(u *url.URL, includeDefaultPort bool) []string {
return []string{u.Hostname(), net.JoinHostPort(u.Hostname(), defaultPort)}
}
func AllDomainsForURL(u *url.URL, includeDefaultPort bool) func(yield func(string, string) bool) {
return func(yield func(string, string) bool) {
if u == nil {
return
}
// tcp+https://ssh.example.com:22
// => ssh.example.com:22
// tcp+https://proxy.example.com/ssh.example.com:22
// => ssh.example.com:22
if strings.HasPrefix(u.Scheme, "tcp+") {
hosts := strings.Split(strings.TrimPrefix(u.Path, "/"), "/")
// if there are no domains in the path part of the URL, use the host
// otherwise use the path parts of the URL as the hosts
for _, hostport := range append(hosts, u.Host) {
if host, port, err := net.SplitHostPort(hostport); err == nil {
yield(host, port)
} else {
yield(hostport, "")
}
}
return
}
var defaultPort string
if u.Scheme == "http" {
defaultPort = "80"
} else {
defaultPort = "443"
}
// for hosts like 'example.com:1234' we only return one route
if host, port, err := net.SplitHostPort(u.Host); err == nil {
if port != defaultPort {
yield(host, port)
return
}
}
hostname := u.Hostname()
if !includeDefaultPort {
yield(hostname, "")
return
}
// for everything else we return two routes: 'example.com' and 'example.com:443'
if !yield(hostname, "") {
return
}
yield(hostname, defaultPort)
}
}
// Join joins elements of a URL with '/'.
func Join(elements ...string) string {
var builder strings.Builder