Skip to content

Commit 9f46179

Browse files
committed
feat: lookup wildcard domain with random sub
1 parent 2f943f3 commit 9f46179

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

internal/dnslookup/dnslookup.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
package dnslookup
22

33
import (
4+
"math/rand"
45
"net"
56
"strings"
67

78
"github.com/sunggun-yu/dnsq/internal/models"
89
)
910

11+
// randomHostname generates a random hostname
12+
func randomHostname() string {
13+
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
14+
length := 8
15+
// TODO: need to check length of hostname and see if it is over 255 long including the subdomain
16+
b := make([]byte, length)
17+
for i := range b {
18+
b[i] = charset[rand.Intn(len(charset))]
19+
}
20+
return string(b)
21+
}
22+
1023
// GetDNSRecords returns DNS records for a given hostname
1124
func GetDNSRecords(hostname string) []models.DNSRecord {
1225
var records []models.DNSRecord
1326
currentHost := hostname
27+
isWildcard := false
28+
29+
if strings.HasPrefix(hostname, "*.") {
30+
randomSubdomain := randomHostname()
31+
currentHost = randomSubdomain + hostname[1:]
32+
isWildcard = true
33+
}
1434

1535
// CNAME lookup
1636
cname, err := net.LookupCNAME(currentHost)
@@ -24,6 +44,12 @@ func GetDNSRecords(hostname string) []models.DNSRecord {
2444

2545
// A and AAAA lookup
2646
ips, err := net.LookupIP(currentHost)
47+
48+
// if it is a wildcard, set the original hostname to the records
49+
if isWildcard {
50+
currentHost = hostname
51+
}
52+
2753
if err == nil {
2854
for _, ip := range ips {
2955
if ipv4 := ip.To4(); ipv4 != nil {

0 commit comments

Comments
 (0)