nslookup

package module
v0.0.0-...-c81566b Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: MIT Imports: 6 Imported by: 0

README

nslookup-go

This library let you do nslookup by UDP connection. You can choose the server, what you want to use or use default 8.8.8.8 server.

Supported query types

TypeA = 1 //IPv4
TypeNS = 2
TypeCNAME = 5
TypePTR = 12
TypeMX = 15
TypeTXT = 16
TyepAAAA= 28 //IPv6

Example usage

package main

import (
	"fmt"
	"log"

	"github.com/P1tirim/nslookup-go"
)

func main(){
    // Accept domain as first parametr and ip of DNS server as second.
    // You can not passing DNS server, then will be use standart 8.8.8.8 server
    // Return []net.IP
    ips, err := nslookup.LookupIP("google.com", "")
	if err != nil {
		log.Fatal(err)
	}

	for _, ip := range ips {
		fmt.Println("IP: " + ip.String())
	}
}

Another expamples you can find in the folder "examples"

Advanced usage

If you want to get answer from DNS server by DNS protocol message, then this exapmle for you

package main

import (
	"fmt"
	"log"

	"github.com/P1tirim/nslookup-go"
)

func main(){
    query := nslookup.NewQueryDNS("google.com", nslookup.TypeA)

    // This function return nslookup.Response
	resp, err := query.Lookup("8.8.8.8:53")
	if err != nil {
		log.Fatal(err)
	}

    // You need to assert every answer to struct
	for _, answer := range resp.Answers {
		if answer.Type == nslookup.TypeA {
			fmt.Println("IPv4: " + answer.Data.(nslookup.AnswerTypeString).Data)
		}
	}
}

The struct, what returns DNS server

type Response struct {
	TransactionID     uint16
	Flags             uint16
	QuestionsCount    uint16
	AnswersCount      uint16
	AuthorityCounts   uint16
	AdditionalCounts  uint16
	Queries           []Query
	Answers           []Answer
	AuthorityRecords  []Answer
	AdditionalRecords []Answer
}

Documentation

Index

Constants

View Source
const (
	// IPv4
	TypeA     = 1
	TypeNS    = 2
	TypeCNAME = 5
	TypePTR   = 12
	TypeMX    = 15
	TypeTXT   = 16
	// IPv6
	TypeAAAA = 28
)

Variables

View Source
var (
	ErrNoSuchName              error = errors.New("no such name")
	ErrNoAnswer                      = errors.New("no answer")
	ErrInvalidAnswerFromServer       = errors.New("invalid answer from server")
	ErrUnsupportedDNSType            = errors.New("unsupported dns type")
	ErrEmptyQueries                  = errors.New("empty queries in QueryDNS")
	ErrFormatQuery                   = errors.New("the query was constructed incorrectly")
	ErrInternalServerDNS             = errors.New("internal error in the DNS server")
	ErrRefused                       = errors.New("the query refused by DNS server")
	ErrYXDomain                      = errors.New("name exists when it should not")
	ErrYXRRSet                       = errors.New("RR Set Exists when it should not")
	ErrNXRRSet                       = errors.New("RR Set that should exist does not")
	ErrNotAuth                       = errors.New("not authorized")

	ErrNotValidIP = errors.New("not valid ip in arguments")
)

Functions

func LookupCNAME

func LookupCNAME(domain, server string) (cnames []string, err error)

Return array of CNAMEs of this domain.

func LookupIP

func LookupIP(domain, server string) (ips []net.IP, err error)

Return array of IPv4 and IPv6 of this domain.

func LookupIPv4

func LookupIPv4(domain, server string) (ips []net.IP, err error)

func LookupIPv6

func LookupIPv6(domain, server string) (ips []net.IP, err error)

func LookupNS

func LookupNS(domain, server string) (ns []string, err error)

func LookupPTR

func LookupPTR(ip net.IP, server string) (ptr []string, err error)

func LookupTEXT

func LookupTEXT(domain, server string) (texts []string, err error)

Return array of TEXTs of this domain.

Types

type Answer

type Answer struct {
	Name       uint16
	Type       uint16
	Class      uint16
	TimeToLive uint32
	DataLength uint16
	Data       interface{}
}

type AnswerTypeMX

type AnswerTypeMX struct {
	Preference   int
	MailExchange string
}

func LookupMX

func LookupMX(domain, server string) (mx []AnswerTypeMX, err error)

type AnswerTypeString

type AnswerTypeString struct {
	Data string
}

TypeA, TypeAAAA, TypeCNAME, TypeNS, TypePTR

type AnswerTypeTXT

type AnswerTypeTXT struct {
	TextLength int
	Text       string
}

type Query

type Query struct {
	Name  string
	Type  uint16
	Class uint16
}

type QueryDNS

type QueryDNS struct {
	TransactionID    uint16
	Flags            uint16
	QuestionsCount   uint16
	AnswersCount     uint16
	AuthorityCount   uint16
	AdditionalsCount uint16
	Queries          []Query
}

func NewQueryDNS

func NewQueryDNS(name string, dnsType uint16) *QueryDNS

func (*QueryDNS) Lookup

func (q *QueryDNS) Lookup(server string) (*Response, error)

Return response from DNS server in DNS message protocol format. Default server is 8.8.8.8:53.

type Response

type Response struct {
	TransactionID     uint16
	Flags             uint16
	QuestionsCount    uint16
	AnswersCount      uint16
	AuthorityCounts   uint16
	AdditionalCounts  uint16
	Queries           []Query
	Answers           []Answer
	AuthorityRecords  []Answer
	AdditionalRecords []Answer
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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