-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
sub.go
41 lines (36 loc) · 1.12 KB
/
sub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright © by Jeff Foley 2021-2024. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// SPDX-License-Identifier: Apache-2.0
package resolve
import (
"context"
"strings"
"github.com/miekg/dns"
)
// FirstProperSubdomain returns the first subdomain name using the provided name and
// Resolver that responds successfully to a DNS query for the NS record type.
func FirstProperSubdomain(ctx context.Context, r *Resolvers, name string) string {
var domain string
// Obtain all parts of the subdomain name
labels := strings.Split(strings.TrimSpace(name), ".")
loop:
for i := 0; i < len(labels)-1; i++ {
sub := strings.Join(labels[i:], ".")
for i := 0; i < maxQueryAttempts; i++ {
resp, err := r.QueryBlocking(ctx, QueryMsg(sub, dns.TypeNS))
if err != nil || resp.Rcode == dns.RcodeNameError {
continue loop
}
if resp.Rcode == dns.RcodeSuccess {
if len(resp.Answer) == 0 {
continue loop
}
if d := AnswersByType(ExtractAnswers(resp), dns.TypeNS); len(d) > 0 {
domain = sub
break loop
}
}
}
}
return domain
}