ExistsViaCoderConnect checks if the given hostname exists via Coder Connect. This doesn't guarantee the workspace is actually reachable, if, for example, its agent is unhealthy, but rather that Coder Connect knows about the workspace and advertises the hostname via DNS.
(ctx context.Context, hostname string)
| 434 | // workspace is actually reachable, if, for example, its agent is unhealthy, but rather that Coder Connect knows about |
| 435 | // the workspace and advertises the hostname via DNS. |
| 436 | func ExistsViaCoderConnect(ctx context.Context, hostname string) (bool, error) { |
| 437 | resolver := testOrDefaultResolver(ctx) |
| 438 | var dnsError *net.DNSError |
| 439 | ips, err := resolver.LookupIP(ctx, "ip6", hostname) |
| 440 | if xerrors.As(err, &dnsError) { |
| 441 | if dnsError.IsNotFound { |
| 442 | return false, nil |
| 443 | } |
| 444 | } |
| 445 | if err != nil { |
| 446 | return false, xerrors.Errorf("lookup DNS %s: %w", hostname, err) |
| 447 | } |
| 448 | |
| 449 | // The returned IP addresses are probably from the Coder Connect DNS server, but there are sometimes weird captive |
| 450 | // internet setups where the DNS server is configured to return an address for any IP query. So, to avoid false |
| 451 | // positives, check if we can find an address from our service prefix. |
| 452 | for _, ip := range ips { |
| 453 | addr, ok := netip.AddrFromSlice(ip) |
| 454 | if !ok { |
| 455 | continue |
| 456 | } |
| 457 | if tailnet.CoderServicePrefix.AsNetip().Contains(addr) { |
| 458 | return true, nil |
| 459 | } |
| 460 | } |
| 461 | return false, nil |
| 462 | } |