dns dig for golang
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE Initial commit May 11, 2016
README.md update md Dec 14, 2018
dig.go SetBackupDNS设置后查询DNS将同时向两个server发起请求,并响应第一个收到的msg Dec 14, 2018
dig_test.go SetBackupDNS Nov 29, 2018
go.mod go mod github.com/miekg/dns Nov 30, 2018
go.sum go mod github.com/miekg/dns Nov 30, 2018

README.md

Go Report Card

dnsutil

Golang DNS dig功能库

go get github.com/lixiangzhong/dnsutil
//Normal

package main

import (
	"fmt"
	"github.com/lixiangzhong/dnsutil"
)

func main() {
    var dig dnsutil.Dig 
    dig.SetDNS("8.8.8.8") //or ns.xxx.com 
    a, err := dig.A("google.com")  // dig google.com @8.8.8.8
    fmt.Println(a, err)
}
//msg

package main

import (
	"fmt"
	"github.com/lixiangzhong/dnsutil"
	"github.com/miekg/dns"
)

func main() {
	var dig dnsutil.Dig
	dig.SetDNS("1.1.1.1")
	msg, err := dig.GetMsg(dns.TypeA, "google.com")
	if err != nil {
		fmt.Println(err)
		return
	}
	// fmt.Println(msg)
	// or
	fmt.Println(msg.Question) //question section
	fmt.Println(msg.Answer)   //answer section.
	fmt.Println(msg.Ns)       //authority section.
	fmt.Println(msg.Extra)    //additional section.
}
//EDNS0ClientSubnet

package main

import (
	"fmt"
	"github.com/lixiangzhong/dnsutil"
)

func main() {
    var dig dnsutil.Dig
    dig.SetDNS("8.8.8.8") //or ns.xxx.com
    dig.SetEDNS0ClientSubnet("123.123.123.123")   //support edns0clientsubnet
    a, err := dig.A("google.com")  // dig google.com @8.8.8.8 +client=123.123.123.123
    fmt.Println(a, err)
}
//Retry

package main

import (
	"fmt"
	"github.com/lixiangzhong/dnsutil"
)

func main() {
    var dig dnsutil.Dig
    dig.Retry=3 //retry  when write or read message return error . defualt 1
    dig.SetDNS("8.8.8.8") //or ns.xxx.com 
    a, err := dig.A("google.com")  // dig google.com @8.8.8.8
    fmt.Println(a, err)
}
//dig +trace

package main

import (
	"fmt"
	"github.com/lixiangzhong/dnsutil"
)

func main() {
	domain := "google.com"
	var dig dnsutil.Dig
	rsps, err := dig.Trace(domain)  //dig +trace google.com
	if err != nil {
		fmt.Println(err)
		return
	}
	for _, rsp := range rsps {
		if rsp.Msg.Authoritative {
			for _, answer := range rsp.Msg.Answer {
				fmt.Println(answer)
			}
		}
		for _, ns := range rsp.Msg.Ns {
			fmt.Println(ns)
		}
		fmt.Println("\tReceived from", rsp.Server, rsp.ServerIP)
	}
}
//检查是否被污染

package main

import (
	"fmt"
	"github.com/lixiangzhong/dnsutil"
)

func main() {
	polluted, err := dnsutil.IsPolluted("facebook.com")
	if err != nil {
		fmt.Println(err)
		return
	}
	if polluted {
		fmt.Println("被污染,你懂的")
	} else {
		fmt.Println("正常")
	}
}
//SetBackupDNS
//同时向设置的2个DNS发起请求,返回最快响应的那个结果
//Initiate requests to two DNS settings at the same time, returning the fastest response result
package main

import (
	"fmt"
	"github.com/lixiangzhong/dnsutil"
)

func main() {
	var dig dnsutil.Dig
	dig.SetDNS("1.1.1.1")
	dig.SetBackupDNS("8.8.8.8")
	a, err := dig.A("google.com")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(a)
}