diff options
| author | MDK <[email protected]> | 2024-03-25 17:15:25 +0800 |
|---|---|---|
| committer | MDK <[email protected]> | 2024-03-25 17:15:25 +0800 |
| commit | 7115311c42ccd8a7565a5f8728bcaa18bf20bf02 (patch) | |
| tree | 48eaac3c73914db31600fa8c9e96d6ffbf22c0f6 /utils | |
programs for edns, svcb, https support measurement
Diffstat (limited to 'utils')
| -rw-r--r-- | utils/dns_utils.go | 62 | ||||
| -rw-r--r-- | utils/other_utils.go | 31 |
2 files changed, 93 insertions, 0 deletions
diff --git a/utils/dns_utils.go b/utils/dns_utils.go new file mode 100644 index 0000000..4a4131d --- /dev/null +++ b/utils/dns_utils.go @@ -0,0 +1,62 @@ +// dns utils +package utils + +import ( + "github.com/miekg/dns" +) + +type DNSOptions struct { + Domain string + RD bool + Qclass uint16 + Qtype uint16 + EDNS bool +} + +// build the question section of a dns packet +func questionMaker(domain string, qclass uint16, qtype uint16) *dns.Question { + return &dns.Question{Name: dns.Fqdn(domain), Qtype: qtype, Qclass: qclass} +} + +// build a specific query message +func queryMaker(domain string, rd bool, qclass uint16, qtype uint16, edns bool) *dns.Msg { + msg := new(dns.Msg) + msg.Id = dns.Id() + msg.RecursionDesired = rd + msg.Question = make([]dns.Question, 1) + msg.Question[0] = *questionMaker(domain, qclass, qtype) + if edns { + msg = msg.SetEdns0(1232, false) + } + return msg +} + +// query and receive the response +// addr must contain dest port +// func DNSQuery(addr string, domain string, rd bool, qclass uint16, qtype uint16, edns bool) (*dns.Msg, error) { +func DNSQuery(addr string, opt DNSOptions) (*dns.Msg, error) { + if opt.Qclass == 0 { + opt.Qclass = 1 + } + if opt.Qtype == 0 { + opt.Qtype = 1 + } + msg := queryMaker(opt.Domain, opt.RD, opt.Qclass, opt.Qtype, opt.EDNS) + res, err := dns.Exchange(msg, addr) + return res, err +} + +func AsyncDNSQuery(addr string, domain string, rd bool, qclass uint16, qtype uint16, edns bool) error { + msg := queryMaker(domain, rd, qclass, qtype, edns) + client := dns.Client{Net: "udp"} + conn, err := client.Dial(addr) + if err != nil { + return err + } + defer conn.Close() + err = conn.WriteMsg(msg) + if err != nil { + return err + } + return nil +} diff --git a/utils/other_utils.go b/utils/other_utils.go new file mode 100644 index 0000000..d0b360e --- /dev/null +++ b/utils/other_utils.go @@ -0,0 +1,31 @@ +package utils + +import ( + "bufio" + _ "encoding/json" + _ "fmt" + "io" + _ "net" + _ "net/http" + "os" + _ "strconv" + "strings" +) + +func RetrieveLines(pool chan string, filename string) { + f, err := os.Open(filename) + if err != nil { + panic(err) + } + //fmt.Println("reading file ...") + reader := bufio.NewReader(f) + for { + s, err := reader.ReadString('\n') + if err == io.EOF { + break + } + s = strings.Trim(s, "\n") + pool <- s + } + close(pool) +} |
