dns

package
v1.43.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package dns provides DNS lookup functionality with built-in caching support.

The package offers a simple API for resolving hostnames to IP addresses with automatic caching to improve performance and reduce DNS query load. It handles both raw hostnames and URLs, automatically extracting the hostname portion when needed.

Features:

  • Automatic caching with 1-hour TTL
  • Support for both hostnames and URLs
  • IPv4 address filtering
  • Direct IP address passthrough

Basic Usage:

// Lookup with caching (recommended)
ips, err := dns.CacheLookup("A", "example.com")
if err != nil {
	log.Fatal(err)
}
for _, ip := range ips {
	fmt.Printf("IP: %s\n", ip)
}

// Direct lookup without caching
ips, err := dns.Lookup("A", "https://example.com")
// Automatically extracts hostname from URL

The package prioritizes IPv4 addresses in results and handles IP addresses directly without performing DNS lookups.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CacheLookup

func CacheLookup(recordType, hostname string) ([]net.IP, error)

CacheLookup performs a DNS lookup with caching support. Results are cached for 1 hour to reduce DNS query load.

Parameters:

  • recordType: DNS record type (e.g., "A", "AAAA"). Currently not used but reserved for future use.
  • hostname: The hostname, URL, or IP address to resolve

Returns:

  • []net.IP: Slice of resolved IPv4 addresses
  • error: If the lookup fails or hostname is invalid

Example:

ips, err := CacheLookup("A", "google.com")
if err != nil {
	return err
}
fmt.Printf("Resolved IPs: %v\n", ips)

func Lookup

func Lookup(recordType, hostname string) ([]net.IP, error)

Lookup performs a direct DNS lookup without caching. It uses Go's default resolver to query DNS servers.

The function handles multiple input formats:

  • Raw hostnames: "example.com"
  • URLs: "https://example.com:8080/path" (extracts hostname)
  • IP addresses: "192.168.1.1" (returns immediately without lookup)

Only IPv4 addresses are returned in the result.

Parameters:

  • recordType: DNS record type (currently not used but reserved for future use)
  • hostname: The hostname, URL, or IP address to resolve

Returns:

  • []net.IP: Slice of resolved IPv4 addresses
  • error: If the lookup fails or hostname is invalid

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL